From b7b9266735abc2c361eb723795ae9054e9fab964 Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 9 Dec 2022 10:49:57 +0100 Subject: [PATCH] Set up correct test for build image --- .../image/resources/entrypoint.sh | 4 +- .../c4k-website-build/test/Dockerfile | 5 +++ .../test/resources/entrypoint.sh | 36 +++++++++++++++ .../test/resources/exclude.pattern | 2 + .../test/resources/functions.sh | 45 +++++++++++++++++++ .../test/resources/install.sh | 8 ++++ .../test/resources/project.clj | 11 +++++ .../c4k-website-build/test/serverspec.edn | 7 ++- 8 files changed, 112 insertions(+), 6 deletions(-) create mode 100755 infrastructure/c4k-website-build/test/resources/entrypoint.sh create mode 100644 infrastructure/c4k-website-build/test/resources/exclude.pattern create mode 100644 infrastructure/c4k-website-build/test/resources/functions.sh create mode 100755 infrastructure/c4k-website-build/test/resources/install.sh create mode 100644 infrastructure/c4k-website-build/test/resources/project.clj diff --git a/infrastructure/c4k-website-build/image/resources/entrypoint.sh b/infrastructure/c4k-website-build/image/resources/entrypoint.sh index 871a00d..f18d5bd 100755 --- a/infrastructure/c4k-website-build/image/resources/entrypoint.sh +++ b/infrastructure/c4k-website-build/image/resources/entrypoint.sh @@ -18,8 +18,8 @@ newHash=$( get-hash-data ) if [[ $currentHash == $newHash ]] then echo "Nothing to do" - else - write-hash-data $currentHash $hashfilename + else + echo $currentHash > $HASHFILEDIR/$hashfilename echo "Downloading website data" get-website-data $filename unzip-website-data $filename diff --git a/infrastructure/c4k-website-build/test/Dockerfile b/infrastructure/c4k-website-build/test/Dockerfile index 47c3702..db96cce 100644 --- a/infrastructure/c4k-website-build/test/Dockerfile +++ b/infrastructure/c4k-website-build/test/Dockerfile @@ -1,8 +1,13 @@ FROM clojure:lein +# Prepare Entrypoint Script +ADD resources /tmp + +RUN /tmp/install.sh RUN apt update RUN apt -yqq --no-install-recommends --yes install curl default-jre-headless + RUN curl -L -o /tmp/serverspec.jar \ https://github.com/DomainDrivenArchitecture/dda-serverspec-crate/releases/download/2.0.0/dda-serverspec-standalone.jar diff --git a/infrastructure/c4k-website-build/test/resources/entrypoint.sh b/infrastructure/c4k-website-build/test/resources/entrypoint.sh new file mode 100755 index 0000000..871a00d --- /dev/null +++ b/infrastructure/c4k-website-build/test/resources/entrypoint.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +mkdir $BUILDDIR +mkdir $SOURCEDIR + +set -euo pipefail + +source /usr/local/bin/functions.sh + +filename="website.zip" +hashfilename="hashfile" + +echo "Check for new content" +touch $HASHFILEDIR/$hashfilename +currentHash=$( cat $HASHFILEDIR/$hashfilename ) +newHash=$( get-hash-data ) + +if [[ $currentHash == $newHash ]] + then + echo "Nothing to do" + else + write-hash-data $currentHash $hashfilename + echo "Downloading website data" + get-website-data $filename + unzip-website-data $filename + echo "Executing Custom Scripts, if applicable" + execute-scripts-when-existing + echo "Building website" + build-website + echo "Moving files" + move-website-files-to-target +fi + + + + diff --git a/infrastructure/c4k-website-build/test/resources/exclude.pattern b/infrastructure/c4k-website-build/test/resources/exclude.pattern new file mode 100644 index 0000000..3978a0f --- /dev/null +++ b/infrastructure/c4k-website-build/test/resources/exclude.pattern @@ -0,0 +1,2 @@ +.git +.gitignore diff --git a/infrastructure/c4k-website-build/test/resources/functions.sh b/infrastructure/c4k-website-build/test/resources/functions.sh new file mode 100644 index 0000000..7ab68db --- /dev/null +++ b/infrastructure/c4k-website-build/test/resources/functions.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +function get-website-data() { + curl -H "Authorization: token $AUTHTOKEN" -o $SOURCEDIR/$1 $GITREPOURL +} + +function get-hash-data() { + curl -s -H "Authorization: token $AUTHTOKEN" $GITCOMMITURL | jq '.sha' +} + +function write-hash-data() { + echo $1 > $HASHFILEDIR/$2 +} + +function unzip-website-data() { + unzip $SOURCEDIR/$1 -d $BUILDDIR +} + +function execute-scripts-when-existing() { + websitedir=$(ls $BUILDDIR) + if [[ -f $BUILDDIR/$websitedir/$SCRIPTFILE ]] + then + checksum="$(sha256sum $BUILDDIR/$websitedir/$SCRIPTFILE | grep -oE "^[a-z0-9]+")" + if [[ "$SHA256SUM" == "$checksum" ]] + then + chmod +x $BUILDDIR/$websitedir/$SCRIPTFILE + (cd $BUILDDIR; dir=$(ls); cd $dir; ./$SCRIPTFILE) #make sure paths defined in scriptfile are relative to $dir + else + printf "Provided SHA256 Sum does not match calculated sum. Exiting." + printf "Calculated SHA256: $checksum" + printf "Given SHA256: $SHA256SUM" + exit 1 + fi + else + printf "No script file provided." + fi +} + +function build-website() { + (cd $BUILDDIR; dir=$(ls); cd $dir; lein run;) +} + +function move-website-files-to-target() { + (cd $BUILDDIR; dir=$(ls); cd $dir; rsync -ru --exclude-from "/etc/exclude.pattern" --delete resources/public/* $WEBSITEROOT;) +} diff --git a/infrastructure/c4k-website-build/test/resources/install.sh b/infrastructure/c4k-website-build/test/resources/install.sh new file mode 100755 index 0000000..fddcecb --- /dev/null +++ b/infrastructure/c4k-website-build/test/resources/install.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +mkdir /etc/lein/ + +install -m 0700 /tmp/entrypoint.sh / +install -m 0700 /tmp/functions.sh /usr/local/bin/ +install -m 0700 /tmp/exclude.pattern /etc/ +install -m 0700 /tmp/project.clj /etc/lein/ diff --git a/infrastructure/c4k-website-build/test/resources/project.clj b/infrastructure/c4k-website-build/test/resources/project.clj new file mode 100644 index 0000000..403046f --- /dev/null +++ b/infrastructure/c4k-website-build/test/resources/project.clj @@ -0,0 +1,11 @@ +(defproject org.domaindrivenarchitecture/c4k-website-build "0.1.1-SNAPSHOT" + :description "website c4k-build package" + :url "https://domaindrivenarchitecture.org" + :license {:name "Apache License, Version 2.0" + :url "https://www.apache.org/licenses/LICENSE-2.0.html"} + :dependencies [[org.clojure/clojure "1.9.0"] + [dda/cryogen-bootstrap "0.1.5"]] + :plugins [[lein-ring "0.12.5"]] + :main cryogen.core + :ring {:init cryogen.server/init + :handler cryogen.server/handler}) diff --git a/infrastructure/c4k-website-build/test/serverspec.edn b/infrastructure/c4k-website-build/test/serverspec.edn index 000622f..87038f9 100644 --- a/infrastructure/c4k-website-build/test/serverspec.edn +++ b/infrastructure/c4k-website-build/test/serverspec.edn @@ -1,5 +1,4 @@ -{:file [{:path "/usr/local/bin/install.sh" :mod "700"} +{:file [{:path "/entrypoint.sh" :mod "700"} {:path "/usr/local/bin/functions.sh" :mod "700"} - {:path "/usr/local/bin/exclude.pattern" :mod "700"} - {:path "/usr/local/bin/project.clj" :mod "700"} - {:path "/entrypoint.sh" :mod "700"}]} + {:path "/etc/exclude.pattern" :mod "700"} + {:path "/etc/lein/project.clj" :mod "700"}]}