Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2015 The ChromiumOS Authors |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Alex Klein | aaddc93 | 2020-01-30 15:02:24 -0700 | [diff] [blame] | 5 | """Deploy packages onto a target device. |
| 6 | |
| 7 | Integration tests for this file can be found at cli/cros/tests/cros_vm_tests.py. |
| 8 | See that file for more information. |
| 9 | """ |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 10 | |
Mike Frysinger | 93e8ffa | 2019-07-03 20:24:18 -0400 | [diff] [blame] | 11 | from __future__ import division |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 12 | |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 13 | import bz2 |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 14 | import fnmatch |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 15 | import functools |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 16 | import json |
Chris McDonald | 14ac61d | 2021-07-21 11:49:56 -0600 | [diff] [blame] | 17 | import logging |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 18 | import os |
Jae Hoon Kim | 2376e14 | 2022-09-03 00:18:58 +0000 | [diff] [blame] | 19 | from pathlib import Path |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 20 | import tempfile |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 21 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 22 | from chromite.cli import command |
Mike Frysinger | 06a51c8 | 2021-04-06 11:39:17 -0400 | [diff] [blame] | 23 | from chromite.lib import build_target_lib |
Ram Chandrasekar | 56152ec | 2021-11-22 17:10:41 +0000 | [diff] [blame] | 24 | from chromite.lib import constants |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 25 | from chromite.lib import cros_build_lib |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 26 | from chromite.lib import dlc_lib |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 27 | from chromite.lib import operation |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 28 | from chromite.lib import osutils |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 29 | from chromite.lib import portage_util |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 30 | from chromite.lib import remote_access |
Kimiyuki Onaka | a4ec7f6 | 2020-08-25 13:58:48 +0900 | [diff] [blame] | 31 | from chromite.lib import workon_helper |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 32 | from chromite.lib.parser import package_info |
| 33 | |
Chris McDonald | 14ac61d | 2021-07-21 11:49:56 -0600 | [diff] [blame] | 34 | |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 35 | try: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 36 | import portage |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 37 | except ImportError: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | if cros_build_lib.IsInsideChroot(): |
| 39 | raise |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 40 | |
| 41 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 42 | _DEVICE_BASE_DIR = "/usr/local/tmp/cros-deploy" |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 43 | # This is defined in src/platform/dev/builder.py |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 44 | _STRIPPED_PACKAGES_DIR = "stripped-packages" |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 45 | |
| 46 | _MAX_UPDATES_NUM = 10 |
| 47 | _MAX_UPDATES_WARNING = ( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 48 | "You are about to update a large number of installed packages, which " |
| 49 | "might take a long time, fail midway, or leave the target in an " |
| 50 | "inconsistent state. It is highly recommended that you flash a new image " |
| 51 | "instead." |
| 52 | ) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 53 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 54 | _DLC_ID = "DLC_ID" |
| 55 | _DLC_PACKAGE = "DLC_PACKAGE" |
| 56 | _DLC_ENABLED = "DLC_ENABLED" |
| 57 | _ENVIRONMENT_FILENAME = "environment.bz2" |
| 58 | _DLC_INSTALL_ROOT = "/var/cache/dlc" |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 59 | |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 60 | |
| 61 | class DeployError(Exception): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | """Thrown when an unrecoverable error is encountered during deploy.""" |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 63 | |
| 64 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 65 | class BrilloDeployOperation(operation.ProgressBarOperation): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 66 | """ProgressBarOperation specific for brillo deploy.""" |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 67 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 68 | # These two variables are used to validate the output in the VM integration |
| 69 | # tests. Changes to the output must be reflected here. |
| 70 | MERGE_EVENTS = ( |
| 71 | "Preparing local packages", |
| 72 | "NOTICE: Copying binpkgs", |
| 73 | "NOTICE: Installing", |
| 74 | "been installed.", |
| 75 | "Please restart any updated", |
| 76 | ) |
| 77 | UNMERGE_EVENTS = ( |
| 78 | "NOTICE: Unmerging", |
| 79 | "been uninstalled.", |
| 80 | "Please restart any updated", |
| 81 | ) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 82 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 83 | def __init__(self, emerge): |
| 84 | """Construct BrilloDeployOperation object. |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 85 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 86 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 87 | emerge: True if emerge, False is unmerge. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 88 | """ |
| 89 | super().__init__() |
| 90 | if emerge: |
| 91 | self._events = self.MERGE_EVENTS |
| 92 | else: |
| 93 | self._events = self.UNMERGE_EVENTS |
| 94 | self._total = len(self._events) |
| 95 | self._completed = 0 |
| 96 | |
| 97 | def ParseOutput(self, output=None): |
| 98 | """Parse the output of brillo deploy to update a progress bar.""" |
| 99 | stdout = self._stdout.read() |
| 100 | stderr = self._stderr.read() |
| 101 | output = stdout + stderr |
| 102 | for event in self._events: |
| 103 | self._completed += output.count(event) |
| 104 | self.ProgressBar(self._completed / self._total) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 105 | |
| 106 | |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 107 | class _InstallPackageScanner(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | """Finds packages that need to be installed on a target device. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 109 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 110 | Scans the sysroot bintree, beginning with a user-provided list of packages, |
| 111 | to find all packages that need to be installed. If so instructed, |
| 112 | transitively scans forward (mandatory) and backward (optional) dependencies |
| 113 | as well. A package will be installed if missing on the target (mandatory |
| 114 | packages only), or it will be updated if its sysroot version and build time |
| 115 | are different from the target. Common usage: |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 116 | |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 117 | pkg_scanner = _InstallPackageScanner(sysroot) |
| 118 | pkgs = pkg_scanner.Run(...) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 119 | """ |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 120 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 121 | class VartreeError(Exception): |
| 122 | """An error in the processing of the installed packages tree.""" |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 123 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 124 | class BintreeError(Exception): |
| 125 | """An error in the processing of the source binpkgs tree.""" |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 126 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 127 | class PkgInfo(object): |
| 128 | """A record containing package information.""" |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 129 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 130 | __slots__ = ("cpv", "build_time", "rdeps_raw", "rdeps", "rev_rdeps") |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 131 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 132 | def __init__( |
| 133 | self, cpv, build_time, rdeps_raw, rdeps=None, rev_rdeps=None |
| 134 | ): |
| 135 | self.cpv = cpv |
| 136 | self.build_time = build_time |
| 137 | self.rdeps_raw = rdeps_raw |
| 138 | self.rdeps = set() if rdeps is None else rdeps |
| 139 | self.rev_rdeps = set() if rev_rdeps is None else rev_rdeps |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 140 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 141 | # Python snippet for dumping vartree info on the target. Instantiate using |
| 142 | # _GetVartreeSnippet(). |
| 143 | _GET_VARTREE = """ |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 144 | import json |
Gwendal Grignou | 99e6f53 | 2018-10-25 12:16:28 -0700 | [diff] [blame] | 145 | import os |
| 146 | import portage |
| 147 | |
| 148 | # Normalize the path to match what portage will index. |
| 149 | target_root = os.path.normpath('%(root)s') |
| 150 | if not target_root.endswith('/'): |
| 151 | target_root += '/' |
| 152 | trees = portage.create_trees(target_root=target_root, config_root='/') |
| 153 | vartree = trees[target_root]['vartree'] |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 154 | pkg_info = [] |
| 155 | for cpv in vartree.dbapi.cpv_all(): |
| 156 | slot, rdep_raw, build_time = vartree.dbapi.aux_get( |
| 157 | cpv, ('SLOT', 'RDEPEND', 'BUILD_TIME')) |
| 158 | pkg_info.append((cpv, slot, rdep_raw, build_time)) |
| 159 | |
| 160 | print(json.dumps(pkg_info)) |
| 161 | """ |
| 162 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | def __init__(self, sysroot): |
| 164 | self.sysroot = sysroot |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 165 | # Members containing the sysroot (binpkg) and target (installed) package |
| 166 | # DB. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 167 | self.target_db = None |
| 168 | self.binpkgs_db = None |
| 169 | # Members for managing the dependency resolution work queue. |
| 170 | self.queue = None |
| 171 | self.seen = None |
| 172 | self.listed = None |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 173 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 174 | @staticmethod |
| 175 | def _GetCP(cpv): |
| 176 | """Returns the CP value for a given CPV string.""" |
| 177 | attrs = package_info.SplitCPV(cpv, strict=False) |
| 178 | if not attrs.cp: |
| 179 | raise ValueError("Cannot get CP value for %s" % cpv) |
| 180 | return attrs.cp |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 181 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 182 | @staticmethod |
| 183 | def _InDB(cp, slot, db): |
| 184 | """Returns whether CP and slot are found in a database (if provided).""" |
| 185 | cp_slots = db.get(cp) if db else None |
| 186 | return cp_slots is not None and (not slot or slot in cp_slots) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 187 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 188 | @staticmethod |
| 189 | def _AtomStr(cp, slot): |
| 190 | """Returns 'CP:slot' if slot is non-empty, else just 'CP'.""" |
| 191 | return "%s:%s" % (cp, slot) if slot else cp |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 192 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 193 | @classmethod |
| 194 | def _GetVartreeSnippet(cls, root="/"): |
| 195 | """Returns a code snippet for dumping the vartree on the target. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 196 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 197 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 198 | root: The installation root. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 199 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 200 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 201 | The said code snippet (string) with parameters filled in. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 202 | """ |
| 203 | return cls._GET_VARTREE % {"root": root} |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 204 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 205 | @classmethod |
| 206 | def _StripDepAtom(cls, dep_atom, installed_db=None): |
| 207 | """Strips a dependency atom and returns a (CP, slot) pair.""" |
| 208 | # TODO(garnold) This is a gross simplification of ebuild dependency |
| 209 | # semantics, stripping and ignoring various qualifiers (versions, slots, |
| 210 | # USE flag, negation) and will likely need to be fixed. chromium:447366. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 211 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 212 | # Ignore unversioned blockers, leaving them for the user to resolve. |
| 213 | if dep_atom[0] == "!" and dep_atom[1] not in "<=>~": |
| 214 | return None, None |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 215 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 216 | cp = dep_atom |
| 217 | slot = None |
| 218 | require_installed = False |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 219 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 220 | # Versioned blockers should be updated, but only if already installed. |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 221 | # These are often used for forcing cascaded updates of multiple |
| 222 | # packages, so we're treating them as ordinary constraints with hopes |
| 223 | # that it'll lead to the desired result. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 224 | if cp.startswith("!"): |
| 225 | cp = cp.lstrip("!") |
| 226 | require_installed = True |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 227 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 228 | # Remove USE flags. |
| 229 | if "[" in cp: |
| 230 | cp = cp[: cp.index("[")] + cp[cp.index("]") + 1 :] |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 231 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 232 | # Separate the slot qualifier and strip off subslots. |
| 233 | if ":" in cp: |
| 234 | cp, slot = cp.split(":") |
| 235 | for delim in ("/", "="): |
| 236 | slot = slot.split(delim, 1)[0] |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 237 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 238 | # Strip version wildcards (right), comparators (left). |
| 239 | cp = cp.rstrip("*") |
| 240 | cp = cp.lstrip("<=>~") |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 241 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 242 | # Turn into CP form. |
| 243 | cp = cls._GetCP(cp) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 244 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 245 | if require_installed and not cls._InDB(cp, None, installed_db): |
| 246 | return None, None |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 247 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 248 | return cp, slot |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 249 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 250 | @classmethod |
| 251 | def _ProcessDepStr(cls, dep_str, installed_db, avail_db): |
| 252 | """Resolves and returns a list of dependencies from a dependency string. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 253 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 254 | This parses a dependency string and returns a list of package names and |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 255 | slots. Other atom qualifiers (version, sub-slot, block) are ignored. |
| 256 | When resolving disjunctive deps, we include all choices that are fully |
| 257 | present in |installed_db|. If none is present, we choose an arbitrary |
| 258 | one that is available. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 259 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 260 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 261 | dep_str: A raw dependency string. |
| 262 | installed_db: A database of installed packages. |
| 263 | avail_db: A database of packages available for installation. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 264 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 265 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 266 | A list of pairs (CP, slot). |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 267 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 268 | Raises: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 269 | ValueError: the dependencies string is malformed. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 270 | """ |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 271 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 272 | def ProcessSubDeps(dep_exp, disjunct): |
| 273 | """Parses and processes a dependency (sub)expression.""" |
| 274 | deps = set() |
| 275 | default_deps = set() |
| 276 | sub_disjunct = False |
| 277 | for dep_sub_exp in dep_exp: |
| 278 | sub_deps = set() |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 279 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 280 | if isinstance(dep_sub_exp, (list, tuple)): |
| 281 | sub_deps = ProcessSubDeps(dep_sub_exp, sub_disjunct) |
| 282 | sub_disjunct = False |
| 283 | elif sub_disjunct: |
| 284 | raise ValueError("Malformed disjunctive operation in deps") |
| 285 | elif dep_sub_exp == "||": |
| 286 | sub_disjunct = True |
| 287 | elif dep_sub_exp.endswith("?"): |
| 288 | raise ValueError("Dependencies contain a conditional") |
| 289 | else: |
| 290 | cp, slot = cls._StripDepAtom(dep_sub_exp, installed_db) |
| 291 | if cp: |
| 292 | sub_deps = set([(cp, slot)]) |
| 293 | elif disjunct: |
| 294 | raise ValueError("Atom in disjunct ignored") |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 295 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 296 | # Handle sub-deps of a disjunctive expression. |
| 297 | if disjunct: |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 298 | # Make the first available choice the default, for use in |
| 299 | # case that no option is installed. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 300 | if ( |
| 301 | not default_deps |
| 302 | and avail_db is not None |
| 303 | and all( |
| 304 | cls._InDB(cp, slot, avail_db) |
| 305 | for cp, slot in sub_deps |
| 306 | ) |
| 307 | ): |
| 308 | default_deps = sub_deps |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 309 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 310 | # If not all sub-deps are installed, then don't consider |
| 311 | # them. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 312 | if not all( |
| 313 | cls._InDB(cp, slot, installed_db) |
| 314 | for cp, slot in sub_deps |
| 315 | ): |
| 316 | sub_deps = set() |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 317 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 318 | deps.update(sub_deps) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 319 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 320 | return deps or default_deps |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 321 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 322 | try: |
| 323 | return ProcessSubDeps(portage.dep.paren_reduce(dep_str), False) |
| 324 | except portage.exception.InvalidDependString as e: |
| 325 | raise ValueError("Invalid dep string: %s" % e) |
| 326 | except ValueError as e: |
| 327 | raise ValueError("%s: %s" % (e, dep_str)) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 328 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 329 | def _BuildDB( |
| 330 | self, cpv_info, process_rdeps, process_rev_rdeps, installed_db=None |
| 331 | ): |
| 332 | """Returns a database of packages given a list of CPV info. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 333 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 334 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 335 | cpv_info: A list of tuples containing package CPV and attributes. |
| 336 | process_rdeps: Whether to populate forward dependencies. |
| 337 | process_rev_rdeps: Whether to populate reverse dependencies. |
| 338 | installed_db: A database of installed packages for filtering |
| 339 | disjunctive choices against; if None, using own built database. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 340 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 341 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 342 | A map from CP values to another dictionary that maps slots |
| 343 | to package attribute tuples. Tuples contain a CPV value |
| 344 | (string), build time (string), runtime dependencies (set), |
| 345 | and reverse dependencies (set, empty if not populated). |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 346 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 347 | Raises: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 348 | ValueError: If more than one CPV occupies a single slot. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 349 | """ |
| 350 | db = {} |
| 351 | logging.debug("Populating package DB...") |
| 352 | for cpv, slot, rdeps_raw, build_time in cpv_info: |
| 353 | cp = self._GetCP(cpv) |
| 354 | cp_slots = db.setdefault(cp, dict()) |
| 355 | if slot in cp_slots: |
| 356 | raise ValueError( |
| 357 | "More than one package found for %s" |
| 358 | % self._AtomStr(cp, slot) |
| 359 | ) |
| 360 | logging.debug( |
| 361 | " %s -> %s, built %s, raw rdeps: %s", |
| 362 | self._AtomStr(cp, slot), |
| 363 | cpv, |
| 364 | build_time, |
| 365 | rdeps_raw, |
| 366 | ) |
| 367 | cp_slots[slot] = self.PkgInfo(cpv, build_time, rdeps_raw) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 368 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 369 | avail_db = db |
| 370 | if installed_db is None: |
| 371 | installed_db = db |
| 372 | avail_db = None |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 373 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 374 | # Add approximate forward dependencies. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 375 | if process_rdeps: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 376 | logging.debug("Populating forward dependencies...") |
| 377 | for cp, cp_slots in db.items(): |
| 378 | for slot, pkg_info in cp_slots.items(): |
| 379 | pkg_info.rdeps.update( |
| 380 | self._ProcessDepStr( |
| 381 | pkg_info.rdeps_raw, installed_db, avail_db |
| 382 | ) |
| 383 | ) |
| 384 | logging.debug( |
| 385 | " %s (%s) processed rdeps: %s", |
| 386 | self._AtomStr(cp, slot), |
| 387 | pkg_info.cpv, |
| 388 | " ".join( |
| 389 | [ |
| 390 | self._AtomStr(rdep_cp, rdep_slot) |
| 391 | for rdep_cp, rdep_slot in pkg_info.rdeps |
| 392 | ] |
| 393 | ), |
| 394 | ) |
| 395 | |
| 396 | # Add approximate reverse dependencies (optional). |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 397 | if process_rev_rdeps: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 398 | logging.debug("Populating reverse dependencies...") |
| 399 | for cp, cp_slots in db.items(): |
| 400 | for slot, pkg_info in cp_slots.items(): |
| 401 | for rdep_cp, rdep_slot in pkg_info.rdeps: |
| 402 | to_slots = db.get(rdep_cp) |
| 403 | if not to_slots: |
| 404 | continue |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 405 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 406 | for to_slot, to_pkg_info in to_slots.items(): |
| 407 | if rdep_slot and to_slot != rdep_slot: |
| 408 | continue |
| 409 | logging.debug( |
| 410 | " %s (%s) added as rev rdep for %s (%s)", |
| 411 | self._AtomStr(cp, slot), |
| 412 | pkg_info.cpv, |
| 413 | self._AtomStr(rdep_cp, to_slot), |
| 414 | to_pkg_info.cpv, |
| 415 | ) |
| 416 | to_pkg_info.rev_rdeps.add((cp, slot)) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 417 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 418 | return db |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 419 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 420 | def _InitTargetVarDB(self, device, root, process_rdeps, process_rev_rdeps): |
| 421 | """Initializes a dictionary of packages installed on |device|.""" |
| 422 | get_vartree_script = self._GetVartreeSnippet(root) |
| 423 | try: |
Mike Frysinger | c0780a6 | 2022-08-29 04:41:56 -0400 | [diff] [blame] | 424 | result = device.agent.RemoteSh( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 425 | ["python"], remote_sudo=True, input=get_vartree_script |
| 426 | ) |
| 427 | except cros_build_lib.RunCommandError as e: |
| 428 | logging.error("Cannot get target vartree:\n%s", e.stderr) |
| 429 | raise |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 430 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 431 | try: |
| 432 | self.target_db = self._BuildDB( |
| 433 | json.loads(result.stdout), process_rdeps, process_rev_rdeps |
| 434 | ) |
| 435 | except ValueError as e: |
| 436 | raise self.VartreeError(str(e)) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 437 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 438 | def _InitBinpkgDB(self, process_rdeps): |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 439 | """Initializes a dictionary of binpkgs for updating the target.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 440 | # Get build root trees; portage indexes require a trailing '/'. |
| 441 | build_root = os.path.join(self.sysroot, "") |
| 442 | trees = portage.create_trees( |
| 443 | target_root=build_root, config_root=build_root |
| 444 | ) |
| 445 | bintree = trees[build_root]["bintree"] |
| 446 | binpkgs_info = [] |
| 447 | for cpv in bintree.dbapi.cpv_all(): |
| 448 | slot, rdep_raw, build_time = bintree.dbapi.aux_get( |
| 449 | cpv, ["SLOT", "RDEPEND", "BUILD_TIME"] |
| 450 | ) |
| 451 | binpkgs_info.append((cpv, slot, rdep_raw, build_time)) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 452 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 453 | try: |
| 454 | self.binpkgs_db = self._BuildDB( |
| 455 | binpkgs_info, process_rdeps, False, installed_db=self.target_db |
| 456 | ) |
| 457 | except ValueError as e: |
| 458 | raise self.BintreeError(str(e)) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 459 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 460 | def _InitDepQueue(self): |
| 461 | """Initializes the dependency work queue.""" |
| 462 | self.queue = set() |
| 463 | self.seen = {} |
| 464 | self.listed = set() |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 465 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 466 | def _EnqDep(self, dep, listed, optional): |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 467 | """Enqueues a dependency if not seen before or if set non-optional.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 468 | if dep in self.seen and (optional or not self.seen[dep]): |
| 469 | return False |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 470 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 471 | self.queue.add(dep) |
| 472 | self.seen[dep] = optional |
| 473 | if listed: |
| 474 | self.listed.add(dep) |
| 475 | return True |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 476 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 477 | def _DeqDep(self): |
| 478 | """Dequeues and returns a dependency, its listed and optional flags. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 479 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 480 | This returns listed packages first, if any are present, to ensure that |
| 481 | we correctly mark them as such when they are first being processed. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 482 | """ |
| 483 | if self.listed: |
| 484 | dep = self.listed.pop() |
| 485 | self.queue.remove(dep) |
| 486 | listed = True |
| 487 | else: |
| 488 | dep = self.queue.pop() |
| 489 | listed = False |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 490 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 491 | return dep, listed, self.seen[dep] |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 492 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 493 | def _FindPackageMatches(self, cpv_pattern): |
| 494 | """Returns list of binpkg (CP, slot) pairs that match |cpv_pattern|. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 495 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 496 | This is breaking |cpv_pattern| into its C, P and V components, each of |
| 497 | which may or may not be present or contain wildcards. It then scans the |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 498 | binpkgs database to find all atoms that match these components, |
| 499 | returning a list of CP and slot qualifier. When the pattern does not |
| 500 | specify a version, or when a CP has only one slot in the binpkgs |
| 501 | database, we omit the slot qualifier in the result. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 502 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 503 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 504 | cpv_pattern: A CPV pattern, potentially partial and/or having |
| 505 | wildcards. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 506 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 507 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 508 | A list of (CPV, slot) pairs of packages in the binpkgs database that |
| 509 | match the pattern. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 510 | """ |
| 511 | attrs = package_info.SplitCPV(cpv_pattern, strict=False) |
| 512 | cp_pattern = os.path.join(attrs.category or "*", attrs.package or "*") |
| 513 | matches = [] |
| 514 | for cp, cp_slots in self.binpkgs_db.items(): |
| 515 | if not fnmatch.fnmatchcase(cp, cp_pattern): |
| 516 | continue |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 517 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 518 | # If no version attribute was given or there's only one slot, omit |
| 519 | # the slot qualifier. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 520 | if not attrs.version or len(cp_slots) == 1: |
| 521 | matches.append((cp, None)) |
| 522 | else: |
| 523 | cpv_pattern = "%s-%s" % (cp, attrs.version) |
| 524 | for slot, pkg_info in cp_slots.items(): |
| 525 | if fnmatch.fnmatchcase(pkg_info.cpv, cpv_pattern): |
| 526 | matches.append((cp, slot)) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 527 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 528 | return matches |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 529 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 530 | def _FindPackage(self, pkg): |
| 531 | """Returns the (CP, slot) pair for a package matching |pkg|. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 532 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 533 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 534 | pkg: Path to a binary package or a (partial) package CPV specifier. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 535 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 536 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 537 | A (CP, slot) pair for the given package; slot may be None |
| 538 | (unspecified). |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 539 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 540 | Raises: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 541 | ValueError: if |pkg| is not a binpkg file nor does it match |
| 542 | something that's in the bintree. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 543 | """ |
| 544 | if pkg.endswith(".tbz2") and os.path.isfile(pkg): |
| 545 | package = os.path.basename(os.path.splitext(pkg)[0]) |
| 546 | category = os.path.basename(os.path.dirname(pkg)) |
| 547 | return self._GetCP(os.path.join(category, package)), None |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 548 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 549 | matches = self._FindPackageMatches(pkg) |
| 550 | if not matches: |
| 551 | raise ValueError("No package found for %s" % pkg) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 552 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 553 | idx = 0 |
| 554 | if len(matches) > 1: |
| 555 | # Ask user to pick among multiple matches. |
| 556 | idx = cros_build_lib.GetChoice( |
| 557 | "Multiple matches found for %s: " % pkg, |
| 558 | ["%s:%s" % (cp, slot) if slot else cp for cp, slot in matches], |
| 559 | ) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 560 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 561 | return matches[idx] |
| 562 | |
| 563 | def _NeedsInstall(self, cpv, slot, build_time, optional): |
| 564 | """Returns whether a package needs to be installed on the target. |
| 565 | |
| 566 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 567 | cpv: Fully qualified CPV (string) of the package. |
| 568 | slot: Slot identifier (string). |
| 569 | build_time: The BUILT_TIME value (string) of the binpkg. |
| 570 | optional: Whether package is optional on the target. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 571 | |
| 572 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 573 | A tuple (install, update) indicating whether to |install| the |
| 574 | package and whether it is an |update| to an existing package. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 575 | |
| 576 | Raises: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 577 | ValueError: if slot is not provided. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 578 | """ |
| 579 | # If not checking installed packages, always install. |
| 580 | if not self.target_db: |
| 581 | return True, False |
| 582 | |
| 583 | cp = self._GetCP(cpv) |
| 584 | target_pkg_info = self.target_db.get(cp, dict()).get(slot) |
| 585 | if target_pkg_info is not None: |
| 586 | if cpv != target_pkg_info.cpv: |
| 587 | attrs = package_info.SplitCPV(cpv) |
| 588 | target_attrs = package_info.SplitCPV(target_pkg_info.cpv) |
| 589 | logging.debug( |
| 590 | "Updating %s: version (%s) different on target (%s)", |
| 591 | cp, |
| 592 | attrs.version, |
| 593 | target_attrs.version, |
| 594 | ) |
| 595 | return True, True |
| 596 | |
| 597 | if build_time != target_pkg_info.build_time: |
| 598 | logging.debug( |
| 599 | "Updating %s: build time (%s) different on target (%s)", |
| 600 | cpv, |
| 601 | build_time, |
| 602 | target_pkg_info.build_time, |
| 603 | ) |
| 604 | return True, True |
| 605 | |
| 606 | logging.debug( |
| 607 | "Not updating %s: already up-to-date (%s, built %s)", |
| 608 | cp, |
| 609 | target_pkg_info.cpv, |
| 610 | target_pkg_info.build_time, |
| 611 | ) |
| 612 | return False, False |
| 613 | |
| 614 | if optional: |
| 615 | logging.debug( |
| 616 | "Not installing %s: missing on target but optional", cp |
| 617 | ) |
| 618 | return False, False |
| 619 | |
| 620 | logging.debug( |
| 621 | "Installing %s: missing on target and non-optional (%s)", cp, cpv |
| 622 | ) |
| 623 | return True, False |
| 624 | |
| 625 | def _ProcessDeps(self, deps, reverse): |
| 626 | """Enqueues dependencies for processing. |
| 627 | |
| 628 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 629 | deps: List of dependencies to enqueue. |
| 630 | reverse: Whether these are reverse dependencies. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 631 | """ |
| 632 | if not deps: |
| 633 | return |
| 634 | |
| 635 | logging.debug( |
| 636 | "Processing %d %s dep(s)...", |
| 637 | len(deps), |
| 638 | "reverse" if reverse else "forward", |
| 639 | ) |
| 640 | num_already_seen = 0 |
| 641 | for dep in deps: |
| 642 | if self._EnqDep(dep, False, reverse): |
| 643 | logging.debug(" Queued dep %s", dep) |
| 644 | else: |
| 645 | num_already_seen += 1 |
| 646 | |
| 647 | if num_already_seen: |
| 648 | logging.debug("%d dep(s) already seen", num_already_seen) |
| 649 | |
| 650 | def _ComputeInstalls(self, process_rdeps, process_rev_rdeps): |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 651 | """Returns a dict of packages that need to be installed on the target. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 652 | |
| 653 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 654 | process_rdeps: Whether to trace forward dependencies. |
| 655 | process_rev_rdeps: Whether to trace backward dependencies as well. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 656 | |
| 657 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 658 | A dictionary mapping CP values (string) to tuples containing |
| 659 | a CPV (string), a slot (string), a boolean indicating whether |
| 660 | the package was initially listed in the queue, and a boolean |
| 661 | indicating whether this is an update to an existing package. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 662 | """ |
| 663 | installs = {} |
| 664 | while self.queue: |
| 665 | dep, listed, optional = self._DeqDep() |
| 666 | cp, required_slot = dep |
| 667 | if cp in installs: |
| 668 | logging.debug("Already updating %s", cp) |
| 669 | continue |
| 670 | |
| 671 | cp_slots = self.binpkgs_db.get(cp, dict()) |
| 672 | logging.debug( |
| 673 | "Checking packages matching %s%s%s...", |
| 674 | cp, |
| 675 | " (slot: %s)" % required_slot if required_slot else "", |
| 676 | " (optional)" if optional else "", |
| 677 | ) |
| 678 | num_processed = 0 |
| 679 | for slot, pkg_info in cp_slots.items(): |
| 680 | if required_slot and slot != required_slot: |
| 681 | continue |
| 682 | |
| 683 | num_processed += 1 |
| 684 | logging.debug(" Checking %s...", pkg_info.cpv) |
| 685 | |
| 686 | install, update = self._NeedsInstall( |
| 687 | pkg_info.cpv, slot, pkg_info.build_time, optional |
| 688 | ) |
| 689 | if not install: |
| 690 | continue |
| 691 | |
| 692 | installs[cp] = (pkg_info.cpv, slot, listed, update) |
| 693 | |
| 694 | # Add forward and backward runtime dependencies to queue. |
| 695 | if process_rdeps: |
| 696 | self._ProcessDeps(pkg_info.rdeps, False) |
| 697 | if process_rev_rdeps: |
| 698 | target_pkg_info = self.target_db.get(cp, dict()).get(slot) |
| 699 | if target_pkg_info: |
| 700 | self._ProcessDeps(target_pkg_info.rev_rdeps, True) |
| 701 | |
| 702 | if num_processed == 0: |
| 703 | logging.warning( |
| 704 | "No qualified bintree package corresponding to %s", cp |
| 705 | ) |
| 706 | |
| 707 | return installs |
| 708 | |
| 709 | def _SortInstalls(self, installs): |
| 710 | """Returns a sorted list of packages to install. |
| 711 | |
| 712 | Performs a topological sort based on dependencies found in the binary |
| 713 | package database. |
| 714 | |
| 715 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 716 | installs: Dictionary of packages to install indexed by CP. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 717 | |
| 718 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 719 | A list of package CPVs (string). |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 720 | |
| 721 | Raises: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 722 | ValueError: If dependency graph contains a cycle. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 723 | """ |
| 724 | not_visited = set(installs.keys()) |
| 725 | curr_path = [] |
| 726 | sorted_installs = [] |
| 727 | |
| 728 | def SortFrom(cp): |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 729 | """Traverses deps recursively, emitting nodes in reverse order.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 730 | cpv, slot, _, _ = installs[cp] |
| 731 | if cpv in curr_path: |
| 732 | raise ValueError( |
| 733 | "Dependencies contain a cycle: %s -> %s" |
| 734 | % (" -> ".join(curr_path[curr_path.index(cpv) :]), cpv) |
| 735 | ) |
| 736 | curr_path.append(cpv) |
| 737 | for rdep_cp, _ in self.binpkgs_db[cp][slot].rdeps: |
| 738 | if rdep_cp in not_visited: |
| 739 | not_visited.remove(rdep_cp) |
| 740 | SortFrom(rdep_cp) |
| 741 | |
| 742 | sorted_installs.append(cpv) |
| 743 | curr_path.pop() |
| 744 | |
| 745 | # So long as there's more packages, keep expanding dependency paths. |
| 746 | while not_visited: |
| 747 | SortFrom(not_visited.pop()) |
| 748 | |
| 749 | return sorted_installs |
| 750 | |
| 751 | def _EnqListedPkg(self, pkg): |
| 752 | """Finds and enqueues a listed package.""" |
| 753 | cp, slot = self._FindPackage(pkg) |
| 754 | if cp not in self.binpkgs_db: |
| 755 | raise self.BintreeError( |
| 756 | "Package %s not found in binpkgs tree" % pkg |
| 757 | ) |
| 758 | self._EnqDep((cp, slot), True, False) |
| 759 | |
| 760 | def _EnqInstalledPkgs(self): |
| 761 | """Enqueues all available binary packages that are already installed.""" |
| 762 | for cp, cp_slots in self.binpkgs_db.items(): |
| 763 | target_cp_slots = self.target_db.get(cp) |
| 764 | if target_cp_slots: |
| 765 | for slot in cp_slots.keys(): |
| 766 | if slot in target_cp_slots: |
| 767 | self._EnqDep((cp, slot), True, False) |
| 768 | |
| 769 | def Run( |
| 770 | self, |
| 771 | device, |
| 772 | root, |
| 773 | listed_pkgs, |
| 774 | update, |
| 775 | process_rdeps, |
| 776 | process_rev_rdeps, |
| 777 | ): |
| 778 | """Computes the list of packages that need to be installed on a target. |
| 779 | |
| 780 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 781 | device: Target handler object. |
| 782 | root: Package installation root. |
| 783 | listed_pkgs: Package names/files listed by the user. |
| 784 | update: Whether to read the target's installed package database. |
| 785 | process_rdeps: Whether to trace forward dependencies. |
| 786 | process_rev_rdeps: Whether to trace backward dependencies as well. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 787 | |
| 788 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 789 | A tuple (sorted, listed, num_updates, install_attrs) where |sorted| |
| 790 | is a list of package CPVs (string) to install on the target |
| 791 | in an order that satisfies their inter-dependencies, |listed| |
| 792 | the subset that was requested by the user, and |num_updates| |
| 793 | the number of packages being installed over preexisting |
| 794 | versions. Note that installation order should be reversed for |
| 795 | removal, |install_attrs| is a dictionary mapping a package |
| 796 | CPV (string) to some of its extracted environment attributes. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 797 | """ |
| 798 | if process_rev_rdeps and not process_rdeps: |
| 799 | raise ValueError( |
| 800 | "Must processing forward deps when processing rev deps" |
| 801 | ) |
| 802 | if process_rdeps and not update: |
| 803 | raise ValueError( |
| 804 | "Must check installed packages when processing deps" |
| 805 | ) |
| 806 | |
| 807 | if update: |
| 808 | logging.info("Initializing target intalled packages database...") |
| 809 | self._InitTargetVarDB( |
| 810 | device, root, process_rdeps, process_rev_rdeps |
| 811 | ) |
| 812 | |
| 813 | logging.info("Initializing binary packages database...") |
| 814 | self._InitBinpkgDB(process_rdeps) |
| 815 | |
| 816 | logging.info("Finding listed package(s)...") |
| 817 | self._InitDepQueue() |
| 818 | for pkg in listed_pkgs: |
| 819 | if pkg == "@installed": |
| 820 | if not update: |
| 821 | raise ValueError( |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 822 | "Must check installed packages when updating all of " |
| 823 | "them." |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 824 | ) |
| 825 | self._EnqInstalledPkgs() |
| 826 | else: |
| 827 | self._EnqListedPkg(pkg) |
| 828 | |
| 829 | logging.info("Computing set of packages to install...") |
| 830 | installs = self._ComputeInstalls(process_rdeps, process_rev_rdeps) |
| 831 | |
| 832 | num_updates = 0 |
| 833 | listed_installs = [] |
| 834 | for cpv, _, listed, isupdate in installs.values(): |
| 835 | if listed: |
| 836 | listed_installs.append(cpv) |
| 837 | if isupdate: |
| 838 | num_updates += 1 |
| 839 | |
| 840 | logging.info( |
| 841 | "Processed %d package(s), %d will be installed, %d are " |
| 842 | "updating existing packages", |
| 843 | len(self.seen), |
| 844 | len(installs), |
| 845 | num_updates, |
| 846 | ) |
| 847 | |
| 848 | sorted_installs = self._SortInstalls(installs) |
| 849 | |
| 850 | install_attrs = {} |
| 851 | for pkg in sorted_installs: |
| 852 | pkg_path = os.path.join(root, portage_util.VDB_PATH, pkg) |
| 853 | dlc_id, dlc_package = _GetDLCInfo(device, pkg_path, from_dut=True) |
| 854 | install_attrs[pkg] = {} |
| 855 | if dlc_id and dlc_package: |
| 856 | install_attrs[pkg][_DLC_ID] = dlc_id |
| 857 | |
| 858 | return sorted_installs, listed_installs, num_updates, install_attrs |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 859 | |
| 860 | |
Mike Frysinger | 63d3551 | 2021-01-26 23:16:13 -0500 | [diff] [blame] | 861 | def _Emerge(device, pkg_paths, root, extra_args=None): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 862 | """Copies |pkg_paths| to |device| and emerges them. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 863 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 864 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 865 | device: A ChromiumOSDevice object. |
| 866 | pkg_paths: (Local) paths to binary packages. |
| 867 | root: Package installation root path. |
| 868 | extra_args: Extra arguments to pass to emerge. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 869 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 870 | Raises: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 871 | DeployError: Unrecoverable error during emerge. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 872 | """ |
Mike Frysinger | 63d3551 | 2021-01-26 23:16:13 -0500 | [diff] [blame] | 873 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 874 | def path_to_name(pkg_path): |
| 875 | return os.path.basename(pkg_path) |
Mike Frysinger | 63d3551 | 2021-01-26 23:16:13 -0500 | [diff] [blame] | 876 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 877 | def path_to_category(pkg_path): |
| 878 | return os.path.basename(os.path.dirname(pkg_path)) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 879 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 880 | pkg_names = ", ".join(path_to_name(x) for x in pkg_paths) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 881 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 882 | pkgroot = os.path.join(device.work_dir, "packages") |
| 883 | portage_tmpdir = os.path.join(device.work_dir, "portage-tmp") |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 884 | # Clean out the dirs first if we had a previous emerge on the device so as |
| 885 | # to free up space for this emerge. The last emerge gets implicitly cleaned |
| 886 | # up when the device connection deletes its work_dir. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 887 | device.run( |
| 888 | f"cd {device.work_dir} && " |
| 889 | f"rm -rf packages portage-tmp && " |
| 890 | f"mkdir -p portage-tmp packages && " |
| 891 | f"cd packages && " |
| 892 | f'mkdir -p {" ".join(set(path_to_category(x) for x in pkg_paths))}', |
| 893 | shell=True, |
| 894 | remote_sudo=True, |
| 895 | ) |
Mike Frysinger | 63d3551 | 2021-01-26 23:16:13 -0500 | [diff] [blame] | 896 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 897 | logging.info("Use portage temp dir %s", portage_tmpdir) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 898 | |
Mike Frysinger | 63d3551 | 2021-01-26 23:16:13 -0500 | [diff] [blame] | 899 | # This message is read by BrilloDeployOperation. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 900 | logging.notice("Copying binpkgs to device.") |
| 901 | for pkg_path in pkg_paths: |
| 902 | pkg_name = path_to_name(pkg_path) |
| 903 | logging.info("Copying %s", pkg_name) |
| 904 | pkg_dir = os.path.join(pkgroot, path_to_category(pkg_path)) |
| 905 | device.CopyToDevice( |
| 906 | pkg_path, pkg_dir, mode="rsync", remote_sudo=True, compress=False |
| 907 | ) |
| 908 | |
| 909 | # This message is read by BrilloDeployOperation. |
| 910 | logging.notice("Installing: %s", pkg_names) |
| 911 | |
| 912 | # We set PORTAGE_CONFIGROOT to '/usr/local' because by default all |
| 913 | # chromeos-base packages will be skipped due to the configuration |
| 914 | # in /etc/protage/make.profile/package.provided. However, there is |
| 915 | # a known bug that /usr/local/etc/portage is not setup properly |
| 916 | # (crbug.com/312041). This does not affect `cros deploy` because |
| 917 | # we do not use the preset PKGDIR. |
| 918 | extra_env = { |
| 919 | "FEATURES": "-sandbox", |
| 920 | "PKGDIR": pkgroot, |
| 921 | "PORTAGE_CONFIGROOT": "/usr/local", |
| 922 | "PORTAGE_TMPDIR": portage_tmpdir, |
| 923 | "PORTDIR": device.work_dir, |
| 924 | "CONFIG_PROTECT": "-*", |
| 925 | } |
| 926 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 927 | # --ignore-built-slot-operator-deps because we don't rebuild everything. It |
| 928 | # can cause errors, but that's expected with cros deploy since it's just a |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 929 | # best effort to prevent developers avoid rebuilding an image every time. |
| 930 | cmd = [ |
| 931 | "emerge", |
| 932 | "--usepkg", |
| 933 | "--ignore-built-slot-operator-deps=y", |
| 934 | "--root", |
| 935 | root, |
| 936 | ] + [os.path.join(pkgroot, *x.split("/")[-2:]) for x in pkg_paths] |
| 937 | if extra_args: |
| 938 | cmd.append(extra_args) |
| 939 | |
| 940 | logging.warning( |
| 941 | "Ignoring slot dependencies! This may break things! e.g. " |
| 942 | "packages built against the old version may not be able to " |
| 943 | "load the new .so. This is expected, and you will just need " |
| 944 | "to build and flash a new image if you have problems." |
| 945 | ) |
| 946 | try: |
| 947 | result = device.run( |
| 948 | cmd, |
| 949 | extra_env=extra_env, |
| 950 | remote_sudo=True, |
| 951 | capture_output=True, |
| 952 | debug_level=logging.INFO, |
| 953 | ) |
| 954 | |
| 955 | pattern = ( |
| 956 | "A requested package will not be merged because " |
| 957 | "it is listed in package.provided" |
| 958 | ) |
| 959 | output = result.stderr.replace("\n", " ").replace("\r", "") |
| 960 | if pattern in output: |
| 961 | error = ( |
| 962 | "Package failed to emerge: %s\n" |
| 963 | "Remove %s from /etc/portage/make.profile/" |
| 964 | "package.provided/chromeos-base.packages\n" |
| 965 | "(also see crbug.com/920140 for more context)\n" |
| 966 | % (pattern, pkg_name) |
| 967 | ) |
| 968 | cros_build_lib.Die(error) |
| 969 | except Exception: |
| 970 | logging.error("Failed to emerge packages %s", pkg_names) |
| 971 | raise |
| 972 | else: |
| 973 | # This message is read by BrilloDeployOperation. |
| 974 | logging.notice("Packages have been installed.") |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 975 | |
| 976 | |
Qijiang Fan | d595819 | 2019-07-26 12:32:36 +0900 | [diff] [blame] | 977 | def _RestoreSELinuxContext(device, pkgpath, root): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 978 | """Restore SELinux context for files in a given package. |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 979 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 980 | This reads the tarball from pkgpath, and calls restorecon on device to |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 981 | restore SELinux context for files listed in the tarball, assuming those |
| 982 | files are installed to / |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 983 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 984 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 985 | device: a ChromiumOSDevice object |
| 986 | pkgpath: path to tarball |
| 987 | root: Package installation root path. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 988 | """ |
| 989 | pkgroot = os.path.join(device.work_dir, "packages") |
| 990 | pkg_dirname = os.path.basename(os.path.dirname(pkgpath)) |
| 991 | pkgpath_device = os.path.join( |
| 992 | pkgroot, pkg_dirname, os.path.basename(pkgpath) |
| 993 | ) |
| 994 | # Testing shows restorecon splits on newlines instead of spaces. |
| 995 | device.run( |
| 996 | [ |
| 997 | "cd", |
| 998 | root, |
| 999 | "&&", |
| 1000 | "tar", |
| 1001 | "tf", |
| 1002 | pkgpath_device, |
| 1003 | "|", |
| 1004 | "restorecon", |
| 1005 | "-i", |
| 1006 | "-f", |
| 1007 | "-", |
| 1008 | ], |
| 1009 | remote_sudo=True, |
| 1010 | ) |
Qijiang Fan | 352d0eb | 2019-02-25 13:10:08 +0900 | [diff] [blame] | 1011 | |
| 1012 | |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 1013 | def _GetPackagesByCPV(cpvs, strip, sysroot): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1014 | """Returns paths to binary packages corresponding to |cpvs|. |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 1015 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1016 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 1017 | cpvs: List of CPV components given by package_info.SplitCPV(). |
| 1018 | strip: True to run strip_package. |
| 1019 | sysroot: Sysroot path. |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 1020 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1021 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 1022 | List of paths corresponding to |cpvs|. |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 1023 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1024 | Raises: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 1025 | DeployError: If a package is missing. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1026 | """ |
| 1027 | packages_dir = None |
| 1028 | if strip: |
| 1029 | try: |
| 1030 | cros_build_lib.run( |
| 1031 | [ |
| 1032 | os.path.join( |
| 1033 | constants.CHROMITE_SCRIPTS_DIR, "strip_package" |
| 1034 | ), |
| 1035 | "--sysroot", |
| 1036 | sysroot, |
| 1037 | ] |
| 1038 | + [cpv.cpf for cpv in cpvs] |
| 1039 | ) |
| 1040 | packages_dir = _STRIPPED_PACKAGES_DIR |
| 1041 | except cros_build_lib.RunCommandError: |
| 1042 | logging.error( |
| 1043 | "Cannot strip packages %s", " ".join([str(cpv) for cpv in cpvs]) |
| 1044 | ) |
| 1045 | raise |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 1046 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1047 | paths = [] |
| 1048 | for cpv in cpvs: |
| 1049 | path = portage_util.GetBinaryPackagePath( |
| 1050 | cpv.category, |
| 1051 | cpv.package, |
| 1052 | cpv.version, |
| 1053 | sysroot=sysroot, |
| 1054 | packages_dir=packages_dir, |
| 1055 | ) |
| 1056 | if not path: |
| 1057 | raise DeployError("Missing package %s." % cpv) |
| 1058 | paths.append(path) |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 1059 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1060 | return paths |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 1061 | |
| 1062 | |
| 1063 | def _GetPackagesPaths(pkgs, strip, sysroot): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1064 | """Returns paths to binary |pkgs|. |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 1065 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1066 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 1067 | pkgs: List of package CPVs string. |
| 1068 | strip: Whether or not to run strip_package for CPV packages. |
| 1069 | sysroot: The sysroot path. |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 1070 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1071 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 1072 | List of paths corresponding to |pkgs|. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1073 | """ |
| 1074 | cpvs = [package_info.SplitCPV(p) for p in pkgs] |
| 1075 | return _GetPackagesByCPV(cpvs, strip, sysroot) |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 1076 | |
| 1077 | |
Mike Frysinger | 22bb550 | 2021-01-29 13:05:46 -0500 | [diff] [blame] | 1078 | def _Unmerge(device, pkgs, root): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1079 | """Unmerges |pkgs| on |device|. |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 1080 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1081 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 1082 | device: A RemoteDevice object. |
| 1083 | pkgs: Package names. |
| 1084 | root: Package installation root path. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1085 | """ |
| 1086 | pkg_names = ", ".join(os.path.basename(x) for x in pkgs) |
Mike Frysinger | 22bb550 | 2021-01-29 13:05:46 -0500 | [diff] [blame] | 1087 | # This message is read by BrilloDeployOperation. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1088 | logging.notice("Unmerging %s.", pkg_names) |
| 1089 | cmd = ["qmerge", "--yes"] |
| 1090 | # Check if qmerge is available on the device. If not, use emerge. |
| 1091 | if device.run(["qmerge", "--version"], check=False).returncode != 0: |
| 1092 | cmd = ["emerge"] |
| 1093 | |
| 1094 | cmd += ["--unmerge", "--root", root] |
| 1095 | cmd.extend("f={x}" for x in pkgs) |
| 1096 | try: |
| 1097 | # Always showing the emerge output for clarity. |
| 1098 | device.run( |
| 1099 | cmd, |
| 1100 | capture_output=False, |
| 1101 | remote_sudo=True, |
| 1102 | debug_level=logging.INFO, |
| 1103 | ) |
| 1104 | except Exception: |
| 1105 | logging.error("Failed to unmerge packages %s", pkg_names) |
| 1106 | raise |
| 1107 | else: |
| 1108 | # This message is read by BrilloDeployOperation. |
| 1109 | logging.notice("Packages have been uninstalled.") |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 1110 | |
| 1111 | |
| 1112 | def _ConfirmDeploy(num_updates): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1113 | """Returns whether we can continue deployment.""" |
| 1114 | if num_updates > _MAX_UPDATES_NUM: |
| 1115 | logging.warning(_MAX_UPDATES_WARNING) |
| 1116 | return cros_build_lib.BooleanPrompt(default=False) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 1117 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1118 | return True |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 1119 | |
| 1120 | |
Andrew | 06a5f81 | 2020-01-23 08:08:32 -0800 | [diff] [blame] | 1121 | def _EmergePackages(pkgs, device, strip, sysroot, root, board, emerge_args): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1122 | """Call _Emerge for each package in pkgs.""" |
Ben Pastene | 5f03b05 | 2019-08-12 18:03:24 -0700 | [diff] [blame] | 1123 | if device.IsSELinuxAvailable(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1124 | enforced = device.IsSELinuxEnforced() |
| 1125 | if enforced: |
| 1126 | device.run(["setenforce", "0"]) |
| 1127 | else: |
| 1128 | enforced = False |
Andrew | c7e1c6b | 2020-02-27 16:03:53 -0800 | [diff] [blame] | 1129 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1130 | dlc_deployed = False |
| 1131 | # This message is read by BrilloDeployOperation. |
| 1132 | logging.info("Preparing local packages for transfer.") |
| 1133 | pkg_paths = _GetPackagesPaths(pkgs, strip, sysroot) |
| 1134 | # Install all the packages in one pass so inter-package blockers work. |
| 1135 | _Emerge(device, pkg_paths, root, extra_args=emerge_args) |
| 1136 | logging.info("Updating SELinux settings & DLC images.") |
| 1137 | for pkg_path in pkg_paths: |
| 1138 | if device.IsSELinuxAvailable(): |
| 1139 | _RestoreSELinuxContext(device, pkg_path, root) |
Mike Frysinger | 5f4c274 | 2021-02-08 14:37:23 -0500 | [diff] [blame] | 1140 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1141 | dlc_id, dlc_package = _GetDLCInfo(device, pkg_path, from_dut=False) |
| 1142 | if dlc_id and dlc_package: |
| 1143 | _DeployDLCImage(device, sysroot, board, dlc_id, dlc_package) |
| 1144 | dlc_deployed = True |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 1145 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1146 | if dlc_deployed: |
| 1147 | # Clean up empty directories created by emerging DLCs. |
| 1148 | device.run( |
| 1149 | [ |
| 1150 | "test", |
| 1151 | "-d", |
| 1152 | "/build/rootfs", |
| 1153 | "&&", |
| 1154 | "rmdir", |
| 1155 | "--ignore-fail-on-non-empty", |
| 1156 | "/build/rootfs", |
| 1157 | "/build", |
| 1158 | ], |
| 1159 | check=False, |
| 1160 | ) |
Mike Frysinger | 4eb5f4e | 2021-01-26 21:48:37 -0500 | [diff] [blame] | 1161 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1162 | if enforced: |
| 1163 | device.run(["setenforce", "1"]) |
| 1164 | |
| 1165 | # Restart dlcservice so it picks up the newly installed DLC modules (in case |
| 1166 | # we installed new DLC images). |
| 1167 | if dlc_deployed: |
| 1168 | device.run(["restart", "dlcservice"]) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 1169 | |
| 1170 | |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 1171 | def _UnmergePackages(pkgs, device, root, pkgs_attrs): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1172 | """Call _Unmege for each package in pkgs.""" |
| 1173 | dlc_uninstalled = False |
| 1174 | _Unmerge(device, pkgs, root) |
| 1175 | logging.info("Cleaning up DLC images.") |
| 1176 | for pkg in pkgs: |
| 1177 | if _UninstallDLCImage(device, pkgs_attrs[pkg]): |
| 1178 | dlc_uninstalled = True |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 1179 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1180 | # Restart dlcservice so it picks up the uninstalled DLC modules (in case we |
| 1181 | # uninstalled DLC images). |
| 1182 | if dlc_uninstalled: |
| 1183 | device.run(["restart", "dlcservice"]) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 1184 | |
| 1185 | |
| 1186 | def _UninstallDLCImage(device, pkg_attrs): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1187 | """Uninstall a DLC image.""" |
| 1188 | if _DLC_ID in pkg_attrs: |
| 1189 | dlc_id = pkg_attrs[_DLC_ID] |
| 1190 | logging.notice("Uninstalling DLC image for %s", dlc_id) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 1191 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1192 | device.run(["dlcservice_util", "--uninstall", "--id=%s" % dlc_id]) |
| 1193 | return True |
| 1194 | else: |
| 1195 | logging.debug("DLC_ID not found in package") |
| 1196 | return False |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 1197 | |
| 1198 | |
Andrew | 06a5f81 | 2020-01-23 08:08:32 -0800 | [diff] [blame] | 1199 | def _DeployDLCImage(device, sysroot, board, dlc_id, dlc_package): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1200 | """Deploy (install and mount) a DLC image. |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 1201 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1202 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 1203 | device: A device object. |
| 1204 | sysroot: The sysroot path. |
| 1205 | board: Board to use. |
| 1206 | dlc_id: The DLC ID. |
| 1207 | dlc_package: The DLC package name. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1208 | """ |
| 1209 | # Requires `sudo_rm` because installations of files are running with sudo. |
| 1210 | with osutils.TempDir(sudo_rm=True) as tempdir: |
| 1211 | temp_rootfs = Path(tempdir) |
| 1212 | # Build the DLC image if the image is outdated or doesn't exist. |
| 1213 | dlc_lib.InstallDlcImages( |
| 1214 | sysroot=sysroot, rootfs=temp_rootfs, dlc_id=dlc_id, board=board |
| 1215 | ) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 1216 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1217 | logging.debug("Uninstall DLC %s if it is installed.", dlc_id) |
| 1218 | try: |
| 1219 | device.run(["dlcservice_util", "--uninstall", "--id=%s" % dlc_id]) |
| 1220 | except cros_build_lib.RunCommandError as e: |
| 1221 | logging.info( |
| 1222 | "Failed to uninstall DLC:%s. Continue anyway.", e.stderr |
| 1223 | ) |
| 1224 | except Exception: |
| 1225 | logging.error("Failed to uninstall DLC.") |
| 1226 | raise |
Andrew | c7e1c6b | 2020-02-27 16:03:53 -0800 | [diff] [blame] | 1227 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1228 | logging.notice("Deploy the DLC image for %s", dlc_id) |
| 1229 | dlc_img_path_src = os.path.join( |
| 1230 | sysroot, |
| 1231 | dlc_lib.DLC_BUILD_DIR, |
| 1232 | dlc_id, |
| 1233 | dlc_package, |
| 1234 | dlc_lib.DLC_IMAGE, |
| 1235 | ) |
Jae Hoon Kim | c3cf227 | 2022-10-28 23:59:10 +0000 | [diff] [blame] | 1236 | if not os.path.exists(dlc_img_path_src): |
| 1237 | dlc_img_path_src = os.path.join( |
| 1238 | sysroot, |
| 1239 | dlc_lib.DLC_BUILD_DIR_SCALED, |
| 1240 | dlc_id, |
| 1241 | dlc_package, |
| 1242 | dlc_lib.DLC_IMAGE, |
| 1243 | ) |
| 1244 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1245 | dlc_img_path = os.path.join(_DLC_INSTALL_ROOT, dlc_id, dlc_package) |
| 1246 | dlc_img_path_a = os.path.join(dlc_img_path, "dlc_a") |
| 1247 | dlc_img_path_b = os.path.join(dlc_img_path, "dlc_b") |
| 1248 | # Create directories for DLC images. |
| 1249 | device.run(["mkdir", "-p", dlc_img_path_a, dlc_img_path_b]) |
| 1250 | # Copy images to the destination directories. |
| 1251 | device.CopyToDevice( |
| 1252 | dlc_img_path_src, |
| 1253 | os.path.join(dlc_img_path_a, dlc_lib.DLC_IMAGE), |
| 1254 | mode="rsync", |
| 1255 | ) |
| 1256 | device.run( |
| 1257 | [ |
| 1258 | "cp", |
| 1259 | os.path.join(dlc_img_path_a, dlc_lib.DLC_IMAGE), |
| 1260 | os.path.join(dlc_img_path_b, dlc_lib.DLC_IMAGE), |
| 1261 | ] |
| 1262 | ) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 1263 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1264 | # Set the proper perms and ownership so dlcservice can access the image. |
| 1265 | device.run(["chmod", "-R", "u+rwX,go+rX,go-w", _DLC_INSTALL_ROOT]) |
| 1266 | device.run(["chown", "-R", "dlcservice:dlcservice", _DLC_INSTALL_ROOT]) |
Jae Hoon Kim | 2376e14 | 2022-09-03 00:18:58 +0000 | [diff] [blame] | 1267 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1268 | # Copy metadata to device. |
| 1269 | dest_meta_dir = Path("/") / dlc_lib.DLC_META_DIR / dlc_id / dlc_package |
| 1270 | device.run(["mkdir", "-p", dest_meta_dir]) |
| 1271 | src_meta_dir = os.path.join( |
| 1272 | sysroot, |
| 1273 | dlc_lib.DLC_BUILD_DIR, |
| 1274 | dlc_id, |
| 1275 | dlc_package, |
| 1276 | dlc_lib.DLC_TMP_META_DIR, |
| 1277 | ) |
Jae Hoon Kim | c3cf227 | 2022-10-28 23:59:10 +0000 | [diff] [blame] | 1278 | if not os.path.exists(src_meta_dir): |
| 1279 | src_meta_dir = os.path.join( |
| 1280 | sysroot, |
| 1281 | dlc_lib.DLC_BUILD_DIR_SCALED, |
| 1282 | dlc_id, |
| 1283 | dlc_package, |
| 1284 | dlc_lib.DLC_TMP_META_DIR, |
| 1285 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1286 | device.CopyToDevice( |
| 1287 | src_meta_dir + "/", |
| 1288 | dest_meta_dir, |
| 1289 | mode="rsync", |
| 1290 | recursive=True, |
| 1291 | remote_sudo=True, |
| 1292 | ) |
Jae Hoon Kim | 2376e14 | 2022-09-03 00:18:58 +0000 | [diff] [blame] | 1293 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1294 | # TODO(kimjae): Make this generic so it recomputes all the DLCs + copies |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 1295 | # over a fresh list of dm-verity digests instead of appending and |
| 1296 | # keeping the stale digests when developers are testing. |
Jae Hoon Kim | 2376e14 | 2022-09-03 00:18:58 +0000 | [diff] [blame] | 1297 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1298 | # Copy the LoadPin dm-verity digests to device. |
| 1299 | loadpin = dlc_lib.DLC_LOADPIN_TRUSTED_VERITY_DIGESTS |
| 1300 | dst_loadpin = Path("/") / dlc_lib.DLC_META_DIR / loadpin |
| 1301 | src_loadpin = temp_rootfs / dlc_lib.DLC_META_DIR / loadpin |
| 1302 | if src_loadpin.exists(): |
| 1303 | digests = set(osutils.ReadFile(src_loadpin).split()) |
| 1304 | try: |
| 1305 | digests.update(device.CatFile(dst_loadpin).split()) |
| 1306 | except remote_access.CatFileError: |
| 1307 | pass |
Jae Hoon Kim | 2376e14 | 2022-09-03 00:18:58 +0000 | [diff] [blame] | 1308 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1309 | with tempfile.NamedTemporaryFile(dir=temp_rootfs) as f: |
| 1310 | osutils.WriteFile(f.name, "\n".join(digests)) |
| 1311 | device.CopyToDevice( |
| 1312 | f.name, dst_loadpin, mode="rsync", remote_sudo=True |
| 1313 | ) |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 1314 | |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 1315 | |
| 1316 | def _GetDLCInfo(device, pkg_path, from_dut): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1317 | """Returns information of a DLC given its package path. |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 1318 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1319 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 1320 | device: commandline.Device object; None to use the default device. |
| 1321 | pkg_path: path to the package. |
| 1322 | from_dut: True if extracting DLC info from DUT, False if extracting DLC |
| 1323 | info from host. |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 1324 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1325 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 1326 | A tuple (dlc_id, dlc_package). |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1327 | """ |
| 1328 | environment_content = "" |
| 1329 | if from_dut: |
| 1330 | # On DUT, |pkg_path| is the directory which contains environment file. |
| 1331 | environment_path = os.path.join(pkg_path, _ENVIRONMENT_FILENAME) |
| 1332 | try: |
| 1333 | environment_data = device.CatFile( |
| 1334 | environment_path, max_size=None, encoding=None |
| 1335 | ) |
| 1336 | except remote_access.CatFileError: |
| 1337 | # The package is not installed on DUT yet. Skip extracting info. |
| 1338 | return None, None |
| 1339 | else: |
| 1340 | # On host, pkg_path is tbz2 file which contains environment file. |
| 1341 | # Extract the metadata of the package file. |
| 1342 | data = portage.xpak.tbz2(pkg_path).get_data() |
| 1343 | environment_data = data[_ENVIRONMENT_FILENAME.encode("utf-8")] |
| 1344 | |
| 1345 | # Extract the environment metadata. |
| 1346 | environment_content = bz2.decompress(environment_data) |
| 1347 | |
| 1348 | with tempfile.NamedTemporaryFile() as f: |
| 1349 | # Dumps content into a file so we can use osutils.SourceEnvironment. |
| 1350 | path = os.path.realpath(f.name) |
| 1351 | osutils.WriteFile(path, environment_content, mode="wb") |
| 1352 | content = osutils.SourceEnvironment( |
| 1353 | path, (_DLC_ID, _DLC_PACKAGE, _DLC_ENABLED) |
| 1354 | ) |
| 1355 | |
| 1356 | dlc_enabled = content.get(_DLC_ENABLED) |
| 1357 | if dlc_enabled is not None and ( |
| 1358 | dlc_enabled is False or str(dlc_enabled) == "false" |
| 1359 | ): |
| 1360 | logging.info("Installing DLC in rootfs.") |
| 1361 | return None, None |
| 1362 | return content.get(_DLC_ID), content.get(_DLC_PACKAGE) |
| 1363 | |
| 1364 | |
| 1365 | def Deploy( |
| 1366 | device, |
| 1367 | packages, |
| 1368 | board=None, |
| 1369 | emerge=True, |
| 1370 | update=False, |
| 1371 | deep=False, |
| 1372 | deep_rev=False, |
| 1373 | clean_binpkg=True, |
| 1374 | root="/", |
| 1375 | strip=True, |
| 1376 | emerge_args=None, |
| 1377 | ssh_private_key=None, |
| 1378 | ping=True, |
| 1379 | force=False, |
| 1380 | dry_run=False, |
| 1381 | ): |
| 1382 | """Deploys packages to a device. |
| 1383 | |
| 1384 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 1385 | device: commandline.Device object; None to use the default device. |
| 1386 | packages: List of packages (strings) to deploy to device. |
| 1387 | board: Board to use; None to automatically detect. |
| 1388 | emerge: True to emerge package, False to unmerge. |
| 1389 | update: Check installed version on device. |
| 1390 | deep: Install dependencies also. Implies |update|. |
| 1391 | deep_rev: Install reverse dependencies. Implies |deep|. |
| 1392 | clean_binpkg: Clean outdated binary packages. |
| 1393 | root: Package installation root path. |
| 1394 | strip: Run strip_package to filter out preset paths in the package. |
| 1395 | emerge_args: Extra arguments to pass to emerge. |
| 1396 | ssh_private_key: Path to an SSH private key file; None to use test keys. |
| 1397 | ping: True to ping the device before trying to connect. |
| 1398 | force: Ignore confidence checks and prompts. |
| 1399 | dry_run: Print deployment plan but do not deploy anything. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1400 | |
| 1401 | Raises: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 1402 | ValueError: Invalid parameter or parameter combination. |
| 1403 | DeployError: Unrecoverable failure during deploy. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1404 | """ |
| 1405 | if deep_rev: |
| 1406 | deep = True |
| 1407 | if deep: |
| 1408 | update = True |
| 1409 | |
| 1410 | if not packages: |
| 1411 | raise DeployError("No packages provided, nothing to deploy.") |
| 1412 | |
| 1413 | if update and not emerge: |
| 1414 | raise ValueError("Cannot update and unmerge.") |
| 1415 | |
| 1416 | if device: |
| 1417 | hostname, username, port = device.hostname, device.username, device.port |
| 1418 | else: |
| 1419 | hostname, username, port = None, None, None |
| 1420 | |
| 1421 | lsb_release = None |
| 1422 | sysroot = None |
Mike Frysinger | acd06cd | 2021-01-27 13:33:52 -0500 | [diff] [blame] | 1423 | try: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1424 | # Somewhat confusing to clobber, but here we are. |
| 1425 | # pylint: disable=redefined-argument-from-local |
| 1426 | with remote_access.ChromiumOSDeviceHandler( |
| 1427 | hostname, |
| 1428 | port=port, |
| 1429 | username=username, |
| 1430 | private_key=ssh_private_key, |
| 1431 | base_dir=_DEVICE_BASE_DIR, |
| 1432 | ping=ping, |
| 1433 | ) as device: |
| 1434 | lsb_release = device.lsb_release |
Mike Frysinger | acd06cd | 2021-01-27 13:33:52 -0500 | [diff] [blame] | 1435 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1436 | board = cros_build_lib.GetBoard( |
| 1437 | device_board=device.board, override_board=board |
| 1438 | ) |
| 1439 | if not force and board != device.board: |
| 1440 | raise DeployError( |
| 1441 | "Device (%s) is incompatible with board %s. Use " |
| 1442 | "--force to deploy anyway." % (device.board, board) |
| 1443 | ) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 1444 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1445 | sysroot = build_target_lib.get_default_sysroot_path(board) |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 1446 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 1447 | # Don't bother trying to clean for unmerges. We won't use the local |
| 1448 | # db, and it just slows things down for the user. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1449 | if emerge and clean_binpkg: |
| 1450 | logging.notice( |
| 1451 | "Cleaning outdated binary packages from %s", sysroot |
| 1452 | ) |
| 1453 | portage_util.CleanOutdatedBinaryPackages(sysroot) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 1454 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1455 | # Remount rootfs as writable if necessary. |
| 1456 | if not device.MountRootfsReadWrite(): |
| 1457 | raise DeployError( |
| 1458 | "Cannot remount rootfs as read-write. Exiting." |
| 1459 | ) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 1460 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1461 | # Obtain list of packages to upgrade/remove. |
| 1462 | pkg_scanner = _InstallPackageScanner(sysroot) |
| 1463 | pkgs, listed, num_updates, pkgs_attrs = pkg_scanner.Run( |
| 1464 | device, root, packages, update, deep, deep_rev |
| 1465 | ) |
| 1466 | if emerge: |
| 1467 | action_str = "emerge" |
| 1468 | else: |
| 1469 | pkgs.reverse() |
| 1470 | action_str = "unmerge" |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 1471 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1472 | if not pkgs: |
| 1473 | logging.notice("No packages to %s", action_str) |
| 1474 | return |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 1475 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1476 | # Warn when the user installs & didn't `cros workon start`. |
| 1477 | if emerge: |
| 1478 | all_workon = workon_helper.WorkonHelper(sysroot).ListAtoms( |
| 1479 | use_all=True |
| 1480 | ) |
| 1481 | worked_on_cps = workon_helper.WorkonHelper(sysroot).ListAtoms() |
| 1482 | for package in listed: |
| 1483 | cp = package_info.SplitCPV(package).cp |
| 1484 | if cp in all_workon and cp not in worked_on_cps: |
| 1485 | logging.warning( |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 1486 | "Are you intentionally deploying unmodified " |
| 1487 | "packages, or did you forget to run " |
| 1488 | "`cros workon --board=$BOARD start %s`?", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1489 | cp, |
| 1490 | ) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 1491 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1492 | logging.notice("These are the packages to %s:", action_str) |
| 1493 | for i, pkg in enumerate(pkgs): |
| 1494 | logging.notice( |
| 1495 | "%s %d) %s", "*" if pkg in listed else " ", i + 1, pkg |
| 1496 | ) |
Gilad Arnold | a0a9806 | 2015-07-07 08:34:27 -0700 | [diff] [blame] | 1497 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1498 | if dry_run or not _ConfirmDeploy(num_updates): |
| 1499 | return |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 1500 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1501 | # Select function (emerge or unmerge) and bind args. |
| 1502 | if emerge: |
| 1503 | func = functools.partial( |
| 1504 | _EmergePackages, |
| 1505 | pkgs, |
| 1506 | device, |
| 1507 | strip, |
| 1508 | sysroot, |
| 1509 | root, |
| 1510 | board, |
| 1511 | emerge_args, |
| 1512 | ) |
| 1513 | else: |
| 1514 | func = functools.partial( |
| 1515 | _UnmergePackages, pkgs, device, root, pkgs_attrs |
| 1516 | ) |
David Pursell | 2e77338 | 2015-04-03 14:30:47 -0700 | [diff] [blame] | 1517 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1518 | # Call the function with the progress bar or with normal output. |
| 1519 | if command.UseProgressBar(): |
| 1520 | op = BrilloDeployOperation(emerge) |
| 1521 | op.Run(func, log_level=logging.DEBUG) |
| 1522 | else: |
| 1523 | func() |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 1524 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1525 | if device.IsSELinuxAvailable(): |
| 1526 | if sum(x.count("selinux-policy") for x in pkgs): |
| 1527 | logging.warning( |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame^] | 1528 | "Deploying SELinux policy will not take effect until " |
| 1529 | "reboot. SELinux policy is loaded by init. Also, " |
| 1530 | "changing the security contexts (labels) of a file " |
| 1531 | "will require building a new image and flashing the " |
| 1532 | "image onto the device." |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1533 | ) |
Bertrand SIMONNET | 60c9449 | 2015-04-30 17:46:28 -0700 | [diff] [blame] | 1534 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1535 | # This message is read by BrilloDeployOperation. |
Mike Frysinger | 5c7b951 | 2020-12-04 02:30:56 -0500 | [diff] [blame] | 1536 | logging.warning( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1537 | "Please restart any updated services on the device, " |
| 1538 | "or just reboot it." |
| 1539 | ) |
| 1540 | except Exception: |
| 1541 | if lsb_release: |
| 1542 | lsb_entries = sorted(lsb_release.items()) |
| 1543 | logging.info( |
| 1544 | "Following are the LSB version details of the device:\n%s", |
| 1545 | "\n".join("%s=%s" % (k, v) for k, v in lsb_entries), |
| 1546 | ) |
| 1547 | raise |