From 5eed8506e9d3a7e037fe9949bc74e9607415dbb0 Mon Sep 17 00:00:00 2001 From: Marcos Oliveira Date: Thu, 2 Jan 2020 14:53:03 -0800 Subject: [PATCH 1/5] fix Makefile --- Makefile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 8ff0fe3..1f14fe0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ bin/docker_entrypoint: bin - cd go/cmd && go build -o $@ + cd go/cmd && go build -o ../../$@ bin: mkdir bin @@ -10,5 +10,7 @@ install: docker: docker build --no-cache -t docker_entrypoint . - -.PHONY: install docker +clean: + rm -rf bin \ + docker rmi docker_entrypoint:latest +.PHONY: install docker clean From 7a2d86f2e8530e1b11a852cbe949e4675ced3d8e Mon Sep 17 00:00:00 2001 From: Marcos Oliveira Date: Thu, 2 Jan 2020 22:17:55 -0800 Subject: [PATCH 2/5] add integration test, for real --- go/render/templatebuilder.go | 26 ++++++++++++++++------ go/render/templatebuilder_test.go | 6 ++--- test/Dockerfile.test | 10 ++++++++- test/docker-compose.yml | 9 +++++--- test/etc/server/server | 3 +++ test/etc/server/server.properties.template | 3 +++ vai.sh | 2 +- 7 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 test/etc/server/server create mode 100644 test/etc/server/server.properties.template diff --git a/go/render/templatebuilder.go b/go/render/templatebuilder.go index 94ab9e7..81002d2 100644 --- a/go/render/templatebuilder.go +++ b/go/render/templatebuilder.go @@ -14,12 +14,17 @@ var templateExtension = ".template" func ExecTemplates(paths ...string) error { for _, path := range paths { - fmt.Println(path) if !strings.HasSuffix(path, templateExtension) { return fmt.Errorf("%s is not a .template file", path) } destPath := removeExtension(path) - envVars := getEnvironVars(replaceWithUnderscore(destPath)) + + filterString := strings.ToUpper(replaceWithUnderscore(destPath)) + envVars, err := getEnvironVars(filterString) + if err != nil { + return fmt.Errorf("error getting environment variables for %s: %s", filterString, err) + } + t := template.New(path, destPath, envVars) if err := t.Write(); err != nil { return fmt.Errorf("error rendering template %s: %s", path, err) @@ -38,14 +43,21 @@ func removeExtension(path string) string { return path[:len(path)-len(templateExtension)] } -func getEnvironVars(filterString string) map[string]string { - regex, _ := regexp.Compile("^" + filterString + "_") +func getEnvironVars(filterString string) (map[string]string, error) { + prefix := fmt.Sprintf("%s_", filterString) + filter := fmt.Sprintf("^%s", prefix) + regex, err := regexp.Compile(filter) + + if err != nil { + return nil, err + } + vars := make(map[string]string) for _, e := range os.Environ() { - pair := strings.SplitAfterN(e, "=", 2) + pair := strings.Split(e, "=") if regex.MatchString(pair[0]) { - vars[pair[0][len(filterString)+1:len(pair[0])-1]] = pair[1] + vars[pair[0][len(prefix):]] = pair[1] } } - return vars + return vars, nil } diff --git a/go/render/templatebuilder_test.go b/go/render/templatebuilder_test.go index f381586..2e63406 100644 --- a/go/render/templatebuilder_test.go +++ b/go/render/templatebuilder_test.go @@ -5,10 +5,10 @@ import ( "testing" ) -func TestGetEnvironVars(t*testing.T){ +func TestGetEnvironVars(t *testing.T) { _ = os.Setenv("A_B_C", "1") - vars := getEnvironVars("A") - if vars["B_C"] != "1"{ + vars, _ := getEnvironVars("A") + if vars["B_C"] != "1" { t.Fatalf("%v", vars) } } diff --git a/test/Dockerfile.test b/test/Dockerfile.test index 8125785..9912e54 100644 --- a/test/Dockerfile.test +++ b/test/Dockerfile.test @@ -1,6 +1,14 @@ FROM golang:1.13-alpine WORKDIR /src/ +COPY ./go /src -CMD ls -l /src/ +WORKDIR go +RUN go build -o /docker_entrypoint github.com/bejelith/docker_entrypoint/cmd + +ENV SERVER_PROPERTIES_LISTEN_ADDRESS="localhost" +ENV SERVER_PROPERTIES_LISTEN_PORT=8080 +ENV SERVER_PROPERTIES_ID=1 + +WORKDIR /src/ CMD go test ./... -v diff --git a/test/docker-compose.yml b/test/docker-compose.yml index a1d4442..d83d834 100644 --- a/test/docker-compose.yml +++ b/test/docker-compose.yml @@ -7,7 +7,10 @@ networks: services: docker_entrypoint_tests: build: - context: . - dockerfile: ./Dockerfile.test + context: .. + dockerfile: ./test/Dockerfile.test + docker_entrypoint_integration: + image: test_docker_entrypoint_tests + command: ["/docker_entrypoint","-template","/etc/server/server.properties.template"] volumes: - - $PWD/../go:/src + - $PWD/etc/server:/etc/server diff --git a/test/etc/server/server b/test/etc/server/server new file mode 100644 index 0000000..e7dc255 --- /dev/null +++ b/test/etc/server/server @@ -0,0 +1,3 @@ +server_address=localhost +server_port=8080 +id=1 diff --git a/test/etc/server/server.properties.template b/test/etc/server/server.properties.template new file mode 100644 index 0000000..46b9981 --- /dev/null +++ b/test/etc/server/server.properties.template @@ -0,0 +1,3 @@ +server_address={{ .LISTEN_ADDRESS }} +server_port={{ .LISTEN_PORT }} +id={{ .ID }} diff --git a/vai.sh b/vai.sh index b5a34fa..fa60664 100755 --- a/vai.sh +++ b/vai.sh @@ -4,4 +4,4 @@ set -euxo pipefail cd ./test -docker-compose up --build --abort-on-container-exit +docker-compose up --build From 8a415d8488219d83874d0b22b02faf93660f06ae Mon Sep 17 00:00:00 2001 From: Marcos Oliveira Date: Fri, 3 Jan 2020 11:10:40 -0800 Subject: [PATCH 3/5] fix extra chompchomp, separate tests run --- .gitignore | 1 + go/template/template.go | 4 ++-- go/template/template_test.go | 6 +++--- test/docker-compose.yml | 2 +- vai.sh | 3 ++- 5 files changed, 9 insertions(+), 7 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd304ec --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +test/etc/server/server.properties diff --git a/go/template/template.go b/go/template/template.go index 0978942..b0668f4 100644 --- a/go/template/template.go +++ b/go/template/template.go @@ -16,7 +16,7 @@ func New(templatePath string, outputPath string, vars map[string]string) Templat t := Template{ vars, templatePath, - trimExtension(outputPath), + outputPath, } return t } @@ -44,4 +44,4 @@ func (t *Template) WriteToPath(path string) error { func trimExtension(path string) string { var extension = filepath.Ext(path) return path[0 : len(path)-len(extension)] -} \ No newline at end of file +} diff --git a/go/template/template_test.go b/go/template/template_test.go index b6a9934..6f309d6 100644 --- a/go/template/template_test.go +++ b/go/template/template_test.go @@ -23,10 +23,10 @@ func TestMain(m *testing.M) { func mockFile(content []byte) (string, error) { if tempFile, err := os.Create(tempDir + "/file.temp"); err == nil { - defer func (){ _ = tempFile.Close() }() + defer func() { _ = tempFile.Close() }() _, err = tempFile.Write(content) return tempFile.Name(), err - }else{ + } else { return "", err } } @@ -42,7 +42,7 @@ func TestTemplate(t *testing.T) { t.Fatal(err) } vars := map[string]string{ - testVar : testVal, + testVar: testVal, } template := New(templatePath, templatePath, vars) if err := template.WriteToPath(resultPath); err != nil { diff --git a/test/docker-compose.yml b/test/docker-compose.yml index d83d834..58619e9 100644 --- a/test/docker-compose.yml +++ b/test/docker-compose.yml @@ -11,6 +11,6 @@ services: dockerfile: ./test/Dockerfile.test docker_entrypoint_integration: image: test_docker_entrypoint_tests - command: ["/docker_entrypoint","-template","/etc/server/server.properties.template"] + command: ["/docker_entrypoint","-template","/etc/server/server.properties.template","/bin/cat","/etc/server/server.properties"] volumes: - $PWD/etc/server:/etc/server diff --git a/vai.sh b/vai.sh index fa60664..1cde9d5 100755 --- a/vai.sh +++ b/vai.sh @@ -4,4 +4,5 @@ set -euxo pipefail cd ./test -docker-compose up --build +docker-compose up --build --abort-on-container-exit --scale=docker_entrypoint_integration=0 +docker-compose up --build --exit-code-from docker_entrypoint_integration --scale docker_entrypoint_tests=0 From 870bfd322a6a3185046ef5aa019f93b8eb4b2051 Mon Sep 17 00:00:00 2001 From: Marcos Oliveira Date: Fri, 3 Jan 2020 11:13:59 -0800 Subject: [PATCH 4/5] fix typo --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index fe5240c..adf2aae 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,16 @@ # docker_entrypoint Templating engine for docker images -Did you have to spend time in finding out to configure a random docker image you just downloaded from Docker registry? +Did you have to spend time in finding out to configure a random docker image you just downloaded from Docker registry? -Wouldn't be nice having the same ENV variable be the same across images without the need of replacing entirelly the whole congiguration file or having to dig `docker inspect` and just run an help command? +Wouldn't be nice having the same ENV variable be the same across images without the need of replacing entirelly the whole configuration file or having to dig `docker inspect` and just run an help command? -Docker entrypoint manages your configuration file templates rendering them from environemnt variables values. +Docker entrypoint manages your configuration file templates rendering them from environemnt variables values. -For example, setting Kafka's broker ID would be as simple as setting an environment variable +For example, setting Kafka's broker ID would be as simple as setting an environment variable `docker run -d --name kafka -e SERVER_PROPERPERTIES_BROKER_ID = 1 mykafka-image` -and gives users an easy way to access all configuration variables with a `help` command: +and gives users an easy way to access all configuration variables with a `help` command: `docker run -ti --rm mykafka-image --help ## Dependencies @@ -23,20 +23,20 @@ to build the base docker_entrypoing image run ## Usage ### Build a configuration file -Create a template configuration file `server.properties.template` like the following +Create a template configuration file `server.properties.template` like the following ``` server_address={{ .LISTEN_ADDRESS }} server_port={{ .LISTEN_PORT }} id={{ .ID }} ``` ### Example dockerfile -The build your image using docker_entrypoint as first stage of a multi-stage build: +The build your image using docker_entrypoint as first stage of a multi-stage build: ``` FROM docker_entrypoint as entrypoint FROM alpine #OR YOU FAVOURITE BASE IMAGE COPY --from=entrypoint /docker_entrypoint /docker_entrypoint RUN ... -MKDIR ... +RUN mkdir ... ENV SERVER_PROPERTIES_LISTEN_ADDRESS="localhost" ENV SERVER_PROPERTIES_LISTEN_PORT=8080 ENV SERVER_PROPERTIES_ID=1 From ecee45b230af2fd6b4f0e05ee204a5e0328c097d Mon Sep 17 00:00:00 2001 From: Marcos Oliveira Date: Fri, 3 Jan 2020 11:24:00 -0800 Subject: [PATCH 5/5] remote extra file --- test/etc/server/server | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 test/etc/server/server diff --git a/test/etc/server/server b/test/etc/server/server deleted file mode 100644 index e7dc255..0000000 --- a/test/etc/server/server +++ /dev/null @@ -1,3 +0,0 @@ -server_address=localhost -server_port=8080 -id=1