Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Runs on autotest servers from a cron job to self update them. |
| 7 | |
| 8 | This script is designed to run on all autotest servers to allow them to |
| 9 | automatically self-update based on the manifests used to create their (existing) |
| 10 | repos. |
| 11 | """ |
| 12 | |
| 13 | from __future__ import print_function |
| 14 | |
| 15 | import ConfigParser |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 16 | import argparse |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 17 | import os |
Don Garrett | 699b4b3 | 2014-12-11 13:10:15 -0800 | [diff] [blame] | 18 | import re |
Dan Shi | cf27804 | 2016-04-06 21:16:34 -0700 | [diff] [blame] | 19 | import socket |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 20 | import subprocess |
| 21 | import sys |
| 22 | import time |
| 23 | |
| 24 | import common |
| 25 | |
| 26 | from autotest_lib.client.common_lib import global_config |
Dan Shi | ac6fdbf | 2016-04-09 17:36:28 -0700 | [diff] [blame] | 27 | from autotest_lib.server import utils as server_utils |
Dan Shi | cf27804 | 2016-04-06 21:16:34 -0700 | [diff] [blame] | 28 | from autotest_lib.server.cros.dynamic_suite import frontend_wrappers |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 29 | |
Dan Shi | ac6fdbf | 2016-04-09 17:36:28 -0700 | [diff] [blame] | 30 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 31 | # How long after restarting a service do we watch it to see if it's stable. |
Don Garrett | 6073ba9 | 2015-07-23 15:01:39 -0700 | [diff] [blame] | 32 | SERVICE_STABILITY_TIMER = 120 |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 33 | |
Dan Shi | cf27804 | 2016-04-06 21:16:34 -0700 | [diff] [blame] | 34 | # A list of commands that only applies to primary server. For example, |
| 35 | # test_importer should only be run in primary master scheduler. If two servers |
| 36 | # are both running test_importer, there is a chance to fail as both try to |
| 37 | # update the same table. |
| 38 | PRIMARY_ONLY_COMMANDS = ['test_importer'] |
Shuqian Zhao | a3438a5 | 2016-09-20 15:11:02 -0700 | [diff] [blame] | 39 | # A dict to map update_commands defined in config file to repos or files that |
| 40 | # decide whether need to update these commands. E.g. if no changes under |
| 41 | # frontend repo, no need to update afe. |
Aviv Keshet | 8562203 | 2016-10-03 03:17:26 -0700 | [diff] [blame] | 42 | COMMANDS_TO_REPOS_DICT = {'afe': 'frontend/', |
| 43 | 'tko': 'tko/'} |
Dan Shi | cf27804 | 2016-04-06 21:16:34 -0700 | [diff] [blame] | 44 | |
Dan Shi | ac6fdbf | 2016-04-09 17:36:28 -0700 | [diff] [blame] | 45 | AFE = frontend_wrappers.RetryingAFE( |
| 46 | server=server_utils.get_global_afe_hostname(), timeout_min=5, |
| 47 | delay_sec=10) |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 48 | |
| 49 | class DirtyTreeException(Exception): |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 50 | """Raised when the tree has been modified in an unexpected way.""" |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 51 | |
| 52 | |
| 53 | class UnknownCommandException(Exception): |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 54 | """Raised when we try to run a command name with no associated command.""" |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 55 | |
| 56 | |
| 57 | class UnstableServices(Exception): |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 58 | """Raised if a service appears unstable after restart.""" |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 59 | |
| 60 | |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 61 | def strip_terminal_codes(text): |
| 62 | """This function removes all terminal formatting codes from a string. |
| 63 | |
| 64 | @param text: String of text to cleanup. |
| 65 | @returns String with format codes removed. |
| 66 | """ |
| 67 | ESC = '\x1b' |
| 68 | return re.sub(ESC+r'\[[^m]*m', '', text) |
| 69 | |
| 70 | |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 71 | def verify_repo_clean(): |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 72 | """This function cleans the current repo then verifies that it is valid. |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 73 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 74 | @raises DirtyTreeException if the repo is still not clean. |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 75 | @raises subprocess.CalledProcessError on a repo command failure. |
| 76 | """ |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 77 | subprocess.check_output(['git', 'reset', '--hard']) |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 78 | out = subprocess.check_output(['repo', 'status'], stderr=subprocess.STDOUT) |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 79 | out = strip_terminal_codes(out).strip() |
Don Garrett | 699b4b3 | 2014-12-11 13:10:15 -0800 | [diff] [blame] | 80 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 81 | if not 'working directory clean' in out: |
Dan Shi | cf27804 | 2016-04-06 21:16:34 -0700 | [diff] [blame] | 82 | raise DirtyTreeException(out) |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 83 | |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 84 | |
| 85 | def repo_versions(): |
| 86 | """This function collects the versions of all git repos in the general repo. |
| 87 | |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 88 | @returns A dictionary mapping project names to git hashes for HEAD. |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 89 | @raises subprocess.CalledProcessError on a repo command failure. |
| 90 | """ |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 91 | cmd = ['repo', 'forall', '-p', '-c', 'pwd && git log -1 --format=%h'] |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 92 | output = strip_terminal_codes(subprocess.check_output(cmd)) |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 93 | |
| 94 | # The expected output format is: |
| 95 | |
| 96 | # project chrome_build/ |
| 97 | # /dir/holding/chrome_build |
| 98 | # 73dee9d |
| 99 | # |
| 100 | # project chrome_release/ |
| 101 | # /dir/holding/chrome_release |
| 102 | # 9f3a5d8 |
| 103 | |
| 104 | lines = output.splitlines() |
| 105 | |
| 106 | PROJECT_PREFIX = 'project ' |
| 107 | |
| 108 | project_heads = {} |
| 109 | for n in range(0, len(lines), 4): |
| 110 | project_line = lines[n] |
| 111 | project_dir = lines[n+1] |
| 112 | project_hash = lines[n+2] |
| 113 | # lines[n+3] is a blank line, but doesn't exist for the final block. |
| 114 | |
| 115 | # Convert 'project chrome_build/' -> 'chrome_build' |
| 116 | assert project_line.startswith(PROJECT_PREFIX) |
| 117 | name = project_line[len(PROJECT_PREFIX):].rstrip('/') |
| 118 | |
| 119 | project_heads[name] = (project_dir, project_hash) |
| 120 | |
| 121 | return project_heads |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 122 | |
| 123 | |
Shuqian Zhao | a3438a5 | 2016-09-20 15:11:02 -0700 | [diff] [blame] | 124 | def repo_versions_to_decide_whether_run_cmd_update(): |
| 125 | """Collect versions of repos/files defined in COMMANDS_TO_REPOS_DICT. |
| 126 | |
| 127 | For the update_commands defined in config files, no need to run the command |
| 128 | every time. Only run it when the repos/files related to the commands have |
| 129 | been changed. |
| 130 | |
| 131 | @returns A set of tuples: {(cmd, repo_version), ()...} |
| 132 | """ |
| 133 | results = set() |
| 134 | for cmd, repo in COMMANDS_TO_REPOS_DICT.iteritems(): |
| 135 | version = subprocess.check_output( |
| 136 | ['git', 'log', '-1', '--pretty=tformat:%h', |
| 137 | '%s/%s' % (common.autotest_dir, repo)]) |
| 138 | results.add((cmd, version.strip())) |
| 139 | return results |
| 140 | |
| 141 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 142 | def repo_sync(update_push_servers=False): |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 143 | """Perform a repo sync. |
| 144 | |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 145 | @param update_push_servers: If True, then update test_push servers to ToT. |
| 146 | Otherwise, update server to prod branch. |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 147 | @raises subprocess.CalledProcessError on a repo command failure. |
| 148 | """ |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 149 | subprocess.check_output(['repo', 'sync']) |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 150 | if update_push_servers: |
| 151 | print('Updating push servers, checkout cros/master') |
| 152 | subprocess.check_output(['git', 'checkout', 'cros/master']) |
| 153 | else: |
| 154 | print('Updating server to prod branch') |
| 155 | subprocess.check_output(['git', 'checkout', 'cros/prod']) |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 156 | |
| 157 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 158 | def discover_update_commands(): |
| 159 | """Lookup the commands to run on this server. |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 160 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 161 | These commonly come from shadow_config.ini, since they vary by server type. |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 162 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 163 | @returns List of command names in string format. |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 164 | """ |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 165 | try: |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 166 | return global_config.global_config.get_config_value( |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 167 | 'UPDATE', 'commands', type=list) |
| 168 | |
| 169 | except (ConfigParser.NoSectionError, global_config.ConfigError): |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 170 | return [] |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 171 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 172 | |
| 173 | def discover_restart_services(): |
| 174 | """Find the services that need restarting on the current server. |
| 175 | |
| 176 | These commonly come from shadow_config.ini, since they vary by server type. |
| 177 | |
| 178 | @returns List of service names in string format. |
| 179 | """ |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 180 | try: |
| 181 | # From shadow_config.ini, lookup which services to restart. |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 182 | return global_config.global_config.get_config_value( |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 183 | 'UPDATE', 'services', type=list) |
| 184 | |
| 185 | except (ConfigParser.NoSectionError, global_config.ConfigError): |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 186 | return [] |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 187 | |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 188 | |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 189 | def update_command(cmd_tag, dryrun=False): |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 190 | """Restart a command. |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 191 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 192 | The command name is looked up in global_config.ini to find the full command |
| 193 | to run, then it's executed. |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 194 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 195 | @param cmd_tag: Which command to restart. |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 196 | @param dryrun: If true print the command that would have been run. |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 197 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 198 | @raises UnknownCommandException If cmd_tag can't be looked up. |
| 199 | @raises subprocess.CalledProcessError on a command failure. |
| 200 | """ |
| 201 | # Lookup the list of commands to consider. They are intended to be |
| 202 | # in global_config.ini so that they can be shared everywhere. |
| 203 | cmds = dict(global_config.global_config.config.items( |
| 204 | 'UPDATE_COMMANDS')) |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 205 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 206 | if cmd_tag not in cmds: |
| 207 | raise UnknownCommandException(cmd_tag, cmds) |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 208 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 209 | expanded_command = cmds[cmd_tag].replace('AUTOTEST_REPO', |
| 210 | common.autotest_dir) |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 211 | |
Don Garrett | 699b4b3 | 2014-12-11 13:10:15 -0800 | [diff] [blame] | 212 | print('Running: %s: %s' % (cmd_tag, expanded_command)) |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 213 | if dryrun: |
Don Garrett | 699b4b3 | 2014-12-11 13:10:15 -0800 | [diff] [blame] | 214 | print('Skip: %s' % expanded_command) |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 215 | else: |
Don Garrett | 4769c90 | 2015-01-05 15:58:56 -0800 | [diff] [blame] | 216 | try: |
| 217 | subprocess.check_output(expanded_command, shell=True, |
| 218 | stderr=subprocess.STDOUT) |
| 219 | except subprocess.CalledProcessError as e: |
| 220 | print('FAILED:') |
| 221 | print(e.output) |
| 222 | raise |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 223 | |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 224 | |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 225 | def restart_service(service_name, dryrun=False): |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 226 | """Restart a service. |
| 227 | |
| 228 | Restarts the standard service with "service <name> restart". |
| 229 | |
| 230 | @param service_name: The name of the service to restart. |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 231 | @param dryrun: Don't really run anything, just print out the command. |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 232 | |
| 233 | @raises subprocess.CalledProcessError on a command failure. |
| 234 | """ |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 235 | cmd = ['sudo', 'service', service_name, 'restart'] |
Don Garrett | 699b4b3 | 2014-12-11 13:10:15 -0800 | [diff] [blame] | 236 | print('Restarting: %s' % service_name) |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 237 | if dryrun: |
Don Garrett | 699b4b3 | 2014-12-11 13:10:15 -0800 | [diff] [blame] | 238 | print('Skip: %s' % ' '.join(cmd)) |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 239 | else: |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 240 | subprocess.check_call(cmd) |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 241 | |
| 242 | |
| 243 | def service_status(service_name): |
| 244 | """Return the results "status <name>" for a given service. |
| 245 | |
| 246 | This string is expected to contain the pid, and so to change is the service |
| 247 | is shutdown or restarted for any reason. |
| 248 | |
| 249 | @param service_name: The name of the service to check on. |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 250 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 251 | @returns The output of the external command. |
| 252 | Ex: autofs start/running, process 1931 |
| 253 | |
| 254 | @raises subprocess.CalledProcessError on a command failure. |
| 255 | """ |
| 256 | return subprocess.check_output(['sudo', 'status', service_name]) |
| 257 | |
| 258 | |
Dan Shi | 57d4c73 | 2015-01-22 18:38:50 -0800 | [diff] [blame] | 259 | def restart_services(service_names, dryrun=False, skip_service_status=False): |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 260 | """Restart services as needed for the current server type. |
| 261 | |
| 262 | Restart the listed set of services, and watch to see if they are stable for |
| 263 | at least SERVICE_STABILITY_TIMER. It restarts all services quickly, |
| 264 | waits for that delay, then verifies the status of all of them. |
| 265 | |
| 266 | @param service_names: The list of service to restart and monitor. |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 267 | @param dryrun: Don't really restart the service, just print out the command. |
Dan Shi | 57d4c73 | 2015-01-22 18:38:50 -0800 | [diff] [blame] | 268 | @param skip_service_status: Set to True to skip service status check. |
| 269 | Default is False. |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 270 | |
| 271 | @raises subprocess.CalledProcessError on a command failure. |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 272 | @raises UnstableServices if any services are unstable after restart. |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 273 | """ |
| 274 | service_statuses = {} |
| 275 | |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 276 | if dryrun: |
| 277 | for name in service_names: |
| 278 | restart_service(name, dryrun=True) |
| 279 | return |
| 280 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 281 | # Restart each, and record the status (including pid). |
| 282 | for name in service_names: |
| 283 | restart_service(name) |
| 284 | service_statuses[name] = service_status(name) |
| 285 | |
Dan Shi | 57d4c73 | 2015-01-22 18:38:50 -0800 | [diff] [blame] | 286 | # Skip service status check if --skip-service-status is specified. Used for |
| 287 | # servers in backup status. |
| 288 | if skip_service_status: |
| 289 | print('--skip-service-status is specified, skip checking services.') |
| 290 | return |
| 291 | |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 292 | # Wait for a while to let the services settle. |
| 293 | time.sleep(SERVICE_STABILITY_TIMER) |
| 294 | |
| 295 | # Look for any services that changed status. |
| 296 | unstable_services = [n for n in service_names |
| 297 | if service_status(n) != service_statuses[n]] |
| 298 | |
| 299 | # Report any services having issues. |
| 300 | if unstable_services: |
| 301 | raise UnstableServices(unstable_services) |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 302 | |
| 303 | |
Shuqian Zhao | a3438a5 | 2016-09-20 15:11:02 -0700 | [diff] [blame] | 304 | def run_deploy_actions(cmds_skip=set(), dryrun=False, |
| 305 | skip_service_status=False): |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 306 | """Run arbitrary update commands specified in global.ini. |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 307 | |
Shuqian Zhao | a3438a5 | 2016-09-20 15:11:02 -0700 | [diff] [blame] | 308 | @param cmds_skip: cmds no need to run since the corresponding repo/file |
| 309 | does not change. |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 310 | @param dryrun: Don't really restart the service, just print out the command. |
Dan Shi | 57d4c73 | 2015-01-22 18:38:50 -0800 | [diff] [blame] | 311 | @param skip_service_status: Set to True to skip service status check. |
| 312 | Default is False. |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 313 | |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 314 | @raises subprocess.CalledProcessError on a command failure. |
| 315 | @raises UnstableServices if any services are unstable after restart. |
| 316 | """ |
Shuqian Zhao | a3438a5 | 2016-09-20 15:11:02 -0700 | [diff] [blame] | 317 | defined_cmds = set(discover_update_commands()) |
| 318 | cmds = defined_cmds - cmds_skip |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 319 | if cmds: |
| 320 | print('Running update commands:', ', '.join(cmds)) |
| 321 | for cmd in cmds: |
Dan Shi | cf27804 | 2016-04-06 21:16:34 -0700 | [diff] [blame] | 322 | if (cmd in PRIMARY_ONLY_COMMANDS and |
| 323 | not AFE.run('get_servers', hostname=socket.getfqdn(), |
| 324 | status='primary')): |
| 325 | print('Command %s is only applicable to primary servers.' % cmd) |
| 326 | continue |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 327 | update_command(cmd, dryrun=dryrun) |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 328 | |
| 329 | services = discover_restart_services() |
| 330 | if services: |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 331 | print('Restarting Services:', ', '.join(services)) |
Dan Shi | 57d4c73 | 2015-01-22 18:38:50 -0800 | [diff] [blame] | 332 | restart_services(services, dryrun=dryrun, |
| 333 | skip_service_status=skip_service_status) |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 334 | |
| 335 | |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 336 | def report_changes(versions_before, versions_after): |
| 337 | """Produce a report describing what changed in all repos. |
| 338 | |
| 339 | @param versions_before: Results of repo_versions() from before the update. |
| 340 | @param versions_after: Results of repo_versions() from after the update. |
| 341 | |
| 342 | @returns string containing a human friendly changes report. |
| 343 | """ |
| 344 | result = [] |
| 345 | |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 346 | if versions_after: |
| 347 | for project in sorted(set(versions_before.keys() + versions_after.keys())): |
| 348 | result.append('%s:' % project) |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 349 | |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 350 | _, before_hash = versions_before.get(project, (None, None)) |
| 351 | after_dir, after_hash = versions_after.get(project, (None, None)) |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 352 | |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 353 | if project not in versions_before: |
| 354 | result.append('Added.') |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 355 | |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 356 | elif project not in versions_after: |
| 357 | result.append('Removed.') |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 358 | |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 359 | elif before_hash == after_hash: |
| 360 | result.append('No Change.') |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 361 | |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 362 | else: |
| 363 | hashes = '%s..%s' % (before_hash, after_hash) |
| 364 | cmd = ['git', 'log', hashes, '--oneline'] |
| 365 | out = subprocess.check_output(cmd, cwd=after_dir, |
| 366 | stderr=subprocess.STDOUT) |
| 367 | result.append(out.strip()) |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 368 | |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 369 | result.append('') |
| 370 | else: |
| 371 | for project in sorted(versions_before.keys()): |
| 372 | _, before_hash = versions_before[project] |
| 373 | result.append('%s: %s' % (project, before_hash)) |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 374 | result.append('') |
| 375 | |
| 376 | return '\n'.join(result) |
| 377 | |
| 378 | |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 379 | def parse_arguments(args): |
| 380 | """Parse command line arguments. |
| 381 | |
| 382 | @param args: The command line arguments to parse. (ususally sys.argsv[1:]) |
| 383 | |
Don Garrett | 4003636 | 2014-12-08 15:52:44 -0800 | [diff] [blame] | 384 | @returns An argparse.Namespace populated with argument values. |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 385 | """ |
| 386 | parser = argparse.ArgumentParser( |
| 387 | description='Command to update an autotest server.') |
| 388 | parser.add_argument('--skip-verify', action='store_false', |
| 389 | dest='verify', default=True, |
| 390 | help='Disable verification of a clean repository.') |
| 391 | parser.add_argument('--skip-update', action='store_false', |
| 392 | dest='update', default=True, |
| 393 | help='Skip the repository source code update.') |
| 394 | parser.add_argument('--skip-actions', action='store_false', |
| 395 | dest='actions', default=True, |
| 396 | help='Skip the post update actions.') |
| 397 | parser.add_argument('--skip-report', action='store_false', |
| 398 | dest='report', default=True, |
| 399 | help='Skip the git version report.') |
Don Garrett | e371891 | 2014-12-05 13:11:44 -0800 | [diff] [blame] | 400 | parser.add_argument('--actions-only', action='store_true', |
| 401 | help='Run the post update actions (restart services).') |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 402 | parser.add_argument('--dryrun', action='store_true', |
| 403 | help='Don\'t actually run any commands, just log.') |
Dan Shi | 57d4c73 | 2015-01-22 18:38:50 -0800 | [diff] [blame] | 404 | parser.add_argument('--skip-service-status', action='store_true', |
| 405 | help='Skip checking the service status.') |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 406 | parser.add_argument('--update_push_servers', action='store_true', |
| 407 | help='Indicate to update test_push server. If not ' |
| 408 | 'specify, then update server to production.') |
Shuqian Zhao | a3438a5 | 2016-09-20 15:11:02 -0700 | [diff] [blame] | 409 | parser.add_argument('--force_update', action='store_true', |
| 410 | help='Force to run the update commands for afe, tko ' |
| 411 | 'and build_externals') |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 412 | |
| 413 | results = parser.parse_args(args) |
| 414 | |
Don Garrett | e371891 | 2014-12-05 13:11:44 -0800 | [diff] [blame] | 415 | if results.actions_only: |
| 416 | results.verify = False |
| 417 | results.update = False |
| 418 | results.report = False |
| 419 | |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 420 | # TODO(dgarrett): Make these behaviors support dryrun. |
| 421 | if results.dryrun: |
| 422 | results.verify = False |
| 423 | results.update = False |
| 424 | |
| 425 | return results |
| 426 | |
| 427 | |
| 428 | def main(args): |
| 429 | """Main method.""" |
| 430 | os.chdir(common.autotest_dir) |
| 431 | global_config.global_config.parse_config_file() |
| 432 | |
| 433 | behaviors = parse_arguments(args) |
| 434 | |
| 435 | if behaviors.verify: |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 436 | try: |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 437 | print('Checking tree status:') |
| 438 | verify_repo_clean() |
| 439 | print('Clean.') |
| 440 | except DirtyTreeException as e: |
| 441 | print('Local tree is dirty, can\'t perform update safely.') |
| 442 | print() |
| 443 | print('repo status:') |
Don Garrett | d032172 | 2014-11-18 16:03:33 -0800 | [diff] [blame] | 444 | print(e.args[0]) |
| 445 | return 1 |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 446 | |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 447 | versions_before = repo_versions() |
| 448 | versions_after = {} |
Shuqian Zhao | a3438a5 | 2016-09-20 15:11:02 -0700 | [diff] [blame] | 449 | cmd_versions_before = repo_versions_to_decide_whether_run_cmd_update() |
| 450 | cmd_versions_after = {} |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 451 | |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 452 | if behaviors.update: |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 453 | print('Updating Repo.') |
Shuqian Zhao | 8754a1a | 2016-08-24 12:54:11 -0700 | [diff] [blame] | 454 | repo_sync(behaviors.update_push_servers) |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 455 | versions_after = repo_versions() |
Shuqian Zhao | a3438a5 | 2016-09-20 15:11:02 -0700 | [diff] [blame] | 456 | cmd_versions_after = repo_versions_to_decide_whether_run_cmd_update() |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 457 | |
| 458 | if behaviors.actions: |
Shuqian Zhao | a3438a5 | 2016-09-20 15:11:02 -0700 | [diff] [blame] | 459 | # If the corresponding repo/file not change, no need to run the cmd. |
| 460 | cmds_skip = (set() if behaviors.force_update else |
| 461 | {t[0] for t in cmd_versions_before & cmd_versions_after}) |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 462 | try: |
Dan Shi | 57d4c73 | 2015-01-22 18:38:50 -0800 | [diff] [blame] | 463 | run_deploy_actions( |
Shuqian Zhao | a3438a5 | 2016-09-20 15:11:02 -0700 | [diff] [blame] | 464 | cmds_skip, behaviors.dryrun, behaviors.skip_service_status) |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 465 | except UnstableServices as e: |
| 466 | print('The following services were not stable after ' |
| 467 | 'the update:') |
| 468 | print(e.args[0]) |
| 469 | return 1 |
| 470 | |
Don Garrett | 3571121 | 2014-12-18 14:33:41 -0800 | [diff] [blame] | 471 | if behaviors.report: |
Don Garrett | fa2c1c4 | 2014-12-11 12:11:49 -0800 | [diff] [blame] | 472 | print('Changes:') |
| 473 | print(report_changes(versions_before, versions_after)) |
Don Garrett | 8db752c | 2014-10-17 16:56:55 -0700 | [diff] [blame] | 474 | |
| 475 | |
| 476 | if __name__ == '__main__': |
Don Garrett | 03432d6 | 2014-11-19 18:18:35 -0800 | [diff] [blame] | 477 | sys.exit(main(sys.argv[1:])) |