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 write_setting_to_current_branch(keyname, value): source_control = SourceControl.get_source_control() branch_name = source_control.get_current_branch() fileoperations.write_config_setting('branch-defaults', branch_name, {keyname: value})
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(commonops.get_default_region()): # Show the current setup if there is one and ask if they want to continue codecommit_setup = print_current_codecommit_settings() if codecommit_setup: try: io.validate_action("Do you wish to continue (y/n)", "y") except ValidationError: return # Setup git config settings for code commit credentials source_control.setup_codecommit_cred_config() # Get user desired repository from ..controllers import initialize repository = initialize.get_repository_interactive() branch = initialize.get_branch_interactive(repository) # set defaults for current environment 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(commonops.get_default_region()))
def cleanup_ignore_file(): sc = fileoperations.get_config_setting('global', 'sc') if sc: source_control = SourceControl.get_source_control() source_control.clean_up_ignore_file() fileoperations.write_config_setting('global', 'sc', None)
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(commonops.get_default_region()): # Show the current setup if there is one and ask if they want to continue codecommit_setup = print_current_codecommit_settings() if codecommit_setup: try: io.validate_action("Do you wish to continue (y/n)", "y") except ValidationError: return # Setup git config settings for code commit credentials source_control.setup_codecommit_cred_config() # Get user desired repository from ebcli.controllers import initialize repository = initialize.get_repository_interactive() branch = initialize.get_branch_interactive(repository) # set defaults for current environment 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(commonops.get_default_region()))
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
def setup_ignore_file(): io.log_info('Setting up ignore file for source control') sc = fileoperations.get_config_setting('global', 'sc') if not sc: source_control = SourceControl.get_source_control() source_control.set_up_ignore_file() sc_name = source_control.get_name() fileoperations.write_config_setting('global', 'sc', sc_name)
def create_codecommit_app_version(app_name, process=False, label=None, message=None, build_config=None): fileoperations.ProjectRoot.traverse() source_control = SourceControl.get_source_control() if source_control.get_current_commit() is None: io.log_warning( 'There are no commits for the current branch, attempting ' 'to create an empty commit and launching with the sample ' 'application') source_control.create_initial_commit() if source_control.untracked_changes_exist(): io.log_warning(strings['sc.unstagedchanges']) if label: version_label = label else: version_label = source_control.get_version_label() if message: description = message else: description = source_control.get_message() if len(description) > 200: description = description[:195] + '...' try: source_control.push_codecommit_code() except CommandError as e: io.echo("Could not push code to the CodeCommit repository:") raise e from ebcli.operations import gitops repository = gitops.get_default_repository() commit_id = source_control.get_current_commit() if repository is None or commit_id is None: raise ServiceError( "Could not find repository or commit id to create an application version" ) io.log_info('Creating AppVersion ' + version_label) return _create_application_version(app_name, version_label, description, None, None, process, repository=repository, commit_id=commit_id, build_config=build_config)
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
def __attempt_to_checkout_branch_specified_in_source_input(source): source_location, repo, branch = utils.parse_source(source) source_control = SourceControl.get_source_control() source_control.is_setup() repo = repo or gitops.get_default_repository() useops.switch_default_repo_and_branch(repo, branch) successfully_checked_out_branch = source_control.checkout_branch( branch) if not successfully_checked_out_branch: raise NotFoundError( "Could not checkout branch {0}.".format(branch))
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 get_setting_from_current_branch(keyname): try: source_control = SourceControl.get_source_control() branch_name = source_control.get_current_branch() except CommandError: LOG.debug("Git is not installed returning None for setting: %s".format(keyname)) return None branch_dict = fileoperations.get_config_setting('branch-defaults', branch_name) if branch_dict is None: return None else: try: return branch_dict[keyname] except KeyError: return None
def configure_codecommit(source): source_location, repository, branch = utils.parse_source(source) source_control = SourceControl.get_source_control() try: if not source_location: io.validate_action(prompts['codecommit.usecc'], "y") # 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) except ValidationError: LOG.debug("Denied option to use CodeCommit, continuing initialization") return repository, branch
def create_platform_version( version, major_increment, minor_increment, patch_increment, instance_type, vpc=None, staged=False, timeout=None, tags=None, ): _raise_if_directory_is_empty() _raise_if_platform_definition_file_is_missing() version and _raise_if_version_format_is_invalid(version) platform_name = fileoperations.get_platform_name() instance_profile = fileoperations.get_instance_profile(None) key_name = commonops.get_default_keyname() version = version or _resolve_version_number( platform_name, major_increment, minor_increment, patch_increment) tags = tagops.get_and_validate_tags(tags) source_control = SourceControl.get_source_control() io.log_warning(strings['sc.unstagedchanges'] ) if source_control.untracked_changes_exist() else None version_label = _resolve_version_label(source_control, staged) bucket, key, file_path = _resolve_s3_bucket_and_key( platform_name, version_label, source_control, staged) _upload_platform_version_to_s3_if_necessary(bucket, key, file_path) io.log_info('Creating Platform Version ' + version_label) response = elasticbeanstalk.create_platform_version( platform_name, version, bucket, key, instance_profile, key_name, instance_type, tags, vpc) environment_name = 'eb-custom-platform-builder-packer' io.echo( colored( strings['platformbuildercreation.info'].format(environment_name), attrs=['reverse'])) fileoperations.update_platform_version(version) commonops.set_environment_for_current_branch(environment_name) stream_platform_logs(response, platform_name, version, timeout)
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
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
def create_platform_version( version, major_increment, minor_increment, patch_increment, instance_type, vpc = None, staged=False, timeout=None): platform_name = fileoperations.get_platform_name() instance_profile = fileoperations.get_instance_profile(None) key_name = commonops.get_default_keyname() if version is None: version = _get_latest_version(platform_name=platform_name, owner=Constants.OWNED_BY_SELF, ignored_states=[]) if version is None: version = '1.0.0' else: major, minor, patch = version.split('.', 3) if major_increment: major = str(int(major) + 1) minor = '0' patch = '0' if minor_increment: minor = str(int(minor) + 1) patch = '0' if patch_increment or not(major_increment or minor_increment): patch = str(int(patch) + 1) version = "%s.%s.%s" % (major, minor, patch) if not VALID_PLATFORM_VERSION_FORMAT.match(version): raise InvalidPlatformVersionError(strings['exit.invalidversion']) cwd = os.getcwd() fileoperations._traverse_to_project_root() try: if heuristics.directory_is_empty(): raise PlatformWorkspaceEmptyError(strings['exit.platformworkspaceempty']) finally: os.chdir(cwd) if not heuristics.has_platform_definition_file(): raise PlatformWorkspaceEmptyError(strings['exit.no_pdf_file']) source_control = SourceControl.get_source_control() if source_control.untracked_changes_exist(): io.log_warning(strings['sc.unstagedchanges']) version_label = source_control.get_version_label() if staged: # Make a unique version label timestamp = datetime.now().strftime("%y%m%d_%H%M%S") version_label = version_label + '-stage-' + timestamp file_descriptor, original_platform_yaml = tempfile.mkstemp() os.close(file_descriptor) copyfile('platform.yaml', original_platform_yaml) s3_bucket = None s3_key = None try: # Add option settings to platform.yaml _enable_healthd() s3_bucket, s3_key = get_app_version_s3_location(platform_name, version_label) # Create zip file if the application version doesn't exist if s3_bucket is None and s3_key is None: file_name, file_path = _zip_up_project(version_label, source_control, staged=staged) else: file_name = None file_path = None finally: # Restore original platform.yaml move(original_platform_yaml, 'platform.yaml') # Use existing bucket if it exists bucket = elasticbeanstalk.get_storage_location() if s3_bucket is None else s3_bucket # Use existing key if it exists key = platform_name + '/' + file_name if s3_key is None else s3_key try: s3.get_object_info(bucket, key) io.log_info('S3 Object already exists. Skipping upload.') except NotFoundError: io.log_info('Uploading archive to s3 location: ' + key) s3.upload_platform_version(bucket, key, file_path) # Just deletes the local zip fileoperations.delete_app_versions() io.log_info('Creating Platform Version ' + version_label) response = elasticbeanstalk.create_platform_version( platform_name, version, bucket, key, instance_profile, key_name, instance_type, vpc) # TODO: Enable this once the API returns the name of the environment associated with a # CreatePlatformRequest, and remove hard coded value. There is currently only one type # of platform builder, we may support additional builders in the future. #environment_name = response['PlatformSummary']['EnvironmentName'] environment_name = 'eb-custom-platform-builder-packer' io.echo(colored( strings['platformbuildercreation.info'].format(environment_name), attrs=['reverse'])) fileoperations.update_platform_version(version) commonops.set_environment_for_current_branch(environment_name) arn = response['PlatformSummary']['PlatformArn'] request_id = response['ResponseMetadata']['RequestId'] if not timeout: timeout = 30 # Share streamer for platform events and builder events streamer = io.get_event_streamer() builder_events = threading.Thread( target=logsops.stream_platform_logs, args=(platform_name, version, streamer, 5, None, PackerStreamFormatter())) builder_events.daemon = True # Watch events from builder logs builder_events.start() commonops.wait_for_success_events( request_id, platform_arn=arn, streamer=streamer, timeout_in_minutes=timeout )
def create_platform_version(version, major_increment, minor_increment, patch_increment, instance_type, vpc=None, staged=False, timeout=None): platform_name = fileoperations.get_platform_name() instance_profile = fileoperations.get_instance_profile(None) key_name = commonops.get_default_keyname() if version is None: version = _get_latest_version(platform_name=platform_name, owner=Constants.OWNED_BY_SELF, ignored_states=[]) if version is None: version = '1.0.0' else: major, minor, patch = version.split('.', 3) if major_increment: major = str(int(major) + 1) minor = '0' patch = '0' if minor_increment: minor = str(int(minor) + 1) patch = '0' if patch_increment or not (major_increment or minor_increment): patch = str(int(patch) + 1) version = "%s.%s.%s" % (major, minor, patch) if not VALID_PLATFORM_VERSION_FORMAT.match(version): raise InvalidPlatformVersionError(strings['exit.invalidversion']) cwd = os.getcwd() fileoperations._traverse_to_project_root() try: if heuristics.directory_is_empty(): raise PlatformWorkspaceEmptyError( strings['exit.platformworkspaceempty']) finally: os.chdir(cwd) if not heuristics.has_platform_definition_file(): raise PlatformWorkspaceEmptyError(strings['exit.no_pdf_file']) source_control = SourceControl.get_source_control() if source_control.untracked_changes_exist(): io.log_warning(strings['sc.unstagedchanges']) version_label = source_control.get_version_label() if staged: # Make a unique version label timestamp = datetime.now().strftime("%y%m%d_%H%M%S") version_label = version_label + '-stage-' + timestamp file_descriptor, original_platform_yaml = tempfile.mkstemp() os.close(file_descriptor) copyfile('platform.yaml', original_platform_yaml) try: # Add option settings to platform.yaml _enable_healthd() s3_bucket, s3_key = get_app_version_s3_location( platform_name, version_label) # Create zip file if the application version doesn't exist if s3_bucket is None and s3_key is None: file_name, file_path = _zip_up_project(version_label, source_control, staged=staged) else: file_name = None file_path = None finally: # Restore original platform.yaml move(original_platform_yaml, 'platform.yaml') # Use existing bucket if it exists bucket = elasticbeanstalk.get_storage_location( ) if s3_bucket is None else s3_bucket # Use existing key if it exists key = platform_name + '/' + file_name if s3_key is None else s3_key try: s3.get_object_info(bucket, key) io.log_info('S3 Object already exists. Skipping upload.') except NotFoundError: io.log_info('Uploading archive to s3 location: ' + key) s3.upload_platform_version(bucket, key, file_path) # Just deletes the local zip fileoperations.delete_app_versions() io.log_info('Creating Platform Version ' + version_label) response = elasticbeanstalk.create_platform_version( platform_name, version, bucket, key, instance_profile, key_name, instance_type, vpc) # TODO: Enable this once the API returns the name of the environment associated with a # CreatePlatformRequest, and remove hard coded value. There is currently only one type # of platform builder, we may support additional builders in the future. #environment_name = response['PlatformSummary']['EnvironmentName'] environment_name = 'eb-custom-platform-builder-packer' io.echo( colored( strings['platformbuildercreation.info'].format(environment_name), attrs=['reverse'])) fileoperations.update_platform_version(version) commonops.set_environment_for_current_branch(environment_name) arn = response['PlatformSummary']['PlatformArn'] request_id = response['ResponseMetadata']['RequestId'] if not timeout: timeout = 30 # Share streamer for platform events and builder events streamer = io.get_event_streamer() builder_events = threading.Thread(target=logsops.stream_platform_logs, args=(platform_name, version, streamer, 5, None, PackerStreamFormatter())) builder_events.daemon = True # Watch events from builder logs builder_events.start() commonops.wait_for_success_events(request_id, platform_arn=arn, streamer=streamer, timeout_in_minutes=timeout)
def create_app_version_from_source( app_name, source, process=False, label=None, message=None, build_config=None ): cwd = os.getcwd() fileoperations.ProjectRoot.traverse() try: if heuristics.directory_is_empty(): io.echo('NOTE: {}'.format(strings['appversion.none'])) return None finally: os.chdir(cwd) source_control = SourceControl.get_source_control() if source_control.untracked_changes_exist(): io.log_warning(strings['sc.unstagedchanges']) if label: version_label = label else: version_label = source_control.get_version_label() if message: description = message else: description = source_control.get_message() if len(description) > 200: description = description[:195] + '...' source_location, repository, branch = utils.parse_source(source) if not branch or not repository: raise InvalidOptionsError(strings['codecommit.bad_source']) if source_location == "codecommit": try: result = codecommit.get_branch(repository, branch) except ServiceError as ex: io.log_error( "Could not get branch '{0}' for the repository '{1}' " "because of this error: {2}".format( branch, repository, ex.code ) ) raise ex commit_id = result['branch']['commitId'] if repository is None or commit_id is None: raise ServiceError("Could not find repository or commit id to create an application version") else: LOG.debug("Source location '{0}' is not supported".format(source_location)) raise InvalidOptionsError( "This command does not support the given source location: {0}".format( source_location ) ) io.log_info('Creating AppVersion ' + version_label) return _create_application_version(app_name, version_label, description, None, None, process, repository=repository, commit_id=commit_id, build_config=build_config)
def __attempt_to_change_branch_to_environment_default(): source_control = SourceControl.get_source_control() default_branch = gitops.get_branch_default_for_current_environment() if default_branch: source_control.checkout_branch(default_branch)
def create_app_version(app_name, process=False, label=None, message=None, staged=False, build_config=None): cwd = os.getcwd() fileoperations.ProjectRoot.traverse() try: if heuristics.directory_is_empty(): io.echo('NOTE: {}'.format(strings['appversion.none'])) return None finally: os.chdir(cwd) source_control = SourceControl.get_source_control() if source_control.untracked_changes_exist(): io.log_warning(strings['sc.unstagedchanges']) if label: version_label = label else: version_label = source_control.get_version_label() if staged: timestamp = datetime.now().strftime("%y%m%d_%H%M%S") version_label = version_label + '-stage-' + timestamp if message: description = message else: description = source_control.get_message() if len(description) > 200: description = description[:195] + '...' artifact = fileoperations.get_config_setting('deploy', 'artifact') if artifact: file_name, file_extension = os.path.splitext(artifact) file_name = version_label + file_extension file_path = artifact s3_key = None s3_bucket = None else: s3_bucket, s3_key = get_app_version_s3_location(app_name, version_label) if s3_bucket is None and s3_key is None: file_name, file_path = _zip_up_project( version_label, source_control, staged=staged) else: file_name = None file_path = None bucket = elasticbeanstalk.get_storage_location() if s3_bucket is None else s3_bucket key = app_name + '/' + file_name if s3_key is None else s3_key try: s3.get_object_info(bucket, key) io.log_info('S3 Object already exists. Skipping upload.') except NotFoundError: if file_name is None and file_path is None: raise NotFoundError('Application Version does not exist in the S3 bucket.' ' Try uploading the Application Version again.') io.log_info('Uploading archive to s3 location: ' + key) s3.upload_application_version(bucket, key, file_path) fileoperations.delete_app_versions() io.log_info('Creating AppVersion ' + version_label) return _create_application_version(app_name, version_label, description, bucket, key, process, build_config=build_config)
def do_command(self): # get arguments self.interactive = self.app.pargs.interactive self.region = self.app.pargs.region self.noverify = self.app.pargs.no_verify_ssl self.force_non_interactive = False # Determine if the customer is avoiding interactive mode by setting the platform flag if self.app.pargs.platform: self.force_non_interactive = True # Code Commit integration self.source = self.app.pargs.source source_location = None branch = None repository = None if self.source is not None: source_location, repository, branch = utils.parse_source( self.source) # The user specifies directories to initialize self.modules = self.app.pargs.modules if self.modules and len(self.modules) > 0: self.initialize_multiple_directories() return default_env = self.get_old_values() fileoperations.touch_config_folder() if self.interactive: self.region = get_region(self.region, self.interactive, self.force_non_interactive) else: self.region = get_region_from_inputs(self.app.pargs.region) aws.set_region(self.region) # Warn the customer if they picked a region that CodeCommit is not supported codecommit_region_supported = codecommit.region_supported(self.region) if self.source is not None and not codecommit_region_supported: io.log_warning(strings['codecommit.badregion']) self.region = set_up_credentials(self.app.pargs.profile, self.region, self.interactive) self.solution = self.get_solution_stack() self.app_name = self.get_app_name() if self.noverify: fileoperations.write_config_setting('global', 'no-verify-ssl', True) if not default_env and not self.interactive: # try to get default env from config file if exists try: default_env = commonops.get_current_branch_environment() except NotInitializedError: default_env = None elif self.interactive: default_env = None if self.force_non_interactive: default_env = '/ni' # Create application sstack, key = commonops.pull_down_app_info(self.app_name, default_env=default_env) if elasticbeanstalk.application_exist(self.app_name) \ else commonops.create_app(self.app_name, default_env=default_env) if not self.solution: self.solution = sstack platform_set = False if not self.solution or \ (self.interactive and not self.app.pargs.platform): if fileoperations.env_yaml_exists(): env_yaml_platform = fileoperations.get_platform_from_env_yaml() if env_yaml_platform: platform = solutionstack.SolutionStack( env_yaml_platform).version self.solution = platform platform_set = True if not platform_set: result = commonops.prompt_for_solution_stack() self.solution = result.version # Select CodeBuild image if BuildSpec is present do not prompt or show if we are non-interactive if fileoperations.build_spec_exists( ) and not self.force_non_interactive: build_spec = fileoperations.get_build_configuration() if build_spec is not None and build_spec.image is None: LOG.debug( "Buildspec file is present but image is does not exist. Attempting to fill best guess." ) platform_image = initializeops.get_codebuild_image_from_platform( self.solution) # If the return is a dictionary then it must be a single image and we can use that automatically if type(platform_image) is dict: io.echo('codebuild.latestplatform'.replace( '{platform}', self.solution)) else: # Otherwise we have an array for images which we must prompt the customer to pick from io.echo(prompts['codebuild.getplatform'].replace( '{platform}', self.solution)) selected = utils.prompt_for_index_in_list( map(lambda image: image['description'], platform_image)) platform_image = platform_image[selected] platform_image['name'] = utils.decode_bytes( platform_image['name']) # Finally write the CodeBuild image back to the buildspec file fileoperations.write_config_setting( fileoperations.buildspec_config_header, 'Image', platform_image['name'], file=fileoperations.buildspec_name) # Setup code commit integration # Ensure that git is setup source_control = SourceControl.get_source_control() try: source_control_setup = source_control.is_setup() if source_control_setup is None: source_control_setup = False except CommandError: source_control_setup = False default_branch_exists = False if gitops.git_management_enabled() and not self.interactive: default_branch_exists = True prompt_codecommit = True # Do not prompt if we are in non-interactive mode, the region is not supported for CodeCommit, # the specified source is from CodeCommit OR we already have default CodeCommit values set. if self.force_non_interactive \ or not codecommit.region_supported(self.region) \ or self.source is not None \ or default_branch_exists: prompt_codecommit = False # Prompt for interactive CodeCommit if prompt_codecommit: if not source_control_setup: io.echo(strings['codecommit.nosc']) else: io.echo(strings['codecommit.ccwarning']) try: io.validate_action(prompts['codecommit.usecc'], "y") # Setup git config settings for code commit credentials source_control.setup_codecommit_cred_config() # Get user specified repository if repository is None: repository = get_repository_interactive() else: try: result = codecommit.get_repository(repository) source_control.setup_codecommit_remote_repo( remote_url=result['repositoryMetadata'] ['cloneUrlHttp']) except ServiceError as ex: io.log_error(strings['codecommit.norepo']) raise ex # Get user specified branch if branch is None: branch = get_branch_interactive(repository) else: try: codecommit.get_branch(repository, branch) except ServiceError as ex: io.log_error(strings['codecommit.nobranch']) raise ex source_control.setup_existing_codecommit_branch(branch) except ValidationError: LOG.debug( "Denied option to use CodeCommit, continuing initialization" ) # Initialize the whole setup initializeops.setup(self.app_name, self.region, self.solution, dir_path=None, repository=repository, branch=branch) if 'IIS' not in self.solution: self.keyname = self.get_keyname(default=key) if self.keyname == -1: self.keyname = None fileoperations.write_config_setting('global', 'default_ec2_keyname', self.keyname) # Default to including git submodules when creating zip files through `eb create`/`eb deploy`. fileoperations.write_config_setting('global', 'include_git_submodules', True)
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
def create_app_version(app_name, process=False, label=None, message=None, staged=False, build_config=None): cwd = os.getcwd() fileoperations._traverse_to_project_root() try: if heuristics.directory_is_empty(): io.echo('NOTE: {}'.format(strings['appversion.none'])) return None finally: os.chdir(cwd) source_control = SourceControl.get_source_control() if source_control.untracked_changes_exist(): io.log_warning(strings['sc.unstagedchanges']) #get version_label if label: version_label = label else: version_label = source_control.get_version_label() if staged: # Make a unique version label timestamp = datetime.now().strftime("%y%m%d_%H%M%S") version_label = version_label + '-stage-' + timestamp # get description if message: description = message else: description = source_control.get_message() if len(description) > 200: description = description[:195] + '...' # Check for zip or artifact deploy artifact = fileoperations.get_config_setting('deploy', 'artifact') if artifact: file_name, file_extension = os.path.splitext(artifact) file_name = version_label + file_extension file_path = artifact s3_key = None s3_bucket = None else: # Check if the app version already exists s3_bucket, s3_key = get_app_version_s3_location( app_name, version_label) # Create zip file if the application version doesn't exist if s3_bucket is None and s3_key is None: file_name, file_path = _zip_up_project(version_label, source_control, staged=staged) else: file_name = None file_path = None # Get s3 location bucket = elasticbeanstalk.get_storage_location( ) if s3_bucket is None else s3_bucket key = app_name + '/' + file_name if s3_key is None else s3_key # Upload to S3 if needed try: s3.get_object_info(bucket, key) io.log_info('S3 Object already exists. Skipping upload.') except NotFoundError: # If we got the bucket/key from the app version describe call and it doesn't exist then # the application version must have been deleted out-of-band and we should throw an exception if file_name is None and file_path is None: raise NotFoundError( 'Application Version does not exist in the S3 bucket.' ' Try uploading the Application Version again.') # Otherwise attempt to upload the local application version io.log_info('Uploading archive to s3 location: ' + key) s3.upload_application_version(bucket, key, file_path) fileoperations.delete_app_versions() io.log_info('Creating AppVersion ' + version_label) return _create_application_version(app_name, version_label, description, bucket, key, process, build_config=build_config)
def create_codecommit_app_version(app_name, process=False, label=None, message=None, build_config=None): cwd = os.getcwd() fileoperations._traverse_to_project_root() source_control = SourceControl.get_source_control() if source_control.get_current_commit() is None: io.log_warning( 'There are no commits for the current branch, attempting to create an empty commit and launching with the sample application' ) source_control.create_initial_commit() if source_control.untracked_changes_exist(): io.log_warning(strings['sc.unstagedchanges']) #get version_label if label: version_label = label else: version_label = source_control.get_version_label() # get description if message: description = message else: description = source_control.get_message() if len(description) > 200: description = description[:195] + '...' # Push code with git try: source_control.push_codecommit_code() except CommandError as e: io.echo("Could not push code to the CodeCommit repository:") raise e # Get additional arguments for deploying code commit and poll # for the commit to propagate to code commit. from ebcli.operations import gitops repository = gitops.get_default_repository() commit_id = source_control.get_current_commit() if repository is None or commit_id is None: raise ServiceError( "Could not find repository or commit id to create an application version" ) # Deploy Application version with freshly pushed git commit io.log_info('Creating AppVersion ' + version_label) return _create_application_version(app_name, version_label, description, None, None, process, repository=repository, commit_id=commit_id, build_config=build_config)
def do_command(self): # get arguments self.interactive = self.app.pargs.interactive self.region = self.app.pargs.region self.noverify = self.app.pargs.no_verify_ssl self.force_non_interactive = False # Determine if the customer is avoiding interactive mode by setting the platform flag if self.app.pargs.platform: self.force_non_interactive = True # Code Commit integration self.source = self.app.pargs.source source_location = None branch = None repository = None if self.source is not None: source_location, repository, branch = utils.parse_source(self.source) # The user specifies directories to initialize self.modules = self.app.pargs.modules if self.modules and len(self.modules) > 0: self.initialize_multiple_directories() return default_env = self.get_old_values() fileoperations.touch_config_folder() if self.interactive: self.region = get_region(self.region, self.interactive, self.force_non_interactive) else: self.region = get_region_from_inputs(self.region) aws.set_region(self.region) self.region = set_up_credentials(self.app.pargs.profile, self.region, self.interactive) self.solution = self.get_solution_stack() self.app_name = self.get_app_name() if self.noverify: fileoperations.write_config_setting('global', 'no-verify-ssl', True) if not default_env and not self.interactive: # try to get default env from config file if exists try: default_env = commonops.get_current_branch_environment() except NotInitializedError: default_env = None elif self.interactive: default_env = None if self.force_non_interactive: default_env = '/ni' # Create application sstack, key = commonops.pull_down_app_info(self.app_name, default_env=default_env) if elasticbeanstalk.application_exist(self.app_name) \ else commonops.create_app(self.app_name, default_env=default_env) if not self.solution: self.solution = sstack platform_set = False if not self.solution or \ (self.interactive and not self.app.pargs.platform): if fileoperations.env_yaml_exists(): env_yaml_platform = fileoperations.get_platform_from_env_yaml() if env_yaml_platform: platform = solutionstack.SolutionStack(env_yaml_platform).platform_shorthand self.solution = platform platform_set = True if not platform_set: self.solution = solution_stack_ops.get_solution_stack_from_customer().platform_shorthand # Select CodeBuild image if BuildSpec is present do not prompt or show if we are non-interactive if fileoperations.build_spec_exists() and not self.force_non_interactive: build_spec = fileoperations.get_build_configuration() if build_spec is not None and build_spec.image is None: LOG.debug("Buildspec file is present but image is does not exist. Attempting to fill best guess.") platform_image = initializeops.get_codebuild_image_from_platform(self.solution) # If the return is a dictionary then it must be a single image and we can use that automatically if type(platform_image) is dict: io.echo('codebuild.latestplatform'.replace('{platform}', self.solution)) else: # Otherwise we have an array for images which we must prompt the customer to pick from io.echo(prompts['codebuild.getplatform'].replace('{platform}', self.solution)) selected = utils.prompt_for_index_in_list(map(lambda image: image['description'], platform_image)) platform_image = platform_image[selected] platform_image['name'] = utils.decode_bytes(platform_image['name']) # Finally write the CodeBuild image back to the buildspec file fileoperations.write_config_setting(fileoperations.buildspec_config_header, 'Image', platform_image['name'], file=fileoperations.buildspec_name) # Setup code commit integration # Ensure that git is setup source_control = SourceControl.get_source_control() try: source_control_setup = source_control.is_setup() if source_control_setup is None: source_control_setup = False except CommandError: source_control_setup = False default_branch_exists = False if gitops.git_management_enabled() and not self.interactive: default_branch_exists = True # Warn the customer if they picked a region that CodeCommit is not supported codecommit_region_supported = codecommit.region_supported(self.region) if self.source is not None and not codecommit_region_supported: io.log_warning(strings['codecommit.badregion']) # Prompt customer to opt into CodeCommit unless one of the follows holds: if self.force_non_interactive: prompt_codecommit = False elif not codecommit.region_supported(self.region): prompt_codecommit = False elif self.source and source_location.lower() != 'codecommit': # Do not prompt if customer has already specified a code source to # associate the EB workspace with prompt_codecommit = False elif default_branch_exists: # Do not prompt if customer has already configured the EB application # in the present working directory with Git prompt_codecommit = False else: prompt_codecommit = True # Prompt for interactive CodeCommit if prompt_codecommit: if not source_control_setup: io.echo(strings['codecommit.nosc']) else: io.echo(strings['codecommit.ccwarning']) try: if not self.source: io.validate_action(prompts['codecommit.usecc'], "y") # Setup git config settings for code commit credentials source_control.setup_codecommit_cred_config() # Get user specified repository remote_url = None if repository is None: repository = get_repository_interactive() else: try: setup_codecommit_remote_repo(repository, source_control) except ServiceError as ex: if self.source: create_codecommit_repository(repository) setup_codecommit_remote_repo(repository, source_control) else: io.log_error(strings['codecommit.norepo']) raise ex # Get user specified branch if branch is None: branch = get_branch_interactive(repository) else: try: codecommit.get_branch(repository, branch) except ServiceError as ex: if self.source: create_codecommit_branch(source_control, branch) else: io.log_error(strings['codecommit.nobranch']) raise ex source_control.setup_existing_codecommit_branch(branch, remote_url) except ValidationError: LOG.debug("Denied option to use CodeCommit, continuing initialization") # Initialize the whole setup initializeops.setup(self.app_name, self.region, self.solution, dir_path=None, repository=repository, branch=branch) if 'IIS' not in self.solution: self.keyname = self.get_keyname(default=key) if self.keyname == -1: self.keyname = None fileoperations.write_config_setting('global', 'default_ec2_keyname', self.keyname) # Default to including git submodules when creating zip files through `eb create`/`eb deploy`. fileoperations.write_config_setting('global', 'include_git_submodules', True)
def do_command(self): # get arguments self.interactive = self.app.pargs.interactive self.region = self.app.pargs.region self.noverify = self.app.pargs.no_verify_ssl self.force_non_interactive = False # Determine if the customer is avoiding interactive mode by setting the platform flag if self.app.pargs.platform: self.force_non_interactive = True # The user specifies directories to initialize self.modules = self.app.pargs.modules if self.modules and len(self.modules) > 0: self.initialize_multiple_directories() return source_location, branch, repository = None, None, None if self.app.pargs.source: source_location, repository, branch = utils.parse_source( self.app.pargs.source) default_env = self.get_old_values() fileoperations.touch_config_folder() self.region = set_region_for_application(self.interactive, self.region, self.force_non_interactive) self.region = set_up_credentials(self.app.pargs.profile, self.region, self.interactive) self.solution = self.get_solution_stack() self.app_name = self.get_app_name() if self.noverify: fileoperations.write_config_setting('global', 'no-verify-ssl', True) default_env = set_default_env(default_env, self.interactive, self.force_non_interactive) sstack, key = create_app_or_use_existing_one(self.app_name, default_env) self.solution = self.solution or sstack if fileoperations.env_yaml_exists(): self.solution = self.solution or extract_solution_stack_from_env_yaml( ) self.solution = self.solution or solution_stack_ops.get_solution_stack_from_customer( ).platform_shorthand handle_buildspec_image(self.solution, self.force_non_interactive) # Setup code commit integration # Ensure that git is setup source_control = SourceControl.get_source_control() try: source_control_setup = source_control.is_setup() if source_control_setup is None: source_control_setup = False except CommandError: source_control_setup = False default_branch_exists = not not (gitops.git_management_enabled() and not self.interactive) if source_location and not codecommit.region_supported(self.region): io.log_warning(strings['codecommit.badregion']) # Prompt customer to opt into CodeCommit unless one of the follows holds: if self.force_non_interactive: prompt_codecommit = False elif not codecommit.region_supported(self.region): prompt_codecommit = False elif source_location and source_location.lower() != 'codecommit': # Do not prompt if customer has already specified a code source to # associate the EB workspace with prompt_codecommit = False elif default_branch_exists: # Do not prompt if customer has already configured the EB application # in the present working directory with Git prompt_codecommit = False elif not source_control_setup: if source_location: io.echo(strings['codecommit.nosc']) prompt_codecommit = False else: prompt_codecommit = True # Prompt for interactive CodeCommit if prompt_codecommit: try: if not source_location: io.validate_action(prompts['codecommit.usecc'], "y") # Setup git config settings for code commit credentials source_control.setup_codecommit_cred_config() # Get user specified repository remote_url = None if repository is None: repository = get_repository_interactive() else: try: setup_codecommit_remote_repo(repository, source_control) except ServiceError as ex: if source_location: create_codecommit_repository(repository) setup_codecommit_remote_repo( repository, source_control) else: io.log_error(strings['codecommit.norepo']) raise ex # Get user specified branch if branch is None: branch = get_branch_interactive(repository) else: try: codecommit.get_branch(repository, branch) except ServiceError as ex: if source_location: create_codecommit_branch(source_control, branch) else: io.log_error(strings['codecommit.nobranch']) raise ex source_control.setup_existing_codecommit_branch( branch, remote_url) except ValidationError: LOG.debug( "Denied option to use CodeCommit, continuing initialization" ) # Initialize the whole setup initializeops.setup(self.app_name, self.region, self.solution, dir_path=None, repository=repository, branch=branch) if 'IIS' not in self.solution: self.keyname = self.get_keyname(default=key) if self.keyname == -1: self.keyname = None fileoperations.write_config_setting('global', 'default_ec2_keyname', self.keyname) # Default to including git submodules when creating zip files through `eb create`/`eb deploy`. fileoperations.write_config_setting('global', 'include_git_submodules', True)