/
post-processor.go
187 lines (155 loc) · 4.33 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
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
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
)
type Config struct {
common.PackerConfig `mapstructure:",squash"`
Name string `mapstructure:"name"`
MetadataPath string `mapstructure:"output"`
UrlPrefix string `mapstructure:"url_prefix"`
BoxDir string `mapstructure:"box_dir"`
Version string `mapstructure:"version"`
Description string `mapstructure:"description"`
ctx interpolate.Context
}
type PostProcessor struct {
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
}
errs := new(packer.MultiError)
// required configuration
templates := map[string]*string{
"name": &p.config.Name,
"output": &p.config.MetadataPath,
"url_prefix": &p.config.UrlPrefix,
"box_dir": &p.config.BoxDir,
"version": &p.config.Version,
}
for key, ptr := range templates {
if *ptr == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("%s must be set", key))
}
}
for key, ptr := range templates {
if err = interpolate.Validate(*ptr, &p.config.ctx); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Error parsing %s template: %s", key, err))
}
}
if len(errs.Errors) > 0 {
return errs
}
return nil
}
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
if artifact.BuilderId() != "mitchellh.post-processor.vagrant" {
return nil, false, fmt.Errorf(
"Unknown artifact type, requires box from vagrant post-processor: %s", artifact.BuilderId())
}
box := artifact.Files()[0]
if !strings.HasSuffix(box, ".box") {
return nil, false, fmt.Errorf(
"Unknown files in artifact from vagrant post-processor: %s", artifact.Files())
}
provider := providerFromBuilderName(artifact.Id())
file, err := os.Open(box)
if err != nil {
return nil, false, err
}
defer file.Close()
info, err := file.Stat()
if err != nil {
return nil, false, err
}
size := info.Size()
ui.Message(fmt.Sprintf("Box size: %s (%d bytes)", box, size))
metadata, err := p.getMetadata()
if err != nil {
return nil, false, err
}
ui.Message("Generating checksum")
checksum, err := sum256(file)
if err != nil {
return nil, false, err
}
ui.Message(fmt.Sprintf("Checksum is %s", checksum))
ui.Message(fmt.Sprintf("Adding %s %s box to metadata", provider, p.config.Version))
if err := metadata.Add(p.config.Version, &Provider{
Name: provider,
Url: fmt.Sprintf("%s/%s/%s", p.config.UrlPrefix, p.config.BoxDir, path.Base(box)),
ChecksumType: "sha256",
Checksum: checksum,
}); err != nil {
return nil, false, err
}
ui.Message(fmt.Sprintf("Saving the metadata: %s", p.config.MetadataPath))
if err := p.putMetadata(metadata); err != nil {
return nil, false, err
}
return &Artifact{fmt.Sprintf("%s/%s", p.config.UrlPrefix, p.config.MetadataPath)}, true, nil
}
func (p *PostProcessor) getMetadata() (*Metadata, error) {
buf, err := ioutil.ReadFile(p.config.MetadataPath)
if err != nil {
return &Metadata{Name: p.config.Name, Description: p.config.Description}, nil
}
metadata := &Metadata{}
if err := json.Unmarshal(buf, metadata); err != nil {
return nil, err
}
return metadata, nil
}
func (p *PostProcessor) putMetadata(metadata *Metadata) error {
buf, err := json.MarshalIndent(metadata, "", " ")
if err != nil {
return err
}
if err := ioutil.WriteFile(p.config.MetadataPath, buf, os.FileMode(0644)); err != nil {
return err
}
return nil
}
func sum256(file *os.File) (string, error) {
h := sha256.New()
if _, err := io.Copy(h, file); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func providerFromBuilderName(name string) string {
switch name {
case "aws":
return "aws"
case "digitalocean":
return "digitalocean"
case "virtualbox":
return "virtualbox"
case "vmware":
return "vmware_desktop"
case "parallels":
return "parallels"
default:
return name
}
}