Skip to content

Commit

Permalink
DEV: partially bake in env
Browse files Browse the repository at this point in the history
It may be useful for launcher2 build to bake in some env, allowing for default
env to run from a built image.

Introduce defaultBakeEnv as a list of env to bake into images. This allows for
folks pulling images to not need to mark every env variable, allowing for some
convenience in starting images.
  • Loading branch information
featheredtoast committed Sep 28, 2024
1 parent 9835070 commit 8546d96
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 4 deletions.
25 changes: 25 additions & 0 deletions launcher_go/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 launcher_go/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

0 comments on commit 8546d96

Please sign in to comment.