Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # coding: utf-8 | 2 # coding: utf-8 |
3 | 3 |
4 import argparse | 4 import argparse |
5 import sys | 5 import sys |
6 import os | 6 import os |
7 import posixpath | |
7 import re | 8 import re |
8 import subprocess | 9 import subprocess |
9 import yaml | 10 import yaml |
10 | 11 |
11 def createArgumentParser(**kwargs): | 12 def createArgumentParser(**kwargs): |
12 parser = argparse.ArgumentParser(**kwargs) | 13 parser = argparse.ArgumentParser(**kwargs) |
13 parser.add_argument( | 14 parser.add_argument( |
14 '-u', '--user', metavar='user', dest='user', type=str, default='vagrant', | 15 '-u', '--user', metavar='user', dest='user', type=str, |
mathias
2015/04/08 15:29:58
Also removed this default, it's senseless until th
| |
15 help='user name for use with SSH, must exist on all hosts' | 16 help='user name for use with SSH, must exist on all hosts' |
16 ) | 17 ) |
17 | 18 |
18 parser.add_argument( | 19 parser.add_argument( |
19 '-l', '--local', action='store_false', dest='remote', default=None, | 20 '-l', '--local', action='store_false', dest='remote', default=None, |
20 help='use the local version of hosts.yaml' | 21 help='use the local version of hosts.yaml' |
21 ) | 22 ) |
22 | 23 |
23 parser.add_argument( | 24 parser.add_argument( |
24 '-r', '--remote', metavar='master', dest='remote', type=str, | 25 '-r', '--remote', metavar='master', dest='remote', type=str, |
(...skipping 24 matching lines...) Expand all Loading... | |
49 | 50 |
50 options = parser.parse_args(args) | 51 options = parser.parse_args(args) |
51 options.hosts = hosts | 52 options.hosts = hosts |
52 return options | 53 return options |
53 | 54 |
54 def getValidHosts(options): | 55 def getValidHosts(options): |
55 path_canonical = ('modules', 'private', 'hiera', 'hosts.yaml') | 56 path_canonical = ('modules', 'private', 'hiera', 'hosts.yaml') |
56 | 57 |
57 if options.remote: | 58 if options.remote: |
58 login = ['-l', options.user] if options.user else [] | 59 login = ['-l', options.user] if options.user else [] |
59 path_name = '/etc/puppet/infrastructure/%s' % ("/".join(path_canonical),) | 60 path_name = posixpath.join('/etc/puppet/infrastructure', *path_canonical) |
Wladimir Palant
2015/04/08 14:59:35
Please join paths properly:
import posixpath
path
mathias
2015/04/08 15:29:58
Done.
| |
60 command = ['ssh'] + login + [options.remote, '--', 'sudo', 'cat', path_name] | 61 command = ['ssh'] + login + [options.remote, '--', 'sudo', 'cat', path_name] |
61 child = subprocess.Popen(command, stderr=sys.stderr, stdout=subprocess.PIPE) | 62 child = subprocess.Popen(command, stderr=sys.stderr, stdout=subprocess.PIPE) |
62 config = yaml.load(child.stdout) | 63 try: |
63 child.stdout.close() | 64 config = yaml.load(child.stdout) |
64 child.wait() | 65 finally: |
Wladimir Palant
2015/04/08 14:59:35
Put yaml.load() into a try block and the two state
mathias
2015/04/08 15:29:58
Done.
| |
66 child.stdout.close() | |
67 child.wait() | |
65 elif options.remote is False: | 68 elif options.remote is False: |
66 dirname = os.path.dirname(sys.argv[0]) | 69 dirname = os.path.dirname(sys.argv[0]) |
67 path_name = os.path.join(dirname, *path_canonical) | 70 path_name = os.path.join(dirname, *path_canonical) |
68 with open(path_name, 'rb') as handle: | 71 with open(path_name, 'rb') as handle: |
69 config = yaml.load(handle) | 72 config = yaml.load(handle) |
70 else: | 73 else: |
71 print >>sys.stderr, 'Please either specify a --remote host or use --local' | 74 sys.exit('Please either specify a --remote host or use --local') |
72 sys.exit(1) | |
Wladimir Palant
2015/04/08 14:59:35
The two lines above are equivalent to:
sys.exit('
mathias
2015/04/08 15:29:58
Done.
| |
73 | 75 |
74 servers = config.get('servers', {}) | 76 servers = config.get('servers', {}) |
75 return servers | 77 return servers |
76 | 78 |
77 def resolveHostList(options): | 79 def resolveHostList(options): |
78 | 80 |
79 result = set() | 81 result = set() |
80 | 82 |
81 try: | 83 try: |
82 valid_hosts = getValidHosts(options) | 84 valid_hosts = getValidHosts(options) |
(...skipping 29 matching lines...) Expand all Loading... | |
112 | 114 |
113 if __name__ == '__main__': | 115 if __name__ == '__main__': |
114 options = parseOptions(sys.argv[1:]) | 116 options = parseOptions(sys.argv[1:]) |
115 selectedHosts = resolveHostList(options) | 117 selectedHosts = resolveHostList(options) |
116 if len(selectedHosts) == 0: | 118 if len(selectedHosts) == 0: |
117 print >>sys.stderr, 'No valid hosts or groups specified, nothing to do' | 119 print >>sys.stderr, 'No valid hosts or groups specified, nothing to do' |
118 sys.exit(0) | 120 sys.exit(0) |
119 for host in selectedHosts: | 121 for host in selectedHosts: |
120 print >>sys.stderr, 'Running on %s...' % host | 122 print >>sys.stderr, 'Running on %s...' % host |
121 runCommand(options.user, host, options.args, options.ignore_errors) | 123 runCommand(options.user, host, options.args, options.ignore_errors) |
LEFT | RIGHT |