Initial commit

This commit is contained in:
IamTheFij 2021-05-14 15:31:31 -07:00
commit 03406f4105
13 changed files with 305 additions and 0 deletions

100
.drone.yml Normal file
View File

@ -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

17
.pre-commit-config.yaml Normal file
View File

@ -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

8
Dockerfile Normal file
View File

@ -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"]

21
LICENSE.txt Normal file
View File

@ -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.

28
Makefile Normal file
View File

@ -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

46
Readme.md Normal file
View File

@ -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"
]
```

25
manifest.tmpl Normal file
View File

@ -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

25
run.sh Executable file
View File

@ -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"

1
tests/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
out

10
tests/docker-compose.yml Normal file
View File

@ -0,0 +1,10 @@
---
version: "3.8"
services:
main:
build: ..
volumes:
- ./in:/templates
- ./out:/config
environment:
MY_VAR: friend

1
tests/expected/test.txt Normal file
View File

@ -0,0 +1 @@
Hello friend!

1
tests/in/test.txt Normal file
View File

@ -0,0 +1 @@
Hello {{ .Env.MY_VAR }}!

22
tests/itest.sh Executable file
View File

@ -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