Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bake in default env for environment agnostic vars #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions v2/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ import (

const defaultBootCommand = "/sbin/boot"

var defaultBakeEnv = []string{
"RAILS_ENV",
"UNICORN_WORKERS",
"UNICORN_SIDEKIQS",
"RUBY_GC_HEAP_GROWTH_MAX_SLOTS",
"RUBY_GC_HEAP_INIT_SLOTS",
"RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR",
"CREATE_DB_ON_BOOT",
"MIGRATE_ON_BOOT",
"PRECOMPILE_ON_BOOT",
}

type Config struct {
Name string `yaml:-`
rawYaml []string
Expand Down Expand Up @@ -137,6 +149,8 @@ func (config *Config) Dockerfile(pupsArgs string, bakeEnv bool) string {
builder.WriteString(config.dockerfileArgs() + "\n")
if bakeEnv {
builder.WriteString(config.dockerfileEnvs() + "\n")
} else {
builder.WriteString(config.dockerfileDefaultEnvs() + "\n")
}
builder.WriteString(config.dockerfileExpose() + "\n")
builder.WriteString("COPY config.yaml /temp-config.yaml\n")
Expand Down Expand Up @@ -190,6 +204,17 @@ func (config *Config) dockerfileEnvs() string {
return strings.Join(builder, "\n")
}

func (config *Config) dockerfileDefaultEnvs() string {
builder := []string{}
for k, _ := range config.Env {
if slices.Contains(defaultBakeEnv, k) {
builder = append(builder, "ENV "+k+"=${"+k+"}")
}
}
slices.Sort(builder)
return strings.Join(builder, "\n")
}

func (config *Config) dockerfileArgs() string {
builder := []string{}
for k, _ := range config.Env {
Expand Down
92 changes: 88 additions & 4 deletions v2/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,95 @@ var _ = Describe("Config", func() {
Expect(string(out[:])).To(ContainSubstring("DISCOURSE_DEVELOPER_EMAILS: '[email protected],[email protected]'"))
})

It("can convert pups config to dockerfile format", func() {
It("can convert pups config to dockerfile format and bake in default env", func() {
dockerfile := conf.Dockerfile("", false)
Expect(dockerfile).To(ContainSubstring("ARG DISCOURSE_DEVELOPER_EMAILS"))
Expect(dockerfile).To(ContainSubstring("RUN cat /temp-config.yaml"))
Expect(dockerfile).To(ContainSubstring("EXPOSE 80"))
Expect(dockerfile).To(ContainSubstring(`FROM ${dockerfile_from_image}
ARG DISCOURSE_DB_HOST
ARG DISCOURSE_DB_PASSWORD
ARG DISCOURSE_DB_PORT
ARG DISCOURSE_DB_SOCKET
ARG DISCOURSE_DEVELOPER_EMAILS
ARG DISCOURSE_HOSTNAME
ARG DISCOURSE_REDIS_HOST
ARG DISCOURSE_SMTP_ADDRESS
ARG DISCOURSE_SMTP_PASSWORD
ARG DISCOURSE_SMTP_USER_NAME
ARG LANG
ARG LANGUAGE
ARG LC_ALL
ARG MULTI
ARG RAILS_ENV
ARG REPLACED
ARG RUBY_GC_HEAP_GROWTH_MAX_SLOTS
ARG RUBY_GC_HEAP_INIT_SLOTS
ARG RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR
ARG UNICORN_SIDEKIQS
ARG UNICORN_WORKERS
ENV RAILS_ENV=${RAILS_ENV}
ENV RUBY_GC_HEAP_GROWTH_MAX_SLOTS=${RUBY_GC_HEAP_GROWTH_MAX_SLOTS}
ENV RUBY_GC_HEAP_INIT_SLOTS=${RUBY_GC_HEAP_INIT_SLOTS}
ENV RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR=${RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR}
ENV UNICORN_SIDEKIQS=${UNICORN_SIDEKIQS}
ENV UNICORN_WORKERS=${UNICORN_WORKERS}
EXPOSE 443
EXPOSE 80
EXPOSE 90
COPY config.yaml /temp-config.yaml
RUN cat /temp-config.yaml | /usr/local/bin/pups --stdin && rm /temp-config.yaml
CMD ["/sbin/boot"]`))
})

It("can generate a dockerfile with all env baked into the image", func() {
dockerfile := conf.Dockerfile("", true)
Expect(dockerfile).To(ContainSubstring(`FROM ${dockerfile_from_image}
ARG DISCOURSE_DB_HOST
ARG DISCOURSE_DB_PASSWORD
ARG DISCOURSE_DB_PORT
ARG DISCOURSE_DB_SOCKET
ARG DISCOURSE_DEVELOPER_EMAILS
ARG DISCOURSE_HOSTNAME
ARG DISCOURSE_REDIS_HOST
ARG DISCOURSE_SMTP_ADDRESS
ARG DISCOURSE_SMTP_PASSWORD
ARG DISCOURSE_SMTP_USER_NAME
ARG LANG
ARG LANGUAGE
ARG LC_ALL
ARG MULTI
ARG RAILS_ENV
ARG REPLACED
ARG RUBY_GC_HEAP_GROWTH_MAX_SLOTS
ARG RUBY_GC_HEAP_INIT_SLOTS
ARG RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR
ARG UNICORN_SIDEKIQS
ARG UNICORN_WORKERS
ENV DISCOURSE_DB_HOST=${DISCOURSE_DB_HOST}
ENV DISCOURSE_DB_PASSWORD=${DISCOURSE_DB_PASSWORD}
ENV DISCOURSE_DB_PORT=${DISCOURSE_DB_PORT}
ENV DISCOURSE_DB_SOCKET=${DISCOURSE_DB_SOCKET}
ENV DISCOURSE_DEVELOPER_EMAILS=${DISCOURSE_DEVELOPER_EMAILS}
ENV DISCOURSE_HOSTNAME=${DISCOURSE_HOSTNAME}
ENV DISCOURSE_REDIS_HOST=${DISCOURSE_REDIS_HOST}
ENV DISCOURSE_SMTP_ADDRESS=${DISCOURSE_SMTP_ADDRESS}
ENV DISCOURSE_SMTP_PASSWORD=${DISCOURSE_SMTP_PASSWORD}
ENV DISCOURSE_SMTP_USER_NAME=${DISCOURSE_SMTP_USER_NAME}
ENV LANG=${LANG}
ENV LANGUAGE=${LANGUAGE}
ENV LC_ALL=${LC_ALL}
ENV MULTI=${MULTI}
ENV RAILS_ENV=${RAILS_ENV}
ENV REPLACED=${REPLACED}
ENV RUBY_GC_HEAP_GROWTH_MAX_SLOTS=${RUBY_GC_HEAP_GROWTH_MAX_SLOTS}
ENV RUBY_GC_HEAP_INIT_SLOTS=${RUBY_GC_HEAP_INIT_SLOTS}
ENV RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR=${RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR}
ENV UNICORN_SIDEKIQS=${UNICORN_SIDEKIQS}
ENV UNICORN_WORKERS=${UNICORN_WORKERS}
EXPOSE 443
EXPOSE 80
EXPOSE 90
COPY config.yaml /temp-config.yaml
RUN cat /temp-config.yaml | /usr/local/bin/pups --stdin && rm /temp-config.yaml
CMD ["/sbin/boot"]`))
})

Context("hostname tests", func() {
Expand Down
Loading