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

Delta Between Two Patch Sets: run.py

Issue 4810150141493248: Issue 122 - Puppet ENC via Hiera (Closed)
Left Patch Set: 112 - Integrate run.py and monitoring with Hiera Created Feb. 26, 2015, 5:32 p.m.
Right Patch Set: Puppet ENC via Hiera - Without Arrow Alignment Created March 16, 2015, 2:04 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 | « modules/statsmaster/manifests/init.pp ('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 sys 4 import sys
5 import os 5 import os
6 import os.path
7 import re 6 import re
8 import subprocess 7 import subprocess
9 import getopt 8 import getopt
10 import yaml 9 import yaml
11 10
12 def usage(): 11 def usage():
13 print >>sys.stderr, ''' 12 print >>sys.stderr, '''
14 Usage: %s [-u <user>] [-h <host>|<group>] [-i] ... <command> 13 Usage: %s [-u <user>] [-h <host>|<group>] [-i] ... <command>
15 14
16 Runs a command on the given hosts or groups of hosts. 15 Runs a command on the given hosts or groups of hosts.
(...skipping 29 matching lines...) Expand all
46 def getValidHosts(): 45 def getValidHosts():
47 dirname = os.path.dirname(sys.argv[0]) 46 dirname = os.path.dirname(sys.argv[0])
48 path_name = os.path.join(dirname, "hiera", "private", "hosts.yaml") 47 path_name = os.path.join(dirname, "hiera", "private", "hosts.yaml")
49 with open(path_name, 'rb') as handle: 48 with open(path_name, 'rb') as handle:
50 config = yaml.load(handle) 49 config = yaml.load(handle)
51 servers = config.get('servers', {}) 50 servers = config.get('servers', {})
52 return servers 51 return servers
53 52
54 def resolveHostList(hosts): 53 def resolveHostList(hosts):
55 54
56 host_names = set(str(item) for item in hosts)
57 result = set() 55 result = set()
58 56
59 try: 57 try:
60 valid_hosts = getValidHosts() 58 valid_hosts = getValidHosts()
61 except Warning as error: 59 except Warning as error:
62 print >>sys.stderr, 'Warning: failed to determine valid hosts:', error 60 print >>sys.stderr, 'Warning: failed to determine valid hosts:', error
63 result.update(host_names) 61 result.update(hosts)
64 else: 62 else:
65 for name in host_names: 63 for name in hosts:
66 chunk = tuple( 64 chunk = [
67 item[1].get('dns', item[0]) for item in valid_hosts.items() 65 value.get('dns', key) for (key, value) in valid_hosts.items()
68 66
69 if name == item[0] 67 if name == key
70 or name == '*' 68 or name == '*'
71 or name == item[1].get('dns', None) 69 or name == value.get('dns', None)
72 or name in item[1]['ip'] 70 or name in value.get('groups', ())
73 or name in item[1].get('groups', ()) 71 ]
74 )
75 72
76 if len(chunk) == 0: 73 if len(chunk) == 0:
77 print >>sys.stderr, 'Warning: failed to recognize host or group', name 74 print >>sys.stderr, 'Warning: failed to recognize host or group', name
78 else: 75 else:
79 result.update(chunk) 76 result.update(chunk)
80 77
81 return result 78 return result
82 79
83 def runCommand(user, host, command, ignore_errors=False): 80 def runCommand(user, host, command, ignore_errors=False):
84 if not isinstance(command, list): 81 if not isinstance(command, list):
85 command = [command] 82 command = [command]
86 command = ["ssh"] + (["-l", user] if user else []) + [host] + command 83 command = ["ssh"] + (["-l", user] if user else []) + [host] + command
87 if ignore_errors: 84 if ignore_errors:
88 subprocess.call(command) 85 subprocess.call(command)
89 else: 86 else:
90 subprocess.check_call(command) 87 subprocess.check_call(command)
91 88
92 if __name__ == "__main__": 89 if __name__ == "__main__":
93 user, hosts, ignore_errors, args = parseOptions(sys.argv[1:]) 90 user, hosts, ignore_errors, args = parseOptions(sys.argv[1:])
94 selectedHosts = resolveHostList(hosts) 91 selectedHosts = resolveHostList(hosts)
95 if len(selectedHosts) == 0: 92 if len(selectedHosts) == 0:
96 print >>sys.stderr, 'No valid hosts or groups specified, nothing to do' 93 print >>sys.stderr, 'No valid hosts or groups specified, nothing to do'
97 sys.exit(0) 94 sys.exit(0)
98 for host in selectedHosts: 95 for host in selectedHosts:
99 print >>sys.stderr, 'Running on %s...' % host 96 print >>sys.stderr, 'Running on %s...' % host
100 runCommand(user, host, args, ignore_errors=ignore_errors) 97 runCommand(user, host, args, ignore_errors=ignore_errors)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld