forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.source.go
173 lines (148 loc) · 4.65 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
package hcl2template
import (
"fmt"
"strconv"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/packer/packer"
"github.com/zclconf/go-cty/cty"
)
// SourceBlock references an HCL 'source' block.
type SourceBlock struct {
// Type of source; ex: virtualbox-iso
Type string
// Given name; if any
Name string
block *hcl.Block
// addition will be merged into block to allow user to override builder settings
// per build.source block.
addition hcl.Body
// 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
}
func (b *SourceBlock) name() string {
if b.LocalName != "" {
return b.LocalName
}
return b.Name
}
func (b *SourceBlock) 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 *SourceBlock) 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) (SourceRef, hcl.Diagnostics) {
ref := sourceRefFromString(block.Labels[0])
var b struct {
Name string `hcl:"name,optional"`
Rest hcl.Body `hcl:",remain"`
}
diags := gohcl.DecodeBody(block.Body, nil, &b)
if diags.HasErrors() {
return ref, diags
}
ref.addition = b.Rest
ref.LocalName = b.Name
return ref, 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
if !p.BuilderSchemas.Has(source.Type) {
diags = append(diags, &hcl.Diagnostic{
Summary: "Unknown " + buildSourceLabel + " type " + source.Type,
Subject: block.LabelRanges[0].Ptr(),
Detail: fmt.Sprintf("known builders: %v", p.BuilderSchemas.List()),
Severity: hcl.DiagError,
})
return source, diags
}
return source, diags
}
func (cfg *PackerConfig) startBuilder(source SourceBlock, ectx *hcl.EvalContext, opts packer.GetBuildsOptions) (packer.Builder, hcl.Diagnostics, []string) {
var diags hcl.Diagnostics
builder, err := cfg.builderSchemas.Start(source.Type)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Summary: "Failed to load " + sourceLabel + " type",
Detail: err.Error(),
Subject: &source.block.LabelRanges[0],
})
return builder, diags, nil
}
body := source.block.Body
if source.addition != nil {
body = hcl.MergeBodies([]hcl.Body{source.block.Body, source.addition})
}
decoded, moreDiags := decodeHCL2Spec(body, ectx, builder)
diags = append(diags, moreDiags...)
if moreDiags.HasErrors() {
return nil, diags, nil
}
// 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(opts.Debug)
builderVars["packer_force"] = strconv.FormatBool(opts.Force)
builderVars["packer_on_error"] = opts.OnError
generatedVars, warning, err := builder.Prepare(builderVars, decoded)
moreDiags = warningErrorsToDiags(source.block, warning, err)
diags = append(diags, moreDiags...)
return builder, diags, generatedVars
}
// These variables will populate the PackerConfig inside of the builders.
func (source *SourceBlock) 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,
}
}
type SourceRef struct {
Type string
Name string
// The content of this body will be merged into a new block to allow to
// override builder settings per build section.
addition hcl.Body
// 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
}
// the 'addition' field makes of ref a different entry in the sources map, so
// Ref is here to make sure only one is returned.
func (r *SourceRef) Ref() SourceRef {
return SourceRef{
Type: r.Type,
Name: r.Name,
}
}
// 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)
}