blob: 61f26549a88595a6b32800d71eea518a5d3f4f61 [file] [log] [blame]
Yilin Yang19da6932019-12-10 13:39:28 +08001#!/usr/bin/env python3
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +08002#
Hung-Te Lin1990b742017-08-09 17:34:57 +08003# Copyright 2014 The Chromium OS Authors. All rights reserved.
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +08004# 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
Yilin Yang71e39412019-09-24 09:26:46 +080014from __future__ import print_function
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080015
16import argparse
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +080017from contextlib import contextmanager
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +080018import getpass
Hung-Te Lineb7632b2016-07-29 15:38:34 +080019import glob
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080020import os
Wei-Ning Huang4855e792015-06-11 15:33:39 +080021import shutil
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080022import sys
Jon Salz4f3ade52014-02-20 17:55:09 +080023import tempfile
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +080024import time
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080025
Yilin Yang8cc5dfb2019-10-22 15:58:53 +080026from six.moves import input
Yilin Yange6639682019-10-03 12:49:21 +080027from six.moves import xrange
Yilin Yang8cc5dfb2019-10-22 15:58:53 +080028
Wei-Han Chen2ebb92d2016-01-12 14:51:41 +080029from cros.factory.test.env import paths
Cheng-Han Yang3f746e82019-04-10 14:33:29 +080030from cros.factory.test.test_lists import test_list_common
Jon Salz25590302014-07-11 16:07:20 +080031from cros.factory.tools import install_symlinks
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +080032from cros.factory.utils import file_utils
Yong Hong89938e62018-10-26 11:59:21 +080033from cros.factory.utils import json_utils
Youcheng Syuac391772017-04-20 09:08:58 +000034from cros.factory.utils.process_utils import Spawn
Peter Shihfe221582017-06-15 13:40:53 +080035from cros.factory.utils import sys_utils
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +080036
37
Wei-Han Chen1efb9142019-10-03 17:42:01 +080038PYTHONPATH = 'usr/local/factory/py_pkg'
39INSTALLER_MODULE = 'cros.factory.toolkit.installer'
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +080040VERSION_PATH = 'usr/local/factory/TOOLKIT_VERSION'
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +080041
Jon Salz4f3ade52014-02-20 17:55:09 +080042# Short and sweet help header for the executable generated by makeself.
43HELP_HEADER = """
44Installs the factory toolkit, transforming a test image into a factory test
45image. You can:
46
47- Install the factory toolkit on a CrOS device that is running a test
48 image. To do this, copy install_factory_toolkit.run to the device and
49 run it. The factory tests will then come up on the next boot.
50
51 rsync -a install_factory_toolkit.run crosdevice:/tmp
52 ssh crosdevice '/tmp/install_factory_toolkit.run && sync && reboot'
53
54- Modify a test image, turning it into a factory test image. When you
55 use the image on a device, the factory tests will come up.
56
57 install_factory_toolkit.run chromiumos_test_image.bin
58"""
59
60HELP_HEADER_ADVANCED = """
61- (advanced) Modify a mounted stateful partition, turning it into a factory
62 test image. This is equivalent to the previous command:
63
64 mount_partition -rw chromiumos_test_image.bin 1 /mnt/stateful
65 install_factory_toolkit.run /mnt/stateful
66 umount /mnt/stateful
67
68- (advanced) Unpack the factory toolkit, modify a file, and then repack it.
69
70 # Unpack but don't actually install
71 install_factory_toolkit.run --target /tmp/toolkit --noexec
72 # Edit some files in /tmp/toolkit
73 emacs /tmp/toolkit/whatever
74 # Repack
75 install_factory_toolkit.run -- --repack /tmp/toolkit \\
76 --pack-into /path/to/new/install_factory_toolkit.run
77"""
78
79# The makeself-generated header comes next. This is a little confusing,
80# so explain.
81HELP_HEADER_MAKESELF = """
82For complete usage information and advanced operations, run
83"install_factory_toolkit.run -- --help" (note the extra "--").
84
85Following is the help message from makeself, which was used to create
86this self-extracting archive.
87
88-----
89"""
90
Vic Yangdb1e20e2014-10-05 12:10:33 +080091SERVER_FILE_MASK = [
92 # Exclude Umpire server but keep Umpire client
93 '--include', 'py/umpire/__init__.*',
94 '--include', 'py/umpire/common.*',
95 '--include', 'py/umpire/client',
96 '--include', 'py/umpire/client/**',
97 '--exclude', 'py/umpire/**',
Peter Shihac904782017-06-14 15:24:09 +080098 '--exclude', 'bin/umpire',
99 '--exclude', 'bin/umpired',
Vic Yangdb1e20e2014-10-05 12:10:33 +0800100]
101
Jon Salz4f3ade52014-02-20 17:55:09 +0800102
Hung-Te Lin7b596ff2015-01-16 20:19:15 +0800103class FactoryToolkitInstaller(object):
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800104 """Factory toolkit installer.
105
106 Args:
107 src: Source path containing usr/ and var/.
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800108 dest: Installation destination path. Set this to the mount point of the
109 stateful partition if patching a test image.
110 no_enable: True to not install the tag file.
111 system_root: The path to the root of the file system. This must be left
112 as its default value except for unit testing.
Peter Shih2f1f8c42017-06-15 15:05:55 +0800113 apps: The list of apps to enable/disable under factory/init/main.d/.
114 active_test_list: The id of active test list for Goofy.
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800115 """
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800116
Jon Salzb7e44262014-05-07 15:53:37 +0800117 # Whether to sudo when rsyncing; set to False for testing.
118 _sudo = True
119
Hung-Te Linfc162e62017-09-21 00:45:04 +0800120 def __init__(self, src, dest, no_enable, non_cros=False, system_root='/',
Peter Shih2f1f8c42017-06-15 15:05:55 +0800121 apps=None, active_test_list=None):
Jon Salz4f3ade52014-02-20 17:55:09 +0800122 self._src = src
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800123 self._system_root = system_root
124 if dest == self._system_root:
125 self._usr_local_dest = os.path.join(dest, 'usr', 'local')
Jon Salz4f3ade52014-02-20 17:55:09 +0800126
127 # Make sure we're on a CrOS device.
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +0800128 if not non_cros and not sys_utils.InCrOSDevice():
Jon Salz4f3ade52014-02-20 17:55:09 +0800129 sys.stderr.write(
Vic Yang196d5242014-08-05 13:51:35 +0800130 "ERROR: You're not on a CrOS device (for more details, please\n"
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +0800131 'check sys_utils.py:InCrOSDevice), so you must specify a test\n'
Hung-Te Lin56b18402015-01-16 14:52:30 +0800132 'image or a mounted stateful partition on which to install the\n'
133 'factory toolkit. Please run\n'
134 '\n'
135 ' install_factory_toolkit.run -- --help\n'
136 '\n'
Vic Yang70fdae92015-02-17 19:21:08 -0800137 'for help.\n'
138 '\n'
Hung-Te Linfc162e62017-09-21 00:45:04 +0800139 'If you want to install on a non-CrOS host,\n'
Vic Yang70fdae92015-02-17 19:21:08 -0800140 'please run\n'
141 '\n'
Hung-Te Linfc162e62017-09-21 00:45:04 +0800142 ' install_factory_toolkit.run -- --non-cros \n'
Vic Yang70fdae92015-02-17 19:21:08 -0800143 '\n')
Jon Salz4f3ade52014-02-20 17:55:09 +0800144 sys.exit(1)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800145 if os.getuid() != 0:
Jon Salz4f3ade52014-02-20 17:55:09 +0800146 raise Exception('You must be root to install the factory toolkit on a '
147 'CrOS device.')
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800148 else:
149 self._usr_local_dest = os.path.join(dest, 'dev_image')
Hung-Te Lin4aeabe62016-10-21 15:45:20 +0800150 if not os.path.exists(self._usr_local_dest):
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800151 raise Exception(
152 'The destination path %s is not a stateful partition!' % dest)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800153
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800154 self._dest = dest
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800155 self._usr_local_src = os.path.join(src, 'usr', 'local')
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800156 self._no_enable = no_enable
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800157 self._tag_file = os.path.join(self._usr_local_dest, 'factory', 'enabled')
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800158
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800159 self._apps = apps
Peter Shih2f1f8c42017-06-15 15:05:55 +0800160 self._active_test_list = active_test_list
Vic Yang7039f422014-07-07 15:38:13 -0700161
Hung-Te Lin4aeabe62016-10-21 15:45:20 +0800162 if not os.path.exists(self._usr_local_src):
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800163 raise Exception(
164 'This installer must be run from within the factory toolkit!')
165
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800166 def WarningMessage(self, target_test_image=None):
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +0800167 ret = file_utils.ReadFile(os.path.join(self._src, VERSION_PATH))
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800168 if target_test_image:
Jon Salz4f3ade52014-02-20 17:55:09 +0800169 ret += (
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800170 '\n'
171 '\n'
Jon Salz4f3ade52014-02-20 17:55:09 +0800172 '*** You are about to patch the factory toolkit into:\n'
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800173 '*** %s\n'
174 '***' % target_test_image)
175 else:
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 install the factory toolkit to:\n'
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800180 '*** %s\n'
181 '***' % self._dest)
182 if self._dest == self._system_root:
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800183 if self._no_enable:
Hung-Te Lin7b596ff2015-01-16 20:19:15 +0800184 ret += ('\n*** Factory tests will be disabled after this process is '
Hung-Te Lin56b18402015-01-16 14:52:30 +0800185 'done, but\n*** you can enable them by creating the factory '
186 'enabled tag:\n*** %s\n***' % self._tag_file)
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800187 else:
Hung-Te Lin7b596ff2015-01-16 20:19:15 +0800188 ret += ('\n*** After this process is done, your device will start '
Hung-Te Lin56b18402015-01-16 14:52:30 +0800189 'factory\n*** tests on the next reboot.\n***\n*** Factory '
190 'tests can be disabled by deleting the factory enabled\n*** '
191 'tag:\n*** %s\n***' % self._tag_file)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800192 return ret
193
Vic Yang7039f422014-07-07 15:38:13 -0700194 def _SetTagFile(self, name, path, enabled):
195 """Install or remove a tag file."""
196 if enabled:
Yilin Yang71e39412019-09-24 09:26:46 +0800197 print('*** Installing %s enabled tag...' % name)
Youcheng Syuac391772017-04-20 09:08:58 +0000198 Spawn(['touch', path], sudo=True, log=True, check_call=True)
199 Spawn(['chmod', 'go+r', path], sudo=True, log=True, check_call=True)
Vic Yang7039f422014-07-07 15:38:13 -0700200 else:
Yilin Yang71e39412019-09-24 09:26:46 +0800201 print('*** Removing %s enabled tag...' % name)
Youcheng Syuac391772017-04-20 09:08:58 +0000202 Spawn(['rm', '-f', path], sudo=True, log=True, check_call=True)
Vic Yang7039f422014-07-07 15:38:13 -0700203
Peter Shih2f1f8c42017-06-15 15:05:55 +0800204 def _SetActiveTestList(self):
205 """Set the active test list for Goofy."""
206 if self._active_test_list is not None:
Cheng-Han Yang5242a1f2019-01-16 20:00:05 +0800207 path = os.path.join(self._usr_local_dest, 'factory',
Cheng-Han Yang3f746e82019-04-10 14:33:29 +0800208 test_list_common.ACTIVE_TEST_LIST_CONFIG_RELPATH)
209 json_utils.DumpFile(
210 path,
211 test_list_common.GenerateActiveTestListConfig(self._active_test_list))
Peter Shih2f1f8c42017-06-15 15:05:55 +0800212
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800213 def _EnableApp(self, app, enabled):
214 """Enable / disable @app.
215
216 In factory/init/startup, a main app is considered disabled if and only:
217 1. file "factory/init/main.d/disable-@app" exists OR
218 2. file "factory/init/main.d/enable-@app" doesn't exist AND
219 file "factory/init/main.d/@app.sh" is not executable.
220
221 Therefore, we enable an app by removing file "disable-@app" and creating
You-Cheng Syu70e99ad2017-03-09 17:15:00 +0800222 file "enable-@app", and vice versa.
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800223 """
224 app_enable = os.path.join(self._usr_local_dest,
225 'factory', 'init', 'main.d', 'enable-' + app)
226 app_disable = os.path.join(self._usr_local_dest,
227 'factory', 'init', 'main.d', 'disable-' + app)
228 if enabled:
Yilin Yang71e39412019-09-24 09:26:46 +0800229 print('*** Enabling {app} ***'.format(app=app))
Youcheng Syuac391772017-04-20 09:08:58 +0000230 Spawn(['rm', '-f', app_disable], sudo=self._sudo, log=True,
231 check_call=True)
232 Spawn(['touch', app_enable], sudo=self._sudo, log=True, check_call=True)
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800233 else:
Yilin Yang71e39412019-09-24 09:26:46 +0800234 print('*** Disabling {app} ***'.format(app=app))
Youcheng Syuac391772017-04-20 09:08:58 +0000235 Spawn(['touch', app_disable], sudo=self._sudo, log=True, check_call=True)
236 Spawn(['rm', '-f', app_enable], sudo=self._sudo, log=True,
237 check_call=True)
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800238
239 def _EnableApps(self):
240 if not self._apps:
241 return
242
243 app_list = []
244 for app in self._apps:
245 if app[0] == '+':
246 app_list.append((app[1:], True))
247 elif app[0] == '-':
248 app_list.append((app[1:], False))
249 else:
250 raise ValueError(
251 'Use +{app} to enable and -{app} to disable'.format(app=app))
252
253 for app, enabled in app_list:
254 self._EnableApp(app, enabled)
255
Wei-Han Chen7137dcf2016-08-03 15:43:22 +0800256 def InstallFactorySubDir(self, sub_dirs):
257 """Install only the specified directories under factory folder."""
258
259 def _InstallOneSubDir(sub_dir_name):
260 sub_dir_dest = os.path.join(self._usr_local_dest, 'factory', sub_dir_name)
261 sub_dir_src = os.path.join(self._src, 'usr', 'local', 'factory',
262 sub_dir_name)
263 try:
Youcheng Syuac391772017-04-20 09:08:58 +0000264 Spawn(['mkdir', '-p', sub_dir_dest], sudo=True, log=True,
265 check_call=True)
Wei-Han Chen7137dcf2016-08-03 15:43:22 +0800266 except OSError as e:
Yilin Yang71e39412019-09-24 09:26:46 +0800267 print(str(e))
Wei-Han Chen7137dcf2016-08-03 15:43:22 +0800268 return
269
Youcheng Syuac391772017-04-20 09:08:58 +0000270 Spawn(['rsync', '-a', '--force', '-v',
271 sub_dir_src + '/', sub_dir_dest],
272 sudo=self._sudo, log=True, check_call=True)
273 Spawn(['chown', '-R', 'root', sub_dir_dest],
274 sudo=self._sudo, log=True, check_call=True)
275 Spawn(['chmod', '-R', 'go+rX', sub_dir_dest],
276 sudo=self._sudo, log=True, check_call=True)
Wei-Han Chen7137dcf2016-08-03 15:43:22 +0800277
278 for sub_dir_name in sub_dirs:
279 _InstallOneSubDir(sub_dir_name)
280
281 self._SetTagFile('factory', self._tag_file, not self._no_enable)
282 self._EnableApps()
283
Yilin Yangc7bcdc42020-04-08 10:50:22 +0800284 def InstallPy3Modules(self):
285 if sys_utils.InChroot():
286 return
287
288 misc_dir = os.path.join(self._usr_local_dest, 'factory/misc/')
289 ws4py_dir = os.path.join(misc_dir, 'ws4py-0.5.1')
290 Spawn(['python3', 'setup.py', 'install'], sudo=self._sudo, log=True,
291 check_call=True, cwd=ws4py_dir)
292
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800293 def Install(self):
Yilin Yang71e39412019-09-24 09:26:46 +0800294 print('*** Installing factory toolkit...')
Mao Huangf75aa3e2016-11-24 11:44:17 +0800295
296 # --no-owner and --no-group will set owner/group to the current user/group
297 # running the command. This is important if we're running with sudo, so
298 # the destination will be changed to root/root instead of the user/group
299 # before sudo (doesn't matter if sudo is not present). --force is also
300 # necessary to allow goofy directory from prior toolkit installations to
301 # be overwritten by the goofy symlink.
Yilin Yang71e39412019-09-24 09:26:46 +0800302 print('*** %s -> %s' % (self._usr_local_src, self._usr_local_dest))
Youcheng Syuac391772017-04-20 09:08:58 +0000303 Spawn(['rsync', '-a', '--no-owner', '--no-group', '--chmod=ugo+rX',
304 '--force'] + SERVER_FILE_MASK + [self._usr_local_src + '/',
305 self._usr_local_dest],
306 sudo=self._sudo, log=True, check_output=True, cwd=self._usr_local_src)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800307
Yilin Yang71e39412019-09-24 09:26:46 +0800308 print('*** Ensure SSH keys file permission...')
Hung-Te Lineb7632b2016-07-29 15:38:34 +0800309 sshkeys_dir = os.path.join(self._usr_local_dest, 'factory/misc/sshkeys')
310 sshkeys = glob.glob(os.path.join(sshkeys_dir, '*'))
311 ssh_public_keys = glob.glob(os.path.join(sshkeys_dir, '*.pub'))
312 ssh_private_keys = list(set(sshkeys) - set(ssh_public_keys))
313 if ssh_private_keys:
Youcheng Syuac391772017-04-20 09:08:58 +0000314 Spawn(['chmod', '600'] + ssh_private_keys, log=True, check_call=True,
315 sudo=self._sudo)
Hung-Te Lineb7632b2016-07-29 15:38:34 +0800316
Yilin Yang71e39412019-09-24 09:26:46 +0800317 print('*** Installing symlinks...')
Jon Salz25590302014-07-11 16:07:20 +0800318 install_symlinks.InstallSymlinks(
319 '../factory/bin',
320 os.path.join(self._usr_local_dest, 'bin'),
321 install_symlinks.MODE_FULL,
322 sudo=self._sudo)
323
Yilin Yangc7bcdc42020-04-08 10:50:22 +0800324 print('*** Installing python3 modules...')
325 self.InstallPy3Modules()
326
Vic Yang7039f422014-07-07 15:38:13 -0700327 self._SetTagFile('factory', self._tag_file, not self._no_enable)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800328
Peter Shih2f1f8c42017-06-15 15:05:55 +0800329 self._SetActiveTestList()
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800330 self._EnableApps()
Wei-Ning Huang5135b7e2015-07-03 17:31:15 +0800331
Yilin Yang71e39412019-09-24 09:26:46 +0800332 print('*** Installation completed.')
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800333
334
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800335@contextmanager
336def DummyContext(arg):
337 """A context manager that simply yields its argument."""
338 yield arg
339
340
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800341def PrintBuildInfo(src_root):
342 """Print build information."""
343 info_file = os.path.join(src_root, 'REPO_STATUS')
344 if not os.path.exists(info_file):
345 raise OSError('Build info file not found!')
Yilin Yang71e39412019-09-24 09:26:46 +0800346 print(file_utils.ReadFile(info_file))
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800347
348
Hung-Te Linfc162e62017-09-21 00:45:04 +0800349def PackFactoryToolkit(src_root, output_path, initial_version):
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800350 """Packs the files containing this script into a factory toolkit."""
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +0800351 if initial_version is None:
352 complete_version = '%s repacked by %s@%s at %s\n' % (
353 file_utils.ReadFile(os.path.join(src_root, VERSION_PATH)),
354 getpass.getuser(), os.uname()[1], time.strftime('%Y-%m-%d %H:%M:%S'))
355 initial_version = complete_version.splitlines()[0]
356 else:
357 complete_version = initial_version + '\n'
358 modified_times = len(complete_version.splitlines()) - 1
359 if modified_times == 0:
360 modified_msg = ''
361 else:
362 modified_msg = ' (modified %d times)' % modified_times
Yilin Yang09312c52019-12-04 13:50:35 +0800363 with tempfile.NamedTemporaryFile('w') as help_header:
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +0800364 help_header.write(initial_version + '\n' +
365 HELP_HEADER + HELP_HEADER_MAKESELF)
Jon Salz4f3ade52014-02-20 17:55:09 +0800366 help_header.flush()
Wei-Ning Huangb723d7f2014-11-17 17:26:32 +0800367 cmd = [os.path.join(src_root, 'makeself.sh'), '--bzip2', '--nox11',
Jon Salz4f3ade52014-02-20 17:55:09 +0800368 '--help-header', help_header.name,
Meng-Huan Yuea9dd912019-08-07 17:45:17 +0800369 src_root, # archive_dir
370 output_path, # file_name
371 initial_version + modified_msg, # label
372 # startup script and args
373 # We have to explicitly execute python instead of directly execute
374 # INSTALLER_PATH because files under INSTALLER_PATH may not be
375 # executable.
Wei-Han Chen1efb9142019-10-03 17:42:01 +0800376 'env', 'PYTHONPATH=' + PYTHONPATH,
Yilin Yang19da6932019-12-10 13:39:28 +0800377 'python3', '-m', INSTALLER_MODULE, '--in-exe']
Youcheng Syuac391772017-04-20 09:08:58 +0000378 Spawn(cmd, check_call=True, log=True)
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +0800379 with file_utils.TempDirectory() as tmp_dir:
380 version_path = os.path.join(tmp_dir, VERSION_PATH)
381 os.makedirs(os.path.dirname(version_path))
382 file_utils.WriteFile(version_path, complete_version)
You-Cheng Syuc0682732017-05-10 12:17:05 +0800383 Spawn([cmd[0], '--lsm', version_path, '--append', tmp_dir, output_path],
384 check_call=True, log=True)
Yilin Yang71e39412019-09-24 09:26:46 +0800385 print('\n'
386 ' Factory toolkit generated at %s.\n'
387 '\n'
388 ' To install factory toolkit on a live device running a test image,\n'
389 ' copy this to the device and execute it as root.\n'
390 '\n'
391 ' Alternatively, the factory toolkit can be used to patch a test\n'
392 ' image. For more information, run:\n'
393 ' %s --help\n'
394 '\n' % (output_path, output_path))
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800395
396
Wei-Ning Huang4855e792015-06-11 15:33:39 +0800397def ExtractOverlord(src_root, output_dir):
398 output_dir = os.path.join(output_dir, 'overlord')
399 try:
400 os.makedirs(output_dir)
401 except OSError as e:
Yilin Yang71e39412019-09-24 09:26:46 +0800402 print(str(e))
Wei-Ning Huang4855e792015-06-11 15:33:39 +0800403 return
404
405 # Copy overlord binary and resource files
406 shutil.copyfile(os.path.join(src_root, 'usr/bin/overlordd'),
407 os.path.join(output_dir, 'overlordd'))
408 shutil.copytree(os.path.join(src_root, 'usr/share/overlord/app'),
409 os.path.join(output_dir, 'app'))
410
411 # Give overlordd execution permission
Peter Shihe6afab32018-09-11 17:16:48 +0800412 os.chmod(os.path.join(output_dir, 'overlordd'), 0o755)
Yilin Yang71e39412019-09-24 09:26:46 +0800413 print("Extracted overlord under '%s'" % output_dir)
Wei-Ning Huang4855e792015-06-11 15:33:39 +0800414
415
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800416def main():
Jon Salz4f3ade52014-02-20 17:55:09 +0800417 import logging
418 logging.basicConfig(level=logging.INFO)
419
420 # In order to determine which usage message to show, first determine
421 # whether we're in the self-extracting archive. Do this first
422 # because we need it to even parse the arguments.
423 if '--in-exe' in sys.argv:
424 sys.argv = [x for x in sys.argv if x != '--in-exe']
425 in_archive = True
426 else:
427 in_archive = False
428
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800429 parser = argparse.ArgumentParser(
Jon Salz4f3ade52014-02-20 17:55:09 +0800430 description=HELP_HEADER + HELP_HEADER_ADVANCED,
431 usage=('install_factory_toolkit.run -- [options]' if in_archive
432 else None),
433 formatter_class=argparse.RawDescriptionHelpFormatter)
Hung-Te Lin56b18402015-01-16 14:52:30 +0800434 parser.add_argument(
435 'dest', nargs='?', default='/',
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800436 help='A test image or the mount point of the stateful partition. '
437 "If omitted, install to live system, i.e. '/'.")
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800438 parser.add_argument('--no-enable', '-n', action='store_true',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800439 help="Don't enable factory tests after installing")
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800440 parser.add_argument('--yes', '-y', action='store_true',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800441 help="Don't ask for confirmation")
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800442 parser.add_argument('--build-info', action='store_true',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800443 help='Print build information and exit')
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800444 parser.add_argument('--pack-into', metavar='NEW_TOOLKIT',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800445 help='Pack the files into a new factory toolkit')
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800446 parser.add_argument('--repack', metavar='UNPACKED_TOOLKIT',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800447 help='Repack from previously unpacked toolkit')
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +0800448 parser.add_argument('--version', metavar='VERSION',
449 help='String to write into TOOLKIT_VERSION when packing')
Vic Yang7039f422014-07-07 15:38:13 -0700450
Vic Yang70fdae92015-02-17 19:21:08 -0800451 parser.add_argument('--non-cros', dest='non_cros',
452 action='store_true',
453 help='Install on non-ChromeOS host.')
454
Vic Yang423c2722014-09-24 16:05:06 +0800455
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 Chen7137dcf2016-08-03 15:43:22 +0800462 parser.add_argument('--install-dirs', nargs='+', default=None,
463 help=('Install only the specified directories under '
464 'factory folder. Can be used with --apps to '
465 'enable / disable some apps. Defaults to install '
466 'all folders.'))
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800467 parser.add_argument('--apps', type=lambda s: s.split(','), default=None,
468 help=('Enable or disable some apps under '
469 'factory/init/main.d/. Use prefix "-" to disable, '
You-Cheng Syu70e99ad2017-03-09 17:15:00 +0800470 'prefix "+" to enable, and use "," to separate. '
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800471 'For example: --apps="-goofy,+whale_servo"'))
Peter Shih2f1f8c42017-06-15 15:05:55 +0800472 parser.add_argument('--active-test-list', dest='active_test_list',
473 default=None,
474 help='Set the id of active test list for Goofy.')
Vic Yang7039f422014-07-07 15:38:13 -0700475
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800476 args = parser.parse_args()
477
Peter Shihad166772017-05-31 11:36:17 +0800478 src_root = paths.FACTORY_DIR
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800479 for _ in xrange(3):
480 src_root = os.path.dirname(src_root)
481
Wei-Ning Huang4855e792015-06-11 15:33:39 +0800482 if args.extract_overlord is not None:
483 ExtractOverlord(src_root, args.extract_overlord)
484 return
485
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800486 # --pack-into may be called directly so this must be done before changing
487 # working directory to OLDPWD.
488 if args.pack_into and args.repack is None:
Hung-Te Linfc162e62017-09-21 00:45:04 +0800489 PackFactoryToolkit(src_root, args.pack_into, args.version)
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800490 return
491
Jon Salz4f3ade52014-02-20 17:55:09 +0800492 if not in_archive:
493 # If you're not in the self-extracting archive, you're not allowed to
494 # do anything except the above --pack-into call.
495 parser.error('Not running from install_factory_toolkit.run; '
496 'only --pack-into (without --repack) is allowed')
497
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800498 # Change to original working directory in case the user specifies
499 # a relative path.
500 # TODO: Use USER_PWD instead when makeself is upgraded
501 os.chdir(os.environ['OLDPWD'])
502
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800503 if args.repack:
504 if args.pack_into is None:
505 parser.error('Must specify --pack-into when using --repack.')
Wei-Han Chen1efb9142019-10-03 17:42:01 +0800506 env = dict(os.environ,
507 PYTHONPATH=os.path.join(args.repack, PYTHONPATH))
Yilin Yang19da6932019-12-10 13:39:28 +0800508 Spawn(['python3', '-m', INSTALLER_MODULE, '--pack-into', args.pack_into],
Wei-Han Chen1efb9142019-10-03 17:42:01 +0800509 check_call=True, log=True, env=env)
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800510 return
511
512 if args.build_info:
513 PrintBuildInfo(src_root)
514 return
515
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800516 if not os.path.exists(args.dest):
517 parser.error('Destination %s does not exist!' % args.dest)
518
519 patch_test_image = os.path.isfile(args.dest)
520
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +0800521 with (sys_utils.MountPartition(args.dest, 1, rw=True) if patch_test_image
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800522 else DummyContext(args.dest)) as dest:
Wei-Han Chen7137dcf2016-08-03 15:43:22 +0800523
Hung-Te Lin56b18402015-01-16 14:52:30 +0800524 installer = FactoryToolkitInstaller(
Wei-Ning Huang5135b7e2015-07-03 17:31:15 +0800525 src=src_root, dest=dest, no_enable=args.no_enable,
Hung-Te Linfc162e62017-09-21 00:45:04 +0800526 non_cros=args.non_cros, apps=args.apps,
527 active_test_list=args.active_test_list)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800528
Yilin Yang71e39412019-09-24 09:26:46 +0800529 print(installer.WarningMessage(args.dest if patch_test_image else None))
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800530
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800531 if not args.yes:
Yilin Yang8cc5dfb2019-10-22 15:58:53 +0800532 answer = input('*** Continue? [y/N] ')
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800533 if not answer or answer[0] not in 'yY':
534 sys.exit('Aborting.')
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800535
Wei-Han Chen7137dcf2016-08-03 15:43:22 +0800536 if args.install_dirs:
537 installer.InstallFactorySubDir(args.install_dirs)
538 else:
539 installer.Install()
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800540
541if __name__ == '__main__':
You-Cheng Syu70e99ad2017-03-09 17:15:00 +0800542 # makself interprets "LICENSE" environment variable string as license text and
Hung-Te Lin5a2a1c42016-11-10 12:43:40 +0800543 # will prompt user to accept before installation. For factory toolkit, we
544 # don't want any user interaction in installation and the license is already
545 # covered by ebuild or download platform like CPFE.
546 os.putenv('LICENSE', '')
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800547 main()