Skip to content

Commit 697d2af

Browse files
authored
Merge pull request #33 from CSCfi/auto_base_os
Features
2 parents d013b5e + 3e0febc commit 697d2af

File tree

8 files changed

+94
-9
lines changed

8 files changed

+94
-9
lines changed

configs/local.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defaults:
99
installation_path: "/LOCAL_TYKKY"
1010
# if this is not a thing which exist
1111
# I will do a singularity pull
12-
container_src: 'docker://rockylinux:8.9'
12+
container_src: auto
1313
# name of the container image when on disk
1414
container_image: container.sif
1515
sqfs_image: img.sqfs

configs/lumi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defaults:
1212
composable: true
1313
# if this is not a thing which exist
1414
# I will do a singularity pull
15-
container_src: 'docker://opensuse/leap:15.5'
15+
container_src: auto
1616
# name of the container image when on disk
1717
container_image: container.sif
1818
sqfs_image: img.sqfs

configs/mahti.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defaults:
1212
composable: true
1313
# if this is not a thing which exist
1414
# I will do a singularity pull
15-
container_src: 'docker://redhat/ubi8:8.10'
15+
container_src: auto
1616
# name of the container image when on disk
1717
container_image: container.sif
1818
sqfs_image: img.sqfs

configs/puhti.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defaults:
1111
composable: true
1212
# if this is not a thing which exist
1313
# I will do a singularity pull
14-
container_src: 'docker://redhat/ubi8:8.6'
14+
container_src: auto
1515
# name of the container image when on disk
1616
container_image: container.sif
1717
sqfs_image: img.sqfs

construct.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@
5757
if os.getenv("CW_LOG_LEVEL"):
5858
full_conf["log_level"]=os.getenv("CW_LOG_LEVEL")
5959

60+
if full_conf["container_src"] == "auto":
61+
success,container_source = get_docker_image("/etc/os-release")
62+
if not success:
63+
print_err("Unable to automatically determine base container to use: "+container_source,True)
64+
sys.exit(1)
65+
else:
66+
print_info("Automatically determined base container to: "+container_source,full_conf["log_level"],2,True)
67+
full_conf["container_src"] = f"docker://{container_source}"
68+
6069
if os.getenv("CW_BUILD_TMPDIR"):
6170
full_conf["build_tmpdir_base"]=os.getenv("CW_BUILD_TMPDIR")
6271
tmpdir_base=full_conf["build_tmpdir_base"]

cw_common.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
colors["GREEN"]='\033[0;32m'
1313
colors["YELLOW"]='\033[1;33m'
1414
colors["BLUE"]="\033[1;34m"
15+
colors["PURPLE"]='\033[0;35m'
1516
colors["NC"]='\033[0m' # No Color
1617

1718
def print_err(txt,err=False):
@@ -27,6 +28,28 @@ def print_err(txt,err=False):
2728
print("[ ERROR ] "+txt)
2829
else:
2930
print("["+colors["RED"]+" ERROR "+colors["NC"]+"] "+txt)
31+
32+
def print_info(txt,log_level,msg_level,err=False):
33+
"""Pretty info message, color is disabled if not in a TTY"""
34+
if int(log_level) <= msg_level:
35+
return
36+
if msg_level >= 2:
37+
msg="DEBUG"
38+
color=colors["PURPLE"]
39+
else:
40+
msg="INFO"
41+
color=colors["BLUE"]
42+
if(err):
43+
if not sys.stderr.isatty():
44+
print(f"[ {msg} ] "+txt,file=sys.stderr)
45+
else:
46+
print("["+color+f" {msg} "+colors["NC"]+"] "+txt,file=sys.stderr)
47+
else:
48+
if not sys.stdout.isatty():
49+
print(f"[ {msg} ] "+txt,file=sys.stderr)
50+
else:
51+
print("["+color+f" {msg} "+colors["NC"]+"] "+txt,file=sys.stdout)
52+
3053
def print_warn(txt,err=False):
3154
if(err):
3255
if not sys.stderr.isatty():
@@ -65,3 +88,49 @@ def installation_in_PATH():
6588
def is_installation(base_path):
6689
markers=["bin","_bin","common.sh"]
6790
return all( pathlib.Path(base_path+'/../'+m).exists() for m in markers )
91+
92+
# UBI images are namespaced with the major version as part of the name
93+
# and not just the tag.
94+
special={}
95+
special["rhel"]= lambda namespace,version: namespace+version.split('.')[0]
96+
97+
# Get the docker image matching the host OS
98+
def get_docker_image(release_file):
99+
os_release_file = release_file
100+
docker_images = {
101+
"sles": "opensuse/leap",
102+
"rhel": "redhat/ubi",
103+
"almalinux": "almalinux",
104+
"rocky": "rockylinux",
105+
"ubuntu": "ubuntu"
106+
}
107+
108+
try:
109+
with open(os_release_file, 'r') as file:
110+
lines = file.readlines()
111+
os_info = {}
112+
for line in lines:
113+
# Lazy way to handle empty lines
114+
try:
115+
key, value = line.strip().split('=', 1)
116+
except:
117+
continue
118+
os_info[key] = value.strip('"')
119+
120+
os_id = os_info.get("ID", "").lower()
121+
version_id = os_info.get("VERSION_ID", "").lower()
122+
123+
if os_id in docker_images:
124+
docker_image = docker_images[os_id]
125+
if os_id in special:
126+
docker_image = special[os_id](docker_image,version_id)
127+
return (True,f"{docker_image}:{version_id}")
128+
else:
129+
# Guess what the name could be
130+
# Will most likely fail for most small distros
131+
return (True,f"{os_id}:{version_id}")
132+
133+
except FileNotFoundError:
134+
return (False,"OS release file not found")
135+
except Exception as e:
136+
return (False,f"An error occurred: {e}")

frontends/containerize

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ clean_up () {
2323
else
2424
test -f "$_usr_yaml" && rm "$_usr_yaml"
2525
test -d "$CW_BUILD_TMPDIR" && rm -r "$CW_BUILD_TMPDIR"
26-
print_err "Set CW_DEBUG_KEEP_FILES env variable to keep build files"
26+
print_err "Set CW_DEBUG_KEEP_FILES env variable to keep build files or set CW_LOG_LEVEL to a higher value for more output, e.g. CW_LOG_LEVEL=3"
2727
fi
2828
trap 2
2929
kill -- -$$ &>/dev/null
@@ -90,8 +90,10 @@ else
9090
print_warn "Umask set to $CW_UMASK, check permissions of finished installation"
9191
fi
9292

93-
chgrp $(stat -c "%G" $CW_INSTALLATION_PREFIX ) $CW_BUILD_TMPDIR
94-
chmod g+s $CW_BUILD_TMPDIR
93+
_install_prefix_group=$(stat -c "%G" $CW_INSTALLATION_PREFIX )
94+
chgrp $_install_prefix_group $CW_BUILD_TMPDIR || print_warn "Failed to match group ownership of installation directory, attempted group was $_install_prefix_group"
95+
chmod g+s $CW_BUILD_TMPDIR || print_warn "Failed to set group stickybit for build directory, installation permissions might be incorrect. Please check once the installation is done."
96+
print_info "Setting group ownership of build directory to $_install_prefix_group" 2
9597

9698
if [[ "$CW_INSTALLATION_PREFIX" = /* ]]; then
9799
export _inst_path=$CW_INSTALLATION_PREFIX

generate_wrappers.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,17 @@ if [[ \${_CW_IN_CONTAINER+defined} ]];then
216216
else" >> _deploy/bin/$target
217217
if [[ ${CONDA_CMD+defined} ]];then
218218
echo "
219-
if [[ ( -e \$(/usr/bin/dirname \$_O_SOURCE )/../pyvenv.cfg && ! \${CW_FORCE_CONDA_ACTIVATE+defined} ) || \${CW_NO_CONDA_ACTIVATE+defined} ]];then
219+
_venvd=\$(/usr/bin/dirname \$_O_SOURCE )
220+
test -e \$_venvd/../pyvenv.cfg
221+
_v_in_use=\$?
222+
if [[ ( \$_v_in_use -eq 0 && ! \${CW_FORCE_CONDA_ACTIVATE+defined} ) || \${CW_NO_CONDA_ACTIVATE+defined} ]];then
220223
export PATH=\"\$OLD_PATH\"
221224
$_RUN_CMD $_default_cws exec -a \$_O_SOURCE \$DIR/$target $_cwe
222225
else
223226
export PATH=\"\$OLD_PATH\"
224-
$_RUN_CMD $_cws exec -a \$_O_SOURCE \$DIR/$target $_cwe
227+
_venv_act=\":\"
228+
test 0 -eq \$_v_in_use && _venv_act=\"source \$_venvd/activate\"
229+
$_RUN_CMD $_cws \$_venv_act && exec -a \$_O_SOURCE \$DIR/$target $_cwe
225230
fi
226231
fi
227232
" >> _deploy/bin/$target

0 commit comments

Comments
 (0)