| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 |
|
Wladimir Palant
2015/04/07 14:58:04
getopt isn't being used any more.
mathias
2015/04/07 15:36:51
Done.
| |
| 6 from run import resolveHostList, runCommand, createArgumentParser | 6 from run import resolveHostList, runCommand, createArgumentParser |
| 7 | 7 |
| 8 def parseOptions(args): | 8 def parseOptions(args): |
| 9 description = 'Run provisioning on the given hosts or groups of hosts' | 9 description = 'Run provisioning on the given hosts or groups of hosts' |
| 10 parser = createArgumentParser(description=description, add_help=False) | 10 parser = createArgumentParser(description=description) |
|
Wladimir Palant
2015/04/07 14:58:04
I don't think there is a point in reusing the pars
mathias
2015/04/07 15:36:51
While I understand your point (and in fact had the
| |
| 11 parser.add_argument( | 11 parser.add_argument( |
| 12 '-t', action='store_true', dest='dry_run', | 12 '-t', '--test', action='store_true', dest='dry_run', |
| 13 help='Dry-run mode, will not apply any host setup changes' | 13 help='dry-run mode, will not apply any host setup changes' |
| 14 ) | 14 ) |
| 15 | 15 |
| 16 parser.add_argument( | 16 parser.add_argument( |
| 17 '-q', action='store_true', dest='quiet', | 17 '-q', '--quiet', action='store_true', dest='quiet', |
| 18 help='Quiet mode, suppresses Puppet output to console' | 18 help='quiet mode, suppresses Puppet output to console' |
| 19 ) | 19 ) |
|
Wladimir Palant
2015/04/07 14:58:04
If you are switching to argparse, then you should
mathias
2015/04/07 15:36:51
Done.
| |
| 20 | 20 |
| 21 parser.add_argument( | 21 parser.add_argument( |
| 22 'hosts', metavar='<host|group>', nargs='+', | 22 'hosts', metavar='host|group', nargs='+', |
|
Wladimir Palant
2015/04/07 14:58:04
Nit (here and elsewhere): we should use the usual
mathias
2015/04/07 15:36:51
Done.
| |
| 23 help='target host or group, can be specified multiple times', | 23 help='target host or group, can be specified multiple times', |
| 24 ) | 24 ) |
| 25 | 25 |
| 26 options = parser.parse_args(args) | 26 options = parser.parse_args(args) |
| 27 | 27 |
| 28 if options.quiet and options.dry_run: | 28 if options.quiet and options.dry_run: |
| 29 print >>sys.stderr, 'Only one mode flag can be specified, either -t or -q' | 29 print >>sys.stderr, 'Only one mode flag can be specified, either -t or -q' |
| 30 sys.exit(1) | 30 sys.exit(1) |
| 31 elif options.quiet: | 31 elif options.quiet: |
| 32 options.mode = '' | 32 options.mode = '' |
| 33 elif options.dry_run: | 33 elif options.dry_run: |
| 34 options.mode = ' --test --noop' | 34 options.mode = ' --test --noop' |
| 35 else: | 35 else: |
| 36 options.mode = ' --test' | 36 options.mode = ' --test' |
| 37 | 37 |
| 38 return options | 38 return options |
| 39 | 39 |
| 40 def updateMaster(user): | 40 def updateMaster(user): |
| 41 print 'Updating data on the puppet master...' | 41 print 'Updating data on the puppet master...' |
| 42 remoteCommand = ' && '.join([ | 42 remoteCommand = ' && '.join([ |
| 43 'sudo hg pull -qu -R /etc/puppet/infrastructure', | 43 'sudo hg pull -qu -R /etc/puppet/infrastructure', |
| 44 'sudo hg pull -qu -R /etc/puppet/infrastructure/modules/private', | 44 'sudo hg pull -qu -R /etc/puppet/infrastructure/modules/private', |
| 45 'sudo /etc/puppet/infrastructure/ensure_dependencies.py /etc/puppet/infrastr ucture', | 45 'sudo /etc/puppet/infrastructure/ensure_dependencies.py /etc/puppet/infrastr ucture', |
| 46 ]) | 46 ]) |
| 47 runCommand(user, "puppetmaster.adblockplus.org", remoteCommand) | 47 runCommand(user, 'puppetmaster.adblockplus.org', remoteCommand) |
| 48 | 48 |
| 49 def updateClient(user, host, mode): | 49 def updateClient(user, host, mode): |
| 50 print 'Provisioning %s...' % host | 50 print 'Provisioning %s...' % host |
| 51 remoteCommand = 'sudo puppet agent%s' % mode | 51 remoteCommand = 'sudo puppet agent%s' % mode |
| 52 | 52 |
| 53 # 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 |
| 54 runCommand(user, host, remoteCommand, ignore_errors=True) | 54 runCommand(user, host, remoteCommand, ignore_errors=True) |
| 55 | 55 |
| 56 if __name__ == "__main__": | 56 if __name__ == '__main__': |
| 57 options = parseOptions(sys.argv[1:]) | 57 options = parseOptions(sys.argv[1:]) |
| 58 needKicking = resolveHostList(options.hosts) | 58 needKicking = resolveHostList(options.hosts) |
| 59 if len(needKicking) == 0: | 59 if len(needKicking) == 0: |
| 60 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' |
| 61 sys.exit(0) | 61 sys.exit(0) |
| 62 updateMaster(options.user) | 62 updateMaster(options.user) |
| 63 for host in needKicking: | 63 for host in needKicking: |
| 64 updateClient(options.user, host, options.mode) | 64 updateClient(options.user, host, options.mode) |
| LEFT | RIGHT |