def test_resolve_roles__interactive_mode__service_role_could_not_be_found__simulate_customer_viewing_policies(
            self, get_managed_policy_document_mock, get_input_mock,
            create_default_service_role_mock, get_service_role_mock,
            create_default_instance_profile_mock):
        get_managed_policy_document_mock.side_effect = [
            {
                "PolicyVersion": {
                    "Document": {}
                }
            },
            {
                "PolicyVersion": {
                    "Document": {}
                }
            },
        ]
        get_input_mock.side_effect = ['view', '\n']
        create_default_instance_profile_mock.return_value = 'default-profile'
        get_service_role_mock.return_value = None
        create_default_service_role_mock.return_value = 'aws-elasticbeanstalk-service-role'
        env_request = CreateEnvironmentRequest(
            app_name='my-application',
            env_name='environment-1',
            cname='cname-1',
            platform=SolutionStack(
                '64bit Amazon Linux 2015.09 v2.0.6 running Multi-container Docker 1.7.1 (Generic)'
            ),
            elb_type='network',
            group_name='dev',
        )

        createops.resolve_roles(env_request, True)
        self.assertEqual('aws-elasticbeanstalk-service-role',
                         env_request.service_role)
    def test_make_new_env_from_code_in_directory__use_source__process_app_version(
            self, wait_for_processed_app_versions_mock,
            create_app_version_mock, upload_keypair_if_needed_mock,
            wait_for_success_events_mock, get_current_branch_environment_mock,
            create_env_mock, create_app_version_from_source_mock, echo_mock,
            log_info_mock, git_management_enabled_mock, build_spec_exists_mock,
            resolve_roles_mock):
        build_spec_exists_mock.return_value = False
        git_management_enabled_mock.return_value = False
        create_environment_result_mock = mock.MagicMock()
        create_environment_result_mock.name = 'result'
        create_env_mock.return_value = (create_environment_result_mock,
                                        'request-id')
        get_current_branch_environment_mock.return_value = 'environment-1'
        env_request = CreateEnvironmentRequest(
            app_name='my-application',
            env_name='environment-1',
            cname='cname-1',
            platform=SolutionStack(
                '64bit Amazon Linux 2015.09 v2.0.6 running Multi-container Docker 1.7.1 (Generic)'
            ),
            elb_type='network',
            group_name='dev',
            key_name='aws-eb-us-west-2')
        create_app_version_mock.return_value = 'version-label-1'
        wait_for_processed_app_versions_mock.return_value = True
        create_app_version_from_source_mock.return_value = 'version-label-1'

        createops.make_new_env(env_request,
                               source='codecommit/my-repository/my-branch')

        create_app_version_from_source_mock.assert_called_once_with(
            'my-application',
            'codecommit/my-repository/my-branch',
            build_config=None,
            label=None,
            process=False)
        create_environment_result_mock.print_env_details.assert_called_once_with(
            createops.io.echo,
            createops.elasticbeanstalk.get_environments,
            createops.elasticbeanstalk.get_environment_resources,
            health=False)
        wait_for_success_events_mock.assert_called_once_with(
            'request-id', timeout_in_minutes=None)
        create_env_mock.assert_called_once_with(env_request, interactive=True)
        upload_keypair_if_needed_mock.assert_called_once_with(
            'aws-eb-us-west-2')
        wait_for_processed_app_versions_mock.assert_called_once_with(
            'my-application', ['version-label-1'], timeout=5)
        log_info_mock.assert_has_calls([
            mock.call('Creating new application version using remote source'),
            mock.call('Creating new environment')
        ])
        echo_mock.assert_has_calls([
            mock.call('Starting environment deployment via remote source'),
            mock.call('Printing Status:')
        ])
    def test_make_new_env_from_code_in_directory__non_interactive_mode__buildspec_present__using_application_code_in_directory_to_create_app_version(
            self, download_and_extract_sample_app_mock,
            stream_build_configuration_app_version_creation_mock,
            create_app_version_mock, upload_keypair_if_needed_mock,
            wait_for_success_events_mock, get_current_branch_environment_mock,
            create_env_mock, log_info_mock, git_management_enabled_mock,
            build_spec_exists_mock, get_build_configuration_mock,
            resolve_roles_mock):
        resolve_roles_mock.side_effect = None
        build_spec_exists_mock.return_value = True
        build_config = BuildConfiguration(
            compute_type='t2.micro',
            image='java-ami',
            service_role='codebuild-service-role',
            timeout=10)
        get_build_configuration_mock.return_value = build_config
        git_management_enabled_mock.return_value = False
        create_environment_result_mock = mock.MagicMock()
        create_environment_result_mock.name = 'result'
        create_env_mock.return_value = (create_environment_result_mock,
                                        'request-id')
        get_current_branch_environment_mock.return_value = 'environment-1'
        env_request = CreateEnvironmentRequest(
            app_name='my-application',
            env_name='environment-1',
            cname='cname-1',
            platform=SolutionStack(
                '64bit Amazon Linux 2015.09 v2.0.6 running Multi-container Docker 1.7.1 (Generic)'
            ),
            elb_type='network',
            group_name='dev',
            key_name='aws-eb-us-west-2')
        create_app_version_mock.return_value = 'version-label-1'

        createops.make_new_env(env_request, interactive=True)

        stream_build_configuration_app_version_creation_mock.assert_called_once_with(
            'my-application', 'version-label-1', build_config)
        create_environment_result_mock.print_env_details.assert_called_once_with(
            createops.io.echo,
            createops.elasticbeanstalk.get_environments,
            createops.elasticbeanstalk.get_environment_resources,
            health=False)
        wait_for_success_events_mock.assert_called_once_with(
            'request-id', timeout_in_minutes=None)
        create_env_mock.assert_called_once_with(env_request, interactive=True)
        upload_keypair_if_needed_mock.assert_called_once_with(
            'aws-eb-us-west-2')
        create_app_version_mock.assert_called_once_with(
            'my-application', build_config=build_config, process=False)
        log_info_mock.assert_has_calls([
            mock.call('Creating new application version using project code'),
            mock.call('Creating new environment')
        ])
        download_and_extract_sample_app_mock.assert_not_called()
    def test_create_new_environment_app_notexists(self):
        self.mock_elasticbeanstalk.create_environment.side_effect = [
            InvalidParameterValueError('Application \'app-name\' already exists.'),
        ]

        try:
            env_request = CreateEnvironmentRequest(
                app_name='app-name',
                env_name='env-name',
                )
            createops.create_env(env_request, interactive=True)
            self.fail('Should have thrown InvalidParameterValueError')
        except InvalidParameterValueError:
            pass
    def test_create_new_environment_cname_taken_script(self):
        self.mock_elasticbeanstalk.create_environment.side_effect = [
            InvalidParameterValueError('DNS name (cname) is not available.'),
        ]

        try:
            env_request = CreateEnvironmentRequest(
                app_name='app-name',
                env_name='env-name',
                )
            createops.create_env(env_request, interactive=False)
            self.fail('Should have thrown InvalidParameterValueError')
        except InvalidParameterValueError:
            pass
    def test_create_new_environment_cname_taken(self):
        self.mock_elasticbeanstalk.create_environment.side_effect = [
            InvalidParameterValueError('DNS name (cname) is not available.'),
            None,
        ]

        self.mock_input.return_value = 'new-cname'

        env_request = CreateEnvironmentRequest(
            app_name='app-name',
            env_name='env-name',
            )
        createops.create_env(env_request, interactive=True)

        self.assertEqual(self.mock_elasticbeanstalk.create_environment.call_count, 2)
    def test_create_new_environment_envname_taken(self, mock_names):
        mock_names.return_value = ['my-env', 'my-env2']
        self.mock_elasticbeanstalk.create_environment.side_effect = [
            InvalidParameterValueError('Environment env-name already exists.'),
            None,
        ]

        self.mock_input.return_value = 'new-env-name'

        env_request = CreateEnvironmentRequest(
            app_name='app-name',
            env_name='env-name',
        )
        createops.create_env(env_request, interactive=True)
        self.assertEqual(self.mock_elasticbeanstalk.create_environment.call_count, 2)
    def test_resolve_roles__non_interactive_mode__no_healthd_support_for_platform__service_role_cannot_determined(
            self, get_service_role_mock, create_default_instance_profile_mock):
        env_request = CreateEnvironmentRequest(
            app_name='my-application',
            env_name='environment-1',
            cname='cname-1',
            platform=SolutionStack(
                '64bit Windows Server Core 2016 v1.2.0 running IIS 10.0'),
            elb_type='network',
            group_name='dev',
            instance_profile='developer')

        createops.resolve_roles(env_request, False)

        get_service_role_mock.assert_not_called()
        create_default_instance_profile_mock.assert_not_called()
        self.assertIsNone(env_request.service_role)
    def test_resolve_roles__non_interactive_mode__profile_passed_is_other_role_name_other_than_service_role__existing_role_retrieved_from_service(
            self, get_service_role_mock, create_default_instance_profile_mock):
        get_service_role_mock.return_value = 'service-role'
        env_request = CreateEnvironmentRequest(
            app_name='my-application',
            env_name='environment-1',
            cname='cname-1',
            platform=SolutionStack(
                '64bit Amazon Linux 2015.09 v2.0.6 running Multi-container Docker 1.7.1 (Generic)'
            ),
            elb_type='network',
            group_name='dev',
            instance_profile='developer')

        createops.resolve_roles(env_request, False)

        create_default_instance_profile_mock.assert_not_called()
    def test_make_new_env_from_code_in_directory__process_app_version_failed(
            self, wait_for_processed_app_versions_mock,
            create_app_version_mock, upload_keypair_if_needed_mock,
            wait_for_success_events_mock, get_current_branch_environment_mock,
            create_env_mock, log_info_mock, git_management_enabled_mock,
            build_spec_exists_mock, resolve_roles_mock):
        resolve_roles_mock.side_effect = None
        build_spec_exists_mock.return_value = False
        git_management_enabled_mock.return_value = False
        create_environment_result_mock = mock.MagicMock()
        create_environment_result_mock.name = 'result'
        create_env_mock.return_value = (create_environment_result_mock,
                                        'request-id')
        get_current_branch_environment_mock.return_value = 'environment-1'
        wait_for_processed_app_versions_mock.return_value = False
        env_request = CreateEnvironmentRequest(
            app_name='my-application',
            env_name='environment-1',
            cname='cname-1',
            platform=SolutionStack(
                '64bit Amazon Linux 2015.09 v2.0.6 running Multi-container Docker 1.7.1 (Generic)'
            ),
            elb_type='network',
            group_name='dev',
            key_name='aws-eb-us-west-2')
        create_app_version_mock.return_value = 'version-label-1'

        createops.make_new_env(env_request,
                               process_app_version=True,
                               timeout=5)

        wait_for_processed_app_versions_mock.assert_called_once_with(
            'my-application', ['version-label-1'], timeout=5)
        create_app_version_mock.assert_called_once_with('my-application',
                                                        build_config=None,
                                                        process=True)
        log_info_mock.assert_has_calls([
            mock.call('Creating new application version using project code'),
        ])
        create_environment_result_mock.print_env_details.assert_not_called()
        wait_for_success_events_mock.assert_not_called()
        create_env_mock.assert_not_called()
        upload_keypair_if_needed_mock.assert_not_called()
Exemple #11
0
    def do_command(self):
        env_name = self.app.pargs.environment_name
        modules = self.app.pargs.modules
        if modules and len(modules) > 0:
            self.compose_multiple_apps()
            return
        group = self.app.pargs.env_group_suffix
        cname = self.app.pargs.cname
        tier = self.app.pargs.tier
        itype = self.app.pargs.instance_type
        platform = self.app.pargs.platform
        single = self.app.pargs.single
        iprofile = self.app.pargs.instance_profile
        service_role = self.app.pargs.service_role
        label = self.app.pargs.version
        branch_default = self.app.pargs.branch_default
        key_name = self.app.pargs.keyname
        sample = self.app.pargs.sample
        nohang = self.app.pargs.nohang
        tags = self.app.pargs.tags
        envvars = self.app.pargs.envvars
        scale = self.app.pargs.scale
        timeout = self.app.pargs.timeout
        cfg = self.app.pargs.cfg
        elb_type = self.app.pargs.elb_type
        source = self.app.pargs.source
        process = self.app.pargs.process
        enable_spot = self.app.pargs.enable_spot
        spot_max_price = self.app.pargs.spot_max_price
        instance_types = self.app.pargs.instance_types
        on_demand_base_capacity = self.app.pargs.on_demand_base_capacity
        on_demand_above_base_capacity = self.app.pargs.on_demand_above_base_capacity
        max_instances = self.app.pargs.max_instances
        min_instances = self.app.pargs.min_instances

        interactive = False if env_name else True

        provided_env_name = env_name

        if sample and label:
            raise InvalidOptionsError(strings['create.sampleandlabel'])

        if single and scale:
            raise InvalidOptionsError(strings['create.singleandsize'])

        if (max_instances or min_instances) and scale:
            raise InvalidOptionsError(strings['create.scaleandminmax'])

        if (max_instances or min_instances) and single:
            raise InvalidOptionsError(strings['create.singleandminmax'])

        if single and elb_type:
            raise InvalidOptionsError(strings['create.single_and_elb_type'])

        if cname and tier and Tier.looks_like_worker_tier(tier):
            raise InvalidOptionsError(strings['worker.cname'])

        if cname and not elasticbeanstalk.is_cname_available(cname):
            raise AlreadyExistsError(strings['cname.unavailable'].replace(
                '{cname}', cname))

        if tier and Tier.looks_like_worker_tier(tier):
            if self.app.pargs.vpc_elbpublic or self.app.pargs.vpc_elbsubnets or self.app.pargs.vpc_publicip:
                raise InvalidOptionsError(
                    strings['create.worker_and_incompatible_vpc_arguments'])

        if (not tier or Tier.looks_like_webserver_tier(tier)) and single:
            if self.app.pargs.vpc_elbpublic or self.app.pargs.vpc_elbsubnets:
                raise InvalidOptionsError(
                    strings['create.single_and_elbpublic_or_elb_subnet'])

        if (spot_max_price or on_demand_base_capacity
                or on_demand_above_base_capacity) and not enable_spot:
            raise InvalidOptionsError(strings['create.missing_enable_spot'])

        if instance_types is "":
            raise InvalidOptionsError(
                strings['spot.instance_types_validation'])

        if itype and instance_types:
            raise InvalidOptionsError(strings['create.itype_and_instances'])

        platform = _determine_platform(platform, iprofile)

        app_name = self.get_app_name()
        tags = tagops.get_and_validate_tags(tags)
        envvars = get_and_validate_envars(envvars)
        process_app_version = fileoperations.env_yaml_exists() or process
        template_name = get_template_name(app_name, cfg)
        tier = get_environment_tier(tier)
        env_name = provided_env_name or get_environment_name(app_name, group)
        cname = cname or get_environment_cname(env_name, provided_env_name,
                                               tier)
        key_name = key_name or commonops.get_default_keyname()
        elb_type = elb_type or get_elb_type_from_customer(
            interactive, single, tier)
        enable_spot = enable_spot or spotops.get_spot_request_from_customer(
            interactive)
        instance_types = instance_types or spotops.get_spot_instance_types_from_customer(
            interactive, enable_spot)
        database = self.form_database_object()
        vpc = self.form_vpc_object(tier, single)

        if not timeout and database:
            timeout = 15

        env_request = CreateEnvironmentRequest(
            app_name=app_name,
            env_name=env_name,
            group_name=group,
            cname=cname,
            template_name=template_name,
            platform=platform,
            tier=tier,
            instance_type=itype,
            version_label=label,
            instance_profile=iprofile,
            service_role=service_role,
            single_instance=single,
            key_name=key_name,
            sample_application=sample,
            tags=tags,
            scale=scale,
            database=database,
            vpc=vpc,
            elb_type=elb_type,
            enable_spot=enable_spot,
            instance_types=instance_types,
            spot_max_price=spot_max_price,
            on_demand_base_capacity=on_demand_base_capacity,
            on_demand_above_base_capacity=on_demand_above_base_capacity,
            min_instances=min_instances,
            max_instances=max_instances)

        env_request.option_settings += envvars

        createops.make_new_env(env_request,
                               branch_default=branch_default,
                               process_app_version=process_app_version,
                               nohang=nohang,
                               interactive=interactive,
                               timeout=timeout,
                               source=source)
Exemple #12
0
    def do_command(self):
        # save command line args
        env_name = self.app.pargs.environment_name
        modules = self.app.pargs.modules
        if modules and len(modules) > 0:
            self.compose_multiple_apps()
            return
        group = self.app.pargs.env_group_suffix
        cname = self.app.pargs.cname
        tier = self.app.pargs.tier
        itype = self.app.pargs.instance_type
        solution_string = self.app.pargs.platform or solution_stack_ops.get_default_solution_stack(
        )
        single = self.app.pargs.single
        iprofile = self.app.pargs.instance_profile
        service_role = self.app.pargs.service_role
        label = self.app.pargs.version
        branch_default = self.app.pargs.branch_default
        key_name = self.app.pargs.keyname
        sample = self.app.pargs.sample
        nohang = self.app.pargs.nohang
        tags = self.app.pargs.tags
        envvars = self.app.pargs.envvars
        scale = self.app.pargs.scale
        timeout = self.app.pargs.timeout
        cfg = self.app.pargs.cfg
        elb_type = self.app.pargs.elb_type
        source = self.app.pargs.source
        process = self.app.pargs.process
        region = self.app.pargs.region
        interactive = False if env_name else True

        provided_env_name = env_name

        if sample and label:
            raise InvalidOptionsError(strings['create.sampleandlabel'])

        if single and scale:
            raise InvalidOptionsError(strings['create.singleandsize'])

        if single and elb_type:
            raise InvalidOptionsError(strings['create.single_and_elb_type'])

        if cname and tier and Tier.looks_like_worker_tier(tier):
            raise InvalidOptionsError(strings['worker.cname'])

        if cname and not elasticbeanstalk.is_cname_available(cname):
            raise AlreadyExistsError(strings['cname.unavailable'].replace(
                '{cname}', cname))

        if tier and Tier.looks_like_worker_tier(tier):
            if self.app.pargs.vpc_elbpublic or self.app.pargs.vpc_elbsubnets or self.app.pargs.vpc_publicip:
                raise InvalidOptionsError(
                    strings['create.worker_and_incompatible_vpc_arguments'])

        if (not tier or Tier.looks_like_webserver_tier(tier)) and single:
            if self.app.pargs.vpc_elbpublic or self.app.pargs.vpc_elbsubnets:
                raise InvalidOptionsError(
                    strings['create.single_and_elbpublic_or_elb_subnet'])

        app_name = self.get_app_name()
        tags = createops.get_and_validate_tags(tags)
        envvars = get_and_validate_envars(envvars)
        process_app_version = fileoperations.env_yaml_exists() or process
        platform = get_platform(solution_string, iprofile)
        template_name = get_template_name(app_name, cfg)
        tier = get_environment_tier(tier)
        env_name = provided_env_name or get_environment_name(app_name, group)
        cname = cname or get_environment_cname(env_name, provided_env_name,
                                               tier)
        key_name = key_name or commonops.get_default_keyname()
        elb_type = elb_type or get_elb_type_from_customer(
            interactive, single, region, tier)
        database = self.form_database_object()
        vpc = self.form_vpc_object(tier, single)

        # avoid prematurely timing out in the CLI when an environment is launched with a RDS DB
        if not timeout and database:
            timeout = 15

        env_request = CreateEnvironmentRequest(app_name=app_name,
                                               env_name=env_name,
                                               group_name=group,
                                               cname=cname,
                                               template_name=template_name,
                                               platform=platform,
                                               tier=tier,
                                               instance_type=itype,
                                               version_label=label,
                                               instance_profile=iprofile,
                                               service_role=service_role,
                                               single_instance=single,
                                               key_name=key_name,
                                               sample_application=sample,
                                               tags=tags,
                                               scale=scale,
                                               database=database,
                                               vpc=vpc,
                                               elb_type=elb_type)

        env_request.option_settings += envvars

        createops.make_new_env(env_request,
                               branch_default=branch_default,
                               process_app_version=process_app_version,
                               nohang=nohang,
                               interactive=interactive,
                               timeout=timeout,
                               source=source)