-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontainerize.bash
executable file
·134 lines (102 loc) · 3.28 KB
/
containerize.bash
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
#!/bin/bash
# Use this script to generate consistent singularity / docker definition files.
# Collect in this temporary variable.
DEPS=""
deps() {
DEPS="${DEPS} $@"
}
# "Build" dependencies are removed after compilation.
BUILD_DEPS=""
bdeps() {
BUILD_DEPS="${BUILD_DEPS} $@"
}
#==== Dependencies =============================================================
# Build-time only dependencies.
bdeps git # To get source code.
bdeps cmake make python # Build tools.
bdeps gcc gcc-fortran # Compilers
bdeps boost # Template lib.
bdeps pacman-contrib # For paccache.
# Runtime dependencies.
deps openblas lapack gsl
#=== Construction layers =======================================================
read -r -d '' DEPENDENCIES_LAYER <<-EOF
#---- Dependencies ---------------------------------------------------------
echo 'Server = https://mirrors.kernel.org/archlinux/\$repo/os/\$arch' \\
> /etc/pacman.d/mirrorlist
pacman -Syu --noconfirm
pacman -Sy --noconfirm ${BUILD_DEPS} ${DEPS}
#---------------------------------------------------------------------------
EOF
read -r -d '' COMPILATION_LAYER <<-'EOF'
#---- Compilation ----------------------------------------------------------
# Get source code.
cd /opt
git clone --recursive https://github.com/champost/DECX
cd DECX
# Compile
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j $(nproc)
# Test
../tests/input/main.bash
#---------------------------------------------------------------------------
EOF
read -r -d '' INSTALLATION_LAYER <<-EOF
#---- Installation ---------------------------------------------------------
ln -s /opt/DECX/build/decx /usr/bin/decx
#---------------------------------------------------------------------------
EOF
read -r -d '' CLEANUP_LAYER <<-EOF
#---- Cleanup --------------------------------------------------------------
# Remove the packages downloaded to image's Pacman cache dir.
paccache -r -k0
# Uninstall build dependencies.
pacman -Rns --noconfirm ${BUILD_DEPS}
#---------------------------------------------------------------------------
EOF
#=== Generate Singularity file =================================================
FILENAME="singularity.def"
cat <<EOF > $FILENAME
BootStrap: docker
From: archlinux
# This file was automatically generated from ./containerize.bash.
%post
echo "Building container.."
${DEPENDENCIES_LAYER}
${COMPILATION_LAYER}
${INSTALLATION_LAYER}
${CLEANUP_LAYER}
echo "export CONTAINER_BUILD_TIME=\\"\$(date)\\"" \\
>> \${SINGULARITY_ENVIRONMENT}
%runscript
echo "Running DECX container (created on \${CONTAINER_BUILD_TIME})"
decx \$@
EOF
echo "Generated $FILENAME"
#=== Generate Docker file ======================================================
FILENAME="Dockerfile"
cat <<DEOF > $FILENAME
# syntax=docker/dockerfile:1.3-labs
FROM archlinux
# This file was automatically generated from ./containerize.bash.
RUN <<EOF
${DEPENDENCIES_LAYER}
EOF
RUN <<EOF
${COMPILATION_LAYER}
EOF
RUN <<EOF
${INSTALLATION_LAYER}
EOF
RUN <<EOF
${CLEANUP_LAYER}
EOF
# Now pick a folder to work within.
RUN mkdir -p /home/decx
WORKDIR /home/decx
ENTRYPOINT ["decx"]
DEOF
echo "Generated $FILENAME"
exit 0