Merge lp:~jimbaker/juju-jitsu/provider-info into lp:juju-jitsu

Proposed by Jim Baker
Status: Merged
Approved by: Mark Mims
Approved revision: 73
Merge reported by: Mark Mims
Merged at revision: not available
Proposed branch: lp:~jimbaker/juju-jitsu/provider-info
Merge into: lp:juju-jitsu
Diff against target: 127 lines (+95/-7)
2 files modified
bin/jitsu.in (+8/-7)
sub-commands/provider-info (+87/-0)
To merge this branch: bzr merge lp:~jimbaker/juju-jitsu/provider-info
Reviewer Review Type Date Requested Status
Mark Mims (community) Approve
Review via email: mp+115261@code.launchpad.net

Description of the change

Adds provider-info subcommand, which collects together all available provider info (machine list, provider state), then dumps YAML. Sample output:

$ jitsu provider-info
machines:
- dns_name: localhost
    instance_id: local
    private_dns_name: localhost
    state: running
provider-state:
    zookeeper-address: 192.168.122.1:45239
    zookeeper-instances:
    - local

This also fixes a bug in how jitsu dispatches to subcommands; it should not use the result of parse_known_args for the dispatch, but instead do this directly from the original args, so that's sys.argv[2:]

To post a comment you must log in.
Revision history for this message
Mark Mims (mark-mims) wrote :

lgtm

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/jitsu.in'
2--- bin/jitsu.in 2012-07-05 17:30:37 +0000
3+++ bin/jitsu.in 2012-07-17 01:14:22 +0000
4@@ -52,23 +52,24 @@
5 for cmd, details in sorted(cmds.iteritems()):
6 add_command_help(subparser, subcommand_home, cmd, details)
7
8-(args, cmd_args) = parser.parse_known_args()
9-
10+args, cmd_args = parser.parse_known_args()
11 run_cmd = args.subcommand
12
13 if args.subcommand == 'help':
14 run_cmd = args.help_subcommand
15 cmd_args = ['--help']
16+else:
17+ cmd_args = sys.argv[2:]
18
19 def is_executable(path):
20 return isfile(path) and os.access(path, os.X_OK)
21
22 user_path = expanduser(join(user_subcommands, run_cmd))
23-if is_executable( user_path ):
24- os.execv(user_path, [user_path]+cmd_args)
25+if is_executable(user_path):
26+ os.execv(user_path, [user_path] + cmd_args)
27
28 subcommand_path = join(subcommand_home, run_cmd)
29-if is_executable( subcommand_path ):
30- os.execv(subcommand_path, [subcommand_path]+cmd_args)
31+if is_executable(subcommand_path):
32+ os.execv(subcommand_path, [subcommand_path] + cmd_args)
33
34-os.execvp('juju', ['juju',run_cmd]+cmd_args)
35+os.execvp('juju', ['juju', run_cmd] + cmd_args)
36
37=== added file 'sub-commands/provider-info'
38--- sub-commands/provider-info 1970-01-01 00:00:00 +0000
39+++ sub-commands/provider-info 2012-07-17 01:14:22 +0000
40@@ -0,0 +1,87 @@
41+#!/usr/bin/env python
42+
43+import logging
44+import sys
45+import traceback
46+
47+from twisted.internet import reactor
48+from twisted.internet.defer import inlineCallbacks
49+
50+from aiki.cli import make_arg_parser, setup_logging
51+from juju.environment.config import EnvironmentsConfig
52+from juju.errors import EnvironmentNotFound
53+from juju.lib.format import YAMLFormat
54+
55+
56+log = logging.getLogger("provider-info")
57+
58+
59+def main():
60+ parser = make_parser()
61+ options = parser.parse_args()
62+ setup_logging(options)
63+ run_command(get_info, options)
64+
65+
66+def make_parser(root_parser=None):
67+ main_parser = make_arg_parser(
68+ root_parser, "provider-info",
69+ description="Gets the available provider info for specified environment")
70+ return main_parser
71+
72+
73+def get_provider(options):
74+ env_config = EnvironmentsConfig()
75+ env_config.load()
76+ if options.environment is None:
77+ environment = env_config.get_default()
78+ else:
79+ environment = env_config.get(options.environment)
80+ if environment is None:
81+ raise EnvironmentNotFound(
82+ "Environment %r not configured in environments.yaml" % options.environment)
83+ return environment.get_machine_provider()
84+
85+
86+def run_command(command, options):
87+ try:
88+ provider = get_provider(options)
89+ except Exception, e:
90+ print >> sys.stderr, e
91+ sys.exit(1)
92+
93+ exit_code = [0]
94+ reactor.callWhenRunning(run_command_in_reactor, command, exit_code, provider, options)
95+ reactor.run()
96+ sys.exit(exit_code[0])
97+
98+
99+@inlineCallbacks
100+def run_command_in_reactor(command, exit_code, provider, options):
101+ try:
102+ yield command(exit_code, provider, options)
103+ except Exception, e:
104+ exit_code[0] = 1
105+ if options.verbose:
106+ traceback.print_exc() # Writes to stderr
107+ else:
108+ print >> sys.stderr, e
109+ finally:
110+ reactor.stop()
111+
112+
113+@inlineCallbacks
114+def get_info(result, provider, options):
115+ info = {
116+ "machines": [],
117+ "provider-state": (yield provider.load_state())}
118+ machines = yield provider.get_machines()
119+ for machine in machines:
120+ info["machines"].append(dict(
121+ dns_name=machine.dns_name, private_dns_name=machine.private_dns_name,
122+ instance_id=machine.instance_id, state=machine.state))
123+ print YAMLFormat().format(info)
124+
125+
126+if __name__ == '__main__':
127+ main()

Subscribers

People subscribed via source and target branches

to all changes: