aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wick...@apache.org
Subject git commit: Client v2: add support for options shared between verbs.
Date Thu, 19 Dec 2013 20:00:06 GMT
Updated Branches:
  refs/heads/master 16bb1442d -> 7ae9e70bc


Client v2: add support for options shared between verbs.

Reviewed at https://reviews.apache.org/r/16325/


Project: http://git-wip-us.apache.org/repos/asf/incubator-aurora/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-aurora/commit/7ae9e70b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-aurora/tree/7ae9e70b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-aurora/diff/7ae9e70b

Branch: refs/heads/master
Commit: 7ae9e70bc851970b994ab9e23faff198af9fa2fa
Parents: 16bb144
Author: Mark Chu-Carroll <mchucarroll@twitter.com>
Authored: Thu Dec 19 11:59:47 2013 -0800
Committer: Brian Wickman <wickman@twitter.com>
Committed: Thu Dec 19 11:59:47 2013 -0800

----------------------------------------------------------------------
 src/main/python/twitter/aurora/client/cli/BUILD |  2 +-
 .../twitter/aurora/client/cli/__init__.py       | 22 +++++++++++++---
 .../python/twitter/aurora/client/cli/jobs.py    | 27 ++++++++++----------
 .../python/twitter/aurora/client/cli/options.py | 25 ++++++++++++++++++
 4 files changed, 59 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/7ae9e70b/src/main/python/twitter/aurora/client/cli/BUILD
----------------------------------------------------------------------
diff --git a/src/main/python/twitter/aurora/client/cli/BUILD b/src/main/python/twitter/aurora/client/cli/BUILD
index aaeede4..0a44a21 100644
--- a/src/main/python/twitter/aurora/client/cli/BUILD
+++ b/src/main/python/twitter/aurora/client/cli/BUILD
@@ -7,7 +7,7 @@ python_binary(
 
 python_library(
   name='cli',
-  sources = [ '__init__.py', 'context.py', 'jobs.py' ],
+  sources = [ '__init__.py', 'context.py', 'jobs.py', 'options.py' ],
   dependencies = [
     pants('aurora/twitterdeps/src/python/twitter/common/python'),
     pants('src/main/python/twitter/aurora/client/api:command_runner'),

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/7ae9e70b/src/main/python/twitter/aurora/client/cli/__init__.py
----------------------------------------------------------------------
diff --git a/src/main/python/twitter/aurora/client/cli/__init__.py b/src/main/python/twitter/aurora/client/cli/__init__.py
index 4b771d6..2c08cf9 100644
--- a/src/main/python/twitter/aurora/client/cli/__init__.py
+++ b/src/main/python/twitter/aurora/client/cli/__init__.py
@@ -17,11 +17,9 @@ For example:
 
 from __future__ import print_function
 
-import sys
-
 from abc import abstractmethod
-
 import argparse
+import sys
 
 
 # Constants for standard return codes.
@@ -54,6 +52,18 @@ class Context(object):
     self.options = options
 
 
+class CommandOption(object):
+  """A lightweight encapsulation of an argparse option specification, which can be used to
+  define options that can be reused by multiple commands."""
+
+  def __init__(self, *args, **kwargs):
+    self.args = args
+    self.kwargs = kwargs
+
+  def add_to_parser(self, parser):
+    parser.add_argument(*self.args, **self.kwargs)
+
+
 class AuroraCommand(object):
   def setup_options_parser(self, argparser):
     """Set up command line options parsing for this command.
@@ -62,6 +72,12 @@ class AuroraCommand(object):
     """
     pass
 
+  def add_option(self, argparser, option):
+    """Add a predefined argument encapsulated an a CommandOption to an argument parser."""
+    if not isinstance(option, CommandOption):
+      raise TypeError('Command option object must be an instance of CommandOption')
+    option.add_to_parser(argparser)
+
   @property
   def help(self):
     """The help message for a command that will be used in the argparse help message"""

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/7ae9e70b/src/main/python/twitter/aurora/client/cli/jobs.py
----------------------------------------------------------------------
diff --git a/src/main/python/twitter/aurora/client/cli/jobs.py b/src/main/python/twitter/aurora/client/cli/jobs.py
index 58a723f..e66f181 100644
--- a/src/main/python/twitter/aurora/client/cli/jobs.py
+++ b/src/main/python/twitter/aurora/client/cli/jobs.py
@@ -5,6 +5,13 @@ from twitter.aurora.client.cli import (
     Verb
 )
 from twitter.aurora.client.cli.context import AuroraCommandContext
+from twitter.aurora.client.cli.options import (
+    BIND_OPTION,
+    BROWSER_OPTION,
+    CONFIG_OPTION,
+    JOBSPEC_OPTION,
+    JSON_OPTION
+)
 from twitter.aurora.common.aurora_job_key import AuroraJobKey
 
 from pystachio.config import Config
@@ -39,20 +46,15 @@ class CreateJobCommand(Verb):
   CREATE_STATES = ('PENDING', 'RUNNING', 'FINISHED')
 
   def setup_options_parser(self, parser):
-    parser.add_argument('--bind', type=str, default=[], dest='bindings',
-        action='append',
-        help='Bind a thermos mustache variable name to a value. '
-         'Multiple flags may be used to specify multiple values.')
-    parser.add_argument('--open-browser', default=False, dest='open_browser', action='store_true',
-        help='open browser to view job page after job is created')
-    parser.add_argument('--json', default=False, dest='json', action='store_true',
-        help='Read job configuration in json format')
+    self.add_option(parser, BIND_OPTION)
+    self.add_option(parser, BROWSER_OPTION)
+    self.add_option(parser, JSON_OPTION)
     parser.add_argument('--wait_until', choices=self.CREATE_STATES,
         default='PENDING',
         help=('Block the client until all the tasks have transitioned into the requested
state. '
                         'Default: PENDING'))
-    parser.add_argument('jobspec', type=AuroraJobKey.from_path)
-    parser.add_argument('config_file', type=str)
+    self.add_option(parser, JOBSPEC_OPTION)
+    self.add_option(parser, CONFIG_OPTION)
 
   def execute(self, context):
     try:
@@ -78,15 +80,14 @@ class KillJobCommand(Verb):
     return 'kill'
 
   def setup_options_parser(self, parser):
-    parser.add_argument('--open-browser', default=False, dest='open_browser', action='store_true',
-        help='open browser to view job page after job is created')
+    self.add_option(parser, BROWSER_OPTION)
     parser.add_argument('--instances', type=parse_instances, dest='instances', default=None,
         help='A list of instance ids to act on. Can either be a comma-separated list (e.g.
0,1,2) '
             'or a range (e.g. 0-2) or any combination of the two (e.g. 0-2,5,7-9). If not
set, '
             'all instances will be acted on.')
     parser.add_argument('--config', type=str, default=None, dest='config',
          help='Config file for the job, possibly containing hooks')
-    parser.add_argument('jobspec', type=AuroraJobKey.from_path)
+    self.add_option(parser, JOBSPEC_OPTION)
 
   def execute(self, context):
     api = context.get_api(context.options.jobspec.cluster)

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/7ae9e70b/src/main/python/twitter/aurora/client/cli/options.py
----------------------------------------------------------------------
diff --git a/src/main/python/twitter/aurora/client/cli/options.py b/src/main/python/twitter/aurora/client/cli/options.py
new file mode 100644
index 0000000..64a58f8
--- /dev/null
+++ b/src/main/python/twitter/aurora/client/cli/options.py
@@ -0,0 +1,25 @@
+from twitter.aurora.client.cli import CommandOption
+from twitter.aurora.common.aurora_job_key import AuroraJobKey
+
+
+BIND_OPTION = CommandOption('--bind', type=str, default=[], dest='bindings',
+    action='append',
+    help='Bind a thermos mustache variable name to a value. '
+    'Multiple flags may be used to specify multiple values.')
+
+
+BROWSER_OPTION = CommandOption('--open-browser', default=False, dest='open_browser',
+    action='store_true',
+    help='open browser to view job page after job is created')
+
+
+CONFIG_OPTION = CommandOption('config_file', type='str', dest='config_file',
+    help='pathname of the aurora configuration file contain the job specification')
+
+
+JOBSPEC_OPTION = CommandOption('jobspec', type=AuroraJobKey.from_path,
+    help='Fully specified job key, in CLUSTER/ROLE/ENV/NAME format')
+
+
+JSON_OPTION = CommandOption('--json', default=False, dest='json', action='store_true',
+    help='Read job configuration in json format')


Mime
View raw message