-
Notifications
You must be signed in to change notification settings - Fork 0
/
am-dapp
executable file
·588 lines (587 loc) · 24.4 KB
/
am-dapp
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/usr/bin/env bash
set -E # any trap on ERR is inherited by shell functions
set -e # exit if error occurs
set -u # treat unset variables and parameters as an error
set -o pipefail # fail if pipe failed
# set -x # show every commond
#
DOCKER_PASSTHOUGH=""
set +u
if [[ -n "$3" ]]; then
# shellcheck disable=SC2124
DOCKER_PASSTHOUGH="${@:3}"
fi
set -u
#
GH_API_HEADER="Accept: application/vnd.github.v3+json"
GH_AUTH_HEADER=""
if [[ -n ${LP_GITHUB_API_TOKEN+x} ]]; then
GH_AUTH_HEADER="Authorization: token ${LP_GITHUB_API_TOKEN}"
fi
if [[ -z ${DISPLAY+x} ]]; then
DISPLAY=":0"
fi
#
# Ubuntu - latest = latest LTS, rolling = Latest release, devel.
DISTRO="$(curl --fail --silent --location --url https://raw.githubusercontent.com/tianon/docker-brew-ubuntu-core/master/rolling)"
# shellcheck disable=SC2181
if [[ -z "${DISTRO}" || $? != 0 ]]; then
HAVE_IP=0
else
HAVE_IP=1
fi
CO="forwardcomputers"
set +u
if [[ "${CIRCLECI}" ]]; then
ROOT="${CIRCLE_WORKING_DIRECTORY}/"
elif [[ "${GITHUB_ACTIONS}" ]]; then
ROOT="${GITHUB_WORKSPACE}/"
else
if [[ ! ${SU_COMMAND} ]]; then
if ! grep -q 'index.docker.io' "${HOME}"/.docker/config.json > /dev/null 2>&1 ; then
# shellcheck disable=SC2046
docker login --username forwardcomputers --password $(lpass show LP_DOCKER_PASSWORD --password) > /dev/null 2>&1
fi
fi
ROOT="/opt/filer/os/dockerfiles/"
fi
set -u
NAME=$( echo "${2-none}" | cut -d '/' -f 1 )
if [[ -h "${ROOT}${NAME}" ]]; then
NAME=$(readlink --quiet --silent "${ROOT}${NAME}")
fi
IMG="${CO}/${NAME}"
DESKTOP_DOCKERFILE=
HAVE_DOCKERFILE=0
DOCKERFILE_DOCKER_OPT=()
if [[ -d "${ROOT}" ]]; then
HAVE_DOCKERFILE=1
if [[ -f "${ROOT}${NAME}"/Dockerfile ]]; then
DESKTOP_DOCKERFILE=$( < "${ROOT}${NAME}"/Dockerfile )
fi
fi
if [[ ! -d ~/.local/share/applications ]]; then
mkdir -p ~/.local/share/applications
fi
#
# shellcheck disable=SC2034
BLACK=$'\033[30m'
# shellcheck disable=SC2034
BLUE=$'\033[34m'
# shellcheck disable=SC2034
GREEN=$'\033[32m'
# shellcheck disable=SC2034
CYAN=$'\033[36m'
# shellcheck disable=SC2034
PURPLE=$'\033[35m'
# shellcheck disable=SC2034
RED=$'\033[31m'
# shellcheck disable=SC2034
WHITE=$'\033[37m'
# shellcheck disable=SC2034
YELLOW=$'\033[1;33m'
# shellcheck disable=SC2034
NC=$'\033[0m'
#
# shellcheck disable=SC2191,SC2207,SC2086
DOCKER_OPT_BASE=(--rm --network=host --hostname=docker_"${NAME}" --shm-size=1gb \
--env HOME=/home/duser \
--volume /opt/filer:/opt/filer \
--volume /tmp/.X11-unix:/tmp/.X11-unix:rw \
--volume "${HOME}":/home/duser \
--user $(id -u ${USER}):$(id -g ${USER}) \
--workdir /home/duser)
# shellcheck disable=SC2191
DOCKER_OPT_LINUX=(--env DISPLAY=unix"${DISPLAY}" \
--env PULSE_SERVER=unix:/run/user/"${UID}"/pulse/native \
--device /dev/bus/usb \
--device /dev/dri \
--device /dev/snd \
--volume /etc/machine-id:/etc/machine-id:ro \
--volume /run/user/"${UID}"/pulse:/run/user/1001/pulse)
set +u
# shellcheck disable=SC2191
DOCKER_OPT_CROS=(--env DISPLAY=unix"${DISPLAY_LOW_DENSITY}" \
--device /dev/snd \
--volume /etc/machine-id:/etc/machine-id:ro \
--volume /run/user/"${UID}"/pulse:/run/user/1001/pulse)
# shellcheck disable=SC2191,SC2206
DOCKER_OPT_DARWIN=(--env DISPLAY=host.docker.internal:0 \
--env PULSE_SERVER=host.docker.internal)
#
# shellcheck disable=SC2207,SC2191
DOCKER_OPT_DUSER=(--env HOME=/home/duser \
--volume "${HOME}":/home/duser \
--user $(id -u "${USER}"):$(id -g "${USER}") \
--workdir /home/duser)
# shellcheck disable=SC2191
DOCKER_OPT_ROOT=(--env HOME=/root \
--user root \
--workdir /root)
if [[ $(uname -s) == Darwin ]]; then
DOCKER_OPT=("${DOCKER_OPT_BASE[@]}" "${DOCKER_OPT_DARWIN[@]}")
elif [[ ( -f /etc/lsb-release || -f /etc/os-release ) && -z "$SOMMELIER_VERSION" ]]; then
DOCKER_OPT=("${DOCKER_OPT_BASE[@]}" "${DOCKER_OPT_LINUX[@]}")
else
DOCKER_OPT=("${DOCKER_OPT_BASE[@]}" "${DOCKER_OPT_CROS[@]}")
fi
set -u
# shellcheck disable=SC2162
IFS=' ' read -a DOCKERFILE_DOCKER_OPT <<< "$(perl -ne 'print $& if /(?<=^#DOCKER_OPT ).*/' <<< "${DESKTOP_DOCKERFILE}" 2> /dev/null || true)"
unset IFS
if [[ "${DOCKERFILE_DOCKER_OPT[0]-}" == "+" ]]; then
# shellcheck disable=SC2145,SC2294
eval "DOCKER_OPT+=(${DOCKERFILE_DOCKER_OPT[@]:1})"
fi
if [[ "${DOCKERFILE_DOCKER_OPT[0]-}" == "=" ]]; then
# shellcheck disable=SC2145,SC2294
eval "DOCKER_OPT=(${DOCKERFILE_DOCKER_OPT[@]:1})"
fi
if [[ "${DOCKERFILE_DOCKER_OPT[0]-}" == "-" ]]; then
for i in "${DOCKERFILE_DOCKER_OPT[@]:1}"; do
DOCKER_OPT=("${DOCKER_OPT[@]/$i}")
done
fi
#
main () {
TARGET="${1-help}"
if [[ "${TARGET}" == "help" || ( "${NAME}" == "none" && "${TARGET}" != "info_all" && "${TARGET}" != "rebuild_all" && "${TARGET}" != "rebuild_all_ci" && "${TARGET}" != "upgrade_all" && "${TARGET}" != "upgrade_all_ci" && "${TARGET}" != "desktop_all" && "${TARGET}" != "readme" ) ]]; then
help
else
if ! type "${TARGET}" >/dev/null 2>&1; then
help
else
"${TARGET}"
fi
fi
}
#
help () { ## Show this help message
printf '\n%s\n %s\n\n%s\n' "Usage:" "${GREEN}dapp ${YELLOW}[target] ${CYAN}directory${NC}" "Targets:"
grep -E '^[a-zA-Z_-]+.*{.*?## .*$' "${0}" | sort | awk 'BEGIN {FS = "\\(.*?## "}; { printf " '"${GREEN}"'%-15s'"${YELLOW}"'%s'"${NC}"'\n", $1, $2} '
printf '\n'
}
#
info () { ## Check if there is a newer application version
if ! (( "${HAVE_IP}" )); then
printf '%s\n' "${RED}Unable to connect to internet${NC}"
exit
fi
checkbaseimage
if [[ ! "${APPNEW}" ]]; then
APPNEW=null
fi
# shellcheck disable=SC2053
if [[ "${APPNEW}" == ${APPHUB} && ! -f /tmp/MAKE_DISTRO_UPDATED ]]; then
printf '\r%s\n' "${YELLOW}${NAME}${GREEN} build is on latest version ${YELLOW}${APPNEW}${NC}"
else
if [[ ${APPHUB} == 0 ]]; then
if [[ ${APPNEW} == "null" ]]; then
APPNEW=""
fi
printf '%s\n' "${YELLOW}${NAME}${GREEN} nonexistant in Docker, use ${YELLOW}'dapp upgrade ${NAME}'${GREEN} or ${YELLOW}'dapp build ${NAME}'${GREEN} to generate the latest version ${YELLOW}${APPNEW}${NC}"
else
if [[ -f /tmp/MAKE_DISTRO_UPDATED ]]; then
printf '%s\n' "${YELLOW}${NAME}${GREEN} ${RED}base image ${YELLOW}${DISTRO_IMAGE}${NC} ${GREEN}has been updated"
else \
printf '%s\n' "${YELLOW}${NAME}${GREEN} ${RED}build is on older version ${YELLOW}${APPHUB}${NC} ${GREEN}current version ${YELLOW}${APPNEW}${NC}"
fi
fi
touch -f /tmp/MAKE_REBUILD
fi
rm -f /tmp/MAKE_DISTRO_UPDATED
}
#
info_all () { ## Check if there is a newer version for all applications
printf '%s\n' "Info on applications"
all_apps
for NAME in "${APPS[@]}"; do
IMG="${CO}/${NAME}"
appversions
info
done
}
#
update () {
upgrade
}
#
upgrade () { ## Upgrade if there is a newer application version
printf '%s' "${GREEN}Upgrading ${YELLOW}${NAME}${NC} - "
info
if [[ -f /tmp/MAKE_REBUILD ]]; then
build
push
printf '%s\n' "${GREEN}Rebuilt ${YELLOW}${NAME}${GREEN} to the latest version ${YELLOW}${APPNEW}${NC}"
rm -f /tmp/MAKE_REBUILD
readme
fi
}
#
upgrade_all () { ## Upgrade all applications
printf '%s\n' "Upgrading applications"
all_apps
set +e
for NAME in "${APPS[@]}"; do
IMG="${CO}/${NAME}"
appversions
upgrade
done
set -e
}
#
upgrade_all_ci () {
printf '%s\n' "Upgrading applications"
all_apps
set +e
for NAME in "${APPS[@]}"; do
IMG="${CO}/${NAME}"
appversions
upgrade
#shellcheck disable=SC2046
# set -x
# docker image rm -f $( docker image ls -q --filter reference="${IMG}" ) || true
# set +x
done
readme
set -e
}
#
rebuild () { ## Rebuild application
build
push
printf '%s\n' "${GREEN}Rebuilt ${YELLOW}${NAME}${GREEN} to the latest version ${YELLOW}${APPNEW}${NC}"
}
#
rebuild_all () { ## Rebuild all applications
printf '%s\n' "Rebuilding all applications"
all_apps
set +e
for NAME in "${APPS[@]}"; do
IMG="${CO}/${NAME}"
appversions
build
push
printf '%s\n' "${GREEN}Built ${YELLOW}${NAME}${GREEN} to the latest version ${YELLOW}${APPNEW}${NC}"
rm -f /tmp/MAKE_REBUILD
done
set -e
readme
}
#
rebuild_all_ci () {
printf '%s\n' "Rebuilding all applications"
all_apps
set +e
for NAME in "${APPS[@]}"; do
IMG="${CO}/${NAME}"
appversions
build
push
printf '%s\n' "${GREEN}Built ${YELLOW}${NAME}${GREEN} to the latest version ${YELLOW}${APPNEW}${NC}"
rm -f /tmp/MAKE_REBUILD
#shellcheck disable=SC2046
# set -x
# docker image rm -f $( docker image ls -q --filter reference="${IMG}" ) || true
# set +x
done
readme
set -e
}
#
build () { ## Build docker image file
if ! (( "${HAVE_IP}" )); then
printf '%s\n' "${RED}Cannot build ${GREEN}${NAME}${RED} unable to connect to internet${NC}"
exit
fi
if ! (( "${HAVE_DOCKERFILE}" )); then
printf '%s\n' "${RED}Cannot build ${GREEN}${NAME}${RED} missing dockerfile${NC}"
exit
fi
local BUILD_DATE
BUILD_DATE="$(date +'%-d-%-m-%G %r')"
PLATFORM="$(perl -nle 'print $& if /(?<=^#PLATFORM ).*/' "${ROOT}${NAME}"/Dockerfile || true)"
printf '%s\n' "Building ${NAME}"
set -x
# docker image build --rm --force-rm --squash \
# docker image build --rm --force-rm --compress \
# docker image build --rm --force-rm \
# docker image build \
# docker buildx build ${PLATFORM} --push \
docker image build \
--build-arg NAME="${NAME}" \
--build-arg CO="${CO}" \
--build-arg APPNEW="${APPNEW}" \
--build-arg BUILD_DATE="${BUILD_DATE}" \
--tag "${IMG}" \
--tag "${IMG}":"${APPNEW}" \
"${ROOT}${NAME}"
set +x
}
#
push () { ## Push image to Docker Hub
if [ ! $(perl -nle 'print $& if /(?<=^#PLATFORM ).*/' "${ROOT}${NAME}"/Dockerfile ) ]; then
printf '%s\n' "Pushing ${NAME}-${APPNEW} to Docker hub"
docker image push "${IMG}":latest
docker image push "${IMG}":"${APPNEW}"
fi
tweet
}
#
run () { ## Run the docker application
printf '%s\n' "Runing ${NAME}"
checklocalimage
if [[ ${APPHUB} > ${APPLOCAL} ]]; then
printf '%s\n' "Pulling ${IMG}"
docker image pull "${IMG}":latest
fi
xhost + LOCAL: 127.0.0.1 > /dev/null 2>&1 || true
if [[ $(uname -s) == Darwin ]]; then
rm -f /tmp/DOCKER_RUN_SCRIPT
printf '%s\n' \
"#!/usr/bin/env bash" \
"pulseaudio --kill || true" \
"pulseaudio --load=module-native-protocol-tcp --exit-idle-time=-1 --daemon" \
"PREFFERED_SINK=\$( pacmd list-sinks | awk '/index:/ {print \$1 \$2 \$3} /card:/ {print \$0};' | grep -m1 -B1 'Jabra_SPEAK_510_USB' | grep index | awk '{print \$1}' | cut -d ':' -f2 )" \
"pacmd set-default-sink \${PREFFERED_SINK}" \
"docker container run --name ${NAME} ${DOCKER_OPT[*]} ${IMG} ${DOCKER_PASSTHOUGH}" \
"pulseaudio --kill || true" \
"rm -f /tmp/DOCKER_RUN_SCRIPT" > /tmp/DOCKER_RUN_SCRIPT
bash /tmp/DOCKER_RUN_SCRIPT 2>/dev/null &
else
docker container run --detach --name "${NAME}" "${DOCKER_OPT[@]}" "${IMG}" ${DOCKER_PASSTHOUGH}
fi
}
#
shell () { ## Run shell in docker application
printf '%s\n' "Runing shell in ${NAME}"
checklocalimage
xhost + LOCAL: 127.0.0.1 > /dev/null 2>&1 || true
# if [[ $(uname -s) == Darwin ]]; then
# pulseaudio --kill || true
# pulseaudio --load=module-native-protocol-tcp --exit-idle-time=-1 --daemon
# PREFFERED_SINK=$( pacmd list-sinks | awk '/index:/ {print $1 $2 $3} /card:/ {print $0};' | grep -m1 -B1 'Jabra_SPEAK_510_USB' | grep index | awk '{print $1}' | cut -d ':' -f2 )
# pacmd set-default-sink "${PREFFERED_SINK}"
# fi
docker container run --interactive --tty --name "${NAME}"_shell --entrypoint /bin/sh "${DOCKER_OPT[@]}" "${DOCKER_OPT_DUSER[@]}" "${IMG}"
# if [[ $(uname -s) == Darwin ]]; then
# pulseaudio --kill || true
# fi
}
#
root () { ## Run shell in docker application as root
printf '%s\n' "Runing shell in ${NAME}"
checklocalimage
xhost + LOCAL: 127.0.0.1 > /dev/null 2>&1 || true
# if [[ $(uname -s) == Darwin ]]; then
# pulseaudio --kill || true
# pulseaudio --load=module-native-protocol-tcp --exit-idle-time=-1 --daemon
# PREFFERED_SINK=$( pacmd list-sinks | awk '/index:/ {print $1 $2 $3} /card:/ {print $0};' | grep -m1 -B1 'Jabra_SPEAK_510_USB' | grep index | awk '{print $1}' | cut -d ':' -f2 )
# pacmd set-default-sink "${PREFFERED_SINK}"
# fi
docker container run --interactive --tty --name "${NAME}"_shell --entrypoint /bin/sh "${DOCKER_OPT[@]}" "${DOCKER_OPT_ROOT[@]}" "${IMG}"
# if [[ $(uname -s) == Darwin ]]; then
# pulseaudio --kill || true
# fi
}
#
desktop () { ## Populate desktop application menu
if [[ $( perl -nle 'print $& if /(?<=^#DESKTOP).*/' <<< "${DESKTOP_DOCKERFILE}" ) ]]; then
printf '%s\n' "Populating application menu for ${NAME}"
# shellcheck disable=SC2086
DESKTOP_COUNT="$( perl -nle 'print $& if /(?<=^#DESKTOP_COUNT ).*/' < ${DESKTOP_DOCKERFILE} 2> /dev/null || echo '999' )"
for ((i=1; i<="${DESKTOP_COUNT}"; i++)); do
if [[ "${DESKTOP_COUNT}" = 999 ]]; then
i=''
fi
# shellcheck disable=SC2086
DESKTOP_NAME="$(perl -nle 'print $& if /(?<=^#'${i}'DESKTOP_NAME ).*/' <<< ${DESKTOP_DOCKERFILE} 2> /dev/null || true)"
# shellcheck disable=SC2086
DESKTOP_COMMENT="$(perl -nle 'print $& if /(?<=^#'${i}'DESKTOP_COMMENT ).*/' <<< ${DESKTOP_DOCKERFILE} 2> /dev/null || true)"
# shellcheck disable=SC2086
DESKTOP_CATEGORIES="$(perl -nle 'print $& if /(?<=^#'${i}'DESKTOP_CATEGORIES ).*/' <<< ${DESKTOP_DOCKERFILE} 2> /dev/null || true)"
# shellcheck disable=SC2086
DESKTOP_MIMETYPE="$(perl -nle 'print $& if /(?<=^#'${i}'DESKTOP_MIMETYPE ).*/' <<< ${DESKTOP_DOCKERFILE} 2> /dev/null || true)"
# shellcheck disable=SC2086
DESKTOP_LOGO="$(perl -nle 'print $& if /(?<=^#'${i}'DESKTOP_LOGO ).*/' <<< ${DESKTOP_DOCKERFILE} 2> /dev/null || true)"
# shellcheck disable=SC2001,SC2046,SC2086
DESKTOP_LOGO_NAME="${NAME}_$(basename $( sed 's/?format.*//' <<< ${DESKTOP_LOGO}))"
# shellcheck disable=SC2086
DESKTOP_EXEC="$(perl -nle 'print $& if /(?<=^#'${i}'DESKTOP_EXEC ).*/' <<< ${DESKTOP_DOCKERFILE} 2> /dev/null || true)"
if [[ ! "${DESKTOP_EXEC}" ]]; then
DESKTOP_EXEC="${NAME}"
fi
curl --silent --location --output ~/.local/share/applications/"${DESKTOP_LOGO_NAME}" "${DESKTOP_LOGO}"
if [[ "${DESKTOP_LOGO}" == *?format=TEXT* ]]; then
mv ~/.local/share/applications/"${DESKTOP_LOGO_NAME}" /tmp
base64 --decode /tmp/"${DESKTOP_LOGO_NAME}" > ~/.local/share/applications/"${DESKTOP_LOGO_NAME}"
fi
printf '%s\n' \
"[Desktop Entry]" \
"Version=1.0" \
"Type=Application" \
"Terminal=false" \
"Encoding=UTF-8" \
"StartupNotify=true" \
"Name=${DESKTOP_NAME}" \
"Comment=${DESKTOP_COMMENT}" \
"Categories=${DESKTOP_CATEGORIES}" \
"MimeType=${DESKTOP_MIMETYPE}" \
"Exec=${HOME}/bin/dapp run ${DESKTOP_EXEC}" \
"Icon=${HOME}/.local/share/applications/${DESKTOP_LOGO_NAME}" > ~/.local/share/applications/"${NAME}${i}".desktop
printf '%s\n' "${DESKTOP_MIMETYPE}=${NAME}.desktop;" >> ~/.local/share/applications/mimeapps.list
if [[ "${i}" == '' || "${i}" == "${DESKTOP_COUNT}" ]]; then
i=999
fi
done
fi
}
#
desktop_all () { ## Populate all desktop applications
printf '%s\n' "Populating all desktop applications"
all_apps
for NAME in "${APPS[@]}"; do
DESKTOP_DOCKERFILE=$( < "${ROOT}${NAME}"/Dockerfile )
desktop
done
}
#
remove () { ## Remove app docker and desktop files
printf '%s\n' "Removing ${NAME}"
rm --force "${HOME}"/.local/share/applications/"${NAME}"*.*
# shellcheck disable=SC2046
docker image rm --force $( docker image ls --quiet "${IMG}" | uniq ) 2> /dev/null
}
#
readme () { ## Create readme file
printf '%s\n' "Creating readme file"
all_apps
printf '%s\n' \
"# Docker files" \
"#### Docker files for various Linux applications." \
"---" \
"| Repository | Status | GitHub | Docker | Tag | Size | Layers |" \
"| --- | --- | :---: | :---: | :--- | :---: | :---: |" \
"| [![](https://img.shields.io/badge/dockerfiles-grey)](https://github.com/forwardcomputers/dockerfiles) | [![](https://img.shields.io/github/actions/workflow/status/forwardcomputers/dockerfiles/build_all.yml?label)](https://github.com/forwardcomputers/dockerfiles/actions) | [![](https://img.shields.io/badge/github--grey.svg?label=&logo=github&logoColor=white)](https://github.com/forwardcomputers/dockerfiles) | [![](https://img.shields.io/badge/docker--E5E5E5.svg?label=&logo=docker)](https://hub.docker.com/r/forwardcomputers) |" > "${ROOT}"README.md
for APP in "${APPS[@]}"; do
_token=$( curl -sSL "https://auth.docker.io/token?service=registry.docker.io&scope=repository:forwardcomputers/${APP}:pull" | jq -r .token )
_digest=$( curl -sL -H "Authorization: Bearer ${_token}" -H "Accept: application/vnd.docker.distribution.manifest.v2+json" "https://registry-1.docker.io/v2/forwardcomputers/${APP}/manifests/latest" | jq -r .config.digest )
_blob=$( curl -sL -H "Authorization: Bearer ${_token}" -H "Accept: application/vnd.docker.distribution.manifest.v2+json" "https://registry-1.docker.io/v2/forwardcomputers/${APP}/blobs/${_digest}" )
# _rsize=$( curl -sL "https://hub.docker.com/v2/repositories/${_repo}/tags/latest" )
#
# _size=$( jq -r '((.full_size/1024/1024)*100 | round/100 | tostring)+" MB"' <<< _rsize )
_built=$( jq -r '.config.Labels."org.opencontainers.image.created" | gsub("-";"--") | gsub(" ";"_")' <<< $_blob || echo "" )
_version=$( jq -r '.config.Labels."org.opencontainers.image.version" | gsub("-";"--")' <<< $_blob || echo "" )
_layers=$( jq -r '.history | length' <<< $_blob || echo "" )
#
printf '%s' \
"| [![](https://img.shields.io/badge/${APP}-grey)](https://hub.docker.com/r/forwardcomputers/${APP}) " \
"| ![](https://img.shields.io/badge/${_built}-blue) " \
"| [![](https://img.shields.io/badge/github--grey.svg?label=&logo=github&logoColor=white)](https://github.com/forwardcomputers/dockerfiles/tree/main/${APP}) " \
"| [![](https://img.shields.io/badge/docker--E5E5E5.svg?label=&logo=docker)](https://hub.docker.com/r/forwardcomputers/${APP}) " \
"| [![](https://img.shields.io/badge/${_version}-blue)](https://hub.docker.com/r/forwardcomputers/${APP}) " \
"| ![](https://img.shields.io/docker/image-size/forwardcomputers/${APP}/latest?label=) " \
"| ![](https://img.shields.io/badge/${_layers}-blue) " \
"|" >> "${ROOT}"README.md
printf '\n' >> "${ROOT}"README.md
done
printf '\n%s' \
"- This setup is largely based on Jessie Frazelle work [https://github.com/jfrazelle/dockerfiles](https://github.com/jfrazelle/dockerfiles) " >> "${ROOT}"README.md
# "- FoxitReader - From the Arch Linux repository @ [https://aur.archlinux.org/packages/foxitreader](https://aur.archlinux.org/packages/foxitreader) " \
# "- Code-server " \
# " - Home Assistant Visual Studio Code add-on @ [https://github.com/hassio-addons/addon-vscode](https://github.com/hassio-addons/addon-vscode) " \
# " - LinuxServer @ [https://github.com/linuxserver/docker-code-server](https://github.com/linuxserver/docker-code-server) " \
# sed -i 's/firefox-esr-grey/firefox%E2%80%93esr-grey/g' "${ROOT}"README.md
}
#
appversions () {
if (( "${HAVE_IP}" )); then
DISTRO_IMAGE="$(awk '/^FROM /{print $2;exit}' "${ROOT}${NAME}"/Dockerfile 2> /dev/null || true)"
# DISTRO_IMAGE="$(sed --silent --expression='s/^FROM //g; s/ AS .*//p' "${ROOT}${NAME}"/Dockerfile | head -1 2> /dev/null || true)"
APPLOCAL="$(docker image inspect --format '{{ index .Config.Labels "org.opencontainers.image.version" }}' "${IMG}" 2> /dev/null || true)"
until APPHUB="$(curl --silent --location --url https://registry.hub.docker.com/v2/repositories/forwardcomputers/"${NAME}"/tags | jq --raw-output '.results|.[1]|.name // 0')"; do
sleep 1
done
if (( "${HAVE_DOCKERFILE}" )); then
APPNEW="$(perl -nle 'print $& if /(?<=^#APPNEW ).*/' "${ROOT}${NAME}"/Dockerfile 2> /dev/null || true)"
else
printf '%s\n' "${RED}Cannot build ${GREEN}${NAME}${RED} missing dockerfile${NC}"
exit
fi
if ! [[ "${APPNEW}" =~ ^[+-]?[0-9]+\.?[0-9]*$ ]]; then
if [[ "${APPNEW}" == "apk" ]]; then
APPNEW=$(curl -sL https://git.alpinelinux.org/aports/plain/main/"${NAME}"/APKBUILD | awk -F= '/pkgver=/ {print $2}')
if [[ -z "${APPNEW}" ]]; then
APPNEW=$(curl -sL https://git.alpinelinux.org/aports/plain/community/"${NAME}"/APKBUILD | awk -F= '/pkgver=/ {print $2}')
if [[ -z "${APPNEW}" ]]; then
APPNEW=$(curl -sL https://git.alpinelinux.org/aports/plain/testing/"${NAME}"/APKBUILD | awk -F= '/^pkgver=/ {print $2}')
fi
fi
elif [[ "${APPNEW}" == "apt" ]]; then
until APTNEWPAGE=$(curl --fail --silent --location --url https://packages.ubuntu.com/"${DISTRO}"/"${NAME}"); do
sleep 1
done
APPNEW="$(echo "${APTNEWPAGE}" | perl -nle 'print $1 if /Package: '"${NAME}"' \((\K[^\)]+)/' | cut -f 1 -d '-' | cut -f 1 -d '+' | cut -f 1 -d '~' | cut -f 2 -d ':')"
else
set +e
eval APPNEW=\$\("$(perl -nle 'print $& if /(?<=^#APPNEW ).*/' "${ROOT}${NAME}"/Dockerfile 2> /dev/null || true)"\)
set -e
fi
fi
fi
}
#
checkbaseimage () {
rm -f /tmp/MAKE_DISTRO_LOG /tmp/MAKE_DISTRO_UPDATED /tmp/MAKE_REBUILD
docker image pull "${IMG}":latest > /dev/null 2>&1 || true
if [[ "${DISTRO_IMAGE}" != *"python"* ]]; then
docker image pull "${DISTRO_IMAGE}" > /tmp/MAKE_DISTRO_LOG
fi
# shellcheck disable=SC2143
if [[ -f /tmp/MAKE_DISTRO_LOG && -n "$(grep 'Pull complete' /tmp/MAKE_DISTRO_LOG)" ]]; then
touch -f /tmp/MAKE_DISTRO_UPDATED
fi
rm -f /tmp/MAKE_DISTRO_LOG
}
#
checklocalimage () {
if ! docker image inspect "${IMG}" > /dev/null 2>&1; then
printf '%s\n' "Pulling ${IMG}"
# shellcheck disable=SC2015
docker image pull "${IMG}":latest > /dev/null 2>&1 || { [[ -d "${ROOT}" ]] && build || { printf '%s\n' "${RED}Cannot build ${GREEN}${NAME}${RED} unable to connect to file share${NC}"; exit; } }
APPLOCAL="$(docker image inspect --format '{{ index .Config.Labels "org.opencontainers.image.version" }}' "${IMG}" 2> /dev/null || true)"
fi
}
#
tweet () {
printf '%s\n' "Tweet ${NAME} push"
# Code bits from - https://github.com/moebiuscurve/tweetExperiments/tree/master/curlTweets
message="Pushed ${NAME}-${APPNEW}"
message_string=$(echo -n "${message}"|sed -e s'/ /%2520/g')
message_curl=$(echo -n "${message}"|sed -e s'/ /+/g')
timestamp=$(date +%s)
nonce=$(date +%s%T | openssl base64 | sed -e s'/[+=/]//g')
api_version=1.1
signature_base_string="POST&https%3A%2F%2Fapi.twitter.com%2F${api_version}%2Fstatuses%2Fupdate.json&oauth_consumer_key%3D${LP_T_CONSUMER_KEY}%26oauth_nonce%3D${nonce}%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D${timestamp}%26oauth_token%3D${LP_T_OAUTH_TOKEN}%26oauth_version%3D1.0%26status%3D${message_string}"
signature_key="${LP_T_CONSUMER_SECRET}&${LP_T_OAUTH_SECRET}"
oauth_signature=$(echo -n "${signature_base_string}" | openssl dgst -sha1 -hmac "${signature_key}" -binary | openssl base64 | sed -e s'/+/%2B/g' -e s'/\//%2F/g' -e s'/=/%3D/g')
header="Authorization: OAuth oauth_consumer_key=\"${LP_T_CONSUMER_KEY}\", oauth_nonce=\"${nonce}\", oauth_signature=\"${oauth_signature}\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"${timestamp}\", oauth_token=\"${LP_T_OAUTH_TOKEN}\", oauth_version=\"1.0\""
# shellcheck disable=SC2034
result=$(curl -s -X POST "https://api.twitter.com/${api_version}/statuses/update.json" --data "status=${message_curl}" --header "Content-Type: application/x-www-form-urlencoded" --header "${header}")
}
#
all_apps () {
# shellcheck disable=SC2207
APPS_base=( $( \
curl --silent --location --header "${GH_AUTH_HEADER}" --header "${GH_API_HEADER}" --url https://api.github.com/repos/forwardcomputers/dockerfiles/contents | \
jq -r 'sort_by( .name )[] | select( .type == "dir" and ( .name | startswith( "base" ) ) and ( .name | startswith( "." ) | not ) ) | .name' ) \
)
APPS_rest=( $( \
curl --silent --location --header "${GH_AUTH_HEADER}" --header "${GH_API_HEADER}" --url https://api.github.com/repos/forwardcomputers/dockerfiles/contents | \
jq -r 'sort_by( .name )[] | select( .type == "dir" and ( .name | startswith( "base" ) | not ) and ( .name | startswith( "." ) | not ) ) | .name' ) \
)
APPS=( "${APPS_base[@]}" "${APPS_rest[@]}" )
}
#
appversions
main "$@"