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