blob: 0798ba4ec2fa2e8a23db5b1fc123493ba0e2c4f5 [file] [log] [blame]
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +08001#!/usr/bin/python -Bu
2#
3# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Factory toolkit installer.
8
9The factory toolkit is a self-extracting shellball containing factory test
10related files and this installer. This installer is invoked when the toolkit
11is deployed and is responsible for installing files.
12"""
13
14
15import argparse
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +080016from contextlib import contextmanager
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080017import os
Wei-Ning Huang4855e792015-06-11 15:33:39 +080018import shutil
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080019import sys
Jon Salz4f3ade52014-02-20 17:55:09 +080020import tempfile
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080021
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +080022import factory_common # pylint: disable=W0611
Wei-Ning Huang5135b7e2015-07-03 17:31:15 +080023from cros.factory.test import event_log
Wei-Han Chen2ebb92d2016-01-12 14:51:41 +080024from cros.factory.test.env import paths
Jon Salz25590302014-07-11 16:07:20 +080025from cros.factory.tools import install_symlinks
Rong Chang6d65fad2014-08-22 16:00:12 +080026from cros.factory.utils import file_utils
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +080027from cros.factory.utils import sys_utils
Wei-Han Chen2ebb92d2016-01-12 14:51:41 +080028from cros.factory.utils.process_utils import Spawn
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080029
30
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +080031INSTALLER_PATH = 'usr/local/factory/py/toolkit/installer.py'
Rong Chang6d65fad2014-08-22 16:00:12 +080032MAKESELF_SHELL = '/bin/sh'
33TOOLKIT_NAME = 'install_factory_toolkit.run'
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +080034
Jon Salz4f3ade52014-02-20 17:55:09 +080035# Short and sweet help header for the executable generated by makeself.
36HELP_HEADER = """
37Installs the factory toolkit, transforming a test image into a factory test
38image. You can:
39
40- Install the factory toolkit on a CrOS device that is running a test
41 image. To do this, copy install_factory_toolkit.run to the device and
42 run it. The factory tests will then come up on the next boot.
43
44 rsync -a install_factory_toolkit.run crosdevice:/tmp
45 ssh crosdevice '/tmp/install_factory_toolkit.run && sync && reboot'
46
47- Modify a test image, turning it into a factory test image. When you
48 use the image on a device, the factory tests will come up.
49
50 install_factory_toolkit.run chromiumos_test_image.bin
51"""
52
53HELP_HEADER_ADVANCED = """
54- (advanced) Modify a mounted stateful partition, turning it into a factory
55 test image. This is equivalent to the previous command:
56
57 mount_partition -rw chromiumos_test_image.bin 1 /mnt/stateful
58 install_factory_toolkit.run /mnt/stateful
59 umount /mnt/stateful
60
61- (advanced) Unpack the factory toolkit, modify a file, and then repack it.
62
63 # Unpack but don't actually install
64 install_factory_toolkit.run --target /tmp/toolkit --noexec
65 # Edit some files in /tmp/toolkit
66 emacs /tmp/toolkit/whatever
67 # Repack
68 install_factory_toolkit.run -- --repack /tmp/toolkit \\
69 --pack-into /path/to/new/install_factory_toolkit.run
70"""
71
72# The makeself-generated header comes next. This is a little confusing,
73# so explain.
74HELP_HEADER_MAKESELF = """
75For complete usage information and advanced operations, run
76"install_factory_toolkit.run -- --help" (note the extra "--").
77
78Following is the help message from makeself, which was used to create
79this self-extracting archive.
80
81-----
82"""
83
Vic Yangdb1e20e2014-10-05 12:10:33 +080084SERVER_FILE_MASK = [
85 # Exclude Umpire server but keep Umpire client
86 '--include', 'py/umpire/__init__.*',
87 '--include', 'py/umpire/common.*',
88 '--include', 'py/umpire/client',
89 '--include', 'py/umpire/client/**',
90 '--exclude', 'py/umpire/**',
91
92 # Lumberjack is only used on Umpire server
93 '--exclude', 'py/lumberjack'
94]
95
Jon Salz4f3ade52014-02-20 17:55:09 +080096
Hung-Te Lin7b596ff2015-01-16 20:19:15 +080097class FactoryToolkitInstaller(object):
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080098 """Factory toolkit installer.
99
100 Args:
101 src: Source path containing usr/ and var/.
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800102 dest: Installation destination path. Set this to the mount point of the
103 stateful partition if patching a test image.
104 no_enable: True to not install the tag file.
105 system_root: The path to the root of the file system. This must be left
106 as its default value except for unit testing.
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800107 """
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800108
Jon Salzb7e44262014-05-07 15:53:37 +0800109 # Whether to sudo when rsyncing; set to False for testing.
110 _sudo = True
111
Peter Ammon948b7172014-07-15 12:43:06 -0700112 def __init__(self, src, dest, no_enable, enable_presenter,
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800113 enable_device, non_cros=False, device_id=None, system_root='/',
114 apps=None):
Jon Salz4f3ade52014-02-20 17:55:09 +0800115 self._src = src
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800116 self._system_root = system_root
117 if dest == self._system_root:
118 self._usr_local_dest = os.path.join(dest, 'usr', 'local')
119 self._var_dest = os.path.join(dest, 'var')
Jon Salz4f3ade52014-02-20 17:55:09 +0800120
121 # Make sure we're on a CrOS device.
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +0800122 if not non_cros and not sys_utils.InCrOSDevice():
Jon Salz4f3ade52014-02-20 17:55:09 +0800123 sys.stderr.write(
Vic Yang196d5242014-08-05 13:51:35 +0800124 "ERROR: You're not on a CrOS device (for more details, please\n"
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +0800125 'check sys_utils.py:InCrOSDevice), so you must specify a test\n'
Hung-Te Lin56b18402015-01-16 14:52:30 +0800126 'image or a mounted stateful partition on which to install the\n'
127 'factory toolkit. Please run\n'
128 '\n'
129 ' install_factory_toolkit.run -- --help\n'
130 '\n'
Vic Yang70fdae92015-02-17 19:21:08 -0800131 'for help.\n'
132 '\n'
133 'If you want to install the presenter on a non-CrOS host,\n'
134 'please run\n'
135 '\n'
136 ' install_factory_toolkit.run -- \\\n'
Hung-Te Lin0cba9d52016-03-22 11:45:35 +0800137 ' --non-cros --enable-presenter\n'
Vic Yang70fdae92015-02-17 19:21:08 -0800138 '\n')
Jon Salz4f3ade52014-02-20 17:55:09 +0800139 sys.exit(1)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800140 if os.getuid() != 0:
Jon Salz4f3ade52014-02-20 17:55:09 +0800141 raise Exception('You must be root to install the factory toolkit on a '
142 'CrOS device.')
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800143 else:
144 self._usr_local_dest = os.path.join(dest, 'dev_image')
145 self._var_dest = os.path.join(dest, 'var_overlay')
146 if (not os.path.exists(self._usr_local_dest) or
147 not os.path.exists(self._var_dest)):
148 raise Exception(
149 'The destination path %s is not a stateful partition!' % dest)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800150
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800151 self._dest = dest
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800152 self._usr_local_src = os.path.join(src, 'usr', 'local')
153 self._var_src = os.path.join(src, 'var')
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800154 self._no_enable = no_enable
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800155 self._tag_file = os.path.join(self._usr_local_dest, 'factory', 'enabled')
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800156
Peter Ammon948b7172014-07-15 12:43:06 -0700157 self._enable_presenter = enable_presenter
158 self._presenter_tag_file = os.path.join(self._usr_local_dest, 'factory',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800159 'init', 'run_goofy_presenter')
Vic Yang7039f422014-07-07 15:38:13 -0700160
161 self._enable_device = enable_device
Ricky Liangd7716912014-07-10 11:52:24 +0800162 self._device_tag_file = os.path.join(self._usr_local_dest, 'factory',
163 'init', 'run_goofy_device')
Wei-Ning Huang5135b7e2015-07-03 17:31:15 +0800164 self._device_id = device_id
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800165 self._apps = apps
Vic Yang7039f422014-07-07 15:38:13 -0700166
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800167 if (not os.path.exists(self._usr_local_src) or
168 not os.path.exists(self._var_src)):
169 raise Exception(
170 'This installer must be run from within the factory toolkit!')
171
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800172 def WarningMessage(self, target_test_image=None):
Jon Salz4f3ade52014-02-20 17:55:09 +0800173 with open(os.path.join(self._src, 'VERSION')) as f:
174 ret = f.read()
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800175 if target_test_image:
Jon Salz4f3ade52014-02-20 17:55:09 +0800176 ret += (
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800177 '\n'
178 '\n'
Jon Salz4f3ade52014-02-20 17:55:09 +0800179 '*** You are about to patch the factory toolkit into:\n'
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800180 '*** %s\n'
181 '***' % target_test_image)
182 else:
Jon Salz4f3ade52014-02-20 17:55:09 +0800183 ret += (
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800184 '\n'
185 '\n'
Jon Salz4f3ade52014-02-20 17:55:09 +0800186 '*** You are about to install the factory toolkit to:\n'
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800187 '*** %s\n'
188 '***' % self._dest)
189 if self._dest == self._system_root:
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800190 if self._no_enable:
Hung-Te Lin7b596ff2015-01-16 20:19:15 +0800191 ret += ('\n*** Factory tests will be disabled after this process is '
Hung-Te Lin56b18402015-01-16 14:52:30 +0800192 'done, but\n*** you can enable them by creating the factory '
193 'enabled tag:\n*** %s\n***' % self._tag_file)
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800194 else:
Hung-Te Lin7b596ff2015-01-16 20:19:15 +0800195 ret += ('\n*** After this process is done, your device will start '
Hung-Te Lin56b18402015-01-16 14:52:30 +0800196 'factory\n*** tests on the next reboot.\n***\n*** Factory '
197 'tests can be disabled by deleting the factory enabled\n*** '
198 'tag:\n*** %s\n***' % self._tag_file)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800199 return ret
200
Vic Yang7039f422014-07-07 15:38:13 -0700201 def _SetTagFile(self, name, path, enabled):
202 """Install or remove a tag file."""
203 if enabled:
204 print '*** Installing %s enabled tag...' % name
205 Spawn(['touch', path], sudo=True, log=True, check_call=True)
Ricky Liang8c88a122014-07-11 21:21:22 +0800206 Spawn(['chmod', 'go+r', path], sudo=True, log=True, check_call=True)
Vic Yang7039f422014-07-07 15:38:13 -0700207 else:
208 print '*** Removing %s enabled tag...' % name
209 Spawn(['rm', '-f', path], sudo=True, log=True, check_call=True)
210
Wei-Ning Huang5135b7e2015-07-03 17:31:15 +0800211 def _SetDeviceID(self):
212 if self._device_id is not None:
213 with open(os.path.join(event_log.DEVICE_ID_PATH), 'w') as f:
214 f.write(self._device_id)
215
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800216 def _EnableApp(self, app, enabled):
217 """Enable / disable @app.
218
219 In factory/init/startup, a main app is considered disabled if and only:
220 1. file "factory/init/main.d/disable-@app" exists OR
221 2. file "factory/init/main.d/enable-@app" doesn't exist AND
222 file "factory/init/main.d/@app.sh" is not executable.
223
224 Therefore, we enable an app by removing file "disable-@app" and creating
225 file "enable-@app", and vise versa.
226 """
227 app_enable = os.path.join(self._usr_local_dest,
228 'factory', 'init', 'main.d', 'enable-' + app)
229 app_disable = os.path.join(self._usr_local_dest,
230 'factory', 'init', 'main.d', 'disable-' + app)
231 if enabled:
232 print '*** Enabling {app} ***'.format(app=app)
233 Spawn(['rm', '-f', app_disable], sudo=True, log=True, check_call=True)
234 Spawn(['touch', app_enable], sudo=True, log=True, check_call=True)
235 else:
236 print '*** Disabling {app} ***'.format(app=app)
237 Spawn(['touch', app_disable], sudo=True, log=True, check_call=True)
238 Spawn(['rm', '-f', app_enable], sudo=True, log=True, check_call=True)
239
240 def _EnableApps(self):
241 if not self._apps:
242 return
243
244 app_list = []
245 for app in self._apps:
246 if app[0] == '+':
247 app_list.append((app[1:], True))
248 elif app[0] == '-':
249 app_list.append((app[1:], False))
250 else:
251 raise ValueError(
252 'Use +{app} to enable and -{app} to disable'.format(app=app))
253
254 for app, enabled in app_list:
255 self._EnableApp(app, enabled)
256
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800257 def Install(self):
258 print '*** Installing factory toolkit...'
Jon Salzb7e44262014-05-07 15:53:37 +0800259 for src, dest in ((self._usr_local_src, self._usr_local_dest),
260 (self._var_src, self._var_dest)):
261 # Change the source directory to root, and add group/world read
262 # permissions. This is necessary because when the toolkit was
263 # unpacked, the user may not have been root so the permessions
264 # may be hosed. This is skipped for testing.
Peter Ammon5ac58422014-06-09 14:45:50 -0700265 # --force is necessary to allow goofy directory from prior
266 # toolkit installations to be overwritten by the goofy symlink.
Ricky Liang5e95be22014-07-09 12:52:07 +0800267 try:
268 if self._sudo:
269 Spawn(['chown', '-R', 'root', src],
270 sudo=True, log=True, check_call=True)
271 Spawn(['chmod', '-R', 'go+rX', src],
272 sudo=True, log=True, check_call=True)
273 print '*** %s -> %s' % (src, dest)
Hung-Te Lin56b18402015-01-16 14:52:30 +0800274 Spawn(['rsync', '-a', '--force'] + SERVER_FILE_MASK +
Vic Yangdb1e20e2014-10-05 12:10:33 +0800275 [src + '/', dest], sudo=self._sudo, log=True,
276 check_output=True, cwd=src)
Ricky Liang5e95be22014-07-09 12:52:07 +0800277 finally:
278 # Need to change the source directory back to the original user, or the
279 # script in makeself will fail to remove the temporary source directory.
280 if self._sudo:
281 myuser = os.environ.get('USER')
282 Spawn(['chown', '-R', myuser, src],
283 sudo=True, log=True, check_call=True)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800284
Jon Salz25590302014-07-11 16:07:20 +0800285 print '*** Installing symlinks...'
286 install_symlinks.InstallSymlinks(
287 '../factory/bin',
288 os.path.join(self._usr_local_dest, 'bin'),
289 install_symlinks.MODE_FULL,
290 sudo=self._sudo)
291
Vic Yang7039f422014-07-07 15:38:13 -0700292 self._SetTagFile('factory', self._tag_file, not self._no_enable)
Peter Ammon948b7172014-07-15 12:43:06 -0700293 self._SetTagFile('presenter', self._presenter_tag_file,
294 self._enable_presenter)
Vic Yang7039f422014-07-07 15:38:13 -0700295 self._SetTagFile('device', self._device_tag_file, self._enable_device)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800296
Wei-Ning Huang5135b7e2015-07-03 17:31:15 +0800297 self._SetDeviceID()
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800298 self._EnableApps()
Wei-Ning Huang5135b7e2015-07-03 17:31:15 +0800299
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800300 print '*** Installation completed.'
301
302
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800303@contextmanager
304def DummyContext(arg):
305 """A context manager that simply yields its argument."""
306 yield arg
307
308
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800309def PrintBuildInfo(src_root):
310 """Print build information."""
311 info_file = os.path.join(src_root, 'REPO_STATUS')
312 if not os.path.exists(info_file):
313 raise OSError('Build info file not found!')
314 with open(info_file, 'r') as f:
315 print f.read()
316
317
Wei-Ning Huangb723d7f2014-11-17 17:26:32 +0800318def PackFactoryToolkit(src_root, output_path, enable_device, enable_presenter):
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800319 """Packs the files containing this script into a factory toolkit."""
320 with open(os.path.join(src_root, 'VERSION'), 'r') as f:
321 version = f.read().strip()
Jon Salz4f3ade52014-02-20 17:55:09 +0800322 with tempfile.NamedTemporaryFile() as help_header:
Hung-Te Lin56b18402015-01-16 14:52:30 +0800323 help_header.write(version + '\n' + HELP_HEADER + HELP_HEADER_MAKESELF)
Jon Salz4f3ade52014-02-20 17:55:09 +0800324 help_header.flush()
Wei-Ning Huangb723d7f2014-11-17 17:26:32 +0800325 cmd = [os.path.join(src_root, 'makeself.sh'), '--bzip2', '--nox11',
Jon Salz4f3ade52014-02-20 17:55:09 +0800326 '--help-header', help_header.name,
Wei-Ning Huangb723d7f2014-11-17 17:26:32 +0800327 src_root, output_path, version, INSTALLER_PATH, '--in-exe']
328 if not enable_device:
329 cmd.append('--no-enable-device')
330 if not enable_presenter:
331 cmd.append('--no-enable-presenter')
332 Spawn(cmd, check_call=True, log=True)
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800333 print ('\n'
Hung-Te Lin56b18402015-01-16 14:52:30 +0800334 ' Factory toolkit generated at %s.\n'
335 '\n'
336 ' To install factory toolkit on a live device running a test image,\n'
337 ' copy this to the device and execute it as root.\n'
338 '\n'
339 ' Alternatively, the factory toolkit can be used to patch a test\n'
340 ' image. For more information, run:\n'
341 ' %s --help\n'
342 '\n' % (output_path, output_path))
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800343
344
Rong Chang6d65fad2014-08-22 16:00:12 +0800345def InitUmpire(exe_path, src_root, target_board):
346 """Inits Umpire server environment."""
347 if exe_path is None:
348 parent_cmdline = open('/proc/%s/cmdline' % os.getppid(),
349 'r').read().rstrip('\0').split('\0')
350
351 if parent_cmdline > 1 and parent_cmdline[0] == MAKESELF_SHELL:
352 # Get parent script name from parent process.
353 exe_path = parent_cmdline[1]
354 else:
355 # Set to default.
356 exe_path = TOOLKIT_NAME
357
358 if not exe_path.startswith('/'):
359 exe_path = os.path.join(os.environ.get('OLDPWD'), exe_path)
360
361 with file_utils.TempDirectory() as nano_bundle:
362 bundle_toolkit_dir = os.path.join(nano_bundle, 'factory_toolkit')
363 os.mkdir(bundle_toolkit_dir)
364 os.symlink(exe_path, os.path.join(bundle_toolkit_dir,
365 os.path.basename(exe_path)))
366 umpire_bin = os.path.join(src_root, 'usr', 'local', 'factory', 'bin',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800367 'umpire')
Rong Chang6d65fad2014-08-22 16:00:12 +0800368 Spawn([umpire_bin, 'init', '--board', target_board, nano_bundle],
369 check_call=True, log=True)
370 print ('\n'
Hung-Te Lin56b18402015-01-16 14:52:30 +0800371 ' Umpire initialized successfully. Upstart service is running:\n'
372 ' umpire BOARD=%(board)s.\n'
373 ' For more information, please check umpire command line:\n'
374 '\n'
375 ' umpire-%(board)s --help (if your id is in umpire group)\n'
376 ' or sudo umpire-%(board)s --help\n'
377 '\n' % {'board': target_board})
Rong Chang6d65fad2014-08-22 16:00:12 +0800378
379
Wei-Ning Huang4855e792015-06-11 15:33:39 +0800380def ExtractOverlord(src_root, output_dir):
381 output_dir = os.path.join(output_dir, 'overlord')
382 try:
383 os.makedirs(output_dir)
384 except OSError as e:
385 print str(e)
386 return
387
388 # Copy overlord binary and resource files
389 shutil.copyfile(os.path.join(src_root, 'usr/bin/overlordd'),
390 os.path.join(output_dir, 'overlordd'))
391 shutil.copytree(os.path.join(src_root, 'usr/share/overlord/app'),
392 os.path.join(output_dir, 'app'))
393
394 # Give overlordd execution permission
395 os.chmod(os.path.join(output_dir, 'overlordd'), 0755)
396 print "Extarcted overlord under '%s'" % output_dir
397
398
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800399def main():
Jon Salz4f3ade52014-02-20 17:55:09 +0800400 import logging
401 logging.basicConfig(level=logging.INFO)
402
403 # In order to determine which usage message to show, first determine
404 # whether we're in the self-extracting archive. Do this first
405 # because we need it to even parse the arguments.
406 if '--in-exe' in sys.argv:
407 sys.argv = [x for x in sys.argv if x != '--in-exe']
408 in_archive = True
409 else:
410 in_archive = False
411
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800412 parser = argparse.ArgumentParser(
Jon Salz4f3ade52014-02-20 17:55:09 +0800413 description=HELP_HEADER + HELP_HEADER_ADVANCED,
414 usage=('install_factory_toolkit.run -- [options]' if in_archive
415 else None),
416 formatter_class=argparse.RawDescriptionHelpFormatter)
Hung-Te Lin56b18402015-01-16 14:52:30 +0800417 parser.add_argument(
418 'dest', nargs='?', default='/',
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800419 help='A test image or the mount point of the stateful partition. '
420 "If omitted, install to live system, i.e. '/'.")
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800421 parser.add_argument('--no-enable', '-n', action='store_true',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800422 help="Don't enable factory tests after installing")
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800423 parser.add_argument('--yes', '-y', action='store_true',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800424 help="Don't ask for confirmation")
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800425 parser.add_argument('--build-info', action='store_true',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800426 help='Print build information and exit')
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800427 parser.add_argument('--pack-into', metavar='NEW_TOOLKIT',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800428 help='Pack the files into a new factory toolkit')
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800429 parser.add_argument('--repack', metavar='UNPACKED_TOOLKIT',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800430 help='Repack from previously unpacked toolkit')
Vic Yang7039f422014-07-07 15:38:13 -0700431
Peter Ammon948b7172014-07-15 12:43:06 -0700432 parser.add_argument('--enable-presenter', dest='enable_presenter',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800433 action='store_true',
434 help='Run goofy in presenter mode on startup')
Peter Ammon948b7172014-07-15 12:43:06 -0700435 parser.add_argument('--no-enable-presenter', dest='enable_presenter',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800436 action='store_false', help=argparse.SUPPRESS)
Hung-Te Lin0cba9d52016-03-22 11:45:35 +0800437 parser.set_defaults(enable_presenter=False)
Vic Yang7039f422014-07-07 15:38:13 -0700438
Vic Yang70fdae92015-02-17 19:21:08 -0800439 parser.add_argument('--non-cros', dest='non_cros',
440 action='store_true',
441 help='Install on non-ChromeOS host.')
442
Vic Yang7039f422014-07-07 15:38:13 -0700443 parser.add_argument('--enable-device', dest='enable_device',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800444 action='store_true',
445 help='Run goofy in device mode on startup')
Vic Yang7039f422014-07-07 15:38:13 -0700446 parser.add_argument('--no-enable-device', dest='enable_device',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800447 action='store_false', help=argparse.SUPPRESS)
Hung-Te Lin0cba9d52016-03-22 11:45:35 +0800448 parser.set_defaults(enable_device=False)
Vic Yang423c2722014-09-24 16:05:06 +0800449
Wei-Ning Huang5135b7e2015-07-03 17:31:15 +0800450 parser.add_argument('--device-id', dest='device_id', type=str, default=None,
451 help='Set device ID for this device')
452
Rong Chang6d65fad2014-08-22 16:00:12 +0800453 parser.add_argument('--init-umpire-board', dest='umpire_board',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800454 nargs='?', default=None,
455 help='Locally install Umpire server for specific board')
Rong Chang6d65fad2014-08-22 16:00:12 +0800456 parser.add_argument('--exe-path', dest='exe_path',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800457 nargs='?', default=None,
458 help='Current self-extracting archive pathname')
Wei-Ning Huang4855e792015-06-11 15:33:39 +0800459 parser.add_argument('--extract-overlord', dest='extract_overlord',
460 metavar='OUTPUT_DIR', type=str, default=None,
461 help='Extract overlord from the toolkit')
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800462 parser.add_argument('--apps', type=lambda s: s.split(','), default=None,
463 help=('Enable or disable some apps under '
464 'factory/init/main.d/. Use prefix "-" to disable, '
465 'prefix "+" to enable, and use "," to seperate. '
466 'For example: --apps="-goofy,+whale_servo"'))
Vic Yang7039f422014-07-07 15:38:13 -0700467
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800468 args = parser.parse_args()
469
Wei-Han Chen2ebb92d2016-01-12 14:51:41 +0800470 src_root = paths.FACTORY_PATH
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800471 for _ in xrange(3):
472 src_root = os.path.dirname(src_root)
473
Rong Chang6d65fad2014-08-22 16:00:12 +0800474 # --init-umpire-board creates a nano bundle, then calls umpire command
475 # line utility to install the server code and upstart configurations.
476 if args.umpire_board:
477 InitUmpire(args.exe_path, src_root, args.umpire_board)
478 return
479
Wei-Ning Huang4855e792015-06-11 15:33:39 +0800480 if args.extract_overlord is not None:
481 ExtractOverlord(src_root, args.extract_overlord)
482 return
483
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800484 # --pack-into may be called directly so this must be done before changing
485 # working directory to OLDPWD.
486 if args.pack_into and args.repack is None:
Wei-Ning Huangb723d7f2014-11-17 17:26:32 +0800487 PackFactoryToolkit(src_root, args.pack_into, args.enable_device,
488 args.enable_presenter)
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800489 return
490
Jon Salz4f3ade52014-02-20 17:55:09 +0800491 if not in_archive:
492 # If you're not in the self-extracting archive, you're not allowed to
493 # do anything except the above --pack-into call.
494 parser.error('Not running from install_factory_toolkit.run; '
495 'only --pack-into (without --repack) is allowed')
496
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800497 # Change to original working directory in case the user specifies
498 # a relative path.
499 # TODO: Use USER_PWD instead when makeself is upgraded
500 os.chdir(os.environ['OLDPWD'])
501
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800502 if args.repack:
503 if args.pack_into is None:
504 parser.error('Must specify --pack-into when using --repack.')
505 Spawn([os.path.join(args.repack, INSTALLER_PATH),
506 '--pack-into', args.pack_into], check_call=True, log=True)
507 return
508
509 if args.build_info:
510 PrintBuildInfo(src_root)
511 return
512
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800513 if not os.path.exists(args.dest):
514 parser.error('Destination %s does not exist!' % args.dest)
515
516 patch_test_image = os.path.isfile(args.dest)
517
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +0800518 with (sys_utils.MountPartition(args.dest, 1, rw=True) if patch_test_image
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800519 else DummyContext(args.dest)) as dest:
Hung-Te Lin56b18402015-01-16 14:52:30 +0800520 installer = FactoryToolkitInstaller(
Wei-Ning Huang5135b7e2015-07-03 17:31:15 +0800521 src=src_root, dest=dest, no_enable=args.no_enable,
522 enable_presenter=args.enable_presenter,
523 enable_device=args.enable_device, non_cros=args.non_cros,
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800524 device_id=args.device_id,
525 apps=args.apps)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800526
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800527 print installer.WarningMessage(args.dest if patch_test_image else None)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800528
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800529 if not args.yes:
530 answer = raw_input('*** Continue? [y/N] ')
531 if not answer or answer[0] not in 'yY':
532 sys.exit('Aborting.')
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800533
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800534 installer.Install()
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800535
536if __name__ == '__main__':
537 main()