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
def delete_app_version_label(app_name, version_label): if version_label: # check if version_label exists under app_name app_versions = elasticbeanstalk.get_application_versions(app_name)['ApplicationVersions'] # if the given version label does not exist at all! if not any(version_label == app_version['VersionLabel'] for app_version in app_versions): raise ValidationError(strings['appversion.delete.notfound'].format(app_name, version_label)) envs = elasticbeanstalk.get_app_environments(app_name) versions_in_use = [(e.version_label, e.name) for e in envs] # find all the environments that are using the app version used_envs = [version[1] for version in versions_in_use if version[0] == version_label] if used_envs: raise ValidationError(strings['appversion.delete.deployed'].format(version_label, ','.join(used_envs))) try: io.validate_action(prompts['appversion.delete.validate'].format(version_label), "y") elasticbeanstalk.delete_application_version(app_name, version_label) io.echo('Application Version deleted successfully.') delete_successful = True except ValidationError: io.echo('Application Version will not be deleted.') delete_successful = False return delete_successful else: raise NotFoundError(strings['appversion.delete.none'])
def delete_app_version_label(app_name, version_label): if version_label: # check if version_label exists under app_name app_versions = elasticbeanstalk.get_application_versions(app_name)['ApplicationVersions'] # if the given version label does not exist at all! if not any(version_label == app_version['VersionLabel'] for app_version in app_versions): raise ValidationError(strings['appversion.delete.notfound'].format(app_name, version_label)) envs = elasticbeanstalk.get_app_environments(app_name) versions_in_use = [(e.version_label, e.name) for e in envs] # find all the environments that are using the app version used_envs = [version[1] for version in versions_in_use if version[0] == version_label] if used_envs: raise ValidationError(strings['appversion.delete.deployed'].format(version_label, ','.join(used_envs))) try: io.validate_action(prompts['appversion.delete.validate'].format(version_label), "y") elasticbeanstalk.delete_application_version(app_name, version_label) io.echo('Application Version deleted successfully.') delete_successful = True except ValidationError: io.echo('Application Version will not be deleted.') delete_successful = False return delete_successful else: raise NotFoundError(strings['appversion.delete.none'])
def do_command(self): app_name = self.get_app_name() num_to_leave = self.app.pargs.num_to_leave older_than = self.app.pargs.older_than force = self.app.pargs.force envs = elasticbeanstalk.get_app_environments(app_name) versions_in_use = [e.version_label for e in envs] app_versions = elasticbeanstalk.get_application_versions( app_name)['ApplicationVersions'] app_versions.sort(key=itemgetter('DateUpdated'), reverse=True) # Filter out versions currently being used app_versions = [ v for v in app_versions if v['VersionLabel'] not in versions_in_use ] total_num_unused_versions = len(app_versions) if total_num_unused_versions < num_to_leave: io.echo( 'Not enough unused application version to leave behind {0}; No application versions to delete.' .format(num_to_leave)) return # Filter out versions newer than filter date app_versions = [ v for v in app_versions if utils.get_delta_from_now_and_datetime( v['DateUpdated']).days > older_than ] # dont include most recent app_versions = app_versions[num_to_leave:] if app_versions: if not force: response = io.get_boolean_response( '{} application versions will be deleted. ' 'Continue?'.format(len(app_versions))) if not response: return else: io.echo('No application versions to delete.') return for version in app_versions: label = version['VersionLabel'] try: elasticbeanstalk.delete_application_version(app_name, label) except ServiceError as e: io.log_warning('Error deleting version {0}. Error: {1}'.format( label, e.message))
def delete_app_version_label(app_name, version_label): if version_label: app_versions = elasticbeanstalk.get_application_versions( app_name)['ApplicationVersions'] if not any(version_label == app_version['VersionLabel'] for app_version in app_versions): raise ValidationError(strings['appversion.delete.notfound'].format( app_name, version_label)) envs = elasticbeanstalk.get_app_environments(app_name) versions_in_use = [(e.version_label, e.name) for e in envs] used_envs = [ version[1] for version in versions_in_use if version[0] == version_label ] if used_envs: raise ValidationError(strings['appversion.delete.deployed'].format( version_label, ','.join(used_envs))) should_delete = io.get_boolean_response( text=prompts['appversion.delete.validate'].format(version_label), default=True) if not should_delete: io.echo('Application Version will not be deleted.') delete_successful = False else: elasticbeanstalk.delete_application_version( app_name, version_label) io.echo('Application Version deleted successfully.') delete_successful = True return delete_successful else: raise NotFoundError(strings['appversion.delete.none'])
def get_abortable_envs(app_name): envs = elasticbeanstalk.get_app_environments(app_name) return [e for e in envs if e.is_abortable]