| OLD | NEW |
| 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 posixpath |
| 8 import re | 8 import re |
| 9 import subprocess | 9 import subprocess |
| 10 import yaml | 10 import yaml |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 child.stdout.close() | 69 child.stdout.close() |
| 70 child.wait() | 70 child.wait() |
| 71 elif options.remote is False: | 71 elif options.remote is False: |
| 72 dirname = os.path.dirname(sys.argv[0]) | 72 dirname = os.path.dirname(sys.argv[0]) |
| 73 path_name = os.path.join(dirname, *path_canonical) | 73 path_name = os.path.join(dirname, *path_canonical) |
| 74 with open(path_name, 'rb') as handle: | 74 with open(path_name, 'rb') as handle: |
| 75 config = yaml.load(handle) | 75 config = yaml.load(handle) |
| 76 else: | 76 else: |
| 77 sys.exit('Please either specify a --remote host or use --local') | 77 sys.exit('Please either specify a --remote host or use --local') |
| 78 | 78 |
| 79 servers = config.get('servers', {}) | 79 servers = config.get('adblockplus::hosts', {}) |
| 80 return servers | 80 return servers |
| 81 | 81 |
| 82 | 82 |
| 83 def resolveHostList(options): | 83 def resolveHostList(options): |
| 84 | 84 |
| 85 result = set() | 85 result = set() |
| 86 | 86 |
| 87 try: | 87 try: |
| 88 valid_hosts = getValidHosts(options) | 88 valid_hosts = getValidHosts(options) |
| 89 except Warning as error: | 89 except Warning as error: |
| 90 print >>sys.stderr, 'Warning: failed to determine valid hosts:', error | 90 print >>sys.stderr, 'Warning: failed to determine valid hosts:', error |
| 91 result.update(options.hosts) | 91 result.update(options.hosts) |
| 92 else: | 92 else: |
| 93 for name in options.hosts: | 93 for name in options.hosts: |
| 94 chunk = [ | 94 chunk = [ |
| 95 value.get('dns', key) for (key, value) in valid_hosts.items() | 95 value.get('fqdn', key) for (key, value) in valid_hosts.items() |
| 96 | 96 |
| 97 if name == key | 97 if name == key |
| 98 or name == '*' | 98 or name == '*' |
| 99 or name == value.get('dns', None) | 99 or name == value.get('fqdn', None) |
| 100 or name in value.get('groups', ()) | 100 or name in value.get('groups', ()) |
| 101 ] | 101 ] |
| 102 | 102 |
| 103 if len(chunk) == 0: | 103 if len(chunk) == 0: |
| 104 print >>sys.stderr, 'Warning: failed to recognize host or group'
, name | 104 print >>sys.stderr, 'Warning: failed to recognize host or group'
, name |
| 105 else: | 105 else: |
| 106 result.update(chunk) | 106 result.update(chunk) |
| 107 | 107 |
| 108 return result | 108 return result |
| 109 | 109 |
| 110 | 110 |
| 111 def runCommand(user, host, command, ignore_errors=False): | 111 def runCommand(user, host, command, ignore_errors=False): |
| 112 if not isinstance(command, list): | 112 if not isinstance(command, list): |
| 113 command = [command] | 113 command = [command] |
| 114 command = ['ssh'] + (['-l', user] if user else []) + [host] + command | 114 command = ['ssh'] + (['-l', user] if user else []) + [host] + command |
| 115 if ignore_errors: | 115 if ignore_errors: |
| 116 subprocess.call(command) | 116 subprocess.call(command) |
| 117 else: | 117 else: |
| 118 subprocess.check_call(command) | 118 subprocess.check_call(command) |
| 119 | 119 |
| 120 if __name__ == '__main__': | 120 if __name__ == '__main__': |
| 121 options = parseOptions(sys.argv[1:]) | 121 options = parseOptions(sys.argv[1:]) |
| 122 selectedHosts = resolveHostList(options) | 122 selectedHosts = resolveHostList(options) |
| 123 if len(selectedHosts) == 0: | 123 if len(selectedHosts) == 0: |
| 124 print >>sys.stderr, 'No valid hosts or groups specified, nothing to do' | 124 print >>sys.stderr, 'No valid hosts or groups specified, nothing to do' |
| 125 sys.exit(0) | 125 sys.exit(0) |
| 126 for host in selectedHosts: | 126 for host in selectedHosts: |
| 127 print >>sys.stderr, 'Running on %s...' % host | 127 print >>sys.stderr, 'Running on %s...' % host |
| 128 runCommand(options.user, host, options.args, options.ignore_errors) | 128 runCommand(options.user, host, options.args, options.ignore_errors) |
| OLD | NEW |