From aa601dc1fa7cc6f5b9362543b7e90282ec778360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Val=C3=AD=C4=8Dek=20=28YCNet=29?= Date: Thu, 8 Feb 2018 15:09:47 +0100 Subject: [PATCH] Working version of cloner, basic setup of git and so on --- Dockerfile | 6 +++-- README.md | 13 ++++++++++ checker/cloner-lib-auth | 18 ++++++++++++++ checker/cloner-lib-cfg | 12 ++++++++++ checker/cloner-lib-general | 12 ++++++++++ checker/run-checker | 49 ++++++++++++++++++++++++++++++++++++++ run.sh | 1 + 7 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 README.md create mode 100644 checker/cloner-lib-auth create mode 100644 checker/cloner-lib-cfg create mode 100644 checker/cloner-lib-general create mode 100755 checker/run-checker diff --git a/Dockerfile b/Dockerfile index bc87e87..b1c5b7d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,9 @@ VOLUME /data RUN apk add --no-cache git bash - -CMD [ "/bin/bash" ] +ADD checker/* src/* /usr/local/bin/ + + +CMD [ "/usr/local/bin/run-checker" ] diff --git a/README.md b/README.md new file mode 100644 index 0000000..6bdb89e --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# Docker Repo Checker + +Container, that runs mirror process of git repository periodically + +# Run command: + +Tiniest case is: + +```bash +docker run -v cloner-test:/data -it --rm docker-cloner +``` + +when you HAVE to specify volume with configuration etc diff --git a/checker/cloner-lib-auth b/checker/cloner-lib-auth new file mode 100644 index 0000000..86280fd --- /dev/null +++ b/checker/cloner-lib-auth @@ -0,0 +1,18 @@ +#!/bin/bash +# library made for sourcing - just to prepare git auth environment + +function prepareGitAuth(){ + # usage + # $1 - config directory + confdir=$1/auth + [ -d $confdir ] || mkdir $confdir + [ -d $confdir/ssh ] || mkdir $confdir/ssh + + chmod 0600 $confdir + + # git configure http authenticator + git config --global credential.helper "store --file=$confdir/git-credentials" + # git configure ssh auth + git config --global core.sshcommand "ssh -i $confdir/ssh/id_rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes" +} + diff --git a/checker/cloner-lib-cfg b/checker/cloner-lib-cfg new file mode 100644 index 0000000..ad6e3f3 --- /dev/null +++ b/checker/cloner-lib-cfg @@ -0,0 +1,12 @@ +#!/bin/bash +# library for work with config file + +# check directories if they exist +[ -d $CONFIG_DIR ] || die "Config directory does not exist on volune - $CONFIG_DIR" +[ -f $CONFIG_DIR/cloner.cfg ] || die "Config file does not exist - create please cloner.cfg" +# load config files +source $CONFIG_DIR/cloner.cfg + +# check if the url is specified +repo=${cloner_repo_url:-} +[ -z "$repo" ] && die "No repository url is specified" || true diff --git a/checker/cloner-lib-general b/checker/cloner-lib-general new file mode 100644 index 0000000..9bcf2f1 --- /dev/null +++ b/checker/cloner-lib-general @@ -0,0 +1,12 @@ +#!/bin/bash +# configure +export BASE=/data +export CCLONE_PATH=$BASE/repos +export CCLONE_CACHE=$BASE/cache +export CONFIG_DIR=$BASE/config + +function die(){ + echo "$@" 1>&2 + exit 1 +} + diff --git a/checker/run-checker b/checker/run-checker new file mode 100755 index 0000000..80f44ac --- /dev/null +++ b/checker/run-checker @@ -0,0 +1,49 @@ +#!/bin/bash +set -euo pipefail +IFS=$'\n\t' + +# source libs +mydir=$(dirname $(realpath $0)) +source $mydir/cloner-lib-general +source $mydir/cloner-lib-auth +source $mydir/cloner-lib-cfg + + +# interval - in minutes +interval=${cloner_interval:-0} +stampfile=$CCLONE_CACHE/last-check-time + +# does it exist - if not, sync +[ -d $CCLONE_CACHE ] || mkdir $CCLONE_CACHE +[ -f $stampfile ] || echo 0 > $stampfile + + +now=$(date +"%s") +last=$(cat $stampfile) +diff=$(($now - $last)) +mindiff=$(($interval * 60)) +unset now last + +if [ $diff -lt $mindiff ] +then + echo "Limit not reached - not syncing now" + exit 0 +fi + +# check and clone repo +submodules=${cloner_submodules:-0} +depth=${cloner_submodule_depth:-} + +prepareGitAuth $CONFIG_DIR + +# without submodule support +if [ ! "x$submodules" = "x1" ] +then + mirror-main-repo $repo +else + mirror-recursive $repo $depth +fi + +date +"%s" > $stampfile + + diff --git a/run.sh b/run.sh index 83792ca..2f7b98b 100755 --- a/run.sh +++ b/run.sh @@ -1,3 +1,4 @@ #!/bin/bash docker build -t docker-cloner . docker run -v cloner-test:/data -it --rm docker-cloner +