blob: b39c24f96d035b1abe4dfcb75c6e8c341d704399 [file] [log] [blame]
Mike Frysinger63bb3c72019-09-01 15:16:26 -04001#!/usr/bin/env python2
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
27
You-Cheng Syu70e99ad2017-03-09 17:15:00 +080028import factory_common # pylint: disable=unused-import
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
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +080038INSTALLER_PATH = 'usr/local/factory/py/toolkit/installer.py'
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +080039VERSION_PATH = 'usr/local/factory/TOOLKIT_VERSION'
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +080040
Jon Salz4f3ade52014-02-20 17:55:09 +080041# Short and sweet help header for the executable generated by makeself.
42HELP_HEADER = """
43Installs the factory toolkit, transforming a test image into a factory test
44image. You can:
45
46- Install the factory toolkit on a CrOS device that is running a test
47 image. To do this, copy install_factory_toolkit.run to the device and
48 run it. The factory tests will then come up on the next boot.
49
50 rsync -a install_factory_toolkit.run crosdevice:/tmp
51 ssh crosdevice '/tmp/install_factory_toolkit.run && sync && reboot'
52
53- Modify a test image, turning it into a factory test image. When you
54 use the image on a device, the factory tests will come up.
55
56 install_factory_toolkit.run chromiumos_test_image.bin
57"""
58
59HELP_HEADER_ADVANCED = """
60- (advanced) Modify a mounted stateful partition, turning it into a factory
61 test image. This is equivalent to the previous command:
62
63 mount_partition -rw chromiumos_test_image.bin 1 /mnt/stateful
64 install_factory_toolkit.run /mnt/stateful
65 umount /mnt/stateful
66
67- (advanced) Unpack the factory toolkit, modify a file, and then repack it.
68
69 # Unpack but don't actually install
70 install_factory_toolkit.run --target /tmp/toolkit --noexec
71 # Edit some files in /tmp/toolkit
72 emacs /tmp/toolkit/whatever
73 # Repack
74 install_factory_toolkit.run -- --repack /tmp/toolkit \\
75 --pack-into /path/to/new/install_factory_toolkit.run
76"""
77
78# The makeself-generated header comes next. This is a little confusing,
79# so explain.
80HELP_HEADER_MAKESELF = """
81For complete usage information and advanced operations, run
82"install_factory_toolkit.run -- --help" (note the extra "--").
83
84Following is the help message from makeself, which was used to create
85this self-extracting archive.
86
87-----
88"""
89
Vic Yangdb1e20e2014-10-05 12:10:33 +080090SERVER_FILE_MASK = [
91 # Exclude Umpire server but keep Umpire client
92 '--include', 'py/umpire/__init__.*',
93 '--include', 'py/umpire/common.*',
94 '--include', 'py/umpire/client',
95 '--include', 'py/umpire/client/**',
96 '--exclude', 'py/umpire/**',
Peter Shihac904782017-06-14 15:24:09 +080097 '--exclude', 'bin/umpire',
98 '--exclude', 'bin/umpired',
Vic Yangdb1e20e2014-10-05 12:10:33 +080099]
100
Jon Salz4f3ade52014-02-20 17:55:09 +0800101
Hung-Te Lin7b596ff2015-01-16 20:19:15 +0800102class FactoryToolkitInstaller(object):
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800103 """Factory toolkit installer.
104
105 Args:
106 src: Source path containing usr/ and var/.
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800107 dest: Installation destination path. Set this to the mount point of the
108 stateful partition if patching a test image.
109 no_enable: True to not install the tag file.
110 system_root: The path to the root of the file system. This must be left
111 as its default value except for unit testing.
Peter Shih2f1f8c42017-06-15 15:05:55 +0800112 apps: The list of apps to enable/disable under factory/init/main.d/.
113 active_test_list: The id of active test list for Goofy.
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800114 """
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800115
Jon Salzb7e44262014-05-07 15:53:37 +0800116 # Whether to sudo when rsyncing; set to False for testing.
117 _sudo = True
118
Hung-Te Linfc162e62017-09-21 00:45:04 +0800119 def __init__(self, src, dest, no_enable, non_cros=False, system_root='/',
Peter Shih2f1f8c42017-06-15 15:05:55 +0800120 apps=None, active_test_list=None):
Jon Salz4f3ade52014-02-20 17:55:09 +0800121 self._src = src
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800122 self._system_root = system_root
123 if dest == self._system_root:
124 self._usr_local_dest = os.path.join(dest, 'usr', 'local')
Jon Salz4f3ade52014-02-20 17:55:09 +0800125
126 # Make sure we're on a CrOS device.
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +0800127 if not non_cros and not sys_utils.InCrOSDevice():
Jon Salz4f3ade52014-02-20 17:55:09 +0800128 sys.stderr.write(
Vic Yang196d5242014-08-05 13:51:35 +0800129 "ERROR: You're not on a CrOS device (for more details, please\n"
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +0800130 'check sys_utils.py:InCrOSDevice), so you must specify a test\n'
Hung-Te Lin56b18402015-01-16 14:52:30 +0800131 'image or a mounted stateful partition on which to install the\n'
132 'factory toolkit. Please run\n'
133 '\n'
134 ' install_factory_toolkit.run -- --help\n'
135 '\n'
Vic Yang70fdae92015-02-17 19:21:08 -0800136 'for help.\n'
137 '\n'
Hung-Te Linfc162e62017-09-21 00:45:04 +0800138 'If you want to install on a non-CrOS host,\n'
Vic Yang70fdae92015-02-17 19:21:08 -0800139 'please run\n'
140 '\n'
Hung-Te Linfc162e62017-09-21 00:45:04 +0800141 ' install_factory_toolkit.run -- --non-cros \n'
Vic Yang70fdae92015-02-17 19:21:08 -0800142 '\n')
Jon Salz4f3ade52014-02-20 17:55:09 +0800143 sys.exit(1)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800144 if os.getuid() != 0:
Jon Salz4f3ade52014-02-20 17:55:09 +0800145 raise Exception('You must be root to install the factory toolkit on a '
146 'CrOS device.')
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800147 else:
148 self._usr_local_dest = os.path.join(dest, 'dev_image')
Hung-Te Lin4aeabe62016-10-21 15:45:20 +0800149 if not os.path.exists(self._usr_local_dest):
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800150 raise Exception(
151 'The destination path %s is not a stateful partition!' % dest)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800152
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800153 self._dest = dest
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800154 self._usr_local_src = os.path.join(src, 'usr', 'local')
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800155 self._no_enable = no_enable
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800156 self._tag_file = os.path.join(self._usr_local_dest, 'factory', 'enabled')
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800157
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800158 self._apps = apps
Peter Shih2f1f8c42017-06-15 15:05:55 +0800159 self._active_test_list = active_test_list
Vic Yang7039f422014-07-07 15:38:13 -0700160
Hung-Te Lin4aeabe62016-10-21 15:45:20 +0800161 if not os.path.exists(self._usr_local_src):
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800162 raise Exception(
163 'This installer must be run from within the factory toolkit!')
164
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800165 def WarningMessage(self, target_test_image=None):
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +0800166 ret = file_utils.ReadFile(os.path.join(self._src, VERSION_PATH))
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800167 if target_test_image:
Jon Salz4f3ade52014-02-20 17:55:09 +0800168 ret += (
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800169 '\n'
170 '\n'
Jon Salz4f3ade52014-02-20 17:55:09 +0800171 '*** You are about to patch the factory toolkit into:\n'
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800172 '*** %s\n'
173 '***' % target_test_image)
174 else:
Jon Salz4f3ade52014-02-20 17:55:09 +0800175 ret += (
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800176 '\n'
177 '\n'
Jon Salz4f3ade52014-02-20 17:55:09 +0800178 '*** You are about to install the factory toolkit to:\n'
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800179 '*** %s\n'
180 '***' % self._dest)
181 if self._dest == self._system_root:
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800182 if self._no_enable:
Hung-Te Lin7b596ff2015-01-16 20:19:15 +0800183 ret += ('\n*** Factory tests will be disabled after this process is '
Hung-Te Lin56b18402015-01-16 14:52:30 +0800184 'done, but\n*** you can enable them by creating the factory '
185 'enabled tag:\n*** %s\n***' % self._tag_file)
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800186 else:
Hung-Te Lin7b596ff2015-01-16 20:19:15 +0800187 ret += ('\n*** After this process is done, your device will start '
Hung-Te Lin56b18402015-01-16 14:52:30 +0800188 'factory\n*** tests on the next reboot.\n***\n*** Factory '
189 'tests can be disabled by deleting the factory enabled\n*** '
190 'tag:\n*** %s\n***' % self._tag_file)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800191 return ret
192
Vic Yang7039f422014-07-07 15:38:13 -0700193 def _SetTagFile(self, name, path, enabled):
194 """Install or remove a tag file."""
195 if enabled:
Yilin Yang71e39412019-09-24 09:26:46 +0800196 print('*** Installing %s enabled tag...' % name)
Youcheng Syuac391772017-04-20 09:08:58 +0000197 Spawn(['touch', path], sudo=True, log=True, check_call=True)
198 Spawn(['chmod', 'go+r', path], sudo=True, log=True, check_call=True)
Vic Yang7039f422014-07-07 15:38:13 -0700199 else:
Yilin Yang71e39412019-09-24 09:26:46 +0800200 print('*** Removing %s enabled tag...' % name)
Youcheng Syuac391772017-04-20 09:08:58 +0000201 Spawn(['rm', '-f', path], sudo=True, log=True, check_call=True)
Vic Yang7039f422014-07-07 15:38:13 -0700202
Peter Shih2f1f8c42017-06-15 15:05:55 +0800203 def _SetActiveTestList(self):
204 """Set the active test list for Goofy."""
205 if self._active_test_list is not None:
Cheng-Han Yang5242a1f2019-01-16 20:00:05 +0800206 path = os.path.join(self._usr_local_dest, 'factory',
Cheng-Han Yang3f746e82019-04-10 14:33:29 +0800207 test_list_common.ACTIVE_TEST_LIST_CONFIG_RELPATH)
208 json_utils.DumpFile(
209 path,
210 test_list_common.GenerateActiveTestListConfig(self._active_test_list))
Peter Shih2f1f8c42017-06-15 15:05:55 +0800211
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800212 def _EnableApp(self, app, enabled):
213 """Enable / disable @app.
214
215 In factory/init/startup, a main app is considered disabled if and only:
216 1. file "factory/init/main.d/disable-@app" exists OR
217 2. file "factory/init/main.d/enable-@app" doesn't exist AND
218 file "factory/init/main.d/@app.sh" is not executable.
219
220 Therefore, we enable an app by removing file "disable-@app" and creating
You-Cheng Syu70e99ad2017-03-09 17:15:00 +0800221 file "enable-@app", and vice versa.
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800222 """
223 app_enable = os.path.join(self._usr_local_dest,
224 'factory', 'init', 'main.d', 'enable-' + app)
225 app_disable = os.path.join(self._usr_local_dest,
226 'factory', 'init', 'main.d', 'disable-' + app)
227 if enabled:
Yilin Yang71e39412019-09-24 09:26:46 +0800228 print('*** Enabling {app} ***'.format(app=app))
Youcheng Syuac391772017-04-20 09:08:58 +0000229 Spawn(['rm', '-f', app_disable], sudo=self._sudo, log=True,
230 check_call=True)
231 Spawn(['touch', app_enable], sudo=self._sudo, log=True, check_call=True)
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800232 else:
Yilin Yang71e39412019-09-24 09:26:46 +0800233 print('*** Disabling {app} ***'.format(app=app))
Youcheng Syuac391772017-04-20 09:08:58 +0000234 Spawn(['touch', app_disable], sudo=self._sudo, log=True, check_call=True)
235 Spawn(['rm', '-f', app_enable], sudo=self._sudo, log=True,
236 check_call=True)
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800237
238 def _EnableApps(self):
239 if not self._apps:
240 return
241
242 app_list = []
243 for app in self._apps:
244 if app[0] == '+':
245 app_list.append((app[1:], True))
246 elif app[0] == '-':
247 app_list.append((app[1:], False))
248 else:
249 raise ValueError(
250 'Use +{app} to enable and -{app} to disable'.format(app=app))
251
252 for app, enabled in app_list:
253 self._EnableApp(app, enabled)
254
Wei-Han Chen7137dcf2016-08-03 15:43:22 +0800255 def InstallFactorySubDir(self, sub_dirs):
256 """Install only the specified directories under factory folder."""
257
258 def _InstallOneSubDir(sub_dir_name):
259 sub_dir_dest = os.path.join(self._usr_local_dest, 'factory', sub_dir_name)
260 sub_dir_src = os.path.join(self._src, 'usr', 'local', 'factory',
261 sub_dir_name)
262 try:
Youcheng Syuac391772017-04-20 09:08:58 +0000263 Spawn(['mkdir', '-p', sub_dir_dest], sudo=True, log=True,
264 check_call=True)
Wei-Han Chen7137dcf2016-08-03 15:43:22 +0800265 except OSError as e:
Yilin Yang71e39412019-09-24 09:26:46 +0800266 print(str(e))
Wei-Han Chen7137dcf2016-08-03 15:43:22 +0800267 return
268
Youcheng Syuac391772017-04-20 09:08:58 +0000269 Spawn(['rsync', '-a', '--force', '-v',
270 sub_dir_src + '/', sub_dir_dest],
271 sudo=self._sudo, log=True, check_call=True)
272 Spawn(['chown', '-R', 'root', sub_dir_dest],
273 sudo=self._sudo, log=True, check_call=True)
274 Spawn(['chmod', '-R', 'go+rX', sub_dir_dest],
275 sudo=self._sudo, log=True, check_call=True)
Wei-Han Chen7137dcf2016-08-03 15:43:22 +0800276
277 for sub_dir_name in sub_dirs:
278 _InstallOneSubDir(sub_dir_name)
279
280 self._SetTagFile('factory', self._tag_file, not self._no_enable)
281 self._EnableApps()
282
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800283 def Install(self):
Yilin Yang71e39412019-09-24 09:26:46 +0800284 print('*** Installing factory toolkit...')
Mao Huangf75aa3e2016-11-24 11:44:17 +0800285
286 # --no-owner and --no-group will set owner/group to the current user/group
287 # running the command. This is important if we're running with sudo, so
288 # the destination will be changed to root/root instead of the user/group
289 # before sudo (doesn't matter if sudo is not present). --force is also
290 # necessary to allow goofy directory from prior toolkit installations to
291 # be overwritten by the goofy symlink.
Yilin Yang71e39412019-09-24 09:26:46 +0800292 print('*** %s -> %s' % (self._usr_local_src, self._usr_local_dest))
Youcheng Syuac391772017-04-20 09:08:58 +0000293 Spawn(['rsync', '-a', '--no-owner', '--no-group', '--chmod=ugo+rX',
294 '--force'] + SERVER_FILE_MASK + [self._usr_local_src + '/',
295 self._usr_local_dest],
296 sudo=self._sudo, log=True, check_output=True, cwd=self._usr_local_src)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800297
Yilin Yang71e39412019-09-24 09:26:46 +0800298 print('*** Ensure SSH keys file permission...')
Hung-Te Lineb7632b2016-07-29 15:38:34 +0800299 sshkeys_dir = os.path.join(self._usr_local_dest, 'factory/misc/sshkeys')
300 sshkeys = glob.glob(os.path.join(sshkeys_dir, '*'))
301 ssh_public_keys = glob.glob(os.path.join(sshkeys_dir, '*.pub'))
302 ssh_private_keys = list(set(sshkeys) - set(ssh_public_keys))
303 if ssh_private_keys:
Youcheng Syuac391772017-04-20 09:08:58 +0000304 Spawn(['chmod', '600'] + ssh_private_keys, log=True, check_call=True,
305 sudo=self._sudo)
Hung-Te Lineb7632b2016-07-29 15:38:34 +0800306
Yilin Yang71e39412019-09-24 09:26:46 +0800307 print('*** Installing symlinks...')
Jon Salz25590302014-07-11 16:07:20 +0800308 install_symlinks.InstallSymlinks(
309 '../factory/bin',
310 os.path.join(self._usr_local_dest, 'bin'),
311 install_symlinks.MODE_FULL,
312 sudo=self._sudo)
313
Vic Yang7039f422014-07-07 15:38:13 -0700314 self._SetTagFile('factory', self._tag_file, not self._no_enable)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800315
Peter Shih2f1f8c42017-06-15 15:05:55 +0800316 self._SetActiveTestList()
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800317 self._EnableApps()
Wei-Ning Huang5135b7e2015-07-03 17:31:15 +0800318
Yilin Yang71e39412019-09-24 09:26:46 +0800319 print('*** Installation completed.')
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800320
321
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800322@contextmanager
323def DummyContext(arg):
324 """A context manager that simply yields its argument."""
325 yield arg
326
327
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800328def PrintBuildInfo(src_root):
329 """Print build information."""
330 info_file = os.path.join(src_root, 'REPO_STATUS')
331 if not os.path.exists(info_file):
332 raise OSError('Build info file not found!')
Yilin Yang71e39412019-09-24 09:26:46 +0800333 print(file_utils.ReadFile(info_file))
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800334
335
Hung-Te Linfc162e62017-09-21 00:45:04 +0800336def PackFactoryToolkit(src_root, output_path, initial_version):
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800337 """Packs the files containing this script into a factory toolkit."""
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +0800338 if initial_version is None:
339 complete_version = '%s repacked by %s@%s at %s\n' % (
340 file_utils.ReadFile(os.path.join(src_root, VERSION_PATH)),
341 getpass.getuser(), os.uname()[1], time.strftime('%Y-%m-%d %H:%M:%S'))
342 initial_version = complete_version.splitlines()[0]
343 else:
344 complete_version = initial_version + '\n'
345 modified_times = len(complete_version.splitlines()) - 1
346 if modified_times == 0:
347 modified_msg = ''
348 else:
349 modified_msg = ' (modified %d times)' % modified_times
Jon Salz4f3ade52014-02-20 17:55:09 +0800350 with tempfile.NamedTemporaryFile() as help_header:
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +0800351 help_header.write(initial_version + '\n' +
352 HELP_HEADER + HELP_HEADER_MAKESELF)
Jon Salz4f3ade52014-02-20 17:55:09 +0800353 help_header.flush()
Wei-Ning Huangb723d7f2014-11-17 17:26:32 +0800354 cmd = [os.path.join(src_root, 'makeself.sh'), '--bzip2', '--nox11',
Jon Salz4f3ade52014-02-20 17:55:09 +0800355 '--help-header', help_header.name,
Meng-Huan Yuea9dd912019-08-07 17:45:17 +0800356 src_root, # archive_dir
357 output_path, # file_name
358 initial_version + modified_msg, # label
359 # startup script and args
360 # We have to explicitly execute python instead of directly execute
361 # INSTALLER_PATH because files under INSTALLER_PATH may not be
362 # executable.
363 'env', 'python', INSTALLER_PATH, '--in-exe']
Youcheng Syuac391772017-04-20 09:08:58 +0000364 Spawn(cmd, check_call=True, log=True)
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +0800365 with file_utils.TempDirectory() as tmp_dir:
366 version_path = os.path.join(tmp_dir, VERSION_PATH)
367 os.makedirs(os.path.dirname(version_path))
368 file_utils.WriteFile(version_path, complete_version)
You-Cheng Syuc0682732017-05-10 12:17:05 +0800369 Spawn([cmd[0], '--lsm', version_path, '--append', tmp_dir, output_path],
370 check_call=True, log=True)
Yilin Yang71e39412019-09-24 09:26:46 +0800371 print('\n'
372 ' Factory toolkit generated at %s.\n'
373 '\n'
374 ' To install factory toolkit on a live device running a test image,\n'
375 ' copy this to the device and execute it as root.\n'
376 '\n'
377 ' Alternatively, the factory toolkit can be used to patch a test\n'
378 ' image. For more information, run:\n'
379 ' %s --help\n'
380 '\n' % (output_path, output_path))
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800381
382
Wei-Ning Huang4855e792015-06-11 15:33:39 +0800383def ExtractOverlord(src_root, output_dir):
384 output_dir = os.path.join(output_dir, 'overlord')
385 try:
386 os.makedirs(output_dir)
387 except OSError as e:
Yilin Yang71e39412019-09-24 09:26:46 +0800388 print(str(e))
Wei-Ning Huang4855e792015-06-11 15:33:39 +0800389 return
390
391 # Copy overlord binary and resource files
392 shutil.copyfile(os.path.join(src_root, 'usr/bin/overlordd'),
393 os.path.join(output_dir, 'overlordd'))
394 shutil.copytree(os.path.join(src_root, 'usr/share/overlord/app'),
395 os.path.join(output_dir, 'app'))
396
397 # Give overlordd execution permission
Peter Shihe6afab32018-09-11 17:16:48 +0800398 os.chmod(os.path.join(output_dir, 'overlordd'), 0o755)
Yilin Yang71e39412019-09-24 09:26:46 +0800399 print("Extracted overlord under '%s'" % output_dir)
Wei-Ning Huang4855e792015-06-11 15:33:39 +0800400
401
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800402def main():
Jon Salz4f3ade52014-02-20 17:55:09 +0800403 import logging
404 logging.basicConfig(level=logging.INFO)
405
406 # In order to determine which usage message to show, first determine
407 # whether we're in the self-extracting archive. Do this first
408 # because we need it to even parse the arguments.
409 if '--in-exe' in sys.argv:
410 sys.argv = [x for x in sys.argv if x != '--in-exe']
411 in_archive = True
412 else:
413 in_archive = False
414
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800415 parser = argparse.ArgumentParser(
Jon Salz4f3ade52014-02-20 17:55:09 +0800416 description=HELP_HEADER + HELP_HEADER_ADVANCED,
417 usage=('install_factory_toolkit.run -- [options]' if in_archive
418 else None),
419 formatter_class=argparse.RawDescriptionHelpFormatter)
Hung-Te Lin56b18402015-01-16 14:52:30 +0800420 parser.add_argument(
421 'dest', nargs='?', default='/',
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800422 help='A test image or the mount point of the stateful partition. '
423 "If omitted, install to live system, i.e. '/'.")
Vic (Chun-Ju) Yang7cc3e672014-01-20 14:06:39 +0800424 parser.add_argument('--no-enable', '-n', action='store_true',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800425 help="Don't enable factory tests after installing")
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800426 parser.add_argument('--yes', '-y', action='store_true',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800427 help="Don't ask for confirmation")
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800428 parser.add_argument('--build-info', action='store_true',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800429 help='Print build information and exit')
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800430 parser.add_argument('--pack-into', metavar='NEW_TOOLKIT',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800431 help='Pack the files into a new factory toolkit')
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800432 parser.add_argument('--repack', metavar='UNPACKED_TOOLKIT',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800433 help='Repack from previously unpacked toolkit')
You-Cheng Syu53f4a0c2017-04-20 17:46:12 +0800434 parser.add_argument('--version', metavar='VERSION',
435 help='String to write into TOOLKIT_VERSION when packing')
Vic Yang7039f422014-07-07 15:38:13 -0700436
Vic Yang70fdae92015-02-17 19:21:08 -0800437 parser.add_argument('--non-cros', dest='non_cros',
438 action='store_true',
439 help='Install on non-ChromeOS host.')
440
Vic Yang423c2722014-09-24 16:05:06 +0800441
Rong Chang6d65fad2014-08-22 16:00:12 +0800442 parser.add_argument('--exe-path', dest='exe_path',
Hung-Te Lin56b18402015-01-16 14:52:30 +0800443 nargs='?', default=None,
444 help='Current self-extracting archive pathname')
Wei-Ning Huang4855e792015-06-11 15:33:39 +0800445 parser.add_argument('--extract-overlord', dest='extract_overlord',
446 metavar='OUTPUT_DIR', type=str, default=None,
447 help='Extract overlord from the toolkit')
Wei-Han Chen7137dcf2016-08-03 15:43:22 +0800448 parser.add_argument('--install-dirs', nargs='+', default=None,
449 help=('Install only the specified directories under '
450 'factory folder. Can be used with --apps to '
451 'enable / disable some apps. Defaults to install '
452 'all folders.'))
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800453 parser.add_argument('--apps', type=lambda s: s.split(','), default=None,
454 help=('Enable or disable some apps under '
455 'factory/init/main.d/. Use prefix "-" to disable, '
You-Cheng Syu70e99ad2017-03-09 17:15:00 +0800456 'prefix "+" to enable, and use "," to separate. '
Wei-Han Chene6fe3872016-06-30 14:29:06 +0800457 'For example: --apps="-goofy,+whale_servo"'))
Peter Shih2f1f8c42017-06-15 15:05:55 +0800458 parser.add_argument('--active-test-list', dest='active_test_list',
459 default=None,
460 help='Set the id of active test list for Goofy.')
Vic Yang7039f422014-07-07 15:38:13 -0700461
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800462 args = parser.parse_args()
463
Peter Shihad166772017-05-31 11:36:17 +0800464 src_root = paths.FACTORY_DIR
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800465 for _ in xrange(3):
466 src_root = os.path.dirname(src_root)
467
Wei-Ning Huang4855e792015-06-11 15:33:39 +0800468 if args.extract_overlord is not None:
469 ExtractOverlord(src_root, args.extract_overlord)
470 return
471
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800472 # --pack-into may be called directly so this must be done before changing
473 # working directory to OLDPWD.
474 if args.pack_into and args.repack is None:
Hung-Te Linfc162e62017-09-21 00:45:04 +0800475 PackFactoryToolkit(src_root, args.pack_into, args.version)
Vic (Chun-Ju) Yang98b4fbc2014-02-18 19:32:32 +0800476 return
477
Jon Salz4f3ade52014-02-20 17:55:09 +0800478 if not in_archive:
479 # If you're not in the self-extracting archive, you're not allowed to
480 # do anything except the above --pack-into call.
481 parser.error('Not running from install_factory_toolkit.run; '
482 'only --pack-into (without --repack) is allowed')
483
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800484 # Change to original working directory in case the user specifies
485 # a relative path.
486 # TODO: Use USER_PWD instead when makeself is upgraded
487 os.chdir(os.environ['OLDPWD'])
488
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800489 if args.repack:
490 if args.pack_into is None:
491 parser.error('Must specify --pack-into when using --repack.')
Youcheng Syuac391772017-04-20 09:08:58 +0000492 Spawn([os.path.join(args.repack, INSTALLER_PATH),
493 '--pack-into', args.pack_into], check_call=True, log=True)
Vic (Chun-Ju) Yangb7388f72014-02-19 15:22:58 +0800494 return
495
496 if args.build_info:
497 PrintBuildInfo(src_root)
498 return
499
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800500 if not os.path.exists(args.dest):
501 parser.error('Destination %s does not exist!' % args.dest)
502
503 patch_test_image = os.path.isfile(args.dest)
504
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +0800505 with (sys_utils.MountPartition(args.dest, 1, rw=True) if patch_test_image
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800506 else DummyContext(args.dest)) as dest:
Wei-Han Chen7137dcf2016-08-03 15:43:22 +0800507
Hung-Te Lin56b18402015-01-16 14:52:30 +0800508 installer = FactoryToolkitInstaller(
Wei-Ning Huang5135b7e2015-07-03 17:31:15 +0800509 src=src_root, dest=dest, no_enable=args.no_enable,
Hung-Te Linfc162e62017-09-21 00:45:04 +0800510 non_cros=args.non_cros, apps=args.apps,
511 active_test_list=args.active_test_list)
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800512
Yilin Yang71e39412019-09-24 09:26:46 +0800513 print(installer.WarningMessage(args.dest if patch_test_image else None))
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800514
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800515 if not args.yes:
Yilin Yang8cc5dfb2019-10-22 15:58:53 +0800516 answer = input('*** Continue? [y/N] ')
Vic (Chun-Ju) Yang469592b2014-02-18 19:15:41 +0800517 if not answer or answer[0] not in 'yY':
518 sys.exit('Aborting.')
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800519
Wei-Han Chen7137dcf2016-08-03 15:43:22 +0800520 if args.install_dirs:
521 installer.InstallFactorySubDir(args.install_dirs)
522 else:
523 installer.Install()
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800524
525if __name__ == '__main__':
You-Cheng Syu70e99ad2017-03-09 17:15:00 +0800526 # makself interprets "LICENSE" environment variable string as license text and
Hung-Te Lin5a2a1c42016-11-10 12:43:40 +0800527 # will prompt user to accept before installation. For factory toolkit, we
528 # don't want any user interaction in installation and the license is already
529 # covered by ebuild or download platform like CPFE.
530 os.putenv('LICENSE', '')
Vic (Chun-Ju) Yang296871a2014-01-13 12:05:18 +0800531 main()