You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2013/11/08 02:21:35 UTC
[2/4] git commit: Created 'usage' and moved 'get' helper to cli.py.
Created 'usage' and moved 'get' helper to cli.py.
Review: https://reviews.apache.org/r/15300
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/39017657
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/39017657
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/39017657
Branch: refs/heads/master
Commit: 39017657fc5f938f581dbffa636bb07c91ba1f11
Parents: a63ca0f
Author: Benjamin Hindman <be...@gmail.com>
Authored: Wed Nov 6 20:11:50 2013 -1000
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Thu Nov 7 15:18:08 2013 -1000
----------------------------------------------------------------------
src/Makefile.am | 3 ++-
src/cli/mesos-ps | 37 ++++++++++++++-----------------------
src/cli/python/mesos/cli.py | 38 +++++++++++++++++++++++---------------
src/cli/python/mesos/http.py | 27 +++++++++++++++++++++++++++
4 files changed, 66 insertions(+), 39 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/39017657/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index d9e7bcd..52244dd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -388,7 +388,8 @@ mesospythonpkglibexecdir = $(pkglibexecdir)/python/mesos
dist_mesospythonpkglibexec_SCRIPTS = \
cli/python/mesos/__init__.py \
cli/python/mesos/cli.py \
- cli/python/mesos/futures.py
+ cli/python/mesos/futures.py \
+ cli/python/mesos/http.py
# Need to distribute/install webui javascript. We use 'pkgdatadir'
# instead of 'datadir' as the install directory so we get the the
http://git-wip-us.apache.org/repos/asf/mesos/blob/39017657/src/cli/mesos-ps
----------------------------------------------------------------------
diff --git a/src/cli/mesos-ps b/src/cli/mesos-ps
index 0d1694e..f32c812 100755
--- a/src/cli/mesos-ps
+++ b/src/cli/mesos-ps
@@ -4,11 +4,12 @@ import datetime
import json
import signal
import sys
-import urllib2
from contextlib import closing
from optparse import OptionParser
+from urllib2 import urlopen
+from mesos import http
from mesos.cli import *
from mesos.futures import *
@@ -62,7 +63,7 @@ def mem(task, statistics):
# An executorless task has an empty executor ID in the master but
# uses the same executor ID as task ID in the slave.
- if executor_id == "": executor_id = task['id']
+ if executor_id == '': executor_id = task['id']
mem_rss_bytes = None
for entry in statistics:
@@ -73,7 +74,7 @@ def mem(task, statistics):
if mem_rss_bytes is not None:
MB = 1024.0 * 1024.0
- return "{0:.1f} MB".format(mem_rss_bytes / MB)
+ return '{0:.1f} MB'.format(mem_rss_bytes / MB)
return None
@@ -88,7 +89,7 @@ def time(task, statistics):
# An executorless task has an empty executor ID in the master but
# uses the same executor ID as task ID in the slave.
- if executor_id == "": executor_id = task['id']
+ if executor_id == '': executor_id = task['id']
cpus_time_secs = None
for entry in statistics:
@@ -115,9 +116,7 @@ def main():
(options, args) = parser.parse_args(sys.argv)
if options.master is None:
- sys.stderr.write('Missing --master\n')
- parser.print_help()
- sys.exit(-1)
+ usage('Missing --master', parser)
try:
timeout = float(options.timeout)
@@ -126,13 +125,11 @@ def main():
sys.exit(-1)
# Get the master's state.
- url = 'http://' + resolve(options.master) + '/master/state.json'
- with closing(urllib2.urlopen(url)) as file:
- try:
- state = json.loads(file.read())
- except:
- sys.stderr.write('Failed to read the master state\n')
- sys.exit(1)
+ try:
+ state = http.get(resolve(options.master), '/master/state.json')
+ except:
+ sys.stderr.write('Failed to get the master state\n')
+ sys.exit(1)
# Collect all the active frameworks and tasks by slave ID.
active = {}
@@ -157,19 +154,13 @@ def main():
sys.stdout.write(columns[i].title)
sys.stdout.write(' ' * columns[i].padding)
- # Helper to get the statistics from the slave.
- def get(slave):
- pid = slave['pid']
- url = 'http://' + pid[len('slave(1)@'):] + '/monitor/statistics.json'
- with closing(urllib2.urlopen(url)) as file:
- return json.loads(file.read())
-
with ThreadingExecutor() as executor:
# Grab all the slaves with active tasks.
slaves = [slave for slave in state['slaves'] if slave['id'] in active]
# Now submit calls to get the statistics for each slave.
- futures = dict((executor.submit(get, slave), slave)
+ path = '/monitor/statistics.json'
+ futures = dict((executor.submit(http.get, slave['pid'], path), slave)
for slave in slaves)
# And wait for each future to complete!
@@ -198,7 +189,7 @@ def main():
sys.exit(0)
-if __name__ == "__main__":
+if __name__ == '__main__':
def handler(signal, frame):
sys.stdout.write('\n')
sys.exit(130)
http://git-wip-us.apache.org/repos/asf/mesos/blob/39017657/src/cli/python/mesos/cli.py
----------------------------------------------------------------------
diff --git a/src/cli/python/mesos/cli.py b/src/cli/python/mesos/cli.py
index 7712886..5c11d46 100644
--- a/src/cli/python/mesos/cli.py
+++ b/src/cli/python/mesos/cli.py
@@ -1,4 +1,10 @@
-import subprocess
+# Helper for printing out a message and then the "usage" then exiting.
+def usage(message, parser):
+ import sys
+ sys.stderr.write(message + '\n')
+ parser.print_help()
+ sys.exit(-1)
+
# Helper that uses 'mesos-resolve' to resolve a master IP:port from
# one of:
@@ -6,19 +12,21 @@ import subprocess
# zk://username:password@host1:port1,host2:port2,.../path
# file://path/to/file (where file contains one of the above)
def resolve(master):
- process = subprocess.Popen(
- ['mesos-resolve', master],
- stdin=None,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- shell=False)
+ import subprocess
+
+ process = subprocess.Popen(
+ ['mesos-resolve', master],
+ stdin=None,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ shell=False)
- status = process.wait()
- if status != 0:
- raise Exception('Failed to execute \'mesos-resolve %s\':\n%s'
- % (master, process.stderr.read()))
+ status = process.wait()
+ if status != 0:
+ raise Exception('Failed to execute \'mesos-resolve %s\':\n%s'
+ % (master, process.stderr.read()))
- result = process.stdout.read()
- process.stdout.close()
- process.stderr.close()
- return result
+ result = process.stdout.read()
+ process.stdout.close()
+ process.stderr.close()
+ return result
http://git-wip-us.apache.org/repos/asf/mesos/blob/39017657/src/cli/python/mesos/http.py
----------------------------------------------------------------------
diff --git a/src/cli/python/mesos/http.py b/src/cli/python/mesos/http.py
new file mode 100644
index 0000000..e65701b
--- /dev/null
+++ b/src/cli/python/mesos/http.py
@@ -0,0 +1,27 @@
+# Helper for doing an HTTP GET given a PID, a path, and a query dict.
+# For example:
+#
+# get('foo@1.2.3.4:123',
+# '/endpoint',
+# {'first': 'ben',
+# 'last': 'hindman'})
+#
+# Would yield: 1.2.3.4:123/endpoint?first='ben'&last='hindman'
+#
+# Note that you can also pass an IP:port (or hostname:port) for 'pid'
+# (i.e., you can omit the ID component of the PID, e.g., 'foo@').
+def get(pid, path, query=None):
+ import json
+ import urllib2
+
+ from contextlib import closing
+
+ url = 'http://' + pid[(pid.find('@') + 1):] + path
+
+ if query is not None and len(query) > 0:
+ url += '?' + '&'.join(
+ ['%s=%s' % (urllib2.quote(str(key)), urllib2.quote(str(value)))
+ for (key, value) in query.items()])
+
+ with closing(urllib2.urlopen(url)) as file:
+ return json.loads(file.read())