def wrap_wsgi_app(app, use_legacy_context_mode=True): """Wrap a WSGI app with middlewares required to access App Engine APIs.""" from google.appengine.ext.vmruntime import middlewares from google.appengine.ext.vmruntime import vmstub vmstub.Register(vmstub.VMStub()) os.environ['APPLICATION_ID'] = os.environ['GAE_APPLICATION'] def if_legacy(array): return array if use_legacy_context_mode else [] return middlewares.Wrap( app, if_legacy([ middlewares.MakeInitLegacyRequestOsEnvironMiddleware(), ]) + [ middlewares.RunInNewContextMiddleware, middlewares.SetContextFromHeadersMiddleware, middlewares.CallbackMiddleware, middlewares.UseRequestSecurityTicketForApiMiddleware, middlewares.WaitForResponseMiddleware, middlewares.WsgiEnvSettingMiddleware, middlewares.MakeLegacyWsgiEnvSettingMiddleware(), ] + if_legacy([ middlewares.LegacyWsgiRemoveXAppenginePrefixMiddleware, middlewares.LegacyCopyWsgiEnvToOsEnvMiddleware, ]) + [ middlewares.ErrorLoggingMiddleware, ])
def CreateServer(self): with open(self.filename) as stream: appinfo_external = appinfo_includes.Parse(stream) appengine_config = vmconfig.BuildVmAppengineEnvConfig() vmstub.Register(vmstub.VMStub(appengine_config.default_ticket)) app = meta_app.FullyWrappedApp(appinfo_external, appengine_config) self.server = self.server_class(self.host, self.port, app, appinfo_external) logging.info('Configured server on %s:%s', self.host, self.port)
def VerifyApi(): """Verify that App Engine APIs are available. It keeps trying one of App Engine modules api (i.e. modules.get_num_instances()), until it gets valid results or timeout (2 minutes). """ stub = vmstub.VMStub() apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap(stub) start = time.time() while True: try: num_instances = modules.get_num_instances() print 'number of instances: ', num_instances sys.exit(0) except Exception as e: # pylint: disable=broad-except print 'API is not available yet because of exception:', e time.sleep(1) if time.time() > start + 900: sys.exit(1)
def CreateServer(self): with open(self.filename) as stream: appinfo_external = appinfo_includes.Parse(stream) appengine_config = vmconfig.BuildVmAppengineEnvConfig() vmstub.Register(vmstub.VMStub(appengine_config.default_ticket)) if 'googleclouddebugger' in sys.modules: try: googleclouddebugger.AttachDebugger() except Exception as e: logging.warn('Exception while initializing Cloud Debugger: %s', traceback.format_exc(e)) try: import appengine_config as user_appengine_config except ImportError: pass app = meta_app.FullyWrappedApp(appinfo_external, appengine_config) self.server = self.server_class(self.host, self.port, app, appinfo_external) logging.info('Configured server on %s:%s', self.host, self.port)
def wrap_wsgi_app(app): """Wrap a WSGI app with middlewares required to access App Engine APIs.""" from google.appengine.ext.vmruntime import middlewares from google.appengine.ext.vmruntime import vmstub vmstub.Register(vmstub.VMStub()) app = middlewares.ErrorLoggingMiddleware(app) app = middlewares.OsEnvSetupMiddleware(app) class FakeAppinfoExternal: """As we don't have an app.yaml to parse, just assume threadsafe.""" threadsafe = True app = middlewares.WsgiEnvSettingMiddleware(app, FakeAppinfoExternal) app = middlewares.WaitForResponseMiddleware(app) app = middlewares.UseRequestSecurityTicketForApiMiddleware(app) app = middlewares.CallbackMiddleware(app) app = middlewares.InitRequestEnvironMiddleware(app, copy_gae_application=True) return app
try: handler = cloud_logging.CloudLoggingHandler() root_logger.addHandler(handler) except IOError: # If the Cloud Logging endpoint does not exist, just use the default handler # instead. This will be the case when running in local dev mode. pass root_logger.setLevel(logging.INFO) # Fetch application configuration via the config file. appinfo = get_module_config(get_module_config_filename()) env_config = vmconfig.BuildVmAppengineEnvConfig() # Ensure API requests include a valid ticket by default. vmstub.Register(vmstub.VMStub(env_config.default_ticket)) # Take an immutable snapshot of env data from env_config. This is added to the # environment in `reset_environment_middleware` in a particular order to ensure # that it cannot be overridden by other steps. frozen_env_config_env = tuple( env_vars_from_env_config(env_config).iteritems()) # Also freeze user env vars specified in app.yaml. Later steps to modify the # environment such as env_vars_from_env_config and request middleware # will overwrite these changes. This is added to the environment in # `reset_environment_middleware`. frozen_user_env = tuple( user_env_vars_from_appinfo(appinfo).iteritems()) # While the primary use of the frozen env vars is for
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Python CLI for sending email. Currently does not handle attachments from google.appengine.api import mail import click import logging import json import os '''The below statements are required because we are not using the standard python runtime This will be fixed in the next launch but we have to live with it for now''' from google.appengine.ext.vmruntime import vmconfig from google.appengine.ext.vmruntime import vmstub vmstub.Register(vmstub.VMStub(vmconfig.BuildVmAppengineEnvConfig().default_ticket)) vmstub.app_is_loaded = True @click.command() @click.option('--to', help='users to receive the email') @click.option('--email_subject', help='email subject to be sent to users') @click.option('--email_body', help='email body to be sent to users') @click.option('--cc', help='cc users to receive the email') def send_mail(to, email_subject, email_body, cc): application_id = os.environ.get('GAE_LONG_APP_ID') message = mail.EmailMessage(sender="*****@*****.**", subject=email_subject) message.sender = "noreply@" + application_id + ".appspotmail.com" message.to = json.loads(to) message.body = email_body if cc: