-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathminichris.sh
executable file
·233 lines (193 loc) · 5.93 KB
/
minichris.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
#!/bin/bash
# Over-engineered script to run ChRIS using Podman.
#
# -----------------------------------------------------------------------------
#
# TERMINAL OUTPUT COLOR HELPERS
#
# -----------------------------------------------------------------------------
# override tput so that it doesn't run in environments which does not support color
__REAL_TPUT=$(which tput)
function tput () {
if [ -z "$CI" ] && [[ "$TERM" = xterm* ]]; then
$__REAL_TPUT "$@"
fi
}
# define output colors
BOLD="$(tput bold)"
DIM="$(tput dim)"
UNDERLINE="$(tput smul)"
RESET="$(tput sgr0)"
RED="$(tput setaf 1)"
YELLOW="$(tput setaf 3)"
# Apply styles to text
function style_text () {
printf "$1%s$RESET" "$2"
}
# -----------------------------------------------------------------------------
#
# HELPER FUNCTIONS
#
# -----------------------------------------------------------------------------
function minichris_chrisomatic () {
if [ -z "$CI" ]; then
tty_flags=-it
fi
noisy_sh podman run --rm $tty_flags --restart=no \
--pod $CUBE_POD \
--security-opt label=disable \
-v "\"$(realpath "$HERE")/chrisomatic.yml:/chrisomatic.yml:ro\"" \
-v "$SOCK:/var/run/docker.sock:rw" \
ghcr.io/fnndsc/chrisomatic:1.0.0 chrisomatic "$@"
}
function minichris_down () {
noisy_sh "cat $HERE/podman/kube/*.yml $HERE/podman/pfcon-podman.yml | exec podman kube down --force -"
}
function minichris_up () {
start_cube
start_pfcon
minichris_chrisomatic
}
function minichris_cat () {
cat $HERE/podman/kube/*.yml
sed "s#$HARD_CODED_SOCK#$SOCK#" $HERE/podman/pfcon-podman.yml
}
function minichris_watch_migration () {
if is_running $SERVER_CONTAINER; then
echo "$SERVER_CONTAINER is already running"
return
fi
watch_for_logs $INIT_CONTAINER
}
function start_cube () {
watch_for_logs $INIT_CONTAINER &
noisy_sh "cat $HERE/podman/kube/*.yml | podman kube play --replace -"
reap_background_wait
}
function start_pfcon () {
noisy_sh "sed \"s#$HARD_CODED_SOCK#$SOCK#\" $HERE/podman/pfcon-podman.yml | podman kube play --replace -"
}
# Follow the logs of a container which has not yet been created and/or started.
# Returns the lock file for the waiting process, refer to run_in_background
function watch_for_logs () {
wait_until_running $1
noisy_sh podman logs --follow $1
}
# Poll Podman until a container is running.
function wait_until_running () {
while sleep 0.5; do
if is_running $1; then
break
fi
done
}
# Check whether a container is running
function is_running () {
[ -n "$(podman ps -f name=$1 -f status=running --quiet)" ]
}
# Check whether there is a background job running.
function is_bg_running () {
# we need to check twice:
# if bg finished, `jobs %%` will indicate "done" the first time, then error on the next.
# if bg running, `jobs %%` will indicate "running" twice.
# if no bg job, `jobs %%` will error on the first call.
jobs %% > /dev/null 2>&1 && jobs %% > /dev/null 2>&1
}
# If we're waiting in the background for the initContainer logs, stop and print a warning.
function reap_background_wait () {
if is_bg_running; then
style_text "$YELLOW" "warning"
echo ": missed poll for initContainer '$INIT_CONTAINER'"
kill %%
return 1
elif [ "$rc" = 1 ]; then
die "poll for initContainer exited with code $rc"
fi
}
# Print the arguments and run them using sh.
# Exits if the command fails.
function noisy_sh () {
style_text "$DIM" "\$ $*"
echo
set -e
sh -ec "$*"
set +e
}
# Exit the program with an error message
function die () {
style_text "$RED" "error: "
echo "$@"
exit 1
}
# -----------------------------------------------------------------------------
#
# CONSTANTS
#
# -----------------------------------------------------------------------------
CUBE_POD=minichris-cube-pod
INIT_CONTAINER=minichris-cube-pod-migratedb
SERVER_CONTAINER=minichris-cube-pod-server
# A hard-coded value in ./pfcon-podman.yml which needs to be changed
HARD_CODED_SOCK=/run/user/1000/podman/podman.sock
HERE="${0%/*}"
SOCK="$(podman info --format '{{ .Host.RemoteSocket.Path }}')"
USAGE="
Run ChRIS on Podman.
$(style_text "$BOLD$UNDERLINE" "Usage:") $0 [up|down|chrisomatic]
$(style_text "$BOLD$UNDERLINE" "Commands:")
$(style_text "$BOLD" "up") start or update ChRIS
$(style_text "$BOLD" "down") stop ChRIS and delete data
$(style_text "$BOLD" "cat") generate k8s YAML for ChRIS
$(style_text "$BOLD" "watch-migration") poll for output of the initContainer
$(style_text "$BOLD" "chrisomatic") sync plugins from $HERE/chrisomatic.yml file to CUBE
"
# -----------------------------------------------------------------------------
#
# PRE-FLIGHT CHECKS
#
# -----------------------------------------------------------------------------
# the helper function noisy_sh will misbehave if $HERE contains shell-sensitive syntax
if [[ "$HERE" = *" "* ]]; then
die "Directory '$HERE' contains space characters, please rename it!"
fi
if [ "$(podman info --format '{{ .Host.RemoteSocket.Exists }}')" != 'true' ]; then
echo "$(tput setaf 1)error$(tput sgr0): podman socket must be active for pman."
echo "try running:"
echo
echo " systemctl --user start podman.service"
echo
exit 1
fi
# -----------------------------------------------------------------------------
#
# RUN COMMAND
#
# -----------------------------------------------------------------------------
# show help and exit
for arg in "$@"; do
if [[ "$arg" =~ ^-*h|help$ ]]; then
echo "$USAGE"
exit 0
fi
done
subcommand="${1:-up}"
case "$subcommand" in
u|up )
minichris_up
;;
d|down )
minichris_down
;;
cat )
minichris_cat
;;
watch-migration )
minichris_watch_migration
;;
c|chrisomatic )
minichris_chrisomatic
;;
*)
die "unrecognized command $(style_text "$BOLD" "$subcommand")"
;;
esac