blob: 5920cbea2cd9fef7069634dfbbcd78c356acd61c [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
32
33try:
34 from chromite.lib import auto_updater
35 from chromite.lib import remote_access
36 from chromite.lib import timeout_util
37except ImportError as e:
38 logging.debug('chromite cannot be imported: %r', e)
39 auto_updater = None
40 remote_access = None
41
42# Timeout for CrOS auto-update process.
43CROS_UPDATE_TIMEOUT_MIN = 30
44
45# The preserved path in remote device, won't be deleted after rebooting.
46CROS_PRESERVED_PATH = ('/mnt/stateful_partition/unencrypted/'
47 'preserve/cros-update')
48
49# Standard error tmeplate to be written into status tracking log.
50CROS_ERROR_TEMPLATE = cros_update_progress.ERROR_TAG + ' %r'
51
52
53class CrOSAUParser(object):
54 """Custom command-line options parser for cros-update."""
55 def __init__(self):
56 self.args = sys.argv[1:]
57 self.parser = argparse.ArgumentParser(
58 usage='%(prog)s [options] [control-file]')
59 self.SetupOptions()
60 self.removed_args = []
61
62 # parse an empty list of arguments in order to set self.options
63 # to default values.
64 self.options = self.parser.parse_args(args=[])
65
66 def SetupOptions(self):
67 """Setup options to call cros-update command."""
68 self.parser.add_argument('-d', action='store', type=str,
69 dest='host_name',
70 help='host_name of a DUT')
71 self.parser.add_argument('-b', action='store', type=str,
72 dest='build_name',
73 help='build name to be auto-updated')
74 self.parser.add_argument('--static_dir', action='store', type=str,
75 dest='static_dir',
76 help='static directory of the devserver')
77 self.parser.add_argument('--force_update', action='store_true',
78 dest='force_update', default=False,
79 help=('force an update even if the version '
80 'installed is the same'))
81 self.parser.add_argument('--full_update', action='store_true',
82 dest='full_update', default=False,
83 help=('force a rootfs update, skip stateful '
84 'update'))
85
86 def ParseArgs(self):
87 """Parse and process command line arguments."""
88 # Positional arguments from the end of the command line will be included
89 # in the list of unknown_args.
90 self.options, unknown_args = self.parser.parse_known_args()
91 # Filter out none-positional arguments
92 while unknown_args and unknown_args[0][0] == '-':
93 self.removed_args.append(unknown_args.pop(0))
94 # Always assume the argument has a value.
95 if unknown_args:
96 self.removed_args.append(unknown_args.pop(0))
97 if self.removed_args:
98 logging.warn('Unknown arguments are removed from the options: %s',
99 self.removed_args)
100
101
102class CrOSUpdateTrigger(object):
103 """The class for CrOS auto-updater trigger.
104
105 This class is used for running all CrOS auto-update trigger logic.
106 """
107 def __init__(self, host_name, build_name, static_dir, progress_tracker=None,
108 log_file=None, force_update=False, full_update=False):
109 self.host_name = host_name
110 self.build_name = build_name
111 self.static_dir = static_dir
112 self.progress_tracker = progress_tracker
113 self.log_file = log_file
114 self.force_update = force_update
115 self.full_update = full_update
116
117 def _WriteAUStatus(self, content):
118 if self.progress_tracker:
119 self.progress_tracker.WriteStatus(content)
120
121 def _StatefulUpdate(self, cros_updater):
122 """The detailed process in stateful update.
123
124 Args:
125 cros_updater: The CrOS auto updater for auto-update.
126 """
127 self._WriteAUStatus('pre-setup stateful update')
128 cros_updater.PreSetupStatefulUpdate()
129 self._WriteAUStatus('perform stateful update')
130 cros_updater.UpdateStateful()
131 self._WriteAUStatus('post-check stateful update')
132 cros_updater.PostCheckStatefulUpdate()
133
134 def _RootfsUpdate(self, cros_updater):
135 """The detailed process in rootfs update.
136
137 Args:
138 cros_updater: The CrOS auto updater for auto-update.
139 """
140 self._WriteAUStatus('transfer rootfs update package')
141 cros_updater.TransferRootfsUpdate()
142 self._WriteAUStatus('pre-setup rootfs update')
143 cros_updater.PreSetupRootfsUpdate()
144 self._WriteAUStatus('rootfs update')
145 cros_updater.UpdateRootfs()
146 self._WriteAUStatus('post-check rootfs update')
147 cros_updater.PostCheckRootfsUpdate()
148
149 def TriggerAU(self):
150 """Execute auto update for cros_host.
151
152 The auto update includes 4 steps:
153 1. if devserver cannot run, restore the stateful partition.
xixuan2aca0ac2016-07-29 12:02:06 -0700154 2. if possible, do stateful update first, but never raise errors, except
155 for timeout_util.TimeoutError caused by system.signal.
xixuan52c2fba2016-05-20 17:02:48 -0700156 3. If required or stateful_update fails, first do rootfs update, then do
157 stateful_update.
158 4. Post-check for the whole update.
159 """
160 try:
161 with remote_access.ChromiumOSDeviceHandler(
162 self.host_name, port=None,
163 base_dir=CROS_PRESERVED_PATH,
164 ping=True) as device:
165
166 logging.debug('Remote device %s is connected', self.host_name)
167 payload_dir = os.path.join(self.static_dir, self.build_name)
168 chromeos_AU = auto_updater.ChromiumOSUpdater(
169 device, self.build_name, payload_dir, log_file=self.log_file,
170 yes=True)
171 chromeos_AU.CheckPayloads()
172
173 self._WriteAUStatus('Transfer Devserver/Stateful Update Package')
174 chromeos_AU.TransferDevServerPackage()
175 chromeos_AU.TransferStatefulUpdate()
176
177 restore_stateful = chromeos_AU.CheckRestoreStateful()
178 do_stateful_update = (not self.full_update) and (
179 chromeos_AU.PreSetupCrOSUpdate() and self.force_update)
180 stateful_update_complete = False
181 logging.debug('Start CrOS update process...')
182 try:
183 if restore_stateful:
184 self._WriteAUStatus('Restore Stateful Partition')
185 chromeos_AU.RestoreStateful()
186 stateful_update_complete = True
187 else:
188 # Whether to execute stateful update depends on:
189 # a. full_update=False: No full reimage is required.
190 # b. The update version is matched to the current version, And
191 # force_update=True: Update is forced even if the version
192 # installed is the same.
193 if do_stateful_update:
194 self._StatefulUpdate(chromeos_AU)
195 stateful_update_complete = True
196
xixuan2aca0ac2016-07-29 12:02:06 -0700197 except timeout_util.TimeoutError:
198 raise
xixuan52c2fba2016-05-20 17:02:48 -0700199 except Exception as e:
200 logging.debug('Error happens in stateful update: %r', e)
201
202 # Whether to execute rootfs update depends on:
203 # a. stateful update is not completed, or completed by
204 # update action 'restore_stateful'.
205 # b. force_update=True: Update is forced no matter what the current
206 # version is. Or, the update version is not matched to the current
207 # version.
208 require_rootfs_update = self.force_update or (
209 not chromeos_AU.CheckVersion())
210 if (not (do_stateful_update and stateful_update_complete)
211 and require_rootfs_update):
212 self._RootfsUpdate(chromeos_AU)
213 self._StatefulUpdate(chromeos_AU)
214
215 self._WriteAUStatus('post-check for CrOS auto-update')
216 chromeos_AU.PostCheckCrOSUpdate()
217 self._WriteAUStatus(cros_update_progress.FINISHED)
218 except Exception as e:
219 logging.debug('Error happens in CrOS auto-update: %r', e)
220 self._WriteAUStatus(CROS_ERROR_TEMPLATE % e)
221 raise
222
223
224def main():
225 # Setting logging level
226 logConfig = cros_update_logging.loggingConfig()
227 logConfig.ConfigureLogging()
228
229 # Create one cros_update_parser instance for parsing CrOS auto-update cmd.
230 AU_parser = CrOSAUParser()
231 try:
232 AU_parser.ParseArgs()
233 except Exception as e:
234 logging.error('Error in Parsing Args: %r', e)
235 raise
236
237 if len(sys.argv) == 1:
238 AU_parser.parser.print_help()
239 sys.exit(1)
240
241 host_name = AU_parser.options.host_name
242 build_name = AU_parser.options.build_name
243 static_dir = AU_parser.options.static_dir
244 force_update = AU_parser.options.force_update
245 full_update = AU_parser.options.full_update
246
247 # Reset process group id to make current process running on the background.
248 pid = os.getpid()
249 os.setsid()
250
251 # Setting log files for CrOS auto-update process.
252 # Log file: file to record every details of CrOS auto-update process.
253 log_file = cros_update_progress.GetExecuteLogFile(host_name, pid)
254 logging.info('Writing executing logs into file: %s', log_file)
255 logConfig.SetFileHandler(log_file)
256
257 # Create a progress_tracker for tracking CrOS auto-update progress.
258 progress_tracker = cros_update_progress.AUProgress(host_name, pid)
259
260 # Create cros_update instance to run CrOS auto-update.
261 cros_updater_trigger = CrOSUpdateTrigger(host_name, build_name, static_dir,
262 progress_tracker=progress_tracker,
263 log_file=log_file,
264 force_update=force_update,
265 full_update=full_update)
266
267 # Set timeout the cros-update process.
268 try:
269 with timeout_util.Timeout(CROS_UPDATE_TIMEOUT_MIN*60):
270 cros_updater_trigger.TriggerAU()
271 except timeout_util.TimeoutError as e:
272 error_msg = ('%s. The CrOS auto-update process is timed out, thus will be '
273 'terminated' % str(e))
274 progress_tracker.WriteStatus(CROS_ERROR_TEMPLATE % error_msg)
275
276
277if __name__ == '__main__':
278 main()