Skip to content

Commit

Permalink
Remove old munch workaround from mirror/update (#644)
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Berendt <[email protected]>
  • Loading branch information
berendt authored Aug 30, 2023
1 parent 818d279 commit 5c3efd1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
23 changes: 10 additions & 13 deletions openstack_image_manager/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from minio import Minio
from minio.error import S3Error
from munch import Munch
from os import listdir
from os.path import isfile, join
from urllib.parse import urlparse
Expand All @@ -37,9 +36,7 @@ def main(
),
minio_bucket: str = typer.Option("openstack-images", help="Minio bucket"),
):
CONF = Munch.fromDict(locals())

if CONF.debug:
if debug:
level = logging.DEBUG
logging.getLogger("paramiko").setLevel(logging.DEBUG)
else:
Expand All @@ -50,22 +47,22 @@ def main(
)

onlyfiles = []
for f in listdir(CONF.images):
if isfile(join(CONF.images, f)):
for f in listdir(images):
if isfile(join(images, f)):
onlyfiles.append(f)

all_images = []
for file in onlyfiles:
with open(join(CONF.images, file)) as fp:
with open(join(images, file)) as fp:
data = yaml.load(fp, Loader=yaml.SafeLoader)
imgs = data.get("images")
for image in imgs:
all_images.append(image)

client = Minio(
CONF.minio_server,
access_key=CONF.minio_access_key,
secret_key=CONF.minio_secret_key,
minio_server,
access_key=minio_access_key,
secret_key=minio_secret_key,
)

for image in all_images:
Expand Down Expand Up @@ -93,7 +90,7 @@ def main(
logging.debug("filename: %s" % filename)

try:
client.stat_object(CONF.minio_bucket, os.path.join(dirname, filename))
client.stat_object(minio_bucket, os.path.join(dirname, filename))
logging.info("'%s' available in '%s'" % (filename, dirname))
except S3Error:
logging.info("'%s' not yet available in '%s'" % (filename, dirname))
Expand All @@ -111,10 +108,10 @@ def main(
patoolib.extract_archive(os.path.basename(path.path), outdir=".")
os.remove(os.path.basename(path.path))

if not CONF.dry_run:
if not dry_run:
logging.info("Uploading '%s' to '%s'" % (filename, dirname))
client.fput_object(
CONF.minio_bucket, os.path.join(dirname, filename), filename
minio_bucket, os.path.join(dirname, filename), filename
)
else:
logging.info(
Expand Down
44 changes: 27 additions & 17 deletions openstack_image_manager/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from loguru import logger
from minio import Minio
from minio.error import S3Error
from munch import Munch
from natsort import natsorted
import patoolib
import requests
Expand All @@ -26,11 +25,13 @@
IMAGES = ["almalinux", "centos", "debian", "rockylinux", "ubuntu"]


def mirror_image(image, latest_url, CONF):
def mirror_image(
image, latest_url, minio_server, minio_bucket, minio_access_key, minio_secret_key
):
client = Minio(
CONF.minio_server,
access_key=CONF.minio_access_key,
secret_key=CONF.minio_secret_key,
minio_server,
access_key=minio_access_key,
secret_key=minio_secret_key,
)

version = image["versions"][0]
Expand All @@ -49,7 +50,7 @@ def mirror_image(image, latest_url, CONF):
new_filename = f"{new_version}-{shortname}.{format}"

try:
client.stat_object(CONF.minio_bucket, os.path.join(dirname, new_filename))
client.stat_object(minio_bucket, os.path.join(dirname, new_filename))
logger.info("'%s' available in '%s'" % (new_filename, dirname))
except S3Error:
logger.info("'%s' not yet available in '%s'" % (new_filename, dirname))
Expand All @@ -68,13 +69,11 @@ def mirror_image(image, latest_url, CONF):
"Uploading '%s' to '%s' as '%s'" % (filename, dirname, new_filename)
)

client.fput_object(
CONF.minio_bucket, os.path.join(dirname, new_filename), filename
)
client.fput_object(minio_bucket, os.path.join(dirname, new_filename), filename)
os.remove(filename)


def update_image(image, CONF):
def update_image(image, minio_server, minio_bucket, minio_access_key, minio_secret_key):
name = image["name"]
logger.info(f"Checking image {name}")

Expand Down Expand Up @@ -186,14 +185,21 @@ def update_image(image, CONF):
shortname = image["shortname"]
format = image["format"]

minio_server = str(CONF.minio_server)
minio_bucket = str(CONF.minio_bucket)
minio_server = str(minio_server)
minio_bucket = str(minio_bucket)
new_url = f"https://{minio_server}/{minio_bucket}/{shortname}/{new_version}-{shortname}.{format}"
logger.info(f"New URL is {new_url}")
image["versions"][0]["mirror_url"] = new_url
image["versions"][0]["url"] = latest_url

mirror_image(image, latest_url, CONF)
mirror_image(
image,
latest_url,
minio_server,
minio_bucket,
minio_access_key,
minio_secret_key,
)
del image["versions"][0]["source"]

else:
Expand All @@ -217,9 +223,7 @@ def main(
minio_bucket: str = typer.Option("openstack-images", help="Minio bucket"),
):

CONF = Munch.fromDict(locals())

if CONF.debug:
if debug:
level = "DEBUG"
else:
level = "INFO"
Expand All @@ -239,7 +243,13 @@ def main(

for index, image in enumerate(data["images"]):
if "latest_url" in image:
updated_image = update_image(image, CONF)
updated_image = update_image(
image,
minio_server,
minio_bucket,
minio_access_key,
minio_secret_key,
)
data["images"][index] = updated_image

with open(p, "w+") as fp:
Expand Down

0 comments on commit 5c3efd1

Please sign in to comment.