Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: run.py

Issue 6572117575335936: Issue 2200 - PART II/II - Introduce --local and --remote parameters in {run,kick}.py (Closed)
Left Patch Set: Created April 8, 2015, 12:31 p.m.
Right Patch Set: Issue 2200 - Introduce --local and --remote parameters in {run,kick}.py Created April 8, 2015, 3:29 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « kick.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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,
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,
25 help='use a remote (puppet-master) version of hosts.yaml' 26 help='use a remote (puppet-master) version of hosts.yaml'
26 ) 27 )
27 28
28 return parser 29 return parser
29 30
30 def parseOptions(args): 31 def parseOptions(args):
31 description = 'Run a command on the given hosts or groups of hosts' 32 description = 'Run a command on the given hosts or groups of hosts'
32 parser = createArgumentParser(description=description) 33 parser = createArgumentParser(description=description)
33 parser.add_argument( 34 parser.add_argument(
34 '-i', '--ignore-errors', action='store_true', dest='ignore_errors', 35 '-i', '--ignore-errors', action='store_true', dest='ignore_errors',
(...skipping 14 matching lines...) Expand all
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)
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:
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)
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
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)
LEFTRIGHT
« kick.py ('k') | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld