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: Simplify AuroraCommandContext
Date Wed, 28 Jan 2015 19:30:49 GMT
Repository: incubator-aurora
Updated Branches:
  refs/heads/master 46a046661 -> 59025743b


Simplify AuroraCommandContext

The AuroraCommandContext class is used in multiple commands and contains common
code for all of them. However some portions are only used by one command. This
patch takes some of those portions and moves them to the command that requires
that functionality.

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

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


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

Branch: refs/heads/master
Commit: 59025743b81c6a012056701869ccc4c062d2c22a
Parents: 46a0466
Author: Zameer Manji <zmanji@apache.org>
Authored: Wed Jan 28 11:30:34 2015 -0800
Committer: Zameer Manji <zmanji@apache.org>
Committed: Wed Jan 28 11:30:34 2015 -0800

----------------------------------------------------------------------
 .../python/apache/aurora/client/cli/context.py  | 30 +-------------------
 .../python/apache/aurora/client/cli/jobs.py     | 22 ++++++++++++--
 .../python/apache/aurora/client/cli/update.py   | 10 +++----
 .../apache/aurora/client/cli/test_supdate.py    |  6 ++++
 .../apache/aurora/client/cli/test_update.py     | 12 ++++----
 5 files changed, 38 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/59025743/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 45fb404..f8b2899 100644
--- a/src/main/python/apache/aurora/client/cli/context.py
+++ b/src/main/python/apache/aurora/client/cli/context.py
@@ -15,8 +15,6 @@
 from __future__ import print_function
 
 import logging
-import time
-from collections import namedtuple
 from fnmatch import fnmatch
 
 from pystachio import Ref
@@ -37,9 +35,6 @@ from apache.aurora.common.clusters import CLUSTERS
 from gen.apache.aurora.api.constants import ACTIVE_STATES
 from gen.apache.aurora.api.ttypes import ResponseCode
 
-# Utility type, representing job keys with wildcards.
-PartialJobKey = namedtuple('PartialJobKey', ['cluster', 'role', 'env', 'name'])
-
 
 def bindings_to_list(bindings):
   """Pystachio takes bindings in the form of a list of dictionaries. Each pystachio binding
@@ -114,8 +109,7 @@ class AuroraCommandContext(Context):
 
   def open_job_page(self, api, jobkey):
     """Opens the page for a job in the system web browser."""
-    self.open_page(synthesize_url(api.scheduler_proxy.scheduler_client().url, jobkey.role,
-        jobkey.env, jobkey.name))
+    self.open_page(self.get_job_page(api, jobkey))
 
   def get_job_page(self, api, jobkey):
     return synthesize_url(api.scheduler_proxy.scheduler_client().url, jobkey.role,
@@ -141,20 +135,6 @@ class AuroraCommandContext(Context):
       raise self.CommandErrorLogged(err_code, err_msg)
 
   @classmethod
-  def parse_partial_jobkey(cls, key):
-    """Given a partial jobkey, where parts can be wildcards, parse it.
-    Slots that are wildcards will be replaced by "*".
-    """
-    parts = []
-    for part in key.split('/'):
-      parts.append(part)
-    if len(parts) > 4:
-      raise cls.CommandError(EXIT_INVALID_PARAMETER, 'Job key must have no more than 4 segments')
-    while len(parts) < 4:
-      parts.append('*')
-    return PartialJobKey(*parts)
-
-  @classmethod
   def render_partial_jobkey(cls, jobkey):
     return "%s/%s/%s/%s" % jobkey
 
@@ -224,11 +204,3 @@ class AuroraCommandContext(Context):
     if unrecognized:
       raise self.CommandError(EXIT_INVALID_PARAMETER,
           "Invalid instance parameter: %s" % (list(unrecognized)))
-
-  def timestamp_to_string(self, timestamp):
-    return time.ctime(timestamp)
-
-  def warn_and_pause(self, warning):
-    self.print_out(warning)
-    self.print_out("Press ^c within five seconds to abort.")
-    time.sleep(5)

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/59025743/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 1ac7145..7c53744 100644
--- a/src/main/python/apache/aurora/client/cli/jobs.py
+++ b/src/main/python/apache/aurora/client/cli/jobs.py
@@ -20,6 +20,8 @@ import os
 import pprint
 import subprocess
 import textwrap
+import time
+from collections import namedtuple
 from copy import deepcopy
 from datetime import datetime
 from tempfile import NamedTemporaryFile
@@ -64,9 +66,23 @@ from apache.aurora.common.aurora_job_key import AuroraJobKey
 from gen.apache.aurora.api.constants import ACTIVE_STATES, AURORA_EXECUTOR_NAME
 from gen.apache.aurora.api.ttypes import ExecutorConfig, ResponseCode, ScheduleStatus
 
+# Utility type, representing job keys with wildcards.
+PartialJobKey = namedtuple('PartialJobKey', ['cluster', 'role', 'env', 'name'])
+
 
 def arg_type_jobkey(key):
-  return AuroraCommandContext.parse_partial_jobkey(key)
+  """Given a partial jobkey, where parts can be wildcards, parse it.
+  Slots that are wildcards will be replaced by "*".
+  """
+  parts = []
+  for part in key.split('/'):
+    parts.append(part)
+  if len(parts) > 4:
+    raise ValueError('Job key must have no more than 4 segments')
+  while len(parts) < 4:
+    parts.append('*')
+  return PartialJobKey(*parts)
+
 WILDCARD_JOBKEY_OPTION = CommandOption("jobspec", type=arg_type_jobkey,
         metavar="cluster[/role[/env[/name]]]",
         help="A jobkey, optionally containing wildcards")
@@ -709,7 +725,9 @@ class UpdateCommand(Verb):
     if (local_task_count >= 4 * remote_task_count or
         4 * local_task_count <= remote_task_count or
         local_task_count == 0):
-      context.warn_and_pause("Warning: this update is a large change. ")
+      context.print_out("Warning: this update is a large change. ")
+      context.print_out("Press ^c within five seconds to abort.")
+      time.sleep(5)
 
   def execute(self, context):
     job = context.options.instance_spec.jobkey

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/59025743/src/main/python/apache/aurora/client/cli/update.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/cli/update.py b/src/main/python/apache/aurora/client/cli/update.py
index 56f9228..6e7e9c6 100644
--- a/src/main/python/apache/aurora/client/cli/update.py
+++ b/src/main/python/apache/aurora/client/cli/update.py
@@ -16,6 +16,7 @@ from __future__ import print_function
 
 import json
 import textwrap
+import time
 
 from apache.aurora.client.base import combine_messages
 from apache.aurora.client.cli import EXIT_API_ERROR, EXIT_COMMAND_FAILURE, EXIT_OK, Noun,
Verb
@@ -281,9 +282,8 @@ class UpdateStatus(Verb):
       context.print_out("Job: %s, UpdateID: %s" % (context.options.jobspec,
           details.update.summary.updateId))
       context.print_out("Started %s, last updated: %s" %
-          (context.timestamp_to_string(details.update.summary.state.createdTimestampMs),
-          context.timestamp_to_string(
-              details.update.summary.state.lastModifiedTimestampMs)))
+          (time.ctime(details.update.summary.state.createdTimestampMs),
+          time.ctime(details.update.summary.state.lastModifiedTimestampMs)))
       context.print_out("Current status: %s" %
           JobUpdateStatus._VALUES_TO_NAMES[details.update.summary.state.status])
       update_events = details.updateEvents
@@ -292,14 +292,14 @@ class UpdateStatus(Verb):
         for event in update_events:
           context.print_out("Status: %s at %s" % (
               JobUpdateStatus._VALUES_TO_NAMES[event.status],
-              context.timestamp_to_string(event.timestampMs)
+              time.ctime(event.timestampMs)
           ), indent=2)
       instance_events = details.instanceEvents
       if instance_events is not None and len(instance_events) > 0:
         context.print_out("Instance events:")
         for event in instance_events:
           context.print_out("Instance %s at %s: %s" % (
-            event.instanceId, context.timestamp_to_string(event.timestampMs),
+            event.instanceId, time.ctime(event.timestampMs),
             JobUpdateAction._VALUES_TO_NAMES[event.action]
           ), indent=2)
     return EXIT_OK

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/59025743/src/test/python/apache/aurora/client/cli/test_supdate.py
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/client/cli/test_supdate.py b/src/test/python/apache/aurora/client/cli/test_supdate.py
index 5c3e22f..0114e20 100644
--- a/src/test/python/apache/aurora/client/cli/test_supdate.py
+++ b/src/test/python/apache/aurora/client/cli/test_supdate.py
@@ -105,6 +105,12 @@ class TestStartUpdateCommand(AuroraClientCommandTest):
 
 class TestUpdateCommand(AuroraClientCommandTest):
 
+  def setUp(self):
+    patcher = patch("time.ctime")
+    self.addCleanup(patcher.stop)
+    mock_ctime = patcher.start()
+    mock_ctime.return_value = "YYYY-MM-DD HH:MM:SS"
+
   def test_start_update_command_line_succeeds(self):
     mock_context = FakeAuroraCommandContext()
     resp = self.create_simple_success_response()

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/59025743/src/test/python/apache/aurora/client/cli/test_update.py
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/client/cli/test_update.py b/src/test/python/apache/aurora/client/cli/test_update.py
index 7985436..c470ee6 100644
--- a/src/test/python/apache/aurora/client/cli/test_update.py
+++ b/src/test/python/apache/aurora/client/cli/test_update.py
@@ -387,14 +387,14 @@ class TestUpdateCommand(AuroraClientCommandTest):
         patch('apache.aurora.client.api.updater.SchedulerMux', return_value=fake_mux),
         patch('time.time', side_effect=functools.partial(self.fake_time, self)),
         patch('threading._Event.wait')):
-      with patch('apache.aurora.client.cli.context.AuroraCommandContext.warn_and_pause')
as pause:
+      with patch('time.sleep') as sleep:
         with temporary_file() as fp:
           fp.write(config)
           fp.flush()
           cmd = AuroraCommandLine()
           result = cmd.execute(['job', 'update', 'west/bozo/test/hello', fp.name])
           assert result == EXIT_OK
-          assert pause.call_count == 1
+          sleep.assert_called_once_with(5)
 
   def test_large_with_instances_warn(self):
     (mock_api, mock_scheduler_proxy) = self.create_mock_api()
@@ -416,13 +416,13 @@ class TestUpdateCommand(AuroraClientCommandTest):
         patch('apache.aurora.client.api.updater.SchedulerMux', return_value=fake_mux),
         patch('time.time', side_effect=functools.partial(self.fake_time, self)),
         patch('threading._Event.wait')):
-      with patch('apache.aurora.client.cli.context.AuroraCommandContext.warn_and_pause')
as pause:
+      with patch('time.sleep') as sleep:
         with temporary_file() as fp:
           fp.write(config)
           fp.flush()
           cmd = AuroraCommandLine()
           cmd.execute(['job', 'update', 'west/bozo/test/hello/1,3,40-160', fp.name])
-          assert pause.call_count == 1
+          sleep.assert_called_once_with(5)
 
   def test_large_with_instances_doesnt_warn(self):
     (mock_api, mock_scheduler_proxy) = self.create_mock_api()
@@ -443,13 +443,13 @@ class TestUpdateCommand(AuroraClientCommandTest):
         patch('apache.aurora.client.api.updater.SchedulerMux', return_value=fake_mux),
         patch('time.time', side_effect=functools.partial(self.fake_time, self)),
         patch('threading._Event.wait')):
-      with patch('apache.aurora.client.cli.context.AuroraCommandContext.warn_and_pause')
as pause:
+      with patch('time.sleep') as sleep:
         with temporary_file() as fp:
           fp.write(config)
           fp.flush()
           cmd = AuroraCommandLine()
           cmd.execute(['job', 'update', 'west/bozo/test/hello/1,3', fp.name])
-          assert pause.call_count == 0
+          assert sleep.call_count == 0
 
   @classmethod
   def assert_correct_addinstance_calls(cls, api):


Mime
View raw message