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 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 9 | import glob |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 10 | import os |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 11 | import pwd |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 12 | import sys |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 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 | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 17 | from chromite.lib import commandline |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 18 | from chromite.lib import cros_build_lib |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 19 | from chromite.lib import locking |
Josh Triplett | e759b23 | 2013-03-08 13:03:43 -0800 | [diff] [blame] | 20 | from chromite.lib import namespaces |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 21 | from chromite.lib import osutils |
Mike Frysinger | 8e727a3 | 2013-01-16 16:57:53 -0500 | [diff] [blame] | 22 | from chromite.lib import toolchain |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 23 | |
| 24 | cros_build_lib.STRICT_SUDO = True |
| 25 | |
| 26 | |
Zdenek Behan | aa52cea | 2012-05-30 01:31:11 +0200 | [diff] [blame] | 27 | COMPRESSION_PREFERENCE = ('xz', 'bz2') |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 28 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 29 | # TODO(zbehan): Remove the dependency on these, reimplement them in python |
Mike Frysinger | 648ba2d | 2013-01-08 14:19:34 -0500 | [diff] [blame] | 30 | MAKE_CHROOT = [os.path.join(constants.SOURCE_ROOT, |
| 31 | 'src/scripts/sdk_lib/make_chroot.sh')] |
| 32 | ENTER_CHROOT = [os.path.join(constants.SOURCE_ROOT, |
| 33 | 'src/scripts/sdk_lib/enter_chroot.sh')] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 34 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 35 | # Proxy simulator configuration. |
| 36 | PROXY_HOST_IP = '192.168.240.1' |
| 37 | PROXY_PORT = 8080 |
| 38 | PROXY_GUEST_IP = '192.168.240.2' |
| 39 | PROXY_NETMASK = 30 |
| 40 | PROXY_VETH_PREFIX = 'veth' |
| 41 | PROXY_CONNECT_PORTS = (80, 443, 9418) |
| 42 | PROXY_APACHE_FALLBACK_USERS = ('www-data', 'apache', 'nobody') |
| 43 | PROXY_APACHE_MPMS = ('event', 'worker', 'prefork') |
| 44 | PROXY_APACHE_FALLBACK_PATH = ':'.join( |
| 45 | '/usr/lib/apache2/mpm-%s' % mpm for mpm in PROXY_APACHE_MPMS |
| 46 | ) |
| 47 | PROXY_APACHE_MODULE_GLOBS = ('/usr/lib*/apache2/modules', '/usr/lib*/apache2') |
| 48 | |
Josh Triplett | 9a495f6 | 2013-03-15 18:06:55 -0700 | [diff] [blame] | 49 | # We need these tools to run. Very common tools (tar,..) are omitted. |
Josh Triplett | e759b23 | 2013-03-08 13:03:43 -0800 | [diff] [blame] | 50 | NEEDED_TOOLS = ('curl', 'xz') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 51 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 52 | # Tools needed for --proxy-sim only. |
| 53 | PROXY_NEEDED_TOOLS = ('ip',) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 54 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 55 | def GetArchStageTarballs(version): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 56 | """Returns the URL for a given arch/version""" |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 57 | extension = {'bz2':'tbz2', 'xz':'tar.xz'} |
Mike Frysinger | 8e727a3 | 2013-01-16 16:57:53 -0500 | [diff] [blame] | 58 | return [toolchain.GetSdkURL(suburl='cros-sdk-%s.%s' |
| 59 | % (version, extension[compressor])) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 60 | for compressor in COMPRESSION_PREFERENCE] |
| 61 | |
| 62 | |
| 63 | def GetStage3Urls(version): |
Mike Frysinger | 8e727a3 | 2013-01-16 16:57:53 -0500 | [diff] [blame] | 64 | return [toolchain.GetSdkURL(suburl='stage3-amd64-%s.tar.%s' % (version, ext)) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 65 | for ext in COMPRESSION_PREFERENCE] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 66 | |
| 67 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 68 | def FetchRemoteTarballs(storage_dir, urls): |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 69 | """Fetches a tarball given by url, and place it in sdk/. |
| 70 | |
| 71 | Args: |
| 72 | urls: List of URLs to try to download. Download will stop on first success. |
| 73 | |
| 74 | Returns: |
| 75 | Full path to the downloaded file |
| 76 | """ |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 77 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 78 | # Note we track content length ourselves since certain versions of curl |
| 79 | # fail if asked to resume a complete file. |
| 80 | # pylint: disable=C0301,W0631 |
| 81 | # 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] | 82 | for url in urls: |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 83 | # http://www.logilab.org/ticket/8766 |
| 84 | # pylint: disable=E1101 |
| 85 | parsed = urlparse.urlparse(url) |
| 86 | tarball_name = os.path.basename(parsed.path) |
| 87 | if parsed.scheme in ('', 'file'): |
| 88 | if os.path.exists(parsed.path): |
| 89 | return parsed.path |
| 90 | continue |
| 91 | content_length = 0 |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 92 | print 'Attempting download: %s' % url |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 93 | result = cros_build_lib.RunCurl( |
| 94 | ['-I', url], redirect_stdout=True, redirect_stderr=True, |
| 95 | print_cmd=False) |
| 96 | successful = False |
| 97 | for header in result.output.splitlines(): |
| 98 | # We must walk the output to find the string '200 OK' for use cases where |
| 99 | # a proxy is involved and may have pushed down the actual header. |
| 100 | if header.find('200 OK') != -1: |
| 101 | successful = True |
| 102 | elif header.lower().startswith("content-length:"): |
| 103 | content_length = int(header.split(":", 1)[-1].strip()) |
| 104 | if successful: |
| 105 | break |
| 106 | if successful: |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 107 | break |
| 108 | else: |
| 109 | raise Exception('No valid URLs found!') |
| 110 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 111 | tarball_dest = os.path.join(storage_dir, tarball_name) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 112 | current_size = 0 |
| 113 | if os.path.exists(tarball_dest): |
| 114 | current_size = os.path.getsize(tarball_dest) |
| 115 | if current_size > content_length: |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 116 | osutils.SafeUnlink(tarball_dest) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 117 | current_size = 0 |
Zdenek Behan | b2fa72e | 2012-03-16 04:49:30 +0100 | [diff] [blame] | 118 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 119 | if current_size < content_length: |
| 120 | cros_build_lib.RunCurl( |
| 121 | ['-f', '-L', '-y', '30', '-C', '-', '--output', tarball_dest, url], |
| 122 | print_cmd=False) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 123 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 124 | # Cleanup old tarballs now since we've successfull fetched; only cleanup |
| 125 | # the tarballs for our prefix, or unknown ones. |
| 126 | ignored_prefix = ('stage3-' if tarball_name.startswith('cros-sdk-') |
| 127 | else 'cros-sdk-') |
| 128 | for filename in os.listdir(storage_dir): |
| 129 | if filename == tarball_name or filename.startswith(ignored_prefix): |
| 130 | continue |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 131 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 132 | print 'Cleaning up old tarball: %s' % (filename,) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 133 | osutils.SafeUnlink(os.path.join(storage_dir, filename)) |
Zdenek Behan | 9c644dd | 2012-04-05 06:24:02 +0200 | [diff] [blame] | 134 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 135 | return tarball_dest |
| 136 | |
| 137 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 138 | def CreateChroot(chroot_path, sdk_tarball, cache_dir, nousepkg=False): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 139 | """Creates a new chroot from a given SDK""" |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 140 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 141 | cmd = MAKE_CHROOT + ['--stage3_path', sdk_tarball, |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 142 | '--chroot', chroot_path, |
| 143 | '--cache_dir', cache_dir] |
Mike Frysinger | 2de7f04 | 2012-07-10 04:45:03 -0400 | [diff] [blame] | 144 | if nousepkg: |
| 145 | cmd.append('--nousepkg') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 146 | |
| 147 | try: |
| 148 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 149 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 150 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 151 | |
| 152 | |
| 153 | def DeleteChroot(chroot_path): |
| 154 | """Deletes an existing chroot""" |
| 155 | cmd = MAKE_CHROOT + ['--chroot', chroot_path, |
| 156 | '--delete'] |
| 157 | try: |
| 158 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 159 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 160 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 161 | |
| 162 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 163 | def EnterChroot(chroot_path, cache_dir, chrome_root, chrome_root_mount, |
| 164 | additional_args): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 165 | """Enters an existing SDK chroot""" |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 166 | cmd = ENTER_CHROOT + ['--chroot', chroot_path, '--cache_dir', cache_dir] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 167 | if chrome_root: |
| 168 | cmd.extend(['--chrome_root', chrome_root]) |
| 169 | if chrome_root_mount: |
| 170 | cmd.extend(['--chrome_root_mount', chrome_root_mount]) |
| 171 | if len(additional_args) > 0: |
| 172 | cmd.append('--') |
| 173 | cmd.extend(additional_args) |
Brian Harring | 7199e7d | 2012-03-23 04:10:08 -0700 | [diff] [blame] | 174 | |
| 175 | ret = cros_build_lib.RunCommand(cmd, print_cmd=False, error_code_ok=True) |
| 176 | # If we were in interactive mode, ignore the exit code; it'll be whatever |
| 177 | # they last ran w/in the chroot and won't matter to us one way or another. |
| 178 | # Note this does allow chroot entrance to fail and be ignored during |
| 179 | # interactive; this is however a rare case and the user will immediately |
| 180 | # see it (nor will they be checking the exit code manually). |
| 181 | if ret.returncode != 0 and additional_args: |
| 182 | raise SystemExit('Running %r failed with exit code %i' |
| 183 | % (cmd, ret.returncode)) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 184 | |
| 185 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 186 | def _SudoCommand(): |
| 187 | """Get the 'sudo' command, along with all needed environment variables.""" |
| 188 | |
David James | 5a73b4d | 2013-03-07 10:23:40 -0800 | [diff] [blame] | 189 | # Pass in the ENVIRONMENT_WHITELIST and ENV_PASSTHRU variables so that |
| 190 | # scripts in the chroot know what variables to pass through. |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 191 | cmd = ['sudo'] |
David James | 5a73b4d | 2013-03-07 10:23:40 -0800 | [diff] [blame] | 192 | for key in constants.CHROOT_ENVIRONMENT_WHITELIST + constants.ENV_PASSTHRU: |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 193 | value = os.environ.get(key) |
| 194 | if value is not None: |
| 195 | cmd += ['%s=%s' % (key, value)] |
| 196 | |
| 197 | # Pass in the path to the depot_tools so that users can access them from |
| 198 | # within the chroot. |
Stefan Zager | 9c13a67 | 2013-08-30 12:24:10 -0700 | [diff] [blame] | 199 | cmd += ['DEPOT_TOOLS=%s' % cros_build_lib.FindDepotTools()] |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 200 | |
| 201 | return cmd |
| 202 | |
| 203 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 204 | def _ReportMissing(missing): |
| 205 | """Report missing utilities, then exit. |
| 206 | |
| 207 | Args: |
| 208 | missing: List of missing utilities, as returned by |
| 209 | osutils.FindMissingBinaries. If non-empty, will not return. |
| 210 | """ |
| 211 | |
| 212 | if missing: |
| 213 | raise SystemExit( |
| 214 | 'The tool(s) %s were not found.\n' |
| 215 | 'Please install the appropriate package in your host.\n' |
| 216 | 'Example(ubuntu):\n' |
| 217 | ' sudo apt-get install <packagename>' |
| 218 | % ', '.join(missing)) |
| 219 | |
| 220 | |
| 221 | def _ProxySimSetup(options): |
| 222 | """Set up proxy simulator, and return only in the child environment. |
| 223 | |
| 224 | TODO: Ideally, this should support multiple concurrent invocations of |
| 225 | cros_sdk --proxy-sim; currently, such invocations will conflict with each |
| 226 | other due to the veth device names and IP addresses. Either this code would |
| 227 | need to generate fresh, unused names for all of these before forking, or it |
| 228 | would need to support multiple concurrent cros_sdk invocations sharing one |
| 229 | proxy and allowing it to exit when unused (without counting on any local |
| 230 | service-management infrastructure on the host). |
| 231 | """ |
| 232 | |
| 233 | may_need_mpm = False |
| 234 | apache_bin = osutils.Which('apache2') |
| 235 | if apache_bin is None: |
| 236 | apache_bin = osutils.Which('apache2', PROXY_APACHE_FALLBACK_PATH) |
| 237 | if apache_bin is None: |
| 238 | _ReportMissing(('apache2',)) |
| 239 | else: |
| 240 | may_need_mpm = True |
| 241 | |
| 242 | # Module names and .so names included for ease of grepping. |
| 243 | apache_modules = [('proxy_module', 'mod_proxy.so'), |
| 244 | ('proxy_connect_module', 'mod_proxy_connect.so'), |
| 245 | ('proxy_http_module', 'mod_proxy_http.so'), |
| 246 | ('proxy_ftp_module', 'mod_proxy_ftp.so')] |
| 247 | |
| 248 | # Find the apache module directory, and make sure it has the modules we need. |
| 249 | module_dirs = {} |
| 250 | for g in PROXY_APACHE_MODULE_GLOBS: |
| 251 | for mod, so in apache_modules: |
| 252 | for f in glob.glob(os.path.join(g, so)): |
| 253 | module_dirs.setdefault(os.path.dirname(f), []).append(so) |
| 254 | for apache_module_path, modules_found in module_dirs.iteritems(): |
| 255 | if len(modules_found) == len(apache_modules): |
| 256 | break |
| 257 | else: |
| 258 | # Appease cros lint, which doesn't understand that this else block will not |
| 259 | # fall through to the subsequent code which relies on apache_module_path. |
| 260 | apache_module_path = None |
| 261 | raise SystemExit( |
| 262 | 'Could not find apache module path containing all required modules: %s' |
| 263 | % ', '.join(so for mod, so in apache_modules)) |
| 264 | |
| 265 | def check_add_module(name): |
| 266 | so = 'mod_%s.so' % name |
| 267 | if os.access(os.path.join(apache_module_path, so), os.F_OK): |
| 268 | mod = '%s_module' % name |
| 269 | apache_modules.append((mod, so)) |
| 270 | return True |
| 271 | return False |
| 272 | |
| 273 | check_add_module('authz_core') |
| 274 | if may_need_mpm: |
| 275 | for mpm in PROXY_APACHE_MPMS: |
| 276 | if check_add_module('mpm_%s' % mpm): |
| 277 | break |
| 278 | |
| 279 | veth_host = '%s-host' % PROXY_VETH_PREFIX |
| 280 | veth_guest = '%s-guest' % PROXY_VETH_PREFIX |
| 281 | |
| 282 | # Set up pipes from parent to child and vice versa. |
| 283 | # The child writes a byte to the parent after calling unshare, so that the |
| 284 | # parent can then assign the guest end of the veth interface to the child's |
| 285 | # new network namespace. The parent then writes a byte to the child after |
| 286 | # assigning the guest interface, so that the child can then configure that |
| 287 | # interface. In both cases, if we get back an EOF when reading from the |
| 288 | # pipe, we assume the other end exited with an error message, so just exit. |
| 289 | parent_readfd, child_writefd = os.pipe() |
| 290 | child_readfd, parent_writefd = os.pipe() |
| 291 | SUCCESS_FLAG = '+' |
| 292 | |
| 293 | pid = os.fork() |
| 294 | if not pid: |
| 295 | os.close(parent_readfd) |
| 296 | os.close(parent_writefd) |
| 297 | |
| 298 | namespaces.Unshare(namespaces.CLONE_NEWNET) |
| 299 | os.write(child_writefd, SUCCESS_FLAG) |
| 300 | os.close(child_writefd) |
| 301 | if os.read(child_readfd, 1) != SUCCESS_FLAG: |
| 302 | # Parent failed; it will have already have outputted an error message. |
| 303 | sys.exit(1) |
| 304 | os.close(child_readfd) |
| 305 | |
| 306 | # Set up child side of the network. |
| 307 | commands = ( |
| 308 | ('ip', 'address', 'add', |
| 309 | '%s/%u' % (PROXY_GUEST_IP, PROXY_NETMASK), |
| 310 | 'dev', veth_guest), |
| 311 | ('ip', 'link', 'set', veth_guest, 'up'), |
| 312 | ) |
| 313 | try: |
| 314 | for cmd in commands: |
| 315 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 316 | except cros_build_lib.RunCommandError: |
| 317 | raise SystemExit('Running %r failed!' % (cmd,)) |
| 318 | |
| 319 | proxy_url = 'http://%s:%u' % (PROXY_HOST_IP, PROXY_PORT) |
| 320 | for proto in ('http', 'https', 'ftp'): |
| 321 | os.environ[proto + '_proxy'] = proxy_url |
| 322 | for v in ('all_proxy', 'RSYNC_PROXY', 'no_proxy'): |
| 323 | os.environ.pop(v, None) |
| 324 | return |
| 325 | |
| 326 | os.close(child_readfd) |
| 327 | os.close(child_writefd) |
| 328 | |
| 329 | if os.read(parent_readfd, 1) != SUCCESS_FLAG: |
| 330 | # Child failed; it will have already have outputted an error message. |
| 331 | sys.exit(1) |
| 332 | os.close(parent_readfd) |
| 333 | |
| 334 | # Set up parent side of the network. |
| 335 | uid = int(os.environ.get('SUDO_UID', '0')) |
| 336 | gid = int(os.environ.get('SUDO_GID', '0')) |
| 337 | if uid == 0 or gid == 0: |
| 338 | for username in PROXY_APACHE_FALLBACK_USERS: |
| 339 | try: |
| 340 | pwnam = pwd.getpwnam(username) |
| 341 | uid, gid = pwnam.pw_uid, pwnam.pw_gid |
| 342 | break |
| 343 | except KeyError: |
| 344 | continue |
| 345 | if uid == 0 or gid == 0: |
| 346 | raise SystemExit('Could not find a non-root user to run Apache as') |
| 347 | |
| 348 | chroot_parent, chroot_base = os.path.split(options.chroot) |
| 349 | pid_file = os.path.join(chroot_parent, '.%s-apache-proxy.pid' % chroot_base) |
| 350 | log_file = os.path.join(chroot_parent, '.%s-apache-proxy.log' % chroot_base) |
| 351 | |
| 352 | apache_directives = [ |
| 353 | 'User #%u' % uid, |
| 354 | 'Group #%u' % gid, |
| 355 | 'PidFile %s' % pid_file, |
| 356 | 'ErrorLog %s' % log_file, |
| 357 | 'Listen %s:%u' % (PROXY_HOST_IP, PROXY_PORT), |
| 358 | 'ServerName %s' % PROXY_HOST_IP, |
| 359 | 'ProxyRequests On', |
| 360 | 'AllowCONNECT %s' % ' '.join(map(str, PROXY_CONNECT_PORTS)), |
| 361 | ] + [ |
| 362 | 'LoadModule %s %s' % (mod, os.path.join(apache_module_path, so)) |
| 363 | for (mod, so) in apache_modules |
| 364 | ] |
| 365 | commands = ( |
| 366 | ('ip', 'link', 'add', 'name', veth_host, |
| 367 | 'type', 'veth', 'peer', 'name', veth_guest), |
| 368 | ('ip', 'address', 'add', |
| 369 | '%s/%u' % (PROXY_HOST_IP, PROXY_NETMASK), |
| 370 | 'dev', veth_host), |
| 371 | ('ip', 'link', 'set', veth_host, 'up'), |
| 372 | [apache_bin, '-f', '/dev/null'] |
| 373 | + [arg for d in apache_directives for arg in ('-C', d)], |
| 374 | ('ip', 'link', 'set', veth_guest, 'netns', str(pid)), |
| 375 | ) |
| 376 | cmd = None # Make cros lint happy. |
| 377 | try: |
| 378 | for cmd in commands: |
| 379 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 380 | except cros_build_lib.RunCommandError: |
| 381 | # Clean up existing interfaces, if any. |
| 382 | cmd_cleanup = ('ip', 'link', 'del', veth_host) |
| 383 | try: |
| 384 | cros_build_lib.RunCommand(cmd_cleanup, print_cmd=False) |
| 385 | except cros_build_lib.RunCommandError: |
| 386 | cros_build_lib.Error('running %r failed', cmd_cleanup) |
| 387 | raise SystemExit('Running %r failed!' % (cmd,)) |
| 388 | os.write(parent_writefd, SUCCESS_FLAG) |
| 389 | os.close(parent_writefd) |
| 390 | |
| 391 | sys.exit(os.waitpid(pid, 0)[1]) |
| 392 | |
| 393 | |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 394 | def _ReExecuteIfNeeded(argv): |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 395 | """Re-execute cros_sdk as root. |
| 396 | |
| 397 | Also unshare the mount namespace so as to ensure that processes outside |
| 398 | the chroot can't mess with our mounts. |
| 399 | """ |
| 400 | if os.geteuid() != 0: |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 401 | cmd = _SudoCommand() + ['--'] + argv |
| 402 | os.execvp(cmd[0], cmd) |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 403 | else: |
Josh Triplett | e759b23 | 2013-03-08 13:03:43 -0800 | [diff] [blame] | 404 | cgroups.Cgroup.InitSystem() |
| 405 | namespaces.Unshare(namespaces.CLONE_NEWNS) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 406 | |
| 407 | |
Brian Harring | 6be2efc | 2012-03-01 05:04:00 -0800 | [diff] [blame] | 408 | def main(argv): |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 409 | usage = """usage: %prog [options] [VAR1=val1 .. VARn=valn -- args] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 410 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 411 | This script is used for manipulating local chroot environments; creating, |
| 412 | deleting, downloading, etc. If given --enter (or no args), it defaults |
| 413 | to an interactive bash shell within the chroot. |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 414 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 415 | If given args those are passed to the chroot environment, and executed.""" |
Mike Frysinger | 648ba2d | 2013-01-08 14:19:34 -0500 | [diff] [blame] | 416 | conf = cros_build_lib.LoadKeyValueFile( |
| 417 | os.path.join(constants.SOURCE_ROOT, constants.SDK_VERSION_FILE), |
| 418 | ignore_missing=True) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 419 | sdk_latest_version = conf.get('SDK_LATEST_VERSION', '<unknown>') |
| 420 | bootstrap_latest_version = conf.get('BOOTSTRAP_LATEST_VERSION', '<unknown>') |
| 421 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 422 | parser = commandline.OptionParser(usage=usage, caching=True) |
| 423 | |
| 424 | commands = parser.add_option_group("Commands") |
| 425 | commands.add_option( |
| 426 | '--enter', action='store_true', default=False, |
| 427 | help='Enter the SDK chroot. Implies --create.') |
| 428 | commands.add_option( |
| 429 | '--create', action='store_true',default=False, |
| 430 | help='Create the chroot only if it does not already exist. ' |
| 431 | 'Implies --download.') |
| 432 | commands.add_option( |
| 433 | '--bootstrap', action='store_true', default=False, |
| 434 | help='Build everything from scratch, including the sdk. ' |
| 435 | 'Use this only if you need to validate a change ' |
| 436 | 'that affects SDK creation itself (toolchain and ' |
| 437 | 'build are typically the only folk who need this). ' |
| 438 | 'Note this will quite heavily slow down the build. ' |
| 439 | 'This option implies --create --nousepkg.') |
| 440 | commands.add_option( |
| 441 | '-r', '--replace', action='store_true', default=False, |
| 442 | help='Replace an existing SDK chroot. Basically an alias ' |
| 443 | 'for --delete --create.') |
| 444 | commands.add_option( |
| 445 | '--delete', action='store_true', default=False, |
| 446 | help='Delete the current SDK chroot if it exists.') |
| 447 | commands.add_option( |
| 448 | '--download', action='store_true', default=False, |
| 449 | help='Download the sdk.') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 450 | |
| 451 | # Global options: |
Mike Frysinger | 648ba2d | 2013-01-08 14:19:34 -0500 | [diff] [blame] | 452 | default_chroot = os.path.join(constants.SOURCE_ROOT, |
| 453 | constants.DEFAULT_CHROOT_DIR) |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 454 | parser.add_option( |
| 455 | '--chroot', dest='chroot', default=default_chroot, type='path', |
| 456 | help=('SDK chroot dir name [%s]' % constants.DEFAULT_CHROOT_DIR)) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 457 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 458 | parser.add_option('--chrome_root', default=None, type='path', |
| 459 | help='Mount this chrome root into the SDK chroot') |
| 460 | parser.add_option('--chrome_root_mount', default=None, type='path', |
| 461 | help='Mount chrome into this path inside SDK chroot') |
| 462 | parser.add_option('--nousepkg', action='store_true', default=False, |
| 463 | help='Do not use binary packages when creating a chroot.') |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 464 | parser.add_option('--proxy-sim', action='store_true', default=False, |
| 465 | help='Simulate a restrictive network requiring an outbound' |
| 466 | ' proxy.') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 467 | parser.add_option('-u', '--url', |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 468 | dest='sdk_url', default=None, |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 469 | help=('''Use sdk tarball located at this url. |
| 470 | Use file:// for local files.''')) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 471 | parser.add_option('--sdk-version', default=None, |
| 472 | help='Use this sdk version. For prebuilt, current is %r' |
| 473 | ', for bootstrapping its %r.' |
| 474 | % (sdk_latest_version, bootstrap_latest_version)) |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 475 | options, chroot_command = parser.parse_args(argv) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 476 | |
| 477 | # Some sanity checks first, before we ask for sudo credentials. |
Mike Frysinger | 8fd67dc | 2012-12-03 23:51:18 -0500 | [diff] [blame] | 478 | cros_build_lib.AssertOutsideChroot() |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 479 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 480 | host = os.uname()[4] |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 481 | if host != 'x86_64': |
| 482 | parser.error( |
| 483 | "cros_sdk is currently only supported on x86_64; you're running" |
| 484 | " %s. Please find a x86_64 machine." % (host,)) |
| 485 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 486 | _ReportMissing(osutils.FindMissingBinaries(NEEDED_TOOLS)) |
| 487 | if options.proxy_sim: |
| 488 | _ReportMissing(osutils.FindMissingBinaries(PROXY_NEEDED_TOOLS)) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 489 | |
David James | 471532c | 2013-01-21 10:23:31 -0800 | [diff] [blame] | 490 | _ReExecuteIfNeeded([sys.argv[0]] + argv) |
| 491 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 492 | # Expand out the aliases... |
| 493 | if options.replace: |
| 494 | options.delete = options.create = True |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 495 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 496 | if options.bootstrap: |
| 497 | options.create = True |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 498 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 499 | # If a command is not given, default to enter. |
| 500 | options.enter |= not any(getattr(options, x.dest) |
| 501 | for x in commands.option_list) |
| 502 | options.enter |= bool(chroot_command) |
| 503 | |
| 504 | if options.enter and options.delete and not options.create: |
| 505 | parser.error("Trying to enter the chroot when --delete " |
| 506 | "was specified makes no sense.") |
| 507 | |
| 508 | # Finally, discern if we need to create the chroot. |
| 509 | chroot_exists = os.path.exists(options.chroot) |
| 510 | if options.create or options.enter: |
| 511 | # Only create if it's being wiped, or if it doesn't exist. |
| 512 | if not options.delete and chroot_exists: |
| 513 | options.create = False |
| 514 | else: |
| 515 | options.download = True |
| 516 | |
| 517 | # Finally, flip create if necessary. |
| 518 | if options.enter: |
| 519 | options.create |= not chroot_exists |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 520 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 521 | if not options.sdk_version: |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 522 | sdk_version = (bootstrap_latest_version if options.bootstrap |
| 523 | else sdk_latest_version) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 524 | else: |
| 525 | sdk_version = options.sdk_version |
| 526 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 527 | # Based on selections, fetch the tarball. |
| 528 | if options.sdk_url: |
| 529 | urls = [options.sdk_url] |
| 530 | elif options.bootstrap: |
| 531 | urls = GetStage3Urls(sdk_version) |
| 532 | else: |
| 533 | urls = GetArchStageTarballs(sdk_version) |
| 534 | |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 535 | lock_path = os.path.dirname(options.chroot) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 536 | lock_path = os.path.join(lock_path, |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 537 | '.%s_lock' % os.path.basename(options.chroot)) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 538 | with cgroups.SimpleContainChildren('cros_sdk'): |
| 539 | with locking.FileLock(lock_path, 'chroot lock') as lock: |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 540 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 541 | if options.proxy_sim: |
| 542 | _ProxySimSetup(options) |
| 543 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 544 | if options.delete and os.path.exists(options.chroot): |
| 545 | lock.write_lock() |
| 546 | DeleteChroot(options.chroot) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 547 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 548 | sdk_cache = os.path.join(options.cache_dir, 'sdks') |
| 549 | distfiles_cache = os.path.join(options.cache_dir, 'distfiles') |
| 550 | osutils.SafeMakedirs(options.cache_dir) |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 551 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 552 | for target in (sdk_cache, distfiles_cache): |
Mike Frysinger | 648ba2d | 2013-01-08 14:19:34 -0500 | [diff] [blame] | 553 | src = os.path.join(constants.SOURCE_ROOT, os.path.basename(target)) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 554 | if not os.path.exists(src): |
| 555 | osutils.SafeMakedirs(target) |
| 556 | continue |
| 557 | lock.write_lock( |
| 558 | "Upgrade to %r needed but chroot is locked; please exit " |
| 559 | "all instances so this upgrade can finish." % src) |
| 560 | if not os.path.exists(src): |
| 561 | # Note that while waiting for the write lock, src may've vanished; |
| 562 | # it's a rare race during the upgrade process that's a byproduct |
| 563 | # of us avoiding taking a write lock to do the src check. If we |
| 564 | # took a write lock for that check, it would effectively limit |
| 565 | # all cros_sdk for a chroot to a single instance. |
| 566 | osutils.SafeMakedirs(target) |
| 567 | elif not os.path.exists(target): |
| 568 | # Upgrade occurred, but a reversion, or something whacky |
| 569 | # occurred writing to the old location. Wipe and continue. |
| 570 | os.rename(src, target) |
| 571 | else: |
| 572 | # Upgrade occurred once already, but either a reversion or |
| 573 | # some before/after separate cros_sdk usage is at play. |
| 574 | # Wipe and continue. |
| 575 | osutils.RmDir(src) |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 576 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 577 | if options.download: |
| 578 | lock.write_lock() |
| 579 | sdk_tarball = FetchRemoteTarballs(sdk_cache, urls) |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 580 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 581 | if options.create: |
| 582 | lock.write_lock() |
| 583 | CreateChroot(options.chroot, sdk_tarball, options.cache_dir, |
| 584 | nousepkg=(options.bootstrap or options.nousepkg)) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 585 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 586 | if options.enter: |
| 587 | lock.read_lock() |
| 588 | EnterChroot(options.chroot, options.cache_dir, options.chrome_root, |
| 589 | options.chrome_root_mount, chroot_command) |