Ejemplo n.º 1
0
def prompt_for_language_name(language_names_to_display, module_name=None):
	"""
	Method prompts the customer to select a platform name in the interactive flow.

	:param language_names_to_display: A list of platform names to ask the customer
			to pick from.

			e.g.
				1. Node.js
				2. PHP
				3. Docker
				4. ...

	:param module_name: In case of a multi-module application, the name of the module
						the present selection of platform is for.

	:return: A string representing the platform the customer picked in the interactive
			flow
	"""
	chosen_solution_stack = detect_platform()

	if not chosen_solution_stack:
		io.echo()

		if not module_name:
			io.echo(prompts['platform.prompt'])
		else:
			io.echo(prompts['platform.prompt.withmodule'].replace('{module_name}', module_name))

		chosen_solution_stack = utils.prompt_for_item_in_list(language_names_to_display)

	return chosen_solution_stack
Ejemplo n.º 2
0
def prompt_customer_for_custom_platform_version(version_to_arn_mappings):
    custom_platform_versions_to_display = sorted(version_to_arn_mappings.keys())
    io.echo()
    io.echo(prompts['sstack.version'])
    chosen_custom_platform_version = utils.prompt_for_item_in_list(custom_platform_versions_to_display)

    return version_to_arn_mappings[chosen_custom_platform_version]
Ejemplo n.º 3
0
def _get_application_name_interactive():
    app_list = commonops.get_application_names()
    file_name = fileoperations.get_current_directory_name()
    new_app = False
    if len(app_list) > 0:
        io.echo()
        io.echo('Select an application to use')
        new_app_option = '[ Create new Application ]'
        app_list.append(new_app_option)
        try:
            default_option = app_list.index(file_name) + 1
        except ValueError:
            default_option = len(app_list)
        app_name = utils.prompt_for_item_in_list(app_list,
                                                 default=default_option)
        if app_name == new_app_option:
            new_app = True

    if len(app_list) == 0 or new_app:
        io.echo()
        io.echo('Enter Application Name')
        unique_name = utils.get_unique_name(file_name, app_list)
        app_name = io.prompt_for_unique_name(unique_name, app_list)

    return app_name
Ejemplo n.º 4
0
def get_shared_lb_port_from_customer(interactive, selected_lb):
    """
    Method accepts load balancer ARN
    Prompt customer to select port from list of listener port
    return: selected listener port
    """
    if not interactive or selected_lb is None:
        return

    result = elbv2.get_listeners_for_load_balancer(selected_lb)
    listener_list = [listener['Port'] for listener in result['Listeners']]
    listener_list.sort()
    default_listener_index = listener_list.index(
        80) + 1 if 80 in listener_list else None

    if len(listener_list) < 1:
        raise NotFoundError(alerts['sharedlb.listener'])
    elif len(listener_list) == 1:
        selected_listener_port = listener_list[0]
    else:
        io.echo(prompts['sharedlb.listener_prompt'])
        selected_listener_port = utils.prompt_for_item_in_list(
            listener_list, default=default_listener_index)

    return selected_listener_port
Ejemplo n.º 5
0
def get_platform_name_and_version_interactive():
    platforms = get_platforms(owner=Constants.OWNED_BY_SELF,
                              platform_version="latest")
    platform_list = list(platforms)

    file_name = fileoperations.get_current_directory_name()
    new_platform = False
    version = None

    if len(platform_list) > 0:
        io.echo()
        io.echo('Select a platform to use')
        new_platform_option = '[ Create new Platform ]'
        platform_list.append(new_platform_option)
        try:
            default_option = platform_list.index(file_name) + 1
        except ValueError:
            default_option = len(platform_list)
        platform_name = utils.prompt_for_item_in_list(platform_list,
                                                      default=default_option)
        if platform_name == new_platform_option:
            new_platform = True
        else:
            version = platforms[platform_name]

    if len(platform_list) == 0 or new_platform:
        io.echo()
        io.echo('Enter Platform Name')
        unique_name = utils.get_unique_name(file_name, platform_list)
        platform_name = io.prompt_for_unique_name(unique_name, platform_list)

    return platform_name, version
Ejemplo n.º 6
0
def pull_down_app_info(app_name, default_env=None):
    envs = elasticbeanstalk.get_app_environments(app_name)
    if len(envs) == 0:
        set_environment_for_current_branch(None)
        return None, None
    elif len(envs) == 1:
        env = envs[0]
        io.log_info('Setting only environment "' +
                    env.name + '" as default')
    elif len(envs) > 1:
        if default_env:
            if default_env == '/ni':
                env = envs[0]
            else:
                env = next((env for env in envs if env.name == default_env),
                           None)
        if not default_env or env is None:
            io.echo(prompts['init.selectdefaultenv'])
            env = utils.prompt_for_item_in_list(envs)

    set_environment_for_current_branch(env.name)

    io.log_info('Pulling down defaults from environment ' + env.name)
    keyname = elasticbeanstalk.get_specific_configuration_for_env(
        app_name, env.name, 'aws:autoscaling:launchconfiguration',
        'EC2KeyName'
    )
    if keyname is None:
        keyname = -1

    return env.platform.arn, keyname
Ejemplo n.º 7
0
def _get_application_name_interactive():
    app_list = elasticbeanstalk.get_application_names()
    file_name = fileoperations.get_current_directory_name()
    new_app = False
    if len(app_list) > 0:
        io.echo()
        io.echo('Select an application to use')
        new_app_option = '[ Create new Application ]'
        app_list.append(new_app_option)
        try:
            default_option = app_list.index(file_name) + 1
        except ValueError:
            default_option = len(app_list)
        app_name = utils.prompt_for_item_in_list(app_list,
                                                 default=default_option)
        if app_name == new_app_option:
            new_app = True

    if len(app_list) == 0 or new_app:
        io.echo()
        io.echo('Enter Application Name')
        unique_name = utils.get_unique_name(file_name, app_list)
        app_name = io.prompt_for_unique_name(unique_name, app_list)

    return app_name
Ejemplo n.º 8
0
def get_platform_name_and_version_interactive():
    platforms = get_platforms(owner=Constants.OWNED_BY_SELF, platform_version="latest")
    platform_list = list(platforms)

    file_name = fileoperations.get_current_directory_name()
    new_platform = False
    version = None

    if len(platform_list) > 0:
        io.echo()
        io.echo('Select a platform to use')
        new_platform_option = '[ Create new Platform ]'
        platform_list.append(new_platform_option)
        try:
            default_option = platform_list.index(file_name) + 1
        except ValueError:
            default_option = len(platform_list)
        platform_name = utils.prompt_for_item_in_list(platform_list, default=default_option)
        if platform_name == new_platform_option:
            new_platform = True
        else:
            version = platforms[platform_name]

    if len(platform_list) == 0 or new_platform:
        io.echo()
        io.echo('Enter Platform Name')
        unique_name = utils.get_unique_name(file_name, platform_list)
        platform_name = io.prompt_for_unique_name(unique_name, platform_list)

    return platform_name, version
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
def prompt_customer_for_custom_platform_version(version_to_arn_mappings):
    custom_platform_versions_to_display = sorted(version_to_arn_mappings.keys())
    io.echo()
    io.echo(prompts['sstack.version'])
    chosen_custom_platform_version = utils.prompt_for_item_in_list(custom_platform_versions_to_display)

    return version_to_arn_mappings[chosen_custom_platform_version]
Ejemplo n.º 11
0
def get_elb_type_from_customer(interactive, single, tier):
    """
    Prompt customer to specify the ELB type if operating in the interactive mode and
    on a load-balanced environment.

    Selection defaults to 'application' when provided with blank input.
    :param interactive: True/False depending on whether operating in the interactive mode or not
    :param single: False/True depending on whether environment is load balanced or not
    :param region: AWS region in which in load balancer will be created
    :param tier: the tier type of the environment
    :return: selected ELB type which is one among ['application', 'classic', 'network']
    """
    if single or (tier and not tier.is_webserver()):
        return
    elif not interactive:
        return elb_names.APPLICATION_VERSION

    io.echo()
    io.echo('Select a load balancer type')
    result = utils.prompt_for_item_in_list([
        elb_names.CLASSIC_VERSION, elb_names.APPLICATION_VERSION,
        elb_names.NETWORK_VERSION
    ],
                                           default=2)
    elb_type = result

    return elb_type
Ejemplo n.º 12
0
def get_branch_interactive(repository):
    source_control = SourceControl.get_source_control()
    # Give list of code commit branches to use
    new_branch = False
    branch_list = codecommit.list_branches(repository)["branches"]
    current_branch = source_control.get_current_branch()

    # If there are existing branches prompt the user to pick one
    if len(branch_list) > 0:
        io.echo('Select a branch')
        new_branch_option = '[ Create new Branch with local HEAD ]'
        branch_list.append(new_branch_option)
        try:
            default_option = branch_list.index(current_branch) + 1
        except ValueError:
            default_option = len(branch_list)
        branch_name = utils.prompt_for_item_in_list(branch_list,
                                                    default=default_option)
        if branch_name == new_branch_option:
            new_branch = True

    # Create a new branch if the user specifies or there are no existing branches

    if len(branch_list) == 0 or new_branch:
        new_branch = True
        io.echo()
        io.echo('Enter Branch Name')
        io.echo(
            '***** Must have at least one commit to create a new branch with CodeCommit *****'
        )
        unique_name = utils.get_unique_name(current_branch, branch_list)
        branch_name = io.prompt_for_unique_name(unique_name, branch_list)

    # Setup git to push to this repo
    result = codecommit.get_repository(repository)
    remote_url = result['repositoryMetadata']['cloneUrlHttp']
    source_control.setup_codecommit_remote_repo(remote_url=remote_url)

    if len(branch_list) == 0 or new_branch:
        LOG.debug("Creating a new branch")
        try:
            create_codecommit_branch(source_control, branch_name)
        except ServiceError:
            io.echo(
                "Could not set CodeCommit branch with the current commit, run with '--debug' to get the full error"
            )
            return None
    elif not new_branch:
        LOG.debug("Setting up an existing branch")
        succesful_branch = source_control.setup_existing_codecommit_branch(
            branch_name, remote_url)
        if not succesful_branch:
            io.echo(
                "Could not set CodeCommit branch, run with '--debug' to get the full error"
            )
            return None

    return branch_name
Ejemplo n.º 13
0
 def prompt_for_codesource(self):
     gitops.print_current_codecommit_settings()
     io.echo(prompts['codesource.codesourceprompt'])
     setup_choices = ['CodeCommit', 'Local']
     choice = utils.prompt_for_item_in_list(setup_choices, 2)
     if choice == setup_choices[0]:
         self.set_codecommit()
     elif choice == setup_choices[1]:
         self.set_local()
Ejemplo n.º 14
0
    def test_prompt_for_item_in_list__with_default(
        self,
        prompt_for_index_in_list_mock,
    ):
        lst = ['a', 'b', 'c']
        prompt_for_index_in_list_mock.return_value = 2

        result = utils.prompt_for_item_in_list(lst, default=2)

        prompt_for_index_in_list_mock.assert_called_once_with(lst, 2)
        self.assertEqual('c', result)
Ejemplo n.º 15
0
def get_branch_interactive(repository):
    source_control = SourceControl.get_source_control()
    # Give list of code commit branches to use
    new_branch = False
    branch_list = codecommit.list_branches(repository)["branches"]
    current_branch = source_control.get_current_branch()

    # If there are existing branches prompt the user to pick one
    if len(branch_list) > 0:
        io.echo('Select a branch')
        new_branch_option = '[ Create new Branch with local HEAD ]'
        branch_list.append(new_branch_option)
        try:
            default_option = branch_list.index(current_branch) + 1
        except ValueError:
            default_option = len(branch_list)
        branch_name = utils.prompt_for_item_in_list(branch_list,
                                                    default=default_option)
        if branch_name == new_branch_option:
            new_branch = True

    # Create a new branch if the user specifies or there are no existing branches

    if len(branch_list) == 0 or new_branch:
        new_branch = True
        io.echo()
        io.echo('Enter Branch Name')
        io.echo('***** Must have at least one commit to create a new branch with CodeCommit *****')
        unique_name = utils.get_unique_name(current_branch, branch_list)
        branch_name = io.prompt_for_unique_name(unique_name, branch_list)

    # Setup git to push to this repo
    result = codecommit.get_repository(repository)
    remote_url = result['repositoryMetadata']['cloneUrlHttp']
    source_control.setup_codecommit_remote_repo(remote_url=remote_url)

    if len(branch_list) == 0 or new_branch:
        LOG.debug("Creating a new branch")
        try:
            create_codecommit_branch(source_control, branch_name)
        except ServiceError:
            io.echo("Could not set CodeCommit branch with the current commit, run with '--debug' to get the full error")
            return None
    elif not new_branch:
        LOG.debug("Setting up an existing branch")
        succesful_branch = source_control.setup_existing_codecommit_branch(branch_name, remote_url)
        if not succesful_branch:
            io.echo("Could not set CodeCommit branch, run with '--debug' to get the full error")
            return None

    return branch_name
Ejemplo n.º 16
0
def prompt_for_platform_family(include_custom=False):
    families = list_nonretired_platform_families()
    families.sort()

    detected_platform_family = detect_platform_family(families)

    if detected_platform_family:
        return detected_platform_family

    if include_custom:
        families.append(prompts['platformfamily.prompt.customplatform'])

    io.echo(prompts['platformfamily.prompt'])
    return utils.prompt_for_item_in_list(families, default=None)
Ejemplo n.º 17
0
def prepare_for_ssh(env_name,
                    instance,
                    keep_open,
                    force,
                    setup,
                    number,
                    keyname=None,
                    no_keypair_error_message=None,
                    custom_ssh=None,
                    command=None,
                    timeout=None):
    if setup:
        setup_ssh(env_name, keyname, timeout=timeout)
        return

    if instance and number:
        raise InvalidOptionsError(strings['ssh.instanceandnumber'])

    if not instance:
        instances = commonops.get_instance_ids(env_name)
        if number is not None:
            if number > len(instances) or number < 1:
                raise InvalidOptionsError('Invalid index number (' +
                                          str(number) +
                                          ') for environment with ' +
                                          str(len(instances)) + ' instances')
            else:
                instance = instances[number - 1]

        elif len(instances) == 1:
            instance = instances[0]
        else:
            io.echo()
            io.echo('Select an instance to ssh into')
            instance = utils.prompt_for_item_in_list(instances)

    try:
        ssh_into_instance(instance,
                          keep_open=keep_open,
                          force_open=force,
                          custom_ssh=custom_ssh,
                          command=command)
    except NoKeypairError:
        if not no_keypair_error_message:
            no_keypair_error_message = prompts['ssh.nokey']
        io.log_error(no_keypair_error_message)
Ejemplo n.º 18
0
def get_region(region_argument, interactive, force_non_interactive=False):
    # Get region from command line arguments
    region = get_region_from_inputs(region_argument)

    # Ask for region
    if (not region) and force_non_interactive:
        # Choose defaults
        region_list = regions.get_all_regions()
        region = region_list[2].name

    if not region or (interactive and not region_argument):
        io.echo()
        io.echo('Select a default region')
        region_list = regions.get_all_regions()
        result = utils.prompt_for_item_in_list(region_list, default=3)
        region = result.name

    return region
Ejemplo n.º 19
0
def get_region(region_argument, interactive, force_non_interactive=False):
    # Get region from command line arguments
    region = get_region_from_inputs(region_argument)

    # Ask for region
    if (not region) and force_non_interactive:
        # Choose defaults
        region_list = regions.get_all_regions()
        region = region_list[2].name

    if not region or (interactive and not region_argument):
        io.echo()
        io.echo('Select a default region')
        region_list = regions.get_all_regions()
        result = utils.prompt_for_item_in_list(region_list, default=3)
        region = result.name

    return region
Ejemplo n.º 20
0
    def do_command(self):
        app_name = self.get_app_name()
        source_env = self.get_env_name()
        destination_env = self.app.pargs.destination_name

        if not destination_env:
            envs = elasticbeanstalk.get_environment_names(app_name)
            if len(envs) < 2:
                raise NotSupportedError(strings['swap.unsupported'])

            envs = [e for e in envs if e != source_env]
            if len(envs) == 1:
                destination_env = envs[0]
            else:
                io.echo()
                io.echo(prompts['swap.envprompt'])
                destination_env = utils.prompt_for_item_in_list(envs)

        swapops.cname_swap(source_env, destination_env)
Ejemplo n.º 21
0
    def get_region(self):
        # Get region from command line arguments
        region = self.get_region_from_inputs()

        # Ask for region
        if (not region) and self.force_non_interactive:
            # Choose defaults
            region_list = regions.get_all_regions()
            region = region_list[2].name

        if not region or \
                (self.interactive and not self.app.pargs.region):
            io.echo()
            io.echo('Select a default region')
            region_list = regions.get_all_regions()
            result = utils.prompt_for_item_in_list(region_list, default=3)
            region = result.name

        return region
Ejemplo n.º 22
0
def prompt_for_solution_stack_version(matching_language_versions):
	"""
	Method prompts customer to pick a solution stack version, given a set of platform
	versions of a language

	:param matching_language_versions: A list of platform versions of a language to allow
			the customer to choose from.

				e.g. Given Platform, Ruby, the following options will be presented
					1. Ruby 2.4 (Passenger standalone)
					2. Ruby 2.4 (Puma)
					3. ...

	:return: A string representing te platform version the customer would like to use.
	"""
	io.echo()
	io.echo(prompts['sstack.version'])
	language_versions_to_display = [version['PlatformShorthand'] for version in matching_language_versions]

	return utils.prompt_for_item_in_list(language_versions_to_display)
Ejemplo n.º 23
0
    def ssh(self):
        aws.set_region(self.region)
        aws.set_profile(self.profile)

        if self.environment_name is None:
            environment_names = get_all_environment_names()
            if environment_names:
                error = "Please chose one of the following environment names:\n\n"
                error += "\n".join(sorted(environment_names)) + "\n"
                io.log_error(error)
            else:
                io.log_error(
                    "The current Elastic Beanstalk application has no environments"
                )
            sys.exit()

        instances = get_instance_ids(self.environment_name)
        if len(instances) == 1:
            instance = instances[0]
        else:
            io.echo()
            io.echo('Select an instance to ssh into')
            instance = utils.prompt_for_item_in_list(instances)

        params = [
            "aws",
            "ssm",
            "start-session",
            "--document-name",
            "AWS-StartInteractiveCommand",
            "--parameters",
            "command='bash -l'",
            "--profile",
            self.profile,
            "--region",
            self.region,
            "--target",
            instance,
        ]

        os.system(" ".join(params))
Ejemplo n.º 24
0
def get_repository_interactive():
    source_control = SourceControl.get_source_control()
    # Give list of code commit repositories to use
    new_repo = False
    repo_list = codecommit.list_repositories()["repositories"]

    current_repository = source_control.get_current_repository()
    current_repository = fileoperations.get_current_directory_name() \
        if current_repository is None else current_repository

    # If there are existing repositories prompt the user to pick one
    # otherwise set default as the file name
    if len(repo_list) > 0:
        repo_list = list(map(lambda r: r["repositoryName"], repo_list))
        io.echo()
        io.echo('Select a repository')
        new_repo_option = '[ Create new Repository ]'
        repo_list.append(new_repo_option)

        try:
            default_option = repo_list.index(current_repository) + 1
        except ValueError:
            default_option = len(repo_list)
        repo_name = utils.prompt_for_item_in_list(repo_list,
                                                  default=default_option)
        if repo_name == new_repo_option:
            new_repo = True

    # Create a new repository if the user specifies or there are no existing repositories
    if len(repo_list) == 0 or new_repo:
        io.echo()
        io.echo('Enter Repository Name')
        unique_name = utils.get_unique_name(current_repository, repo_list)
        repo_name = io.prompt_for_unique_name(unique_name, repo_list)

        # Create the repository if we get here
        codecommit.create_repository(repo_name, "Created with EB CLI")
        io.echo("Successfully created repository: {0}".format(repo_name))

    return repo_name
Ejemplo n.º 25
0
    def do_command(self):
        app_name = self.get_app_name()
        source_env = self.get_env_name()
        destination_env = self.app.pargs.destination_name

        if not destination_env:
            # Ask interactively for an env to swap with
            envs = elasticbeanstalk.get_environment_names(app_name)
            if len(envs) < 2:
                raise NotSupportedError(strings['swap.unsupported'])

            # Filter out current env
            envs = [e for e in envs if e != source_env]
            if len(envs) == 1:
                # Don't ask for env, just swap with only other environment
                destination_env = envs[0]
            else:
                # Ask for env to swap with
                io.echo()
                io.echo(prompts['swap.envprompt'])
                destination_env = utils.prompt_for_item_in_list(envs)

        swapops.cname_swap(source_env, destination_env)
Ejemplo n.º 26
0
    def do_command(self):
        app_name = self.get_app_name()
        env_name = self.get_env_name(noerror=True)
        provided_env_name = bool(self.app.pargs.environment_name)

        if not provided_env_name:
            # Ask interactively for an env to abort
            envs = abortops.get_abortable_envs(app_name)
            if len(envs) < 1:
                raise NotFoundError(strings['abort.noabortableenvs'])
            if len(envs) == 1:
                # Don't ask for env, just abort only abortable environment
                env_name = envs[0].name
            else:
                # Ask for env to abort
                io.echo()
                io.echo(prompts['abort.envprompt'])
                env_name = utils.prompt_for_item_in_list(envs).name
        else:
            # Just do the abort if env_name is provided
            pass

        abortops.abort_operation(env_name)
Ejemplo n.º 27
0
def get_repository_interactive():
    source_control = SourceControl.get_source_control()
    # Give list of code commit repositories to use
    new_repo = False
    repo_list = codecommit.list_repositories()["repositories"]

    current_repository = source_control.get_current_repository()
    current_repository = current_repository or fileoperations.get_current_directory_name()

    # If there are existing repositories prompt the user to pick one
    # otherwise set default as the file name
    if len(repo_list) > 0:
        repo_list = list(map(lambda r: r["repositoryName"], repo_list))
        io.echo()
        io.echo('Select a repository')
        new_repo_option = '[ Create new Repository ]'
        repo_list.append(new_repo_option)

        try:
            default_option = repo_list.index(current_repository) + 1
        except ValueError:
            default_option = len(repo_list)
        repo_name = utils.prompt_for_item_in_list(repo_list,
                                                 default=default_option)
        if repo_name == new_repo_option:
            new_repo = True

    # Create a new repository if the user specifies or there are no existing repositories
    if len(repo_list) == 0 or new_repo:
        io.echo()
        io.echo('Enter Repository Name')
        unique_name = utils.get_unique_name(current_repository, repo_list)
        repo_name = io.prompt_for_unique_name(unique_name, repo_list)

        create_codecommit_repository(repo_name)

    return repo_name
Ejemplo n.º 28
0
def prompt_customer_for_custom_platform_name(custom_platforms):
    custom_platform_names_to_display = group_custom_platforms_by_platform_name(
        custom_platforms)

    return utils.prompt_for_item_in_list(custom_platform_names_to_display)
Ejemplo n.º 29
0
    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)
Ejemplo n.º 30
0
def get_branch_interactive(repository):
    source_control = SourceControl.get_source_control()
    # Give list of code commit branches to use
    new_branch = False
    branch_list = codecommit.list_branches(repository)["branches"]
    current_branch = source_control.get_current_branch()

    # If there are existing branches prompt the user to pick one
    if len(branch_list) > 0:
        io.echo()
        io.echo('Select a branch')
        new_branch_option = '[ Create new Branch with local HEAD ]'
        branch_list.append(new_branch_option)
        try:
            default_option = branch_list.index(current_branch) + 1
        except ValueError:
            default_option = len(branch_list)
        branch_name = utils.prompt_for_item_in_list(branch_list,
                                                    default=default_option)
        if branch_name == new_branch_option:
            new_branch = True

    # Create a new branch if the user specifies or there are no existing branches
    current_commit = source_control.get_current_commit()
    if len(branch_list) == 0 or new_branch:
        new_branch = True
        io.echo()
        io.echo('Enter Branch Name')
        io.echo(
            '***** Must have at least one commit to create a new branch with CodeCommit *****'
        )
        unique_name = utils.get_unique_name(current_branch, branch_list)
        branch_name = io.prompt_for_unique_name(unique_name, branch_list)

    # Setup git to push to this repo
    result = codecommit.get_repository(repository)
    source_control.setup_codecommit_remote_repo(
        remote_url=result['repositoryMetadata']['cloneUrlHttp'])

    if len(branch_list) == 0 or new_branch:
        LOG.debug("Creating a new branch")
        try:
            # Creating the branch requires that we setup the remote branch first
            # to ensure the code commit branch is synced with the local branch
            if current_commit is None:
                # TODO: Test on windows for weird empty returns with the staged files
                staged_files = source_control.get_list_of_staged_files()
                if staged_files is None or not staged_files:
                    source_control.create_initial_commit()
                else:
                    LOG.debug(
                        "Cannot create placeholder commit because there are staged files: {0}"
                        .format(staged_files))
                    io.echo(
                        "Could not set create a commit with staged files; cannot setup CodeCommit branch without a commit"
                    )
                    return None

                current_commit = source_control.get_current_commit()

            source_control.setup_new_codecommit_branch(branch_name=branch_name)
            codecommit.create_branch(repository, branch_name, current_commit)
            io.echo("Successfully created branch: {0}".format(branch_name))
        except ServiceError:
            io.echo(
                "Could not set CodeCommit branch with the current commit, run with '--debug' to get the full error"
            )
            return None
    elif not new_branch:
        LOG.debug("Setting up an existing branch")
        succesful_branch = source_control.setup_existing_codecommit_branch(
            branch_name)
        if not succesful_branch:
            io.echo(
                "Could not set CodeCommit branch, run with '--debug' to get the full error"
            )
            return None

    return branch_name
Ejemplo n.º 31
0
def prompt_customer_for_custom_platform_name(custom_platforms):
    custom_platform_names_to_display = group_custom_platforms_by_platform_name(custom_platforms)

    return utils.prompt_for_item_in_list(custom_platform_names_to_display)