/
build.go
77 lines (64 loc) · 2.38 KB
/
build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package terraform
import (
"context"
"text/template"
"get.porter.sh/porter/pkg/exec/builder"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
)
const dockerfileLines = `
ENV PORTER_TERRAFORM_MIXIN_USER_AGENT_OPT_OUT="{{ .UserAgentOptOut}}"
ENV AZURE_HTTP_USER_AGENT="{{ .AzureUserAgent }}"
RUN --mount=type=cache,target=/var/cache/apt --mount=type=cache,target=/var/lib/apt \
apt-get update && apt-get install -y wget unzip && \
wget https://releases.hashicorp.com/terraform/{{.ClientVersion}}/terraform_{{.ClientVersion}}_linux_amd64.zip --progress=dot:giga && \
unzip terraform_{{.ClientVersion}}_linux_amd64.zip -d /usr/bin && \
rm terraform_{{.ClientVersion}}_linux_amd64.zip
COPY {{.WorkingDir}}/{{.InitFile}} $BUNDLE_DIR/{{.WorkingDir}}/
RUN cd $BUNDLE_DIR/{{.WorkingDir}} && \
terraform init -backend=false && \
rm -fr .terraform/providers && \
terraform providers mirror /usr/local/share/terraform/plugins
`
// BuildInput represents stdin passed to the mixin for the build command.
type BuildInput struct {
Config *MixinConfig
}
// MixinConfig represents configuration that can be set on the terraform mixin in porter.yaml
// mixins:
// - terraform:
// version: 0.12.17
type MixinConfig struct {
// ClientVersion is the version of the terraform CLI to install
ClientVersion string `yaml:"clientVersion,omitempty"`
// UserAgentOptOut allows a bundle author to opt out from adding porter and the mixin's version to the terraform user agent string.
UserAgentOptOut bool `yaml:"userAgentOptOut,omitempty"`
InitFile string `yaml:"initFile,omitempty"`
WorkingDir string `yaml:"workingDir,omitempty"`
}
type buildConfig struct {
MixinConfig
// AzureUserAgent is the contents of the azure user agent environment variable
AzureUserAgent string
}
func (m *Mixin) Build(ctx context.Context) error {
input := BuildInput{
Config: &m.config, // Apply config directly to the mixin
}
err := builder.LoadAction(ctx, m.RuntimeConfig, "", func(contents []byte) (interface{}, error) {
err := yaml.Unmarshal(contents, &input)
return &input, err
})
if err != nil {
return err
}
tmpl, err := template.New("Dockerfile").Parse(dockerfileLines)
if err != nil {
return errors.Wrapf(err, "error parsing terraform mixin Dockerfile template")
}
cfg := buildConfig{MixinConfig: *input.Config}
if !input.Config.UserAgentOptOut {
cfg.AzureUserAgent = m.userAgent
}
return tmpl.Execute(m.Out, cfg)
}