-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainertool.sh
executable file
·242 lines (225 loc) · 5.19 KB
/
containertool.sh
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/bash
#
# container builder script
#
# Copyright (C) 2021 Raphael Bertoche
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
usage() {
if [ -z $DONT_PRINT_USAGE ]; then
echo "$0: --opensuse | --alpine | --all [ --build | --run ] [ --verbose ] [ --only-server ] [ --mount-home ]"
echo "[ --mount-ircd ] [ --name IMAGENAME ] [ --run-args RUNARGS ] [ --pid-file PIDFILEPATH ]"
echo "[[REPOSITORY] BRANCH]"
else
export DO_PRINT_USAGE=1
fi
}
set -e
options=$(getopt -o + -l "help,verbose,build,run,all,alpine,opensuse,only-server,mount-home,mount-ircd,name:,run-args:,pid-file:" -- "$@")
# With this you can set persistent values for variables such as RUN_ARGS and VOLUMES
if [ -f $HOME/.config/containertoolrc ]; then
. $HOME/.config/containertoolrc 2>/dev/null
fi
eval set -- "$options"
while true
do
case $1 in
--help)
usage
exit 0
;;
--verbose)
export verbose=1
set -xv
;;
--build)
BUILD=1;
;;
--run)
RUN=1;
;;
--all)
ALL=1
ALPINE=1;
OPENSUSE=1;
;;
--alpine)
ALPINE=1;
;;
--opensuse)
OPENSUSE=1;
;;
--only-server)
ONLY_SERVER=1;
;;
--mount-home)
MOUNT_HOME=1;
;;
--mount-ircd)
MOUNT_IRCD=1;
;;
--name)
shift
NAME="$1"
;;
--run-args)
shift
RUN_ARGS="$RUN_ARGS $1"
;;
--pid-file)
shift
PID_FILE="$1"
;;
--)
shift
ARGS="$@"
break;;
esac
shift
done
if [ -z "$ALPINE" ] && [ -z "$OPENSUSE" ]; then
echo "Please choose a distro passing --opensuse, --alpine or --all"
usage
exit 1
fi
if [ -n "$ALL" -a -n "$NAME" ]; then
echo "error: using --name and --all at the same time is not currently supported"
usage
exit 1
fi
if [ -n "$ALL" -a -n "$PID_FILE" ]; then
echo "error: using --pid-file and --all at the same time is not currently supported"
usage
exit 1
fi
if [ -n "$ALPINE" ]; then
if [ -z "$ONLY_SERVER" ]; then
IMAGES+=(alpine/tiny_ssh)
fi
IMAGES+=(alpine/build_server)
RUN_IMAGES+=(alpine/build_server)
fi
if [ -n "$OPENSUSE" ]; then
if [ -z "$ONLY_SERVER" ]; then
IMAGES+=(opensuse/tiny_ssh)
IMAGES+=(opensuse/dev_ssh)
IMAGES+=(opensuse/dev_ssh_x)
fi
IMAGES+=(opensuse/dev_server)
RUN_IMAGES+=(opensuse/dev_server)
fi
if [ -n "$MOUNT_HOME" ]; then
mkdir -p /var/home
VOLUMES="$VOLUMES -v /var/home:/home:z"
fi
if [ -n "$MOUNT_IRCD" ]; then
mkdir -p /var/storage/piss/pissircd
mkdir -p /var/storage/piss/unrealircd
chown -R 101000:101000 /var/storage/piss
VOLUMES="$VOLUMES \
-v /var/storage/piss/pissircd:/home/pissnet/pissircd:z \
-v /var/storage/piss/unrealircd:/home/pissnet/unrealircd:z \
-v /var/storage/piss/conf:/home/pissnet/unrealircd/conf:z \
-v /var/storage/piss/data:/home/pissnet/unrealircd/data:z \
-v /var/storage/piss/logs:/home/pissnet/unrealircd/logs:z"
fi
if [ -z "$RUN" ] && [ -z "$BUILD" ]; then
RUN=1;
BUILD=1;
fi
if [ "$#" -ge 2 ]; then
export REPO="$1"
shift
else
export REPO=pissnet/pissircd
fi
if [ "$#" -ge 1 ]; then
export BRANCH="$1"
shift
else
export BRANCH=piss60
fi
REPO_=`echo $REPO | tr \/ _`
if [ -n "$BUILD" ]; then
echo REPO:$REPO\; BRANCH:$BRANCH
# paths used through ADD Containerfile commands
mkdir -p unrealircd
mkdir -p data
mkdir -p conf
for i in "${IMAGES[@]}"; do
i_=`echo $i | tr \/ _`
f="Containerfile_${i_}"
tag="p/$i:${REPO_}_${BRANCH}"
echo "Building $tag..."
if [ "$i" = "opensuse/dev_server" ]; then
echo podman build -f "$f" \
-t "$tag" \
$VOLUMES \
--build-arg BRANCH="$BRANCH"
podman build -f "$f" \
-t "$tag" \
$VOLUMES \
--build-arg BRANCH="$BRANCH"
elif [ "$i" = "alpine/build_server" ]; then
echo podman build -f "$f" \
--build-arg BRANCH="$BRANCH" \
$VOLUMES \
-t "$tag"
podman build -f "$f" \
--build-arg BRANCH="$BRANCH" \
$VOLUMES \
-t "$tag"
else
echo podman build -f "$f" \
-t "$tag"
podman build -f "$f" \
-t "$tag"
fi
done;
fi
if [ -n "$RUN" ]; then
for i in "${RUN_IMAGES[@]}"; do
i_=`echo $i | tr \/ _`
tag="p/$i:${REPO_}_${BRANCH}"
# prevents NAME from being used two times
if [ "${#RUN_IMAGES[@]}" -ne 1 -o \
-z "$NAME" ]; then
NAME="${i_}_${REPO_}_${BRANCH}"
fi
echo "Running $NAME..."
echo podman run -dt --name="$NAME" \
--network podman1 \
$VOLUMES \
$RUN_ARGS \
"$tag"
podman run -dt --name="$NAME" \
--network podman1 \
$VOLUMES \
$RUN_ARGS \
"$tag"
echo "podman run exits with success."
sleep 1
if podman ps | grep -q "$NAME"; then
echo "$NAME is running."
else
echo "$NAME is not running anymore or failed to start."
fi
done
# only makes sense for a single image for now
if [ "${#RUN_IMAGES[@]}" -eq 1 -a \
-n "$PID_FILE" ]; then
sed -ie "s|^.*PIDFile.*|PIDFile=`podman generate systemd $NAME | grep PIDFile`|" $PID_FILE
systemctl daemon-reload
fi
fi