OLD | NEW |
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, createArgumentParser | 6 from run import resolveHostList, runCommand, createArgumentParser |
7 | 7 |
| 8 def parseOptions(args): |
| 9 description = 'Run provisioning on the given hosts or groups of hosts' |
| 10 parser = createArgumentParser(description=description) |
| 11 parser.add_argument( |
| 12 '-t', '--test', action='store_true', dest='dry_run', |
| 13 help='dry-run mode, will not apply any host setup changes' |
| 14 ) |
8 | 15 |
9 def parseOptions(args): | 16 parser.add_argument( |
10 description = 'Run provisioning on the given hosts or groups of hosts' | 17 '-q', '--quiet', action='store_true', dest='quiet', |
11 parser = createArgumentParser(description=description) | 18 help='quiet mode, suppresses Puppet output to console' |
12 parser.add_argument( | 19 ) |
13 '-t', '--test', action='store_true', dest='dry_run', | |
14 help='dry-run mode, will not apply any host setup changes' | |
15 ) | |
16 | 20 |
17 parser.add_argument( | 21 parser.add_argument( |
18 '-q', '--quiet', action='store_true', dest='quiet', | 22 'hosts', metavar='host|group', nargs='+', |
19 help='quiet mode, suppresses Puppet output to console' | 23 help='target host or group, can be specified multiple times', |
20 ) | 24 ) |
21 | 25 |
22 parser.add_argument( | 26 options = parser.parse_args(args) |
23 'hosts', metavar='host|group', nargs='+', | |
24 help='target host or group, can be specified multiple times', | |
25 ) | |
26 | 27 |
27 options = parser.parse_args(args) | 28 if options.quiet and options.dry_run: |
| 29 print >>sys.stderr, 'Only one mode flag can be specified, either -t or -q' |
| 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' |
28 | 37 |
29 if options.quiet and options.dry_run: | 38 return options |
30 print >>sys.stderr, 'Only one mode flag can be specified, either -t or -
q' | |
31 sys.exit(1) | |
32 elif options.quiet: | |
33 options.mode = '' | |
34 elif options.dry_run: | |
35 options.mode = ' --test --noop' | |
36 else: | |
37 options.mode = ' --test' | |
38 | |
39 return options | |
40 | |
41 | 39 |
42 def updateMaster(options): | 40 def updateMaster(options): |
43 print 'Updating data on the puppet master...' | 41 print 'Updating data on the puppet master...' |
44 remoteCommand = ' && '.join([ | 42 remoteCommand = ' && '.join([ |
45 'sudo hg pull -qu -R /etc/puppet/infrastructure', | 43 'sudo hg pull -qu -R /etc/puppet/infrastructure', |
46 'sudo hg pull -qu -R /etc/puppet/infrastructure/modules/private', | 44 'sudo hg pull -qu -R /etc/puppet/infrastructure/modules/private', |
47 'sudo /etc/puppet/infrastructure/ensure_dependencies.py /etc/puppet/infr
astructure', | 45 'sudo /etc/puppet/infrastructure/ensure_dependencies.py /etc/puppet/infrastr
ucture', |
48 ]) | 46 ]) |
49 runCommand(options.user, options.remote, remoteCommand) | 47 runCommand(options.user, options.remote, remoteCommand) |
50 | |
51 | 48 |
52 def updateClient(user, host, mode): | 49 def updateClient(user, host, mode): |
53 print 'Provisioning %s...' % host | 50 print 'Provisioning %s...' % host |
54 remoteCommand = 'sudo puppet agent%s' % mode | 51 remoteCommand = 'sudo puppet agent%s' % mode |
55 | 52 |
56 # Have to ignore errors here, Puppet will return non-zero for successful run
s | 53 # Have to ignore errors here, Puppet will return non-zero for successful runs |
57 runCommand(user, host, remoteCommand, ignore_errors=True) | 54 runCommand(user, host, remoteCommand, ignore_errors=True) |
58 | 55 |
59 if __name__ == '__main__': | 56 if __name__ == '__main__': |
60 options = parseOptions(sys.argv[1:]) | 57 options = parseOptions(sys.argv[1:]) |
61 updateMaster(options) | 58 updateMaster(options) |
62 needKicking = resolveHostList(options) | 59 needKicking = resolveHostList(options) |
63 if len(needKicking) == 0: | 60 if len(needKicking) == 0: |
64 print >>sys.stderr, 'No valid hosts or groups specified, nothing to do' | 61 print >>sys.stderr, 'No valid hosts or groups specified, nothing to do' |
65 sys.exit(0) | 62 sys.exit(0) |
66 for host in needKicking: | 63 for host in needKicking: |
67 updateClient(options.user, host, options.mode) | 64 updateClient(options.user, host, options.mode) |
OLD | NEW |