diff options
author | Dan Duvall <dduvall@wikimedia.org> | 2017-04-19 11:41:28 -0700 |
---|---|---|
committer | Dan Duvall <dduvall@wikimedia.org> | 2017-04-19 11:41:28 -0700 |
commit | 82e80325b9def5054c466c2d44b1a031e0eeab68 (patch) | |
tree | 696312c1a5a7989efb409b69fc01d0b1691eee95 /docker | |
parent | adf22c9e88276abb61317ee66ec807328ffd3363 (diff) | |
download | blubber-82e80325b9def5054c466c2d44b1a031e0eeab68.tar.gz |
Support compilation to multi-stage Dockerfile
Reorganized config type declarations.
Added `RunConfig` for declaring working directory and unprivileged
runtime user. A system for enforcing restrictions should probably be
implemented for this.
Implemented Dockerfile compiler, supporting multi-stage output in cases
whether artifacts from other variants are configured.
Diffstat (limited to 'docker')
-rw-r--r-- | docker/compiler.go | 69 |
1 files changed, 66 insertions, 3 deletions
diff --git a/docker/compiler.go b/docker/compiler.go index dd92a0b..0f6cd92 100644 --- a/docker/compiler.go +++ b/docker/compiler.go @@ -2,12 +2,75 @@ package docker import ( "bytes" - "fmt" "github.com/marxarelli/blubber/config" ) -func Compile(cfg config.ConfigType, variant string) []byte { +func Compile(cfg *config.Config, variant string) *bytes.Buffer { buffer := new(bytes.Buffer) - buffer.WriteString("FROM ") + vcfg, err := config.ExpandVariant(cfg, variant) + + if err == nil { + // write multi-stage sections for each artifact dependency + for _, artifact := range vcfg.Artifacts { + if artifact.From != "" { + dependency, err := config.ExpandVariant(cfg, artifact.From) + + if err == nil { + CompileStage(buffer, artifact.From, dependency) + } + } + } + + CompileStage(buffer, variant, vcfg) + } + + return buffer +} + +func CompileStage(buffer *bytes.Buffer, stage string, vcfg *config.VariantConfig) { + Writeln(buffer, "FROM ", vcfg.Base, " AS ", stage) + + Writeln(buffer, "USER root") + Writeln(buffer, "WORKDIR /srv") + CompileToCommands(buffer, vcfg.Apt) + CompileToCommands(buffer, vcfg.Run) + + if vcfg.Run.As != "" { + Writeln(buffer, "USER ", vcfg.Run.As) + } + + if vcfg.Run.In != "" { + Writeln(buffer, "WORKDIR ", vcfg.Run.In) + } + + CompileToCommands(buffer, vcfg.Npm) + + // Artifact copying + for _, artifact := range vcfg.Artifacts { + Write(buffer, "COPY ") + + if artifact.From != "" { + Write(buffer, "--from=", artifact.From, " ") + } + + Writeln(buffer, artifact.Source, " ", artifact.Destination) + } +} + +func CompileToCommands(buffer *bytes.Buffer, compileable config.CommandCompileable) { + for _, command := range compileable.Commands() { + Writeln(buffer, "RUN ", command) + } +} + +func Write(buffer *bytes.Buffer, strings ...string) { + for _, str := range strings { + buffer.WriteString(str) + } +} + +func Writeln(buffer *bytes.Buffer, strings ...string) { + Write(buffer, strings...) + buffer.WriteString("\n") } |