-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathterranova.go
264 lines (213 loc) · 5.95 KB
/
terranova.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
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
/*
Copyright The Terranova Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package terranova
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/terraform/backend/local"
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/configs/configload"
"github.com/hashicorp/terraform/plans"
"github.com/hashicorp/terraform/providers"
"github.com/hashicorp/terraform/terraform"
"github.com/zclconf/go-cty/cty"
)
// Apply brings the platform to the desired state. It'll destroy the platform
// when `destroy` is `true`.
func (p *Platform) Apply(destroy bool) error {
p.startMiddleware()
p.countHook = new(local.CountHook)
stateHook := new(local.StateHook)
if p.Hooks == nil {
p.Hooks = []terraform.Hook{}
}
p.Hooks = append(p.Hooks, p.countHook, stateHook)
ctx, err := p.newContext(destroy)
if err != nil {
return err
}
// state := ctx.State()
if _, diag := ctx.Refresh(); diag.HasErrors() {
return diag.Err()
}
plan, diag := ctx.Plan()
if diag.HasErrors() {
return diag.Err()
}
p.ExpectedStats = NewStats().FromPlan(plan)
stateHook.StateMgr = p.stateMgr
sts, diag := ctx.Apply()
p.State = sts
// p.State = ctx.State()
if diag.HasErrors() {
return diag.Err()
}
return nil
}
// Plan returns execution plan for an existing configuration to apply to the
// platform.
func (p *Platform) Plan(destroy bool) (*plans.Plan, error) {
p.startMiddleware()
ctx, err := p.newContext(destroy)
if err != nil {
return nil, err
}
if _, diag := ctx.Refresh(); diag.HasErrors() {
return nil, diag.Err()
}
plan, diag := ctx.Plan()
if diag.HasErrors() {
return nil, diag.Err()
}
return plan, nil
}
// startMiddleware starts the Log Middleware to intercept the logs if it has not
// been already started
func (p *Platform) startMiddleware() {
if p.LogMiddleware == nil {
return
}
if !p.LogMiddleware.IsEnabled() {
p.LogMiddleware.Start()
}
}
// newContext creates the Terraform context or configuration
func (p *Platform) newContext(destroy bool) (*terraform.Context, error) {
cfg, err := p.config()
if err != nil {
return nil, err
}
vars, err := p.variables(cfg.Module.Variables)
if err != nil {
return nil, err
}
// providerResolver := providers.ResolverFixed(p.Providers)
// provisioners := p.Provisioners
// Create ContextOpts with the current state and variables to apply
ctxOpts := terraform.ContextOpts{
Config: cfg,
Destroy: destroy,
State: p.State,
Variables: vars,
ProviderResolver: providers.ResolverFixed(p.Providers),
Provisioners: p.Provisioners,
Hooks: p.Hooks,
}
ctx, diags := terraform.NewContext(&ctxOpts)
if diags.HasErrors() {
return nil, diags.Err()
}
// Validate the context
if diags = ctx.Validate(); diags.HasErrors() {
return nil, diags.Err()
}
return ctx, nil
}
func (p *Platform) config() (*configs.Config, error) {
if len(p.Code) == 0 {
return nil, fmt.Errorf("no code to apply")
}
// Get a temporal directory to save the infrastructure code
cfgPath, err := ioutil.TempDir("", ".terranova")
if err != nil {
return nil, err
}
// defer os.RemoveAll(cfgPath)
if err := p.saveCode(cfgPath); err != nil {
return nil, err
}
loader, err := configload.NewLoader(&configload.Config{
ModulesDir: filepath.Join(cfgPath, "modules"),
})
if err != nil {
return nil, err
}
config, diags := loader.LoadConfig(cfgPath)
if diags.HasErrors() {
return nil, fmt.Errorf("failed to load the configuration. %s", diags.Error())
}
return config, nil
}
// Export save all the code to the given directory. The directory must exists
// and there should be code to export.
func (p *Platform) Export(dir string) error {
if len(p.Code) == 0 {
return fmt.Errorf("no code to export")
}
if err := p.saveCode(dir); err != nil {
return err
}
if len(p.Vars) == 0 {
return nil
}
tfvarsFile, err := os.Create(filepath.Join(dir, "terraform.tfvars"))
if err != nil {
return err
}
defer tfvarsFile.Close()
var totalErr string
w := bufio.NewWriter(tfvarsFile)
for name, value := range p.Vars {
_, err := w.WriteString(fmt.Sprintf("%s = \"%s\"\n", name, value))
if err != nil {
totalErr = fmt.Sprintf("%s\n\t%s", totalErr, err)
}
}
if err := w.Flush(); err != nil {
totalErr = fmt.Sprintf("%s\n\t%s", totalErr, err)
}
if len(totalErr) != 0 {
return fmt.Errorf("Failed to create the terraform.tfvars file. Errors:%s", totalErr)
}
return nil
}
func (p *Platform) saveCode(cfgPath string) error {
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
return fmt.Errorf("not found the directory to save the code. %s", err)
}
for filename, content := range p.Code {
cfgFileName := filepath.Join(cfgPath, filename)
cfgFileDir := filepath.Dir(cfgFileName)
if _, err := os.Stat(cfgFileDir); os.IsNotExist(err) {
os.MkdirAll(cfgFileDir, 0700)
}
cfgFile, err := os.Create(cfgFileName)
if err != nil {
return err
}
defer cfgFile.Close()
if _, err = io.Copy(cfgFile, strings.NewReader(content)); err != nil {
return err
}
}
return nil
}
func (p *Platform) variables(v map[string]*configs.Variable) (terraform.InputValues, error) {
iv := make(terraform.InputValues)
for name, value := range p.Vars {
if _, declared := v[name]; !declared {
return iv, fmt.Errorf("variable %q is not declared in the code", name)
}
val := &terraform.InputValue{
Value: cty.StringVal(fmt.Sprintf("%v", value)),
SourceType: terraform.ValueFromCaller,
}
iv[name] = val
}
return iv, nil
}