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