/
types.source.go
182 lines (155 loc) · 5.21 KB
/
types.source.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
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
package hcl2template
import (
"fmt"
"sort"
"strconv"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
hcl2shim "github.com/hashicorp/packer/hcl2template/shim"
"github.com/zclconf/go-cty/cty"
)
// SourceBlock references an HCL 'source' block to be used in a build for
// example.
type SourceBlock struct {
// Type of source; ex: virtualbox-iso
Type string
// Given name; if any
Name string
block *hcl.Block
// LocalName can be set in a singular source block from a build block, it
// allows to give a special name to a build in the logs.
LocalName string
}
// SourceUseBlock is a SourceBlock 'usage' from a config stand point.
// For example when one uses `build.sources = ["..."]` or
// `build.source "..." {...}`.
type SourceUseBlock struct {
// reference to an actual source block definition, or SourceBlock.
SourceRef
// LocalName can be set in a singular source block from a build block, it
// allows to give a special name to a build in the logs.
LocalName string
// Rest of the body, in case the build.source block has more specific
// content
// Body can be expanded by a dynamic tag.
Body hcl.Body
}
func (b *SourceUseBlock) name() string {
if b.LocalName != "" {
return b.LocalName
}
return b.Name
}
func (b *SourceUseBlock) String() string {
return fmt.Sprintf("%s.%s", b.Type, b.name())
}
// EvalContext adds the values of the source to the passed eval context.
func (b *SourceUseBlock) ctyValues() map[string]cty.Value {
return map[string]cty.Value{
"type": cty.StringVal(b.Type),
"name": cty.StringVal(b.name()),
}
}
// decodeBuildSource reads a used source block from a build:
// build {
// source "type.example" {
// name = "local_name"
// }
// }
func (p *Parser) decodeBuildSource(block *hcl.Block) (SourceUseBlock, hcl.Diagnostics) {
ref := sourceRefFromString(block.Labels[0])
out := SourceUseBlock{SourceRef: ref}
var b struct {
Name string `hcl:"name,optional"`
Rest hcl.Body `hcl:",remain"`
}
diags := gohcl.DecodeBody(block.Body, nil, &b)
if diags.HasErrors() {
return out, diags
}
out.LocalName = b.Name
out.Body = b.Rest
return out, nil
}
func (p *Parser) decodeSource(block *hcl.Block) (SourceBlock, hcl.Diagnostics) {
source := SourceBlock{
Type: block.Labels[0],
Name: block.Labels[1],
block: block,
}
var diags hcl.Diagnostics
return source, diags
}
func (cfg *PackerConfig) startBuilder(source SourceUseBlock, ectx *hcl.EvalContext) (packersdk.Builder, hcl.Diagnostics, []string) {
var diags hcl.Diagnostics
builder, err := cfg.parser.PluginConfig.Builders.Start(source.Type)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Failed to load " + sourceLabel + " type",
Detail: err.Error(),
})
return builder, diags, nil
}
body := source.Body
decoded, moreDiags := decodeHCL2Spec(body, ectx, builder)
diags = append(diags, moreDiags...)
if moreDiags.HasErrors() {
return builder, diags, nil
}
// In case of cty.Unknown values, this will write a equivalent placeholder of the same type
// Unknown types are not recognized by the json marshal during the RPC call and we have to do this here
// to avoid json parsing failures when running the validate command.
// We don't do this before so we can validate if variable types matches correctly on decodeHCL2Spec.
decoded = hcl2shim.WriteUnknownPlaceholderValues(decoded)
// Note: HCL prepares inside of the Start func, but Json does not. Json
// builds are instead prepared only in command/build.go
// TODO: either make json prepare when plugins are loaded, or make HCL
// prepare at a later step, to make builds from different template types
// easier to reason about.
builderVars := source.builderVariables()
builderVars["packer_debug"] = strconv.FormatBool(cfg.debug)
builderVars["packer_force"] = strconv.FormatBool(cfg.force)
builderVars["packer_on_error"] = cfg.onError
generatedVars, warning, err := builder.Prepare(builderVars, decoded)
moreDiags = warningErrorsToDiags(cfg.Sources[source.SourceRef].block, warning, err)
diags = append(diags, moreDiags...)
return builder, diags, generatedVars
}
// These variables will populate the PackerConfig inside of the builders.
func (source *SourceUseBlock) builderVariables() map[string]string {
return map[string]string{
"packer_build_name": source.Name,
"packer_builder_type": source.Type,
}
}
func (source *SourceBlock) Ref() SourceRef {
return SourceRef{
Type: source.Type,
Name: source.Name,
}
}
// SourceRef is a nice way to put `virtualbox-iso.source_name`
type SourceRef struct {
// Type of the source, for example `virtualbox-iso`
Type string
// Name of the source, for example `source_name`
Name string
// No other field should be added to the SourceRef because we used that
// struct as a map accessor in many places.
}
// NoSource is the zero value of sourceRef, representing the absense of an
// source.
var NoSource SourceRef
func (r SourceRef) String() string {
return fmt.Sprintf("%s.%s", r.Type, r.Name)
}
func listAvailableSourceNames(srcs map[SourceRef]SourceBlock) []string {
res := make([]string, 0, len(srcs))
for k := range srcs {
res = append(res, k.String())
}
sort.Strings(res)
return res
}