def get_environment_name(app_name, group): """ Returns: - environment name is present in the env.yaml file if one exists, or - prompts customer interactively to enter an environment name If using env.yaml to create an environment with, `group` must be passed through the `-g/--env-group-suffix/` argument. :param app_name: name of the application associated with the present working directory :param group: name of the group associated with :return: Unique name of the environment which will be created by `eb create` """ env_name = None if fileoperations.env_yaml_exists(): env_name = fileoperations.get_env_name_from_env_yaml() if env_name: if env_name.endswith('+') and not group: raise InvalidOptionsError(strings['create.missinggroupsuffix']) elif not env_name.endswith('+') and group: raise InvalidOptionsError( strings['create.missing_plus_sign_in_group_name']) else: env_name = env_name[:-1] + '-' + group return env_name or io.prompt_for_environment_name( get_unique_environment_name(app_name))
def test_prompt_for_environment_name(self, mock_prompt): invalid_cases = [ 'Inv@lid', 'sml', 'this-name-is-definitely-too-long-like-seriously-who-does-this', 'bad:char', 'bad_char', 'bad+char', 'bad=char', 'bad?char', 'bad>char', 'bad\\char', 'bad/char', 'bad#char', 'bad?char', '-startswithhyphen', 'endswithhyphen-', '-bothsideshyphen-', 'spaces are bad', # end in valid case for loop completion 'standard-name', ] valid_cases = [ 'ALL-UPPER', 'MiXeD-CaSe', 'normal-env-name', '1starts-with-num', 'ends-with-num1', 'has0-nums1-23422', 'this-name-is-just-right', 'this-name-is-now-fine-99', '1234567890123456789012345678901234567890', 'four'] mock_prompt.side_effect = invalid_cases result = io.prompt_for_environment_name() self.assertEqual(result, 'standard-name') self.assertEqual(mock_prompt.call_count, len(invalid_cases)) mock_prompt.side_effect = valid_cases for case in valid_cases: value = io.prompt_for_environment_name() self.assertEqual(value, case)
def create_env(env_request, interactive=True): # If a template is being used, we want to try using just the template if env_request.template_name: platform = env_request.platform env_request.platform = None else: platform = None while True: try: return elasticbeanstalk.create_environment(env_request) except InvalidParameterValueError as e: if e.message == responses['app.notexists'].replace( '{app-name}', '\'' + env_request.app_name + '\''): # App doesnt exist, must be a new region. ## Lets create the app in the region commonops.create_app(env_request.app_name) elif e.message == responses['create.noplatform']: if platform: env_request.platform = platform else: raise elif interactive: LOG.debug('creating env returned error: ' + e.message) if re.match(responses['env.cnamenotavailable'], e.message): io.echo(prompts['cname.unavailable']) cname = io.prompt_for_cname() elif re.match(responses['env.nameexists'], e.message): io.echo(strings['env.exists']) current_environments = elasticbeanstalk.get_all_environment_names( ) unique_name = utils.get_unique_name( env_request.env_name, current_environments) env_request.env_name = io.prompt_for_environment_name( default_name=unique_name) elif e.message == responses['app.notexists'].replace( '{app-name}', '\'' + env_request.app_name + '\''): # App doesnt exist, must be a new region. ## Lets create the app in the region commonops.create_app(env_request.app_name) else: raise else: raise
def create_env(env_request, interactive=True): # If a template is being used, we want to try using just the template if env_request.template_name: platform = env_request.platform env_request.platform = None else: platform = None while True: try: return elasticbeanstalk.create_environment(env_request) except InvalidParameterValueError as e: if e.message == responses['app.notexists'].replace( '{app-name}', '\'' + env_request.app_name + '\''): # App doesnt exist, must be a new region. ## Lets create the app in the region commonops.create_app(env_request.app_name) elif e.message == responses['create.noplatform']: if platform: env_request.platform = platform else: raise elif interactive: LOG.debug('creating env returned error: ' + e.message) if re.match(responses['env.cnamenotavailable'], e.message): io.echo(prompts['cname.unavailable']) cname = io.prompt_for_cname() elif re.match(responses['env.nameexists'], e.message): io.echo(strings['env.exists']) current_environments = elasticbeanstalk.get_all_environment_names() unique_name = utils.get_unique_name(env_request.env_name, current_environments) env_request.env_name = io.prompt_for_environment_name( default_name=unique_name) elif e.message == responses['app.notexists'].replace( '{app-name}', '\'' + env_request.app_name + '\''): # App doesnt exist, must be a new region. ## Lets create the app in the region commonops.create_app(env_request.app_name) else: raise else: raise
def clone_env(clone_request): while True: try: return elasticbeanstalk.clone_environment(clone_request) except InvalidParameterValueError as e: LOG.debug('cloning env returned error: ' + e.message) if re.match(responses['env.cnamenotavailable'], e.message): io.echo(prompts['cname.unavailable']) clone_request.cname = io.prompt_for_cname() elif re.match(responses['env.nameexists'], e.message): io.echo(strings['env.exists']) current_environments = elasticbeanstalk.get_environment_names( clone_request.app_name) unique_name = utils.get_unique_name(clone_request.env_name, current_environments) clone_request.env_name = io.prompt_for_environment_name( default_name=unique_name) else: raise
def do_command(self): app_name = self.get_app_name() env_name = self.get_env_name() clone_name = self.app.pargs.clone_name cname = self.app.pargs.cname scale = self.app.pargs.scale nohang = self.app.pargs.nohang tags = self.app.pargs.tags envvars = self.app.pargs.envvars exact = self.app.pargs.exact timeout = self.app.pargs.timeout provided_clone_name = clone_name is not None platform = None env = elasticbeanstalk.get_environment(app_name=app_name, env_name=env_name) tier = env.tier if 'worker' in tier.name.lower() and cname: raise InvalidOptionsError(strings['worker.cname']) if cname: if not elasticbeanstalk.is_cname_available(cname): raise AlreadyExistsError(strings['cname.unavailable'].replace( '{cname}', cname)) tags = tagops.get_and_validate_tags(tags) envvars = get_and_validate_envars(envvars) if not clone_name: if len(env_name) < 16: unique_name = env_name + '-clone' else: unique_name = 'my-cloned-env' env_list = elasticbeanstalk.get_environment_names(app_name) unique_name = utils.get_unique_name(unique_name, env_list) clone_name = io.prompt_for_environment_name( default_name=unique_name, prompt_text='Enter name for Environment Clone') if tier.name.lower() == 'webserver': if not cname and not provided_clone_name: cname = get_cname_from_customer(clone_name) elif not cname: cname = None if not exact: if not provided_clone_name: latest = solution_stack_ops.find_solution_stack_from_string( env.platform.name, find_newer=True) if latest != env.platform: io.echo() io.echo(prompts['clone.latest']) lst = [ 'Latest (' + str(latest) + ')', 'Same (' + str(env.platform) + ')' ] result = utils.prompt_for_item_in_list(lst) if result == lst[0]: platform = latest else: platform = latest else: platform = solution_stack_ops.find_solution_stack_from_string( env.platform.name, find_newer=True) if platform != env.platform: io.log_warning(prompts['clone.latestwarn']) clone_request = CloneEnvironmentRequest( app_name=app_name, env_name=clone_name, original_name=env_name, cname=cname, platform=platform, scale=scale, tags=tags, ) clone_request.option_settings += envvars cloneops.make_cloned_env(clone_request, nohang=nohang, timeout=timeout)