Index: run.py |
=================================================================== |
--- a/run.py |
+++ b/run.py |
@@ -15,6 +15,16 @@ |
help='user name for use with SSH, must exist on all hosts' |
) |
+ parser.add_argument( |
+ '-l', '--local', action='store_false', dest='remote', default=None, |
+ help='use the local version of hosts.yaml' |
+ ) |
+ |
+ parser.add_argument( |
+ '-r', '--remote', metavar='master', dest='remote', type=str, |
+ help='use a remote (puppet-master) version of hosts.yaml' |
+ ) |
+ |
return parser |
def parseOptions(args): |
@@ -41,25 +51,40 @@ |
options.hosts = hosts |
return options |
-def getValidHosts(): |
- dirname = os.path.dirname(sys.argv[0]) |
- path_name = os.path.join(dirname, 'modules', 'private', 'hiera', 'hosts.yaml') |
- with open(path_name, 'rb') as handle: |
- config = yaml.load(handle) |
+def getValidHosts(options): |
+ path_canonical = ('modules', 'private', 'hiera', 'hosts.yaml') |
+ |
+ if options.remote: |
+ login = ['-l', options.user] if options.user else [] |
+ path_name = '/etc/puppet/infrastructure/%s' % ("/".join(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.
|
+ command = ['ssh'] + login + [options.remote, '--', 'sudo', 'cat', path_name] |
+ child = subprocess.Popen(command, stderr=sys.stderr, stdout=subprocess.PIPE) |
+ config = yaml.load(child.stdout) |
+ child.stdout.close() |
+ child.wait() |
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.
|
+ elif options.remote is False: |
+ dirname = os.path.dirname(sys.argv[0]) |
+ path_name = os.path.join(dirname, *path_canonical) |
+ with open(path_name, 'rb') as handle: |
+ config = yaml.load(handle) |
+ else: |
+ print >>sys.stderr, 'Please either specify a --remote host or use --local' |
+ 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.
|
+ |
servers = config.get('servers', {}) |
return servers |
-def resolveHostList(hosts): |
+def resolveHostList(options): |
result = set() |
try: |
- valid_hosts = getValidHosts() |
+ valid_hosts = getValidHosts(options) |
except Warning as error: |
print >>sys.stderr, 'Warning: failed to determine valid hosts:', error |
- result.update(hosts) |
+ result.update(options.hosts) |
else: |
- for name in hosts: |
+ for name in options.hosts: |
chunk = [ |
value.get('dns', key) for (key, value) in valid_hosts.items() |
@@ -87,7 +112,7 @@ |
if __name__ == '__main__': |
options = parseOptions(sys.argv[1:]) |
- selectedHosts = resolveHostList(options.hosts) |
+ selectedHosts = resolveHostList(options) |
if len(selectedHosts) == 0: |
print >>sys.stderr, 'No valid hosts or groups specified, nothing to do' |
sys.exit(0) |