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 /config | |
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 'config')
-rw-r--r-- | config/apt.go | 28 | ||||
-rw-r--r-- | config/artifacts.go | 7 | ||||
-rw-r--r-- | config/common.go | 23 | ||||
-rw-r--r-- | config/config.go | 10 | ||||
-rw-r--r-- | config/npm.go | 36 | ||||
-rw-r--r-- | config/run.go | 44 | ||||
-rw-r--r-- | config/types.go | 54 | ||||
-rw-r--r-- | config/variant.go | 12 |
8 files changed, 160 insertions, 54 deletions
diff --git a/config/apt.go b/config/apt.go new file mode 100644 index 0000000..e6ad22c --- /dev/null +++ b/config/apt.go @@ -0,0 +1,28 @@ +package config + +import ( + "bytes" + "strings" +) + +type AptConfig struct { + Packages []string `json:packages` +} + +func (apt *AptConfig) Merge(apt2 AptConfig) { + apt.Packages = append(apt.Packages, apt2.Packages...) +} + +func (apt AptConfig) Commands() []string { + if len(apt.Packages) < 1 { + return []string{} + } + + buffer := new(bytes.Buffer) + + buffer.WriteString("apt-get update && apt-get install -y ") + buffer.WriteString(strings.Join(apt.Packages, " ")) + buffer.WriteString(" && rm -rf /var/lib/apt/lists/*") + + return []string{buffer.String()} +} diff --git a/config/artifacts.go b/config/artifacts.go new file mode 100644 index 0000000..78b6276 --- /dev/null +++ b/config/artifacts.go @@ -0,0 +1,7 @@ +package config + +type ArtifactsConfig struct { + From string `json:from` + Source string `json:source` + Destination string `json:destination` +} diff --git a/config/common.go b/config/common.go new file mode 100644 index 0000000..ae2e438 --- /dev/null +++ b/config/common.go @@ -0,0 +1,23 @@ +package config + +type CommonConfig struct { + Base string `json:base` + Apt AptConfig `json:apt` + Npm NpmConfig `json:npm` + Run RunConfig `json:run` + EntryPoint []string `json:entrypoint` +} + +func (cc1 *CommonConfig) Merge(cc2 CommonConfig) { + if cc2.Base != "" { + cc1.Base = cc2.Base + } + + cc1.Apt.Merge(cc2.Apt) + cc1.Npm.Merge(cc2.Npm) + cc1.Run.Merge(cc2.Run) + + if len(cc1.EntryPoint) < 1 { + cc1.EntryPoint = cc2.EntryPoint + } +} diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..36eb6e4 --- /dev/null +++ b/config/config.go @@ -0,0 +1,10 @@ +package config + +type Config struct { + CommonConfig + Variants map[string]VariantConfig `json:variants` +} + +type CommandCompileable interface { + Commands() []string +} diff --git a/config/npm.go b/config/npm.go new file mode 100644 index 0000000..d563045 --- /dev/null +++ b/config/npm.go @@ -0,0 +1,36 @@ +package config + +import ( + "bytes" +) + +type NpmConfig struct { + Install bool `json:install` + Env string `json:env` +} + +func (npm *NpmConfig) Merge(npm2 NpmConfig) { + npm.Install = npm.Install || npm2.Install + + if npm2.Env != "" { + npm.Env = npm2.Env + } +} + +func (npm NpmConfig) Commands() []string { + if !npm.Install { + return []string{} + } + + buffer := new(bytes.Buffer) + + buffer.WriteString("npm install") + + if npm.Env == "production" { + buffer.WriteString(" --production") + } + + buffer.WriteString(" && npm dedupe") + + return []string{buffer.String()} +} diff --git a/config/run.go b/config/run.go new file mode 100644 index 0000000..c3ffd08 --- /dev/null +++ b/config/run.go @@ -0,0 +1,44 @@ +package config + +import ( + "strconv" + "strings" +) + +type RunConfig struct { + In string `json:in` + As string `json:as` + Uid int `json:uid` + Gid int `json:gid` +} + +func (run *RunConfig) Merge(run2 RunConfig) { + if run2.In != "" { run.In = run2.In } + if run2.As != "" { run.As = run2.As } + if run2.Uid != 0 { run.Uid = run2.Uid } + if run2.Gid != 0 { run.Gid = run2.Gid } +} + +func (run RunConfig) Commands() []string { + cmds := []string{} + + if run.In != "" { + cmds = append(cmds, strings.Join([]string{"mkdir -p", run.In}, " ")) + } + + if run.As != "" { + cmd := []string{ + "groupadd -o -g", strconv.Itoa(run.Gid), "-r", run.As, "&&", + "useradd -o -m -r -g", run.As, "-u", strconv.Itoa(run.Uid), run.As, + } + + cmds = append(cmds, strings.Join(cmd, " ")) + + if run.In != "" { + owner := strings.Join([]string{run.As, ":", run.As}, "") + cmds = append(cmds, strings.Join([]string{"chown", owner, run.In}, " ")) + } + } + + return cmds +} diff --git a/config/types.go b/config/types.go deleted file mode 100644 index 9fb742c..0000000 --- a/config/types.go +++ /dev/null @@ -1,54 +0,0 @@ -package config - -type Config struct { - CommonConfig - Variants map[string]VariantConfig `json:variants` -} - -type CommonConfig struct { - Base string `json:base` - Apt AptConfig `json:apt` - Npm NpmConfig `json:npm` - EntryPoint []string `json:entrypoint` -} - -type AptConfig struct { - Packages []string `json:packages` -} - -type NpmConfig struct { - Use string `json:use` -} - -type VariantConfig struct { - Includes []string `json:includes` - Artifacts []ArtifactsConfig `json:artifacts` - CommonConfig -} - -type ArtifactsConfig struct { - From string `json:from` - Source string `json:source` - Destination string `json:destination` -} - -func (vc1 *VariantConfig) Merge(vc2 VariantConfig) { - vc1.Artifacts = append(vc1.Artifacts, vc2.Artifacts...) - vc1.CommonConfig.Merge(vc2.CommonConfig) -} - -func (cc1 *CommonConfig) Merge(cc2 CommonConfig) { - if cc1.Base == "" { - cc1.Base = cc2.Base - } - - cc1.Apt.Packages = append(cc1.Apt.Packages, cc2.Apt.Packages...) - - if cc1.Npm.Use == "" { - cc1.Npm.Use = cc2.Npm.Use - } - - if len(cc1.EntryPoint) < 1 { - cc1.EntryPoint = cc2.EntryPoint - } -} diff --git a/config/variant.go b/config/variant.go new file mode 100644 index 0000000..d1640bb --- /dev/null +++ b/config/variant.go @@ -0,0 +1,12 @@ +package config + +type VariantConfig struct { + Includes []string `json:includes` + Artifacts []ArtifactsConfig `json:artifacts` + CommonConfig +} + +func (vc1 *VariantConfig) Merge(vc2 VariantConfig) { + vc1.Artifacts = append(vc1.Artifacts, vc2.Artifacts...) + vc1.CommonConfig.Merge(vc2.CommonConfig) +} |