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

Unified Diff: run.py

Issue 6572117575335936: Issue 2200 - PART II/II - Introduce --local and --remote parameters in {run,kick}.py (Closed)
Patch Set: Issue 2200 - Introduce --local and --remote parameters in {run,kick}.py Created April 8, 2015, 3:29 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « kick.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: run.py
===================================================================
--- a/run.py
+++ b/run.py
@@ -4,6 +4,7 @@
import argparse
import sys
import os
+import posixpath
import re
import subprocess
import yaml
@@ -11,10 +12,20 @@
def createArgumentParser(**kwargs):
parser = argparse.ArgumentParser(**kwargs)
parser.add_argument(
- '-u', '--user', metavar='user', dest='user', type=str, default='vagrant',
+ '-u', '--user', metavar='user', dest='user', type=str,
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 +52,41 @@
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 = posixpath.join('/etc/puppet/infrastructure', *path_canonical)
+ command = ['ssh'] + login + [options.remote, '--', 'sudo', 'cat', path_name]
+ child = subprocess.Popen(command, stderr=sys.stderr, stdout=subprocess.PIPE)
+ try:
+ config = yaml.load(child.stdout)
+ finally:
+ child.stdout.close()
+ child.wait()
+ 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:
+ sys.exit('Please either specify a --remote host or use --local')
+
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 +114,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)
« no previous file with comments | « kick.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld