Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Mike Frysinger | 2de7f04 | 2012-07-10 04:45:03 -0400 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """This script fetches and prepares an SDK chroot. |
| 7 | """ |
| 8 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 9 | import os |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 10 | import urlparse |
| 11 | |
| 12 | from chromite.buildbot import constants |
Brian Harring | cfe762a | 2012-02-29 13:03:53 -0800 | [diff] [blame] | 13 | from chromite.lib import cgroups |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 14 | from chromite.lib import commandline |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 15 | from chromite.lib import cros_build_lib |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 16 | from chromite.lib import locking |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 17 | from chromite.lib import osutils |
Brian Harring | cfe762a | 2012-02-29 13:03:53 -0800 | [diff] [blame] | 18 | from chromite.lib import sudo |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 19 | |
| 20 | cros_build_lib.STRICT_SUDO = True |
| 21 | |
| 22 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 23 | DEFAULT_URL = 'https://commondatastorage.googleapis.com/chromiumos-sdk' |
| 24 | COMPRESSION_PREFERENCE = ('bz2', 'xz') |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 25 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 26 | SRC_ROOT = os.path.realpath(constants.SOURCE_ROOT) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 27 | OVERLAY_DIR = os.path.join(SRC_ROOT, 'src/third_party/chromiumos-overlay') |
| 28 | SDK_VERSION_FILE = os.path.join(OVERLAY_DIR, |
| 29 | 'chromeos/binhost/host/sdk_version.conf') |
| 30 | |
| 31 | # TODO(zbehan): Remove the dependency on these, reimplement them in python |
| 32 | MAKE_CHROOT = [os.path.join(SRC_ROOT, 'src/scripts/sdk_lib/make_chroot.sh')] |
| 33 | ENTER_CHROOT = [os.path.join(SRC_ROOT, 'src/scripts/sdk_lib/enter_chroot.sh')] |
| 34 | |
| 35 | # We need these tools to run. Very common tools (tar,..) are ommited. |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 36 | NEEDED_TOOLS = ('curl',) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 37 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 38 | |
| 39 | def CheckPrerequisites(needed_tools): |
| 40 | """Verifies that the required tools are present on the system. |
| 41 | |
| 42 | This is especially important as this script is intended to run |
| 43 | outside the chroot. |
| 44 | |
| 45 | Arguments: |
| 46 | needed_tools: an array of string specified binaries to look for. |
| 47 | |
| 48 | Returns: |
| 49 | True if all needed tools were found. |
| 50 | """ |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 51 | missing = [] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 52 | for tool in needed_tools: |
| 53 | cmd = ['which', tool] |
| 54 | try: |
| 55 | cros_build_lib.RunCommand(cmd, print_cmd=False, redirect_stdout=True, |
| 56 | combine_stdout_stderr=True) |
| 57 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 58 | missing.append(tool) |
| 59 | return missing |
| 60 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 61 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 62 | def GetSdkConfig(): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 63 | """Extracts latest version from chromiumos-overlay.""" |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 64 | d = {} |
| 65 | with open(SDK_VERSION_FILE) as f: |
| 66 | for raw_line in f: |
| 67 | line = raw_line.split('#')[0].strip() |
| 68 | if not line: |
| 69 | continue |
| 70 | chunks = line.split('=', 1) |
| 71 | if len(chunks) != 2: |
| 72 | raise Exception('Malformed version file; line %r' % raw_line) |
| 73 | d[chunks[0]] = chunks[1].strip().strip('"') |
| 74 | return d |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 75 | |
| 76 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 77 | def GetArchStageTarballs(version): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 78 | """Returns the URL for a given arch/version""" |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 79 | extension = {'bz2':'tbz2', 'xz':'tar.xz'} |
| 80 | return ['%s/cros-sdk-%s.%s' |
| 81 | % (DEFAULT_URL, version, extension[compressor]) |
| 82 | for compressor in COMPRESSION_PREFERENCE] |
| 83 | |
| 84 | |
| 85 | def GetStage3Urls(version): |
| 86 | return ['%s/stage3-amd64-%s.tar.%s' % (DEFAULT_URL, version, ext) |
| 87 | for ext in COMPRESSION_PREFERENCE] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 88 | |
| 89 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 90 | def FetchRemoteTarballs(storage_dir, urls): |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 91 | """Fetches a tarball given by url, and place it in sdk/. |
| 92 | |
| 93 | Args: |
| 94 | urls: List of URLs to try to download. Download will stop on first success. |
| 95 | |
| 96 | Returns: |
| 97 | Full path to the downloaded file |
| 98 | """ |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 99 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 100 | # Note we track content length ourselves since certain versions of curl |
| 101 | # fail if asked to resume a complete file. |
| 102 | # pylint: disable=C0301,W0631 |
| 103 | # https://sourceforge.net/tracker/?func=detail&atid=100976&aid=3482927&group_id=976 |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 104 | for url in urls: |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 105 | # http://www.logilab.org/ticket/8766 |
| 106 | # pylint: disable=E1101 |
| 107 | parsed = urlparse.urlparse(url) |
| 108 | tarball_name = os.path.basename(parsed.path) |
| 109 | if parsed.scheme in ('', 'file'): |
| 110 | if os.path.exists(parsed.path): |
| 111 | return parsed.path |
| 112 | continue |
| 113 | content_length = 0 |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 114 | print 'Attempting download: %s' % url |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 115 | result = cros_build_lib.RunCurl( |
| 116 | ['-I', url], redirect_stdout=True, redirect_stderr=True, |
| 117 | print_cmd=False) |
| 118 | successful = False |
| 119 | for header in result.output.splitlines(): |
| 120 | # We must walk the output to find the string '200 OK' for use cases where |
| 121 | # a proxy is involved and may have pushed down the actual header. |
| 122 | if header.find('200 OK') != -1: |
| 123 | successful = True |
| 124 | elif header.lower().startswith("content-length:"): |
| 125 | content_length = int(header.split(":", 1)[-1].strip()) |
| 126 | if successful: |
| 127 | break |
| 128 | if successful: |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 129 | break |
| 130 | else: |
| 131 | raise Exception('No valid URLs found!') |
| 132 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 133 | tarball_dest = os.path.join(storage_dir, tarball_name) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 134 | current_size = 0 |
| 135 | if os.path.exists(tarball_dest): |
| 136 | current_size = os.path.getsize(tarball_dest) |
| 137 | if current_size > content_length: |
| 138 | osutils.SafeUnlink(tarball_dest, sudo=True) |
| 139 | current_size = 0 |
Zdenek Behan | b2fa72e | 2012-03-16 04:49:30 +0100 | [diff] [blame] | 140 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 141 | if current_size < content_length: |
| 142 | cros_build_lib.RunCurl( |
| 143 | ['-f', '-L', '-y', '30', '-C', '-', '--output', tarball_dest, url], |
| 144 | print_cmd=False) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 145 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 146 | # Cleanup old tarballs now since we've successfull fetched; only cleanup |
| 147 | # the tarballs for our prefix, or unknown ones. |
| 148 | ignored_prefix = ('stage3-' if tarball_name.startswith('cros-sdk-') |
| 149 | else 'cros-sdk-') |
| 150 | for filename in os.listdir(storage_dir): |
| 151 | if filename == tarball_name or filename.startswith(ignored_prefix): |
| 152 | continue |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 153 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 154 | print 'Cleaning up old tarball: %s' % (filename,) |
| 155 | osutils.SafeUnlink(os.path.join(storage_dir, filename), sudo=True) |
Zdenek Behan | 9c644dd | 2012-04-05 06:24:02 +0200 | [diff] [blame] | 156 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 157 | return tarball_dest |
| 158 | |
| 159 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 160 | def CreateChroot(chroot_path, sdk_tarball, cache_dir, nousepkg=False): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 161 | """Creates a new chroot from a given SDK""" |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 162 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 163 | cmd = MAKE_CHROOT + ['--stage3_path', sdk_tarball, |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 164 | '--chroot', chroot_path, |
| 165 | '--cache_dir', cache_dir] |
Mike Frysinger | 2de7f04 | 2012-07-10 04:45:03 -0400 | [diff] [blame] | 166 | if nousepkg: |
| 167 | cmd.append('--nousepkg') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 168 | |
| 169 | try: |
| 170 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 171 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 172 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 173 | |
| 174 | |
| 175 | def DeleteChroot(chroot_path): |
| 176 | """Deletes an existing chroot""" |
| 177 | cmd = MAKE_CHROOT + ['--chroot', chroot_path, |
| 178 | '--delete'] |
| 179 | try: |
| 180 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 181 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 182 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 183 | |
| 184 | |
| 185 | def _CreateLockFile(path): |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 186 | """Create a lockfile via sudo that is writable by current user.""" |
| 187 | cros_build_lib.SudoRunCommand(['touch', path], print_cmd=False) |
| 188 | cros_build_lib.SudoRunCommand(['chown', str(os.getuid()), path], |
| 189 | print_cmd=False) |
| 190 | cros_build_lib.SudoRunCommand(['chmod', '644', path], print_cmd=False) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 191 | |
| 192 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 193 | def EnterChroot(chroot_path, cache_dir, chrome_root, chrome_root_mount, |
| 194 | additional_args): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 195 | """Enters an existing SDK chroot""" |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 196 | cmd = ENTER_CHROOT + ['--chroot', chroot_path, '--cache_dir', cache_dir] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 197 | if chrome_root: |
| 198 | cmd.extend(['--chrome_root', chrome_root]) |
| 199 | if chrome_root_mount: |
| 200 | cmd.extend(['--chrome_root_mount', chrome_root_mount]) |
| 201 | if len(additional_args) > 0: |
| 202 | cmd.append('--') |
| 203 | cmd.extend(additional_args) |
Brian Harring | 7199e7d | 2012-03-23 04:10:08 -0700 | [diff] [blame] | 204 | |
| 205 | ret = cros_build_lib.RunCommand(cmd, print_cmd=False, error_code_ok=True) |
| 206 | # If we were in interactive mode, ignore the exit code; it'll be whatever |
| 207 | # they last ran w/in the chroot and won't matter to us one way or another. |
| 208 | # Note this does allow chroot entrance to fail and be ignored during |
| 209 | # interactive; this is however a rare case and the user will immediately |
| 210 | # see it (nor will they be checking the exit code manually). |
| 211 | if ret.returncode != 0 and additional_args: |
| 212 | raise SystemExit('Running %r failed with exit code %i' |
| 213 | % (cmd, ret.returncode)) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 214 | |
| 215 | |
Brian Harring | 6be2efc | 2012-03-01 05:04:00 -0800 | [diff] [blame] | 216 | def main(argv): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 217 | # TODO(ferringb): make argv required once depot_tools is fixed. |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 218 | usage = """usage: %prog [options] [VAR1=val1 .. VARn=valn -- <args>] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 219 | |
| 220 | This script manages a local CrOS SDK chroot. Depending on the flags, |
| 221 | it can download, build or enter a chroot. |
| 222 | |
| 223 | Action taken is the following: |
| 224 | --enter (default) .. Installs and enters a chroot |
| 225 | --download .. Just download a chroot (enter if combined with --enter) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 226 | --delete .. Removes a chroot |
| 227 | """ |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 228 | conf = GetSdkConfig() |
| 229 | sdk_latest_version = conf.get('SDK_LATEST_VERSION', '<unknown>') |
| 230 | bootstrap_latest_version = conf.get('BOOTSTRAP_LATEST_VERSION', '<unknown>') |
| 231 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 232 | parser = commandline.OptionParser(usage, caching=True) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 233 | # Actions: |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 234 | parser.add_option('--bootstrap', |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 235 | action='store_true', dest='bootstrap', default=False, |
Brian Harring | 6ed482a | 2012-08-21 18:01:44 -0700 | [diff] [blame] | 236 | help=('Build everything from scratch, including the sdk. ' |
| 237 | 'Use this only if you need to validate a change ' |
| 238 | 'that affects SDK creation itself (toolchain and ' |
| 239 | 'build are typically the only folk who need this). ' |
| 240 | 'Note this will quite heavily slow down the build. ' |
| 241 | 'Finally, this option implies --enter.')) |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 242 | parser.add_option('--delete', |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 243 | action='store_true', dest='delete', default=False, |
| 244 | help=('Delete the current SDK chroot')) |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 245 | parser.add_option('--download', |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 246 | action='store_true', dest='download', default=False, |
| 247 | help=('Download and install a prebuilt SDK')) |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 248 | parser.add_option('--enter', |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 249 | action='store_true', dest='enter', default=False, |
| 250 | help=('Enter the SDK chroot, possibly (re)create first')) |
| 251 | |
| 252 | # Global options: |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 253 | default_chroot = os.path.join(SRC_ROOT, constants.DEFAULT_CHROOT_DIR) |
| 254 | parser.add_option('--chroot', dest='chroot', default=default_chroot, |
| 255 | type='path', |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 256 | help=('SDK chroot dir name [%s]' % |
| 257 | constants.DEFAULT_CHROOT_DIR)) |
| 258 | |
| 259 | # Additional options: |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 260 | parser.add_option('--chrome_root', |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 261 | dest='chrome_root', default=None, type='path', |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 262 | help=('Mount this chrome root into the SDK chroot')) |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 263 | parser.add_option('--chrome_root_mount', |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 264 | dest='chrome_root_mount', default=None, type='path', |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 265 | help=('Mount chrome into this path inside SDK chroot')) |
| 266 | parser.add_option('-r', '--replace', |
| 267 | action='store_true', dest='replace', default=False, |
| 268 | help=('Replace an existing SDK chroot')) |
Mike Frysinger | 2de7f04 | 2012-07-10 04:45:03 -0400 | [diff] [blame] | 269 | parser.add_option('--nousepkg', |
| 270 | action='store_true', dest='nousepkg', default=False, |
| 271 | help=('Do not use binary packages when creating a chroot')) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 272 | parser.add_option('-u', '--url', |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 273 | dest='sdk_url', default=None, |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 274 | help=('''Use sdk tarball located at this url. |
| 275 | Use file:// for local files.''')) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 276 | parser.add_option('--sdk-version', default=None, |
| 277 | help='Use this sdk version. For prebuilt, current is %r' |
| 278 | ', for bootstrapping its %r.' |
| 279 | % (sdk_latest_version, bootstrap_latest_version)) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 280 | (options, remaining_arguments) = parser.parse_args(argv) |
| 281 | |
| 282 | # Some sanity checks first, before we ask for sudo credentials. |
| 283 | if cros_build_lib.IsInsideChroot(): |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 284 | parser.error("This needs to be ran outside the chroot") |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 285 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 286 | host = os.uname()[4] |
| 287 | |
| 288 | if host != 'x86_64': |
| 289 | parser.error( |
| 290 | "cros_sdk is currently only supported on x86_64; you're running" |
| 291 | " %s. Please find a x86_64 machine." % (host,)) |
| 292 | |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 293 | missing = CheckPrerequisites(NEEDED_TOOLS) |
| 294 | if missing: |
| 295 | parser.error(( |
| 296 | 'The tool(s) %s were not found.' |
| 297 | 'Please install the appropriate package in your host.' |
| 298 | 'Example(ubuntu):' |
| 299 | ' sudo apt-get install <packagename>' |
| 300 | % (', '.join(missing)))) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 301 | |
| 302 | # Default action is --enter, if no other is selected. |
| 303 | if not (options.bootstrap or options.download or options.delete): |
| 304 | options.enter = True |
| 305 | |
| 306 | # Only --enter can process additional args as passthrough commands. |
| 307 | # Warn and exit for least surprise. |
| 308 | if len(remaining_arguments) > 0 and not options.enter: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 309 | parser.error("Additional arguments are not permitted, unless running " |
| 310 | "with --enter") |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 311 | |
| 312 | # Some actions can be combined, as they merely modify how is the chroot |
| 313 | # going to be made. The only option that hates all others is --delete. |
| 314 | if options.delete and \ |
| 315 | (options.enter or options.download or options.bootstrap): |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 316 | parser.error("--delete cannot be combined with --enter, " |
| 317 | "--download or --bootstrap") |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 318 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 319 | if not options.sdk_version: |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 320 | sdk_version = (bootstrap_latest_version if options.bootstrap |
| 321 | else sdk_latest_version) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 322 | else: |
| 323 | sdk_version = options.sdk_version |
| 324 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 325 | # Based on selections, fetch the tarball. |
| 326 | if options.sdk_url: |
| 327 | urls = [options.sdk_url] |
| 328 | elif options.bootstrap: |
| 329 | urls = GetStage3Urls(sdk_version) |
| 330 | else: |
| 331 | urls = GetArchStageTarballs(sdk_version) |
| 332 | |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 333 | if options.delete and not os.path.exists(options.chroot): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 334 | print "Not doing anything. The chroot you want to remove doesn't exist." |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 335 | return 0 |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 336 | |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 337 | lock_path = os.path.dirname(options.chroot) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 338 | lock_path = os.path.join(lock_path, |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 339 | '.%s_lock' % os.path.basename(options.chroot)) |
David James | 891dccf | 2012-08-20 14:19:54 -0700 | [diff] [blame] | 340 | with sudo.SudoKeepAlive(ttyless_sudo=False): |
Brian Harring | 4e6412d | 2012-03-09 20:54:02 -0800 | [diff] [blame] | 341 | with cgroups.SimpleContainChildren('cros_sdk'): |
Brian Harring | cfe762a | 2012-02-29 13:03:53 -0800 | [diff] [blame] | 342 | _CreateLockFile(lock_path) |
| 343 | with locking.FileLock(lock_path, 'chroot lock') as lock: |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 344 | |
| 345 | if os.path.exists(options.chroot): |
| 346 | if options.delete or options.replace: |
| 347 | lock.write_lock() |
| 348 | DeleteChroot(options.chroot) |
| 349 | if options.delete: |
| 350 | return 0 |
| 351 | elif not options.enter and not options.download: |
| 352 | print "Chroot already exists. Run with --replace to re-create." |
| 353 | elif options.delete: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 354 | return 0 |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 355 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 356 | sdk_cache = os.path.join(options.cache_dir, 'sdks') |
| 357 | distfiles_cache = os.path.join(options.cache_dir, 'distfiles') |
Brian Harring | fa32791 | 2012-09-28 20:57:01 -0700 | [diff] [blame] | 358 | osutils.SafeMakedirs(options.cache_dir) |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 359 | |
| 360 | for target in (sdk_cache, distfiles_cache): |
| 361 | src = os.path.join(SRC_ROOT, os.path.basename(target)) |
| 362 | if not os.path.exists(src): |
| 363 | osutils.SafeMakedirs(target) |
| 364 | continue |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 365 | lock.write_lock( |
| 366 | "Upgrade to %r needed but chroot is locked; please exit " |
| 367 | "all instances so this upgrade can finish." % src) |
| 368 | if not os.path.exists(src): |
| 369 | # Note that while waiting for the write lock, src may've vanished; |
| 370 | # it's a rare race during the upgrade process that's a byproduct |
| 371 | # of us avoiding taking a write lock to do the src check. If we |
| 372 | # took a write lock for that check, it would effectively limit |
| 373 | # all cros_sdk for a chroot to a single instance. |
| 374 | osutils.SafeMakedirs(target) |
| 375 | elif not os.path.exists(target): |
| 376 | # Upgrade occurred, but a reversion, or something whacky |
| 377 | # occurred writing to the old location. Wipe and continue. |
| 378 | cros_build_lib.SudoRunCommand( |
| 379 | ['mv', '--', src, target], print_cmd=False) |
| 380 | else: |
| 381 | # Upgrade occurred once already, but either a reversion or |
| 382 | # some before/after separate cros_sdk usage is at play. |
| 383 | # Wipe and continue. |
| 384 | osutils.RmDir(src, sudo=True) |
| 385 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 386 | if not os.path.exists(options.chroot) or options.download: |
Brian Harring | cfe762a | 2012-02-29 13:03:53 -0800 | [diff] [blame] | 387 | lock.write_lock() |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 388 | sdk_tarball = FetchRemoteTarballs(sdk_cache, urls) |
| 389 | if options.download: |
| 390 | # Nothing further to do. |
| 391 | return 0 |
| 392 | CreateChroot(options.chroot, sdk_tarball, options.cache_dir, |
| 393 | nousepkg=(options.bootstrap or options.nousepkg)) |
| 394 | |
Brian Harring | cfe762a | 2012-02-29 13:03:53 -0800 | [diff] [blame] | 395 | if options.enter: |
| 396 | lock.read_lock() |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 397 | EnterChroot(options.chroot, options.cache_dir, options.chrome_root, |
Brian Harring | cfe762a | 2012-02-29 13:03:53 -0800 | [diff] [blame] | 398 | options.chrome_root_mount, remaining_arguments) |