blob: 242ae017a003d02ba6fa528950562ebba73ce9db [file] [log] [blame]
Alex Millerb0b2d252014-06-25 17:17:01 -07001#!/usr/bin/python
2
Don Garrett40036362014-12-08 15:52:44 -08003from __future__ import print_function
4
5import argparse
6import itertools
J. Richard Barnette868cf642014-07-21 16:34:38 -07007import subprocess
8import sys
Alex Millerb0b2d252014-06-25 17:17:01 -07009
Don Garrett40036362014-12-08 15:52:44 -080010import common
Alex Millerb0b2d252014-06-25 17:17:01 -070011from autotest_lib.site_utils.lib import infra
12
Alex Millerb0b2d252014-06-25 17:17:01 -070013
Don Garrett40036362014-12-08 15:52:44 -080014def discover_servers():
15 """Discover the in-production servers to update.
Alex Millerb0b2d252014-06-25 17:17:01 -070016
Don Garrett40036362014-12-08 15:52:44 -080017 @returns A list of server host-names, in order for updates.
18 """
19 shards = infra.shard_servers()
20 sams = infra.sam_servers()
21 drones = infra.drone_servers()
22 databases = infra.database_servers()
23 extras = infra.extra_servers()
24
25 # We don't manage devservers (yet).
26 # devservers = infra.devserver_servers()
27
28 # This line controls the order in which we update servers.
29 return list(itertools.chain(shards, sams, drones, extras, databases))
Alex Millerb0b2d252014-06-25 17:17:01 -070030
J. Richard Barnettef533b182014-09-04 18:24:42 -070031
Don Garrett40036362014-12-08 15:52:44 -080032def parse_arguments(args):
33 """Parse command line arguments.
34
35 @param args: The command line arguments to parse. (usually sys.argv[1:])
36
37 @returns An argparse.Namespace populated with argument values.
38 """
39 parser = argparse.ArgumentParser(
40 description='Command to update an entire autotest installation.')
41 parser.add_argument('--continue', action='store_true', dest='cont',
42 help='Continue to the next server on failure.')
43 parser.add_argument('--dryrun', action='store_true',
44 help='Don\'t actually run remote commands.')
45 parser.add_argument('args', nargs=argparse.REMAINDER,
46 help=('deploy_production_local.py arguments'
47 ' (use -- to separate).'))
48
49 results = parser.parse_args(args)
50
51 # If -- was used to pass arguments to the remote processes, we don't need to
52 # pass the -- along.
53 if results.args and results.args[0] == '--':
54 results.args.pop(0)
55
56 return results
J. Richard Barnettef533b182014-09-04 18:24:42 -070057
58
Don Garrett40036362014-12-08 15:52:44 -080059def main(args):
60 """Main routine that drives all the real work.
Alex Millerb0b2d252014-06-25 17:17:01 -070061
Don Garrett40036362014-12-08 15:52:44 -080062 @param args: The command line arguments to parse. (usually sys.argv[1:])
J. Richard Barnette868cf642014-07-21 16:34:38 -070063
Don Garrett40036362014-12-08 15:52:44 -080064 @returns The system exit code.
65 """
66 options = parse_arguments(args)
Alex Millerb0b2d252014-06-25 17:17:01 -070067
Don Garrett40036362014-12-08 15:52:44 -080068 print('Discover servers...')
69 servers = discover_servers()
70 print()
Alex Millerb0b2d252014-06-25 17:17:01 -070071
Don Garrett40036362014-12-08 15:52:44 -080072 # Display what we plan to update.
73 print('Will update (in this order):')
74 for server in servers:
75 print(' ', server)
76 print()
Alex Millerb0b2d252014-06-25 17:17:01 -070077
Don Garrett40036362014-12-08 15:52:44 -080078 # Do the updating.
79 for server in servers:
80 cmd = ('/usr/local/autotest/contrib/deploy_production_local.py ' +
81 ' '.join(options.args))
82 print('%s: %s' % (server, cmd))
83 if not options.dryrun:
84 try:
85 infra.execute_command(server, cmd)
86 print('Success')
87 print()
88 except subprocess.CalledProcessError as e:
89 print('Error:')
90 print(e.output)
91 if not options.cont:
92 return 1
J. Richard Barnettef533b182014-09-04 18:24:42 -070093
94
95if __name__ == '__main__':
Don Garrett40036362014-12-08 15:52:44 -080096 sys.exit(main(sys.argv[1:]))