aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zma...@apache.org
Subject incubator-aurora git commit: Remove single caller methods from AuroraCommandContext
Date Fri, 20 Feb 2015 23:56:36 GMT
Repository: incubator-aurora
Updated Branches:
  refs/heads/master e0e3f2e08 -> 439a168f9


Remove single caller methods from AuroraCommandContext

This continues the refactoring started in https://reviews.apache.org/r/30207/
which will allow for easier testing and mocking out of the context object. This
removes more single caller methods from the global context object and moves them
closer to the callers.

Testing Done:
./pants test.pytest --no-fast src/test/python/apache/aurora/client::

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


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

Branch: refs/heads/master
Commit: 439a168f9cdd6fe58a12009b51bedad4ac152948
Parents: e0e3f2e
Author: Zameer Manji <zmanji@apache.org>
Authored: Fri Feb 20 15:53:02 2015 -0800
Committer: Zameer Manji <zmanji@apache.org>
Committed: Fri Feb 20 15:53:02 2015 -0800

----------------------------------------------------------------------
 .../python/apache/aurora/client/cli/context.py  | 35 +-------------------
 .../python/apache/aurora/client/cli/jobs.py     | 16 +++++++--
 .../python/apache/aurora/client/cli/options.py  | 18 ++++++++++
 .../python/apache/aurora/client/cli/util.py     |  7 ----
 4 files changed, 32 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/439a168f/src/main/python/apache/aurora/client/cli/context.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/cli/context.py b/src/main/python/apache/aurora/client/cli/context.py
index 39754d5..ea64010 100644
--- a/src/main/python/apache/aurora/client/cli/context.py
+++ b/src/main/python/apache/aurora/client/cli/context.py
@@ -17,8 +17,6 @@ from __future__ import print_function
 import logging
 from fnmatch import fnmatch
 
-from pystachio import Ref
-
 from apache.aurora.client.base import AURORA_V2_USER_AGENT_NAME, combine_messages, synthesize_url
 from apache.aurora.client.cli import (
     Context,
@@ -36,25 +34,6 @@ from gen.apache.aurora.api.constants import ACTIVE_STATES
 from gen.apache.aurora.api.ttypes import ResponseCode
 
 
-def bindings_to_list(bindings):
-  """Pystachio takes bindings in the form of a list of dictionaries. Each pystachio binding
-  becomes another dictionary in the list. So we need to convert the bindings specified by
-  the user from a list of "name=value" formatted strings to a list of the dictionaries
-  expected by pystachio.
-  """
-  result = []
-  for b in bindings:
-    binding_parts = b.split("=")
-    if len(binding_parts) != 2:
-      raise ValueError('Binding parameter must be formatted name=value')
-    try:
-      ref = Ref.from_address(binding_parts[0])
-    except Ref.InvalidRefError as e:
-      raise ValueError("Could not parse binding parameter %s: %s" % (b, e))
-    result.append({ref: binding_parts[1]})
-  return result
-
-
 class AuroraCommandContext(Context):
 
   LOCK_ERROR_MSG = """Error: job is locked by an incomplete update.
@@ -90,12 +69,11 @@ class AuroraCommandContext(Context):
       # file without double-reading.
       with open(config_file, "r") as fp:
         logging.debug("Config: %s" % fp.readlines())
-      bindings = bindings_to_list(self.options.bindings) if self.options.bindings else None
       result = get_config(
           jobname,
           config_file,
           self.options.read_json,
-          bindings,
+          self.options.bindings,
           select_cluster=jobkey.cluster,
           select_role=jobkey.role,
           select_env=jobkey.env)
@@ -137,10 +115,6 @@ class AuroraCommandContext(Context):
         self.print_err("\t%s" % self.LOCK_ERROR_MSG)
       raise self.CommandErrorLogged(err_code, err_msg)
 
-  @classmethod
-  def render_partial_jobkey(cls, jobkey):
-    return "%s/%s/%s/%s" % jobkey
-
   def get_job_list(self, clusters, role=None):
     """Get a list of jobs from a group of clusters.
     :param clusters: the clusters to query for jobs
@@ -185,13 +159,6 @@ class AuroraCommandContext(Context):
           key.role, key.env, key.name)
       return jobs
 
-  def get_job_status(self, key):
-    """Returns a list of task instances."""
-    api = self.get_api(key.cluster)
-    resp = api.check_status(key)
-    self.log_response_and_raise(resp, err_code=EXIT_INVALID_PARAMETER)
-    return resp.result.scheduleStatusResult.tasks
-
   def get_active_instances(self, key):
     """Returns a list of the currently active instances of a job"""
     api = self.get_api(key.cluster)

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/439a168f/src/main/python/apache/aurora/client/cli/jobs.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/cli/jobs.py b/src/main/python/apache/aurora/client/cli/jobs.py
index f18a791..4485675 100644
--- a/src/main/python/apache/aurora/client/cli/jobs.py
+++ b/src/main/python/apache/aurora/client/cli/jobs.py
@@ -619,6 +619,13 @@ class StatusCommand(Verb):
       result.append(render_task_pretty(t))
     return "".join(result)
 
+  def _get_job_status(self, key, context):
+    """Returns a list of task instances."""
+    api = context.get_api(key.cluster)
+    resp = api.check_status(key)
+    context.log_response_and_raise(resp, err_code=EXIT_INVALID_PARAMETER)
+    return resp.result.scheduleStatusResult.tasks
+
   def get_status_for_jobs(self, jobkeys, context):
     """Retrieve and render the status information for a collection of jobs"""
     def is_active(task):
@@ -626,7 +633,7 @@ class StatusCommand(Verb):
 
     result = []
     for jk in jobkeys:
-      job_tasks = context.get_job_status(jk)
+      job_tasks = self._get_job_status(jk, context)
       if not job_tasks:
         logging.info("No tasks were found for jobkey %s" % jk)
         continue
@@ -645,10 +652,13 @@ class StatusCommand(Verb):
     else:
       return "".join(result)
 
+  def _render_partial_jobkey(self, jobkey):
+    return "%s/%s/%s/%s" % jobkey
+
   def _print_jobs_not_found(self, context):
     if context.options.write_json:
       context.print_out(json.dumps(
-        {"jobspec": context.render_partial_jobkey(context.options.jobspec),
+        {"jobspec": self._render_partial_jobkey(context.options.jobspec),
          "error": "No matching jobs found"},
         separators=[",", ":"],
         sort_keys=False))
@@ -657,7 +667,7 @@ class StatusCommand(Verb):
       return EXIT_OK
     else:
       context.print_err("Found no jobs matching %s" %
-          context.render_partial_jobkey(context.options.jobspec))
+          self._render_partial_jobkey(context.options.jobspec))
       return EXIT_INVALID_PARAMETER
 
   def execute(self, context):

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/439a168f/src/main/python/apache/aurora/client/cli/options.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/cli/options.py b/src/main/python/apache/aurora/client/cli/options.py
index 1106efa..d175e8c 100644
--- a/src/main/python/apache/aurora/client/cli/options.py
+++ b/src/main/python/apache/aurora/client/cli/options.py
@@ -15,6 +15,7 @@
 from argparse import ArgumentTypeError
 from collections import namedtuple
 
+from pystachio import Ref
 from twitter.common.quantity.parse_simple import parse_time
 
 from apache.aurora.common.aurora_job_key import AuroraJobKey
@@ -136,6 +137,22 @@ def instance_specifier(spec_str):
   return TaskInstanceKey(jobkey, instances)
 
 
+def binding_parser(binding):
+  """Pystachio takes bindings in the form of a list of dictionaries. Each pystachio binding
+  becomes another dictionary in the list. So we need to convert the bindings specified by
+  the user from a list of "name=value" formatted strings to a list of the dictionaries
+  expected by pystachio.
+  """
+  binding_parts = binding.split("=")
+  if len(binding_parts) != 2:
+    raise ValueError('Binding parameter must be formatted name=value')
+  try:
+    ref = Ref.from_address(binding_parts[0])
+  except Ref.InvalidRefError as e:
+    raise ValueError("Could not parse binding parameter %s: %s" % (binding, e))
+  return {ref: binding_parts[1]}
+
+
 BATCH_OPTION = CommandOption('--batch-size', type=int, default=1,
         help='Number of instances to be operate on in one iteration')
 
@@ -144,6 +161,7 @@ BIND_OPTION = CommandOption('--bind', dest='bindings',
     action='append',
     default=[],
     metavar="var=value",
+    type=binding_parser,
     help='Bind a pystachio variable name to a value. '
     'Multiple flags may be used to specify multiple values.')
 

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/439a168f/src/test/python/apache/aurora/client/cli/util.py
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/client/cli/util.py b/src/test/python/apache/aurora/client/cli/util.py
index d7096a7..6d3cc51 100644
--- a/src/test/python/apache/aurora/client/cli/util.py
+++ b/src/test/python/apache/aurora/client/cli/util.py
@@ -114,13 +114,6 @@ class FakeAuroraCommandContext(AuroraCommandContext):
   def open_page(self, url):
     self.showed_urls.append(url)
 
-  def handle_open(self, api):
-    pass
-
-  def timestamp_to_string(self, ts):
-    """To fuzz around timezone issues with timestamps, just return fixed strings."""
-    return "YYYY-MM-DD HH:MM:SS"
-
   def add_expected_status_query_result(self, expected_result):
     self.add_task_result(expected_result)
     self.fake_api.check_status.side_effect = self.task_result


Mime
View raw message