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

Side by Side Diff: kick.py

Issue 6569732794744832: Issue 2200 - PART I/II - Migrate to argparse (Closed)
Patch Set: Issue 2200 - PART I/II - Migrate to argparse Created April 7, 2015, 3:36 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | run.py » ('j') | run.py » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 getopt 5 import getopt
6 from run import resolveHostList, runCommand 6 from run import resolveHostList, runCommand, createArgumentParser
7
8 def usage():
9 print >>sys.stderr, '''
10 Usage: %s [-u <user>] [-t|-q] [<host>|<group>] ...
11
12 Runs provisioning on the given hosts or groups of hosts.
13
14 Options:
15 -u <user> User name to use with the SSH command (needs access to puppet
16 master and all hosts)
17 -t Dry-run mode, will produce the usual output but not change
18 host configuration
19 -q Quiet mode, suppress Puppet output to console
20 ''' % sys.argv[0]
21 7
22 def parseOptions(args): 8 def parseOptions(args):
23 try: 9 description = 'Run provisioning on the given hosts or groups of hosts'
24 options, args = getopt.getopt(args, 'u:vt') 10 parser = createArgumentParser(description=description)
25 except getopt.GetoptError, e: 11 parser.add_argument(
26 print >>sys.stderr, e 12 '-t', '--test', action='store_true', dest='dry_run',
27 usage() 13 help='dry-run mode, will not apply any host setup changes'
14 )
15
16 parser.add_argument(
17 '-q', '--quiet', action='store_true', dest='quiet',
18 help='quiet mode, suppresses Puppet output to console'
19 )
20
21 parser.add_argument(
22 'hosts', metavar='host|group', nargs='+',
23 help='target host or group, can be specified multiple times',
24 )
25
26 options = parser.parse_args(args)
27
28 if options.quiet and options.dry_run:
29 print >>sys.stderr, 'Only one mode flag can be specified, either -t or -q'
28 sys.exit(1) 30 sys.exit(1)
31 elif options.quiet:
32 options.mode = ''
33 elif options.dry_run:
34 options.mode = ' --test --noop'
35 else:
36 options.mode = ' --test'
29 37
30 if set(('-t', '-q')).issubset(options): 38 return options
31 print >>sys.stderr, 'Only one mode flag can be specified, either -t or -q'
32 usage()
33 sys.exit(1)
34
35 user = None
36 mode = ' --test'
37 for option, value in options:
38 if option == '-u':
39 user = value
40 elif option == '-q':
41 mode = ''
42 elif option == '-t':
43 mode = ' --test --noop'
44
45 return user, mode, args
46 39
47 def updateMaster(user): 40 def updateMaster(user):
48 print 'Updating data on the puppet master...' 41 print 'Updating data on the puppet master...'
49 remoteCommand = ' && '.join([ 42 remoteCommand = ' && '.join([
50 'sudo hg pull -qu -R /etc/puppet/infrastructure', 43 'sudo hg pull -qu -R /etc/puppet/infrastructure',
51 'sudo hg pull -qu -R /etc/puppet/infrastructure/modules/private', 44 'sudo hg pull -qu -R /etc/puppet/infrastructure/modules/private',
52 'sudo /etc/puppet/infrastructure/ensure_dependencies.py /etc/puppet/infrastr ucture', 45 'sudo /etc/puppet/infrastructure/ensure_dependencies.py /etc/puppet/infrastr ucture',
53 ]) 46 ])
54 runCommand(user, "puppetmaster.adblockplus.org", remoteCommand) 47 runCommand(user, 'puppetmaster.adblockplus.org', remoteCommand)
55 48
56 def updateClient(user, host, mode): 49 def updateClient(user, host, mode):
57 print 'Provisioning %s...' % host 50 print 'Provisioning %s...' % host
58 remoteCommand = 'sudo puppet agent%s' % mode 51 remoteCommand = 'sudo puppet agent%s' % mode
59 52
60 # Have to ignore errors here, Puppet will return non-zero for successful runs 53 # Have to ignore errors here, Puppet will return non-zero for successful runs
61 runCommand(user, host, remoteCommand, ignore_errors=True) 54 runCommand(user, host, remoteCommand, ignore_errors=True)
62 55
63 if __name__ == "__main__": 56 if __name__ == "__main__":
Wladimir Palant 2015/04/07 15:50:25 Nit: We should use single quotes here as well if y
mathias 2015/04/07 15:56:22 Done.
64 user, mode, args = parseOptions(sys.argv[1:]) 57 options = parseOptions(sys.argv[1:])
65 needKicking = resolveHostList(args) 58 needKicking = resolveHostList(options.hosts)
66 if len(needKicking) == 0: 59 if len(needKicking) == 0:
67 print >>sys.stderr, 'No valid hosts or groups specified, nothing to do' 60 print >>sys.stderr, 'No valid hosts or groups specified, nothing to do'
68 sys.exit(0) 61 sys.exit(0)
69 updateMaster(user) 62 updateMaster(options.user)
70 for host in needKicking: 63 for host in needKicking:
71 updateClient(user, host, mode) 64 updateClient(options.user, host, options.mode)
OLDNEW
« no previous file with comments | « no previous file | run.py » ('j') | run.py » ('J')

Powered by Google App Engine
This is Rietveld