Пример #1
0
    def test_domain_name_match(self):
        # Simple sanity check
        zone = Zappa._get_best_match_zone(all_zones={ 'HostedZones': [
            {
                'Name': 'example.com.au.',
                'Id': 'zone-correct'
            }
        ]},
            domain='www.example.com.au')
        assert zone == 'zone-correct'

        # No match test
        zone = Zappa._get_best_match_zone(all_zones={'HostedZones': [
            {
                'Name': 'example.com.au.',
                'Id': 'zone-incorrect'
            }
        ]},
            domain='something-else.com.au')
        assert zone is None

        # More involved, better match should win.
        zone = Zappa._get_best_match_zone(all_zones={'HostedZones': [
            {
                'Name': 'example.com.au.',
                'Id': 'zone-incorrect'
            },
            {
                'Name': 'subdomain.example.com.au.',
                'Id': 'zone-correct'
            }
        ]},
            domain='www.subdomain.example.com.au')
        assert zone == 'zone-correct'
Пример #2
0
    def test_copy_editable_packages(self, mock_remove, mock_find_packages):
        temp_package_dir = '/var/folders/rn/9tj3_p0n1ln4q4jn1lgqy4br0000gn/T/1480455339'
        egg_links = [
            '/user/test/.virtualenvs/test/lib/python2.7/site-packages/package-python.egg-link'
        ]
        egg_path = "/some/other/directory/package"
        mock_find_packages.return_value = ["package", "package.subpackage", "package.another"]
        temp_egg_link = os.path.join(temp_package_dir, 'package-python.egg-link')

        z = Zappa()
        with nested(
                patch_open(), mock.patch('glob.glob'), mock.patch('zappa.zappa.copytree')
        ) as ((mock_open, mock_file), mock_glob, mock_copytree):
            # We read in the contents of the egg-link file
            mock_file.read.return_value = "{}\n.".format(egg_path)

            # we use glob.glob to get the egg-links in the temp packages directory
            mock_glob.return_value = [temp_egg_link]

            z.copy_editable_packages(egg_links, temp_package_dir)

            # make sure we copied the right directories
            mock_copytree.assert_called_with(
                os.path.join(egg_path, 'package'),
                os.path.join(temp_package_dir, 'package'),
                symlinks=False
            )
            self.assertEqual(mock_copytree.call_count, 1)

            # make sure it removes the egg-link from the temp packages directory
            mock_remove.assert_called_with(temp_egg_link)
            self.assertEqual(mock_remove.call_count, 1)
Пример #3
0
    def handle(self, *args, **options):

        if not options.has_key('environment'):
            print("You must call rollback with an environment name. \n python manage.py rollback <environment> <revisions>")
            return

        from django.conf import settings
        if not 'ZAPPA_SETTINGS' in dir(settings):
            print("Please define your ZAPPA_SETTINGS in your settings file before deploying.")
            return

        zappa_settings = settings.ZAPPA_SETTINGS

        # Set your configuration
        project_name = settings.BASE_DIR.split(os.sep)[-1]
        api_stage = options['environment'][0]
        if api_stage not in zappa_settings.keys():
            print("Please make sure that the environment '" + api_stage + "' is defined in your ZAPPA_SETTINGS in your settings file before deploying.")
            return
        lambda_name = project_name + '-' + api_stage

        revision = options['revision'][0]

        # Make your Zappa object
        zappa = Zappa()
        # Load your AWS credentials from ~/.aws/credentials
        zappa.load_credentials()

        print("Rolling back..")
        response = zappa.rollback_lambda_function_version(lambda_name, versions_back=revision)
        print("Done!")
Пример #4
0
    def test_domain_name_match(self):
        # Simple sanity check
        zone = Zappa._get_best_match_zone(all_zones={ 'HostedZones': [
            {
                'Name': 'example.com.au.',
                'Id': 'zone-correct'
            }
        ]},
            domain='www.example.com.au')
        assert zone == 'zone-correct'

        # No match test
        zone = Zappa._get_best_match_zone(all_zones={'HostedZones': [
            {
                'Name': 'example.com.au.',
                'Id': 'zone-incorrect'
            }
        ]},
            domain='something-else.com.au')
        assert zone is None

        # More involved, better match should win.
        zone = Zappa._get_best_match_zone(all_zones={'HostedZones': [
            {
                'Name': 'example.com.au.',
                'Id': 'zone-incorrect'
            },
            {
                'Name': 'subdomain.example.com.au.',
                'Id': 'zone-correct'
            }
        ]},
            domain='www.subdomain.example.com.au')
        assert zone == 'zone-correct'
Пример #5
0
    def test_load_credentials(self):
        z = Zappa()
        z.aws_region = 'us-east-1'
        z.load_credentials()
        self.assertEqual(z.boto_session.region_name, 'us-east-1')
        self.assertEqual(z.aws_region, 'us-east-1')

        z.aws_region = 'eu-west-1'
        z.load_credentials()
        self.assertEqual(z.boto_session.region_name, 'eu-west-1')
        self.assertEqual(z.aws_region, 'eu-west-1')

        creds = {
            'AWS_ACCESS_KEY_ID': 'AK123',
            'AWS_SECRET_ACCESS_KEY': 'JKL456',
            'AWS_DEFAULT_REGION': 'us-west-1'
        }
        with mock.patch.dict('os.environ', creds):
            z.aws_region = None
            z.load_credentials()
            loaded_creds = z.boto_session._session.get_credentials()

        self.assertEqual(loaded_creds.access_key, 'AK123')
        self.assertEqual(loaded_creds.secret_key, 'JKL456')
        self.assertEqual(z.boto_session.region_name, 'us-west-1')
Пример #6
0
    def test_invoke_lambda_function(self, session):
        z = Zappa(session)
        z.credentials_arn = 'arn:aws:iam::724336686645:role/ZappaLambdaExecution'

        function_name = 'django-helloworld-unicode'
        payload = '{"event": "hello"}'
        response = z.invoke_lambda_function(function_name, payload)
Пример #7
0
    def test_invoke_lambda_function(self, session):
        z = Zappa(session)
        z.credentials_arn = 'arn:aws:iam::724336686645:role/ZappaLambdaExecution'

        function_name = 'django-helloworld-unicode'
        payload = '{"event": "hello"}'
        response = z.invoke_lambda_function(function_name, payload)
Пример #8
0
 def test_event_name(self):
     zappa = Zappa()
     truncated = zappa.get_event_name("basldfkjalsdkfjalsdkfjaslkdfjalsdkfjadlsfkjasdlfkjasdlfkjasdflkjasdf-asdfasdfasdfasdfasdf", "this.is.my.dang.function.wassup.yeah.its.long")
     self.assertTrue(len(truncated) <= 64)
     truncated = zappa.get_event_name("basldfkjalsdkfjalsdkfjaslkdfjalsdkfjadlsfkjasdlfkjasdlfkjasdflkjasdf-asdfasdfasdfasdfasdf", "thisidoasdfaljksdfalskdjfalsdkfjasldkfjalsdkfjalsdkfjalsdfkjalasdfasdfasdfasdklfjasldkfjalsdkjfaslkdfjasldkfjasdflkjdasfskdj")
     self.assertTrue(len(truncated) <= 64)
     truncated = zappa.get_event_name("a", "b")
     self.assertTrue(len(truncated) <= 64)
Пример #9
0
    def test_rollback_lambda_function_version(self, session):
        z = Zappa(session)
        z.credentials_arn = 'arn:aws:iam::724336686645:role/ZappaLambdaExecution'

        function_name = 'django-helloworld-unicode'
        too_many_versions = z.rollback_lambda_function_version(function_name, 99999)
        self.assertFalse(too_many_versions)

        function_arn = z.rollback_lambda_function_version(function_name, 1)
Пример #10
0
    def test_rollback_lambda_function_version(self, session):
        z = Zappa(session)
        z.credentials_arn = 'arn:aws:iam::724336686645:role/ZappaLambdaExecution'

        function_name = 'django-helloworld-unicode'
        too_many_versions = z.rollback_lambda_function_version(function_name, 99999)
        self.assertFalse(too_many_versions)

        function_arn = z.rollback_lambda_function_version(function_name, 1)
Пример #11
0
 def test_create_lambda_package(self):
     # mock the pip.get_installed_distributions() to include a package in lambda_packages so that the code
     # for zipping pre-compiled packages gets called
     mock_named_tuple = collections.namedtuple('mock_named_tuple', ['project_name'])
     mock_return_val = [mock_named_tuple(lambda_packages.keys()[0])]  # choose name of 1st package in lambda_packages
     with mock.patch('pip.get_installed_distributions', return_value=mock_return_val):
         z = Zappa()
         path = z.create_lambda_zip(handler_file=os.path.realpath(__file__))
         self.assertTrue(os.path.isfile(path))
         os.remove(path)
Пример #12
0
 def test_create_lambda_package(self):
     # mock the pip.get_installed_distributions() to include a package in lambda_packages so that the code
     # for zipping pre-compiled packages gets called
     mock_named_tuple = collections.namedtuple('mock_named_tuple', ['project_name'])
     mock_return_val = [mock_named_tuple(lambda_packages.keys()[0])]  # choose name of 1st package in lambda_packages
     with mock.patch('pip.get_installed_distributions', return_value=mock_return_val):
         z = Zappa()
         path = z.create_lambda_zip(handler_file=os.path.realpath(__file__))
         self.assertTrue(os.path.isfile(path))
         os.remove(path)
Пример #13
0
    def handle(self, *args, **options):
        """
        Execute the command.

        """
        if not options.has_key('environment'):
            print("You must call deploy with an environment name. \n python manage.py deploy <environment>")
            return

        from django.conf import settings
        if not 'ZAPPA_SETTINGS' in dir(settings):
            print("Please define your ZAPPA_SETTINGS in your settings file before deploying.")
            return

        zappa_settings = settings.ZAPPA_SETTINGS

        # Set your configuration
        project_name = settings.BASE_DIR.split(os.sep)[-1]
        api_stage = options['environment'][0]
        if api_stage not in zappa_settings.keys():
            print("Please make sure that the environment '" + api_stage + "' is defined in your ZAPPA_SETTINGS in your settings file before deploying.")
            return
        lambda_name = project_name + '-' + api_stage

        # Make your Zappa object
        zappa = Zappa()

        # Load your AWS credentials from ~/.aws/credentials
        zappa.load_credentials()

        try:
            # Tail the available logs
            all_logs = zappa.fetch_logs(lambda_name)
            self.print_logs(all_logs)

        # Keep polling, and print any new logs.
            while True:
                all_logs_again = zappa.fetch_logs(lambda_name)
                new_logs = []
                for log in all_logs_again:
                    if log not in all_logs:
                        new_logs.append(log)

                self.print_logs(new_logs)
                all_logs = all_logs + new_logs
        except KeyboardInterrupt:
            # Die gracefully
            try:
                sys.exit(0)
            except SystemExit:
                os._exit(0)

        return
Пример #14
0
    def handle(self, *args, **options):
        """
        Execute the command.

        """

        if not options.has_key('environment') or len(
                options['environment']) < 2:
            print(
                "You must call deploy with an environment name and command. \n python manage.py invoke <environment> <command>"
            )
            return

        from django.conf import settings
        if not 'ZAPPA_SETTINGS' in dir(settings):
            print(
                "Please define your ZAPPA_SETTINGS in your settings file before deploying."
            )
            return

        zappa_settings = settings.ZAPPA_SETTINGS

        # Set your configuration
        project_name = settings.BASE_DIR.split(os.sep)[-1]
        api_stage = options['environment'][0]
        if api_stage not in zappa_settings.keys():
            print(
                "Please make sure that the environment '" + api_stage +
                "' is defined in your ZAPPA_SETTINGS in your settings file before deploying."
            )
            return

        lambda_name = project_name + '-' + api_stage

        # Make your Zappa object
        zappa = Zappa()

        # Invoke it!
        command = {"command": ' '.join(options['environment'][1:])}
        response = zappa.invoke_lambda_function(
            lambda_name,
            json.dumps(command),
            invocation_type='RequestResponse')

        if response.has_key('LogResult'):
            print(base64.b64decode(response['LogResult']))
        else:
            print(response)
            import pdb
            pdb.set_trace()

        return
Пример #15
0
 def test_create_api_gateway_routes(self, session):
     z = Zappa(session)
     z.parameter_depth = 1
     z.integration_response_codes = [200]
     z.method_response_codes = [200]
     z.http_methods = ['GET']
     z.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution'
     lambda_arn = 'arn:aws:lambda:us-east-1:12345:function:helloworld'
     z.create_api_gateway_routes(lambda_arn)
Пример #16
0
    def test_deploy_api_gateway(self, session):
        z = Zappa(session)
        z.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution'

        z.parameter_depth = 1
        z.integration_response_codes = [200]
        z.method_response_codes = [200]
        z.http_methods = ['GET']

        lambda_arn = 'arn:aws:lambda:us-east-1:12345:function:django-helloworld-unicode'
        api_id = z.create_api_gateway_routes(lambda_arn)
        endpoint_url = z.deploy_api_gateway(api_id, "test_stage")
Пример #17
0
 def test_create_api_gateway_routes(self, session):
     z = Zappa(session)
     z.parameter_depth = 1
     z.integration_response_codes = [200]
     z.method_response_codes = [200]
     z.http_methods = ['GET']
     z.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution'
     lambda_arn = 'arn:aws:lambda:us-east-1:12345:function:helloworld'
     with mock.patch('time.time', return_value=123.456):
         api_id = z.create_api_gateway_routes(lambda_arn)
     self.assertEqual(api_id, 'j27idab94h')
Пример #18
0
    def test_upload_remove_s3(self, session):
        bucket_name = 'test_zappa_upload_s3'
        z = Zappa(session)
        zip_path = z.create_lambda_zip()
        res = z.upload_to_s3(zip_path, bucket_name)
        os.remove(zip_path)
        self.assertTrue(res)
        s3 = session.resource('s3')

        # will throw ClientError with 404 if bucket doesn't exist
        s3.meta.client.head_bucket(Bucket=bucket_name)

        # will throw ClientError with 404 if object doesn't exist
        s3.meta.client.head_object(
            Bucket=bucket_name,
            Key=zip_path,
        )
        res = z.remove_from_s3(zip_path, bucket_name)
        self.assertTrue(res)
Пример #19
0
    def test_deploy_api_gateway(self, session):
        z = Zappa(session)
        z.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution'

        z.parameter_depth = 1
        z.integration_response_codes = [200]
        z.method_response_codes = [200]
        z.http_methods = ['GET']

        lambda_arn = 'arn:aws:lambda:us-east-1:12345:function:django-helloworld-unicode'
        api_id = z.create_api_gateway_routes(lambda_arn)
        endpoint_url = z.deploy_api_gateway(api_id, "test_stage")
Пример #20
0
    def handle(self, *args, **options):
        """
        Execute the command.

        """

        if not options.has_key('environment') or len(options['environment']) < 2:
            print("You must call deploy with an environment name and command. \n python manage.py invoke <environment> <command>")
            return

        from django.conf import settings
        if not 'ZAPPA_SETTINGS' in dir(settings):
            print("Please define your ZAPPA_SETTINGS in your settings file before deploying.")
            return

        zappa_settings = settings.ZAPPA_SETTINGS

        # Set your configuration
        project_name = settings.BASE_DIR.split(os.sep)[-1]
        api_stage = options['environment'][0]
        if api_stage not in zappa_settings.keys():
            print("Please make sure that the environment '" + api_stage + "' is defined in your ZAPPA_SETTINGS in your settings file before deploying.")
            return

        lambda_name = project_name + '-' + api_stage

        # Make your Zappa object
        zappa = Zappa()

        # Invoke it!
        command = {"command": ' '.join(options['environment'][1:])}
        response = zappa.invoke_lambda_function(lambda_name, json.dumps(command), invocation_type='RequestResponse')

        if response.has_key('LogResult'):
            print(base64.b64decode(response['LogResult']))
        else:
            print(response)
            import pdb
            pdb.set_trace()

        return
Пример #21
0
    def test_b64_pattern(self):
        head = '<!DOCTYPE html>'

        for code in ['400', '401', '402', '403', '404', '500']:
            pattern = Zappa.selection_pattern(code)

            document = base64.b64encode(head + code + random_string(50))
            self.assertRegexpMatches(document, pattern)

            for bad_code in ['200', '301', '302']:
                document = base64.b64encode(head + bad_code + random_string(50))
                self.assertNotRegexpMatches(document, pattern)
Пример #22
0
    def test_b64_pattern(self):
        head = '<!DOCTYPE html>'

        for code in ['400', '401', '402', '403', '404', '500']:
            pattern = Zappa.selection_pattern(code)

            document = base64.b64encode(head + code + random_string(50))
            self.assertRegexpMatches(document, pattern)

            for bad_code in ['200', '301', '302']:
                document = base64.b64encode(head + bad_code + random_string(50))
                self.assertNotRegexpMatches(document, pattern)
Пример #23
0
    def test_load_credentials(self):
        z = Zappa()

        credentials = '[default]\naws_access_key_id = AK123\naws_secret_access_key = JKL456'
        config = '[default]\noutput = json\nregion = us-east-1'

        credentials_file = open('credentials','w')
        credentials_file.write(credentials) 
        credentials_file.close()

        config_file = open('config','w')
        config_file.write(config) 
        config_file.close()

        z.load_credentials('credentials', 'config')

        os.remove('credentials')
        os.remove('config')

        self.assertTrue((z.access_key == "AK123"))
        self.assertTrue((z.secret_key == "JKL456"))
        self.assertTrue((z.aws_region == 'us-east-1'))
Пример #24
0
 def test_create_api_gateway_routes(self, session):
     z = Zappa(session)
     z.parameter_depth = 1
     z.integration_response_codes = [200]
     z.method_response_codes = [200]
     z.http_methods = ['GET']
     z.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution'
     lambda_arn = 'arn:aws:lambda:us-east-1:12345:function:helloworld'
     with mock.patch('time.time', return_value=123.456):
         api_id = z.create_api_gateway_routes(lambda_arn)
     self.assertEqual(api_id, 'j27idab94h')
Пример #25
0
    def test_load_credentials(self):
        z = Zappa()

        credentials = '[default]\naws_access_key_id = AK123\naws_secret_access_key = JKL456'
        config = '[default]\noutput = json\nregion = us-east-1'

        credentials_file = open('credentials', 'w')
        credentials_file.write(credentials)
        credentials_file.close()

        config_file = open('config', 'w')
        config_file.write(config)
        config_file.close()

        z.load_credentials('credentials', 'config')

        os.remove('credentials')
        os.remove('config')

        self.assertTrue((z.access_key == "AK123"))
        self.assertTrue((z.secret_key == "JKL456"))
        self.assertTrue((z.aws_region == 'us-east-1'))
Пример #26
0
    def test_redirect_pattern(self):
        test_urls = [
            # a regular endpoint url
            'https://asdf1234.execute-api.us-east-1.amazonaws.com/env/path/to/thing',
            # an external url (outside AWS)
            'https://github.com/Miserlou/zappa/issues?q=is%3Aissue+is%3Aclosed',
            # a local url
            '/env/path/to/thing'
        ]

        for code in ['301', '302']:
            pattern = Zappa.selection_pattern(code)

            for url in test_urls:
                self.assertRegexpMatches(url, pattern)
Пример #27
0
    def test_redirect_pattern(self):
        test_urls = [
            # a regular endpoint url
            'https://asdf1234.execute-api.us-east-1.amazonaws.com/env/path/to/thing',
            # an external url (outside AWS)
            'https://github.com/Miserlou/zappa/issues?q=is%3Aissue+is%3Aclosed',
            # a local url
            '/env/path/to/thing'
        ]

        for code in ['301', '302']:
            pattern = Zappa.selection_pattern(code)

            for url in test_urls:
                self.assertRegexpMatches(url, pattern)
Пример #28
0
    def test_get_manylinux(self):
        z = Zappa()
        self.assertNotEqual(z.get_manylinux_wheel('pandas'), None)
        self.assertEqual(z.get_manylinux_wheel('derpderpderpderp'), None)

        # mock the pip.get_installed_distributions() to include a package in manylinux so that the code
        # for zipping pre-compiled packages gets called
        mock_named_tuple = collections.namedtuple('mock_named_tuple', ['project_name'])
        mock_return_val = [mock_named_tuple('pandas')]
        with mock.patch('pip.get_installed_distributions', return_value=mock_return_val):
            z = Zappa()
            path = z.create_lambda_zip(handler_file=os.path.realpath(__file__))
            self.assertTrue(os.path.isfile(path))
            os.remove(path)
Пример #29
0
    def test_create_lambda_function(self, session):
        bucket_name = 'lmbda'
        zip_path = 'Spheres-dev-1454694878.zip'

        z = Zappa(session)
        z.aws_region = 'us-east-1'
        z.load_credentials(session)
        z.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution'

        arn = z.create_lambda_function(bucket=bucket_name,
                                       s3_key=zip_path,
                                       function_name='test_lmbda_function55',
                                       handler='runme.lambda_handler')

        arn = z.update_lambda_function(
            bucket=bucket_name,
            s3_key=zip_path,
            function_name='test_lmbda_function55',
        )
Пример #30
0
def _init(environment, zappa_settings):
    """

    """
    # Make your Zappa object
    zappa = Zappa()

    # Load settings and apply them to the Zappa object
    settings = apply_zappa_settings(zappa, zappa_settings, environment)

    # Create the Lambda zip package (includes project and virtualenvironment)
    # Also define the path the handler file so it can be copied to the zip root
    # for Lambda.
    module_dir = os.path.dirname(os.path.abspath(flask_zappa.__file__))
    handler_file = os.path.join(module_dir, 'handler.py')
    lambda_name = settings['project_name'] + '-' + environment

    return zappa, settings, handler_file, lambda_name
Пример #31
0
    def test_create_lambda_function(self, session):
        bucket_name = 'lmbda'
        zip_path = 'Spheres-dev-1454694878.zip'

        z = Zappa(session)
        z.aws_region = 'us-east-1'
        z.load_credentials(session)
        z.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution'

        arn = z.create_lambda_function(
            bucket=bucket_name, 
            s3_key=zip_path, 
            function_name='test_lmbda_function55', 
            handler='runme.lambda_handler'
        )

        arn = z.update_lambda_function(
            bucket=bucket_name, 
            s3_key=zip_path, 
            function_name='test_lmbda_function55', 
        )
Пример #32
0
    def test_upload_remove_s3(self, session):
        bucket_name = 'test_zappa_upload_s3'
        z = Zappa(session)
        zip_path = z.create_lambda_zip(minify=False)
        res = z.upload_to_s3(zip_path, bucket_name)
        os.remove(zip_path)
        self.assertTrue(res)
        s3 = session.resource('s3')

        # will throw ClientError with 404 if bucket doesn't exist
        s3.meta.client.head_bucket(Bucket=bucket_name)

        # will throw ClientError with 404 if object doesn't exist
        s3.meta.client.head_object(
            Bucket=bucket_name,
            Key=zip_path,
        )
        res = z.remove_from_s3(zip_path, bucket_name)
        self.assertTrue(res)

        fail = z.upload_to_s3('/tmp/this_isnt_real', bucket_name)
        self.assertFalse(fail)
Пример #33
0
 def test_human_units(self):
     zappa = Zappa()
     zappa.human_size(1)
     zappa.human_size(9999999999999)
Пример #34
0
    def test_load_credentials(self):
        z = Zappa()
        z.aws_region = 'us-east-1'
        z.load_credentials()
        self.assertEqual(z.boto_session.region_name, 'us-east-1')
        self.assertEqual(z.aws_region, 'us-east-1')

        z.aws_region = 'eu-west-1'
        z.profile_name = 'default'
        z.load_credentials()
        self.assertEqual(z.boto_session.region_name, 'eu-west-1')
        self.assertEqual(z.aws_region, 'eu-west-1')

        creds = {
            'AWS_ACCESS_KEY_ID': 'AK123',
            'AWS_SECRET_ACCESS_KEY': 'JKL456',
            'AWS_DEFAULT_REGION': 'us-west-1'
        }
        with mock.patch.dict('os.environ', creds):
            z.aws_region = None
            z.load_credentials()
            loaded_creds = z.boto_session._session.get_credentials()

        self.assertEqual(loaded_creds.access_key, 'AK123')
        self.assertEqual(loaded_creds.secret_key, 'JKL456')
        self.assertEqual(z.boto_session.region_name, 'us-west-1')
Пример #35
0
 def test_zappa(self):
     self.assertTrue(True)
     Zappa()
Пример #36
0
    def handle(self, *args, **options):
        """
        Execute the command.

        """
        if not options.has_key("environment"):
            print("You must call deploy with an environment name. \n python manage.py deploy <environment>")
            return

        from django.conf import settings

        if not "ZAPPA_SETTINGS" in dir(settings):
            print("Please define your ZAPPA_SETTINGS in your settings file before deploying.")
            return

        zappa_settings = settings.ZAPPA_SETTINGS

        # Set your configuration
        project_name = settings.BASE_DIR.split(os.sep)[-1]
        api_stage = options["environment"][0]
        if api_stage not in zappa_settings.keys():
            print(
                "Please make sure that the environment '"
                + api_stage
                + "' is defined in your ZAPPA_SETTINGS in your settings file before deploying."
            )
            return

        # Make your Zappa object
        zappa = Zappa()

        # Load environment-specific settings
        s3_bucket_name = zappa_settings[api_stage]["s3_bucket"]
        settings_file = zappa_settings[api_stage]["settings_file"]
        if "~" in settings_file:
            settings_file = settings_file.replace("~", os.path.expanduser("~"))
        if not os.path.isfile(settings_file):
            print("Please make sure your settings_file is properly defined.")
            return

        custom_settings = [
            "http_methods",
            "parameter_depth",
            "integration_response_codes",
            "method_response_codes",
            "role_name",
            "aws_region",
        ]
        for setting in custom_settings:
            if zappa_settings[api_stage].has_key(setting):
                setattr(zappa, setting, zappa_settings[api_stage][setting])

        # Load your AWS credentials from ~/.aws/credentials
        zappa.load_credentials()

        # Create the Lambda zip package (includes project and virtualenvironment)
        # Also define the path the handler file so it can be copied to the zip root for Lambda.
        current_file = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
        handler_file = os.sep.join(current_file.split(os.sep)[0:-2]) + os.sep + "handler.py"
        lambda_name = project_name + "-" + api_stage
        zip_path = zappa.create_lambda_zip(lambda_name, handler_file=handler_file)

        # Add this environment's Django settings to that zipfile
        with open(settings_file, "r") as f:
            contents = f.read()
            all_contents = contents
            if not zappa_settings[api_stage].has_key("domain"):
                script_name = api_stage
            else:
                script_name = ""

            if not "ZappaMiddleware" in all_contents:
                print("\n\nWARNING!\n")
                print("You do not have ZappaMiddleware in your remote settings's MIDDLEWARE_CLASSES.\n")
                print("This means that some aspects of your application may not work!\n\n")

            all_contents = all_contents + "\n# Automatically added by Zappa:\nSCRIPT_NAME='/" + script_name + "'\n"
            f.close()

        with open("zappa_settings.py", "w") as f:
            f.write(all_contents)

        with zipfile.ZipFile(zip_path, "a") as lambda_zip:
            lambda_zip.write("zappa_settings.py", "zappa_settings.py")
            lambda_zip.close()

        os.unlink("zappa_settings.py")

        # Upload it to S3
        zip_arn = zappa.upload_to_s3(zip_path, s3_bucket_name)

        # Register the Lambda function with that zip as the source
        # You'll also need to define the path to your lambda_handler code.
        lambda_arn = zappa.update_lambda_function(s3_bucket_name, zip_path, lambda_name)

        # Finally, delete the local copy our zip package
        if zappa_settings[api_stage].get("delete_zip", True):
            os.remove(zip_path)

        # Remove the uploaded zip from S3, because it is now registered..
        zappa.remove_from_s3(zip_path, s3_bucket_name)

        print("Your updated Zappa deployment is live!")

        return
Пример #37
0
 def __init__(self, *args, **kwargs):
     super(ZappaCommand, self).__init__(*args, **kwargs)
     self.zappa = Zappa()
Пример #38
0
 def test_schedule_events(self):
     z = Zappa()
     import os
     path = os.getcwd()
     print path
     z.schedule_events(path=path)
Пример #39
0
 def test_create_iam_roles(self, session):
     z = Zappa(session)
     arn = z.create_iam_roles()
     self.assertEqual(arn, "arn:aws:iam::123:role/{}".format(z.role_name))
Пример #40
0
 def test_fetch_logs(self, session):
     z = Zappa(session)
     z.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution'
     events = z.fetch_logs('Spheres-demonstration')
     self.assertTrue(events is not None)
Пример #41
0
 def test_get_api_url(self, session):
     z = Zappa(session)
     z.credentials_arn = 'arn:aws:iam::724336686645:role/ZappaLambdaExecution'
     url = z.get_api_url('Spheres-demonstration', 'demonstration')
Пример #42
0
 def test_create_lambda_package(self):
     self.assertTrue(True)
     z = Zappa()
     path = z.create_lambda_zip(handler_file=os.path.realpath(__file__))
     self.assertTrue(os.path.isfile(path))
     os.remove(path)
Пример #43
0
class ZappaCommand(BaseCommand):
    # Management command
    can_import_settings = True
    requires_system_checks = False

    # Zappa settings
    zappa = None
    zappa_settings = None
    api_stage = None
    project_name = None
    lambda_name = None
    api_name = None
    s3_bucket_name = None
    settings_file = None
    zip_path = None
    vpc_config = None
    memory_size = None
    timeout = None

    help = '''Deploy this project to AWS with Zappa.'''

    def add_arguments(self, parser):
        parser.add_argument('environment', nargs='+', type=str)

    def __init__(self, *args, **kwargs):
        super(ZappaCommand, self).__init__(*args, **kwargs)
        self.zappa = Zappa()

    def require_settings(self, args, options):
        """
        Load the ZAPPA_SETTINGS as we expect it.

        """

        if not options.has_key('environment'):
            print(
                "You must call deploy with an environment name. \n python manage.py deploy <environment>")
            raise ImproperlyConfigured

        from django.conf import settings
        if not 'ZAPPA_SETTINGS' in dir(settings):
            print(
                "Please define your ZAPPA_SETTINGS in your settings file before deploying.")
            raise ImproperlyConfigured

        self.zappa_settings = settings.ZAPPA_SETTINGS

        # Set your configuration
        self.project_name = os.path.abspath(settings.BASE_DIR).split(os.sep)[-1]
        if type(options['environment']) == list:
            self.api_stage = options['environment'][0]
        else:
            self.api_stage = options['environment']
        self.lambda_name = self.zappa_settings[self.api_stage].get('lambda_function', slugify(
            self.project_name + '-' + self.api_stage).replace("_", "-"))
        if self.api_stage not in self.zappa_settings.keys():
            print("Please make sure that the environment '" + self.api_stage +
                  "' is defined in your ZAPPA_SETTINGS in your settings file before deploying.")
            raise ImproperlyConfigured
        self.api_name = self.zappa_settings[self.api_stage].get('api_name', self.lambda_name)
        # Load environment-specific settings
        self.s3_bucket_name = self.zappa_settings[self.api_stage]['s3_bucket']
        self.vpc_config = self.zappa_settings[
            self.api_stage].get('vpc_config', {})
        self.memory_size = self.zappa_settings[
            self.api_stage].get('memory_size', 512)
        self.timeout = self.zappa_settings[
            self.api_stage].get('timeout', 30)

        custom_settings = [
            'http_methods',
            'parameter_depth',
            'integration_response_codes',
            'method_response_codes',
            'role_name',
            'aws_region'
        ]
        for setting in custom_settings:
            if self.zappa_settings[self.api_stage].has_key(setting):
                setattr(self.zappa, setting, self.zappa_settings[
                    self.api_stage][setting])

    def get_django_settings_file(self):
        if not self.get_settings_location().startswith('s3://'):
            self.settings_file = self.zappa_settings[
                self.api_stage]['settings_file']
            if '~' in self.settings_file:
                self.settings_file = self.settings_file.replace(
                    '~', os.path.expanduser('~'))
            self.check_settings_file()
        else:
            self.settings_file = self.download_from_s3(*self.parse_s3_url(self.get_settings_location()))
            self.check_settings_file()

    def check_settings_file(self):
        """
        Checks whether the settings file specified is actually a file.
        """
        if not os.path.isfile(self.settings_file):
            print("Please make sure your settings_file is properly defined.")
            raise ImproperlyConfigured

    def get_settings_location(self):
        """
        Returns the value of the settings file location as specified in the
        json file.
        :return:
        """
        return self.zappa_settings[self.api_stage]['settings_file']

    def download_from_s3(self, bucket_name, s3_key,
                         output_filename='temp_zappa_settings.py'):
        """
        Download a file from S3
        :param bucket_name: Name of the S3 bucket (string)
        :param s3_key: Name of the file hosted on S3 (string)
        :param output_filename: Name of the file the download operation
        will create (string)
        :return: False or the value of output_filename
        """
        s3 = boto3.resource('s3')
        bucket = s3.Bucket(bucket_name)
        try:
            s3.meta.client.head_object(Bucket=bucket_name, Key=s3_key)
        except botocore.exceptions.ClientError:
            return False
        print(u'Downloading the settings file ({0}) from S3'.format(s3_key))
        new_file = bucket.download_file(s3_key, output_filename)
        return output_filename

    def parse_s3_url(self, s3_url):
        """
        Parse the S3 url. Format: s3://mybucket:path/to/my/key
        Example: s3://settings-bucket:/production_settings.py
        :param s3_url: Path to the file hosted on S3
        :return:
        """
        return s3_url.replace('s3://', '').split(':')

    def load_credentials(self):
        session = None
        profile_name = self.zappa_settings[self.api_stage].get('profile_name')
        region_name = self.zappa_settings[self.api_stage].get('aws_region')
        if profile_name is not None:
            session = boto3.Session(profile_name=profile_name, region_name=region_name)
        self.zappa.load_credentials(session)

    def create_package(self):
        """
        Ensure that the package can be properly configured,
        and then create it.

        """

        # Create the Lambda zip package (includes project and virtualenvironment)
        # Also define the path the handler file so it can be copied to the zip
        # root for Lambda.
        current_file = os.path.dirname(os.path.abspath(
            inspect.getfile(inspect.currentframe())))
        handler_file = os.sep.join(current_file.split(os.sep)[
                                   0:-2]) + os.sep + 'handler.py'

        exclude = ['static', 'media']
        self.zip_path = self.zappa.create_lambda_zip(
            self.lambda_name,
            handler_file=handler_file,
            use_precompiled_packages=self.zappa_settings.get('use_precompiled_packages', True),
            exclude=exclude
        )

        # Add this environment's Django settings to that zipfile
        with open(self.settings_file, 'r') as f:
            contents = f.read()
            all_contents = contents
            if not self.zappa_settings[self.api_stage].has_key('domain'):
                script_name = self.api_stage
            else:
                script_name = ''

            all_contents = all_contents + \
                           '\n# Automatically added by Zappa:\nSCRIPT_NAME=\'/' + script_name + '\'\n'
            f.close()

        with open('zappa_settings.py', 'w') as f:
            f.write(all_contents)

        with zipfile.ZipFile(self.zip_path, 'a') as lambda_zip:
            lambda_zip.write('zappa_settings.py', 'zappa_settings.py')
            lambda_zip.close()

        os.unlink('zappa_settings.py')

    def remove_s3_local_settings(self):
        # Remove the settings file if downloaded from S3
        if self.get_settings_location().startswith('s3://'):
            os.remove(self.settings_file)

    def remove_local_zip(self):
        """
        Remove our local zip file.
        """

        if self.zappa_settings[self.api_stage].get('delete_zip', True):
            os.remove(self.zip_path)

    def remove_uploaded_zip(self):
        """
        Remove the local and S3 zip file after uploading and updating.
        """

        # Remove the uploaded zip from S3, because it is now registered..
        self.zappa.remove_from_s3(self.zip_path, self.s3_bucket_name)

        # Finally, delete the local copy our zip package
        self.remove_local_zip()
Пример #44
0
 def test_create_iam_roles(self, session):
     z = Zappa(session)
     arn, updated = z.create_iam_roles()
     self.assertEqual(arn, "arn:aws:iam::123:role/{}".format(z.role_name))
Пример #45
0
 def __init__(self, *args, **kwargs):
     super(ZappaCommand, self).__init__(*args, **kwargs)
     self.zappa = Zappa()
Пример #46
0
 def test_create_lambda_package(self):
     self.assertTrue(True)
     z = Zappa()
     path = z.create_lambda_zip()
     self.assertTrue(os.path.isfile(path))
     os.remove(path)
Пример #47
0
class ZappaCommand(BaseCommand):

    # Management command
    can_import_settings = True
    requires_system_checks = False

    # Zappa settings
    zappa = None
    zappa_settings = None
    api_stage = None
    project_name = None
    lambda_name = None
    s3_bucket_name = None
    settings_file = None
    zip_path = None
    vpc_config = None
    memory_size = None

    help = '''Deploy this project to AWS with Zappa.'''

    def add_arguments(self, parser):
        parser.add_argument('environment', nargs='+', type=str)

    def __init__(self, *args, **kwargs):
        super(ZappaCommand, self).__init__(*args, **kwargs)
        self.zappa = Zappa()

    def require_settings(self, args, options):
        """
        Load the ZAPPA_SETTINGS as we expect it.

        """

        if not options.has_key('environment'):
            print(
                "You must call deploy with an environment name. \n python manage.py deploy <environment>")
            raise ImproperlyConfigured

        from django.conf import settings
        if not 'ZAPPA_SETTINGS' in dir(settings):
            print(
                "Please define your ZAPPA_SETTINGS in your settings file before deploying.")
            raise ImproperlyConfigured

        self.zappa_settings = settings.ZAPPA_SETTINGS

        # Set your configuration
        self.project_name = os.path.abspath(settings.BASE_DIR).split(os.sep)[-1]
        if type(options['environment']) == list:
            self.api_stage = options['environment'][0]
        else:
            self.api_stage = options['environment']
        self.lambda_name = self.project_name + '-' + self.api_stage
        if self.api_stage not in self.zappa_settings.keys():
            print("Please make sure that the environment '" + self.api_stage +
                  "' is defined in your ZAPPA_SETTINGS in your settings file before deploying.")
            raise ImproperlyConfigured

        # Load environment-specific settings
        self.s3_bucket_name = self.zappa_settings[self.api_stage]['s3_bucket']
        self.vpc_config = self.zappa_settings[
            self.api_stage].get('vpc_config', {})
        self.memory_size = self.zappa_settings[
            self.api_stage].get('memory_size', 512)
        self.settings_file = self.zappa_settings[
            self.api_stage]['settings_file']
        if '~' in self.settings_file:
            self.settings_file = self.settings_file.replace(
                '~', os.path.expanduser('~'))
        if not os.path.isfile(self.settings_file):
            print("Please make sure your settings_file is properly defined.")
            raise ImproperlyConfigured

        custom_settings = [
            'http_methods',
            'parameter_depth',
            'integration_response_codes',
            'method_response_codes',
            'role_name',
            'aws_region'
        ]
        for setting in custom_settings:
            if self.zappa_settings[self.api_stage].has_key(setting):
                setattr(self.zappa, setting, self.zappa_settings[
                        self.api_stage][setting])

    def create_package(self):
        """
        Ensure that the package can be properly configured,
        and then create it.

        """

        # Create the Lambda zip package (includes project and virtualenvironment)
        # Also define the path the handler file so it can be copied to the zip
        # root for Lambda.
        current_file = os.path.dirname(os.path.abspath(
            inspect.getfile(inspect.currentframe())))
        handler_file = os.sep.join(current_file.split(os.sep)[
                                   0:-2]) + os.sep + 'handler.py'

        exclude = ['static', 'media']
        self.zip_path = self.zappa.create_lambda_zip(
                self.lambda_name,
                handler_file=handler_file,
                use_precompiled_packages=self.zappa_settings.get('use_precompiled_packages', True),
                exclude=exclude
            )

        # Add this environment's Django settings to that zipfile
        with open(self.settings_file, 'r') as f:
            contents = f.read()
            all_contents = contents
            if not self.zappa_settings[self.api_stage].has_key('domain'):
                script_name = self.api_stage
            else:
                script_name = ''

            if not "ZappaMiddleware" in all_contents:
                print("\n\nWARNING!\n")
                print(
                    "You do not have ZappaMiddleware in your remote settings's MIDDLEWARE_CLASSES.\n")
                print(
                    "This means that some aspects of your application may not work!\n\n")

            all_contents = all_contents + \
                '\n# Automatically added by Zappa:\nSCRIPT_NAME=\'/' + script_name + '\'\n'
            f.close()

        with open('zappa_settings.py', 'w') as f:
            f.write(all_contents)

        with zipfile.ZipFile(self.zip_path, 'a') as lambda_zip:
            lambda_zip.write('zappa_settings.py', 'zappa_settings.py')
            lambda_zip.close()

        os.unlink('zappa_settings.py')

    def remove_local_zip(self):
        """
        Remove our local zip file.
        """

        if self.zappa_settings[self.api_stage].get('delete_zip', True):
            os.remove(self.zip_path)

    def remove_uploaded_zip(self):
        """
        Remove the local and S3 zip file after uploading and updating.
        """

        # Remove the uploaded zip from S3, because it is now registered..
        self.zappa.remove_from_s3(self.zip_path, self.s3_bucket_name)

        # Finally, delete the local copy our zip package
        self.remove_local_zip()
Пример #48
0
 def test_fetch_logs(self, session):
     z = Zappa(session)
     z.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution'
     events = z.fetch_logs('Spheres-demonstration')
     self.assertTrue(events is not None)
Пример #49
0
 def test_schedule_events(self):
     z = Zappa()
     path = os.getcwd()
Пример #50
0
class ZappaCommand(BaseCommand):

    # Management command
    can_import_settings = True
    requires_system_checks = False

    # Zappa settings
    zappa = None
    zappa_settings = None
    api_stage = None
    project_name = None
    lambda_name = None
    s3_bucket_name = None
    settings_file = None
    zip_path = None
    vpc_config = None
    memory_size = None
    timeout = None

    help = '''Deploy this project to AWS with Zappa.'''

    def add_arguments(self, parser):
        parser.add_argument('environment', nargs='+', type=str)

    def __init__(self, *args, **kwargs):
        super(ZappaCommand, self).__init__(*args, **kwargs)
        self.zappa = Zappa()

    def require_settings(self, args, options):
        """
        Load the ZAPPA_SETTINGS as we expect it.

        """

        if not options.has_key('environment'):
            print(
                "You must call deploy with an environment name. \n python manage.py deploy <environment>"
            )
            raise ImproperlyConfigured

        from django.conf import settings
        if not 'ZAPPA_SETTINGS' in dir(settings):
            print(
                "Please define your ZAPPA_SETTINGS in your settings file before deploying."
            )
            raise ImproperlyConfigured

        self.zappa_settings = settings.ZAPPA_SETTINGS

        # Set your configuration
        self.project_name = os.path.abspath(settings.BASE_DIR).split(
            os.sep)[-1]
        if type(options['environment']) == list:
            self.api_stage = options['environment'][0]
        else:
            self.api_stage = options['environment']
        self.lambda_name = slugify(self.project_name + '-' +
                                   self.api_stage).replace("_", "-")
        if self.api_stage not in self.zappa_settings.keys():
            print(
                "Please make sure that the environment '" + self.api_stage +
                "' is defined in your ZAPPA_SETTINGS in your settings file before deploying."
            )
            raise ImproperlyConfigured

        # Load environment-specific settings
        self.s3_bucket_name = self.zappa_settings[self.api_stage]['s3_bucket']
        self.vpc_config = self.zappa_settings[self.api_stage].get(
            'vpc_config', {})
        self.memory_size = self.zappa_settings[self.api_stage].get(
            'memory_size', 512)
        self.timeout = self.zappa_settings[self.api_stage].get('timeout', 30)

        custom_settings = [
            'http_methods', 'parameter_depth', 'integration_response_codes',
            'method_response_codes', 'role_name', 'aws_region'
        ]
        for setting in custom_settings:
            if self.zappa_settings[self.api_stage].has_key(setting):
                setattr(self.zappa, setting,
                        self.zappa_settings[self.api_stage][setting])

    def get_django_settings_file(self):
        if not self.get_settings_location().startswith('s3://'):
            self.settings_file = self.zappa_settings[
                self.api_stage]['settings_file']
            if '~' in self.settings_file:
                self.settings_file = self.settings_file.replace(
                    '~', os.path.expanduser('~'))
            self.check_settings_file()
        else:
            self.settings_file = self.download_from_s3(
                *self.parse_s3_url(self.get_settings_location()))
            self.check_settings_file()

    def check_settings_file(self):
        """
        Checks whether the settings file specified is actually a file.
        """
        if not os.path.isfile(self.settings_file):
            print("Please make sure your settings_file is properly defined.")
            raise ImproperlyConfigured

    def get_settings_location(self):
        """
        Returns the value of the settings file location as specified in the
        json file.
        :return:
        """
        return self.zappa_settings[self.api_stage]['settings_file']

    def download_from_s3(self,
                         bucket_name,
                         s3_key,
                         output_filename='temp_zappa_settings.py'):
        """
        Download a file from S3
        :param bucket_name: Name of the S3 bucket (string)
        :param s3_key: Name of the file hosted on S3 (string)
        :param output_filename: Name of the file the download operation
        will create (string)
        :return: False or the value of output_filename
        """
        s3 = boto3.resource('s3')
        bucket = s3.Bucket(bucket_name)
        try:
            s3.meta.client.head_object(Bucket=bucket_name, Key=s3_key)
        except botocore.exceptions.ClientError:
            return False
        print(u'Downloading the settings file ({0}) from S3'.format(s3_key))
        new_file = bucket.download_file(s3_key, output_filename)
        return output_filename

    def parse_s3_url(self, s3_url):
        """
        Parse the S3 url. Format: s3://mybucket:path/to/my/key
        Example: s3://settings-bucket:/production_settings.py
        :param s3_url: Path to the file hosted on S3
        :return:
        """
        return s3_url.replace('s3://', '').split(':')

    def load_credentials(self):
        session = None
        profile_name = self.zappa_settings[self.api_stage].get('profile_name')
        region_name = self.zappa_settings[self.api_stage].get('aws_region')
        if profile_name is not None:
            session = boto3.Session(profile_name=profile_name,
                                    region_name=region_name)
        self.zappa.load_credentials(session)

    def create_package(self):
        """
        Ensure that the package can be properly configured,
        and then create it.

        """

        # Create the Lambda zip package (includes project and virtualenvironment)
        # Also define the path the handler file so it can be copied to the zip
        # root for Lambda.
        current_file = os.path.dirname(
            os.path.abspath(inspect.getfile(inspect.currentframe())))
        handler_file = os.sep.join(current_file.split(
            os.sep)[0:-2]) + os.sep + 'handler.py'

        exclude = ['static', 'media']
        self.zip_path = self.zappa.create_lambda_zip(
            self.lambda_name,
            handler_file=handler_file,
            use_precompiled_packages=self.zappa_settings.get(
                'use_precompiled_packages', True),
            exclude=exclude)

        # Add this environment's Django settings to that zipfile
        with open(self.settings_file, 'r') as f:
            contents = f.read()
            all_contents = contents
            if not self.zappa_settings[self.api_stage].has_key('domain'):
                script_name = self.api_stage
            else:
                script_name = ''

            all_contents = all_contents + \
                '\n# Automatically added by Zappa:\nSCRIPT_NAME=\'/' + script_name + '\'\n'
            f.close()

        with open('zappa_settings.py', 'w') as f:
            f.write(all_contents)

        with zipfile.ZipFile(self.zip_path, 'a') as lambda_zip:
            lambda_zip.write('zappa_settings.py', 'zappa_settings.py')
            lambda_zip.close()

        os.unlink('zappa_settings.py')

    def remove_s3_local_settings(self):
        #Remove the settings file if downloaded from S3
        if self.get_settings_location().startswith('s3://'):
            os.remove(self.settings_file)

    def remove_local_zip(self):
        """
        Remove our local zip file.
        """

        if self.zappa_settings[self.api_stage].get('delete_zip', True):
            os.remove(self.zip_path)

    def remove_uploaded_zip(self):
        """
        Remove the local and S3 zip file after uploading and updating.
        """

        # Remove the uploaded zip from S3, because it is now registered..
        self.zappa.remove_from_s3(self.zip_path, self.s3_bucket_name)

        # Finally, delete the local copy our zip package
        self.remove_local_zip()
Пример #51
0
    def handle(self, *args, **options):
        """
        Execute the command.

        """
        if not options.has_key('environment'):
            print("You must call deploy with an environment name. \n python manage.py deploy <environment>")
            return

        from django.conf import settings
        if not 'ZAPPA_SETTINGS' in dir(settings):
            print("Please define your ZAPPA_SETTINGS in your settings file before deploying.")
            return

        zappa_settings = settings.ZAPPA_SETTINGS

        # Set your configuration
        project_name = settings.BASE_DIR.split(os.sep)[-1]
        api_stage = options['environment'][0]
        if api_stage not in zappa_settings.keys():
            print("Please make sure that the environment '" + api_stage + "' is defined in your ZAPPA_SETTINGS in your settings file before deploying.")
            return

        # Make your Zappa object
        zappa = Zappa()

        # Load environment-specific settings
        s3_bucket_name = zappa_settings[api_stage]['s3_bucket']
        settings_file = zappa_settings[api_stage]['settings_file']
        if '~' in settings_file:
            settings_file = settings_file.replace('~', os.path.expanduser('~'))
        if not os.path.isfile(settings_file):
            print("Please make sure your settings_file is properly defined.")
            return

        custom_settings = [
            'http_methods', 
            'parameter_depth',
            'integration_response_codes',
            'method_response_codes',
            'role_name',
            'aws_region'
        ]
        for setting in custom_settings:
            if zappa_settings[api_stage].has_key(setting):
                setattr(zappa, setting, zappa_settings[api_stage][setting])

        # Load your AWS credentials from ~/.aws/credentials
        zappa.load_credentials()

        # Create the Lambda zip package (includes project and virtualenvironment)
        # Also define the path the handler file so it can be copied to the zip root for Lambda.
        current_file =  os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
        handler_file = os.sep.join(current_file.split(os.sep)[0:-2]) + os.sep + 'handler.py'
        lambda_name = project_name + '-' + api_stage
        zip_path = zappa.create_lambda_zip(lambda_name, handler_file=handler_file)

        # Add this environment's Django settings to that zipfile
        with zipfile.ZipFile(zip_path, 'a') as lambda_zip:
            lambda_zip.write(settings_file, 'zappa_settings.py')
            lambda_zip.close()

        # Upload it to S3
        zip_arn = zappa.upload_to_s3(zip_path, s3_bucket_name)

        # Register the Lambda function with that zip as the source
        # You'll also need to define the path to your lambda_handler code.
        lambda_arn = zappa.update_lambda_function(s3_bucket_name, zip_path, lambda_name)

        # Get the URL!
        endpoint_url = zappa.get_api_url(lambda_name)

        # Finally, delete the local copy our zip package
        os.remove(zip_path)

        # Remove the uploaded zip from S3, because it is now registered..
        zappa.remove_from_s3(zip_path, s3_bucket_name)

        print("Your updated Zappa deployment is live!")

        return
Пример #52
0
 def test_logging(self):
     """
     TODO
     """
     Zappa()
Пример #53
0
 def test_get_api_url(self, session):
     z = Zappa(session)
     z.credentials_arn = 'arn:aws:iam::724336686645:role/ZappaLambdaExecution'
     url = z.get_api_url('Spheres-demonstration')
Пример #54
0
 def test_200_pattern(self):
     pattern = Zappa.selection_pattern('200')
     self.assertEqual(pattern, '')
Пример #55
0
    def handle(self, *args, **options):
        """
        Execute the command.

        """
        if not options.has_key('environment'):
            print("You must call deploy with an environment name. \n python manage.py deploy <environment>")
            return

        from django.conf import settings
        if not 'ZAPPA_SETTINGS' in dir(settings):
            print("Please define your ZAPPA_SETTINGS in your settings file before deploying.")
            return

        zappa_settings = settings.ZAPPA_SETTINGS

        # Set your configuration
        project_name = settings.BASE_DIR.split(os.sep)[-1]
        api_stage = options['environment'][0]
        if api_stage not in zappa_settings.keys():
            print("Please make sure that the environment '" + api_stage + "' is defined in your ZAPPA_SETTINGS in your settings file before deploying.")
            return

        # Make your Zappa object
        zappa = Zappa()

        # Load environment-specific settings
        s3_bucket_name = zappa_settings[api_stage]['s3_bucket']
        settings_file = zappa_settings[api_stage]['settings_file']
        if '~' in settings_file:
            settings_file = settings_file.replace('~', os.path.expanduser('~'))
        if not os.path.isfile(settings_file):
            print("Please make sure your settings_file is properly defined.")
            return

        custom_settings = [
            'http_methods', 
            'parameter_depth',
            'integration_response_codes',
            'method_response_codes',
            'role_name',
            'aws_region'
        ]
        for setting in custom_settings:
            if zappa_settings[api_stage].has_key(setting):
                setattr(zappa, setting, zappa_settings[api_stage][setting])

        # Load your AWS credentials from ~/.aws/credentials
        zappa.load_credentials()

        # Create the Lambda zip package (includes project and virtualenvironment)
        # Also define the path the handler file so it can be copied to the zip root for Lambda.
        current_file =  os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
        handler_file = os.sep.join(current_file.split(os.sep)[0:-2]) + os.sep + 'handler.py'
        lambda_name = project_name + '-' + api_stage
        zip_path = zappa.create_lambda_zip(lambda_name, handler_file=handler_file)

        #Add this environment's Django settings to that zipfile
        with open(settings_file, 'r') as f:
            contents = f.read()
            all_contents = contents + '\n# Automatically added by Zappa:\nSCRIPT_NAME=\'/' + api_stage + '\'\n'
            f.close()

        with open('zappa_settings.py', 'w') as f:
            f.write(all_contents)

        with zipfile.ZipFile(zip_path, 'a') as lambda_zip:
            lambda_zip.write('zappa_settings.py', 'zappa_settings.py')
            lambda_zip.close()

        os.unlink('zappa_settings.py') 

        # Upload it to S3
        zip_arn = zappa.upload_to_s3(zip_path, s3_bucket_name)

        # Register the Lambda function with that zip as the source
        # You'll also need to define the path to your lambda_handler code.
        lambda_arn = zappa.update_lambda_function(s3_bucket_name, zip_path, lambda_name)

        # Get the URL!
        endpoint_url = zappa.get_api_url(lambda_name)

        # Finally, delete the local copy our zip package
        os.remove(zip_path)

        # Remove the uploaded zip from S3, because it is now registered..
        zappa.remove_from_s3(zip_path, s3_bucket_name)

        print("Your updated Zappa deployment is live!")

        return
Пример #56
0
 def test_create_lambda_package(self):
     self.assertTrue(True)
     z = Zappa()
     path = z.create_lambda_zip(handler_file=os.path.realpath(__file__))
     self.assertTrue(os.path.isfile(path))
     os.remove(path)
Пример #57
0
 def test_200_pattern(self):
     pattern = Zappa.selection_pattern('200')
     self.assertEqual(pattern, '')
Пример #58
0
    def handle(self, *args, **options):
        """
        Execute the command.

        """
        if not options.has_key('environment'):
            print("You must call deploy with an environment name. \n python manage.py deploy <environment>")
            return

        from django.conf import settings
        if not 'ZAPPA_SETTINGS' in dir(settings):
            print("Please define your ZAPPA_SETTINGS in your settings file before deploying.")
            return

        zappa_settings = settings.ZAPPA_SETTINGS

        # Set your configuration
        project_name = settings.BASE_DIR.split(os.sep)[-1]
        api_stage = options['environment'][0]
        if api_stage not in zappa_settings.keys():
            print("Please make sure that the environment '" + api_stage + "' is defined in your ZAPPA_SETTINGS in your settings file before deploying.")
            return

        # Make your Zappa object
        zappa = Zappa()

        # Load environment-specific settings
        s3_bucket_name = zappa_settings[api_stage]['s3_bucket']
        vpc_config = zappa_settings[api_stage].get('vpc_config', {})
        memory_size = zappa_settings[api_stage].get('memory_size', 512)
        settings_file = zappa_settings[api_stage]['settings_file']
        if '~' in settings_file:
            settings_file = settings_file.replace('~', os.path.expanduser('~'))
        if not os.path.isfile(settings_file):
            print("Please make sure your settings_file is properly defined.")
            return

        custom_settings = [
            'http_methods', 
            'parameter_depth',
            'integration_response_codes',
            'method_response_codes',
            'role_name',
            'aws_region'
        ]
        for setting in custom_settings:
            if zappa_settings[api_stage].has_key(setting):
                setattr(zappa, setting, zappa_settings[api_stage][setting])

        # Load your AWS credentials from ~/.aws/credentials
        zappa.load_credentials()

        # Make sure the necessary IAM execution roles are available
        zappa.create_iam_roles()

        # Create the Lambda zip package (includes project and virtualenvironment)
        # Also define the path the handler file so it can be copied to the zip root for Lambda.
        current_file =  os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
        handler_file = os.sep.join(current_file.split(os.sep)[0:-2]) + os.sep + 'handler.py'
        lambda_name = project_name + '-' + api_stage
        zip_path = zappa.create_lambda_zip(lambda_name, handler_file=handler_file)

        # Add this environment's Django settings to that zipfile
        with open(settings_file, 'r') as f:
            contents = f.read()
            all_contents = contents
            if not zappa_settings[api_stage].has_key('domain'):
                script_name = api_stage
            else:
                script_name = ''

            if not "ZappaMiddleware" in all_contents:
                print("\n\nWARNING!\n")
                print("You do not have ZappaMiddleware in your remote settings's MIDDLEWARE_CLASSES.\n")
                print("This means that some aspects of your application may not work!\n\n")
            
            all_contents = all_contents + '\n# Automatically added by Zappa:\nSCRIPT_NAME=\'/' + script_name + '\'\n'
            f.close()

        with open('zappa_settings.py', 'w') as f:
            f.write(all_contents)

        with zipfile.ZipFile(zip_path, 'a') as lambda_zip:
            lambda_zip.write('zappa_settings.py', 'zappa_settings.py')
            lambda_zip.close()

        os.unlink('zappa_settings.py') 

        # Upload it to S3
        zip_arn = zappa.upload_to_s3(zip_path, s3_bucket_name)

        # Register the Lambda function with that zip as the source
        # You'll also need to define the path to your lambda_handler code.
        lambda_arn = zappa.create_lambda_function(bucket=s3_bucket_name,
                                                  s3_key=zip_path,
                                                  function_name=lambda_name,
                                                  handler='handler.lambda_handler',
                                                  vpc_config=vpc_config,
                                                  memory_size=memory_size)

        # Create and configure the API Gateway
        api_id = zappa.create_api_gateway_routes(lambda_arn, lambda_name)

        # Deploy the API!
        endpoint_url = zappa.deploy_api_gateway(api_id, api_stage)

        # Finally, delete the local copy our zip package
        if zappa_settings[api_stage].get('delete_zip', True):
            os.remove(zip_path)

        # Remove the uploaded zip from S3, because it is now registered..
        zappa.remove_from_s3(zip_path, s3_bucket_name)

        if zappa_settings[api_stage].get('touch', True):
            requests.get(endpoint_url)

        print("Your Zappa deployment is live!: " + endpoint_url)

        return
Пример #59
0
 def test_human_units(self):
     zappa = Zappa()
     zappa.human_size(1)
     zappa.human_size(9999999999999)
Пример #60
0
 def test_schedule_events(self):
     z = Zappa()
     path = os.getcwd()
     z.schedule_events(path=path)