You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aurora.apache.org by ke...@apache.org on 2014/04/15 03:31:32 UTC

git commit: Add a new subcommand to aurora_admin to dump cluster config as a shell script.

Repository: incubator-aurora
Updated Branches:
  refs/heads/master 1d6284441 -> fad1b716e


Add a new subcommand to aurora_admin to dump cluster config as a shell script.

Useful to allow you to write shell scripts with the current cluster
settings without needing to duplicate information.

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


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

Branch: refs/heads/master
Commit: fad1b716e6eaac7ee9724ab5391f1bede5ec558a
Parents: 1d62844
Author: Kevin Sweeney <ke...@apache.org>
Authored: Mon Apr 14 18:30:03 2014 -0700
Committer: Kevin Sweeney <ke...@apache.org>
Committed: Mon Apr 14 18:30:52 2014 -0700

----------------------------------------------------------------------
 .../apache/aurora/client/commands/admin.py      | 25 +++++++++++++++
 src/main/python/apache/aurora/common/BUILD      |  9 ++++++
 .../python/apache/aurora/common/shellify.py     | 27 ++++++++++++++++
 src/test/python/apache/aurora/common/BUILD      |  9 ++++++
 .../apache/aurora/common/test_shellify.py       | 33 ++++++++++++++++++++
 5 files changed, 103 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/fad1b716/src/main/python/apache/aurora/client/commands/admin.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/commands/admin.py b/src/main/python/apache/aurora/client/commands/admin.py
index 2b37101..3765a2e 100644
--- a/src/main/python/apache/aurora/client/commands/admin.py
+++ b/src/main/python/apache/aurora/client/commands/admin.py
@@ -19,7 +19,10 @@ from __future__ import print_function
 """Command-line client for managing admin-only interactions with the aurora scheduler.
 """
 
+import json
 import os
+import pipes
+import sys
 import subprocess
 
 from apache.aurora.client.api import AuroraClientAPI
@@ -35,6 +38,7 @@ from apache.aurora.client.base import (
 )
 from apache.aurora.common.aurora_job_key import AuroraJobKey
 from apache.aurora.common.clusters import CLUSTERS
+from apache.aurora.common.shellify import shellify
 
 from gen.apache.aurora.constants import ACTIVE_STATES, TERMINAL_STATES
 from gen.apache.aurora.ttypes import (
@@ -455,3 +459,24 @@ def sla_probe_hosts(cluster, percentage, duration):
             for d in sorted(job_details)]))
 
   print_results(results)
+
+@app.command
+@app.command_option('--sh', default=False, action="store_true",
+  help="Emit a shell script instead of JSON.")
+@app.command_option('--export', default=False, action="store_true",
+  help="Emit a shell script prefixed with 'export'.")
+@requires.exactly('cluster')
+def get_cluster_config(cluster):
+  """usage: get_cluster_config [--sh] [--export] CLUSTER
+
+  Dumps the configuration for CLUSTER. By default we emit a json blob to stdout equivalent to
+  an entry in clusters.json. With --sh a shell script is written to stdout that can be used
+  with eval in a script to load the cluster config. With --export the shell script is prefixed
+  with 'export '."""
+  options = app.get_options()
+  cluster = CLUSTERS[cluster]
+  if not options.sh:
+    json.dump(cluster, sys.stdout)
+  else:
+    for line in shellify(cluster, options.export, prefix = "AURORA_CLUSTER_"):
+      print(line)

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/fad1b716/src/main/python/apache/aurora/common/BUILD
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/common/BUILD b/src/main/python/apache/aurora/common/BUILD
index 5d1cfe0..4646c05 100644
--- a/src/main/python/apache/aurora/common/BUILD
+++ b/src/main/python/apache/aurora/common/BUILD
@@ -61,6 +61,14 @@ python_library(
 )
 
 python_library(
+  name = 'shellify',
+  sources = ['shellify.py'],
+  dependencies = [
+    pants('3rdparty/python:twitter.common.lang'),
+  ],
+)
+
+python_library(
   name = 'common',
   dependencies = [
     pants(':aurora_job_key'),
@@ -68,6 +76,7 @@ python_library(
     pants(':cluster_option'),
     pants(':clusters'),
     pants(':http_signaler'),
+    pants(':shellify'),
     pants('src/main/python/apache/aurora/common/auth'),
 
     # covering dependency on gen.*

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/fad1b716/src/main/python/apache/aurora/common/shellify.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/common/shellify.py b/src/main/python/apache/aurora/common/shellify.py
new file mode 100644
index 0000000..67d81f0
--- /dev/null
+++ b/src/main/python/apache/aurora/common/shellify.py
@@ -0,0 +1,27 @@
+import pipes
+
+from twitter.common.lang import Compatibility
+
+
+def shellify(dict_, export=False, prefix=""):
+  """Dump a dict to a shell script."""
+  if export:
+    prefix = "export " + prefix
+  def _recurse(k, v, prefix):
+    if isinstance(v, bool):
+      v = int(v)
+    if isinstance(v, int):
+      yield "%s=%s" % (prefix + k, + v)
+    if isinstance(v, Compatibility.string):
+      yield "%s=%s" % (prefix + k, pipes.quote(str(v)))
+    elif isinstance(v, dict):
+      for k1, v1 in v.items():
+        for i in _recurse(k1.upper(), v1, prefix=prefix + k + "_"):
+          yield i
+    elif isinstance(v, list):
+      for k1, v1 in enumerate(v):
+        for i in _recurse(str(k1).upper(), v1, prefix=prefix + k + "_"):
+          yield i
+  for k, v in dict_.items():
+    for i in _recurse(k.upper(), v, prefix):
+      yield i

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/fad1b716/src/test/python/apache/aurora/common/BUILD
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/common/BUILD b/src/test/python/apache/aurora/common/BUILD
index 2d9ba67..7e0f553 100644
--- a/src/test/python/apache/aurora/common/BUILD
+++ b/src/test/python/apache/aurora/common/BUILD
@@ -22,6 +22,7 @@ python_test_suite(
     pants(':test_clusters'),
     pants(':test_cluster_option'),
     pants(':test_http_signaler'),
+    pants(':test_shellify'),
   ]
 )
 
@@ -69,3 +70,11 @@ python_tests(
     pants('src/main/python/apache/aurora/common:http_signaler'),
   ]
 )
+
+python_tests(
+  name = 'test_shellify',
+  sources = ['test_shellify.py'],
+  dependencies = [
+    pants('src/main/python/apache/aurora/common:shellify'),
+  ]
+)

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/fad1b716/src/test/python/apache/aurora/common/test_shellify.py
----------------------------------------------------------------------
diff --git a/src/test/python/apache/aurora/common/test_shellify.py b/src/test/python/apache/aurora/common/test_shellify.py
new file mode 100644
index 0000000..8ecce7f
--- /dev/null
+++ b/src/test/python/apache/aurora/common/test_shellify.py
@@ -0,0 +1,33 @@
+from apache.aurora.common.shellify import shellify
+
+
+def test_shellify():
+  dump = list(shellify(
+    {
+      "num": 123,
+      "string": "abc",
+      "obj": {
+        "num": 456,
+        "string": "def",
+        "description": "Runs the world",
+      },
+      "arr": [
+        7,
+        "g",
+        {
+          "hi": [0],
+        },
+      ]
+    }
+  , prefix="TEST_"))
+
+  assert set(dump) == set([
+    "TEST_NUM=123",
+    "TEST_STRING=abc",
+    "TEST_OBJ_NUM=456",
+    "TEST_OBJ_STRING=def",
+    "TEST_OBJ_DESCRIPTION='Runs the world'",
+    "TEST_ARR_0=7",
+    "TEST_ARR_1=g",
+    "TEST_ARR_2_HI_0=0",
+  ])