aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mchucarr...@apache.org
Subject git commit: Add a "config" noun with a "list" verb to list jobs defined in a config file.
Date Fri, 16 May 2014 16:06:04 GMT
Repository: incubator-aurora
Updated Branches:
  refs/heads/master cbe7b3b88 -> c8b467da1


Add a "config" noun with a "list" verb to list jobs defined in a config file.

Bugs closed: aurora-403

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


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

Branch: refs/heads/master
Commit: c8b467da11a2a5c692496075c8eec03291308f34
Parents: cbe7b3b
Author: Mark Chu-Carroll <mchucarroll@twopensource.com>
Authored: Fri May 16 11:55:11 2014 -0400
Committer: Mark Chu-Carroll <mchucarroll@twitter.com>
Committed: Fri May 16 11:55:11 2014 -0400

----------------------------------------------------------------------
 src/main/python/apache/aurora/client/cli/BUILD  |  1 +
 .../python/apache/aurora/client/cli/client.py   |  2 +
 .../python/apache/aurora/client/cli/config.py   | 90 ++++++++++++++++++++
 src/test/python/apache/aurora/client/cli/BUILD  | 14 +++
 .../aurora/client/cli/test_config_noun.py       | 86 +++++++++++++++++++
 5 files changed, 193 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/c8b467da/src/main/python/apache/aurora/client/cli/BUILD
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/cli/BUILD b/src/main/python/apache/aurora/client/cli/BUILD
index 0c5a8c5..3838bd3 100644
--- a/src/main/python/apache/aurora/client/cli/BUILD
+++ b/src/main/python/apache/aurora/client/cli/BUILD
@@ -42,6 +42,7 @@ python_library(
   name='cli',
   sources = [
     '__init__.py',
+    'config.py',
     'context.py',
     'command_hooks.py',
     'jobs.py',

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/c8b467da/src/main/python/apache/aurora/client/cli/client.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/cli/client.py b/src/main/python/apache/aurora/client/cli/client.py
index f7bafca..8dbc0f1 100644
--- a/src/main/python/apache/aurora/client/cli/client.py
+++ b/src/main/python/apache/aurora/client/cli/client.py
@@ -30,6 +30,8 @@ class AuroraCommandLine(CommandLine):
     super(AuroraCommandLine, self).register_nouns()
     from apache.aurora.client.cli.jobs import Job
     self.register_noun(Job())
+    from apache.aurora.client.cli.config import ConfigNoun
+    self.register_noun(ConfigNoun())
     from apache.aurora.client.cli.quota import Quota
     self.register_noun(Quota())
     from apache.aurora.client.cli.sla import Sla

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/c8b467da/src/main/python/apache/aurora/client/cli/config.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/cli/config.py b/src/main/python/apache/aurora/client/cli/config.py
new file mode 100644
index 0000000..fb5b92c
--- /dev/null
+++ b/src/main/python/apache/aurora/client/cli/config.py
@@ -0,0 +1,90 @@
+#
+# Copyright 2013 Apache Software Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""
+An implementation of a clientv2 "config" noun, for commands that
+operate in on configuration files.
+"""
+
+from __future__ import print_function
+
+from apache.aurora.client.cli import (
+    EXIT_COMMAND_FAILURE,
+    EXIT_OK,
+    Noun,
+    Verb,
+)
+from apache.aurora.client.cli.context import AuroraCommandContext
+from apache.aurora.client.cli.options import (
+    BIND_OPTION,
+    CONFIG_ARGUMENT,
+)
+from apache.aurora.client.config import AuroraConfig
+from apache.aurora.config.loader import AuroraConfigLoader
+
+
+class ListJobsCommand(Verb):
+  @property
+  def name(self):
+    return 'list'
+
+  @property
+  def help(self):
+    return "List all of the jobs defined in a configuration file"
+
+  def get_options(self):
+    return [BIND_OPTION, CONFIG_ARGUMENT]
+
+  def execute(self, context):
+    def maybe_bind(j):
+      return j.bind(*bindings) if bindings else j
+
+    def get_jobkey(job):
+      return "/".join([job.cluster().get(), job.role().get(), job.environment().get(),
+          job.name().get()])
+
+    try:
+      env = AuroraConfigLoader.load(context.options.config_file)
+    except (AuroraConfig.Error, AuroraConfigLoader.Error, ValueError) as e:
+      context.print_err("Error loading configuration file: %s" % e)
+      return EXIT_COMMAND_FAILURE
+    bindings = context.options.bindings
+    job_list = env.get("jobs", [])
+    if not job_list:
+      context.print_out("jobs=[]")
+    else:
+      bound_jobs = map(maybe_bind, job_list)
+      job_names = map(get_jobkey, bound_jobs)
+      context.print_out("jobs=[%s]" % (", ".join(job_names)))
+    return EXIT_OK
+
+
+class ConfigNoun(Noun):
+  @property
+  def name(self):
+    return 'config'
+
+  @property
+  def help(self):
+    return "Work with an aurora configuration file"
+
+  @classmethod
+  def create_context(cls):
+    return AuroraCommandContext()
+
+  def __init__(self):
+    super(ConfigNoun, self).__init__()
+    self.register_verb(ListJobsCommand())

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/c8b467da/src/test/python/apache/aurora/client/cli/BUILD
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/client/cli/BUILD b/src/test/python/apache/aurora/client/cli/BUILD
index 9766b3b..b73e7d9 100644
--- a/src/test/python/apache/aurora/client/cli/BUILD
+++ b/src/test/python/apache/aurora/client/cli/BUILD
@@ -21,6 +21,7 @@ python_test_suite(
     pants(':command_hooks'),
     pants(':help'),
     pants(':job'),
+    pants(':config'),
     pants(':logging'),
     pants(':plugins'),
     pants(':quota'),
@@ -83,6 +84,19 @@ python_tests(
 )
 
 python_tests(
+  name = 'config',
+  sources = [ 'test_config_noun.py' ],
+  dependencies = [
+    pants(':util'),
+    pants('3rdparty/python:mock'),
+    pants('3rdparty/python:twitter.common.contextutil'),
+    pants('src/main/python/apache/aurora/client/cli'),
+    pants('src/main/python/apache/aurora/client/cli:client_lib'),
+    pants('src/test/python/apache/aurora/client/commands:util')
+  ]
+)
+
+python_tests(
   name = 'job',
   sources = [
     'test_cancel_update.py',

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/c8b467da/src/test/python/apache/aurora/client/cli/test_config_noun.py
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/client/cli/test_config_noun.py b/src/test/python/apache/aurora/client/cli/test_config_noun.py
new file mode 100644
index 0000000..b9c974e
--- /dev/null
+++ b/src/test/python/apache/aurora/client/cli/test_config_noun.py
@@ -0,0 +1,86 @@
+#
+# Copyright 2013 Apache Software Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import contextlib
+import textwrap
+
+from twitter.common.contextutil import temporary_file
+
+from apache.aurora.client.cli.client import AuroraCommandLine
+from apache.aurora.client.cli.util import AuroraClientCommandTest, FakeAuroraCommandContext
+from mock import patch
+
+
+class TestClientCreateCommand(AuroraClientCommandTest):
+
+  def test_list_configs(self):
+    mock_context = FakeAuroraCommandContext()
+    with patch('apache.aurora.client.cli.config.ConfigNoun.create_context',
+        return_value=mock_context):
+      with temporary_file() as fp:
+        fp.write(self.get_valid_config())
+        fp.flush()
+        cmd = AuroraCommandLine()
+        cmd.execute(['config', 'list', fp.name])
+        assert mock_context.out == ['jobs=[west/bozo/test/hello]']
+        assert mock_context.err == []
+
+  def test_list_configs_invalid(self):
+    mock_context = FakeAuroraCommandContext()
+    with patch('apache.aurora.client.cli.config.ConfigNoun.create_context',
+        return_value=mock_context):
+      with temporary_file() as fp:
+        fp.write(self.get_invalid_config("blather=..."))
+        fp.flush()
+        cmd = AuroraCommandLine()
+        cmd.execute(['config', 'list', fp.name])
+        assert mock_context.out == []
+        assert any(line.startswith("Error loading configuration file: invalid syntax") for
line in
+            mock_context.err)
+
+  def get_config_with_no_jobs(self):
+    return textwrap.dedent("""
+      HELLO_WORLD = Job(
+        name = '%(job)s',
+        role = '%(role)s',
+        cluster = '%(cluster)s',
+        environment = '%(env)s',
+        instances = 20,
+        update_config = UpdateConfig(
+          batch_size = 5,
+          restart_threshold = 30,
+          watch_secs = 10,
+          max_per_shard_failures = 2,
+        ),
+        task = Task(
+          name = 'test',
+          processes = [Process(name = 'hello_world', cmdline = 'echo {{thermos.ports[http]}}')],
+          resources = Resources(cpu = 0.1, ram = 64 * MB, disk = 64 * MB),
+        )
+      )
+      """)
+
+  def test_list_configs_nojobs(self):
+    mock_context = FakeAuroraCommandContext()
+    with patch('apache.aurora.client.cli.config.ConfigNoun.create_context',
+        return_value=mock_context):
+      with temporary_file() as fp:
+        fp.write(self.get_config_with_no_jobs())
+        fp.flush()
+        cmd = AuroraCommandLine()
+        cmd.execute(['config', 'list', fp.name])
+        assert mock_context.out == ["jobs=[]"]
+        assert mock_context.err == []


Mime
View raw message