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