def form_vpc_object(self, tier, single): vpc = self.app.pargs.vpc vpc_id = self.app.pargs.vpc_id ec2subnets = self.app.pargs.vpc_ec2subnets elbsubnets = self.app.pargs.vpc_elbsubnets elbpublic = self.app.pargs.vpc_elbpublic publicip = self.app.pargs.vpc_publicip securitygroups = self.app.pargs.vpc_securitygroups dbsubnets = self.app.pargs.vpc_dbsubnets database = self.app.pargs.database if vpc: # Interactively ask for vpc settings io.echo() vpc_id = vpc_id or io.get_input(prompts['vpc.id']) if not tier or tier.is_webserver(): publicip = publicip or io.get_boolean_response( text=prompts['vpc.publicip']) ec2subnets = ec2subnets or io.get_input(prompts['vpc.ec2subnets']) if (not tier or tier.is_webserver()) and not single: elbsubnets = elbsubnets or io.get_input( prompts['vpc.elbsubnets']) elbpublic = elbpublic or io.get_boolean_response( text=prompts['vpc.elbpublic']) securitygroups = securitygroups or io.get_input( prompts['vpc.securitygroups']) if database: dbsubnets = dbsubnets or io.get_input(prompts['vpc.dbsubnets']) if vpc_id or vpc: vpc_object = dict() vpc_object['id'] = vpc_id vpc_object['ec2subnets'] = ec2subnets if (not tier or tier.is_webserver()) and not single: vpc_object['elbsubnets'] = elbsubnets vpc_object['elbscheme'] = 'public' if elbpublic else 'internal' else: vpc_object['elbsubnets'] = None vpc_object['elbscheme'] = None if not tier or tier.is_webserver(): vpc_object['publicip'] = 'true' if publicip else 'false' else: vpc_object['publicip'] = None vpc_object['securitygroups'] = securitygroups vpc_object['dbsubnets'] = dbsubnets return vpc_object else: return {}
def test_get_boolean_response_false(self, get_input_mock): get_input_mock.side_effect = ['n', 'N', 'NO', 'no', 'No'] result1 = io.get_boolean_response() result2 = io.get_boolean_response() result3 = io.get_boolean_response() result4 = io.get_boolean_response() result5 = io.get_boolean_response() self.assertFalse(result1) self.assertFalse(result2) self.assertFalse(result3) self.assertFalse(result4) self.assertFalse(result5)
def test_get_boolean_response_true(self, get_input_mock): get_input_mock.side_effect = ['y', 'Y', 'YES', 'yes', 'Yes'] result1 = io.get_boolean_response() result2 = io.get_boolean_response() result3 = io.get_boolean_response() result4 = io.get_boolean_response() result5 = io.get_boolean_response() self.assertTrue(result1) self.assertTrue(result2) self.assertTrue(result3) self.assertTrue(result4) self.assertTrue(result5)
def test_get_boolean_response_bad(self, mock_io): response_list = ['a', '1', 'Ys', 'x', '', 'nah', '?', 'y'] mock_io.side_effect = response_list result = io.get_boolean_response() self.assertTrue(result) self.assertEqual(mock_io.call_count, len(response_list))
def get_shared_lb_from_customer(interactive, elb_type, platform, vpc=None): """ Prompt customer to select if they would like to use shared load balancer. Selection defaults to 'N' Prompt customer to select load balancer from a list, if they selected 'y' for previous prompt return: ARN of selected load balancer """ if not interactive or elb_type != elb_names.APPLICATION_VERSION: return alb_list = elasticbeanstalk.list_application_load_balancers(platform, vpc) if not alb_list: return should_continue = io.get_boolean_response( text=prompts['sharedlb.shared_load_balancer_request_prompt'], default=False) if not should_continue: return alb_list_display_labels = [] for load_balancer_arn in alb_list: load_balancer_name = parse_load_balancer_name(load_balancer_arn) alb_list_display_labels.append(load_balancer_name + ' - ' + load_balancer_arn) io.echo(prompts['sharedlb.shared_load_balancer_prompt']) selected_index = utils.prompt_for_index_in_list(alb_list_display_labels, default=1) return alb_list[selected_index]
def test_get_boolean_response__bad_input(self, get_input_mock): response_list = ['a', '1', 'Ys', 'x', '', 'nah', '?', 'y'] get_input_mock.side_effect = response_list result = io.get_boolean_response() self.assertTrue(result) self.assertEqual(get_input_mock.call_count, len(response_list))
def initialize_codecommit(): source_control = SourceControl.get_source_control() try: source_control_setup = source_control.is_setup() except CommandError: source_control_setup = False if not source_control_setup: io.log_error( "Cannot setup CodeCommit because there is no Source Control setup") return if codecommit.region_supported(): codecommit_setup = print_current_codecommit_settings() if codecommit_setup: should_continue = io.get_boolean_response( text='Do you wish to continue?', default=True) if not should_continue: return source_control.setup_codecommit_cred_config() from ebcli.controllers import initialize repository = initialize.get_repository_interactive() branch = initialize.get_branch_interactive(repository) set_repo_default_for_current_environment(repository) set_branch_default_for_current_environment(branch) else: io.log_error("The region {0} is not supported by CodeCommit".format( aws.get_region_name()))
def prompt_for_ec2_keyname(env_name=None, message=None, keyname=None): if message is None: message = prompts['ssh.setup'] if env_name: io.validate_action(prompts['terminate.validate'], env_name) else: io.echo(message) ssh = io.get_boolean_response() if not ssh: return None keys = [k['KeyName'] for k in ec2.get_key_pairs()] default_option = len(keys) if keyname: for index, key in enumerate(keys): if key == keyname: default_option = index + 1 if len(keys) < 1: keyname = _generate_and_upload_keypair(keys) else: new_key_option = '[ Create new KeyPair ]' keys.append(new_key_option) io.echo() io.echo(prompts['keypair.prompt']) keyname = utils.prompt_for_item_in_list(keys, default=default_option) if keyname == new_key_option: keyname = _generate_and_upload_keypair(keys) return keyname
def test_get_boolean_response__text_sans_default(self, get_input_mock): get_input_mock.return_value = 'y' result = io.get_boolean_response(text='This is the prompt text.') get_input_mock.assert_called_once_with( 'This is the prompt text. (Y/n)', default='y') self.assertTrue(result)
def detect_platform_family(families_set): detected_platform_family = heuristics.find_platform_family() if detected_platform_family and detected_platform_family in families_set: io.echo() io.echo(prompts['platform.validate'].format( platform=detected_platform_family)) correct = io.get_boolean_response() if correct: return detected_platform_family
def do_command(self): app_name = self.get_app_name() num_to_leave = self.app.pargs.num_to_leave older_than = self.app.pargs.older_than force = self.app.pargs.force envs = elasticbeanstalk.get_app_environments(app_name) versions_in_use = [e.version_label for e in envs] app_versions = elasticbeanstalk.get_application_versions( app_name)['ApplicationVersions'] app_versions.sort(key=itemgetter('DateUpdated'), reverse=True) # Filter out versions currently being used app_versions = [ v for v in app_versions if v['VersionLabel'] not in versions_in_use ] total_num_unused_versions = len(app_versions) if total_num_unused_versions < num_to_leave: io.echo( 'Not enough unused application version to leave behind {0}; No application versions to delete.' .format(num_to_leave)) return # Filter out versions newer than filter date app_versions = [ v for v in app_versions if utils.get_delta_from_now_and_datetime( v['DateUpdated']).days > older_than ] # dont include most recent app_versions = app_versions[num_to_leave:] if app_versions: if not force: response = io.get_boolean_response( '{} application versions will be deleted. ' 'Continue?'.format(len(app_versions))) if not response: return else: io.echo('No application versions to delete.') return for version in app_versions: label = version['VersionLabel'] try: elasticbeanstalk.delete_application_version(app_name, label) except ServiceError as e: io.log_warning('Error deleting version {0}. Error: {1}'.format( label, e.message))
def should_download_sample_app(): """ Method determines whether the present directory is empty. If yes, it allows the user to choose to download the sample application that the environment will be launched with. :return: User's choice of whether the sample application should be downloaded """ if heuristics.directory_is_empty(): return io.get_boolean_response( text=strings['create.sample_application_download_option'], default=True) return False
def get_spot_request_from_customer(interactive): """ Prompt customer to select if they would like to enable spot requests if operating in the interactive mode. Selection defaults to 'No' when provided with blank input. :param interactive: True/False depending on whether operating in the interactive mode or not :return: selected value for if to enable spot requests or not: True/False """ if not interactive: return io.echo() return io.get_boolean_response(text=prompts['spot.enable_spot_prompt'], default=False)
def configure_codecommit(source): source_location, repository, branch = utils.parse_source(source) source_control = SourceControl.get_source_control() if not source_location: should_continue = io.get_boolean_response(text=prompts['codecommit.usecc'], default=True) if not should_continue: LOG.debug("Denied option to use CodeCommit, continuing initialization") return repository, branch # Setup git config settings for code commit credentials source_control.setup_codecommit_cred_config() repository, branch = establish_codecommit_repository_and_branch(repository, branch, source_control, source_location) return repository, branch
def detect_platform(): """ Method attempts to guess the language name depending on the application source code and asks the customer to verify whether the guess is correct. :return: A string containing the name of the platform if the customer approves, otherwise None """ detected_platform = heuristics.find_language_type() if detected_platform: io.echo() io.echo(prompts['platform.validate'].replace('{platform}', detected_platform)) correct = io.get_boolean_response() if correct: return detected_platform
def scale(app_name, env_name, number, confirm, timeout=None): options = [] # get environment env = elasticbeanstalk.describe_configuration_settings( app_name, env_name)['OptionSettings'] # if single instance, offer to switch to load-balanced namespace = 'aws:elasticbeanstalk:environment' setting = next((n for n in env if n["Namespace"] == namespace), None) value = setting['Value'] if value == 'SingleInstance': if not confirm: ## prompt to switch to LoadBalanced environment type io.echo(prompts['scale.switchtoloadbalance']) io.log_warning(prompts['scale.switchtoloadbalancewarn']) switch = io.get_boolean_response() if not switch: return options.append({ 'Namespace': namespace, 'OptionName': 'EnvironmentType', 'Value': 'LoadBalanced' }) # change autoscaling min AND max to number namespace = 'aws:autoscaling:asg' max = 'MaxSize' min = 'MinSize' for name in [max, min]: options.append({ 'Namespace': namespace, 'OptionName': name, 'Value': str(number) }) request_id = elasticbeanstalk.update_environment(env_name, options) commonops.wait_for_success_events(request_id, timeout_in_minutes=timeout or 5, can_abort=True)
def restore_environment_num(self, environment_number): """Take in user input as a string, convert it to a decimal, get the environment that the user input matches, and attempt to restore that environment. """ environment_number = int(environment_number) # raises InvalidOperation Exception environments = self.poller.all_environments e_len = len(environments) if environment_number > e_len or environment_number < 1: raise IndexError environment = environments[e_len - environment_number] env_id = environment.get(u'EnvironmentId') should_exit_display = True if env_id: self.flusher(term.get_terminal()) prompt_text = prompts['restore.selectedenv'].format( env_id=env_id, app=utils.encode_to_ascii(environment.get('ApplicationName')), desc=utils.encode_to_ascii(environment.get('Description')), cname=utils.encode_to_ascii(environment.get('CNAME')), version=utils.encode_to_ascii(environment.get('VersionLabel')), platform=utils.encode_to_ascii(environment.get('SolutionStackName')), dat_term=environment.get('DateUpdated')) should_restore = io.get_boolean_response(prompt_text, default=True) if not should_restore: io.echo(responses['restore.norestore']) time.sleep(1) should_exit_display = False return should_exit_display from ebcli.operations import restoreops # restore specified environment self.request_id = restoreops.restore(env_id) return should_exit_display # Exception should never get thrown else: raise Exception
def delete_app_version_label(app_name, version_label): if version_label: app_versions = elasticbeanstalk.get_application_versions( app_name)['ApplicationVersions'] if not any(version_label == app_version['VersionLabel'] for app_version in app_versions): raise ValidationError(strings['appversion.delete.notfound'].format( app_name, version_label)) envs = elasticbeanstalk.get_app_environments(app_name) versions_in_use = [(e.version_label, e.name) for e in envs] used_envs = [ version[1] for version in versions_in_use if version[0] == version_label ] if used_envs: raise ValidationError(strings['appversion.delete.deployed'].format( version_label, ','.join(used_envs))) should_delete = io.get_boolean_response( text=prompts['appversion.delete.validate'].format(version_label), default=True) if not should_delete: io.echo('Application Version will not be deleted.') delete_successful = False else: elasticbeanstalk.delete_application_version( app_name, version_label) io.echo('Application Version deleted successfully.') delete_successful = True return delete_successful else: raise NotFoundError(strings['appversion.delete.none'])
def test_get_boolean_response(self, get_input_mock): get_input_mock.return_value = 'y' result = io.get_boolean_response() get_input_mock.assert_called_once_with('(Y/n)', default='y') self.assertTrue(result)