blob: ec5df7ffe81725e81abc566bbf32956ba5b1e444 [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
J. Richard Barnette868cf642014-07-21 16:34:38 -07006import subprocess
7import sys
Alex Millerb0b2d252014-06-25 17:17:01 -07008
Don Garrett40036362014-12-08 15:52:44 -08009import common
Don Garrett50713462015-01-07 18:04:05 -080010from autotest_lib.server import frontend
Alex Millerb0b2d252014-06-25 17:17:01 -070011from autotest_lib.site_utils.lib import infra
12
Alex Millerb0b2d252014-06-25 17:17:01 -070013
Dan Shi57d4c732015-01-22 18:38:50 -080014def discover_servers(afe, server_filter=set()):
Don Garrett40036362014-12-08 15:52:44 -080015 """Discover the in-production servers to update.
Alex Millerb0b2d252014-06-25 17:17:01 -070016
Don Garretteecbc132015-01-08 17:26:20 -080017 @param afe: Server to contact with RPC requests.
Dan Shi57d4c732015-01-22 18:38:50 -080018 @param server_filter: A set of servers to get status for.
Don Garretteecbc132015-01-08 17:26:20 -080019
Dan Shi57d4c732015-01-22 18:38:50 -080020 @returns: A list of tuple of (server_name, server_status), the list in
21 sorted by the order to be updated.
Don Garrett40036362014-12-08 15:52:44 -080022 """
Don Garrett50713462015-01-07 18:04:05 -080023 # Example server details....
24 # {
25 # 'hostname': 'server1',
26 # 'status': 'backup',
27 # 'roles': ['drone', 'scheduler'],
28 # 'attributes': {'max_processes': 300}
29 # }
Don Garretteecbc132015-01-08 17:26:20 -080030 rpc = frontend.AFE(server=afe)
Don Garrett50713462015-01-07 18:04:05 -080031 servers = rpc.run('get_servers')
Don Garrett40036362014-12-08 15:52:44 -080032
Dan Shi57d4c732015-01-22 18:38:50 -080033 # Do not update servers that need repair, and filter the server list by
34 # given server_filter if needed.
35 servers = [s for s in servers
36 if (s['status'] != 'repair_required' and
37 (not server_filter or s['hostname'] in server_filter))]
Don Garrett40036362014-12-08 15:52:44 -080038
Don Garrett50713462015-01-07 18:04:05 -080039 # Do not update devservers (not YET supported).
40 servers = [s for s in servers if 'devserver' not in s['roles']]
41
42 def update_order(s):
43 """Sort order for updating servers (lower first).
44
45 @param s: Server details for a single server.
46 """
47 if 'database' in s['roles']:
48 return 0
49 if 'scheduler' in s['roles']:
50 return 1
51 return 2
52
53 # Order in which servers are updated.
54 servers.sort(key=update_order)
55
Dan Shi57d4c732015-01-22 18:38:50 -080056 # Build the return list of (hostname, status)
57 server_status = [(s['hostname'], s['status']) for s in servers]
58 found_servers = set([s['hostname'] for s in servers])
59 # Inject the servers passed in by user but not found in server database.
60 for server in server_filter-found_servers:
61 server_status.append((server, 'unknown'))
62
63 return server_status
Alex Millerb0b2d252014-06-25 17:17:01 -070064
J. Richard Barnettef533b182014-09-04 18:24:42 -070065
Don Garrett40036362014-12-08 15:52:44 -080066def parse_arguments(args):
67 """Parse command line arguments.
68
69 @param args: The command line arguments to parse. (usually sys.argv[1:])
70
71 @returns An argparse.Namespace populated with argument values.
72 """
73 parser = argparse.ArgumentParser(
Don Garrett3f2b6602014-12-16 18:19:16 -080074 formatter_class=argparse.RawDescriptionHelpFormatter,
75 description='Command to update an entire autotest installation.',
76 epilog=('Update all servers:\n'
77 ' deploy_production.py\n'
78 '\n'
79 'Update one server:\n'
80 ' deploy_production.py <server>\n'
81 '\n'
82 'Send arguments to remote deploy_production_local.py:\n'
83 ' deploy_production.py -- --dryrun\n'
84 '\n'
85 'See what arguments would be run on specified servers:\n'
86 ' deploy_production.py --dryrun <server_a> <server_b> --'
87 ' --skip-update\n'))
88
Don Garrett40036362014-12-08 15:52:44 -080089 parser.add_argument('--continue', action='store_true', dest='cont',
Don Garretteecbc132015-01-08 17:26:20 -080090 help='Continue to the next server on failure.')
91 parser.add_argument('--afe', default='cautotest',
92 help='What is the main server for this installation? (cautotest).')
Don Garrett40036362014-12-08 15:52:44 -080093 parser.add_argument('--dryrun', action='store_true',
Don Garretteecbc132015-01-08 17:26:20 -080094 help='Don\'t actually run remote commands.')
Don Garrett40036362014-12-08 15:52:44 -080095 parser.add_argument('args', nargs=argparse.REMAINDER,
Don Garretteecbc132015-01-08 17:26:20 -080096 help=('<server>, <server> ... -- <remote_arg>, <remote_arg> ...'))
Don Garrett40036362014-12-08 15:52:44 -080097
98 results = parser.parse_args(args)
99
Don Garrett3f2b6602014-12-16 18:19:16 -0800100 # We take the args list and further split it down. Everything before --
101 # is a server name, and everything after it is an argument to pass along
102 # to deploy_production_local.py.
103 #
104 # This:
105 # server_a, server_b -- --dryrun --skip-report
106 #
107 # Becomes:
108 # args.servers['server_a', 'server_b']
109 # args.args['--dryrun', '--skip-report']
110 try:
111 local_args_index = results.args.index('--') + 1
112 except ValueError:
113 # If -- isn't present, they are all servers.
114 results.servers = results.args
115 results.args = []
116 else:
117 # Split arguments.
118 results.servers = results.args[:local_args_index-1]
119 results.args = results.args[local_args_index:]
Don Garrett40036362014-12-08 15:52:44 -0800120
121 return results
J. Richard Barnettef533b182014-09-04 18:24:42 -0700122
123
Don Garrett40036362014-12-08 15:52:44 -0800124def main(args):
125 """Main routine that drives all the real work.
Alex Millerb0b2d252014-06-25 17:17:01 -0700126
Don Garrett40036362014-12-08 15:52:44 -0800127 @param args: The command line arguments to parse. (usually sys.argv[1:])
J. Richard Barnette868cf642014-07-21 16:34:38 -0700128
Don Garrett40036362014-12-08 15:52:44 -0800129 @returns The system exit code.
130 """
131 options = parse_arguments(args)
Alex Millerb0b2d252014-06-25 17:17:01 -0700132
Dan Shi57d4c732015-01-22 18:38:50 -0800133 print('Retrieving server status...')
134 server_status = discover_servers(options.afe, set(options.servers or []))
Alex Millerb0b2d252014-06-25 17:17:01 -0700135
Don Garrett40036362014-12-08 15:52:44 -0800136 # Display what we plan to update.
137 print('Will update (in this order):')
Dan Shi57d4c732015-01-22 18:38:50 -0800138 for server, status in server_status:
139 print('\t%-36s:\t%s' % (server, status))
Don Garrett40036362014-12-08 15:52:44 -0800140 print()
Alex Millerb0b2d252014-06-25 17:17:01 -0700141
Don Garrett40036362014-12-08 15:52:44 -0800142 # Do the updating.
Dan Shi57d4c732015-01-22 18:38:50 -0800143 for server, status in server_status:
144 if status == 'backup':
145 extra_args = ['--skip-service-status']
146 else:
147 extra_args = []
148
Don Garrett40036362014-12-08 15:52:44 -0800149 cmd = ('/usr/local/autotest/contrib/deploy_production_local.py ' +
Dan Shi57d4c732015-01-22 18:38:50 -0800150 ' '.join(options.args + extra_args))
Don Garrett40036362014-12-08 15:52:44 -0800151 print('%s: %s' % (server, cmd))
152 if not options.dryrun:
153 try:
Don Garrett699b4b32014-12-11 13:10:15 -0800154 out = infra.execute_command(server, cmd)
155 print(out)
Don Garrett40036362014-12-08 15:52:44 -0800156 print('Success')
157 print()
158 except subprocess.CalledProcessError as e:
159 print('Error:')
160 print(e.output)
161 if not options.cont:
162 return 1
J. Richard Barnettef533b182014-09-04 18:24:42 -0700163
164
165if __name__ == '__main__':
Don Garrett40036362014-12-08 15:52:44 -0800166 sys.exit(main(sys.argv[1:]))