| 1 |
#! /usr/bin/python |
| 2 |
# |
| 3 |
# Note that the above interpreter choice is correct - |
| 4 |
# '/usr/bin/python'. It isn't '/usr/bin/python2' or |
| 5 |
# '/usr/bin/python3'. But, this is OK. This script is run in a |
| 6 |
# container, and we won't know which python we're executing (or even |
| 7 |
# which will be installed), but we should be assured of one or the |
| 8 |
# other. This script was written to be executable by either version of |
| 9 |
# python (checked by running pylint-2 and pylint-3 on it). |
| 10 |
|
| 11 |
"""Install specific versions of packages.""" |
| 12 |
|
| 13 |
# Here we want to make sure that we've got all the right versions of |
| 14 |
# certain software installed. |
| 15 |
|
| 16 |
from __future__ import print_function |
| 17 |
import os |
| 18 |
import os.path |
| 19 |
import sys |
| 20 |
import subprocess |
| 21 |
import re |
| 22 |
import platform |
| 23 |
import getopt |
| 24 |
import shutil |
| 25 |
|
| 26 |
|
| 27 |
# split_nvra() is based on rpmUtils.miscutils.splitFilename() from |
| 28 |
# yum. Why not just use that? There isn't a python3 version supported |
| 29 |
# and yum is going away (at some point) from Fedora. |
| 30 |
def split_nvra(pkg_nvr): |
| 31 |
""" |
| 32 |
Pass in a standard style rpm name |
| 33 |
|
| 34 |
Returns a name, version, release, arch, e.g.:: |
| 35 |
foo-1.0-1.i386.rpm returns foo, 1.0, 1, i386 |
| 36 |
""" |
| 37 |
|
| 38 |
# If we've got a build id link target, it will end with |
| 39 |
# '.debug'. Remove it. |
| 40 |
if pkg_nvr[-6:] == '.debug': |
| 41 |
pkg_nvr = pkg_nvr[:-6] |
| 42 |
|
| 43 |
arch_index = pkg_nvr.rfind('.') |
| 44 |
arch = pkg_nvr[arch_index+1:] |
| 45 |
|
| 46 |
rel_index = pkg_nvr[:arch_index].rfind('-') |
| 47 |
rel = pkg_nvr[rel_index+1:arch_index] |
| 48 |
|
| 49 |
ver_index = pkg_nvr[:rel_index].rfind('-') |
| 50 |
ver = pkg_nvr[ver_index+1:rel_index] |
| 51 |
|
| 52 |
name = pkg_nvr[:ver_index] |
| 53 |
return name, ver, rel, arch |
| 54 |
|
| 55 |
def which(cmd): |
| 56 |
"""Find the full path of a command.""" |
| 57 |
for path in os.environ["PATH"].split(os.pathsep): |
| 58 |
if os.path.exists(os.path.join(path, cmd)): |
| 59 |
return os.path.join(path, cmd) |
| 60 |
|
| 61 |
return None |
| 62 |
|
| 63 |
def _eprint(*args, **kwargs): |
| 64 |
"""Print to stderr.""" |
| 65 |
print(*args, file=sys.stderr, **kwargs) |
| 66 |
|
| 67 |
def build_id_symlink_is_valid(bid_path): |
| 68 |
"""Check if a build id symlink is valid.""" |
| 69 |
symlink_exists = os.path.exists(bid_path) and os.path.islink(bid_path) |
| 70 |
bid_target = '' |
| 71 |
if symlink_exists: |
| 72 |
bid_target = os.readlink(bid_path) |
| 73 |
if not os.path.isabs(bid_target): |
| 74 |
bid_target = \ |
| 75 |
os.path.abspath(os.path.join(os.path.dirname(bid_path), |
| 76 |
bid_target)) |
| 77 |
if not os.path.exists(bid_target) \ |
| 78 |
or os.path.islink(bid_target): |
| 79 |
symlink_exists = 0 |
| 80 |
return symlink_exists, bid_target |
| 81 |
|
| 82 |
class PkgSystem(object): |
| 83 |
"A class to hide the details of package management." |
| 84 |
# pylint: disable=too-many-instance-attributes |
| 85 |
__verbose = 0 |
| 86 |
__distro_id = None |
| 87 |
__release = None |
| 88 |
__pkgr_path = None |
| 89 |
__pkgmgr_path = None |
| 90 |
__using_dnf = 0 |
| 91 |
__wget_path = None |
| 92 |
__local_repo_path = '' |
| 93 |
__local_rpm_dir = '' |
| 94 |
|
| 95 |
def __init__(self, verbose): |
| 96 |
self.__verbose = verbose |
| 97 |
|
| 98 |
# |
| 99 |
# Try to figure out what distro/release we've got. We might |
| 100 |
# have to do more later if necessary to figure out the |
| 101 |
# distro/release (like looking at /etc/redhat-release). |
| 102 |
# |
| 103 |
# Note that it isn't an error if we can't figure out the |
| 104 |
# distro/release - we may not need that information. |
| 105 |
lsb_release = which("lsb_release") |
| 106 |
if lsb_release != None: |
| 107 |
try: |
| 108 |
if self.__verbose: |
| 109 |
print("Running: %s" % [lsb_release, "-is"]) |
| 110 |
self.__distro_id = subprocess.check_output([lsb_release, "-is"]) |
| 111 |
self.__distro_id = self.__distro_id.strip() |
| 112 |
except subprocess.CalledProcessError: |
| 113 |
pass |
| 114 |
try: |
| 115 |
if self.__verbose: |
| 116 |
print("Running: %s" % [lsb_release, "-rs"]) |
| 117 |
self.__release = subprocess.check_output([lsb_release, "-rs"]) |
| 118 |
self.__release = self.__release.strip() |
| 119 |
except subprocess.CalledProcessError: |
| 120 |
pass |
| 121 |
if self.__distro_id is None: |
| 122 |
self.__distro_id = platform.linux_distribution()[0] |
| 123 |
if self.__release is None: |
| 124 |
self.__release = platform.linux_distribution()[1] |
| 125 |
|
| 126 |
# |
| 127 |
# Make sure we know the base package manager the system uses. |
| 128 |
self.__pkgr_path = which("rpm") |
| 129 |
if self.__pkgr_path is None: |
| 130 |
_eprint("Can't find the 'rpm' executable.") |
| 131 |
sys.exit(1) |
| 132 |
|
| 133 |
# |
| 134 |
# Find the package manager for this system. |
| 135 |
self.__pkgmgr_path = which("dnf") |
| 136 |
self.__debuginfo_install = [self.__pkgmgr_path, 'debuginfo-install'] |
| 137 |
self.__using_dnf = 1 |
| 138 |
if self.__pkgmgr_path is None: |
| 139 |
self.__pkgmgr_path = which("yum") |
| 140 |
self.__debuginfo_install = [which('debuginfo-install')] |
| 141 |
self.__using_dnf = 0 |
| 142 |
if self.__pkgmgr_path is None: |
| 143 |
_eprint("Can't find a package manager (either 'dnr' or 'yum').") |
| 144 |
sys.exit(1) |
| 145 |
|
| 146 |
# |
| 147 |
# See if we've got 'wget'. It isn't an error if we don't have |
| 148 |
# it since we may not need it. |
| 149 |
self.__wget_path = which("wget") |
| 150 |
|
| 151 |
def build_id_is_valid(self, name, build_id): |
| 152 |
"""Return true if the 'name' matches the build id.""" |
| 153 |
# If we don't have a build id, pretend it matched. |
| 154 |
if build_id == '': |
| 155 |
return 1 |
| 156 |
# First, make sure the build id symbolic link exists. This has |
| 157 |
# to be an exact match. Note that Centos build id symbolic |
| 158 |
# links don't end in '.debug', Fedora build id symbolic links |
| 159 |
# do. Handle both. |
| 160 |
bid_path = '/usr/lib/debug/.build-id/' + build_id[:2] \ |
| 161 |
+ '/' + build_id[2:] |
| 162 |
(symlink_exists, bid_target) = build_id_symlink_is_valid(bid_path) |
| 163 |
if not symlink_exists: |
| 164 |
bid_path = '/usr/lib/debug/.build-id/' + build_id[:2] \ |
| 165 |
+ '/' + build_id[2:] + '.debug' |
| 166 |
(symlink_exists, bid_target) = build_id_symlink_is_valid(bid_path) |
| 167 |
if not symlink_exists: |
| 168 |
if self.__verbose: |
| 169 |
print("Build id %s doesn't exist." % build_id) |
| 170 |
return 0 |
| 171 |
|
| 172 |
# Now we know the build id exists. But, does it point to the |
| 173 |
# correct file? Note that we're comparing basenames here. Why? |
| 174 |
# (1) The kernel doesn't really have a "path". (2) |
| 175 |
# "/usr/bin/FOO" is really the same file as "/bin/FOO" |
| 176 |
# (UsrMove feature). |
| 177 |
sym_target = os.path.basename(bid_target) |
| 178 |
if name == 'kernel': |
| 179 |
name = 'vmlinux' |
| 180 |
else: |
| 181 |
name = os.path.basename(name) |
| 182 |
matchp = (sym_target == name) |
| 183 |
if not matchp: |
| 184 |
# On Fedora 28, the link doesn't point to "FOO", but |
| 185 |
# "FOO-V-R.A.debug". |
| 186 |
pkg_details = split_nvra(sym_target) |
| 187 |
if pkg_details[0] == name: |
| 188 |
matchp = 1 |
| 189 |
if not matchp: |
| 190 |
if self.__verbose: |
| 191 |
print("Build id %s doesn't match '%s'." % (build_id, name)) |
| 192 |
return 0 |
| 193 |
return 1 |
| 194 |
|
| 195 |
def pkg_exists(self, pkg_nvr): |
| 196 |
"""Return true if the package and its debuginfo exists.""" |
| 197 |
if self.__verbose: |
| 198 |
print("Running: %s" % [self.__pkgr_path, '-qi', '--quiet', |
| 199 |
pkg_nvr]) |
| 200 |
if subprocess.call([self.__pkgr_path, '-qi', '--quiet', pkg_nvr]) != 0: |
| 201 |
return 0 |
| 202 |
return 1 |
| 203 |
|
| 204 |
def pkg_install(self, pkg_nvr, build_id): |
| 205 |
"""Install a package and its debuginfo.""" |
| 206 |
if not self.pkg_exists(pkg_nvr): |
| 207 |
# Why are we using the "noscripts" option here? Background |
| 208 |
# - building a Fedora 28 container on a Centos 7 host. On |
| 209 |
# a kernel install, the Fedora kernel rpm %post script ran |
| 210 |
# "dracut" to create a new initrd. The dracut command hit |
| 211 |
# an error, then proceeded to spew lots of errors and |
| 212 |
# ended up deleting /tmp in the container. So, to avoid |
| 213 |
# this, we won't run %pre/%post rpm scripts (which |
| 214 |
# hopefully shouldn't be needed anyway). If the %pre/%post |
| 215 |
# scripts end up being needed, we could only use |
| 216 |
# "noscripts" when installing a kernel. |
| 217 |
# |
| 218 |
# If we're using dnf, add the '--allowerasing' option so that |
| 219 |
# we can override conflicting packages. |
| 220 |
cmd = [self.__pkgmgr_path, 'install', '-y', '--quiet'] \ |
| 221 |
+ (['--allowerasing'] if self.__using_dnf else []) \ |
| 222 |
+ ['--setopt=tsflags=noscripts', pkg_nvr] |
| 223 |
if self.__verbose: |
| 224 |
print("Running: %s" % cmd) |
| 225 |
if subprocess.call(cmd) != 0: |
| 226 |
return 0 |
| 227 |
|
| 228 |
# If we don't have a build id, we don't need to install |
| 229 |
# debuginfo. |
| 230 |
if build_id == '': |
| 231 |
return 1 |
| 232 |
|
| 233 |
# Here we're assuming the debuginfo package doesn't already exist. |
| 234 |
cmd = self.__debuginfo_install + ['-y', '--quiet', pkg_nvr] |
| 235 |
if self.__verbose: |
| 236 |
print("Running: %s" % cmd) |
| 237 |
if subprocess.call(cmd) != 0: |
| 238 |
return 0 |
| 239 |
# 'dnf debuginfo' has a *really* annoying habit of not |
| 240 |
# installing the exact version you asked for if the version |
| 241 |
# you asked for isn't available. So, make sure we actually |
| 242 |
# installed the right debuginfo package. |
| 243 |
pkg_details = split_nvra(pkg_nvr) |
| 244 |
debuginfo_nvr = (pkg_details[0] + '-debuginfo-' + pkg_details[1] |
| 245 |
+ '-' + pkg_details[2] + '.' + pkg_details[3]) |
| 246 |
if not self.pkg_exists(debuginfo_nvr): |
| 247 |
# The wrong debuginfo package got installed. Try to remove |
| 248 |
# it. |
| 249 |
debuginfo_wildcard = pkg_details[0] + '-debuginfo-*' |
| 250 |
cmd = [self.__pkgmgr_path, 'remove', '-y', |
| 251 |
'--setopt=tsflags=noscripts', debuginfo_wildcard] |
| 252 |
if self.__verbose: |
| 253 |
print("Running: %s" % cmd) |
| 254 |
subprocess.call(cmd) |
| 255 |
return 0 |
| 256 |
return 1 |
| 257 |
|
| 258 |
def pkg_download_and_install(self, pkg_nvr, build_id): |
| 259 |
"""Manually download and install a package.""" |
| 260 |
# If we're not on Fedora, we don't know how to get the |
| 261 |
# package. |
| 262 |
if self.__wget_path is None or self.__distro_id is None \ |
| 263 |
or self.__distro_id.lower() != "fedora": |
| 264 |
_eprint("Can't download package '%s'" % pkg_nvr) |
| 265 |
return 0 |
| 266 |
|
| 267 |
# Try downloading the package from koji, Fedora's build system. |
| 268 |
# |
| 269 |
# Build up the koji url. Koji urls look like: |
| 270 |
# http://kojipkgs.fedoraproject.org/packages/NAME/VER/RELEASE/ARCH/ |
| 271 |
pkg_details = split_nvra(pkg_nvr) |
| 272 |
koji_url = ("http://kojipkgs.fedoraproject.org/packages/%s/%s/%s/%s" |
| 273 |
% (pkg_details[0], pkg_details[1], pkg_details[2], |
| 274 |
pkg_details[3])) |
| 275 |
_eprint("URL: '%s'" % koji_url) |
| 276 |
|
| 277 |
# Download the entire arch directory. Here's a description of |
| 278 |
# wget's arguments: |
| 279 |
# |
| 280 |
# --quiet: Don't display progress. |
| 281 |
# -nH: No host directories (i.e. get rid of the host name in |
| 282 |
# the download directory name). |
| 283 |
# --cut-dirs=4: Ignore 4 directory components. |
| 284 |
# -r: Turn on recursive retrieving. |
| 285 |
# -l 1: Maximum recursion depth is 1. |
| 286 |
# |
| 287 |
if self.__verbose: |
| 288 |
print("Running: %s" % ['wget', '--quiet', '-nH', '--cut-dirs=4', |
| 289 |
'-r', '-l', '1', koji_url]) |
| 290 |
if subprocess.call(['wget', '--quiet', '-nH', '--cut-dirs=4', |
| 291 |
'-r', '-l', '1', koji_url]) != 0: |
| 292 |
_eprint("Can't download package '%s'" % pkg_nvr) |
| 293 |
return 0 |
| 294 |
|
| 295 |
# OK, now we've got a directory which contains all the RPMs |
| 296 |
# for package 'foo'. We can't just do a "dnf install RPM", |
| 297 |
# because (for example) the 'kernel' RPM requires the |
| 298 |
# 'kernel-core' and 'kernel-firmware' RPMs. We might be able |
| 299 |
# to install all the RPMs we just downloaded, but besides |
| 300 |
# being overkill, it is theoretically possible that they might |
| 301 |
# conflict somehow. |
| 302 |
# |
| 303 |
# So, instead we'll create a local repo that then yum/dnf can |
| 304 |
# use when looking for RPMs. |
| 305 |
|
| 306 |
# First create the repo file. |
| 307 |
self.__local_repo_path = '/etc/yum.repos.d/local.repo' |
| 308 |
self.__local_rpm_dir = '/root/%s' % pkg_details[3] |
| 309 |
if not os.path.exists(self.__local_repo_path): |
| 310 |
repo_file = open(self.__local_repo_path, 'w') |
| 311 |
repo_file.write('[local]\n') |
| 312 |
repo_file.write('name=Local repository\n') |
| 313 |
repo_file.write('baseurl=file://%s\n' % self.__local_rpm_dir) |
| 314 |
repo_file.write('enabled=1\n') |
| 315 |
repo_file.write('gpgcheck=0\n') |
| 316 |
repo_file.write('type=rpm\n') |
| 317 |
repo_file.close() |
| 318 |
|
| 319 |
# Next run 'createrepo_c' on the directory. |
| 320 |
if self.__verbose: |
| 321 |
print("Running: %s" % ['createrepo_c', '--quiet', self.__local_rpm_dir]) |
| 322 |
if subprocess.call(['createrepo_c', '--quiet', self.__local_rpm_dir]) != 0: |
| 323 |
_eprint("Can't run createrepo_c") |
| 324 |
return 0 |
| 325 |
|
| 326 |
# At this point we should be set up to let the package manager |
| 327 |
# install the package. |
| 328 |
return self.pkg_install(pkg_nvr, build_id) |
| 329 |
|
| 330 |
def cleanup(self): |
| 331 |
"""Perform cleanup (if necessary).""" |
| 332 |
if self.__local_repo_path: |
| 333 |
os.remove(self.__local_repo_path) |
| 334 |
if self.__local_rpm_dir: |
| 335 |
shutil.rmtree(self.__local_rpm_dir) |
| 336 |
|
| 337 |
def _usage(): |
| 338 |
"""Display command-line usage.""" |
| 339 |
_eprint("Usage: %s [-v] --name NAME --pkg PACKAGE --build_id BUILD_ID" |
| 340 |
% sys.argv[0]) |
| 341 |
sys.exit(1) |
| 342 |
|
| 343 |
def _handle_command_line(): |
| 344 |
"""Process command line.""" |
| 345 |
verbose = 0 |
| 346 |
name = '' |
| 347 |
pkg_nvr = '' |
| 348 |
build_id = '' |
| 349 |
|
| 350 |
# Make sure the command line looks reasonable. |
| 351 |
if len(sys.argv) < 4: |
| 352 |
_usage() |
| 353 |
try: |
| 354 |
(opts, pargs) = getopt.getopt(sys.argv[1:], 'v', ['name=', 'pkg=', 'build_id=']) |
| 355 |
except getopt.GetoptError as err: |
| 356 |
_eprint("Error: %s" % err) |
| 357 |
_usage() |
| 358 |
for (opt, value) in opts: |
| 359 |
if opt == '-v': |
| 360 |
verbose += 1 |
| 361 |
elif opt == '--name': |
| 362 |
name = value |
| 363 |
elif opt == '--pkg': |
| 364 |
pkg_nvr = value |
| 365 |
elif opt == '--build_id': |
| 366 |
build_id = value |
| 367 |
if pargs: |
| 368 |
_usage() |
| 369 |
if not name or not pkg_nvr or not build_id: |
| 370 |
_eprint("Error: '--name', '--pkg', and '--build_id' are required arguments.") |
| 371 |
_usage() |
| 372 |
return (verbose, name, pkg_nvr, build_id) |
| 373 |
|
| 374 |
def main(): |
| 375 |
"""Main function.""" |
| 376 |
(verbose, name, pkg_nvr, build_id) = _handle_command_line() |
| 377 |
|
| 378 |
# Make sure we're in /root. |
| 379 |
os.chdir('/root') |
| 380 |
|
| 381 |
packages = [] |
| 382 |
packages.append([name, pkg_nvr, build_id]) |
| 383 |
|
| 384 |
# If the package name is 'kernel', we've got to do some special |
| 385 |
# processing. We also want to install the matching kernel-devel |
| 386 |
# (along with the debuginfo). |
| 387 |
# |
| 388 |
# Note that we have to handle/recognize kernel variants, like |
| 389 |
# 'kernel-PAE' or 'kernel-debug'. |
| 390 |
kernel_regexp = re.compile(r'^kernel(-\w+)?') |
| 391 |
match = kernel_regexp.match(name) |
| 392 |
if match: |
| 393 |
devel_name = name + '-devel' |
| 394 |
devel_nvr = re.sub(name, devel_name, pkg_nvr) |
| 395 |
# Notice we're not including the build id. When there's no |
| 396 |
# build id, build_id_is_valid() returns a 1. |
| 397 |
packages.append([devel_name, devel_nvr, '']) |
| 398 |
|
| 399 |
pkgsys = PkgSystem(verbose) |
| 400 |
for (name, pkg_nvr, build_id) in packages: |
| 401 |
# Try using the package manager to install the package and its |
| 402 |
# debuginfo. |
| 403 |
if pkgsys.pkg_install(pkg_nvr, build_id): |
| 404 |
# The package and its debuginfo exist. Does the build id |
| 405 |
# match? |
| 406 |
if pkgsys.build_id_is_valid(name, build_id): |
| 407 |
continue |
| 408 |
# If the package and its debuginfo exists, but the build |
| 409 |
# ids don't match, we're done. |
| 410 |
_eprint("Package '%s' is installed, but the build id" |
| 411 |
" doesn't match" % pkg_nvr) |
| 412 |
pkgsys.cleanup() |
| 413 |
sys.exit(1) |
| 414 |
|
| 415 |
# As a last resort, try downloading and installing the package |
| 416 |
# manually. |
| 417 |
if pkgsys.pkg_download_and_install(pkg_nvr, build_id): |
| 418 |
# The package and its debuginfo exist. Does the build id |
| 419 |
# match? |
| 420 |
if pkgsys.build_id_is_valid(name, build_id): |
| 421 |
continue |
| 422 |
# If the package and its debuginfo exists, but the build |
| 423 |
# ids don't match, we're done. |
| 424 |
_eprint("Package '%s' is installed, but the build id" |
| 425 |
" doesn't match" % pkg_nvr) |
| 426 |
pkgsys.cleanup() |
| 427 |
sys.exit(1) |
| 428 |
|
| 429 |
# If the package manager couldn't install the package, and we |
| 430 |
# couldn't download and install the package manually, we're |
| 431 |
# done. |
| 432 |
_eprint("Can't find package '%s'" % pkg_nvr) |
| 433 |
sys.exit(1) |
| 434 |
|
| 435 |
if verbose: |
| 436 |
print("All packages installed.") |
| 437 |
|
| 438 |
# Perform cleanup, if needed. |
| 439 |
pkgsys.cleanup() |
| 440 |
sys.exit(0) |
| 441 |
|
| 442 |
if __name__ == '__main__': |
| 443 |
main() |