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