forked from hashicorp/otto
/
build.go
208 lines (181 loc) · 5.75 KB
/
build.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package packer
import (
"fmt"
"io"
"io/ioutil"
"path/filepath"
"strings"
"github.com/hashicorp/atlas-go/archive"
"github.com/hashicorp/otto/app"
"github.com/hashicorp/otto/directory"
"github.com/hashicorp/otto/foundation"
)
type BuildOptions struct {
// Dir is the directory where Packer will be executed from.
// If this isn't set, it'll default to "#{ctx.Dir}/build"
Dir string
// The path to the template to execute. If this isn't set, it'll
// default to "#{Dir}/template.json"
TemplatePath string
// InfraOutputMap is a map to change the key of an infra output
// to a different key for a Packer variable. The key of this map
// is the infra output key, and teh value is the Packer variable name.
InfraOutputMap map[string]string
}
// Build can be used to build an artifact with Packer and parse the
// artifact out into a Build properly.
//
// This function automatically knows how to parse various built-in
// artifacts of Packer. For the exact functionality of the parse
// functions, see the documentation of the various parse functions.
//
// This function implements the app.App.Build function.
// TODO: Test
func Build(ctx *app.Context, opts *BuildOptions) error {
project := Project(&ctx.Shared)
if err := project.InstallIfNeeded(); err != nil {
return err
}
ctx.Ui.Header("Querying infrastructure data for build...")
// Get the infrastructure, since it needs to be ready for building
// to occur. We'll copy the outputs and the credentials as variables
// to Packer.
infra, err := ctx.Directory.GetInfra(&directory.Infra{
Lookup: directory.Lookup{
Infra: ctx.Appfile.ActiveInfrastructure().Name}})
if err != nil {
return err
}
// If the infra isn't ready then we can't build
if infra == nil || infra.State != directory.InfraStateReady {
return fmt.Errorf(
"Infrastructure for this application hasn't been built yet.\n" +
"The build step requires this because the target infrastructure\n" +
"as well as its final properties can affect the build process.\n" +
"Please run `otto infra` to build the underlying infrastructure,\n" +
"then run `otto build` again.")
}
// Construct the variables for Packer from the infra. We copy them as-is.
vars := make(map[string]string)
for k, v := range infra.Outputs {
if opts.InfraOutputMap != nil {
if nk, ok := opts.InfraOutputMap[k]; ok {
k = nk
}
}
vars[k] = v
}
for k, v := range ctx.InfraCreds {
vars[k] = v
}
// Setup the vars
if err := foundation.WriteVars(&ctx.Shared); err != nil {
return fmt.Errorf("Error preparing build: %s", err)
}
ctx.Ui.Header("Building deployment archive...")
slugPath, err := createAppSlug(filepath.Dir(ctx.Appfile.Path))
if err != nil {
return err
}
vars["slug_path"] = slugPath
// Start building the resulting build
build := &directory.Build{
Lookup: directory.Lookup{
AppID: ctx.Appfile.ID,
Infra: ctx.Tuple.Infra,
InfraFlavor: ctx.Tuple.InfraFlavor,
},
Artifact: make(map[string]string),
}
// Get the paths for Packer execution
packerDir := opts.Dir
templatePath := opts.TemplatePath
if opts.Dir == "" {
packerDir = filepath.Join(ctx.Dir, "build")
}
if opts.TemplatePath == "" {
templatePath = filepath.Join(packerDir, "template.json")
}
ctx.Ui.Header("Building deployment artifact with Packer...")
ctx.Ui.Message(
"Raw Packer output will begin streaming in below. Otto\n" +
"does not create this output. It is mirrored directly from\n" +
"Packer while the build is being run.\n\n")
// Build and execute Packer
p := &Packer{
Path: project.Path(),
Dir: packerDir,
Ui: ctx.Ui,
Variables: vars,
Callbacks: map[string]OutputCallback{
"artifact": ParseArtifactAmazon(build.Artifact),
},
}
if err := p.Execute("build", templatePath); err != nil {
return err
}
// Store the build!
ctx.Ui.Header("Storing build data in directory...")
if err := ctx.Directory.PutBuild(build); err != nil {
return fmt.Errorf(
"Error storing the build in the directory service: %s\n\n"+
"Despite the build itself completing successfully, Otto must\n"+
"also successfully store the results in the directory service\n"+
"to be able to deploy this build. Please fix the above error and\n"+
"rebuild.",
err)
}
ctx.Ui.Header("[green]Build success!")
ctx.Ui.Message(
"[green]The build was completed successfully and stored within\n" +
"the directory service, meaning other members of your team\n" +
"don't need to rebuild this same version and can deploy it\n" +
"immediately.")
return nil
}
// ParseArtifactAmazon parses AMIs out of the output.
//
// The map will be populated where the key is the region and the value is
// the AMI ID.
func ParseArtifactAmazon(m map[string]string) OutputCallback {
return func(o *Output) {
// We're looking for ID events.
//
// Example: 1440649959,amazon-ebs,artifact,0,id,us-east-1:ami-9d66def6
if len(o.Data) < 3 || o.Data[1] != "id" {
return
}
// TODO: multiple AMIs
parts := strings.Split(o.Data[2], ":")
m[parts[0]] = parts[1]
}
}
// createAppSlug makes an archive of the app with (otto-specific exclusions)
// and yields a path to a tempfile containing that archive
//
// TODO: allow customization of the Exclude patterns
func createAppSlug(path string) (string, error) {
archive, err := archive.CreateArchive(path, &archive.ArchiveOpts{
Exclude: []string{".otto", ".vagrant"},
VCS: true,
})
if err != nil {
return "", err
}
defer archive.Close()
// Archive is just a reader, and we need it in a file. The below seems
// fiddly, could there be a better way?
slug, err := ioutil.TempFile("", "otto-slug-")
if err != nil {
return "", err
}
_, err = io.Copy(slug, archive)
cerr := slug.Close()
if err != nil {
return "", err
}
if cerr != nil {
return "", err
}
return slug.Name(), nil
}