Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 1 | #!/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 | |
| 9 | The factory toolkit is a self-extracting shellball containing factory test |
| 10 | related files and this installer. This installer is invoked when the toolkit |
| 11 | is deployed and is responsible for installing files. |
| 12 | """ |
| 13 | |
| 14 | |
| 15 | import argparse |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 16 | from contextlib import contextmanager |
Hung-Te Lin | eb7632b | 2016-07-29 15:38:34 +0800 | [diff] [blame] | 17 | import glob |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 18 | import os |
Wei-Ning Huang | 4855e79 | 2015-06-11 15:33:39 +0800 | [diff] [blame] | 19 | import shutil |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 20 | import sys |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 21 | import tempfile |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 22 | |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 23 | import factory_common # pylint: disable=W0611 |
Wei-Ning Huang | 5135b7e | 2015-07-03 17:31:15 +0800 | [diff] [blame] | 24 | from cros.factory.test import event_log |
Wei-Han Chen | 2ebb92d | 2016-01-12 14:51:41 +0800 | [diff] [blame] | 25 | from cros.factory.test.env import paths |
Jon Salz | 2559030 | 2014-07-11 16:07:20 +0800 | [diff] [blame] | 26 | from cros.factory.tools import install_symlinks |
Hung-Te Lin | f5f2d7f | 2016-01-08 17:12:46 +0800 | [diff] [blame] | 27 | from cros.factory.utils import sys_utils |
Wei-Han Chen | 2ebb92d | 2016-01-12 14:51:41 +0800 | [diff] [blame] | 28 | from cros.factory.utils.process_utils import Spawn |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 29 | |
| 30 | |
Vic (Chun-Ju) Yang | b7388f7 | 2014-02-19 15:22:58 +0800 | [diff] [blame] | 31 | INSTALLER_PATH = 'usr/local/factory/py/toolkit/installer.py' |
Rong Chang | 6d65fad | 2014-08-22 16:00:12 +0800 | [diff] [blame] | 32 | MAKESELF_SHELL = '/bin/sh' |
| 33 | TOOLKIT_NAME = 'install_factory_toolkit.run' |
Vic (Chun-Ju) Yang | b7388f7 | 2014-02-19 15:22:58 +0800 | [diff] [blame] | 34 | |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 35 | # Short and sweet help header for the executable generated by makeself. |
| 36 | HELP_HEADER = """ |
| 37 | Installs the factory toolkit, transforming a test image into a factory test |
| 38 | image. 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 | |
| 53 | HELP_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. |
| 74 | HELP_HEADER_MAKESELF = """ |
| 75 | For complete usage information and advanced operations, run |
| 76 | "install_factory_toolkit.run -- --help" (note the extra "--"). |
| 77 | |
| 78 | Following is the help message from makeself, which was used to create |
| 79 | this self-extracting archive. |
| 80 | |
| 81 | ----- |
| 82 | """ |
| 83 | |
Vic Yang | db1e20e | 2014-10-05 12:10:33 +0800 | [diff] [blame] | 84 | SERVER_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 Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 96 | |
Hung-Te Lin | 7b596ff | 2015-01-16 20:19:15 +0800 | [diff] [blame] | 97 | class FactoryToolkitInstaller(object): |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 98 | """Factory toolkit installer. |
| 99 | |
| 100 | Args: |
| 101 | src: Source path containing usr/ and var/. |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 102 | 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) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 107 | """ |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 108 | |
Jon Salz | b7e4426 | 2014-05-07 15:53:37 +0800 | [diff] [blame] | 109 | # Whether to sudo when rsyncing; set to False for testing. |
| 110 | _sudo = True |
| 111 | |
Peter Ammon | 948b717 | 2014-07-15 12:43:06 -0700 | [diff] [blame] | 112 | def __init__(self, src, dest, no_enable, enable_presenter, |
Wei-Han Chen | e6fe387 | 2016-06-30 14:29:06 +0800 | [diff] [blame] | 113 | enable_device, non_cros=False, device_id=None, system_root='/', |
| 114 | apps=None): |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 115 | self._src = src |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 116 | self._system_root = system_root |
| 117 | if dest == self._system_root: |
| 118 | self._usr_local_dest = os.path.join(dest, 'usr', 'local') |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 119 | |
| 120 | # Make sure we're on a CrOS device. |
Hung-Te Lin | f5f2d7f | 2016-01-08 17:12:46 +0800 | [diff] [blame] | 121 | if not non_cros and not sys_utils.InCrOSDevice(): |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 122 | sys.stderr.write( |
Vic Yang | 196d524 | 2014-08-05 13:51:35 +0800 | [diff] [blame] | 123 | "ERROR: You're not on a CrOS device (for more details, please\n" |
Hung-Te Lin | f5f2d7f | 2016-01-08 17:12:46 +0800 | [diff] [blame] | 124 | 'check sys_utils.py:InCrOSDevice), so you must specify a test\n' |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 125 | 'image or a mounted stateful partition on which to install the\n' |
| 126 | 'factory toolkit. Please run\n' |
| 127 | '\n' |
| 128 | ' install_factory_toolkit.run -- --help\n' |
| 129 | '\n' |
Vic Yang | 70fdae9 | 2015-02-17 19:21:08 -0800 | [diff] [blame] | 130 | 'for help.\n' |
| 131 | '\n' |
| 132 | 'If you want to install the presenter on a non-CrOS host,\n' |
| 133 | 'please run\n' |
| 134 | '\n' |
| 135 | ' install_factory_toolkit.run -- \\\n' |
Hung-Te Lin | 0cba9d5 | 2016-03-22 11:45:35 +0800 | [diff] [blame] | 136 | ' --non-cros --enable-presenter\n' |
Vic Yang | 70fdae9 | 2015-02-17 19:21:08 -0800 | [diff] [blame] | 137 | '\n') |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 138 | sys.exit(1) |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 139 | if os.getuid() != 0: |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 140 | raise Exception('You must be root to install the factory toolkit on a ' |
| 141 | 'CrOS device.') |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 142 | else: |
| 143 | self._usr_local_dest = os.path.join(dest, 'dev_image') |
Hung-Te Lin | 4aeabe6 | 2016-10-21 15:45:20 +0800 | [diff] [blame] | 144 | if not os.path.exists(self._usr_local_dest): |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 145 | raise Exception( |
| 146 | 'The destination path %s is not a stateful partition!' % dest) |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 147 | |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 148 | self._dest = dest |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 149 | self._usr_local_src = os.path.join(src, 'usr', 'local') |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 150 | self._no_enable = no_enable |
Vic (Chun-Ju) Yang | 7cc3e67 | 2014-01-20 14:06:39 +0800 | [diff] [blame] | 151 | self._tag_file = os.path.join(self._usr_local_dest, 'factory', 'enabled') |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 152 | |
Peter Ammon | 948b717 | 2014-07-15 12:43:06 -0700 | [diff] [blame] | 153 | self._enable_presenter = enable_presenter |
| 154 | self._presenter_tag_file = os.path.join(self._usr_local_dest, 'factory', |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 155 | 'init', 'run_goofy_presenter') |
Vic Yang | 7039f42 | 2014-07-07 15:38:13 -0700 | [diff] [blame] | 156 | |
| 157 | self._enable_device = enable_device |
Ricky Liang | d771691 | 2014-07-10 11:52:24 +0800 | [diff] [blame] | 158 | self._device_tag_file = os.path.join(self._usr_local_dest, 'factory', |
| 159 | 'init', 'run_goofy_device') |
Wei-Ning Huang | 5135b7e | 2015-07-03 17:31:15 +0800 | [diff] [blame] | 160 | self._device_id = device_id |
Wei-Han Chen | e6fe387 | 2016-06-30 14:29:06 +0800 | [diff] [blame] | 161 | self._apps = apps |
Vic Yang | 7039f42 | 2014-07-07 15:38:13 -0700 | [diff] [blame] | 162 | |
Hung-Te Lin | 4aeabe6 | 2016-10-21 15:45:20 +0800 | [diff] [blame] | 163 | if not os.path.exists(self._usr_local_src): |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 164 | raise Exception( |
| 165 | 'This installer must be run from within the factory toolkit!') |
| 166 | |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 167 | def WarningMessage(self, target_test_image=None): |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 168 | with open(os.path.join(self._src, 'VERSION')) as f: |
| 169 | ret = f.read() |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 170 | if target_test_image: |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 171 | ret += ( |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 172 | '\n' |
| 173 | '\n' |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 174 | '*** You are about to patch the factory toolkit into:\n' |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 175 | '*** %s\n' |
| 176 | '***' % target_test_image) |
| 177 | else: |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 178 | ret += ( |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 179 | '\n' |
| 180 | '\n' |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 181 | '*** You are about to install the factory toolkit to:\n' |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 182 | '*** %s\n' |
| 183 | '***' % self._dest) |
| 184 | if self._dest == self._system_root: |
Vic (Chun-Ju) Yang | 7cc3e67 | 2014-01-20 14:06:39 +0800 | [diff] [blame] | 185 | if self._no_enable: |
Hung-Te Lin | 7b596ff | 2015-01-16 20:19:15 +0800 | [diff] [blame] | 186 | ret += ('\n*** Factory tests will be disabled after this process is ' |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 187 | 'done, but\n*** you can enable them by creating the factory ' |
| 188 | 'enabled tag:\n*** %s\n***' % self._tag_file) |
Vic (Chun-Ju) Yang | 7cc3e67 | 2014-01-20 14:06:39 +0800 | [diff] [blame] | 189 | else: |
Hung-Te Lin | 7b596ff | 2015-01-16 20:19:15 +0800 | [diff] [blame] | 190 | ret += ('\n*** After this process is done, your device will start ' |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 191 | 'factory\n*** tests on the next reboot.\n***\n*** Factory ' |
| 192 | 'tests can be disabled by deleting the factory enabled\n*** ' |
| 193 | 'tag:\n*** %s\n***' % self._tag_file) |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 194 | return ret |
| 195 | |
Vic Yang | 7039f42 | 2014-07-07 15:38:13 -0700 | [diff] [blame] | 196 | def _SetTagFile(self, name, path, enabled): |
| 197 | """Install or remove a tag file.""" |
| 198 | if enabled: |
| 199 | print '*** Installing %s enabled tag...' % name |
| 200 | Spawn(['touch', path], sudo=True, log=True, check_call=True) |
Ricky Liang | 8c88a12 | 2014-07-11 21:21:22 +0800 | [diff] [blame] | 201 | Spawn(['chmod', 'go+r', path], sudo=True, log=True, check_call=True) |
Vic Yang | 7039f42 | 2014-07-07 15:38:13 -0700 | [diff] [blame] | 202 | else: |
| 203 | print '*** Removing %s enabled tag...' % name |
| 204 | Spawn(['rm', '-f', path], sudo=True, log=True, check_call=True) |
| 205 | |
Wei-Ning Huang | 5135b7e | 2015-07-03 17:31:15 +0800 | [diff] [blame] | 206 | def _SetDeviceID(self): |
| 207 | if self._device_id is not None: |
| 208 | with open(os.path.join(event_log.DEVICE_ID_PATH), 'w') as f: |
| 209 | f.write(self._device_id) |
| 210 | |
Wei-Han Chen | e6fe387 | 2016-06-30 14:29:06 +0800 | [diff] [blame] | 211 | def _EnableApp(self, app, enabled): |
| 212 | """Enable / disable @app. |
| 213 | |
| 214 | In factory/init/startup, a main app is considered disabled if and only: |
| 215 | 1. file "factory/init/main.d/disable-@app" exists OR |
| 216 | 2. file "factory/init/main.d/enable-@app" doesn't exist AND |
| 217 | file "factory/init/main.d/@app.sh" is not executable. |
| 218 | |
| 219 | Therefore, we enable an app by removing file "disable-@app" and creating |
| 220 | file "enable-@app", and vise versa. |
| 221 | """ |
| 222 | app_enable = os.path.join(self._usr_local_dest, |
| 223 | 'factory', 'init', 'main.d', 'enable-' + app) |
| 224 | app_disable = os.path.join(self._usr_local_dest, |
| 225 | 'factory', 'init', 'main.d', 'disable-' + app) |
| 226 | if enabled: |
| 227 | print '*** Enabling {app} ***'.format(app=app) |
Wei-Han Chen | 7137dcf | 2016-08-03 15:43:22 +0800 | [diff] [blame] | 228 | Spawn(['rm', '-f', app_disable], sudo=self._sudo, log=True, |
| 229 | check_call=True) |
| 230 | Spawn(['touch', app_enable], sudo=self._sudo, log=True, check_call=True) |
Wei-Han Chen | e6fe387 | 2016-06-30 14:29:06 +0800 | [diff] [blame] | 231 | else: |
| 232 | print '*** Disabling {app} ***'.format(app=app) |
Wei-Han Chen | 7137dcf | 2016-08-03 15:43:22 +0800 | [diff] [blame] | 233 | Spawn(['touch', app_disable], sudo=self._sudo, log=True, check_call=True) |
| 234 | Spawn(['rm', '-f', app_enable], sudo=self._sudo, log=True, |
| 235 | check_call=True) |
Wei-Han Chen | e6fe387 | 2016-06-30 14:29:06 +0800 | [diff] [blame] | 236 | |
| 237 | def _EnableApps(self): |
| 238 | if not self._apps: |
| 239 | return |
| 240 | |
| 241 | app_list = [] |
| 242 | for app in self._apps: |
| 243 | if app[0] == '+': |
| 244 | app_list.append((app[1:], True)) |
| 245 | elif app[0] == '-': |
| 246 | app_list.append((app[1:], False)) |
| 247 | else: |
| 248 | raise ValueError( |
| 249 | 'Use +{app} to enable and -{app} to disable'.format(app=app)) |
| 250 | |
| 251 | for app, enabled in app_list: |
| 252 | self._EnableApp(app, enabled) |
| 253 | |
Wei-Han Chen | 7137dcf | 2016-08-03 15:43:22 +0800 | [diff] [blame] | 254 | def InstallFactorySubDir(self, sub_dirs): |
| 255 | """Install only the specified directories under factory folder.""" |
| 256 | |
| 257 | def _InstallOneSubDir(sub_dir_name): |
| 258 | sub_dir_dest = os.path.join(self._usr_local_dest, 'factory', sub_dir_name) |
| 259 | sub_dir_src = os.path.join(self._src, 'usr', 'local', 'factory', |
| 260 | sub_dir_name) |
| 261 | try: |
| 262 | Spawn(['mkdir', '-p', sub_dir_dest], sudo=True, log=True, |
| 263 | check_call=True) |
| 264 | except OSError as e: |
| 265 | print str(e) |
| 266 | return |
| 267 | |
| 268 | Spawn(['rsync', '-a', '--force', '-v', |
| 269 | sub_dir_src + '/', sub_dir_dest], |
| 270 | sudo=self._sudo, log=True, check_call=True) |
| 271 | Spawn(['chown', '-R', 'root', sub_dir_dest], |
| 272 | sudo=self._sudo, log=True, check_call=True) |
| 273 | Spawn(['chmod', '-R', 'go+rX', sub_dir_dest], |
| 274 | sudo=self._sudo, log=True, check_call=True) |
| 275 | |
| 276 | for sub_dir_name in sub_dirs: |
| 277 | _InstallOneSubDir(sub_dir_name) |
| 278 | |
| 279 | self._SetTagFile('factory', self._tag_file, not self._no_enable) |
| 280 | self._EnableApps() |
| 281 | |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 282 | def Install(self): |
| 283 | print '*** Installing factory toolkit...' |
Hung-Te Lin | 4aeabe6 | 2016-10-21 15:45:20 +0800 | [diff] [blame] | 284 | for src, dest in ((self._usr_local_src, self._usr_local_dest),): |
Jon Salz | b7e4426 | 2014-05-07 15:53:37 +0800 | [diff] [blame] | 285 | # Change the source directory to root, and add group/world read |
| 286 | # permissions. This is necessary because when the toolkit was |
| 287 | # unpacked, the user may not have been root so the permessions |
| 288 | # may be hosed. This is skipped for testing. |
Peter Ammon | 5ac5842 | 2014-06-09 14:45:50 -0700 | [diff] [blame] | 289 | # --force is necessary to allow goofy directory from prior |
| 290 | # toolkit installations to be overwritten by the goofy symlink. |
Ricky Liang | 5e95be2 | 2014-07-09 12:52:07 +0800 | [diff] [blame] | 291 | try: |
| 292 | if self._sudo: |
| 293 | Spawn(['chown', '-R', 'root', src], |
| 294 | sudo=True, log=True, check_call=True) |
| 295 | Spawn(['chmod', '-R', 'go+rX', src], |
| 296 | sudo=True, log=True, check_call=True) |
| 297 | print '*** %s -> %s' % (src, dest) |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 298 | Spawn(['rsync', '-a', '--force'] + SERVER_FILE_MASK + |
Vic Yang | db1e20e | 2014-10-05 12:10:33 +0800 | [diff] [blame] | 299 | [src + '/', dest], sudo=self._sudo, log=True, |
| 300 | check_output=True, cwd=src) |
Ricky Liang | 5e95be2 | 2014-07-09 12:52:07 +0800 | [diff] [blame] | 301 | finally: |
| 302 | # Need to change the source directory back to the original user, or the |
| 303 | # script in makeself will fail to remove the temporary source directory. |
| 304 | if self._sudo: |
| 305 | myuser = os.environ.get('USER') |
| 306 | Spawn(['chown', '-R', myuser, src], |
| 307 | sudo=True, log=True, check_call=True) |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 308 | |
Hung-Te Lin | eb7632b | 2016-07-29 15:38:34 +0800 | [diff] [blame] | 309 | print '*** Ensure SSH keys file permission...' |
| 310 | sshkeys_dir = os.path.join(self._usr_local_dest, 'factory/misc/sshkeys') |
| 311 | sshkeys = glob.glob(os.path.join(sshkeys_dir, '*')) |
| 312 | ssh_public_keys = glob.glob(os.path.join(sshkeys_dir, '*.pub')) |
| 313 | ssh_private_keys = list(set(sshkeys) - set(ssh_public_keys)) |
| 314 | if ssh_private_keys: |
| 315 | Spawn(['chmod', '600'] + ssh_private_keys, log=True, check_call=True, |
| 316 | sudo=self._sudo) |
| 317 | |
Jon Salz | 2559030 | 2014-07-11 16:07:20 +0800 | [diff] [blame] | 318 | print '*** Installing symlinks...' |
| 319 | install_symlinks.InstallSymlinks( |
| 320 | '../factory/bin', |
| 321 | os.path.join(self._usr_local_dest, 'bin'), |
| 322 | install_symlinks.MODE_FULL, |
| 323 | sudo=self._sudo) |
| 324 | |
Vic Yang | 7039f42 | 2014-07-07 15:38:13 -0700 | [diff] [blame] | 325 | self._SetTagFile('factory', self._tag_file, not self._no_enable) |
Peter Ammon | 948b717 | 2014-07-15 12:43:06 -0700 | [diff] [blame] | 326 | self._SetTagFile('presenter', self._presenter_tag_file, |
| 327 | self._enable_presenter) |
Vic Yang | 7039f42 | 2014-07-07 15:38:13 -0700 | [diff] [blame] | 328 | self._SetTagFile('device', self._device_tag_file, self._enable_device) |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 329 | |
Wei-Ning Huang | 5135b7e | 2015-07-03 17:31:15 +0800 | [diff] [blame] | 330 | self._SetDeviceID() |
Wei-Han Chen | e6fe387 | 2016-06-30 14:29:06 +0800 | [diff] [blame] | 331 | self._EnableApps() |
Wei-Ning Huang | 5135b7e | 2015-07-03 17:31:15 +0800 | [diff] [blame] | 332 | |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 333 | print '*** Installation completed.' |
| 334 | |
| 335 | |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 336 | @contextmanager |
| 337 | def DummyContext(arg): |
| 338 | """A context manager that simply yields its argument.""" |
| 339 | yield arg |
| 340 | |
| 341 | |
Vic (Chun-Ju) Yang | 98b4fbc | 2014-02-18 19:32:32 +0800 | [diff] [blame] | 342 | def PrintBuildInfo(src_root): |
| 343 | """Print build information.""" |
| 344 | info_file = os.path.join(src_root, 'REPO_STATUS') |
| 345 | if not os.path.exists(info_file): |
| 346 | raise OSError('Build info file not found!') |
| 347 | with open(info_file, 'r') as f: |
| 348 | print f.read() |
| 349 | |
| 350 | |
Wei-Ning Huang | b723d7f | 2014-11-17 17:26:32 +0800 | [diff] [blame] | 351 | def PackFactoryToolkit(src_root, output_path, enable_device, enable_presenter): |
Vic (Chun-Ju) Yang | b7388f7 | 2014-02-19 15:22:58 +0800 | [diff] [blame] | 352 | """Packs the files containing this script into a factory toolkit.""" |
| 353 | with open(os.path.join(src_root, 'VERSION'), 'r') as f: |
| 354 | version = f.read().strip() |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 355 | with tempfile.NamedTemporaryFile() as help_header: |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 356 | help_header.write(version + '\n' + HELP_HEADER + HELP_HEADER_MAKESELF) |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 357 | help_header.flush() |
Wei-Ning Huang | b723d7f | 2014-11-17 17:26:32 +0800 | [diff] [blame] | 358 | cmd = [os.path.join(src_root, 'makeself.sh'), '--bzip2', '--nox11', |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 359 | '--help-header', help_header.name, |
Wei-Ning Huang | b723d7f | 2014-11-17 17:26:32 +0800 | [diff] [blame] | 360 | src_root, output_path, version, INSTALLER_PATH, '--in-exe'] |
| 361 | if not enable_device: |
| 362 | cmd.append('--no-enable-device') |
| 363 | if not enable_presenter: |
| 364 | cmd.append('--no-enable-presenter') |
| 365 | Spawn(cmd, check_call=True, log=True) |
Vic (Chun-Ju) Yang | b7388f7 | 2014-02-19 15:22:58 +0800 | [diff] [blame] | 366 | print ('\n' |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 367 | ' Factory toolkit generated at %s.\n' |
| 368 | '\n' |
| 369 | ' To install factory toolkit on a live device running a test image,\n' |
| 370 | ' copy this to the device and execute it as root.\n' |
| 371 | '\n' |
| 372 | ' Alternatively, the factory toolkit can be used to patch a test\n' |
| 373 | ' image. For more information, run:\n' |
| 374 | ' %s --help\n' |
| 375 | '\n' % (output_path, output_path)) |
Vic (Chun-Ju) Yang | b7388f7 | 2014-02-19 15:22:58 +0800 | [diff] [blame] | 376 | |
| 377 | |
Wei-Ning Huang | 4855e79 | 2015-06-11 15:33:39 +0800 | [diff] [blame] | 378 | def ExtractOverlord(src_root, output_dir): |
| 379 | output_dir = os.path.join(output_dir, 'overlord') |
| 380 | try: |
| 381 | os.makedirs(output_dir) |
| 382 | except OSError as e: |
| 383 | print str(e) |
| 384 | return |
| 385 | |
| 386 | # Copy overlord binary and resource files |
| 387 | shutil.copyfile(os.path.join(src_root, 'usr/bin/overlordd'), |
| 388 | os.path.join(output_dir, 'overlordd')) |
| 389 | shutil.copytree(os.path.join(src_root, 'usr/share/overlord/app'), |
| 390 | os.path.join(output_dir, 'app')) |
| 391 | |
| 392 | # Give overlordd execution permission |
| 393 | os.chmod(os.path.join(output_dir, 'overlordd'), 0755) |
| 394 | print "Extarcted overlord under '%s'" % output_dir |
| 395 | |
| 396 | |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 397 | def main(): |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 398 | import logging |
| 399 | logging.basicConfig(level=logging.INFO) |
| 400 | |
| 401 | # In order to determine which usage message to show, first determine |
| 402 | # whether we're in the self-extracting archive. Do this first |
| 403 | # because we need it to even parse the arguments. |
| 404 | if '--in-exe' in sys.argv: |
| 405 | sys.argv = [x for x in sys.argv if x != '--in-exe'] |
| 406 | in_archive = True |
| 407 | else: |
| 408 | in_archive = False |
| 409 | |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 410 | parser = argparse.ArgumentParser( |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 411 | description=HELP_HEADER + HELP_HEADER_ADVANCED, |
| 412 | usage=('install_factory_toolkit.run -- [options]' if in_archive |
| 413 | else None), |
| 414 | formatter_class=argparse.RawDescriptionHelpFormatter) |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 415 | parser.add_argument( |
| 416 | 'dest', nargs='?', default='/', |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 417 | help='A test image or the mount point of the stateful partition. ' |
| 418 | "If omitted, install to live system, i.e. '/'.") |
Vic (Chun-Ju) Yang | 7cc3e67 | 2014-01-20 14:06:39 +0800 | [diff] [blame] | 419 | parser.add_argument('--no-enable', '-n', action='store_true', |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 420 | help="Don't enable factory tests after installing") |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 421 | parser.add_argument('--yes', '-y', action='store_true', |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 422 | help="Don't ask for confirmation") |
Vic (Chun-Ju) Yang | 98b4fbc | 2014-02-18 19:32:32 +0800 | [diff] [blame] | 423 | parser.add_argument('--build-info', action='store_true', |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 424 | help='Print build information and exit') |
Vic (Chun-Ju) Yang | b7388f7 | 2014-02-19 15:22:58 +0800 | [diff] [blame] | 425 | parser.add_argument('--pack-into', metavar='NEW_TOOLKIT', |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 426 | help='Pack the files into a new factory toolkit') |
Vic (Chun-Ju) Yang | b7388f7 | 2014-02-19 15:22:58 +0800 | [diff] [blame] | 427 | parser.add_argument('--repack', metavar='UNPACKED_TOOLKIT', |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 428 | help='Repack from previously unpacked toolkit') |
Vic Yang | 7039f42 | 2014-07-07 15:38:13 -0700 | [diff] [blame] | 429 | |
Peter Ammon | 948b717 | 2014-07-15 12:43:06 -0700 | [diff] [blame] | 430 | parser.add_argument('--enable-presenter', dest='enable_presenter', |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 431 | action='store_true', |
| 432 | help='Run goofy in presenter mode on startup') |
Peter Ammon | 948b717 | 2014-07-15 12:43:06 -0700 | [diff] [blame] | 433 | parser.add_argument('--no-enable-presenter', dest='enable_presenter', |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 434 | action='store_false', help=argparse.SUPPRESS) |
Hung-Te Lin | 0cba9d5 | 2016-03-22 11:45:35 +0800 | [diff] [blame] | 435 | parser.set_defaults(enable_presenter=False) |
Vic Yang | 7039f42 | 2014-07-07 15:38:13 -0700 | [diff] [blame] | 436 | |
Vic Yang | 70fdae9 | 2015-02-17 19:21:08 -0800 | [diff] [blame] | 437 | parser.add_argument('--non-cros', dest='non_cros', |
| 438 | action='store_true', |
| 439 | help='Install on non-ChromeOS host.') |
| 440 | |
Vic Yang | 7039f42 | 2014-07-07 15:38:13 -0700 | [diff] [blame] | 441 | parser.add_argument('--enable-device', dest='enable_device', |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 442 | action='store_true', |
| 443 | help='Run goofy in device mode on startup') |
Vic Yang | 7039f42 | 2014-07-07 15:38:13 -0700 | [diff] [blame] | 444 | parser.add_argument('--no-enable-device', dest='enable_device', |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 445 | action='store_false', help=argparse.SUPPRESS) |
Hung-Te Lin | 0cba9d5 | 2016-03-22 11:45:35 +0800 | [diff] [blame] | 446 | parser.set_defaults(enable_device=False) |
Vic Yang | 423c272 | 2014-09-24 16:05:06 +0800 | [diff] [blame] | 447 | |
Wei-Ning Huang | 5135b7e | 2015-07-03 17:31:15 +0800 | [diff] [blame] | 448 | parser.add_argument('--device-id', dest='device_id', type=str, default=None, |
| 449 | help='Set device ID for this device') |
| 450 | |
Rong Chang | 6d65fad | 2014-08-22 16:00:12 +0800 | [diff] [blame] | 451 | parser.add_argument('--exe-path', dest='exe_path', |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 452 | nargs='?', default=None, |
| 453 | help='Current self-extracting archive pathname') |
Wei-Ning Huang | 4855e79 | 2015-06-11 15:33:39 +0800 | [diff] [blame] | 454 | parser.add_argument('--extract-overlord', dest='extract_overlord', |
| 455 | metavar='OUTPUT_DIR', type=str, default=None, |
| 456 | help='Extract overlord from the toolkit') |
Wei-Han Chen | 7137dcf | 2016-08-03 15:43:22 +0800 | [diff] [blame] | 457 | parser.add_argument('--install-dirs', nargs='+', default=None, |
| 458 | help=('Install only the specified directories under ' |
| 459 | 'factory folder. Can be used with --apps to ' |
| 460 | 'enable / disable some apps. Defaults to install ' |
| 461 | 'all folders.')) |
Wei-Han Chen | e6fe387 | 2016-06-30 14:29:06 +0800 | [diff] [blame] | 462 | 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 Yang | 7039f42 | 2014-07-07 15:38:13 -0700 | [diff] [blame] | 467 | |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 468 | args = parser.parse_args() |
| 469 | |
Wei-Han Chen | 2ebb92d | 2016-01-12 14:51:41 +0800 | [diff] [blame] | 470 | src_root = paths.FACTORY_PATH |
Vic (Chun-Ju) Yang | 98b4fbc | 2014-02-18 19:32:32 +0800 | [diff] [blame] | 471 | for _ in xrange(3): |
| 472 | src_root = os.path.dirname(src_root) |
| 473 | |
Wei-Ning Huang | 4855e79 | 2015-06-11 15:33:39 +0800 | [diff] [blame] | 474 | if args.extract_overlord is not None: |
| 475 | ExtractOverlord(src_root, args.extract_overlord) |
| 476 | return |
| 477 | |
Vic (Chun-Ju) Yang | b7388f7 | 2014-02-19 15:22:58 +0800 | [diff] [blame] | 478 | # --pack-into may be called directly so this must be done before changing |
| 479 | # working directory to OLDPWD. |
| 480 | if args.pack_into and args.repack is None: |
Wei-Ning Huang | b723d7f | 2014-11-17 17:26:32 +0800 | [diff] [blame] | 481 | PackFactoryToolkit(src_root, args.pack_into, args.enable_device, |
| 482 | args.enable_presenter) |
Vic (Chun-Ju) Yang | 98b4fbc | 2014-02-18 19:32:32 +0800 | [diff] [blame] | 483 | return |
| 484 | |
Jon Salz | 4f3ade5 | 2014-02-20 17:55:09 +0800 | [diff] [blame] | 485 | if not in_archive: |
| 486 | # If you're not in the self-extracting archive, you're not allowed to |
| 487 | # do anything except the above --pack-into call. |
| 488 | parser.error('Not running from install_factory_toolkit.run; ' |
| 489 | 'only --pack-into (without --repack) is allowed') |
| 490 | |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 491 | # Change to original working directory in case the user specifies |
| 492 | # a relative path. |
| 493 | # TODO: Use USER_PWD instead when makeself is upgraded |
| 494 | os.chdir(os.environ['OLDPWD']) |
| 495 | |
Vic (Chun-Ju) Yang | b7388f7 | 2014-02-19 15:22:58 +0800 | [diff] [blame] | 496 | if args.repack: |
| 497 | if args.pack_into is None: |
| 498 | parser.error('Must specify --pack-into when using --repack.') |
| 499 | Spawn([os.path.join(args.repack, INSTALLER_PATH), |
| 500 | '--pack-into', args.pack_into], check_call=True, log=True) |
| 501 | return |
| 502 | |
| 503 | if args.build_info: |
| 504 | PrintBuildInfo(src_root) |
| 505 | return |
| 506 | |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 507 | if not os.path.exists(args.dest): |
| 508 | parser.error('Destination %s does not exist!' % args.dest) |
| 509 | |
| 510 | patch_test_image = os.path.isfile(args.dest) |
| 511 | |
Hung-Te Lin | f5f2d7f | 2016-01-08 17:12:46 +0800 | [diff] [blame] | 512 | with (sys_utils.MountPartition(args.dest, 1, rw=True) if patch_test_image |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 513 | else DummyContext(args.dest)) as dest: |
Wei-Han Chen | 7137dcf | 2016-08-03 15:43:22 +0800 | [diff] [blame] | 514 | |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 515 | installer = FactoryToolkitInstaller( |
Wei-Ning Huang | 5135b7e | 2015-07-03 17:31:15 +0800 | [diff] [blame] | 516 | src=src_root, dest=dest, no_enable=args.no_enable, |
| 517 | enable_presenter=args.enable_presenter, |
| 518 | enable_device=args.enable_device, non_cros=args.non_cros, |
Wei-Han Chen | e6fe387 | 2016-06-30 14:29:06 +0800 | [diff] [blame] | 519 | device_id=args.device_id, |
| 520 | apps=args.apps) |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 521 | |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 522 | print installer.WarningMessage(args.dest if patch_test_image else None) |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 523 | |
Vic (Chun-Ju) Yang | 469592b | 2014-02-18 19:15:41 +0800 | [diff] [blame] | 524 | if not args.yes: |
| 525 | answer = raw_input('*** Continue? [y/N] ') |
| 526 | if not answer or answer[0] not in 'yY': |
| 527 | sys.exit('Aborting.') |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 528 | |
Wei-Han Chen | 7137dcf | 2016-08-03 15:43:22 +0800 | [diff] [blame] | 529 | if args.install_dirs: |
| 530 | installer.InstallFactorySubDir(args.install_dirs) |
| 531 | else: |
| 532 | installer.Install() |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 533 | |
| 534 | if __name__ == '__main__': |
Hung-Te Lin | 5a2a1c4 | 2016-11-10 12:43:40 +0800 | [diff] [blame] | 535 | # makself inteprets "LICENSE" environment variable string as license text and |
| 536 | # will prompt user to accept before installation. For factory toolkit, we |
| 537 | # don't want any user interaction in installation and the license is already |
| 538 | # covered by ebuild or download platform like CPFE. |
| 539 | os.putenv('LICENSE', '') |
Vic (Chun-Ju) Yang | 296871a | 2014-01-13 12:05:18 +0800 | [diff] [blame] | 540 | main() |