diff --git a/osism/commands/manage.py b/osism/commands/manage.py index e153334f..1484565e 100644 --- a/osism/commands/manage.py +++ b/osism/commands/manage.py @@ -1,8 +1,129 @@ # SPDX-License-Identifier: Apache-2.0 +import os +from re import findall import subprocess +from urllib.parse import urljoin from cliff.command import Command +import docker +from jinja2 import Template +from loguru import logger +import requests + +from osism.data import TEMPLATE_IMAGE_CLUSTERAPI, TEMPLATE_IMAGE_OCTAVIA + + +class ImageClusterapi(Command): + def get_parser(self, prog_name): + parser = super(ImageClusterapi, self).get_parser(prog_name) + + parser.add_argument( + "--cloud", type=str, help="Cloud name in clouds.yaml", default="openstack" + ) + parser.add_argument( + "--base-url", + type=str, + help="Base URL", + default="https://swift.services.a.regiocloud.tech/swift/v1/AUTH_b182637428444b9aa302bb8d5a5a418c/openstack-k8s-capi-images/", + ) + return parser + + def take_action(self, parsed_args): + cloud = parsed_args.cloud + base_url = parsed_args.base_url + + os.makedirs("/tmp/clusterapi", exist_ok=True) + for kubernetes_release in ["1.27", "1.28", "1.29"]: + url = urljoin(base_url, f"last-{kubernetes_release}") + + response = requests.get(url) + splitted = response.text.strip().split(" ") + + logger.info(f"date: {splitted[0]}") + logger.info(f"image: {splitted[1]}") + + r = findall(r".*ubuntu-2204-kube-v(.*\..*\..*).qcow2", splitted[1]) + logger.info(f"version: {r[0].strip()}") + + url = urljoin(base_url, splitted[1]) + logger.info(f"url: {url}") + + logger.info(f"checksum_url: {url}.CHECKSUM") + response_checksum = requests.get(f"{url}.CHECKSUM") + splitted_checksum = response_checksum.text.strip().split(" ") + logger.info(f"checksum: {splitted_checksum[0]}") + + template = Template(TEMPLATE_IMAGE_CLUSTERAPI) + result = template.render( + image_url=url, + image_checksum=f"sha256:{splitted_checksum[0]}", + image_version=r[0].strip(), + image_builddate=splitted[0], + ) + with open(f"/tmp/clusterapi/k8s-{kubernetes_release}.yml", "w+") as fp: + fp.write(result) + + subprocess.call( + "/usr/local/bin/openstack-image-manager --images=/tmp/clusterapi --cloud admin --filter 'Kubernetes CAPI'", + shell=True, + ) + + +class ImageOctavia(Command): + def get_parser(self, prog_name): + parser = super(ImageOctavia, self).get_parser(prog_name) + + parser.add_argument( + "--cloud", type=str, help="Cloud name in clouds.yaml", default="openstack" + ) + parser.add_argument( + "--base-url", + type=str, + help="Base URL", + default="https://swift.services.a.regiocloud.tech/swift/v1/AUTH_b182637428444b9aa302bb8d5a5a418c/openstack-octavia-amphora-image/", + ) + return parser + + def take_action(self, parsed_args): + cloud = parsed_args.cloud + base_url = parsed_args.base_url + + client = docker.from_env() + container = client.containers.get("kolla-ansible") + openstack_release = container.labels["de.osism.release.openstack"] + url = urljoin(base_url, f"last-{openstack_release}") + + response = requests.get(url) + splitted = response.text.strip().split(" ") + + logger.info(f"date: {splitted[0]}") + logger.info(f"image: {splitted[1]}") + + url = urljoin(base_url, splitted[1]) + logger.info(f"url: {url}") + + logger.info(f"checksum_url: {url}.CHECKSUM") + response_checksum = requests.get(f"{url}.CHECKSUM") + splitted_checksum = response_checksum.text.strip().split(" ") + logger.info(f"checksum: {splitted_checksum[0]}") + + template = Template(TEMPLATE_IMAGE_OCTAVIA) + result = template.render( + image_url=url, + image_checksum=f"sha256:{splitted_checksum[0]}", + image_version=splitted[0], + image_builddate=splitted[0], + ) + + os.makedirs("/tmp/octavia", exist_ok=True) + with open("/tmp/octavia/octavia.yml", "w+") as fp: + fp.write(result) + + subprocess.call( + "/usr/local/bin/openstack-image-manager --images=/tmp/octavia --cloud octavia --deactivate", + shell=True, + ) class Images(Command): diff --git a/osism/data/__init__.py b/osism/data/__init__.py new file mode 100644 index 00000000..2bb4de47 --- /dev/null +++ b/osism/data/__init__.py @@ -0,0 +1,62 @@ +TEMPLATE_IMAGE_OCTAVIA = """--- +images: + - name: OpenStack Octavia Amphora + enable: true + shortname: amphora + format: qcow2 + login: ubuntu + min_disk: 2 + min_ram: 512 + status: active + visibility: private + multi: false + meta: + architecture: x86_64 + hw_disk_bus: scsi + hw_rng_model: virtio + hw_scsi_model: virtio-scsi + hw_watchdog_action: reset + os_distro: ubuntu + replace_frequency: quarterly + uuid_validity: last-1 + provided_until: none + tags: + - amphora + versions: + - version: "{{ image_version }}" + url: "{{ image_url }}" + checksum: "{{ image_checksum }}" + build_date: {{ image_builddate }} + +""" + +TEMPLATE_IMAGE_CLUSTERAPI = """--- +images: + - name: Kubernetes CAPI + enable: true + keep: true + format: qcow2 + login: ubuntu + min_disk: 20 + min_ram: 512 + status: active + visibility: public + multi: false + meta: + architecture: x86_64 + hw_disk_bus: virtio + hw_rng_model: virtio + hw_scsi_model: virtio-scsi + hw_watchdog_action: reset + os_distro: ubuntu + replace_frequency: never + uuid_validity: none + provided_until: none + tags: [] + versions: + - version: "{{ image_version }}" + url: "{{ image_url }}" + checksum: "{{ image_checksum }}" + build_date: {{ image_builddate }} + +""" diff --git a/setup.cfg b/setup.cfg index 1e833059..b57ceeb4 100644 --- a/setup.cfg +++ b/setup.cfg @@ -64,6 +64,8 @@ osism.commands: log file = osism.commands.log:File log opensearch = osism.commands.log:Opensearch manage flavors = osism.commands.manage:Flavors + manage image clusterapi = osism.commands.manage:ImageClusterapi + manage image octavia = osism.commands.manage:ImageOctavia manage images = osism.commands.manage:Images netbox = osism.commands.netbox:Run netbox check = osism.commands.netbox:Check