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 |
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' |
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 | 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. |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 36 | NEEDED_TOOLS = ('curl', 'xz', 'unshare') |
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 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 39 | def GetArchStageTarballs(version): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 40 | """Returns the URL for a given arch/version""" |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 41 | extension = {'bz2':'tbz2', 'xz':'tar.xz'} |
| 42 | return ['%s/cros-sdk-%s.%s' |
| 43 | % (DEFAULT_URL, version, extension[compressor]) |
| 44 | for compressor in COMPRESSION_PREFERENCE] |
| 45 | |
| 46 | |
| 47 | def GetStage3Urls(version): |
| 48 | return ['%s/stage3-amd64-%s.tar.%s' % (DEFAULT_URL, version, ext) |
| 49 | for ext in COMPRESSION_PREFERENCE] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 50 | |
| 51 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 52 | def FetchRemoteTarballs(storage_dir, urls): |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 53 | """Fetches a tarball given by url, and place it in sdk/. |
| 54 | |
| 55 | Args: |
| 56 | urls: List of URLs to try to download. Download will stop on first success. |
| 57 | |
| 58 | Returns: |
| 59 | Full path to the downloaded file |
| 60 | """ |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 61 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 62 | # Note we track content length ourselves since certain versions of curl |
| 63 | # fail if asked to resume a complete file. |
| 64 | # pylint: disable=C0301,W0631 |
| 65 | # 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] | 66 | for url in urls: |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 67 | # http://www.logilab.org/ticket/8766 |
| 68 | # pylint: disable=E1101 |
| 69 | parsed = urlparse.urlparse(url) |
| 70 | tarball_name = os.path.basename(parsed.path) |
| 71 | if parsed.scheme in ('', 'file'): |
| 72 | if os.path.exists(parsed.path): |
| 73 | return parsed.path |
| 74 | continue |
| 75 | content_length = 0 |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 76 | print 'Attempting download: %s' % url |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 77 | result = cros_build_lib.RunCurl( |
| 78 | ['-I', url], redirect_stdout=True, redirect_stderr=True, |
| 79 | print_cmd=False) |
| 80 | successful = False |
| 81 | for header in result.output.splitlines(): |
| 82 | # We must walk the output to find the string '200 OK' for use cases where |
| 83 | # a proxy is involved and may have pushed down the actual header. |
| 84 | if header.find('200 OK') != -1: |
| 85 | successful = True |
| 86 | elif header.lower().startswith("content-length:"): |
| 87 | content_length = int(header.split(":", 1)[-1].strip()) |
| 88 | if successful: |
| 89 | break |
| 90 | if successful: |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 91 | break |
| 92 | else: |
| 93 | raise Exception('No valid URLs found!') |
| 94 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 95 | tarball_dest = os.path.join(storage_dir, tarball_name) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 96 | current_size = 0 |
| 97 | if os.path.exists(tarball_dest): |
| 98 | current_size = os.path.getsize(tarball_dest) |
| 99 | if current_size > content_length: |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 100 | osutils.SafeUnlink(tarball_dest) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 101 | current_size = 0 |
Zdenek Behan | b2fa72e | 2012-03-16 04:49:30 +0100 | [diff] [blame] | 102 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 103 | if current_size < content_length: |
| 104 | cros_build_lib.RunCurl( |
| 105 | ['-f', '-L', '-y', '30', '-C', '-', '--output', tarball_dest, url], |
| 106 | print_cmd=False) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 107 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 108 | # Cleanup old tarballs now since we've successfull fetched; only cleanup |
| 109 | # the tarballs for our prefix, or unknown ones. |
| 110 | ignored_prefix = ('stage3-' if tarball_name.startswith('cros-sdk-') |
| 111 | else 'cros-sdk-') |
| 112 | for filename in os.listdir(storage_dir): |
| 113 | if filename == tarball_name or filename.startswith(ignored_prefix): |
| 114 | continue |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 115 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 116 | print 'Cleaning up old tarball: %s' % (filename,) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 117 | osutils.SafeUnlink(os.path.join(storage_dir, filename)) |
Zdenek Behan | 9c644dd | 2012-04-05 06:24:02 +0200 | [diff] [blame] | 118 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 119 | return tarball_dest |
| 120 | |
| 121 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 122 | def CreateChroot(chroot_path, sdk_tarball, cache_dir, nousepkg=False): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 123 | """Creates a new chroot from a given SDK""" |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 124 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 125 | cmd = MAKE_CHROOT + ['--stage3_path', sdk_tarball, |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 126 | '--chroot', chroot_path, |
| 127 | '--cache_dir', cache_dir] |
Mike Frysinger | 2de7f04 | 2012-07-10 04:45:03 -0400 | [diff] [blame] | 128 | if nousepkg: |
| 129 | cmd.append('--nousepkg') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 130 | |
| 131 | try: |
| 132 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 133 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 134 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 135 | |
| 136 | |
| 137 | def DeleteChroot(chroot_path): |
| 138 | """Deletes an existing chroot""" |
| 139 | cmd = MAKE_CHROOT + ['--chroot', chroot_path, |
| 140 | '--delete'] |
| 141 | try: |
| 142 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 143 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 144 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 145 | |
| 146 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 147 | def EnterChroot(chroot_path, cache_dir, chrome_root, chrome_root_mount, |
| 148 | additional_args): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 149 | """Enters an existing SDK chroot""" |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 150 | cmd = ENTER_CHROOT + ['--chroot', chroot_path, '--cache_dir', cache_dir] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 151 | if chrome_root: |
| 152 | cmd.extend(['--chrome_root', chrome_root]) |
| 153 | if chrome_root_mount: |
| 154 | cmd.extend(['--chrome_root_mount', chrome_root_mount]) |
| 155 | if len(additional_args) > 0: |
| 156 | cmd.append('--') |
| 157 | cmd.extend(additional_args) |
Brian Harring | 7199e7d | 2012-03-23 04:10:08 -0700 | [diff] [blame] | 158 | |
| 159 | ret = cros_build_lib.RunCommand(cmd, print_cmd=False, error_code_ok=True) |
| 160 | # If we were in interactive mode, ignore the exit code; it'll be whatever |
| 161 | # they last ran w/in the chroot and won't matter to us one way or another. |
| 162 | # Note this does allow chroot entrance to fail and be ignored during |
| 163 | # interactive; this is however a rare case and the user will immediately |
| 164 | # see it (nor will they be checking the exit code manually). |
| 165 | if ret.returncode != 0 and additional_args: |
| 166 | raise SystemExit('Running %r failed with exit code %i' |
| 167 | % (cmd, ret.returncode)) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 168 | |
| 169 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 170 | def _SudoCommand(): |
| 171 | """Get the 'sudo' command, along with all needed environment variables.""" |
| 172 | |
| 173 | # Pass in the ENVIRONMENT_WHITELIST variable so that scripts in the chroot |
| 174 | # know what variables to pass through. |
| 175 | cmd = ['sudo'] |
| 176 | for key in constants.CHROOT_ENVIRONMENT_WHITELIST: |
| 177 | value = os.environ.get(key) |
| 178 | if value is not None: |
| 179 | cmd += ['%s=%s' % (key, value)] |
| 180 | |
| 181 | # Pass in the path to the depot_tools so that users can access them from |
| 182 | # within the chroot. |
| 183 | gclient = osutils.Which('gclient') |
| 184 | if gclient is not None: |
| 185 | cmd += ['DEPOT_TOOLS=%s' % os.path.realpath(os.path.dirname(gclient))] |
| 186 | |
| 187 | return cmd |
| 188 | |
| 189 | |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 190 | def _ReExecuteIfNeeded(argv): |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 191 | """Re-execute cros_sdk as root. |
| 192 | |
| 193 | Also unshare the mount namespace so as to ensure that processes outside |
| 194 | the chroot can't mess with our mounts. |
| 195 | """ |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 196 | MAGIC_VAR = '%CROS_SDK_MOUNT_NS' |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 197 | if os.geteuid() != 0: |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 198 | cmd = _SudoCommand() + ['--'] + argv |
| 199 | os.execvp(cmd[0], cmd) |
| 200 | elif os.environ.get(MAGIC_VAR, '0') == '0': |
| 201 | cgroups.Cgroup.InitSystem() |
| 202 | os.environ[MAGIC_VAR] = '1' |
| 203 | os.execvp('unshare', ['unshare', '-m', '--'] + argv) |
| 204 | else: |
| 205 | os.environ.pop(MAGIC_VAR) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 206 | |
| 207 | |
Brian Harring | 6be2efc | 2012-03-01 05:04:00 -0800 | [diff] [blame] | 208 | def main(argv): |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 209 | usage = """usage: %prog [options] [VAR1=val1 .. VARn=valn -- args] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 210 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 211 | This script is used for manipulating local chroot environments; creating, |
| 212 | deleting, downloading, etc. If given --enter (or no args), it defaults |
| 213 | to an interactive bash shell within the chroot. |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 214 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 215 | If given args those are passed to the chroot environment, and executed.""" |
Mike Frysinger | 95f5558 | 2013-01-08 12:18:53 -0500 | [diff] [blame] | 216 | conf = cros_build_lib.LoadKeyValueFile(SDK_VERSION_FILE, ignore_missing=True) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 217 | sdk_latest_version = conf.get('SDK_LATEST_VERSION', '<unknown>') |
| 218 | bootstrap_latest_version = conf.get('BOOTSTRAP_LATEST_VERSION', '<unknown>') |
| 219 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 220 | parser = commandline.OptionParser(usage=usage, caching=True) |
| 221 | |
| 222 | commands = parser.add_option_group("Commands") |
| 223 | commands.add_option( |
| 224 | '--enter', action='store_true', default=False, |
| 225 | help='Enter the SDK chroot. Implies --create.') |
| 226 | commands.add_option( |
| 227 | '--create', action='store_true',default=False, |
| 228 | help='Create the chroot only if it does not already exist. ' |
| 229 | 'Implies --download.') |
| 230 | commands.add_option( |
| 231 | '--bootstrap', action='store_true', default=False, |
| 232 | help='Build everything from scratch, including the sdk. ' |
| 233 | 'Use this only if you need to validate a change ' |
| 234 | 'that affects SDK creation itself (toolchain and ' |
| 235 | 'build are typically the only folk who need this). ' |
| 236 | 'Note this will quite heavily slow down the build. ' |
| 237 | 'This option implies --create --nousepkg.') |
| 238 | commands.add_option( |
| 239 | '-r', '--replace', action='store_true', default=False, |
| 240 | help='Replace an existing SDK chroot. Basically an alias ' |
| 241 | 'for --delete --create.') |
| 242 | commands.add_option( |
| 243 | '--delete', action='store_true', default=False, |
| 244 | help='Delete the current SDK chroot if it exists.') |
| 245 | commands.add_option( |
| 246 | '--download', action='store_true', default=False, |
| 247 | help='Download the sdk.') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 248 | |
| 249 | # Global options: |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 250 | default_chroot = os.path.join(SRC_ROOT, 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 | |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 274 | _ReExecuteIfNeeded([sys.argv[0]] + argv) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 275 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 276 | host = os.uname()[4] |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 277 | if host != 'x86_64': |
| 278 | parser.error( |
| 279 | "cros_sdk is currently only supported on x86_64; you're running" |
| 280 | " %s. Please find a x86_64 machine." % (host,)) |
| 281 | |
David James | aad5cc7 | 2012-10-26 15:03:13 -0700 | [diff] [blame] | 282 | missing = osutils.FindMissingBinaries(NEEDED_TOOLS) |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 283 | if missing: |
| 284 | parser.error(( |
| 285 | 'The tool(s) %s were not found.' |
| 286 | 'Please install the appropriate package in your host.' |
| 287 | 'Example(ubuntu):' |
| 288 | ' sudo apt-get install <packagename>' |
| 289 | % (', '.join(missing)))) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 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): |
| 349 | src = os.path.join(SRC_ROOT, os.path.basename(target)) |
| 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) |