File: post-processor.go

package info (click to toggle)
packer 1.6.6%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 32,016 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (344 lines) | stat: -rw-r--r-- 9,706 bytes parent folder | download | duplicates (2)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//go:generate mapstructure-to-hcl2 -type Config

// vagrant implements the packersdk.PostProcessor interface and adds a
// post-processor that turns artifacts of known builders into Vagrant
// boxes.
package vagrant

import (
	"compress/flate"
	"context"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"text/template"

	"github.com/hashicorp/hcl/v2/hcldec"
	"github.com/hashicorp/packer/packer-plugin-sdk/common"
	packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
	"github.com/hashicorp/packer/packer-plugin-sdk/template/config"
	"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
	"github.com/hashicorp/packer/packer-plugin-sdk/tmp"
	"github.com/hashicorp/packer/post-processor/artifice"
	"github.com/mitchellh/mapstructure"
)

var builtins = map[string]string{
	"mitchellh.amazonebs":                 "aws",
	"mitchellh.amazon.instance":           "aws",
	"mitchellh.virtualbox":                "virtualbox",
	"mitchellh.vmware":                    "vmware",
	"mitchellh.vmware-esx":                "vmware",
	"pearkes.digitalocean":                "digitalocean",
	"packer.googlecompute":                "google",
	"hashicorp.scaleway":                  "scaleway",
	"packer.parallels":                    "parallels",
	"MSOpenTech.hyperv":                   "hyperv",
	"transcend.qemu":                      "libvirt",
	"ustream.lxc":                         "lxc",
	"Azure.ResourceManagement.VMImage":    "azure",
	"packer.post-processor.docker-import": "docker",
	"packer.post-processor.docker-tag":    "docker",
	"packer.post-processor.docker-push":   "docker",
}

func availableProviders() []string {
	dedupedProvidersMap := map[string]string{}

	for _, v := range builtins {
		dedupedProvidersMap[v] = v
	}

	dedupedProviders := []string{}
	for k := range dedupedProvidersMap {
		dedupedProviders = append(dedupedProviders, k)
	}

	return dedupedProviders
}

type Config struct {
	common.PackerConfig `mapstructure:",squash"`

	CompressionLevel             int      `mapstructure:"compression_level"`
	Include                      []string `mapstructure:"include"`
	OutputPath                   string   `mapstructure:"output"`
	Override                     map[string]interface{}
	VagrantfileTemplate          string `mapstructure:"vagrantfile_template"`
	VagrantfileTemplateGenerated bool   `mapstructure:"vagrantfile_template_generated"`
	ProviderOverride             string `mapstructure:"provider_override"`

	ctx interpolate.Context
}

type PostProcessor struct {
	config Config
}

func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec {
	return p.config.FlatMapstructure().HCL2Spec()
}

func (p *PostProcessor) Configure(raws ...interface{}) error {
	if err := p.configureSingle(&p.config, raws...); err != nil {
		return err
	}

	if p.config.ProviderOverride != "" {
		validOverride := false
		providers := availableProviders()
		for _, prov := range providers {
			if prov == p.config.ProviderOverride {
				validOverride = true
				break
			}
		}
		if !validOverride {
			return fmt.Errorf("The given provider_override %s is not valid. "+
				"Please choose from one of %s", p.config.ProviderOverride,
				strings.Join(providers, ", "))
		}
	}
	return nil
}

func (p *PostProcessor) PostProcessProvider(name string, provider Provider, ui packersdk.Ui, artifact packersdk.Artifact) (packersdk.Artifact, bool, error) {
	config, err := p.specificConfig(name)
	if err != nil {
		return nil, false, err
	}

	err = CreateDummyBox(ui, config.CompressionLevel)
	if err != nil {
		return nil, false, err
	}

	ui.Say(fmt.Sprintf("Creating Vagrant box for '%s' provider", name))

	var generatedData map[interface{}]interface{}
	stateData := artifact.State("generated_data")
	if stateData != nil {
		// Make sure it's not a nil map so we can assign to it later.
		generatedData = stateData.(map[interface{}]interface{})
	}
	// If stateData has a nil map generatedData will be nil
	// and we need to make sure it's not
	if generatedData == nil {
		generatedData = make(map[interface{}]interface{})
	}
	generatedData["ArtifactId"] = artifact.Id()
	generatedData["BuildName"] = config.PackerBuildName
	generatedData["Provider"] = name
	config.ctx.Data = generatedData

	outputPath, err := interpolate.Render(config.OutputPath, &config.ctx)
	if err != nil {
		return nil, false, err
	}

	// Create a temporary directory for us to build the contents of the box in
	dir, err := tmp.Dir("packer")
	if err != nil {
		return nil, false, err
	}
	defer os.RemoveAll(dir)

	// Copy all of the includes files into the temporary directory
	for _, src := range config.Include {
		ui.Message(fmt.Sprintf("Copying from include: %s", src))
		dst := filepath.Join(dir, filepath.Base(src))
		if err := CopyContents(dst, src); err != nil {
			err = fmt.Errorf("Error copying include file: %s\n\n%s", src, err)
			return nil, false, err
		}
	}

	// Run the provider processing step
	vagrantfile, metadata, err := provider.Process(ui, artifact, dir)
	if err != nil {
		return nil, false, err
	}

	// Write the metadata we got
	if err := WriteMetadata(dir, metadata); err != nil {
		return nil, false, err
	}

	// Write our Vagrantfile
	var customVagrantfile string
	if config.VagrantfileTemplate != "" {
		ui.Message(fmt.Sprintf("Using custom Vagrantfile: %s", config.VagrantfileTemplate))
		customBytes, err := ioutil.ReadFile(config.VagrantfileTemplate)
		if err != nil {
			return nil, false, err
		}

		customVagrantfile, err = interpolate.Render(string(customBytes), &config.ctx)
		if err != nil {
			return nil, false, err
		}
	}

	f, err := os.Create(filepath.Join(dir, "Vagrantfile"))
	if err != nil {
		return nil, false, err
	}

	t := template.Must(template.New("root").Parse(boxVagrantfileContents))
	err = t.Execute(f, &vagrantfileTemplate{
		ProviderVagrantfile: vagrantfile,
		CustomVagrantfile:   customVagrantfile,
	})
	f.Close()
	if err != nil {
		return nil, false, err
	}

	// Create the box
	if err := DirToBox(outputPath, dir, ui, config.CompressionLevel); err != nil {
		return nil, false, err
	}

	return NewArtifact(name, outputPath), provider.KeepInputArtifact(), nil
}

func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifact packersdk.Artifact) (packersdk.Artifact, bool, bool, error) {
	name := p.config.ProviderOverride
	if name == "" {
		n, ok := builtins[artifact.BuilderId()]
		if !ok {
			return nil, false, false, fmt.Errorf(
				"Unknown artifact type, can't build box: %s", artifact.BuilderId())
		}
		name = n
	}

	provider := providerForName(name)
	if provider == nil {
		if artifact.BuilderId() == artifice.BuilderId {
			return nil, false, false, fmt.Errorf(
				"Unknown provider type: When using an artifact created by " +
					"the artifice post-processor, you need to set the " +
					"provider_override option.")
		} else {
			// This shouldn't happen since we hard code all of these ourselves
			panic(fmt.Sprintf("bad provider name: %s", name))
		}
	}

	artifact, keep, err := p.PostProcessProvider(name, provider, ui, artifact)

	// In some cases, (e.g. AMI), deleting the input artifact would render the
	// resulting vagrant box useless. Because of these cases, we want to
	// forcibly set keep_input_artifact.

	// TODO: rework all provisioners to only forcibly keep those where it matters
	return artifact, keep, true, err
}

func (p *PostProcessor) configureSingle(c *Config, raws ...interface{}) error {
	var md mapstructure.Metadata
	err := config.Decode(c, &config.DecodeOpts{
		Metadata:           &md,
		Interpolate:        true,
		InterpolateContext: &c.ctx,
		InterpolateFilter: &interpolate.RenderFilter{
			Exclude: []string{
				"output",
			},
		},
	}, raws...)
	if err != nil {
		return err
	}

	// Defaults
	if c.OutputPath == "" {
		c.OutputPath = "packer_{{ .BuildName }}_{{.Provider}}.box"
	}

	found := false
	for _, k := range md.Keys {
		if k == "compression_level" {
			found = true
			break
		}
	}

	if !found {
		c.CompressionLevel = flate.DefaultCompression
	}

	var errs *packersdk.MultiError
	if c.VagrantfileTemplate != "" && c.VagrantfileTemplateGenerated == false {
		_, err := os.Stat(c.VagrantfileTemplate)
		if err != nil {
			errs = packersdk.MultiErrorAppend(errs, fmt.Errorf(
				"vagrantfile_template '%s' does not exist", c.VagrantfileTemplate))
		}
	}

	if errs != nil && len(errs.Errors) > 0 {
		return errs
	}

	return nil
}

func (p *PostProcessor) specificConfig(name string) (Config, error) {
	config := p.config
	if _, ok := config.Override[name]; ok {
		if err := mapstructure.Decode(config.Override[name], &config); err != nil {
			err = fmt.Errorf("Error overriding config for %s: %s", name, err)
			return config, err
		}
	}
	return config, nil
}

func providerForName(name string) Provider {
	switch name {
	case "aws":
		return new(AWSProvider)
	case "scaleway":
		return new(ScalewayProvider)
	case "digitalocean":
		return new(DigitalOceanProvider)
	case "virtualbox":
		return new(VBoxProvider)
	case "vmware":
		return new(VMwareProvider)
	case "parallels":
		return new(ParallelsProvider)
	case "hyperv":
		return new(HypervProvider)
	case "libvirt":
		return new(LibVirtProvider)
	case "google":
		return new(GoogleProvider)
	case "lxc":
		return new(LXCProvider)
	case "azure":
		return new(AzureProvider)
	case "docker":
		return new(DockerProvider)
	default:
		return nil
	}
}

type vagrantfileTemplate struct {
	ProviderVagrantfile string
	CustomVagrantfile   string
}

const boxVagrantfileContents string = `
# The contents below were provided by the Packer Vagrant post-processor
{{ .ProviderVagrantfile }}

# The contents below (if any) are custom contents provided by the
# Packer template during image build.
{{ .CustomVagrantfile }}
`