-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.sh
executable file
·260 lines (188 loc) · 7.13 KB
/
run.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/zsh -eu
MYDIR=${0:a:h}
# REQUIRED ENV VARS FROM CALLER:
# $INCOMING - name of the new version of file; the new file contents should be in this temp file
# $CLONE - the repo's git clone url
# $BRANCH - name of repo's current git branch
# $FILE - relative path name of file (relative to top dir of repo)
# NOTE: this defines $REGISTRY_FALLBACK and $DOMAIN_WILDCARD
source $MYDIR/.env
set -o allexport
# VM host needs: zsh git yq
TOP=/coderunr
# xxx filter trash chars out of CLONE and BRANCH
GROUP_REPO=$(echo "$CLONE" | perl -pe 's=\.git$==; s=/+$==' |tr : / |rev |cut -f1-2 -d/ |rev)
GROUP=$(echo "$GROUP_REPO" |cut -d/ -f1)
REPO=$(echo "$GROUP_REPO" |cut -d/ -f2)
EXTRA="-$BRANCH"
[ $BRANCH = main ] && EXTRA=
[ $BRANCH = master ] && EXTRA=
SLUG=${REPO}${EXTRA}
HOST=${SLUG}.$DOMAIN_WILDCARD # xxx optional username if collision or yml config to use them
CLONED_CACHE=$TOP/$GROUP/$REPO/__clone
DIR=$TOP/$GROUP/$REPO/$BRANCH
# C O D E R U N R
# 00011 01111 00100 00101 10010 10101 01110 10010
echo
echo '₀₀₀₁₀₁₀₀₀₁₀₁₀₀₀₁₀₁₀₀₀₁₀₁₀₀₀₁₀₁₀₀₀₁₀₁₀₀₀₁₀₁₀₀₀₁₀₁₀₀₀₁₀₁₀₀₀₁₀₁ ᕕ( ᐛ )ᕗ'
echo
echo BRANCH=$BRANCH
echo CLONE=$CLONE
echo CLONED_CACHE=$CLONED_CACHE
set +u
if [ $VERBOSE ]; then
set -x
fi
set -u
[ -e $TOP/$GROUP ] || mkdir -m777 $TOP/$GROUP
[ -e $TOP/$GROUP/$REPO ] || mkdir -m777 $TOP/$GROUP/$REPO
CLONE_NEEDED=
[ -e $CLONED_CACHE ] || CLONE_NEEDED=1
if [ $CLONE_NEEDED ]; then
mkdir -p $CLONED_CACHE
git clone $CLONE $CLONED_CACHE
fi
function git-pull() {
# If normal pull fails, make sure no local mods are blocking the pull.
# If git pull still fails, it could be a new branch on the editor's end which got pushed to origin
# since we started tracking it.
git pull || ( \
( git stash --include-untracked && git stash drop|cat ) 2>/dev/null >/dev/null && ( \
git pull || ( git branch --set-upstream-to=origin/$BRANCH $BRANCH && git pull ) \
)
)
}
# xxx trigger on main/master branch pushes --> restart docker container
cd $CLONED_CACHE
git-pull
# get branch setup if needed. `cd` into checked out branch dir
if [ -e $DIR ]; then
BRANCH_NEEDS_SETUP=
cd $DIR
git checkout -b $BRANCH 2>/dev/null || git checkout $BRANCH # xxx necessary?
git-pull
else
BRANCH_NEEDS_SETUP=1
cp -pr $CLONED_CACHE/ $DIR/
cd $DIR
git checkout $BRANCH || git checkout -b $BRANCH # xxx or new unpushed branch
fi
# now we have enough information to read any YAML customization
CFG=coderunr.yml
[ -e $CFG ] || CFG=/coderunr/internetarchive/coderunr/main/$GROUP-$REPO.yml # xxx
function cfg-val() {
# Returns yaml configuration key, or "" if not present/empty
local VAL=
if [ -e $CFG ]; then
VAL=$(yq -r "$1" $CFG | sed 's/^[ \t]*//;s/[ \t]*$//' | grep -Ev '^#')
if [ "$VAL" = "" ]; then
VAL=
elif [ "$VAL" = null ]; then
VAL=
fi
fi
echo -n "$VAL"
}
function cfg-vals() {
# Returns yaml configuration array of values, as multiple lines, like cfg-val().
# You can split the returned string via NEWLINE characters.
local VAL=$(cfg-val "$1" | grep -E ^- | cut -b3-)
echo -n "$VAL"
}
PROXY=$(cfg-val .reverse_proxy)
PORT=
PORTHOST=
if [ "$PROXY" = "" ]; then
# NOTE: port: -1 means to have coderunr pick a port, for each branch of your repo to listen on
# It will pass the port number in as env var CODERUNR_PORT into `scripts.branch.start`
# commands.
PORT=$(cfg-val .port)
if [ "$PORT" != "" ]; then
# container has a static port to serve on -- let's map it to a unique higher 10000+ port
# See if we already have a port mapping for this project's container.
# For dynamic ports, that's one port per branch. Otherwise, same port for all branches.
SEARCH="$GROUP-$REPO"
[ "$PORT" = "-1" ] && SEARCH=$SLUG
PROXY=$(grep -E "# $SEARCH$" /coderunr/Caddyfile | grep -Eo "reverse_proxy[^#]+" | tr -s ' ' | cut -f2 -d ' ')
if [ "$PROXY" = "" ]; then
PORTMAX=$(grep reverse_proxy /coderunr/Caddyfile | grep -Eo ':1[0-9][0-9][0-9][0-9]' | sort -u | tail -1 | tr -d : | grep . || echo 10000)
let PORTHOST="1+$PORTMAX"
PROXY=localhost:$PORTHOST
else
PORTHOST=$(echo "$PROXY" | cut -f2 -d :)
fi
fi
fi
if [ $CLONE_NEEDED ]; then
# automatically start docker container & run container setup tasks
typeset -a ARGS
ARGS=($(cfg-val .docker.args))
if [ "$PORT" = "-1" ]; then
ARGS+=(--net=host)
elif [ "$PORTHOST" != "" ]; then
ARGS+=(-p $PORTHOST:$PORT/tcp)
fi
BRANCH_DEFAULT=$(cfg-val .branch.default)
[ $BRANCH_DEFAULT ] || BRANCH_DEFAULT=$(git rev-parse --abbrev-ref origin/HEAD | cut -f2- -d/)
# figure out which docker registry to use
REGISTRY=$REGISTRY_FALLBACK
[[ $CLONE =~ github.com ]] && REGISTRY=ghcr.io
[[ $CLONE =~ gitlab.com ]] && REGISTRY=registry.gitlab.com
IMG=$REGISTRY/$GROUP/$REPO/$BRANCH_DEFAULT
[[ $CLONE =~ github.com ]] && IMG=$REGISTRY/$GROUP/$REPO:$BRANCH_DEFAULT
if [ "$GROUP/$REPO" != "internetarchive/coderunr" ]; then
# www-av fails w/o --security-opt arg xxx _could_ move to www-av.yml if the only one...
docker run -d --restart=always --pull=always -v /coderunr/$GROUP/${REPO}:/coderunr --name=$GROUP-$REPO \
--security-opt seccomp=unconfined \
$ARGS $IMG
sleep 3
fi
(
IFS=$'\n'
for CMD in $(cfg-vals .scripts.container.start); do
docker exec $GROUP-$REPO sh -c "cd /coderunr/$BRANCH && $CMD" # xxx prolly should put all cmds into tmp file, pass file in, exec it
done
)
fi
if [ $BRANCH_NEEDS_SETUP ]; then
# now that docker container is up, run any optional initial build step(s) for new branch
(
IFS=$'\n'
for CMD in $(cfg-vals .scripts.branch.start); do
docker exec -e CODERUNR_PORT="$PORTHOST" $GROUP-$REPO sh -c "cd /coderunr/$BRANCH && $CMD" # xxx prolly should put all cmds into tmp file, pass file in, exec it
done
)
fi
DOCROOT=$(cfg-val .docroot)
[ "$DOCROOT" = "" ] && DOCROOT=www
[ ! -e $DOCROOT ] && mkdir $DOCROOT && (cd $DOCROOT && wget https://raw.githubusercontent.com/internetarchive/coderunr/main/www/index.html )
# now copy edited/save file in place
mkdir -p $(dirname "$FILE")
cat "$INCOMING" >| "$FILE"
rm -f "$INCOMING" # xxx setexit & always remove on errors
IDX=0
while true; do
TRIGGER=$(cfg-val ".triggers[$IDX].pattern")
[ "$TRIGGER" = "" ] && break
echo "$FILE" | grep -qE "$TRIGGER" && (
CMD=$(cfg-val ".triggers[$IDX].cmd")
docker exec $GROUP-$REPO sh -c "cd /coderunr/$BRANCH && $CMD" # xxx prolly should put all cmds into tmp file, pass file in, exec it
)
let "IDX=1+$IDX"
done
# ensure hostname is known to caddy
grep -qE "^$HOST \{" /coderunr/Caddyfile || (
(
echo "$HOST { # $GROUP-$REPO"
if [ "$PROXY" = "" ]; then
echo "
\troot * $DIR/$DOCROOT # $GROUP-$REPO
\tfile_server # $GROUP-$REPO"
else
echo "\treverse_proxy $PROXY # $GROUP-$REPO"
fi
echo "} # $GROUP-$REPO"
) | tee -a /coderunr/Caddyfile
docker exec coderunr zsh -c '/usr/sbin/caddy reload --config /coderunr/Caddyfile'
)
echo "\n\nhttps://$HOST\n\nSUCCESS CODERUNR\n\n"