This repository has been archived by the owner on Jul 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
post-processor.go
156 lines (133 loc) · 4.06 KB
/
post-processor.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"log"
"reflect"
"strings"
"text/template"
"github.com/mitchellh/packer/builder/docker"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/helper/config"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/post-processor/docker-tag"
"github.com/mitchellh/packer/template/interpolate"
)
const BuilderId = "packer.post-processor.docker-dockerfile"
type Config struct {
common.PackerConfig `mapstructure:",squash"`
From string
Maintainer string `mapstructure:"maintainer"`
Cmd interface{} `mapstructure:"cmd"`
Label map[string]string `mapstructure:"label"`
Expose []string `mapstructure:"expose"`
Env map[string]string `mapstructure:"env"`
Entrypoint interface{} `mapstructure:"entrypoint"`
Volume []string `mapstructure:"volume"`
User string `mapstructure:"user"`
WorkDir string `mapstructure:"workdir"`
ctx interpolate.Context
}
type PostProcessor struct {
Driver Driver
config Config
}
func (p *PostProcessor) Configure(raws ...interface{}) error {
err := config.Decode(&p.config, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &p.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{},
},
}, raws...)
if err != nil {
return err
}
return nil
}
func (p *PostProcessor) processVar(v interface{}) (string, error) {
switch t := v.(type) {
case []string:
a := make([]string, 0, len(t))
for _, item := range t {
a = append(a, item)
}
r, _ := json.Marshal(a)
return string(r), nil
case []interface{}:
a := make([]string, 0, len(t))
for _, item := range t {
a = append(a, item.(string))
}
r, _ := json.Marshal(a)
return string(r), nil
case string:
return t, nil
case nil:
return "", nil
}
return "", fmt.Errorf("Unsupported variable type: %s", reflect.TypeOf(v))
}
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
if artifact.BuilderId() != dockertag.BuilderId {
err := fmt.Errorf(
"Unknown artifact type: %s\nCan only build with Dockerfile from Docker tag artifacts.",
artifact.BuilderId())
return nil, false, err
}
p.config.From = artifact.Id()
template_str := `FROM {{ .From }}
{{ if .Maintainer }}MAINTAINER {{ .Maintainer }}
{{ end }}{{ if .Cmd }}CMD {{ process .Cmd }}
{{ end }}{{ if .Label }}{{ range $k, $v := .Label }}LABEL "{{ $k }}"="{{ $v }}"
{{ end }}{{ end }}{{ if .Expose }}EXPOSE {{ join .Expose " " }}
{{ end }}{{ if .Env }}{{ range $k, $v := .Env }}ENV {{ $k }} {{ $v }}
{{ end }}{{ end }}{{ if .Entrypoint }}ENTRYPOINT {{ process .Entrypoint }}
{{ end }}{{ if .Volume }}VOLUME {{ process .Volume }}
{{ end }}{{ if .User }}USER {{ .User }}
{{ end }}{{ if .WorkDir }}WORKDIR {{ .WorkDir }}{{ end }}`
dockerfile := new(bytes.Buffer)
template_writer := bufio.NewWriter(dockerfile)
tmpl, err := template.New("Dockerfile").Funcs(template.FuncMap{
"process": p.processVar,
"join": strings.Join,
}).Parse(template_str)
if err != nil {
return nil, false, err
}
err = tmpl.Execute(template_writer, p.config)
if err != nil {
return nil, false, err
}
template_writer.Flush()
log.Printf("Dockerfile:\n%s", dockerfile.String())
driver := p.Driver
if driver == nil {
// If no driver is set, then we use the real driver
driver = &DockerDriver{&docker.DockerDriver{Ctx: &p.config.ctx, Ui: ui}}
}
ui.Message("Building image from Dockerfile")
id, err := driver.BuildImage(dockerfile)
if err != nil {
return nil, false, err
}
ui.Message("Destroying previously tagged image: " + p.config.From)
err = artifact.Destroy()
if err != nil {
return nil, false, err
}
ui.Message("Tagging new image, " + id + ", as " + p.config.From)
err = driver.TagImage(id, p.config.From, false)
if err != nil {
return nil, false, err
}
// Build the artifact
artifact = &docker.ImportArtifact{
BuilderIdValue: dockertag.BuilderId,
Driver: driver,
IdValue: p.config.From,
}
return artifact, true, nil
}