-
Notifications
You must be signed in to change notification settings - Fork 1
/
condaToContainer
executable file
·67 lines (49 loc) · 1.45 KB
/
condaToContainer
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
#!/bin/bash
USAGE="""
Usage: condaToContainer <CONDA_ENV> <docker|singularity> <OUTPUT>
"""
read -r -d '' DOCKERFILE <<- EOM
FROM continuumio/miniconda3:latest
ADD packed_environment.tar.gz /opt/conda/
RUN conda-unpack
EOM
read -r -d '' SINGULARITY_FILE <<- EOM
Bootstrap: docker
From: continuumio/miniconda3
%files
packed_environment.tar.gz /packed_environment.tar.gz
%environment
export NUMBA_CACHE_DIR=/tmp/numba_cache
%post
tar xvzf /packed_environment.tar.gz -C /opt/conda
conda-unpack
rm /packed_environment.tar.gz
EOM
[[ "$#" -ne 3 ]] && echo "fatal: missing argument(s). " && echo $USAGE && exit 1
[[ ! "$2" =~ doc|sing ]] && echo "fatal: must specify docker or singularity build." && exit 1
CONDAENV=$1
CONTAINER=$(readlink -f $3)
TMPDIR=$(mktemp -d)
DIR=${TMPDIR}/$(echo $CONDAENV | md5sum | awk '{print $1}')
mkdir -p $DIR
echo ENV=$CONDAENV
echo CONTAINER=$CONTAINER
echo DIR=$DIR
echo "Packing conda environment.." > /dev/stderr
conda-pack -n $CONDAENV -o $DIR/packed_environment.tar.gz --ignore-missing-files --force
[[ $? -ne 0 ]] && exit 1
case "$2" in
sing*)
echo "Building singularity container.." > /dev/stderr
echo "$SINGULARITY_FILE" > $DIR/Singularity && \
cd $DIR && \
singularity build --fakeroot --force $CONTAINER Singularity
;;
doc*)
echo "Building docker container.." > /dev/stderr
echo "$DOCKERFILE" | docker build - --tag "$1:docker" --rm -o $CONTAINER
;;
*)
echo $USAGE
;;
esac