def setUp(self): super(TestAbort, self).setUp() fileoperations.create_config_file('myEBCLItest', 'us-west-2', 'my-stack-stack') commonops.set_environment_for_current_branch('my-env') mockservice.enqueue('elasticbeanstalk', 'AbortEnvironmentUpdate', standard_abort())
def compose_multiple_apps(self): module_names = self.app.pargs.modules group = self.app.pargs.env_group_suffix or 'dev' nohang = self.app.pargs.nohang timeout = self.app.pargs.timeout root_dir = os.getcwd() version_labels = [] grouped_env_names = [] app_name = None for module in module_names: if not os.path.isdir(os.path.join(root_dir, module)): io.log_warning(strings['create.appdoesntexist'].replace( '{app_name}', module)) continue os.chdir(os.path.join(root_dir, module)) if not fileoperations.env_yaml_exists(): io.log_warning(strings['compose.noenvyaml'].replace( '{module}', module)) continue io.echo( '--- Creating application version for module: {0} ---'.format( module)) # Re-run hooks to get values from .elasticbeanstalk folders of modules hooks.set_region(None) hooks.set_ssl(None) hooks.set_profile(None) commonops.set_group_suffix_for_current_branch(group) if not app_name: app_name = self.get_app_name() process_app_version = fileoperations.env_yaml_exists() version_label = commonops.create_app_version( app_name, process=process_app_version) version_labels.append(version_label) environment_name = fileoperations.get_env_name_from_env_yaml() if environment_name is not None: commonops.set_environment_for_current_branch( environment_name.replace('+', '-{0}'.format(group))) grouped_env_names.append( environment_name.replace('+', '-{0}'.format(group))) os.chdir(root_dir) if len(version_labels) > 0: composeops.compose(app_name, version_labels, grouped_env_names, group, nohang, timeout) else: io.log_warning(strings['compose.novalidmodules'])
def test_tags__environment_passed_as_positional__default_env_absent_in_config_yml( self, TagOps_mock, get_environment_arn_mock): tagops_mock = MagicMock() TagOps_mock.return_value = tagops_mock commonops.set_environment_for_current_branch(None) app = EB(argv=['tags', '--update', 'key1=value1', 'env_name']) app.setup() app.run() get_environment_arn_mock.assert_called_once_with('env_name') tagops_mock.update_tags.assert_called_once_with(tags.Environment)
def test_tags__other_resource_passed_as_resource_argument__default_env_absent_in_config_yml( self, TagOps_mock, ): tagops_mock = MagicMock() TagOps_mock.return_value = tagops_mock commonops.set_environment_for_current_branch(None) app = EB(argv=[ 'tags', '--update', 'key1=value1', '--resource', 'arn:aws:elasticbeanstalk:us-west-2:123123123:platform/custom-platform-2/1.0.0' ]) app.setup() app.run() tagops_mock.update_tags.assert_called_once_with(None)
def test_tags__environment_passed_as_resource_argument__default_env_present_in_config_yml( self, TagOps_mock, ): tagops_mock = MagicMock() TagOps_mock.return_value = tagops_mock commonops.set_environment_for_current_branch('default-env') app = EB(argv=[ 'tags', '--update', 'key1=value1', '--resource', 'arn:aws:elasticbeanstalk:us-west-2:123123123123:' 'environment/my-application/environment-1' ]) app.setup() app.run() tagops_mock.update_tags.assert_called_once_with(tags.Environment)
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 test_health__mono( self, get_instance_health_mock, get_environment_health_mock, describe_configuration_settings_mock ): fileoperations.create_config_file( 'my-application', 'us-west-2', 'php-7.2' ) commonops.set_environment_for_current_branch('environment-1') describe_configuration_settings_mock.return_value = mock_responses.DESCRIBE_CONFIGURATION_SETTINGS_RESPONSE__2['ConfigurationSettings'][0] get_environment_health_mock.return_value = mock_responses.DESCRIBE_ENVIRONMENT_HEALTH_RESPONSE get_instance_health_mock.return_value = mock_responses.DESCRIBE_INSTANCES_HEALTH_RESPONSE app = EB(argv=['health', '--mono']) app.setup() app.run()
def make_new_env(env_request, branch_default=False, process_app_version=False, nohang=False, interactive=True, timeout=None, source=None): resolve_roles(env_request, interactive) # Parse and get Build Configuration from BuildSpec if it exists build_config = None if fileoperations.build_spec_exists(): build_config = fileoperations.get_build_configuration() LOG.debug("Retrieved build configuration from buildspec: {0}".format( build_config.__str__())) # deploy code codecommit_setup = gitops.git_management_enabled() if not env_request.sample_application and not env_request.version_label: if source is not None: io.log_info('Creating new application version using remote source') io.echo("Starting environment deployment via remote source") env_request.version_label = commonops.create_app_version_from_source( env_request.app_name, source, process=process_app_version, label=env_request.version_label, build_config=build_config) process_app_version = True elif codecommit_setup: io.log_info('Creating new application version using CodeCommit') io.echo("Starting environment deployment via CodeCommit") env_request.version_label = \ commonops.create_codecommit_app_version(env_request.app_name, process=process_app_version, build_config=build_config) process_app_version = True else: io.log_info('Creating new application version using project code') env_request.version_label = \ commonops.create_app_version(env_request.app_name, process=process_app_version, build_config=build_config) if build_config is not None: buildspecops.stream_build_configuration_app_version_creation( env_request.app_name, env_request.version_label, build_config) elif process_app_version is True: success = commonops.wait_for_processed_app_versions( env_request.app_name, [env_request.version_label]) if not success: return if env_request.version_label is None or env_request.sample_application: env_request.version_label = \ commonops.create_dummy_app_version(env_request.app_name) # Create env if env_request.key_name: commonops.upload_keypair_if_needed(env_request.key_name) io.log_info('Creating new environment') result, request_id = create_env(env_request, interactive=interactive) env_name = result.name # get the (possibly) updated name # Edit configurations ## Get default environment default_env = commonops.get_current_branch_environment() ## Save env as branch default if needed if not default_env or branch_default: commonops.set_environment_for_current_branch(env_name) if codecommit_setup: io.echo("Setting up default branch") gitops.set_branch_default_for_current_environment( gitops.get_default_branch()) gitops.set_repo_default_for_current_environment( gitops.get_default_repository()) # Print status of env commonops.print_env_details(result, health=False) if nohang: return io.echo('Printing Status:') try: commonops.wait_for_success_events(request_id, timeout_in_minutes=timeout) except TimeoutError: io.log_error(strings['timeout.error'])
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 dissociate_environment_from_branch(env_name): default_env = commonops.get_current_branch_environment() if default_env == env_name: commonops.set_environment_for_current_branch(None)
def make_new_env( env_request, branch_default=False, process_app_version=False, nohang=False, interactive=True, timeout=None, source=None, ): resolve_roles(env_request, interactive) build_config = None if fileoperations.build_spec_exists(): build_config = fileoperations.get_build_configuration() LOG.debug("Retrieved build configuration from buildspec: {0}".format( build_config.__str__())) codecommit_setup = gitops.git_management_enabled() if not env_request.sample_application and not env_request.version_label: if source is not None: io.log_info('Creating new application version using remote source') io.echo("Starting environment deployment via remote source") env_request.version_label = commonops.create_app_version_from_source( env_request.app_name, source, process=process_app_version, label=env_request.version_label, build_config=build_config) process_app_version = True elif codecommit_setup: io.log_info('Creating new application version using CodeCommit') io.echo("Starting environment deployment via CodeCommit") env_request.version_label = \ commonops.create_codecommit_app_version(env_request.app_name, process=process_app_version, build_config=build_config) process_app_version = True else: io.log_info('Creating new application version using project code') env_request.version_label = \ commonops.create_app_version(env_request.app_name, process=process_app_version, build_config=build_config) if build_config is not None: buildspecops.stream_build_configuration_app_version_creation( env_request.app_name, env_request.version_label, build_config) elif process_app_version is True: success = commonops.wait_for_processed_app_versions( env_request.app_name, [env_request.version_label], timeout=timeout or 5) if not success: return if env_request.version_label is None or env_request.sample_application: env_request.version_label = \ commonops.create_dummy_app_version(env_request.app_name) if env_request.key_name: commonops.upload_keypair_if_needed(env_request.key_name) download_sample_app = None if interactive: download_sample_app = should_download_sample_app() io.log_info('Creating new environment') result, request_id = create_env(env_request, interactive=interactive) env_name = result.name default_env = commonops.get_current_branch_environment() if not default_env or branch_default: commonops.set_environment_for_current_branch(env_name) if codecommit_setup: io.echo("Setting up default branch") gitops.set_branch_default_for_current_environment( gitops.get_default_branch()) gitops.set_repo_default_for_current_environment( gitops.get_default_repository()) if download_sample_app: download_and_extract_sample_app(env_name) result.print_env_details(io.echo, elasticbeanstalk.get_environments, elasticbeanstalk.get_environment_resources, health=False) statusops.alert_environment_status(result) if nohang: return io.echo('Printing Status:') commonops.wait_for_success_events(request_id, timeout_in_minutes=timeout)
def switch_default_environment(env_name): __verify_environment_exists(env_name) commonops.set_environment_for_current_branch(env_name)
def compose_deploy(self): app_name = None modules = self.app.pargs.modules group_name = self.app.pargs.env_group_suffix env_names = [] stages_version_labels = {} stages_env_names = {} top_dir = getcwd() for module in modules: if not path.isdir(path.join(top_dir, module)): io.log_error(strings['deploy.notadirectory'].replace('{module}', module)) continue chdir(path.join(top_dir, module)) if not group_name: group_name = commonops.get_current_branch_group_suffix() if group_name not in stages_version_labels.keys(): stages_version_labels[group_name] = [] stages_env_names[group_name] = [] if not app_name: app_name = self.get_app_name() io.echo('--- Creating application version for module: {0} ---'.format(module)) hooks.set_region(None) hooks.set_ssl(None) hooks.set_profile(None) if not app_name: app_name = self.get_app_name() process_app_version = fileoperations.env_yaml_exists() version_label = commonops.create_app_version(app_name, process=process_app_version) stages_version_labels[group_name].append(version_label) environment_name = fileoperations.get_env_name_from_env_yaml() if environment_name is not None: commonops.set_environment_for_current_branch(environment_name. replace('+', '-{0}'. format(group_name))) env_name = commonops.get_current_branch_environment() stages_env_names[group_name].append(env_name) env_names.append(env_name) else: io.echo(strings['deploy.noenvname'].replace('{module}', module)) stages_version_labels[group_name] = [ v for v in stages_version_labels[group_name] if v != version_label ] chdir(top_dir) if len(stages_version_labels) > 0: for stage in stages_version_labels.keys(): request_id = composeops.compose_no_events(app_name, stages_version_labels[stage], group_name=stage) if request_id is None: io.log_error("Unable to compose modules.") return commonops.wait_for_compose_events(request_id, app_name, env_names, self.timeout) else: io.log_warning(strings['compose.novalidmodules'])
def make_new_env( env_request, branch_default=False, process_app_version=False, nohang=False, interactive=True, timeout=None, source=None, ): resolve_roles(env_request, interactive) # Parse and get Build Configuration from BuildSpec if it exists build_config = None if fileoperations.build_spec_exists(): build_config = fileoperations.get_build_configuration() LOG.debug("Retrieved build configuration from buildspec: {0}".format(build_config.__str__())) # deploy code codecommit_setup = gitops.git_management_enabled() if not env_request.sample_application and not env_request.version_label: if source is not None: io.log_info('Creating new application version using remote source') io.echo("Starting environment deployment via remote source") env_request.version_label = commonops.create_app_version_from_source( env_request.app_name, source, process=process_app_version, label=env_request.version_label, build_config=build_config) process_app_version = True elif codecommit_setup: io.log_info('Creating new application version using CodeCommit') io.echo("Starting environment deployment via CodeCommit") env_request.version_label = \ commonops.create_codecommit_app_version(env_request.app_name, process=process_app_version, build_config=build_config) process_app_version = True else: io.log_info('Creating new application version using project code') env_request.version_label = \ commonops.create_app_version(env_request.app_name, process=process_app_version, build_config=build_config) if build_config is not None: buildspecops.stream_build_configuration_app_version_creation(env_request.app_name, env_request.version_label, build_config) elif process_app_version is True: success = commonops.wait_for_processed_app_versions(env_request.app_name, [env_request.version_label]) if not success: return if env_request.version_label is None or env_request.sample_application: env_request.version_label = \ commonops.create_dummy_app_version(env_request.app_name) # Create env if env_request.key_name: commonops.upload_keypair_if_needed(env_request.key_name) download_sample_app = None if interactive: download_sample_app = should_download_sample_app() io.log_info('Creating new environment') result, request_id = create_env(env_request, interactive=interactive) env_name = result.name # get the (possibly) updated name # Edit configurations ## Get default environment default_env = commonops.get_current_branch_environment() ## Save env as branch default if needed if not default_env or branch_default: commonops.set_environment_for_current_branch(env_name) if codecommit_setup: io.echo("Setting up default branch") gitops.set_branch_default_for_current_environment(gitops.get_default_branch()) gitops.set_repo_default_for_current_environment(gitops.get_default_repository()) if download_sample_app: download_and_extract_sample_app(env_name) # Print status of env result.print_env_details( io.echo, elasticbeanstalk.get_environments, elasticbeanstalk.get_environment_resources, health=False ) if nohang: return io.echo('Printing Status:') commonops.wait_for_success_events(request_id, timeout_in_minutes=timeout)
def switch_default_environment(env_name): __verify_environment_exists(env_name) commonops.set_environment_for_current_branch(env_name)