-
Notifications
You must be signed in to change notification settings - Fork 0
/
container2module
executable file
·179 lines (144 loc) · 3.77 KB
/
container2module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash
# Pull a container and install it as a module through EasyBuild
# Requirements:
# skopeo - inspect container label without pulling
# jq - parse json
#
# Ruoshi Sun
# 2021-03-23 central container dir
# 2020-07-04
if [[ $# -lt 4 || $# -gt 5 ]]; then
echo "Usage: `basename $0` URI_prefix user app tag [version]"
echo " URI_prefix = docker|library|shub|etc."
echo " user = username of repository (if empty, put \"\")"
echo " app = name of repository"
echo " tag = tag of repository"
echo " version = Rivanna module version (default: tag)"
echo
echo "Pull command:"
echo "apptainer pull app-version.sif URI_prefix://[user/]app:tag"
exit 1
fi
# parse input
URI_PREFIX=$1
USER=$2
APP=$3
TAG=$4
[ $# -eq 4 ] && VERSION=$TAG || VERSION=$5
APPTAINER_VERSION=3.7.1
SKOPEO_VERSION=1.13.1
module purge
# constants
HERE=`pwd`
# functions
function exit_if_error {
echo "+ $1"
eval $1
if [ $? -ne 0 ]; then
echo "Error: $2"
exit 1
fi
}
function print_stage {
echo -e "\033[1;34m==> $1\033[0m"
}
# URI
[ -z "$USER" ] && URI="${URI_PREFIX}://${APP}:${TAG}" || URI="${URI_PREFIX}://${USER}/${APP}:${TAG}"
# apptainer
SIF=${APP}-${VERSION}.sif
module load apptainer/$APPTAINER_VERSION easybuild
# check sources
print_stage "Checking sources"
cd $EASYBUILD_SOURCEPATH
SRCDIR=${APP::1}/${APP}
[ -d $SRCDIR ] || {
print_stage "Creating directory $EASYBUILD_SOURCEPATH/$SRCDIR"
mkdir -p $SRCDIR
}
cd $SRCDIR
if [ -e $SIF ]; then
read -p "$SIF already exists. Overwrite? [y/N]" yn
if [[ $yn =~ ^[Yy]$ ]]; then
rm $SIF
else
SKIP_PULL=true
fi
EB_FORCE=true # Module may already exist. Set "eb -f"
fi
# check if container exists and store labels
print_stage "Checking URI"
module load skopeo >/dev/null 2>&1
INSPECT=$($CONTAINERDIR/skopeo-${SKOPEO_VERSION}.sif inspect $URI)
if [ $? -ne 0 ]; then
echo "container $URI does not exist"
exit 1
fi
module unload skopeo
# pull container as sif
if [ "$SKIP_PULL" = true ]; then
print_stage "Pulling container [skipped]"
else
print_stage "Pulling container"
exit_if_error "apptainer pull $SIF $URI" "failed to pull container"
fi
module unload apptainer
# parse labels
print_stage "Parsing labels"
module load jq
declare -A LABEL
for i in homepage description moduleclass gpu; do
LABEL[$i]=$(echo "$INSPECT" | jq -r ".Labels.$i")
done
module unload jq
# write easyconfig
EB=${APP}-${VERSION}-apptainer-${APPTAINER_VERSION}.eb
print_stage "Writing easyconfig $HERE/$EB"
cd $HERE
DATE=$(date +%Y-%m-%d)
EB_HEADER="# $DATE generated by container2module script (Ruoshi Sun)"
[ "${LABEL[gpu]}" = true ] && USE_GPU="True" || USE_GPU="False"
cat >$EB <<EOF
$EB_HEADER
easyblock = 'EB_ApptainerImage'
name = '$APP'
version = '$VERSION'
homepage = '${LABEL[homepage]}'
description = """
${LABEL[description]}
"""
toolchain = {'name': 'apptainer', 'version': '$APPTAINER_VERSION'}
sources = ['%(name)s-%(version)s.sif']
use_gpu = $USE_GPU
skipsteps = ['sanitycheck']
EOF
if [ "$USE_GPU" = "True" ]; then
cat >>$EB <<EOF
modluafooter = 'add_property("arch","gpu")'
EOF
fi
cat >>$EB <<EOF
moduleclass = '${LABEL[moduleclass]}'
EOF
cat $EB
echo
echo "Run: eb $EB"
## CONTAINERDIR set to None in .lua. Don't know why.
#echo "Inspect the above easyconfig."
#read -rsn1 -p "Press any key to start EasyBuild installation"
#echo
#
## EasyBuild installation
#print_stage "EasyBuild"
#if [ "$EB_FORCE" = true ]; then
# exit_if_error "eb $EB -f" "EasyBuild installation failed"
#else
# exit_if_error "eb $EB" "EasyBuild installation failed"
#fi
#
#echo
#echo -n "Done! "
#if command -v xxd &> /dev/null; then
# echo "0000000: f09f 8e89 0a" | xxd -r
#else
# echo
#fi