| 1 |
|
| 2 |
"""List pip dependencies or system package dependencies for cloud-init.""" |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
try: |
| 9 |
from argparse import ArgumentParser |
| 10 |
except ImportError: |
| 11 |
raise RuntimeError( |
| 12 |
"Could not import argparse. Please install python3-argparse " |
| 13 |
"package to continue" |
| 14 |
) |
| 15 |
|
| 16 |
import json |
| 17 |
import os |
| 18 |
import re |
| 19 |
import subprocess |
| 20 |
import sys |
| 21 |
|
| 22 |
DEFAULT_REQUIREMENTS = "requirements.txt" |
| 23 |
|
| 24 |
DEFAULT_PKG_PREFIX = "python3-" |
| 25 |
FREEBSD_PKG_PREFIX = f"py{sys.version_info.major}{sys.version_info.minor}-" |
| 26 |
OPENBSD_PKG_PREFIX = "py3-" |
| 27 |
|
| 28 |
|
| 29 |
DISTRO_PKG_TYPE_MAP = { |
| 30 |
"centos": "redhat", |
| 31 |
"eurolinux": "redhat", |
| 32 |
"miraclelinux": "redhat", |
| 33 |
"freebsd": "freebsd", |
| 34 |
"openbsd": "openbsd", |
| 35 |
"fedora": "fedora", |
| 36 |
"rocky": "redhat", |
| 37 |
"redhat": "redhat", |
| 38 |
"debian": "debian", |
| 39 |
"ubuntu": "debian", |
| 40 |
"opensuse": "suse", |
| 41 |
"opensuse-leap": "suse", |
| 42 |
"opensuse-microos": "suse", |
| 43 |
"opensuse-tumbleweed": "suse", |
| 44 |
"sle_hpc": "suse", |
| 45 |
"sle-micro": "suse", |
| 46 |
"sles": "suse", |
| 47 |
"suse": "suse", |
| 48 |
} |
| 49 |
|
| 50 |
MAYBE_RELIABLE_YUM_INSTALL = [ |
| 51 |
"sh", |
| 52 |
"-c", |
| 53 |
""" |
| 54 |
error() { echo "$@" 1>&2; } |
| 55 |
configure_repos_for_proxy_use() { |
| 56 |
if [ -f /etc/yum.conf ]; then |
| 57 |
CFG_FILE=/etc/yum.conf |
| 58 |
fi |
| 59 |
if [ -f /etc/dnf/dnf.conf ]; then |
| 60 |
CFG_FILE=/etc/dnf/dnf.conf |
| 61 |
fi |
| 62 |
if [ ! "${CFG_FILE}" ]; then |
| 63 |
error "No yum.conf or dnf.conf" |
| 64 |
return 0 |
| 65 |
fi |
| 66 |
grep -q "^proxy=" $CFG_FILE || return 0 |
| 67 |
error ":: http proxy in use => forcing the use of fixed URLs in /etc/yum.repos.d/*.repo" |
| 68 |
sed -i --regexp-extended '/^#baseurl=/s/#// ; /^(mirrorlist|metalink)=/s/^/#/' /etc/yum.repos.d/*.repo |
| 69 |
sed -i 's/download\\.fedoraproject\\.org/dl.fedoraproject.org/g' /etc/yum.repos.d/*.repo |
| 70 |
sed -i 's/download\\.example/dl.fedoraproject.org/g' /etc/yum.repos.d/*.repo |
| 71 |
CENTOS_REPO="/etc/yum.repos.d/centos.repo" |
| 72 |
if [ -f $CENTOS_REPO ]; then |
| 73 |
grep -q baseurl $CENTOS_REPO |
| 74 |
if [ $? -eq 1 ]; then |
| 75 |
# CentOS 9 does not provide baseurl definitions |
| 76 |
sed -i '/\\[baseos\\]/a baseurl=https://mirror.stream.centos.org/9-stream/BaseOS/x86_64/os' ${CENTOS_REPO} |
| 77 |
sed -i '/\\[appstream\\]/a baseurl=https://mirror.stream.centos.org/9-stream/AppStream/x86_64/os' ${CENTOS_REPO} |
| 78 |
sed -i '/\\[crb\\]/a baseurl=https://mirror.stream.centos.org/9-stream/CRB/x86_64/os' ${CENTOS_REPO} |
| 79 |
fi |
| 80 |
CENTOS_EXTRAS_REPO="/etc/yum.repos.d/centos-addons.repo" |
| 81 |
if [ -f $CENTOS_EXTRAS_REPO ]; then |
| 82 |
sed -i '/\\[extras-common\\]/a baseurl=https://mirror.stream.centos.org/SIGs/9-stream/extras/x86_64/extras-common' ${CENTOS_EXTRAS_REPO} |
| 83 |
fi |
| 84 |
fi |
| 85 |
} |
| 86 |
configure_repos_for_proxy_use |
| 87 |
n=0; max=10; |
| 88 |
bcmd="yum install --downloadonly --assumeyes --setopt=keepcache=1" |
| 89 |
while n=$(($n+1)); do |
| 90 |
error ":: running $bcmd $* [$n/$max]" |
| 91 |
$bcmd "$@" |
| 92 |
r=$? |
| 93 |
[ $r -eq 0 ] && break |
| 94 |
[ $n -ge $max ] && { error "gave up on $bcmd"; exit $r; } |
| 95 |
nap=$(($n*5)) |
| 96 |
error ":: failed [$r] ($n/$max). sleeping $nap." |
| 97 |
sleep $nap |
| 98 |
done |
| 99 |
error ":: running yum install --cacheonly --assumeyes $*" |
| 100 |
yum install --cacheonly --assumeyes "$@" |
| 101 |
configure_repos_for_proxy_use |
| 102 |
""", |
| 103 |
"reliable-yum-install", |
| 104 |
] |
| 105 |
|
| 106 |
ZYPPER_INSTALL = [ |
| 107 |
"zypper", |
| 108 |
"--non-interactive", |
| 109 |
"--gpg-auto-import-keys", |
| 110 |
"install", |
| 111 |
"--auto-agree-with-licenses", |
| 112 |
] |
| 113 |
|
| 114 |
DRYRUN_DISTRO_INSTALL_PKG_CMD = { |
| 115 |
"redhat": ["yum", "install", "--assumeyes"], |
| 116 |
"fedora": ["yum", "install", "--assumeyes"], |
| 117 |
"freebsd": ["pkg", "install", "--yes"], |
| 118 |
"openbsd": ["pkg_add", "-n"], |
| 119 |
} |
| 120 |
|
| 121 |
DISTRO_INSTALL_PKG_CMD = { |
| 122 |
"redhat": MAYBE_RELIABLE_YUM_INSTALL, |
| 123 |
"fedora": MAYBE_RELIABLE_YUM_INSTALL, |
| 124 |
"freebsd": ["pkg", "install", "--yes"], |
| 125 |
"openbsd": ["pkg_add", "-I"], |
| 126 |
"debian": ["apt", "install", "-y"], |
| 127 |
"suse": ZYPPER_INSTALL, |
| 128 |
} |
| 129 |
|
| 130 |
DISTRO_UPDATE_PKG_CMD = { |
| 131 |
"redhat": ["yum", "update"], |
| 132 |
"freebsd": ["pkg", "update"], |
| 133 |
"openbsd": ["pkg_add", "-I", "-u"], |
| 134 |
"debian": ["apt", "update", "-q"], |
| 135 |
"suse": ["zypper", "update"], |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
CI_SYSTEM_BASE_PKGS = { |
| 140 |
"common": ["sudo"], |
| 141 |
"linux_common": ["make", "tar"], |
| 142 |
"eurolinux": ["python3-tox"], |
| 143 |
"miraclelinux": ["python3-tox"], |
| 144 |
"fedora": ["python3-tox"], |
| 145 |
"freebsd": [f"{FREEBSD_PKG_PREFIX}tox"], |
| 146 |
"openbsd": [f"{OPENBSD_PKG_PREFIX}tox"], |
| 147 |
"redhat": ["python3-tox"], |
| 148 |
"centos": ["python3-tox"], |
| 149 |
"ubuntu": [ |
| 150 |
"make", |
| 151 |
"devscripts", |
| 152 |
"python3-dev", |
| 153 |
"libssl-dev", |
| 154 |
"tox", |
| 155 |
"sbuild", |
| 156 |
], |
| 157 |
"debian": [ |
| 158 |
"make", |
| 159 |
"devscripts", |
| 160 |
"python3-dev", |
| 161 |
"libssl-dev", |
| 162 |
"tox", |
| 163 |
"sbuild", |
| 164 |
], |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
DISTRO_PKG_DEPS_PATH = "packages/pkg-deps.json" |
| 170 |
|
| 171 |
|
| 172 |
def get_parser(): |
| 173 |
"""Return an argument parser for this command.""" |
| 174 |
parser = ArgumentParser(description=__doc__) |
| 175 |
parser.add_argument( |
| 176 |
"-r", |
| 177 |
"--requirements-file", |
| 178 |
type=str, |
| 179 |
dest="req_files", |
| 180 |
action="append", |
| 181 |
default=None, |
| 182 |
help="pip-style requirements file [default=%s]" % DEFAULT_REQUIREMENTS, |
| 183 |
) |
| 184 |
parser.add_argument( |
| 185 |
"-d", |
| 186 |
"--distro", |
| 187 |
type=str, |
| 188 |
choices=DISTRO_PKG_TYPE_MAP.keys(), |
| 189 |
help="The name of the distro to generate package deps for.", |
| 190 |
) |
| 191 |
deptype = parser.add_mutually_exclusive_group() |
| 192 |
deptype.add_argument( |
| 193 |
"-R", |
| 194 |
"--runtime-requires", |
| 195 |
action="store_true", |
| 196 |
default=False, |
| 197 |
dest="runtime_requires", |
| 198 |
help="Print only runtime required packages", |
| 199 |
) |
| 200 |
deptype.add_argument( |
| 201 |
"-b", |
| 202 |
"--build-requires", |
| 203 |
action="store_true", |
| 204 |
default=False, |
| 205 |
dest="build_requires", |
| 206 |
help="Print only buildtime required packages", |
| 207 |
) |
| 208 |
parser.add_argument( |
| 209 |
"--dry-run", |
| 210 |
action="store_true", |
| 211 |
default=False, |
| 212 |
dest="dry_run", |
| 213 |
help="Dry run the install, making no package changes.", |
| 214 |
) |
| 215 |
parser.add_argument( |
| 216 |
"-s", |
| 217 |
"--system-pkg-names", |
| 218 |
action="store_true", |
| 219 |
default=False, |
| 220 |
dest="system_pkg_names", |
| 221 |
help="Generate distribution package names (python3-pkgname).", |
| 222 |
) |
| 223 |
parser.add_argument( |
| 224 |
"-i", |
| 225 |
"--install", |
| 226 |
action="store_true", |
| 227 |
default=False, |
| 228 |
dest="install", |
| 229 |
help="When specified, install the required system packages.", |
| 230 |
) |
| 231 |
parser.add_argument( |
| 232 |
"-t", |
| 233 |
"--test-distro", |
| 234 |
action="store_true", |
| 235 |
default=False, |
| 236 |
dest="test_distro", |
| 237 |
help="Additionally install continuous integration system packages " |
| 238 |
"required for build and test automation.", |
| 239 |
) |
| 240 |
return parser |
| 241 |
|
| 242 |
|
| 243 |
def get_package_deps_from_json(topdir, distro): |
| 244 |
"""Get a dict of build and runtime package requirements for a distro. |
| 245 |
|
| 246 |
@param topdir: The root directory in which to search for the |
| 247 |
DISTRO_PKG_DEPS_PATH json blob of package requirements information. |
| 248 |
@param distro: The specific distribution shortname to pull dependencies |
| 249 |
for. |
| 250 |
@return: Dict containing "requires", "build-requires" and "rename" lists |
| 251 |
for a given distribution. |
| 252 |
""" |
| 253 |
with open(os.path.join(topdir, DISTRO_PKG_DEPS_PATH), "r") as stream: |
| 254 |
deps = json.loads(stream.read()) |
| 255 |
if distro is None: |
| 256 |
return {} |
| 257 |
if deps.get(distro): |
| 258 |
return deps[distro] |
| 259 |
|
| 260 |
return deps[DISTRO_PKG_TYPE_MAP[distro]] |
| 261 |
|
| 262 |
|
| 263 |
def parse_pip_requirements(requirements_path): |
| 264 |
"""Return the pip requirement names from pip-style requirements_path.""" |
| 265 |
dep_names = [] |
| 266 |
with open(requirements_path, "r") as fp: |
| 267 |
for line in fp: |
| 268 |
line = line.strip() |
| 269 |
if not line or line.startswith(("#", "-r ")): |
| 270 |
continue |
| 271 |
|
| 272 |
|
| 273 |
dep = line.split(";")[0] |
| 274 |
|
| 275 |
|
| 276 |
version_comparison = re.compile(r"[~!>=.<]+") |
| 277 |
if version_comparison.search(dep): |
| 278 |
dep_names.append(version_comparison.split(dep)[0].strip()) |
| 279 |
else: |
| 280 |
dep_names.append(dep) |
| 281 |
return dep_names |
| 282 |
|
| 283 |
|
| 284 |
def translate_pip_to_system_pkg(distro, pip_requires, renames): |
| 285 |
"""Translate pip package names to distro-specific package names. |
| 286 |
|
| 287 |
@param pip_requires: List of versionless pip package names to translate. |
| 288 |
@param renames: Dict containing special case renames from pip name to |
| 289 |
system package name for the distro. |
| 290 |
""" |
| 291 |
if distro in ("freebsd",): |
| 292 |
prefix = FREEBSD_PKG_PREFIX |
| 293 |
elif distro in ("openbsd",): |
| 294 |
prefix = OPENBSD_PKG_PREFIX |
| 295 |
else: |
| 296 |
prefix = DEFAULT_PKG_PREFIX |
| 297 |
standard_pkg_name = "{0}{1}" |
| 298 |
translated_names = [] |
| 299 |
for pip_name in pip_requires: |
| 300 |
pip_name = pip_name.lower() |
| 301 |
|
| 302 |
rename = renames.get(pip_name, "") |
| 303 |
if rename: |
| 304 |
translated_names.append(rename) |
| 305 |
else: |
| 306 |
translated_names.append(standard_pkg_name.format(prefix, pip_name)) |
| 307 |
return translated_names |
| 308 |
|
| 309 |
|
| 310 |
def main(distro): |
| 311 |
parser = get_parser() |
| 312 |
args = parser.parse_args() |
| 313 |
if "CLOUD_INIT_TOP_D" in os.environ: |
| 314 |
topd = os.path.realpath(os.environ.get("CLOUD_INIT_TOP_D")) |
| 315 |
else: |
| 316 |
topd = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 317 |
|
| 318 |
if args.test_distro: |
| 319 |
|
| 320 |
if args.req_files: |
| 321 |
sys.stderr.write( |
| 322 |
"Parameter --test-distro overrides --requirements-file. Use " |
| 323 |
"one or the other.\n" |
| 324 |
) |
| 325 |
sys.exit(1) |
| 326 |
args.req_files = [ |
| 327 |
os.path.join(topd, DEFAULT_REQUIREMENTS), |
| 328 |
os.path.join(topd, "test-" + DEFAULT_REQUIREMENTS), |
| 329 |
] |
| 330 |
args.install = True |
| 331 |
if args.req_files is None: |
| 332 |
args.req_files = [os.path.join(topd, DEFAULT_REQUIREMENTS)] |
| 333 |
if not os.path.isfile(args.req_files[0]): |
| 334 |
sys.stderr.write( |
| 335 |
"Unable to locate '%s' file that should " |
| 336 |
"exist in cloud-init root directory." % args.req_files[0] |
| 337 |
) |
| 338 |
sys.exit(1) |
| 339 |
|
| 340 |
bad_files = [r for r in args.req_files if not os.path.isfile(r)] |
| 341 |
if bad_files: |
| 342 |
sys.stderr.write( |
| 343 |
"Unable to find requirements files: %s\n" % ",".join(bad_files) |
| 344 |
) |
| 345 |
sys.exit(1) |
| 346 |
|
| 347 |
pip_pkg_names = set() |
| 348 |
for req_path in args.req_files: |
| 349 |
pip_pkg_names.update(set(parse_pip_requirements(req_path))) |
| 350 |
deps_from_json = get_package_deps_from_json(topd, args.distro) |
| 351 |
renames = deps_from_json.get("renames", {}) |
| 352 |
translated_pip_names = translate_pip_to_system_pkg( |
| 353 |
args.distro, pip_pkg_names, renames |
| 354 |
) |
| 355 |
all_deps = [] |
| 356 |
select_requires = [args.build_requires, args.runtime_requires] |
| 357 |
if args.distro: |
| 358 |
if not any(select_requires): |
| 359 |
all_deps.extend( |
| 360 |
translated_pip_names |
| 361 |
+ deps_from_json["requires"] |
| 362 |
+ deps_from_json["build-requires"] |
| 363 |
) |
| 364 |
else: |
| 365 |
if args.build_requires: |
| 366 |
all_deps.extend(deps_from_json["build-requires"]) |
| 367 |
else: |
| 368 |
all_deps.extend( |
| 369 |
translated_pip_names + deps_from_json["requires"] |
| 370 |
) |
| 371 |
else: |
| 372 |
if args.system_pkg_names: |
| 373 |
all_deps = translated_pip_names |
| 374 |
else: |
| 375 |
all_deps = pip_pkg_names |
| 376 |
all_deps = sorted(all_deps) |
| 377 |
if args.install: |
| 378 |
pkg_install(all_deps, args.distro, args.test_distro, args.dry_run) |
| 379 |
else: |
| 380 |
print("\n".join(all_deps)) |
| 381 |
|
| 382 |
|
| 383 |
def pkg_install(pkg_list, distro, test_distro=False, dry_run=False): |
| 384 |
"""Install a list of packages using the DISTRO_INSTALL_PKG_CMD.""" |
| 385 |
if test_distro: |
| 386 |
pkg_list = list(pkg_list) + CI_SYSTEM_BASE_PKGS["common"] |
| 387 |
if distro not in ("freebsd", "openbsd"): |
| 388 |
pkg_list += CI_SYSTEM_BASE_PKGS["linux_common"] |
| 389 |
distro_base_pkgs = CI_SYSTEM_BASE_PKGS.get(distro, []) |
| 390 |
pkg_list += distro_base_pkgs |
| 391 |
print( |
| 392 |
"Installing deps: {0}{1}".format( |
| 393 |
"(dryrun)" if dry_run else "", " ".join(pkg_list) |
| 394 |
) |
| 395 |
) |
| 396 |
distro_family = DISTRO_PKG_TYPE_MAP[distro] |
| 397 |
install_cmd = [] |
| 398 |
update_cmd = DISTRO_UPDATE_PKG_CMD.get(distro_family, []) |
| 399 |
if dry_run: |
| 400 |
install_cmd.append("echo") |
| 401 |
if update_cmd: |
| 402 |
update_cmd.insert(0, "echo") |
| 403 |
if os.geteuid() != 0: |
| 404 |
install_cmd.append("sudo") |
| 405 |
if update_cmd: |
| 406 |
update_cmd.append("sudo") |
| 407 |
|
| 408 |
if dry_run and distro_family in DRYRUN_DISTRO_INSTALL_PKG_CMD: |
| 409 |
cmd = DRYRUN_DISTRO_INSTALL_PKG_CMD[distro_family] |
| 410 |
else: |
| 411 |
cmd = DISTRO_INSTALL_PKG_CMD[distro_family] |
| 412 |
install_cmd.extend(cmd) |
| 413 |
|
| 414 |
if update_cmd: |
| 415 |
subprocess.check_call(update_cmd) |
| 416 |
|
| 417 |
if distro in ["centos", "redhat", "rocky", "eurolinux"]: |
| 418 |
|
| 419 |
subprocess.check_call(install_cmd + ["epel-release"]) |
| 420 |
subprocess.check_call( |
| 421 |
["dnf", "config-manager", "--set-enabled", "crb"] |
| 422 |
) |
| 423 |
|
| 424 |
|
| 425 |
subprocess.call( |
| 426 |
["dnf", "config-manager", "--set-disabled", "epel-cisco-openh264"] |
| 427 |
) |
| 428 |
if DISTRO_PKG_TYPE_MAP.get(distro) in ["redhat", "suse", "fedora"]: |
| 429 |
pkg_list.append("rpm-build") |
| 430 |
subprocess.check_call(install_cmd + pkg_list) |
| 431 |
|
| 432 |
|
| 433 |
if __name__ == "__main__": |
| 434 |
parser = get_parser() |
| 435 |
args = parser.parse_args() |
| 436 |
sys.exit(main(args.distro)) |