From 03406f41058adace79bdfb026e646d523c9f8929 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Fri, 14 May 2021 15:31:31 -0700 Subject: [PATCH] Initial commit --- .drone.yml | 100 +++++++++++++++++++++++++++++++++++++++ .pre-commit-config.yaml | 17 +++++++ Dockerfile | 8 ++++ LICENSE.txt | 21 ++++++++ Makefile | 28 +++++++++++ Readme.md | 46 ++++++++++++++++++ manifest.tmpl | 25 ++++++++++ run.sh | 25 ++++++++++ tests/.gitignore | 1 + tests/docker-compose.yml | 10 ++++ tests/expected/test.txt | 1 + tests/in/test.txt | 1 + tests/itest.sh | 22 +++++++++ 13 files changed, 305 insertions(+) create mode 100644 .drone.yml create mode 100644 .pre-commit-config.yaml create mode 100644 Dockerfile create mode 100644 LICENSE.txt create mode 100644 Makefile create mode 100644 Readme.md create mode 100644 manifest.tmpl create mode 100755 run.sh create mode 100644 tests/.gitignore create mode 100644 tests/docker-compose.yml create mode 100644 tests/expected/test.txt create mode 100644 tests/in/test.txt create mode 100755 tests/itest.sh diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..85ec4ac --- /dev/null +++ b/.drone.yml @@ -0,0 +1,100 @@ +--- +kind: pipeline +name: test + +steps: + + - name: check + image: iamthefij/drone-pre-commit:personal + environment: + SKIP: docker-compose-check + +--- +kind: pipeline +name: publish + +depends_on: + - test + +trigger: + event: + - push + - tag + refs: + - refs/heads/master + - refs/tags/v* + +steps: + + - name: push image - arm + image: plugins/docker + settings: + repo: iamthefij/gonfigurator + auto_tag: true + auto_tag_suffix: linux-arm + username: + from_secret: docker_username + password: + from_secret: docker_password + build_args: + - REPO=arm32v7 + + - name: push image - arm64 + image: plugins/docker + settings: + repo: iamthefij/gonfigurator + auto_tag: true + auto_tag_suffix: linux-arm64 + username: + from_secret: docker_username + password: + from_secret: docker_password + build_args: + - REPO=arm64v8 + + - name: push image - amd64 + image: plugins/docker + settings: + repo: iamthefij/gonfigurator + auto_tag: true + auto_tag_suffix: linux-amd64 + username: + from_secret: docker_username + password: + from_secret: docker_password + + - name: publish manifest + image: plugins/manifest + settings: + spec: manifest.tmpl + auto_tag: true + ignore_missing: true + username: + from_secret: docker_username + password: + from_secret: docker_password + +--- +kind: pipeline +name: notify + +depends_on: + - test + - publish + +trigger: + status: + - failure + +steps: + + - name: notify + image: drillster/drone-email + settings: + host: + from_secret: SMTP_HOST # pragma: whitelist secret + username: + from_secret: SMTP_USER # pragma: whitelist secret + password: + from_secret: SMTP_PASS # pragma: whitelist secret + from: drone@iamthefij.com diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..29a6c30 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,17 @@ +--- +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.4.0 + hooks: + - id: check-added-large-files + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-merge-conflict + - repo: https://github.com/IamTheFij/docker-pre-commit + rev: v2.0.0 + hooks: + - id: docker-compose-check + - repo: https://github.com/hadolint/hadolint + rev: v2.4.0 + hooks: + - id: hadolint diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..144c6af --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +ARG REPO=library +FROM ${REPO}/alpine:3.12 + +RUN apk add --no-cache ca-certificates~=20191127 gomplate~=3.7 bash~=5.0 + +COPY /run.sh /run.sh + +CMD ["/run.sh"] diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..54cf9b4 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Ian Fijolek + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2a8e08f --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +.PHONY: build +build: + docker build . + +.PHONY: build-arm +build-arm: + docker build --build-arg REPO=arm32v7 . + +.PHONY: build-arm64 +build-arm64: + docker build --build-arg REPO=arm64v8 . + +.PHONY: build-all +build-all: build build-arm build-arm64 + +.PHONY: itest +itest: + cd tests && ./itest.sh + +# Installs pre-commit hooks +.PHONY: install-hooks +install-hooks: + pre-commit install --install-hooks + +# Runs pre-commit checks on files +.PHONY: check +check: + pre-commit run --all-files diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..0503856 --- /dev/null +++ b/Readme.md @@ -0,0 +1,46 @@ +# Gonfigurator + +Runs `gomplate` to generate config files from a template folder in a way that allows them to be edited. + +On start, this container will look at all files in your `/templates` directory and template them and write them to the `/config` directory. Because some containers may need to modify these files and you may not want those changes blown away, this container will check it see if the template file is newer than the file residing in the `/config` path. If it is not, then the file will be skipped. + +## Usage + +```yaml +version: '3.8' +services: + main: + image: ... + volumes: + - config:/config + + config: + image: iamthefij/gonfigurator + volumes: + - config:/config + - ./templates + +volumes: + config: +``` + +You may override the `/templates` dir and the output `/config` with `$INPUT_DIR` and `$OUTPUT_DIR`, respectively. + +## Alternatives + +### Docker Config / Secrets +Files are read only and cannot be edited by application + +### Using gomplate only +All files will be overrwriten whenver the container starts + +Eg. +```yaml + config: + image: hairyhenderson/gomplate + command: [ + "--datasource", "secrets=/data/secrets.yml", + "--input-dir", "/templates", + "--output-dir", "/config" + ] +``` diff --git a/manifest.tmpl b/manifest.tmpl new file mode 100644 index 0000000..dbeac9e --- /dev/null +++ b/manifest.tmpl @@ -0,0 +1,25 @@ +image: iamthefij/gonfigurator:{{#if build.tag}}{{trimPrefix "v" build.tag}}{{else}}latest{{/if}} +{{#if build.tags}} +tags: +{{#each build.tags}} + - {{this}} +{{/each}} +{{/if}} +manifests: + - + image: iamthefij/gonfigurator:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-amd64 + platform: + architecture: amd64 + os: linux + - + image: iamthefij/gonfigurator:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm64 + platform: + architecture: arm64 + os: linux + variant: v8 + - + image: iamthefij/gonfigurator:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm + platform: + architecture: arm + os: linux + variant: v7 diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..b627d0d --- /dev/null +++ b/run.sh @@ -0,0 +1,25 @@ +#! /bin/bash + +INPUT_DIR=${INPUT_DIR:-/templates} +OUTPUT_DIR=${OUTPUT_DIR:-/config} + +shopt -s globstar + +echo "Running at $(date)..." + +for f in "$INPUT_DIR"/**/* ; do + of="$OUTPUT_DIR${f#$INPUT_DIR}" + if [ ! -f "$f" ]; then + echo " > Skipping: $f not a file" + continue + fi + if [ "$f" -nt "$of" ]; then + echo " > Generating new $of" + mkdir -p "$(dirname $of)" + gomplate $FLAGS -f "$f" -o "$of" + else + echo " > Skipping: $f because $of is newer" + fi +done + +echo "Complete" diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..1fcb152 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +out diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml new file mode 100644 index 0000000..5ef32f6 --- /dev/null +++ b/tests/docker-compose.yml @@ -0,0 +1,10 @@ +--- +version: "3.8" +services: + main: + build: .. + volumes: + - ./in:/templates + - ./out:/config + environment: + MY_VAR: friend diff --git a/tests/expected/test.txt b/tests/expected/test.txt new file mode 100644 index 0000000..4644341 --- /dev/null +++ b/tests/expected/test.txt @@ -0,0 +1 @@ +Hello friend! diff --git a/tests/in/test.txt b/tests/in/test.txt new file mode 100644 index 0000000..1dd71e9 --- /dev/null +++ b/tests/in/test.txt @@ -0,0 +1 @@ +Hello {{ .Env.MY_VAR }}! diff --git a/tests/itest.sh b/tests/itest.sh new file mode 100755 index 0000000..7f1f4a9 --- /dev/null +++ b/tests/itest.sh @@ -0,0 +1,22 @@ +#! /bin/bash + +set -ex + +# Clean output dir +rm -fr ./out + +# Generate output +docker-compose build +docker-compose up + +# Diff the results +diff ./out ./expected + +# Modify results +echo "Modified" > ./out/test.txt + +# Re-run +docker-compose up + +# Verify results do differ now +diff ./out ./expected && exit 1 || echo ok