xixuan | a4f4e71 | 2017-05-08 15:17:54 -0700 | [diff] [blame] | 1 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """An executable function cros-update for auto-update of a CrOS host. |
| 6 | |
| 7 | The reason to create this file is to let devserver to trigger a background |
| 8 | process for CrOS auto-update. Therefore, when devserver service is restarted |
| 9 | sometimes, the CrOS auto-update process is still running and the corresponding |
| 10 | provision task won't claim failure. |
| 11 | |
| 12 | It includes two classes: |
| 13 | a. CrOSUpdateTrigger: |
| 14 | 1. Includes all logics which identify which types of update need to be |
| 15 | performed in the current DUT. |
| 16 | 2. Responsible for write current status of CrOS auto-update process into |
| 17 | progress_tracker. |
| 18 | |
| 19 | b. CrOSAUParser: |
| 20 | 1. Pre-setup the required args for CrOS auto-update. |
| 21 | 2. Parse the input parameters for cmd that runs 'cros_update.py'. |
| 22 | """ |
| 23 | |
| 24 | from __future__ import print_function |
| 25 | |
| 26 | import argparse |
| 27 | import cros_update_logging |
| 28 | import cros_update_progress |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 29 | import logging # pylint: disable=cros-logging-import |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 30 | import os |
| 31 | import sys |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 32 | import time |
xixuan | 28d9907 | 2016-10-06 12:24:16 -0700 | [diff] [blame] | 33 | import traceback |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 34 | |
xixuan | cf58dd3 | 2016-08-24 13:57:06 -0700 | [diff] [blame] | 35 | # only import setup_chromite before chromite import. |
| 36 | import setup_chromite # pylint: disable=unused-import |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 37 | try: |
| 38 | from chromite.lib import auto_updater |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 39 | from chromite.lib import cros_build_lib |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 40 | from chromite.lib import remote_access |
| 41 | from chromite.lib import timeout_util |
| 42 | except ImportError as e: |
| 43 | logging.debug('chromite cannot be imported: %r', e) |
| 44 | auto_updater = None |
| 45 | remote_access = None |
xixuan | cf58dd3 | 2016-08-24 13:57:06 -0700 | [diff] [blame] | 46 | timeout_util = None |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 47 | |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 48 | # The build channel for recovering host's stateful partition |
| 49 | STABLE_BUILD_CHANNEL = 'stable-channel' |
| 50 | |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 51 | # Timeout for CrOS auto-update process. |
| 52 | CROS_UPDATE_TIMEOUT_MIN = 30 |
| 53 | |
| 54 | # The preserved path in remote device, won't be deleted after rebooting. |
| 55 | CROS_PRESERVED_PATH = ('/mnt/stateful_partition/unencrypted/' |
| 56 | 'preserve/cros-update') |
| 57 | |
| 58 | # Standard error tmeplate to be written into status tracking log. |
xixuan | 28d9907 | 2016-10-06 12:24:16 -0700 | [diff] [blame] | 59 | CROS_ERROR_TEMPLATE = cros_update_progress.ERROR_TAG + ' %s' |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 60 | |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 61 | # How long after a quick provision fails to wait before falling back to the |
| 62 | # standard provisioning flow. |
| 63 | QUICK_PROVISION_FAILURE_DELAY_SEC = 45 |
| 64 | |
xixuan | 27d5044 | 2017-08-09 10:38:25 -0700 | [diff] [blame] | 65 | # Setting logging level |
| 66 | logConfig = cros_update_logging.loggingConfig() |
| 67 | logConfig.ConfigureLogging() |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 68 | |
| 69 | class CrOSAUParser(object): |
| 70 | """Custom command-line options parser for cros-update.""" |
| 71 | def __init__(self): |
| 72 | self.args = sys.argv[1:] |
| 73 | self.parser = argparse.ArgumentParser( |
| 74 | usage='%(prog)s [options] [control-file]') |
| 75 | self.SetupOptions() |
| 76 | self.removed_args = [] |
| 77 | |
| 78 | # parse an empty list of arguments in order to set self.options |
| 79 | # to default values. |
| 80 | self.options = self.parser.parse_args(args=[]) |
| 81 | |
| 82 | def SetupOptions(self): |
| 83 | """Setup options to call cros-update command.""" |
| 84 | self.parser.add_argument('-d', action='store', type=str, |
| 85 | dest='host_name', |
| 86 | help='host_name of a DUT') |
| 87 | self.parser.add_argument('-b', action='store', type=str, |
| 88 | dest='build_name', |
| 89 | help='build name to be auto-updated') |
| 90 | self.parser.add_argument('--static_dir', action='store', type=str, |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 91 | help='static directory of the devserver') |
| 92 | self.parser.add_argument('--force_update', action='store_true', |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 93 | default=False, |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 94 | help=('force an update even if the version ' |
| 95 | 'installed is the same')) |
| 96 | self.parser.add_argument('--full_update', action='store_true', |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 97 | default=False, |
| 98 | help='force a rootfs update, skip stateful update') |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 99 | self.parser.add_argument('--original_build', action='store', type=str, |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 100 | default='', |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 101 | help=('force stateful update with the same ' |
| 102 | 'version of previous rootfs partition')) |
David Haddock | 90e4944 | 2017-04-07 19:14:09 -0700 | [diff] [blame] | 103 | self.parser.add_argument('--payload_filename', action='store', type=str, |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 104 | default=None, help='A custom payload filename') |
David Haddock | 2055961 | 2017-06-28 22:15:08 -0700 | [diff] [blame] | 105 | self.parser.add_argument('--clobber_stateful', action='store_true', |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 106 | default=False, help='Whether to clobber stateful') |
| 107 | self.parser.add_argument('--quick_provision', action='store_true', |
| 108 | default=False, |
| 109 | help='Whether to attempt quick provisioning path') |
| 110 | self.parser.add_argument('--devserver_url', action='store', type=str, |
| 111 | default=None, help='Devserver URL base for RPCs') |
| 112 | self.parser.add_argument('--static_url', action='store', type=str, |
| 113 | default=None, |
| 114 | help='Devserver URL base for static files') |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 115 | |
| 116 | def ParseArgs(self): |
| 117 | """Parse and process command line arguments.""" |
| 118 | # Positional arguments from the end of the command line will be included |
| 119 | # in the list of unknown_args. |
| 120 | self.options, unknown_args = self.parser.parse_known_args() |
| 121 | # Filter out none-positional arguments |
| 122 | while unknown_args and unknown_args[0][0] == '-': |
| 123 | self.removed_args.append(unknown_args.pop(0)) |
| 124 | # Always assume the argument has a value. |
| 125 | if unknown_args: |
| 126 | self.removed_args.append(unknown_args.pop(0)) |
| 127 | if self.removed_args: |
| 128 | logging.warn('Unknown arguments are removed from the options: %s', |
| 129 | self.removed_args) |
| 130 | |
| 131 | |
| 132 | class CrOSUpdateTrigger(object): |
| 133 | """The class for CrOS auto-updater trigger. |
| 134 | |
| 135 | This class is used for running all CrOS auto-update trigger logic. |
| 136 | """ |
| 137 | def __init__(self, host_name, build_name, static_dir, progress_tracker=None, |
xixuan | 3bc974e | 2016-10-18 17:21:43 -0700 | [diff] [blame] | 138 | log_file=None, au_tempdir=None, force_update=False, |
David Haddock | 2055961 | 2017-06-28 22:15:08 -0700 | [diff] [blame] | 139 | full_update=False, original_build=None, payload_filename=None, |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 140 | clobber_stateful=True, quick_provision=False, |
| 141 | devserver_url=None, static_url=None): |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 142 | self.host_name = host_name |
| 143 | self.build_name = build_name |
| 144 | self.static_dir = static_dir |
| 145 | self.progress_tracker = progress_tracker |
| 146 | self.log_file = log_file |
xixuan | 3bc974e | 2016-10-18 17:21:43 -0700 | [diff] [blame] | 147 | self.au_tempdir = au_tempdir |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 148 | self.force_update = force_update |
| 149 | self.full_update = full_update |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 150 | self.original_build = original_build |
David Haddock | 90e4944 | 2017-04-07 19:14:09 -0700 | [diff] [blame] | 151 | self.payload_filename = payload_filename |
David Haddock | 2055961 | 2017-06-28 22:15:08 -0700 | [diff] [blame] | 152 | self.clobber_stateful = clobber_stateful |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 153 | self.quick_provision = quick_provision |
| 154 | self.devserver_url = devserver_url |
| 155 | self.static_url = static_url |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 156 | |
| 157 | def _WriteAUStatus(self, content): |
| 158 | if self.progress_tracker: |
| 159 | self.progress_tracker.WriteStatus(content) |
| 160 | |
| 161 | def _StatefulUpdate(self, cros_updater): |
| 162 | """The detailed process in stateful update. |
| 163 | |
| 164 | Args: |
| 165 | cros_updater: The CrOS auto updater for auto-update. |
| 166 | """ |
| 167 | self._WriteAUStatus('pre-setup stateful update') |
| 168 | cros_updater.PreSetupStatefulUpdate() |
| 169 | self._WriteAUStatus('perform stateful update') |
| 170 | cros_updater.UpdateStateful() |
| 171 | self._WriteAUStatus('post-check stateful update') |
| 172 | cros_updater.PostCheckStatefulUpdate() |
| 173 | |
| 174 | def _RootfsUpdate(self, cros_updater): |
| 175 | """The detailed process in rootfs update. |
| 176 | |
| 177 | Args: |
| 178 | cros_updater: The CrOS auto updater for auto-update. |
| 179 | """ |
xixuan | a4f4e71 | 2017-05-08 15:17:54 -0700 | [diff] [blame] | 180 | self._WriteAUStatus('Check whether devserver can run before rootfs update') |
| 181 | cros_updater.CheckDevserverRun() |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 182 | self._WriteAUStatus('transfer rootfs update package') |
| 183 | cros_updater.TransferRootfsUpdate() |
| 184 | self._WriteAUStatus('pre-setup rootfs update') |
| 185 | cros_updater.PreSetupRootfsUpdate() |
| 186 | self._WriteAUStatus('rootfs update') |
| 187 | cros_updater.UpdateRootfs() |
| 188 | self._WriteAUStatus('post-check rootfs update') |
| 189 | cros_updater.PostCheckRootfsUpdate() |
| 190 | |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 191 | def _GetOriginalPayloadDir(self): |
| 192 | """Get the directory of original payload. |
| 193 | |
| 194 | Returns: |
| 195 | The directory of original payload, whose format is like: |
| 196 | 'static/stable-channel/link/3428.210.0' |
| 197 | """ |
| 198 | if self.original_build: |
| 199 | return os.path.join(self.static_dir, '%s/%s' % (STABLE_BUILD_CHANNEL, |
| 200 | self.original_build)) |
| 201 | else: |
| 202 | return None |
| 203 | |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 204 | def _MakeStatusUrl(self, devserver_url, host_name, pid): |
| 205 | """Generates a URL to post auto update status to. |
| 206 | |
| 207 | Args: |
| 208 | devserver_url: URL base for devserver RPCs. |
| 209 | host_name: Host to post status for. |
| 210 | pid: pid of the update process. |
| 211 | |
| 212 | Returns: |
| 213 | An unescaped URL. |
| 214 | """ |
| 215 | return '%s/post_au_status?host_name=%s&pid=%d' % (devserver_url, host_name, |
| 216 | pid) |
| 217 | |
| 218 | def _QuickProvision(self, device): |
| 219 | """Performs a quick provision of device. |
| 220 | |
| 221 | Returns: |
| 222 | A CommandResult of the invocation. |
| 223 | """ |
| 224 | pid = os.getpid() |
| 225 | pgid = os.getpgid(pid) |
| 226 | if self.progress_tracker is None: |
| 227 | self.progress_tracker = cros_update_progress.AUProgress(self.host_name, |
| 228 | pgid) |
| 229 | |
| 230 | dut_script = '/tmp/quick-provision' |
| 231 | status_url = self._MakeStatusUrl(self.devserver_url, self.host_name, pgid) |
| 232 | cmd = ('curl -o %s %s && bash ' |
| 233 | '%s --status_url %s %s %s') % ( |
| 234 | dut_script, os.path.join(self.static_url, 'quick-provision'), |
| 235 | dut_script, cros_build_lib.ShellQuote(status_url), |
| 236 | self.build_name, self.static_url |
| 237 | ) |
| 238 | return device.RunCommand(cmd, log_output=True) |
| 239 | |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 240 | def TriggerAU(self): |
| 241 | """Execute auto update for cros_host. |
| 242 | |
| 243 | The auto update includes 4 steps: |
| 244 | 1. if devserver cannot run, restore the stateful partition. |
xixuan | 2aca0ac | 2016-07-29 12:02:06 -0700 | [diff] [blame] | 245 | 2. if possible, do stateful update first, but never raise errors, except |
| 246 | for timeout_util.TimeoutError caused by system.signal. |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 247 | 3. If required or stateful_update fails, first do rootfs update, then do |
| 248 | stateful_update. |
| 249 | 4. Post-check for the whole update. |
| 250 | """ |
| 251 | try: |
| 252 | with remote_access.ChromiumOSDeviceHandler( |
| 253 | self.host_name, port=None, |
| 254 | base_dir=CROS_PRESERVED_PATH, |
Luigi Semenzato | 8db8b3c | 2017-01-26 18:02:19 -0800 | [diff] [blame] | 255 | ping=False) as device: |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 256 | |
| 257 | logging.debug('Remote device %s is connected', self.host_name) |
| 258 | payload_dir = os.path.join(self.static_dir, self.build_name) |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 259 | original_payload_dir = self._GetOriginalPayloadDir() |
| 260 | |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 261 | chromeos_AU = auto_updater.ChromiumOSUpdater( |
xixuan | 2a0970a | 2016-08-10 12:12:44 -0700 | [diff] [blame] | 262 | device, self.build_name, payload_dir, |
| 263 | dev_dir=os.path.abspath(os.path.dirname(__file__)), |
xixuan | 3bc974e | 2016-10-18 17:21:43 -0700 | [diff] [blame] | 264 | tempdir=self.au_tempdir, |
xixuan | 2a0970a | 2016-08-10 12:12:44 -0700 | [diff] [blame] | 265 | log_file=self.log_file, |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 266 | original_payload_dir=original_payload_dir, |
David Haddock | 90e4944 | 2017-04-07 19:14:09 -0700 | [diff] [blame] | 267 | yes=True, |
David Haddock | 2055961 | 2017-06-28 22:15:08 -0700 | [diff] [blame] | 268 | payload_filename=self.payload_filename, |
| 269 | clobber_stateful=self.clobber_stateful) |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 270 | |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 271 | # Allow fall back if the quick provision does not succeed. |
| 272 | invoke_autoupdate = True |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 273 | |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 274 | if self.quick_provision: |
| 275 | try: |
| 276 | logging.debug('Start CrOS quick provision process...') |
| 277 | self._WriteAUStatus('Start Quick Provision') |
| 278 | self._QuickProvision(device) |
| 279 | logging.debug('Start CrOS check process...') |
| 280 | self._WriteAUStatus('Finish Quick Provision, post-check') |
| 281 | chromeos_AU.PostCheckCrOSUpdate() |
| 282 | self._WriteAUStatus(cros_update_progress.FINISHED) |
| 283 | invoke_autoupdate = False |
| 284 | except (cros_build_lib.RunCommandError, |
| 285 | remote_access.SSHConnectionError) as e: |
| 286 | logging.warning('Error during quick provision, falling back: %s', e) |
| 287 | time.sleep(QUICK_PROVISION_FAILURE_DELAY_SEC) |
| 288 | |
| 289 | if invoke_autoupdate: |
| 290 | chromeos_AU.CheckPayloads() |
| 291 | |
| 292 | version_match = chromeos_AU.PreSetupCrOSUpdate() |
| 293 | self._WriteAUStatus('Transfer Devserver/Stateful Update Package') |
| 294 | chromeos_AU.TransferDevServerPackage() |
| 295 | chromeos_AU.TransferStatefulUpdate() |
| 296 | |
| 297 | restore_stateful = chromeos_AU.CheckRestoreStateful() |
| 298 | do_stateful_update = (not self.full_update) and ( |
| 299 | version_match and self.force_update) |
| 300 | stateful_update_complete = False |
| 301 | logging.debug('Start CrOS update process...') |
| 302 | try: |
| 303 | if restore_stateful: |
| 304 | self._WriteAUStatus('Restore Stateful Partition') |
| 305 | chromeos_AU.RestoreStateful() |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 306 | stateful_update_complete = True |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 307 | else: |
| 308 | # Whether to execute stateful update depends on: |
| 309 | # a. full_update=False: No full reimage is required. |
| 310 | # b. The update version is matched to the current version, And |
| 311 | # force_update=True: Update is forced even if the version |
| 312 | # installed is the same. |
| 313 | if do_stateful_update: |
| 314 | self._StatefulUpdate(chromeos_AU) |
| 315 | stateful_update_complete = True |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 316 | |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 317 | except timeout_util.TimeoutError: |
| 318 | raise |
| 319 | except Exception as e: |
| 320 | logging.debug('Error happens in stateful update: %r', e) |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 321 | |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 322 | # Whether to execute rootfs update depends on: |
| 323 | # a. stateful update is not completed, or completed by |
| 324 | # update action 'restore_stateful'. |
| 325 | # b. force_update=True: Update is forced no matter what the current |
| 326 | # version is. Or, the update version is not matched to the current |
| 327 | # version. |
| 328 | require_rootfs_update = self.force_update or ( |
| 329 | not chromeos_AU.CheckVersion()) |
| 330 | if (not (do_stateful_update and stateful_update_complete) |
| 331 | and require_rootfs_update): |
| 332 | self._RootfsUpdate(chromeos_AU) |
| 333 | self._StatefulUpdate(chromeos_AU) |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 334 | |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 335 | self._WriteAUStatus('post-check for CrOS auto-update') |
| 336 | chromeos_AU.PostCheckCrOSUpdate() |
| 337 | self._WriteAUStatus(cros_update_progress.FINISHED) |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 338 | except Exception as e: |
| 339 | logging.debug('Error happens in CrOS auto-update: %r', e) |
xixuan | 28d9907 | 2016-10-06 12:24:16 -0700 | [diff] [blame] | 340 | self._WriteAUStatus(CROS_ERROR_TEMPLATE % str(traceback.format_exc())) |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 341 | raise |
| 342 | |
| 343 | |
| 344 | def main(): |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 345 | # Create one cros_update_parser instance for parsing CrOS auto-update cmd. |
| 346 | AU_parser = CrOSAUParser() |
| 347 | try: |
| 348 | AU_parser.ParseArgs() |
| 349 | except Exception as e: |
| 350 | logging.error('Error in Parsing Args: %r', e) |
| 351 | raise |
| 352 | |
| 353 | if len(sys.argv) == 1: |
| 354 | AU_parser.parser.print_help() |
| 355 | sys.exit(1) |
| 356 | |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 357 | options = AU_parser.options |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 358 | |
xixuan | 2a0970a | 2016-08-10 12:12:44 -0700 | [diff] [blame] | 359 | # Use process group id as the unique id in track and log files, since |
| 360 | # os.setsid is executed before the current process is run. |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 361 | pid = os.getpid() |
xixuan | 2a0970a | 2016-08-10 12:12:44 -0700 | [diff] [blame] | 362 | pgid = os.getpgid(pid) |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 363 | |
| 364 | # Setting log files for CrOS auto-update process. |
| 365 | # Log file: file to record every details of CrOS auto-update process. |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 366 | log_file = cros_update_progress.GetExecuteLogFile(options.host_name, pgid) |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 367 | logging.info('Writing executing logs into file: %s', log_file) |
| 368 | logConfig.SetFileHandler(log_file) |
| 369 | |
| 370 | # Create a progress_tracker for tracking CrOS auto-update progress. |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 371 | progress_tracker = cros_update_progress.AUProgress(options.host_name, pgid) |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 372 | |
xixuan | 3bc974e | 2016-10-18 17:21:43 -0700 | [diff] [blame] | 373 | # Create a dir for temporarily storing devserver codes and logs. |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 374 | au_tempdir = cros_update_progress.GetAUTempDirectory(options.host_name, pgid) |
xixuan | 3bc974e | 2016-10-18 17:21:43 -0700 | [diff] [blame] | 375 | |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 376 | # Create cros_update instance to run CrOS auto-update. |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 377 | cros_updater_trigger = CrOSUpdateTrigger( |
| 378 | options.host_name, options.build_name, options.static_dir, |
| 379 | progress_tracker=progress_tracker, |
| 380 | log_file=log_file, |
| 381 | au_tempdir=au_tempdir, |
| 382 | force_update=options.force_update, |
| 383 | full_update=options.full_update, |
David Haddock | 90e4944 | 2017-04-07 19:14:09 -0700 | [diff] [blame] | 384 | original_build=options.original_build, |
David Haddock | 2055961 | 2017-06-28 22:15:08 -0700 | [diff] [blame] | 385 | payload_filename=options.payload_filename, |
David Riley | 6382067 | 2017-11-02 10:46:42 -0700 | [diff] [blame] | 386 | clobber_stateful=options.clobber_stateful, |
| 387 | quick_provision=options.quick_provision, |
| 388 | devserver_url=options.devserver_url, |
| 389 | static_url=options.static_url) |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 390 | |
| 391 | # Set timeout the cros-update process. |
| 392 | try: |
| 393 | with timeout_util.Timeout(CROS_UPDATE_TIMEOUT_MIN*60): |
| 394 | cros_updater_trigger.TriggerAU() |
| 395 | except timeout_util.TimeoutError as e: |
| 396 | error_msg = ('%s. The CrOS auto-update process is timed out, thus will be ' |
| 397 | 'terminated' % str(e)) |
| 398 | progress_tracker.WriteStatus(CROS_ERROR_TEMPLATE % error_msg) |
| 399 | |
| 400 | |
| 401 | if __name__ == '__main__': |
| 402 | main() |