def load_remote_project_zip(self, project_zip_path): """ Puts the project files from S3 in /tmp and adds to path """ project_folder = '/tmp/{0!s}'.format(self.settings.PROJECT_NAME) if not os.path.isdir(project_folder): # The project folder doesn't exist in this cold lambda, get it from S3 if not self.session: boto_session = boto3.Session() else: boto_session = self.session # Download the zip remote_bucket, remote_file = parse_s3_url(project_zip_path) s3 = boto_session.resource('s3') zip_path = '/tmp/{0!s}'.format(remote_file) s3.Object(remote_bucket, remote_file).download_file(zip_path) # Unzip contents to project folder with zipfile.ZipFile(zip_path, 'r') as z: z.extractall(path=project_folder) # Add to project path sys.path.insert(0, project_folder) # Change working directory to project folder # Related: https://github.com/Miserlou/Zappa/issues/702 os.chdir(project_folder) return True
def load_remote_project_archive(self, project_zip_path): """ Puts the project files from S3 in /tmp and adds to path """ project_folder = '/tmp/{0!s}'.format(self.settings.PROJECT_NAME) if not os.path.isdir(project_folder): # The project folder doesn't exist in this cold lambda, get it from S3 if not self.session: boto_session = boto3.Session() else: boto_session = self.session # Download zip file from S3 remote_bucket, remote_file = parse_s3_url(project_zip_path) s3 = boto_session.resource('s3') archive_on_s3 = s3.Object(remote_bucket, remote_file).get() with tarfile.open(fileobj=archive_on_s3['Body'], mode="r|gz") as t: t.extractall(project_folder) # Add to project path sys.path.insert(0, project_folder) # Change working directory to project folder # Related: https://github.com/Miserlou/Zappa/issues/702 os.chdir(project_folder) return True
def __init__(self, settings_name="zappa_settings", session=None): # We haven't cached our settings yet, load the settings and app. if not self.settings: # Loading settings from a python module self.settings = importlib.import_module(settings_name) self.settings_name = settings_name self.session = session # Custom log level if self.settings.LOG_LEVEL: level = logging.getLevelName(self.settings.LOG_LEVEL) logger.setLevel(level) remote_env = getattr(self.settings, 'REMOTE_ENV', None) remote_bucket, remote_file = parse_s3_url(remote_env) if remote_bucket and remote_file: self.load_remote_settings(remote_bucket, remote_file) # Let the system know that this will be a Lambda/Zappa/Stack os.environ["SERVERTYPE"] = "AWS Lambda" os.environ["FRAMEWORK"] = "Zappa" try: os.environ["PROJECT"] = self.settings.PROJECT_NAME os.environ["STAGE"] = self.settings.API_STAGE except Exception: # pragma: no cover pass # Set any locally defined env vars # Environement variable keys can't be Unicode # https://github.com/Miserlou/Zappa/issues/604 for key in self.settings.ENVIRONMENT_VARIABLES.keys(): os.environ[str(key)] = self.settings.ENVIRONMENT_VARIABLES[key] # Pulling from S3 if given a zip path project_zip_path = getattr(self.settings, 'ZIP_PATH', None) if project_zip_path: self.load_remote_project_zip(project_zip_path) # Load compliled library to the PythonPath # checks if we are the slim_handler since this is not needed otherwise # https://github.com/Miserlou/Zappa/issues/776 is_slim_handler = getattr(self.settings, 'SLIM_HANDLER', False) if is_slim_handler: included_libraries = getattr(self.settings, 'INCLUDE', ['libmysqlclient.so.18']) try: from ctypes import cdll, util for library in included_libraries: try: cdll.LoadLibrary(os.path.join(os.getcwd(), library)) except OSError: print ("Failed to find library...right filename?") except ImportError: print ("Failed to import cytpes library") # This is a non-WSGI application # https://github.com/Miserlou/Zappa/pull/748 if not hasattr(self.settings, 'APP_MODULE') and not self.settings.DJANGO_SETTINGS: self.app_module = None wsgi_app_function = None # This is probably a normal WSGI app elif not self.settings.DJANGO_SETTINGS: # The app module self.app_module = importlib.import_module(self.settings.APP_MODULE) # The application wsgi_app_function = getattr(self.app_module, self.settings.APP_FUNCTION) self.trailing_slash = False # Django gets special treatment. else: try: # Support both for tests from zappa.ext.django_zappa import get_django_wsgi except ImportError: # pragma: no cover from django_zappa_app import get_django_wsgi # Get the Django WSGI app from our extension wsgi_app_function = get_django_wsgi(self.settings.DJANGO_SETTINGS) self.trailing_slash = True self.wsgi_app = ZappaWSGIMiddleware(wsgi_app_function)
def __init__(self, settings_name="zappa_settings", session=None): # We haven't cached our settings yet, load the settings and app. if not self.settings: # Loading settings from a python module self.settings = importlib.import_module(settings_name) self.settings_name = settings_name self.session = session # Custom log level if self.settings.LOG_LEVEL: level = logging.getLevelName(self.settings.LOG_LEVEL) logger.setLevel(level) remote_env = getattr(self.settings, 'REMOTE_ENV', None) remote_bucket, remote_file = parse_s3_url(remote_env) if remote_bucket and remote_file: self.load_remote_settings(remote_bucket, remote_file) # Let the system know that this will be a Lambda/Zappa/Stack os.environ["SERVERTYPE"] = "AWS Lambda" os.environ["FRAMEWORK"] = "Zappa" try: os.environ["PROJECT"] = self.settings.PROJECT_NAME os.environ["STAGE"] = self.settings.API_STAGE except Exception: # pragma: no cover pass # Set any locally defined env vars # Environment variable keys can't be Unicode # https://github.com/Miserlou/Zappa/issues/604 for key in self.settings.ENVIRONMENT_VARIABLES.keys(): os.environ[str(key)] = self.settings.ENVIRONMENT_VARIABLES[key] # Pulling from S3 if given a zip path project_archive_path = getattr(self.settings, 'ARCHIVE_PATH', None) if project_archive_path: self.load_remote_project_archive(project_archive_path) # Load compliled library to the PythonPath # checks if we are the slim_handler since this is not needed otherwise # https://github.com/Miserlou/Zappa/issues/776 is_slim_handler = getattr(self.settings, 'SLIM_HANDLER', False) if is_slim_handler: included_libraries = getattr(self.settings, 'INCLUDE', ['libmysqlclient.so.18']) try: from ctypes import cdll, util for library in included_libraries: try: cdll.LoadLibrary(os.path.join( os.getcwd(), library)) except OSError: print("Failed to find library...right filename?") except ImportError: print("Failed to import cytpes library") # This is a non-WSGI application # https://github.com/Miserlou/Zappa/pull/748 if not hasattr(self.settings, 'APP_MODULE') and not self.settings.DJANGO_SETTINGS: self.app_module = None wsgi_app_function = None # This is probably a normal WSGI app elif not self.settings.DJANGO_SETTINGS: # The app module self.app_module = importlib.import_module( self.settings.APP_MODULE) # The application wsgi_app_function = getattr(self.app_module, self.settings.APP_FUNCTION) self.trailing_slash = False # Django gets special treatment. else: try: # Support both for tests from zappa.ext.django_zappa import get_django_wsgi except ImportError: # pragma: no cover from django_zappa_app import get_django_wsgi # Get the Django WSGI app from our extension wsgi_app_function = get_django_wsgi( self.settings.DJANGO_SETTINGS) self.trailing_slash = True self.wsgi_app = ZappaWSGIMiddleware(wsgi_app_function)