toolkit/iso/empanadas/empanadas/common.py

121 lines
3.8 KiB
Python
Raw Normal View History

2022-05-20 08:13:57 +00:00
# All imports are here
import os
2022-05-20 08:13:57 +00:00
import platform
import time
import glob
import rpm
import yaml
import logging
import hashlib
2022-05-20 08:13:57 +00:00
from collections import defaultdict
from typing import Tuple
# An implementation from the Fabric python library
class AttributeDict(defaultdict):
def __init__(self):
super(AttributeDict, self).__init__(AttributeDict)
def __getattr__(self, key):
try:
return self[key]
except KeyError:
raise AttributeError(key)
def __setattr__(self, key, value):
self[key] = value
2022-05-20 08:13:57 +00:00
# These are a bunch of colors we may use in terminal output
class Color:
RED = '\033[91m'
GREEN = '\033[92m'
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
YELLOW = '\033[93m'
UNDERLINE = '\033[4m'
BOLD = '\033[1m'
END = '\033[0m'
# vars and additional checks
rldict = AttributeDict()
sigdict = AttributeDict()
config = {
"rlmacro": rpm.expandMacro('%rhel'),
2022-06-13 14:37:50 +00:00
"dist": 'el' + rpm.expandMacro('%rhel'),
"arch": platform.machine(),
2022-05-22 06:20:15 +00:00
"date_stamp": time.strftime("%Y%m%d.%H%M%S", time.localtime()),
"compose_root": "/mnt/compose",
"staging_root": "/mnt/repos-staging",
"production_root": "/mnt/repos-production",
2022-05-22 06:20:15 +00:00
"category_stub": "mirror/pub/rocky",
"sig_category_stub": "mirror/pub/sig",
"repo_base_url": "https://yumrepofs.build.resf.org/v1/projects",
2022-06-15 20:53:12 +00:00
"mock_work_root": "/builddir",
"container": "centos:stream9",
"distname": "Rocky Linux",
2022-06-19 14:29:01 +00:00
"shortname": "Rocky",
"translators": {
"x86_64": "amd64",
"aarch64": "arm64",
"ppc64le": "ppc64le",
"s390x": "s390x"
},
"aws_region": "us-east-2",
"bucket": "resf-empanadas",
"bucket_url": "https://resf-empanadas.s3.us-east-2.amazonaws.com"
}
2022-05-20 08:13:57 +00:00
# Importing the config from yaml
import importlib_resources
_rootdir = importlib_resources.files("empanadas")
for conf in glob.iglob(f"{_rootdir}/configs/*.yaml"):
2022-05-20 08:13:57 +00:00
with open(conf, 'r', encoding="utf-8") as file:
rldict.update(yaml.safe_load(file))
2022-06-10 23:05:44 +00:00
# Import all SIG configs from yaml
for conf in glob.iglob(f"{_rootdir}/sig/*.yaml"):
2022-06-10 23:05:44 +00:00
with open(conf, 'r', encoding="utf-8") as file:
sigdict.update(yaml.safe_load(file))
2022-05-20 08:13:57 +00:00
# The system needs to be a RHEL-like system. It cannot be Fedora or SuSE.
2022-05-22 06:20:15 +00:00
#if "%rhel" in config['rlmacro']:
2022-05-20 08:13:57 +00:00
# raise SystemExit(Color.BOLD + 'This is not a RHEL-like system.' + Color.END
# + '\n\nPlease verify you are running on a RHEL-like system that is '
# 'not Fedora nor SuSE. This means that the %rhel macro will be '
# 'defined with a value equal to the version you are targetting. RHEL'
# ' and its derivatives have this set.')
# These will be set in their respective var files
#REVISION = rlvars['revision'] + '-' + rlvars['rclvl']
2022-05-22 06:20:15 +00:00
#rlvars = rldict[rlver]
#rlvars = rldict[rlmacro]
#COMPOSE_ISO_WORKDIR = COMPOSE_ROOT + "work/" + arch + "/" + date_stamp
def valid_type_variant(_type: str, variant: str="") -> Tuple[bool, str]:
ALLOWED_TYPE_VARIANTS = {
"Container": ["Base", "Minimal"],
"GenericCloud": [],
}
if _type not in ALLOWED_TYPE_VARIANTS:
return False, f"Type is invalid: ({_type}, {variant})"
elif variant not in ALLOWED_TYPE_VARIANTS[_type]:
if variant.capitalize() in ALLOWED_TYPE_VARIANTS[_type]:
return False, f"Capitalization mismatch. Found: ({_type}, {variant}). Expected: ({_type}, {variant.capitalize()})"
return False, f"Type/Variant Combination is not allowed: ({_type}, {variant})"
return True, ""
class Architecture(str):
@staticmethod
def New(architecture: str, version: int):
if architecture not in rldict[version]["allowed_arches"]:
print("Invalid architecture/version combo, skipping")
exit()
return Architecture(architecture)