Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2011-2012 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """This script fetches and prepares an SDK chroot. |
| 7 | """ |
| 8 | |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 9 | import logging |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 10 | import optparse |
| 11 | import os |
| 12 | import sys |
| 13 | import urlparse |
| 14 | |
| 15 | from chromite.buildbot import constants |
Brian Harring | cfe762a | 2012-02-29 13:03:53 -0800 | [diff] [blame] | 16 | from chromite.lib import cgroups |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 17 | from chromite.lib import cros_build_lib |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 18 | from chromite.lib import locking |
Brian Harring | cfe762a | 2012-02-29 13:03:53 -0800 | [diff] [blame] | 19 | from chromite.lib import sudo |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 20 | |
| 21 | cros_build_lib.STRICT_SUDO = True |
| 22 | |
| 23 | |
| 24 | DEFAULT_URL = 'https://commondatastorage.googleapis.com/chromiumos-sdk/' |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 25 | SDK_SUFFIXES = ['.tbz2', '.tar.xz'] |
| 26 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 27 | SRC_ROOT = os.path.realpath(constants.SOURCE_ROOT) |
| 28 | SDK_DIR = os.path.join(SRC_ROOT, 'sdks') |
| 29 | OVERLAY_DIR = os.path.join(SRC_ROOT, 'src/third_party/chromiumos-overlay') |
| 30 | SDK_VERSION_FILE = os.path.join(OVERLAY_DIR, |
| 31 | 'chromeos/binhost/host/sdk_version.conf') |
| 32 | |
| 33 | # TODO(zbehan): Remove the dependency on these, reimplement them in python |
| 34 | MAKE_CHROOT = [os.path.join(SRC_ROOT, 'src/scripts/sdk_lib/make_chroot.sh')] |
| 35 | ENTER_CHROOT = [os.path.join(SRC_ROOT, 'src/scripts/sdk_lib/enter_chroot.sh')] |
| 36 | |
| 37 | # We need these tools to run. Very common tools (tar,..) are ommited. |
| 38 | NEEDED_TOOLS = ['curl'] |
| 39 | |
| 40 | def GetHostArch(): |
| 41 | """Returns a string for the host architecture""" |
| 42 | out = cros_build_lib.RunCommand(['uname', '-m'], |
| 43 | redirect_stdout=True, print_cmd=False).output |
| 44 | return out.rstrip('\n') |
| 45 | |
| 46 | def CheckPrerequisites(needed_tools): |
| 47 | """Verifies that the required tools are present on the system. |
| 48 | |
| 49 | This is especially important as this script is intended to run |
| 50 | outside the chroot. |
| 51 | |
| 52 | Arguments: |
| 53 | needed_tools: an array of string specified binaries to look for. |
| 54 | |
| 55 | Returns: |
| 56 | True if all needed tools were found. |
| 57 | """ |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 58 | missing = [] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 59 | for tool in needed_tools: |
| 60 | cmd = ['which', tool] |
| 61 | try: |
| 62 | cros_build_lib.RunCommand(cmd, print_cmd=False, redirect_stdout=True, |
| 63 | combine_stdout_stderr=True) |
| 64 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 65 | missing.append(tool) |
| 66 | return missing |
| 67 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 68 | |
| 69 | def GetLatestVersion(): |
| 70 | """Extracts latest version from chromiumos-overlay.""" |
| 71 | sdk_file = open(SDK_VERSION_FILE) |
| 72 | buf = sdk_file.readline().rstrip('\n').split('=') |
| 73 | if buf[0] != 'SDK_LATEST_VERSION': |
| 74 | raise Exception('Malformed version file') |
| 75 | return buf[1].strip('"') |
| 76 | |
| 77 | |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 78 | def GetArchStageTarballs(tarballArch, version): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 79 | """Returns the URL for a given arch/version""" |
| 80 | D = { 'x86_64': 'cros-sdk-' } |
| 81 | try: |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 82 | return [DEFAULT_URL + D[tarballArch] + version + x for x in SDK_SUFFIXES] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 83 | except KeyError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 84 | raise SystemExit('Unsupported arch: %s' % (tarballArch,)) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 85 | |
| 86 | |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 87 | def FetchRemoteTarballs(urls): |
| 88 | """Fetches a tarball given by url, and place it in sdk/. |
| 89 | |
| 90 | Args: |
| 91 | urls: List of URLs to try to download. Download will stop on first success. |
| 92 | |
| 93 | Returns: |
| 94 | Full path to the downloaded file |
| 95 | """ |
Zdenek Behan | 9c644dd | 2012-04-05 06:24:02 +0200 | [diff] [blame] | 96 | |
| 97 | def RunCurl(args, **kwargs): |
| 98 | """Runs curl and wraps around all necessary hacks.""" |
| 99 | cmd = ['curl'] |
| 100 | cmd.extend(args) |
| 101 | |
Brian Harring | b45afea | 2012-05-17 08:10:53 -0700 | [diff] [blame] | 102 | # These values were discerned via scraping the curl manpage; they're all |
| 103 | # retry related (dns failed, timeout occurred, etc, see the manpage for |
| 104 | # exact specifics of each). |
| 105 | # Note we allow 22 to deal w/ 500's- they're thrown by google storage |
| 106 | # occasionally. |
| 107 | # Finally, we do not use curl's --retry option since it generally doesn't |
| 108 | # actually retry anything; code 18 for example, it will not retry on. |
Brian Harring | 06d2b27 | 2012-05-22 18:42:02 -0700 | [diff] [blame] | 109 | retriable_exits = frozenset([5, 6, 7, 15, 18, 22, 26, 28, 52, 56]) |
Brian Harring | b45afea | 2012-05-17 08:10:53 -0700 | [diff] [blame] | 110 | try: |
| 111 | return cros_build_lib.RunCommandWithRetries( |
| 112 | 5, cmd, sleep=3, retry_on=retriable_exits, **kwargs) |
| 113 | except cros_build_lib.RunCommandError, e: |
| 114 | code = e.result.returncode |
Brian Harring | 06d2b27 | 2012-05-22 18:42:02 -0700 | [diff] [blame] | 115 | if code in (51, 58, 60): |
Brian Harring | b45afea | 2012-05-17 08:10:53 -0700 | [diff] [blame] | 116 | # These are the return codes of failing certs as per 'man curl'. |
Zdenek Behan | 9c644dd | 2012-04-05 06:24:02 +0200 | [diff] [blame] | 117 | print 'Download failed with certificate error? Try "sudo c_rehash".' |
| 118 | else: |
Brian Harring | b45afea | 2012-05-17 08:10:53 -0700 | [diff] [blame] | 119 | print "Curl failed w/ exit code %i" % code |
Zdenek Behan | 9c644dd | 2012-04-05 06:24:02 +0200 | [diff] [blame] | 120 | sys.exit(1) |
| 121 | |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 122 | def RemoteTarballExists(url): |
| 123 | """Tests if a remote tarball exists.""" |
| 124 | # We also use this for "local" tarballs using file:// urls. Those will |
| 125 | # fail the -I check, so just check the file locally instead. |
| 126 | if url.startswith('file://'): |
| 127 | return os.path.exists(url.replace('file://', '')) |
| 128 | |
| 129 | result = RunCurl(['-I', url], |
| 130 | redirect_stdout=True, redirect_stderr=True, |
| 131 | print_cmd=False) |
Bernie Thompson | e522add | 2012-05-29 12:47:12 -0700 | [diff] [blame^] | 132 | # We must walk the output to find the string '200 OK' for use cases where |
| 133 | # a proxy is involved and may have pushed down the actual header. |
| 134 | for header in result.output.splitlines(): |
| 135 | if header.find('200 OK') != -1: |
| 136 | return 1 |
| 137 | return 0 |
| 138 | |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 139 | |
| 140 | url = None |
| 141 | for url in urls: |
| 142 | print 'Attempting download: %s' % url |
| 143 | if RemoteTarballExists(url): |
| 144 | break |
| 145 | else: |
| 146 | raise Exception('No valid URLs found!') |
| 147 | |
Brian Harring | b45afea | 2012-05-17 08:10:53 -0700 | [diff] [blame] | 148 | # pylint: disable=E1101 |
Zdenek Behan | b2fa72e | 2012-03-16 04:49:30 +0100 | [diff] [blame] | 149 | tarball_name = os.path.basename(urlparse.urlparse(url).path) |
| 150 | tarball_dest = os.path.join(SDK_DIR, tarball_name) |
| 151 | |
| 152 | # Cleanup old tarballs. |
| 153 | files_to_delete = [f for f in os.listdir(SDK_DIR) if f != tarball_name] |
| 154 | if files_to_delete: |
| 155 | print 'Cleaning up old tarballs: ' + str(files_to_delete) |
| 156 | for f in files_to_delete: |
| 157 | f_path = os.path.join(SDK_DIR, f) |
| 158 | # Only delete regular files that belong to us. |
| 159 | if os.path.isfile(f_path) and os.stat(f_path).st_uid == os.getuid(): |
| 160 | os.remove(f_path) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 161 | |
Brian Harring | b45afea | 2012-05-17 08:10:53 -0700 | [diff] [blame] | 162 | curl_opts = ['-f', '-L', '-y', '30', '--output', tarball_dest] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 163 | if not url.startswith('file://') and os.path.exists(tarball_dest): |
| 164 | # Only resume for remote URLs. If the file is local, there's no |
| 165 | # real speedup, and using the same filename for different files |
| 166 | # locally will cause issues. |
Zdenek Behan | 9c644dd | 2012-04-05 06:24:02 +0200 | [diff] [blame] | 167 | curl_opts.extend(['-C', '-']) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 168 | |
| 169 | # Additionally, certain versions of curl incorrectly fail if |
| 170 | # told to resume a file that is fully downloaded, thus do a |
| 171 | # check on our own. |
| 172 | # see: |
Brian Harring | b45afea | 2012-05-17 08:10:53 -0700 | [diff] [blame] | 173 | # pylint: disable=C0301 |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 174 | # https://sourceforge.net/tracker/?func=detail&atid=100976&aid=3482927&group_id=976 |
Zdenek Behan | 9c644dd | 2012-04-05 06:24:02 +0200 | [diff] [blame] | 175 | result = RunCurl(['-I', url], |
| 176 | redirect_stdout=True, |
| 177 | redirect_stderr=True, |
| 178 | print_cmd=False) |
| 179 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 180 | for x in result.output.splitlines(): |
| 181 | if x.lower().startswith("content-length:"): |
| 182 | length = int(x.split(":", 1)[-1].strip()) |
| 183 | if length == os.path.getsize(tarball_dest): |
| 184 | # Fully fetched; bypass invoking curl, since it can screw up handling |
| 185 | # of this (>=7.21.4 and up). |
| 186 | return tarball_dest |
| 187 | break |
Zdenek Behan | 9c644dd | 2012-04-05 06:24:02 +0200 | [diff] [blame] | 188 | curl_opts.append(url) |
| 189 | RunCurl(curl_opts) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 190 | return tarball_dest |
| 191 | |
| 192 | |
| 193 | def BootstrapChroot(chroot_path, stage_url, replace): |
| 194 | """Builds a new chroot from source""" |
| 195 | cmd = MAKE_CHROOT + ['--chroot', chroot_path, |
| 196 | '--nousepkg'] |
| 197 | |
| 198 | stage = None |
| 199 | if stage_url: |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 200 | stage = FetchRemoteTarballs([stage_url]) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 201 | |
| 202 | if stage: |
| 203 | cmd.extend(['--stage3_path', stage]) |
| 204 | |
| 205 | if replace: |
| 206 | cmd.append('--replace') |
| 207 | |
| 208 | try: |
| 209 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 210 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 211 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 212 | |
| 213 | |
| 214 | def CreateChroot(sdk_url, sdk_version, chroot_path, replace): |
| 215 | """Creates a new chroot from a given SDK""" |
| 216 | if not os.path.exists(SDK_DIR): |
| 217 | cros_build_lib.RunCommand(['mkdir', '-p', SDK_DIR], print_cmd=False) |
| 218 | |
| 219 | # Based on selections, fetch the tarball |
| 220 | if sdk_url: |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 221 | urls = [sdk_url] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 222 | else: |
| 223 | arch = GetHostArch() |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 224 | urls = GetArchStageTarballs(arch, sdk_version) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 225 | |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 226 | sdk = FetchRemoteTarballs(urls) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 227 | |
| 228 | # TODO(zbehan): Unpack and install |
| 229 | # For now, we simply call make_chroot on the prebuilt chromeos-sdk. |
| 230 | # make_chroot provides a variety of hacks to make the chroot useable. |
| 231 | # These should all be eliminated/minimised, after which, we can change |
| 232 | # this to just unpacking the sdk. |
| 233 | cmd = MAKE_CHROOT + ['--stage3_path', sdk, |
| 234 | '--chroot', chroot_path] |
| 235 | |
| 236 | if replace: |
| 237 | cmd.append('--replace') |
| 238 | |
| 239 | try: |
| 240 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 241 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 242 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 243 | |
| 244 | |
| 245 | def DeleteChroot(chroot_path): |
| 246 | """Deletes an existing chroot""" |
| 247 | cmd = MAKE_CHROOT + ['--chroot', chroot_path, |
| 248 | '--delete'] |
| 249 | try: |
| 250 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 251 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 252 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 253 | |
| 254 | |
| 255 | def _CreateLockFile(path): |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 256 | """Create a lockfile via sudo that is writable by current user.""" |
| 257 | cros_build_lib.SudoRunCommand(['touch', path], print_cmd=False) |
| 258 | cros_build_lib.SudoRunCommand(['chown', str(os.getuid()), path], |
| 259 | print_cmd=False) |
| 260 | cros_build_lib.SudoRunCommand(['chmod', '644', path], print_cmd=False) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 261 | |
| 262 | |
| 263 | def EnterChroot(chroot_path, chrome_root, chrome_root_mount, additional_args): |
| 264 | """Enters an existing SDK chroot""" |
| 265 | cmd = ENTER_CHROOT + ['--chroot', chroot_path] |
| 266 | if chrome_root: |
| 267 | cmd.extend(['--chrome_root', chrome_root]) |
| 268 | if chrome_root_mount: |
| 269 | cmd.extend(['--chrome_root_mount', chrome_root_mount]) |
| 270 | if len(additional_args) > 0: |
| 271 | cmd.append('--') |
| 272 | cmd.extend(additional_args) |
Brian Harring | 7199e7d | 2012-03-23 04:10:08 -0700 | [diff] [blame] | 273 | |
| 274 | ret = cros_build_lib.RunCommand(cmd, print_cmd=False, error_code_ok=True) |
| 275 | # If we were in interactive mode, ignore the exit code; it'll be whatever |
| 276 | # they last ran w/in the chroot and won't matter to us one way or another. |
| 277 | # Note this does allow chroot entrance to fail and be ignored during |
| 278 | # interactive; this is however a rare case and the user will immediately |
| 279 | # see it (nor will they be checking the exit code manually). |
| 280 | if ret.returncode != 0 and additional_args: |
| 281 | raise SystemExit('Running %r failed with exit code %i' |
| 282 | % (cmd, ret.returncode)) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 283 | |
| 284 | |
Brian Harring | 6be2efc | 2012-03-01 05:04:00 -0800 | [diff] [blame] | 285 | def main(argv): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 286 | # TODO(ferringb): make argv required once depot_tools is fixed. |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 287 | usage = """usage: %prog [options] [VAR1=val1 .. VARn=valn -- <args>] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 288 | |
| 289 | This script manages a local CrOS SDK chroot. Depending on the flags, |
| 290 | it can download, build or enter a chroot. |
| 291 | |
| 292 | Action taken is the following: |
| 293 | --enter (default) .. Installs and enters a chroot |
| 294 | --download .. Just download a chroot (enter if combined with --enter) |
| 295 | --bootstrap .. Builds a chroot from source (enter if --enter) |
| 296 | --delete .. Removes a chroot |
| 297 | """ |
| 298 | sdk_latest_version = GetLatestVersion() |
| 299 | parser = optparse.OptionParser(usage) |
| 300 | # Actions: |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 301 | parser.add_option('--bootstrap', |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 302 | action='store_true', dest='bootstrap', default=False, |
| 303 | help=('Build a new SDK chroot from source')) |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 304 | parser.add_option('--delete', |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 305 | action='store_true', dest='delete', default=False, |
| 306 | help=('Delete the current SDK chroot')) |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 307 | parser.add_option('--download', |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 308 | action='store_true', dest='download', default=False, |
| 309 | help=('Download and install a prebuilt SDK')) |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 310 | parser.add_option('--enter', |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 311 | action='store_true', dest='enter', default=False, |
| 312 | help=('Enter the SDK chroot, possibly (re)create first')) |
| 313 | |
| 314 | # Global options: |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 315 | parser.add_option('--chroot', |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 316 | dest='chroot', default=constants.DEFAULT_CHROOT_DIR, |
| 317 | help=('SDK chroot dir name [%s]' % |
| 318 | constants.DEFAULT_CHROOT_DIR)) |
| 319 | |
| 320 | # Additional options: |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 321 | parser.add_option('--chrome_root', |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 322 | dest='chrome_root', default='', |
| 323 | help=('Mount this chrome root into the SDK chroot')) |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 324 | parser.add_option('--chrome_root_mount', |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 325 | dest='chrome_root_mount', default='', |
| 326 | help=('Mount chrome into this path inside SDK chroot')) |
| 327 | parser.add_option('-r', '--replace', |
| 328 | action='store_true', dest='replace', default=False, |
| 329 | help=('Replace an existing SDK chroot')) |
| 330 | parser.add_option('-u', '--url', |
| 331 | dest='sdk_url', default='', |
| 332 | help=('''Use sdk tarball located at this url. |
| 333 | Use file:// for local files.''')) |
| 334 | parser.add_option('-v', '--version', |
| 335 | dest='sdk_version', default='', |
| 336 | help=('Use this sdk version [%s]' % sdk_latest_version)) |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 337 | parser.add_option('--debug', action='store_true', default=False, |
| 338 | help="Show debugging messages.") |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 339 | (options, remaining_arguments) = parser.parse_args(argv) |
| 340 | |
Brian Harring | 0ebc33c | 2012-05-25 13:36:19 -0700 | [diff] [blame] | 341 | # Setup logging levels first so any parsing triggered log messages |
| 342 | # are appropriately filtered. |
| 343 | logging.getLogger().setLevel( |
| 344 | logging.DEBUG if options.debug else logging.INFO) |
| 345 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 346 | # Some sanity checks first, before we ask for sudo credentials. |
| 347 | if cros_build_lib.IsInsideChroot(): |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 348 | parser.error("This needs to be ran outside the chroot") |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 349 | |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 350 | missing = CheckPrerequisites(NEEDED_TOOLS) |
| 351 | if missing: |
| 352 | parser.error(( |
| 353 | 'The tool(s) %s were not found.' |
| 354 | 'Please install the appropriate package in your host.' |
| 355 | 'Example(ubuntu):' |
| 356 | ' sudo apt-get install <packagename>' |
| 357 | % (', '.join(missing)))) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 358 | |
| 359 | # Default action is --enter, if no other is selected. |
| 360 | if not (options.bootstrap or options.download or options.delete): |
| 361 | options.enter = True |
| 362 | |
| 363 | # Only --enter can process additional args as passthrough commands. |
| 364 | # Warn and exit for least surprise. |
| 365 | if len(remaining_arguments) > 0 and not options.enter: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 366 | parser.error("Additional arguments are not permitted, unless running " |
| 367 | "with --enter") |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 368 | |
| 369 | # Some actions can be combined, as they merely modify how is the chroot |
| 370 | # going to be made. The only option that hates all others is --delete. |
| 371 | if options.delete and \ |
| 372 | (options.enter or options.download or options.bootstrap): |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 373 | parser.error("--delete cannot be combined with --enter, " |
| 374 | "--download or --bootstrap") |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 375 | # NOTE: --delete is a true hater, it doesn't like other options either, but |
| 376 | # those will hardly lead to confusion. Nobody can expect to pass --version to |
| 377 | # delete and actually change something. |
| 378 | |
| 379 | if options.bootstrap and options.download: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 380 | parser.error("Either --bootstrap or --download, not both") |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 381 | |
| 382 | # Bootstrap will start off from a non-selectable stage3 tarball. Attempts to |
| 383 | # select sdk by version are confusing. Warn and exit. We can still specify a |
| 384 | # tarball by path or URL though. |
| 385 | if options.bootstrap and options.sdk_version: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 386 | parser.error("Cannot use --version when bootstrapping") |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 387 | |
| 388 | chroot_path = os.path.join(SRC_ROOT, options.chroot) |
| 389 | chroot_path = os.path.abspath(chroot_path) |
| 390 | chroot_path = os.path.normpath(chroot_path) |
| 391 | |
| 392 | if not options.sdk_version: |
| 393 | sdk_version = sdk_latest_version |
| 394 | else: |
| 395 | sdk_version = options.sdk_version |
| 396 | |
| 397 | if options.delete and not os.path.exists(chroot_path): |
| 398 | 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] | 399 | return 0 |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 400 | |
| 401 | lock_path = os.path.dirname(chroot_path) |
| 402 | lock_path = os.path.join(lock_path, |
| 403 | '.%s_lock' % os.path.basename(chroot_path)) |
| 404 | with sudo.SudoKeepAlive(): |
Brian Harring | 4e6412d | 2012-03-09 20:54:02 -0800 | [diff] [blame] | 405 | with cgroups.SimpleContainChildren('cros_sdk'): |
Brian Harring | cfe762a | 2012-02-29 13:03:53 -0800 | [diff] [blame] | 406 | _CreateLockFile(lock_path) |
| 407 | with locking.FileLock(lock_path, 'chroot lock') as lock: |
| 408 | if options.delete: |
| 409 | lock.write_lock() |
| 410 | DeleteChroot(chroot_path) |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 411 | return 0 |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 412 | |
Brian Harring | cfe762a | 2012-02-29 13:03:53 -0800 | [diff] [blame] | 413 | # Print a suggestion for replacement, but not if running just --enter. |
| 414 | if os.path.exists(chroot_path) and not options.replace and \ |
| 415 | (options.bootstrap or options.download): |
| 416 | print "Chroot already exists. Run with --replace to re-create." |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 417 | |
Brian Harring | cfe762a | 2012-02-29 13:03:53 -0800 | [diff] [blame] | 418 | # Chroot doesn't exist or asked to replace. |
| 419 | if not os.path.exists(chroot_path) or options.replace: |
| 420 | lock.write_lock() |
| 421 | if options.bootstrap: |
| 422 | BootstrapChroot(chroot_path, options.sdk_url, |
| 423 | options.replace) |
| 424 | else: |
| 425 | CreateChroot(options.sdk_url, sdk_version, |
| 426 | chroot_path, options.replace) |
| 427 | if options.enter: |
| 428 | lock.read_lock() |
| 429 | EnterChroot(chroot_path, options.chrome_root, |
| 430 | options.chrome_root_mount, remaining_arguments) |