From 94ba9b86c726857c9a9ac302232f9c40f2c70155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Val=C3=AD=C4=8Dek=20=28YCNet=29?= Date: Sat, 10 Feb 2018 00:57:16 +0100 Subject: [PATCH] Launcher image --- Makefile | 8 +++- launcher-image/Dockerfile | 11 +++++ launcher-image/src/cron-command | 39 ++++++++++++++++ launcher-image/src/entrypoint | 11 +++++ launcher-image/src/run-mirror-update | 67 ++++++++++++++++++++++++++++ 5 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 launcher-image/Dockerfile create mode 100755 launcher-image/src/cron-command create mode 100755 launcher-image/src/entrypoint create mode 100755 launcher-image/src/run-mirror-update diff --git a/Makefile b/Makefile index a406f4a..f890986 100644 --- a/Makefile +++ b/Makefile @@ -12,13 +12,19 @@ mirror: run-mirror: mirror docker run -v $(VOLUME):/data -it --rm $(TAGBASE)-mirror -# Creator - ./creator dir +# Creator - ./creator-image dir creator: docker build -t $(TAGBASE)-creator ./creator-image run-creator: creator docker run -v /var/run/docker.sock:/var/run/docker.sock -it --rm $(TAGBASE)-creator +# Launcher - ./launcher-image +launcher: + docker build -t $(TAGBASE)-launcher ./launcher-image + +run-launcher: launcher + docker run -v /var/run/docker.sock:/var/run/docker.sock -it --rm $(TAGBASE)-launcher # wizzard wizzard: mirror run-creator diff --git a/launcher-image/Dockerfile b/launcher-image/Dockerfile new file mode 100644 index 0000000..20277f6 --- /dev/null +++ b/launcher-image/Dockerfile @@ -0,0 +1,11 @@ +# Alpine base image +FROM alpine:edge + +RUN apk add --no-cache bash docker parallel ; \ + echo "* * * * * /bin/cron-command" >> /etc/crontabs/root + +ADD src/* /bin/ + +CMD [ "/bin/entrypoint" ] + + diff --git a/launcher-image/src/cron-command b/launcher-image/src/cron-command new file mode 100755 index 0000000..c2c466a --- /dev/null +++ b/launcher-image/src/cron-command @@ -0,0 +1,39 @@ +#!/bin/bash +set -euo pipefail +IFS=$'\n\t' + +# trap +lock=/var/run/cloner.pid + +volume_prefix=cloner +clone_prefix=cloner-runner + + +function die(){ + echo $@ 1>&2 + exit 1 +} + +# first, check process +if [ -f $lock ] +then + pid=$(cat $lock) + # if it still runs, die not so quietly + [ -d /proc/$pid ] && die "Another process running!" +fi +# else make some mess and setup trap +echo $BASHPID > $lock +function finish { + rm -rf $lock +} +trap finish EXIT + + +# declare functions +function listVolumes(){ + docker volume ls --filter=name=$volume_prefix- --quiet +} + +# run for every volume in parallel +listVolumes | parallel --lb -j 3 -n 1 --progress run-mirror-update + diff --git a/launcher-image/src/entrypoint b/launcher-image/src/entrypoint new file mode 100755 index 0000000..39e8d52 --- /dev/null +++ b/launcher-image/src/entrypoint @@ -0,0 +1,11 @@ +#!/bin/bash +set -euo pipefail +IFS=$'\n\t' + +# make parallel citation shut up +mkdir $HOME/.parallel +touch $HOME/.parallel/will-cite + +# run cron +crond -f + diff --git a/launcher-image/src/run-mirror-update b/launcher-image/src/run-mirror-update new file mode 100755 index 0000000..76a42a2 --- /dev/null +++ b/launcher-image/src/run-mirror-update @@ -0,0 +1,67 @@ +#!/bin/bash +set -euo pipefail +IFS=$'\n\t' + +mirror_image="valicek1/repo-cloner-mirror" + + +function log(){ + local title=${raw:-$name} + [ -z "$title" ] || title=" [$title]" + echo "[$(date +"%X")]$title $@" +} + +function die(){ + log "$@" 1>&2 + exit 1 +} + + +scratch=$(mktemp -d -t tmp.XXXXXXXXXX) +function finish { + rm -rf "$scratch" +} +trap finish EXIT + +volume_prefix=cloner +clone_prefix=cloner-runner + +name=${1:-} +[ -n "$name" ] || die "No volume specified" + +# contains prefix +[[ "$name" == $volume_prefix-* ]] || die "Volume name does not contain prefix!" + +raw=$(echo $name | sed "s/^$volume_prefix-//g") + +container_name=$clone_prefix-$raw + +# does it run? +docker ps --quiet --no-trunc --filter name=^/$container_name$ > $scratch/running +lines=$(wc -l $scratch/running | cut -f1 -d' ') + +if ! [ $lines -eq 0 ] +then + log "Another copy of container is running.. Exiting silently" + exit 0 +fi +unset lines + +# does it exist? +docker ps --quiet -a --no-trunc --filter name=^/$container_name$ > $scratch/existing +lines=$(wc -l $scratch/existing | cut -d' ' -f1) + +if ! [ $lines -eq 0 ] +then + log "Conflicting container exists, removing.." + docker rm $(cat $scratch/existing) +fi + +unset lines + +# does volume exist? +docker volume inspect $name > /dev/null 2>&1 || die "Volume '$name' does not exist - exiting!" + +log "Running container of update process..." +docker run -v $name:/data --name $container_name --rm $mirror_image | while read line; do log "$line"; done +log "Finished."