Odyssey
cumanage.py
1 """
2 Run cumanage operations in an AWS hosted stack.
3 """
4 
5 import argparse
6 import shellish
7 import boto3
8 from . import base, s3
9 
10 
12  """ Run cumanage operations in an AWS hosted stack. """
13 
14  name = 'cumanage'
15 
16  def setup_args(self, parser):
17  self.add_stack_argument()
18  self.add_argument('proxyargs', nargs=argparse.REMAINDER)
19  self.add_argument(
20  '--passphrase', env='ODYSSEY_STACK_PASSPHRASE',
21  help='To avoid being prompted for a passphrase on protected'
22  'stacks, you can set it on the command line or via '
23  'environment variables.')
24  self.add_argument(
25  '--check-exit-code',
26  action='store_true',
27  help='Block and wait for the exit status of the cumanage task')
28 
29  def run(self, args):
30  stack = self.get_stack(args.stack)
31  if stack is None:
32  raise SystemExit('Stack not found: %s' % args.stack)
33  passphrase = stack.check_passphrase(args.passphrase)
34  wait_until_complete = args.check_exit_code
35 
36  exit(run_cumanage_task(
37  stack,
38  args.proxyargs,
39  wait_until_complete,
40  passphrase
41  ))
42 
43 
44 def task_exit_code(client, cluster, task_arns):
45  '''Check tasks exit codes on `cluster`'''
46  response = client.describe_tasks(cluster=cluster, tasks=task_arns)
47  tasks = response['tasks']
48  containers = [container for containers in (x['containers'] for x in tasks)
49  for container in containers]
50  exit_codes = [x.get('exitCode', -1) for x in containers]
51  for code in exit_codes:
52  if code != 0:
53  return code
54  return 0
55 
56 
58  proxyargs,
59  wait_until_complete=False,
60  passphrase=None):
61  '''Submit and run cumanage'''
62  if passphrase is None:
63  passphrase = ''
64  client = boto3.client('ecs', region_name=stack.region)
65  taskdef = 'odyssey-%s-cumanage-tool' % stack.name
66  shellish.vtmlprint("<b>Submitting:</b> <blue>%s</blue>" %
67  ' '.join(proxyargs))
68  overrides = {
69  "containerOverrides": [{
70  "name": "cumanage",
71  "command": proxyargs
72  }]
73  }
74  response = client.run_task(cluster=stack.cluster,
75  taskDefinition=taskdef,
76  overrides=overrides)
77  stack.save('Ran cumanage: %s' % ' '.join(proxyargs), passphrase)
78  if wait_until_complete:
79  task_arns = [x['taskArn'] for x in response['tasks']]
80  waiter = client.get_waiter('tasks_stopped')
81  if waiter.wait(cluster=stack.cluster, tasks=task_arns):
82  raise SystemExit('Task waiter timed out!')
83  return task_exit_code(client, stack.cluster, task_arns)
84  return 0
def run_cumanage_task(stack, proxyargs, wait_until_complete=False, passphrase=None)
Definition: cumanage.py:57
def task_exit_code(client, cluster, task_arns)
Definition: cumanage.py:44
def add_stack_argument(self, *args, env=DEFAULT_STACK_ENV, help=None, metavar='STACK_NAME', **kwargs)
Definition: base.py:56
def get_stack(self, name)
Definition: s3.py:136