|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# find_img.py |
| 4 | +# |
| 5 | +# Searches for an image with distribution and version and purpose |
| 6 | +# |
| 7 | +# (c) Kurt Garloff <[email protected]>, 7/2025 |
| 8 | +# SPDX-License-Identifier: MIT |
| 9 | +"This module finds the a distribution image with a given purpose" |
| 10 | + |
| 11 | +import os |
| 12 | +import sys |
| 13 | +import openstack |
| 14 | +# import logging |
| 15 | + |
| 16 | + |
| 17 | +def warn(log, msg): |
| 18 | + "warn output" |
| 19 | + if log: |
| 20 | + log.warn(msg) |
| 21 | + else: |
| 22 | + print(f"WARN: {msg}", file=sys.stderr) |
| 23 | + |
| 24 | + |
| 25 | +def debug(log, msg): |
| 26 | + "debug output" |
| 27 | + if log: |
| 28 | + log.debug(msg) |
| 29 | + else: |
| 30 | + print(f"DEBUG: {msg}", file=sys.stderr) |
| 31 | + |
| 32 | + |
| 33 | +def img_sort_heuristic(images, distro, version, purpose): |
| 34 | + """Sort list to prefer old names""" |
| 35 | + # Do sorting magic (could be omitted) |
| 36 | + newlist = [] |
| 37 | + distro = distro.lower() |
| 38 | + version = version.lower() |
| 39 | + purpose = purpose.lower() |
| 40 | + # 0th: Exact match old SCS naming scheme ("Ubuntu 24.04 Minimal") |
| 41 | + for img in images: |
| 42 | + newel = (img.id, img.name) |
| 43 | + if img.name.lower() == f"{distro} {version} {purpose}": |
| 44 | + newlist.append(newel) |
| 45 | + elif img.name.lower() == f"{distro} {purpose} {version}": |
| 46 | + newlist.append(newel) |
| 47 | + # 1st: Exact match old SCS naming scheme ("Ubuntu 24.04") |
| 48 | + for img in images: |
| 49 | + newel = (img.id, img.name) |
| 50 | + if img.name.lower() == f"{distro} {version}": |
| 51 | + newlist.append(newel) |
| 52 | + # 2nd: Fuzzy match old SCS naming scheme ("Ubuntu 24.04*") |
| 53 | + for img in images: |
| 54 | + newel = (img.id, img.name) |
| 55 | + if img.name.lower().startswith(f"{distro} {version}") and newel not in newlist: |
| 56 | + newlist.append(newel) |
| 57 | + # 3rd: Even more fuzzy match old SCS naming scheme ("Ubuntu*24.04") |
| 58 | + for img in images: |
| 59 | + newel = (img.id, img.name) |
| 60 | + if img.name.lower().startswith(f"{distro}") and img.name.lower().endswith(f"{version}") \ |
| 61 | + and newel not in newlist: |
| 62 | + newlist.append(newel) |
| 63 | + # 4th: Rest |
| 64 | + for img in images: |
| 65 | + newel = (img.id, img.name) |
| 66 | + if newel not in newlist: |
| 67 | + newlist.append(newel) |
| 68 | + return newlist |
| 69 | + |
| 70 | + |
| 71 | +def find_image(conn, distro, version, purpose="generic", strict=False, log=None): |
| 72 | + """Return a sorted list of ID,Name pairs that contain the wanted image. |
| 73 | + Empty list indicates no image has been found. The list is sorted such |
| 74 | + that (on SCS-compliant clouds), it will very likely contain the best |
| 75 | + matching, most recent image as first element. |
| 76 | + If strict is set, multiple matches are not allowed. |
| 77 | + """ |
| 78 | + ldistro = distro.lower() |
| 79 | + # FIXME: The image.images() method only passes selected filters |
| 80 | + purpose_out = purpose |
| 81 | + images = [x for x in conn.image.images(os_distro=ldistro, os_version=version, |
| 82 | + sort="name:desc,created_at:desc", visibility="public") |
| 83 | + if x.properties.get("os_purpose") == purpose] |
| 84 | + if len(images) == 0: |
| 85 | + warn(log, f"No image found with os_distro={ldistro} os_version={version} os_purpose={purpose}") |
| 86 | + purpose_out = "" |
| 87 | + # images = list(conn.image.images(os_distro=ldistro, os_version=version, |
| 88 | + # sort="name:desc,created_at:desc")) |
| 89 | + images = [x for x in conn.image.images(os_distro=ldistro, os_version=version, |
| 90 | + sort="name:desc,created_at:desc") |
| 91 | + if "os_purpose" not in x.properties] |
| 92 | + if len(images) == 0: |
| 93 | + warn(log, f"No image found with os_distro={ldistro} os_version={version} without os_purpose") |
| 94 | + return [] |
| 95 | + # Now comes sorting magic for best backwards compatibility |
| 96 | + if len(images) > 1: |
| 97 | + debug(log, f"Several {purpose_out} images found with os_distro={ldistro} os_version={version}") |
| 98 | + if strict: |
| 99 | + return [] |
| 100 | + return img_sort_heuristic(images, distro, version, purpose) |
| 101 | + return [(img.id, img.name) for img in images] |
| 102 | + |
| 103 | + |
| 104 | +def usage(): |
| 105 | + "Usage hints (CLI)" |
| 106 | + print("Usage: find-img.py [-s] DISTRO VERSION [PURPOSE]", file=sys.stderr) |
| 107 | + print("Returns all images matching, latest first, purpose defaulting to generic", file=sys.stderr) |
| 108 | + print("[-s] sets strict mode where only one match is allowed.", file=sys.stderr) |
| 109 | + print("You need to have OS_CLOUD set when running this", file=sys.stderr) |
| 110 | + sys.exit(1) |
| 111 | + |
| 112 | + |
| 113 | +def main(argv): |
| 114 | + "Main entry for CLI" |
| 115 | + if len(argv) < 3: |
| 116 | + usage() |
| 117 | + try: |
| 118 | + conn = openstack.connect(cloud=os.environ["OS_CLOUD"]) |
| 119 | + except openstack.exceptions.ConfigException: |
| 120 | + print(f"No valid entry for cloud {os.environ['OS_CLOUD']}", file=sys.stderr) |
| 121 | + usage() |
| 122 | + except KeyError: |
| 123 | + print("OS_CLOUD environment not configured", file=sys.stderr) |
| 124 | + usage() |
| 125 | + conn.authorize() |
| 126 | + purpose = "generic" |
| 127 | + strict = False |
| 128 | + if argv[1] == "-s": |
| 129 | + argv = argv[1:] |
| 130 | + strict = True |
| 131 | + if len(argv) > 3: |
| 132 | + purpose = argv[3] |
| 133 | + images = find_image(conn, argv[1], argv[2], purpose, strict) |
| 134 | + for img in images: |
| 135 | + print(f"{img[0]} {img[1]}") |
| 136 | + return len(images) == 0 |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == "__main__": |
| 140 | + sys.exit(main(sys.argv)) |
0 commit comments