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
=== modified file 'bin/jitsu.in'
--- bin/jitsu.in 2012-07-05 17:30:37 +0000
+++ bin/jitsu.in 2012-07-17 01:14:22 +0000
@@ -52,23 +52,24 @@
52for cmd, details in sorted(cmds.iteritems()):52for cmd, details in sorted(cmds.iteritems()):
53 add_command_help(subparser, subcommand_home, cmd, details)53 add_command_help(subparser, subcommand_home, cmd, details)
5454
55(args, cmd_args) = parser.parse_known_args()55args, cmd_args = parser.parse_known_args()
56
57run_cmd = args.subcommand56run_cmd = args.subcommand
5857
59if args.subcommand == 'help':58if args.subcommand == 'help':
60 run_cmd = args.help_subcommand59 run_cmd = args.help_subcommand
61 cmd_args = ['--help']60 cmd_args = ['--help']
61else:
62 cmd_args = sys.argv[2:]
6263
63def is_executable(path):64def is_executable(path):
64 return isfile(path) and os.access(path, os.X_OK)65 return isfile(path) and os.access(path, os.X_OK)
6566
66user_path = expanduser(join(user_subcommands, run_cmd))67user_path = expanduser(join(user_subcommands, run_cmd))
67if is_executable( user_path ):68if is_executable(user_path):
68 os.execv(user_path, [user_path]+cmd_args)69 os.execv(user_path, [user_path] + cmd_args)
6970
70subcommand_path = join(subcommand_home, run_cmd)71subcommand_path = join(subcommand_home, run_cmd)
71if is_executable( subcommand_path ):72if is_executable(subcommand_path):
72 os.execv(subcommand_path, [subcommand_path]+cmd_args)73 os.execv(subcommand_path, [subcommand_path] + cmd_args)
7374
74os.execvp('juju', ['juju',run_cmd]+cmd_args)75os.execvp('juju', ['juju', run_cmd] + cmd_args)
7576
=== added file 'sub-commands/provider-info'
--- sub-commands/provider-info 1970-01-01 00:00:00 +0000
+++ sub-commands/provider-info 2012-07-17 01:14:22 +0000
@@ -0,0 +1,87 @@
1#!/usr/bin/env python
2
3import logging
4import sys
5import traceback
6
7from twisted.internet import reactor
8from twisted.internet.defer import inlineCallbacks
9
10from aiki.cli import make_arg_parser, setup_logging
11from juju.environment.config import EnvironmentsConfig
12from juju.errors import EnvironmentNotFound
13from juju.lib.format import YAMLFormat
14
15
16log = logging.getLogger("provider-info")
17
18
19def main():
20 parser = make_parser()
21 options = parser.parse_args()
22 setup_logging(options)
23 run_command(get_info, options)
24
25
26def make_parser(root_parser=None):
27 main_parser = make_arg_parser(
28 root_parser, "provider-info",
29 description="Gets the available provider info for specified environment")
30 return main_parser
31
32
33def get_provider(options):
34 env_config = EnvironmentsConfig()
35 env_config.load()
36 if options.environment is None:
37 environment = env_config.get_default()
38 else:
39 environment = env_config.get(options.environment)
40 if environment is None:
41 raise EnvironmentNotFound(
42 "Environment %r not configured in environments.yaml" % options.environment)
43 return environment.get_machine_provider()
44
45
46def run_command(command, options):
47 try:
48 provider = get_provider(options)
49 except Exception, e:
50 print >> sys.stderr, e
51 sys.exit(1)
52
53 exit_code = [0]
54 reactor.callWhenRunning(run_command_in_reactor, command, exit_code, provider, options)
55 reactor.run()
56 sys.exit(exit_code[0])
57
58
59@inlineCallbacks
60def run_command_in_reactor(command, exit_code, provider, options):
61 try:
62 yield command(exit_code, provider, options)
63 except Exception, e:
64 exit_code[0] = 1
65 if options.verbose:
66 traceback.print_exc() # Writes to stderr
67 else:
68 print >> sys.stderr, e
69 finally:
70 reactor.stop()
71
72
73@inlineCallbacks
74def get_info(result, provider, options):
75 info = {
76 "machines": [],
77 "provider-state": (yield provider.load_state())}
78 machines = yield provider.get_machines()
79 for machine in machines:
80 info["machines"].append(dict(
81 dns_name=machine.dns_name, private_dns_name=machine.private_dns_name,
82 instance_id=machine.instance_id, state=machine.state))
83 print YAMLFormat().format(info)
84
85
86if __name__ == '__main__':
87 main()

Subscribers

People subscribed via source and target branches

to all changes: