Skip to content

Commit 0ca4833

Browse files
committed
RF: to support adding native singularity images from the hub, like ReproNim/reproin
1 parent dcdac83 commit 0ca4833

File tree

1 file changed

+83
-23
lines changed

1 file changed

+83
-23
lines changed

scripts/create_singularities

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#
3030
# TODOs when get to refactor this prototype
3131
# - add options to
32-
# - skip getting an image (datalad containers-add call)
32+
# - skip getting an image (datalad containers-add call) (--dry-run?)
3333

3434
set -eu
3535

@@ -66,7 +66,7 @@ function get_last_docker_version_tag() {
6666
| sed -n -r 's/^([Vv]?(([0-9]{1,10}\..*))|((master)$))/\2\4@\1/gp')
6767
last_version=$(echo -e "$good_versions" |sort -Vr |head -n 1)
6868
elif [ -z "$versions" ]; then
69-
info $dh no version. Tags: $versions
69+
info " $dh no version. Tags: $versions"
7070
return
7171
else
7272
last_version="$versions"
@@ -76,25 +76,35 @@ function get_last_docker_version_tag() {
7676

7777

7878
function get_familyname() {
79-
dh="$1"
79+
repoid="$1"
8080
family="$2"
81-
name=${dh#*/}
81+
82+
name=${repoid#*/}
8283
# sanitize for datalad not allowing _ in the container names
8384
name=${name//_/-}
8485
echo "$family-$name"
8586
}
8687

88+
function get_imagename() {
89+
repoid="$1"
90+
family="$2"
91+
versiontag="$3"
92+
93+
familyname=$(get_familyname "$repoid" "$family")
94+
echo "${familyname}--${versiontag}"
95+
}
96+
8797
function create_singularity_file() {
8898
dh="$1"
8999
version_tag="$2"
90100
version_pure="$3"
91101
family="$4"
92-
familyname=$(get_familyname "$dh" "$family")
93-
image_name="${familyname}--${version_pure}"
102+
103+
imagename=$(get_imagename "$dh" "$family" "$version_pure")
94104
outdir="$IMAGES_DIR/$family"
95105
mkdir -p "$outdir"
96-
singfile="$outdir/Singularity.$image_name"
97-
info "$singfile \t from \t $dh:$version_tag"
106+
singfile="$outdir/Singularity.$imagename"
107+
# info "$singfile \t from \t $dh:$version_tag"
98108
cat >| "$singfile" <<EOF
99109
#
100110
# Automagically prepared for ReproNim/containers distribution.
@@ -118,21 +128,36 @@ EOF
118128
echo "$singfile"
119129
}
120130

121-
function get_image_for_singularity_file() {
122-
singfile="$1"
123-
singtag="$(basename ${singfile//Singularity./})" # Tag on singularity-hub
124-
imagefile="$(dirname $singfile)/$singtag.sing"
125-
imagefilename=$(basename $imagefile)
126-
name=${imagefilename%%--*}
131+
function add_singularity_versioned_image() {
132+
repoid="$1"
133+
family="$2"
134+
version_pure="$3"
135+
# If image repo id is different (e.g. ReproNim/containers) from original
136+
# repoid where we are getting image for (e.g. generating Singularity containers
137+
# from Docker images) -- specify it as 4th arg
138+
if [ "$#" -gt 3 ]; then
139+
origrepoid="$4"
140+
else
141+
origrepoid="$repoid"
142+
fi
143+
144+
imagedir="$IMAGES_DIR/$family"
145+
146+
familyname=$(get_familyname "$origrepoid" "$family")
147+
imagename=$(get_imagename "$origrepoid" "$family" "$version_pure")
148+
149+
imagefilename="$imagename.sing"
150+
imagefile="$imagedir/$imagefilename"
151+
# name=${imagefilename%%--*}
127152
if [ ! -e "$imagefile" ] && [ ! -L "$imagefile" ]; then
128153
info " geting image $imagefile"
129154
# use datalad to add/change a record
130155
# {img_dspath} handling needs https://github.com/datalad/datalad-container/issues/72 TODO WiP
131-
datalad containers-add "$name" \
156+
datalad containers-add "$familyname" \
132157
-i "$imagefile" \
133158
--update \
134159
--call-fmt '{img_dspath}/scripts/singularity_cmd run {img} {cmd}' \
135-
-u shub://ReproNim/containers:$singtag \
160+
-u shub://$repoid:$version_pure \
136161
|| error "No image $imagefilename was found yet"
137162
# TODO: later make it work with updating existing one. We will not be able to use
138163
# containers-add --update since original URL is version specific. That is why it
@@ -145,24 +170,59 @@ function get_image_for_singularity_file() {
145170
}
146171

147172
function generate_singularity_for_docker_image() {
148-
dh="$1"
173+
dockerhubid="$1"
149174
family="$2"
150-
last_version=$(get_last_docker_version_tag "$dh")
175+
176+
info "$family <- docker $dockerhubid"
177+
last_version=$(get_last_docker_version_tag "$dockerhubid")
178+
151179
if [ ! -z "$last_version" ]; then
152180
last_version_pure=${last_version%%@*}
153181
last_version_tag=${last_version#*@}
154-
# echo $dh $last_version
155-
singfile=$(create_singularity_file "$dh" "$last_version_tag" "$last_version_pure" "$family")
156-
get_image_for_singularity_file "$singfile"
182+
183+
# echo $dockerhubid $last_version
184+
singfile=$(create_singularity_file "$dockerhubid" "$last_version_tag" "$last_version_pure" "$family")
185+
# version_tag="$(basename ${singfile//Singularity./})" # Tag on singularity-hub
186+
187+
add_singularity_versioned_image "ReproNim/containers" "$family" "$last_version_pure" "$dockerhubid"
188+
fi
189+
}
190+
191+
function add_singularity_image_from_collection() {
192+
collection="$1"
193+
githubid="$2"
194+
family="$3"
195+
196+
# familyname=$(get_familyname "$githubid" "$family")
197+
198+
info "$family <- singularity $githubid"
199+
200+
# TODO: For now just using the version tag itself since did not encounter yet
201+
# the custom ones like in case of dockers, but we might need to do the same here
202+
# to get "version_pure"
203+
version_tag="$(
204+
curl -s "https://www.singularity-hub.org/collections/$collection" \
205+
| grep "$githubid:[0-9]" \
206+
| sed -e 's,^ *,,g' \
207+
| sed -e 's,^[^ ]*:,,g' \
208+
| sort -V -r \
209+
| head -n 1)"
210+
if [ -z "$version_tag" ]; then
211+
info " no 'proper' version was found - skipping" # for the $githubid"
212+
return
157213
fi
214+
add_singularity_versioned_image "$githubid" "$family" "$version_tag"
158215
}
159216

160217
curl -s -k https://raw.githubusercontent.com/BIDS-Apps/bids-apps.github.io/master/_config.yml \
161218
| awk '/^ *dh:/{print $2}' \
162219
| sed -e "s,',,g" \
163-
| while read dh; do
164-
generate_singularity_for_docker_image "$dh" "bids"
220+
| while read dockerhubid; do
221+
generate_singularity_for_docker_image "$dockerhubid" "bids"
165222
done
166223

167224
# validator is not bids-app but we will stick it along with bids
168225
generate_singularity_for_docker_image "bids/validator" "bids"
226+
227+
# "Native" Singularity images
228+
add_singularity_image_from_collection 588 "ReproNim/reproin" "repronim"

0 commit comments

Comments
 (0)