OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """ |
| 7 This script runs inside chroot environment. It signs and build factory packages. |
| 8 Then serves them using devserver. All paths should be specified relative to the |
| 9 chroot environment. |
| 10 |
| 11 E.g.: ./enter_chroot.sh -- serve_factory_packages.py --board <board> |
| 12 |
| 13 Always precede the call to the script with './enter_chroot.sh -- ". |
| 14 """ |
| 15 |
| 16 import gflags |
| 17 import os |
| 18 import shlex |
| 19 import signal |
| 20 import subprocess |
| 21 import sys |
| 22 |
| 23 |
| 24 CWD = os.getcwd() |
| 25 USER = os.environ['USER'] |
| 26 HOME_DIR = '/home/%s/trunk/src' % USER |
| 27 SCRIPTS_DIR = HOME_DIR + '/scripts' |
| 28 DEVSERVER_DIR = HOME_DIR + '/platform/dev' |
| 29 |
| 30 # Paths to image signing directory and dev key. |
| 31 VBOOT_REF_DIR = HOME_DIR + '/platform/vboot_reference' |
| 32 IMG_SIGN_DIR = VBOOT_REF_DIR + '/scripts/image_signing' |
| 33 DEVKEYS = VBOOT_REF_DIR + '/tests/devkeys' |
| 34 |
| 35 FLAGS = gflags.FLAGS |
| 36 |
| 37 gflags.DEFINE_string('board', None, 'Platform to build.') |
| 38 gflags.DEFINE_string('base_image', None, 'Path to base image.') |
| 39 gflags.DEFINE_string('firmware_updater', None, 'Path to firmware updater.') |
| 40 gflags.DEFINE_boolean('start_devserver', False, 'Start devserver.') |
| 41 |
| 42 class KillableProcess(): |
| 43 """A killable process. |
| 44 """ |
| 45 |
| 46 running_process = None |
| 47 |
| 48 def __init__(self, cmd, timeout=60, cwd=CWD): |
| 49 """Initialize process |
| 50 |
| 51 Args: |
| 52 cmd: command to run. |
| 53 """ |
| 54 self.cmd = shlex.split(cmd) |
| 55 self.cmd_timeout = timeout |
| 56 self.cmd_cwd = cwd |
| 57 |
| 58 def start(self, wait=True): |
| 59 """Start the process. |
| 60 |
| 61 Args: |
| 62 wait: wait for command to complete. |
| 63 """ |
| 64 self.running_process = subprocess.Popen(self.cmd, |
| 65 cwd=self.cmd_cwd) |
| 66 if wait: |
| 67 self.running_process.wait() |
| 68 |
| 69 def stop(self): |
| 70 """Stop the process. |
| 71 |
| 72 This will only work for commands that do not exit. |
| 73 """ |
| 74 self.running_process.send_signal(signal.SIGINT) |
| 75 self.running_process.wait() |
| 76 |
| 77 |
| 78 def start_devserver(): |
| 79 """Starts devserver.""" |
| 80 cmd = 'python devserver.py --factory_config miniomaha.conf' |
| 81 print 'Running command: %s' % cmd |
| 82 devserver_process = KillableProcess(cmd, cwd=DEVSERVER_DIR) |
| 83 devserver_process.start(wait=False) |
| 84 |
| 85 |
| 86 def assert_is_file(path, message): |
| 87 """Assert file exists. |
| 88 |
| 89 Args: |
| 90 path: path to file. |
| 91 message: message to print if file does not exist. |
| 92 """ |
| 93 if not os.path.isfile(path): |
| 94 error_message = '%s: %s is not a file!' % (message, path) |
| 95 print error_message |
| 96 sys.exit(1) |
| 97 |
| 98 |
| 99 def setup_board(board): |
| 100 """Setup the board inside chroot. |
| 101 """ |
| 102 cmd = './setup_board --board %s' % board |
| 103 print 'Setting up board: %s' % board |
| 104 setup_board_process = KillableProcess(cmd, cwd=SCRIPTS_DIR) |
| 105 setup_board_process.start() |
| 106 |
| 107 |
| 108 def sign_build(image, output): |
| 109 """Make an SSD signed build. |
| 110 |
| 111 Args: |
| 112 image: image to sign. |
| 113 output: destination path for signed image. |
| 114 """ |
| 115 assert_is_file(image, 'Asserting base image exists') |
| 116 cmd = ('sudo ./sign_official_build.sh ssd %s %s %s' |
| 117 % (image, DEVKEYS, output)) |
| 118 print 'IMG_SIGN_DIR: %s' % IMG_SIGN_DIR |
| 119 print 'Signing image: %s' % cmd |
| 120 sign_process = KillableProcess(cmd, cwd=IMG_SIGN_DIR) |
| 121 sign_process.start() |
| 122 |
| 123 |
| 124 def build_factory_packages(signed_image, base_image, fw_updater, folder, board): |
| 125 """Build factory packages and modify mini omaha config. |
| 126 |
| 127 Args: |
| 128 signed_image: signed image |
| 129 base_image: base image |
| 130 fw_updater: firmware updater |
| 131 folder: destination folder to write packages |
| 132 board: platform to build |
| 133 """ |
| 134 cmd = ('./make_factory_package.sh --release %s --factory %s' |
| 135 ' --subfolder %s --board %s' |
| 136 % (signed_image, base_image, folder, board)) |
| 137 if fw_updater: |
| 138 cmd = '%s --firmware_updater %s' % (cmd, fw_updater) |
| 139 else: |
| 140 print ('No --firmware_updater specified. Not including firmware shellball.') |
| 141 |
| 142 print 'Building factory packages: %s' % cmd |
| 143 build_packages_process = KillableProcess(cmd, cwd=SCRIPTS_DIR) |
| 144 build_packages_process.start() |
| 145 |
| 146 |
| 147 def exit(message): |
| 148 print message |
| 149 sys.exit(1) |
| 150 |
| 151 |
| 152 def main(argv): |
| 153 try: |
| 154 argv = FLAGS(argv) |
| 155 except gflags.FlagsError, e: |
| 156 print '%s\nUsage: %s ARGS\n%s' % (e, sys.argv[0], FLAGS) |
| 157 sys.exit(1) |
| 158 |
| 159 if not FLAGS.base_image: |
| 160 exit('No --base_image specified.') |
| 161 |
| 162 if FLAGS.firmware_updater: |
| 163 assert_is_file(FLAGS.firmware_updater, |
| 164 'Invalid or missing firmware updater.') |
| 165 |
| 166 assert_is_file(FLAGS.base_image, 'Invalid or missing base image.') |
| 167 |
| 168 signed_image = os.path.join(os.path.dirname(FLAGS.base_image), |
| 169 '%s_ssd_signed.bin' % FLAGS.board) |
| 170 |
| 171 setup_board(FLAGS.board) |
| 172 sign_build(FLAGS.base_image, signed_image) |
| 173 build_factory_packages(signed_image, FLAGS.base_image, |
| 174 FLAGS.firmware_updater, |
| 175 folder=FLAGS.board, board=FLAGS.board) |
| 176 |
| 177 if FLAGS.start_devserver: |
| 178 start_devserver() |
| 179 |
| 180 |
| 181 if __name__ == '__main__': |
| 182 main(sys.argv) |
OLD | NEW |