blob: c4728806bbcfa228735e426c00b75faffbf32696 [file] [log] [blame]
xixuan52c2fba2016-05-20 17:02:48 -07001# Copyright (c) 2016 The Chromium OS Authors. All rights reserved.
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
7The reason to create this file is to let devserver to trigger a background
8process for CrOS auto-update. Therefore, when devserver service is restarted
9sometimes, the CrOS auto-update process is still running and the corresponding
10provision task won't claim failure.
11
12It 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
24from __future__ import print_function
25
26import argparse
27import cros_update_logging
28import cros_update_progress
29import logging
30import os
31import sys
xixuan28d99072016-10-06 12:24:16 -070032import traceback
xixuan52c2fba2016-05-20 17:02:48 -070033
xixuancf58dd32016-08-24 13:57:06 -070034# only import setup_chromite before chromite import.
35import setup_chromite # pylint: disable=unused-import
xixuan52c2fba2016-05-20 17:02:48 -070036try:
37 from chromite.lib import auto_updater
38 from chromite.lib import remote_access
39 from chromite.lib import timeout_util
40except ImportError as e:
41 logging.debug('chromite cannot be imported: %r', e)
42 auto_updater = None
43 remote_access = None
xixuancf58dd32016-08-24 13:57:06 -070044 timeout_util = None
xixuan52c2fba2016-05-20 17:02:48 -070045
46# Timeout for CrOS auto-update process.
47CROS_UPDATE_TIMEOUT_MIN = 30
48
49# The preserved path in remote device, won't be deleted after rebooting.
50CROS_PRESERVED_PATH = ('/mnt/stateful_partition/unencrypted/'
51 'preserve/cros-update')
52
53# Standard error tmeplate to be written into status tracking log.
xixuan28d99072016-10-06 12:24:16 -070054CROS_ERROR_TEMPLATE = cros_update_progress.ERROR_TAG + ' %s'
xixuan52c2fba2016-05-20 17:02:48 -070055
56
57class CrOSAUParser(object):
58 """Custom command-line options parser for cros-update."""
59 def __init__(self):
60 self.args = sys.argv[1:]
61 self.parser = argparse.ArgumentParser(
62 usage='%(prog)s [options] [control-file]')
63 self.SetupOptions()
64 self.removed_args = []
65
66 # parse an empty list of arguments in order to set self.options
67 # to default values.
68 self.options = self.parser.parse_args(args=[])
69
70 def SetupOptions(self):
71 """Setup options to call cros-update command."""
72 self.parser.add_argument('-d', action='store', type=str,
73 dest='host_name',
74 help='host_name of a DUT')
75 self.parser.add_argument('-b', action='store', type=str,
76 dest='build_name',
77 help='build name to be auto-updated')
78 self.parser.add_argument('--static_dir', action='store', type=str,
79 dest='static_dir',
80 help='static directory of the devserver')
81 self.parser.add_argument('--force_update', action='store_true',
82 dest='force_update', default=False,
83 help=('force an update even if the version '
84 'installed is the same'))
85 self.parser.add_argument('--full_update', action='store_true',
86 dest='full_update', default=False,
87 help=('force a rootfs update, skip stateful '
88 'update'))
89
90 def ParseArgs(self):
91 """Parse and process command line arguments."""
92 # Positional arguments from the end of the command line will be included
93 # in the list of unknown_args.
94 self.options, unknown_args = self.parser.parse_known_args()
95 # Filter out none-positional arguments
96 while unknown_args and unknown_args[0][0] == '-':
97 self.removed_args.append(unknown_args.pop(0))
98 # Always assume the argument has a value.
99 if unknown_args:
100 self.removed_args.append(unknown_args.pop(0))
101 if self.removed_args:
102 logging.warn('Unknown arguments are removed from the options: %s',
103 self.removed_args)
104
105
106class CrOSUpdateTrigger(object):
107 """The class for CrOS auto-updater trigger.
108
109 This class is used for running all CrOS auto-update trigger logic.
110 """
111 def __init__(self, host_name, build_name, static_dir, progress_tracker=None,
xixuan3bc974e2016-10-18 17:21:43 -0700112 log_file=None, au_tempdir=None, force_update=False,
113 full_update=False):
xixuan52c2fba2016-05-20 17:02:48 -0700114 self.host_name = host_name
115 self.build_name = build_name
116 self.static_dir = static_dir
117 self.progress_tracker = progress_tracker
118 self.log_file = log_file
xixuan3bc974e2016-10-18 17:21:43 -0700119 self.au_tempdir = au_tempdir
xixuan52c2fba2016-05-20 17:02:48 -0700120 self.force_update = force_update
121 self.full_update = full_update
122
123 def _WriteAUStatus(self, content):
124 if self.progress_tracker:
125 self.progress_tracker.WriteStatus(content)
126
127 def _StatefulUpdate(self, cros_updater):
128 """The detailed process in stateful update.
129
130 Args:
131 cros_updater: The CrOS auto updater for auto-update.
132 """
133 self._WriteAUStatus('pre-setup stateful update')
134 cros_updater.PreSetupStatefulUpdate()
135 self._WriteAUStatus('perform stateful update')
136 cros_updater.UpdateStateful()
137 self._WriteAUStatus('post-check stateful update')
138 cros_updater.PostCheckStatefulUpdate()
139
140 def _RootfsUpdate(self, cros_updater):
141 """The detailed process in rootfs update.
142
143 Args:
144 cros_updater: The CrOS auto updater for auto-update.
145 """
146 self._WriteAUStatus('transfer rootfs update package')
147 cros_updater.TransferRootfsUpdate()
148 self._WriteAUStatus('pre-setup rootfs update')
149 cros_updater.PreSetupRootfsUpdate()
150 self._WriteAUStatus('rootfs update')
151 cros_updater.UpdateRootfs()
152 self._WriteAUStatus('post-check rootfs update')
153 cros_updater.PostCheckRootfsUpdate()
154
155 def TriggerAU(self):
156 """Execute auto update for cros_host.
157
158 The auto update includes 4 steps:
159 1. if devserver cannot run, restore the stateful partition.
xixuan2aca0ac2016-07-29 12:02:06 -0700160 2. if possible, do stateful update first, but never raise errors, except
161 for timeout_util.TimeoutError caused by system.signal.
xixuan52c2fba2016-05-20 17:02:48 -0700162 3. If required or stateful_update fails, first do rootfs update, then do
163 stateful_update.
164 4. Post-check for the whole update.
165 """
166 try:
167 with remote_access.ChromiumOSDeviceHandler(
168 self.host_name, port=None,
169 base_dir=CROS_PRESERVED_PATH,
170 ping=True) as device:
171
172 logging.debug('Remote device %s is connected', self.host_name)
173 payload_dir = os.path.join(self.static_dir, self.build_name)
174 chromeos_AU = auto_updater.ChromiumOSUpdater(
xixuan2a0970a2016-08-10 12:12:44 -0700175 device, self.build_name, payload_dir,
176 dev_dir=os.path.abspath(os.path.dirname(__file__)),
xixuan3bc974e2016-10-18 17:21:43 -0700177 tempdir=self.au_tempdir,
xixuan2a0970a2016-08-10 12:12:44 -0700178 log_file=self.log_file,
xixuan52c2fba2016-05-20 17:02:48 -0700179 yes=True)
180 chromeos_AU.CheckPayloads()
181
xixuanfdfd1e32016-10-06 14:55:17 -0700182 version_match = chromeos_AU.PreSetupCrOSUpdate()
xixuan52c2fba2016-05-20 17:02:48 -0700183 self._WriteAUStatus('Transfer Devserver/Stateful Update Package')
184 chromeos_AU.TransferDevServerPackage()
185 chromeos_AU.TransferStatefulUpdate()
186
187 restore_stateful = chromeos_AU.CheckRestoreStateful()
188 do_stateful_update = (not self.full_update) and (
xixuanfdfd1e32016-10-06 14:55:17 -0700189 version_match and self.force_update)
xixuan52c2fba2016-05-20 17:02:48 -0700190 stateful_update_complete = False
191 logging.debug('Start CrOS update process...')
192 try:
193 if restore_stateful:
194 self._WriteAUStatus('Restore Stateful Partition')
195 chromeos_AU.RestoreStateful()
196 stateful_update_complete = True
197 else:
198 # Whether to execute stateful update depends on:
199 # a. full_update=False: No full reimage is required.
200 # b. The update version is matched to the current version, And
201 # force_update=True: Update is forced even if the version
202 # installed is the same.
203 if do_stateful_update:
204 self._StatefulUpdate(chromeos_AU)
205 stateful_update_complete = True
206
xixuan2aca0ac2016-07-29 12:02:06 -0700207 except timeout_util.TimeoutError:
208 raise
xixuan52c2fba2016-05-20 17:02:48 -0700209 except Exception as e:
210 logging.debug('Error happens in stateful update: %r', e)
211
212 # Whether to execute rootfs update depends on:
213 # a. stateful update is not completed, or completed by
214 # update action 'restore_stateful'.
215 # b. force_update=True: Update is forced no matter what the current
216 # version is. Or, the update version is not matched to the current
217 # version.
218 require_rootfs_update = self.force_update or (
219 not chromeos_AU.CheckVersion())
220 if (not (do_stateful_update and stateful_update_complete)
221 and require_rootfs_update):
222 self._RootfsUpdate(chromeos_AU)
223 self._StatefulUpdate(chromeos_AU)
224
225 self._WriteAUStatus('post-check for CrOS auto-update')
226 chromeos_AU.PostCheckCrOSUpdate()
227 self._WriteAUStatus(cros_update_progress.FINISHED)
228 except Exception as e:
229 logging.debug('Error happens in CrOS auto-update: %r', e)
xixuan28d99072016-10-06 12:24:16 -0700230 self._WriteAUStatus(CROS_ERROR_TEMPLATE % str(traceback.format_exc()))
xixuan52c2fba2016-05-20 17:02:48 -0700231 raise
232
233
234def main():
235 # Setting logging level
236 logConfig = cros_update_logging.loggingConfig()
237 logConfig.ConfigureLogging()
238
239 # Create one cros_update_parser instance for parsing CrOS auto-update cmd.
240 AU_parser = CrOSAUParser()
241 try:
242 AU_parser.ParseArgs()
243 except Exception as e:
244 logging.error('Error in Parsing Args: %r', e)
245 raise
246
247 if len(sys.argv) == 1:
248 AU_parser.parser.print_help()
249 sys.exit(1)
250
251 host_name = AU_parser.options.host_name
252 build_name = AU_parser.options.build_name
253 static_dir = AU_parser.options.static_dir
254 force_update = AU_parser.options.force_update
255 full_update = AU_parser.options.full_update
256
xixuan2a0970a2016-08-10 12:12:44 -0700257 # Use process group id as the unique id in track and log files, since
258 # os.setsid is executed before the current process is run.
xixuan52c2fba2016-05-20 17:02:48 -0700259 pid = os.getpid()
xixuan2a0970a2016-08-10 12:12:44 -0700260 pgid = os.getpgid(pid)
xixuan52c2fba2016-05-20 17:02:48 -0700261
262 # Setting log files for CrOS auto-update process.
263 # Log file: file to record every details of CrOS auto-update process.
xixuan2a0970a2016-08-10 12:12:44 -0700264 log_file = cros_update_progress.GetExecuteLogFile(host_name, pgid)
xixuan52c2fba2016-05-20 17:02:48 -0700265 logging.info('Writing executing logs into file: %s', log_file)
266 logConfig.SetFileHandler(log_file)
267
268 # Create a progress_tracker for tracking CrOS auto-update progress.
xixuan2a0970a2016-08-10 12:12:44 -0700269 progress_tracker = cros_update_progress.AUProgress(host_name, pgid)
xixuan52c2fba2016-05-20 17:02:48 -0700270
xixuan3bc974e2016-10-18 17:21:43 -0700271 # Create a dir for temporarily storing devserver codes and logs.
272 au_tempdir = cros_update_progress.GetAUTempDirectory(host_name, pgid)
273
xixuan52c2fba2016-05-20 17:02:48 -0700274 # Create cros_update instance to run CrOS auto-update.
275 cros_updater_trigger = CrOSUpdateTrigger(host_name, build_name, static_dir,
276 progress_tracker=progress_tracker,
277 log_file=log_file,
xixuan3bc974e2016-10-18 17:21:43 -0700278 au_tempdir=au_tempdir,
xixuan52c2fba2016-05-20 17:02:48 -0700279 force_update=force_update,
280 full_update=full_update)
281
282 # Set timeout the cros-update process.
283 try:
284 with timeout_util.Timeout(CROS_UPDATE_TIMEOUT_MIN*60):
285 cros_updater_trigger.TriggerAU()
286 except timeout_util.TimeoutError as e:
287 error_msg = ('%s. The CrOS auto-update process is timed out, thus will be '
288 'terminated' % str(e))
289 progress_tracker.WriteStatus(CROS_ERROR_TEMPLATE % error_msg)
290
291
292if __name__ == '__main__':
293 main()