def cli(ctx, app_id, pickle_it, dry_run, force): """Restart app. :param app_id: **required**. Ensure create or update app with this name :type app_id: str Example: :: marathon restart-app --pickle my-app """ uri = 'apps/%s/restart' % app_id ctx.obj['logger'].debug({'app_id': app_id, 'uri': uri}) if force: uri += '?force=true' if dry_run: click.echo(format_json({'[DryRun] POST': uri})) return response = post(uri) ctx.obj['logger'].debug({'response': response}) if pickle_it: pickle_object(response, 'restart-app-{}'.format(app_id)) click.echo(format_json(response.json()))
def cli(ctx, wipe, scale, pickle_it, task_ids, dry_run): """Delete specified task ids. """ uri = 'tasks/delete' ctx.obj['logger'].debug({ 'uri': uri, 'wipe': wipe, 'scale': scale, 'pickle_it': pickle_it, 'task_ids': task_ids, 'dry_run': dry_run }) delete_task_ids = [] for tid in task_ids: delete_task_ids.append(tid) ctx.obj['logger'].debug({'delete_task_ids': delete_task_ids}) if dry_run: click.echo(format_json({'[DryRun] POST': uri})) return tasks = post(uri, json={"ids": delete_task_ids}) if pickle_it: pickle_object(tasks, 'delete-tasks') click.echo(format_json(tasks.json()))
def cli(ctx, app_id, pickle_it, dry_run): """Destroy app. :param app_id: **required**. Ensure create or update app with this name :type app_id: str Example: :: marathon delete-app --pickle my-app """ uri = 'apps/%s' % app_id ctx.obj['logger'].debug({'app_id': app_id, 'uri': uri}) if dry_run: click.echo(format_json({'[DryRun] DELETE': uri})) return response = delete(uri) ctx.obj['logger'].debug({'response': response}) if pickle_it: pickle_object(response, 'delete-app-{}'.format(app_id)) click.echo(format_json(response.json()))
def cli(ctx, app_id, template_file, template_vars, pickle_it, dry_run, force): """Update or create an app with id. :param app_id: **required**. Ensure create or update app with this name :type app_id: str :param template_file: **required**. jinja template file :type template_file: file :param template_vars: key value pairs used by the jinja template. - Environment variables: ``env=VAR_NAME`` - Contents of a file: ``file=FILE_NAME`` :type template_vars: mapping Example: :: marathon put-app --dry-run --pickle my-app app.json.j2 prj_key=foo env=AWS_ACCOUNT_ID app_env=dev file=VERSION """ uri = 'apps/' + app_id if force: uri += '/?force=true' ctx.obj['logger'].debug({'app_id': app_id, 'uri': uri, 'template_file': template_file, 'template_vars': template_vars}) jinja_vars = {'app_id': app_id} if template_vars: for var in template_vars: (key, value) = var.split('=') if key == 'env': key = value value = os.getenv(value) elif key == 'file': key = value value = open(key).read().strip() jinja_vars[key] = value app_request = Template(template_file.read()).render(**jinja_vars) app_request = json.loads(app_request) if dry_run: click.echo(format_json(app_request)) return response = put(uri, json=app_request) ctx.obj['logger'].debug({'response': response}) if pickle_it: pickle_object(response, 'put-app-{}'.format(app_id)) click.echo(format_json(response.json()))
def cli(ctx, app_id, pickle_it): """Get app tasks. """ uri = 'apps/%s/tasks/' % app_id ctx.obj['logger'].debug({'app_id': app_id, 'uri': uri}) tasks = get(uri) if pickle_it: pickle_object(tasks, 'app-tasks-{}'.format(app_id)) tasks = tasks.json() tasks = tasks.get('tasks') click.echo(format_json({'app_tasks': tasks, 'count': len(tasks)}))
def cli(ctx, app_ids, pickle_it): """Get apps deployed to a marathon instance. Retrieve all apps when no app ids are given. If an app id is provided, send a GET request for only that app id. If multiple app ids are provided, find those apps and return them. """ ctx.obj['logger'].info('Get all apps deployed to a marathon instance.') uri = 'apps/' if len(app_ids) == 1: uri += app_ids[0] ctx.obj['logger'].debug({'app_ids': app_ids, 'uri': uri}) apps = get(uri) if pickle_it: pickle_object(apps, 'apps') apps = apps.json() if len(app_ids) == 1: click.echo(format_json(apps)) return response = {'apps': [], 'count': 0} apps = apps.get('apps') if len(app_ids) > 1: for app_id in app_ids: if not app_id.startswith('/'): app_id = '/' + app_id for app in apps: if app.get('id') == app_id: response['apps'].append(app) else: response['apps'] = apps response['count'] = len(response['apps']) click.echo(format_json(response))
def cli(ctx, app_id, app_versions, pickle_it): """Get app versions. Retrieve all versions when no version is given. If a version id is provided, send a GET request for only that app version. If multiple versions are provided, find those app versions and return them. """ uri = 'apps/%s/versions/' % app_id if len(app_versions) == 1: uri += app_versions[0] ctx.obj['logger'].debug({'app_id': app_id, 'versions': app_versions, 'uri': uri}) versions = get(uri) if pickle_it: pickle_object(versions, 'app-versions-{}'.format(app_id)) versions = versions.json() if len(app_versions) == 1: click.echo(format_json(versions)) return response = {'app_versions': [], 'count': 0} versions = versions.get('versions') if len(app_versions) > 1: for version_id in app_versions: if not version_id.startswith('/'): version_id = '/' + version_id for version in versions: if version == version_id: response['app_versions'].append(get(uri + version_id)) else: response['app_versions'] = versions response['count'] = len(response['app_versions']) click.echo(format_json(response))
def get_request(ctx, uri, pickle_it, with_version=True): """Send GET request to Marathon instance. .. versionadded:: 1.1.8 """ response = get(uri, with_version=with_version) ctx.obj['logger'].debug({ 'connection.config': response.connection.config, 'elapsed': str(response.elapsed), 'encoding': response.encoding, 'headers': dict(response.headers), 'status_code': response.status_code, 'url': response.url, }) if pickle_it: pickle_object(response, uri.replace('/', '-')) try: return format_json(response.json()) except: return response.text
def __pickle_it(thing, uri): pickle_object( thing, 'delete-app-tasks-{}'.format(uri.replace('/', '__')) )