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 |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 10 | import sys |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 11 | import urlparse |
| 12 | |
| 13 | from chromite.buildbot import constants |
Brian Harring | cfe762a | 2012-02-29 13:03:53 -0800 | [diff] [blame] | 14 | from chromite.lib import cgroups |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 15 | from chromite.lib import commandline |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 16 | from chromite.lib import cros_build_lib |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 17 | from chromite.lib import locking |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 18 | from chromite.lib import osutils |
Mike Frysinger | 8e727a3 | 2013-01-16 16:57:53 -0500 | [diff] [blame] | 19 | from chromite.lib import toolchain |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 20 | |
| 21 | cros_build_lib.STRICT_SUDO = True |
| 22 | |
| 23 | |
Zdenek Behan | aa52cea | 2012-05-30 01:31:11 +0200 | [diff] [blame] | 24 | COMPRESSION_PREFERENCE = ('xz', 'bz2') |
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 | # TODO(zbehan): Remove the dependency on these, reimplement them in python |
Mike Frysinger | 648ba2d | 2013-01-08 14:19:34 -0500 | [diff] [blame] | 27 | MAKE_CHROOT = [os.path.join(constants.SOURCE_ROOT, |
| 28 | 'src/scripts/sdk_lib/make_chroot.sh')] |
| 29 | ENTER_CHROOT = [os.path.join(constants.SOURCE_ROOT, |
| 30 | 'src/scripts/sdk_lib/enter_chroot.sh')] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 31 | |
| 32 | # We need these tools to run. Very common tools (tar,..) are ommited. |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 33 | NEEDED_TOOLS = ('curl', 'xz', 'unshare') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 34 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 35 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 36 | def GetArchStageTarballs(version): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 37 | """Returns the URL for a given arch/version""" |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 38 | extension = {'bz2':'tbz2', 'xz':'tar.xz'} |
Mike Frysinger | 8e727a3 | 2013-01-16 16:57:53 -0500 | [diff] [blame] | 39 | return [toolchain.GetSdkURL(suburl='cros-sdk-%s.%s' |
| 40 | % (version, extension[compressor])) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 41 | for compressor in COMPRESSION_PREFERENCE] |
| 42 | |
| 43 | |
| 44 | def GetStage3Urls(version): |
Mike Frysinger | 8e727a3 | 2013-01-16 16:57:53 -0500 | [diff] [blame] | 45 | return [toolchain.GetSdkURL(suburl='stage3-amd64-%s.tar.%s' % (version, ext)) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 46 | for ext in COMPRESSION_PREFERENCE] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 47 | |
| 48 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 49 | def FetchRemoteTarballs(storage_dir, urls): |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 50 | """Fetches a tarball given by url, and place it in sdk/. |
| 51 | |
| 52 | Args: |
| 53 | urls: List of URLs to try to download. Download will stop on first success. |
| 54 | |
| 55 | Returns: |
| 56 | Full path to the downloaded file |
| 57 | """ |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 58 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 59 | # Note we track content length ourselves since certain versions of curl |
| 60 | # fail if asked to resume a complete file. |
| 61 | # pylint: disable=C0301,W0631 |
| 62 | # 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] | 63 | for url in urls: |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 64 | # http://www.logilab.org/ticket/8766 |
| 65 | # pylint: disable=E1101 |
| 66 | parsed = urlparse.urlparse(url) |
| 67 | tarball_name = os.path.basename(parsed.path) |
| 68 | if parsed.scheme in ('', 'file'): |
| 69 | if os.path.exists(parsed.path): |
| 70 | return parsed.path |
| 71 | continue |
| 72 | content_length = 0 |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 73 | print 'Attempting download: %s' % url |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 74 | result = cros_build_lib.RunCurl( |
| 75 | ['-I', url], redirect_stdout=True, redirect_stderr=True, |
| 76 | print_cmd=False) |
| 77 | successful = False |
| 78 | for header in result.output.splitlines(): |
| 79 | # We must walk the output to find the string '200 OK' for use cases where |
| 80 | # a proxy is involved and may have pushed down the actual header. |
| 81 | if header.find('200 OK') != -1: |
| 82 | successful = True |
| 83 | elif header.lower().startswith("content-length:"): |
| 84 | content_length = int(header.split(":", 1)[-1].strip()) |
| 85 | if successful: |
| 86 | break |
| 87 | if successful: |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 88 | break |
| 89 | else: |
| 90 | raise Exception('No valid URLs found!') |
| 91 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 92 | tarball_dest = os.path.join(storage_dir, tarball_name) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 93 | current_size = 0 |
| 94 | if os.path.exists(tarball_dest): |
| 95 | current_size = os.path.getsize(tarball_dest) |
| 96 | if current_size > content_length: |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 97 | osutils.SafeUnlink(tarball_dest) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 98 | current_size = 0 |
Zdenek Behan | b2fa72e | 2012-03-16 04:49:30 +0100 | [diff] [blame] | 99 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 100 | if current_size < content_length: |
| 101 | cros_build_lib.RunCurl( |
| 102 | ['-f', '-L', '-y', '30', '-C', '-', '--output', tarball_dest, url], |
| 103 | print_cmd=False) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 104 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 105 | # Cleanup old tarballs now since we've successfull fetched; only cleanup |
| 106 | # the tarballs for our prefix, or unknown ones. |
| 107 | ignored_prefix = ('stage3-' if tarball_name.startswith('cros-sdk-') |
| 108 | else 'cros-sdk-') |
| 109 | for filename in os.listdir(storage_dir): |
| 110 | if filename == tarball_name or filename.startswith(ignored_prefix): |
| 111 | continue |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 112 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 113 | print 'Cleaning up old tarball: %s' % (filename,) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 114 | osutils.SafeUnlink(os.path.join(storage_dir, filename)) |
Zdenek Behan | 9c644dd | 2012-04-05 06:24:02 +0200 | [diff] [blame] | 115 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 116 | return tarball_dest |
| 117 | |
| 118 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 119 | def CreateChroot(chroot_path, sdk_tarball, cache_dir, nousepkg=False): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 120 | """Creates a new chroot from a given SDK""" |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 121 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 122 | cmd = MAKE_CHROOT + ['--stage3_path', sdk_tarball, |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 123 | '--chroot', chroot_path, |
| 124 | '--cache_dir', cache_dir] |
Mike Frysinger | 2de7f04 | 2012-07-10 04:45:03 -0400 | [diff] [blame] | 125 | if nousepkg: |
| 126 | cmd.append('--nousepkg') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 127 | |
| 128 | try: |
| 129 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 130 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 131 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 132 | |
| 133 | |
| 134 | def DeleteChroot(chroot_path): |
| 135 | """Deletes an existing chroot""" |
| 136 | cmd = MAKE_CHROOT + ['--chroot', chroot_path, |
| 137 | '--delete'] |
| 138 | try: |
| 139 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 140 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 141 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 142 | |
| 143 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 144 | def EnterChroot(chroot_path, cache_dir, chrome_root, chrome_root_mount, |
| 145 | additional_args): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 146 | """Enters an existing SDK chroot""" |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 147 | cmd = ENTER_CHROOT + ['--chroot', chroot_path, '--cache_dir', cache_dir] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 148 | if chrome_root: |
| 149 | cmd.extend(['--chrome_root', chrome_root]) |
| 150 | if chrome_root_mount: |
| 151 | cmd.extend(['--chrome_root_mount', chrome_root_mount]) |
| 152 | if len(additional_args) > 0: |
| 153 | cmd.append('--') |
| 154 | cmd.extend(additional_args) |
Brian Harring | 7199e7d | 2012-03-23 04:10:08 -0700 | [diff] [blame] | 155 | |
| 156 | ret = cros_build_lib.RunCommand(cmd, print_cmd=False, error_code_ok=True) |
| 157 | # If we were in interactive mode, ignore the exit code; it'll be whatever |
| 158 | # they last ran w/in the chroot and won't matter to us one way or another. |
| 159 | # Note this does allow chroot entrance to fail and be ignored during |
| 160 | # interactive; this is however a rare case and the user will immediately |
| 161 | # see it (nor will they be checking the exit code manually). |
| 162 | if ret.returncode != 0 and additional_args: |
| 163 | raise SystemExit('Running %r failed with exit code %i' |
| 164 | % (cmd, ret.returncode)) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 165 | |
| 166 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 167 | def _SudoCommand(): |
| 168 | """Get the 'sudo' command, along with all needed environment variables.""" |
| 169 | |
David James | 5a73b4d | 2013-03-07 10:23:40 -0800 | [diff] [blame] | 170 | # Pass in the ENVIRONMENT_WHITELIST and ENV_PASSTHRU variables so that |
| 171 | # scripts in the chroot know what variables to pass through. |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 172 | cmd = ['sudo'] |
David James | 5a73b4d | 2013-03-07 10:23:40 -0800 | [diff] [blame] | 173 | for key in constants.CHROOT_ENVIRONMENT_WHITELIST + constants.ENV_PASSTHRU: |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 174 | value = os.environ.get(key) |
| 175 | if value is not None: |
| 176 | cmd += ['%s=%s' % (key, value)] |
| 177 | |
| 178 | # Pass in the path to the depot_tools so that users can access them from |
| 179 | # within the chroot. |
| 180 | gclient = osutils.Which('gclient') |
| 181 | if gclient is not None: |
| 182 | cmd += ['DEPOT_TOOLS=%s' % os.path.realpath(os.path.dirname(gclient))] |
| 183 | |
| 184 | return cmd |
| 185 | |
| 186 | |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 187 | def _ReExecuteIfNeeded(argv): |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 188 | """Re-execute cros_sdk as root. |
| 189 | |
| 190 | Also unshare the mount namespace so as to ensure that processes outside |
| 191 | the chroot can't mess with our mounts. |
| 192 | """ |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 193 | MAGIC_VAR = '%CROS_SDK_MOUNT_NS' |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 194 | if os.geteuid() != 0: |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 195 | cmd = _SudoCommand() + ['--'] + argv |
| 196 | os.execvp(cmd[0], cmd) |
| 197 | elif os.environ.get(MAGIC_VAR, '0') == '0': |
| 198 | cgroups.Cgroup.InitSystem() |
| 199 | os.environ[MAGIC_VAR] = '1' |
| 200 | os.execvp('unshare', ['unshare', '-m', '--'] + argv) |
| 201 | else: |
| 202 | os.environ.pop(MAGIC_VAR) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 203 | |
| 204 | |
Brian Harring | 6be2efc | 2012-03-01 05:04:00 -0800 | [diff] [blame] | 205 | def main(argv): |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 206 | usage = """usage: %prog [options] [VAR1=val1 .. VARn=valn -- args] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 207 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 208 | This script is used for manipulating local chroot environments; creating, |
| 209 | deleting, downloading, etc. If given --enter (or no args), it defaults |
| 210 | to an interactive bash shell within the chroot. |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 211 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 212 | If given args those are passed to the chroot environment, and executed.""" |
Mike Frysinger | 648ba2d | 2013-01-08 14:19:34 -0500 | [diff] [blame] | 213 | conf = cros_build_lib.LoadKeyValueFile( |
| 214 | os.path.join(constants.SOURCE_ROOT, constants.SDK_VERSION_FILE), |
| 215 | ignore_missing=True) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 216 | sdk_latest_version = conf.get('SDK_LATEST_VERSION', '<unknown>') |
| 217 | bootstrap_latest_version = conf.get('BOOTSTRAP_LATEST_VERSION', '<unknown>') |
| 218 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 219 | parser = commandline.OptionParser(usage=usage, caching=True) |
| 220 | |
| 221 | commands = parser.add_option_group("Commands") |
| 222 | commands.add_option( |
| 223 | '--enter', action='store_true', default=False, |
| 224 | help='Enter the SDK chroot. Implies --create.') |
| 225 | commands.add_option( |
| 226 | '--create', action='store_true',default=False, |
| 227 | help='Create the chroot only if it does not already exist. ' |
| 228 | 'Implies --download.') |
| 229 | commands.add_option( |
| 230 | '--bootstrap', action='store_true', default=False, |
| 231 | help='Build everything from scratch, including the sdk. ' |
| 232 | 'Use this only if you need to validate a change ' |
| 233 | 'that affects SDK creation itself (toolchain and ' |
| 234 | 'build are typically the only folk who need this). ' |
| 235 | 'Note this will quite heavily slow down the build. ' |
| 236 | 'This option implies --create --nousepkg.') |
| 237 | commands.add_option( |
| 238 | '-r', '--replace', action='store_true', default=False, |
| 239 | help='Replace an existing SDK chroot. Basically an alias ' |
| 240 | 'for --delete --create.') |
| 241 | commands.add_option( |
| 242 | '--delete', action='store_true', default=False, |
| 243 | help='Delete the current SDK chroot if it exists.') |
| 244 | commands.add_option( |
| 245 | '--download', action='store_true', default=False, |
| 246 | help='Download the sdk.') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 247 | |
| 248 | # Global options: |
Mike Frysinger | 648ba2d | 2013-01-08 14:19:34 -0500 | [diff] [blame] | 249 | default_chroot = os.path.join(constants.SOURCE_ROOT, |
| 250 | constants.DEFAULT_CHROOT_DIR) |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 251 | parser.add_option( |
| 252 | '--chroot', dest='chroot', default=default_chroot, type='path', |
| 253 | help=('SDK chroot dir name [%s]' % constants.DEFAULT_CHROOT_DIR)) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 254 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 255 | parser.add_option('--chrome_root', default=None, type='path', |
| 256 | help='Mount this chrome root into the SDK chroot') |
| 257 | parser.add_option('--chrome_root_mount', default=None, type='path', |
| 258 | help='Mount chrome into this path inside SDK chroot') |
| 259 | parser.add_option('--nousepkg', action='store_true', default=False, |
| 260 | help='Do not use binary packages when creating a chroot.') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 261 | parser.add_option('-u', '--url', |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 262 | dest='sdk_url', default=None, |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 263 | help=('''Use sdk tarball located at this url. |
| 264 | Use file:// for local files.''')) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 265 | parser.add_option('--sdk-version', default=None, |
| 266 | help='Use this sdk version. For prebuilt, current is %r' |
| 267 | ', for bootstrapping its %r.' |
| 268 | % (sdk_latest_version, bootstrap_latest_version)) |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 269 | options, chroot_command = parser.parse_args(argv) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 270 | |
| 271 | # Some sanity checks first, before we ask for sudo credentials. |
Mike Frysinger | 8fd67dc | 2012-12-03 23:51:18 -0500 | [diff] [blame] | 272 | cros_build_lib.AssertOutsideChroot() |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 273 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 274 | host = os.uname()[4] |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 275 | if host != 'x86_64': |
| 276 | parser.error( |
| 277 | "cros_sdk is currently only supported on x86_64; you're running" |
| 278 | " %s. Please find a x86_64 machine." % (host,)) |
| 279 | |
David James | aad5cc7 | 2012-10-26 15:03:13 -0700 | [diff] [blame] | 280 | missing = osutils.FindMissingBinaries(NEEDED_TOOLS) |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 281 | if missing: |
| 282 | parser.error(( |
David James | 471532c | 2013-01-21 10:23:31 -0800 | [diff] [blame] | 283 | 'The tool(s) %s were not found.\n' |
| 284 | 'Please install the appropriate package in your host.\n' |
| 285 | 'Example(ubuntu):\n' |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 286 | ' sudo apt-get install <packagename>' |
| 287 | % (', '.join(missing)))) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 288 | |
David James | 471532c | 2013-01-21 10:23:31 -0800 | [diff] [blame] | 289 | _ReExecuteIfNeeded([sys.argv[0]] + argv) |
| 290 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 291 | # Expand out the aliases... |
| 292 | if options.replace: |
| 293 | options.delete = options.create = True |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 294 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 295 | if options.bootstrap: |
| 296 | options.create = True |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 297 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 298 | # If a command is not given, default to enter. |
| 299 | options.enter |= not any(getattr(options, x.dest) |
| 300 | for x in commands.option_list) |
| 301 | options.enter |= bool(chroot_command) |
| 302 | |
| 303 | if options.enter and options.delete and not options.create: |
| 304 | parser.error("Trying to enter the chroot when --delete " |
| 305 | "was specified makes no sense.") |
| 306 | |
| 307 | # Finally, discern if we need to create the chroot. |
| 308 | chroot_exists = os.path.exists(options.chroot) |
| 309 | if options.create or options.enter: |
| 310 | # Only create if it's being wiped, or if it doesn't exist. |
| 311 | if not options.delete and chroot_exists: |
| 312 | options.create = False |
| 313 | else: |
| 314 | options.download = True |
| 315 | |
| 316 | # Finally, flip create if necessary. |
| 317 | if options.enter: |
| 318 | options.create |= not chroot_exists |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 319 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 320 | if not options.sdk_version: |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 321 | sdk_version = (bootstrap_latest_version if options.bootstrap |
| 322 | else sdk_latest_version) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 323 | else: |
| 324 | sdk_version = options.sdk_version |
| 325 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 326 | # Based on selections, fetch the tarball. |
| 327 | if options.sdk_url: |
| 328 | urls = [options.sdk_url] |
| 329 | elif options.bootstrap: |
| 330 | urls = GetStage3Urls(sdk_version) |
| 331 | else: |
| 332 | urls = GetArchStageTarballs(sdk_version) |
| 333 | |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 334 | lock_path = os.path.dirname(options.chroot) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 335 | lock_path = os.path.join(lock_path, |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 336 | '.%s_lock' % os.path.basename(options.chroot)) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 337 | with cgroups.SimpleContainChildren('cros_sdk'): |
| 338 | with locking.FileLock(lock_path, 'chroot lock') as lock: |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 339 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 340 | if options.delete and os.path.exists(options.chroot): |
| 341 | lock.write_lock() |
| 342 | DeleteChroot(options.chroot) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 343 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 344 | sdk_cache = os.path.join(options.cache_dir, 'sdks') |
| 345 | distfiles_cache = os.path.join(options.cache_dir, 'distfiles') |
| 346 | osutils.SafeMakedirs(options.cache_dir) |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 347 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 348 | for target in (sdk_cache, distfiles_cache): |
Mike Frysinger | 648ba2d | 2013-01-08 14:19:34 -0500 | [diff] [blame] | 349 | src = os.path.join(constants.SOURCE_ROOT, os.path.basename(target)) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 350 | if not os.path.exists(src): |
| 351 | osutils.SafeMakedirs(target) |
| 352 | continue |
| 353 | lock.write_lock( |
| 354 | "Upgrade to %r needed but chroot is locked; please exit " |
| 355 | "all instances so this upgrade can finish." % src) |
| 356 | if not os.path.exists(src): |
| 357 | # Note that while waiting for the write lock, src may've vanished; |
| 358 | # it's a rare race during the upgrade process that's a byproduct |
| 359 | # of us avoiding taking a write lock to do the src check. If we |
| 360 | # took a write lock for that check, it would effectively limit |
| 361 | # all cros_sdk for a chroot to a single instance. |
| 362 | osutils.SafeMakedirs(target) |
| 363 | elif not os.path.exists(target): |
| 364 | # Upgrade occurred, but a reversion, or something whacky |
| 365 | # occurred writing to the old location. Wipe and continue. |
| 366 | os.rename(src, target) |
| 367 | else: |
| 368 | # Upgrade occurred once already, but either a reversion or |
| 369 | # some before/after separate cros_sdk usage is at play. |
| 370 | # Wipe and continue. |
| 371 | osutils.RmDir(src) |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 372 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 373 | if options.download: |
| 374 | lock.write_lock() |
| 375 | sdk_tarball = FetchRemoteTarballs(sdk_cache, urls) |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 376 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 377 | if options.create: |
| 378 | lock.write_lock() |
| 379 | CreateChroot(options.chroot, sdk_tarball, options.cache_dir, |
| 380 | nousepkg=(options.bootstrap or options.nousepkg)) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 381 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 382 | if options.enter: |
| 383 | lock.read_lock() |
| 384 | EnterChroot(options.chroot, options.cache_dir, options.chrome_root, |
| 385 | options.chrome_root_mount, chroot_command) |