@@ -16,9 +16,11 @@ package controlplane
16
16
17
17
import (
18
18
"bytes"
19
+ "encoding/json"
19
20
"fmt"
20
21
"io/ioutil"
21
22
"net"
23
+ "os"
22
24
"os/exec"
23
25
"path/filepath"
24
26
"strings"
@@ -130,10 +132,6 @@ func runUp(options *createOptions, banzaiCli cli.Cli) error {
130
132
return errors .New ("workspace is already initialized but a different --provider is specified" )
131
133
}
132
134
133
- if err := initStateBackend (options .cpContext , values ); err != nil {
134
- return err
135
- }
136
-
137
135
source := "/export"
138
136
var defaultValues map [string ]interface {}
139
137
exportHandlers := []ExportedFilesHandler {
@@ -146,7 +144,34 @@ func runUp(options *createOptions, banzaiCli cli.Cli) error {
146
144
return err
147
145
}
148
146
149
- var env map [string ]string
147
+ env := make (map [string ]string )
148
+
149
+ switch values ["provider" ] {
150
+ case providerCustom :
151
+ if pc , ok := values ["providerConfig" ]; ok {
152
+ pc := cast .ToStringMap (pc )
153
+ if _ , ok := pc ["accessKey" ]; ok { // if using aws key
154
+ _ , creds , err := input .GetAmazonCredentials ()
155
+ if err != nil {
156
+ return errors .WrapIf (err , "failed to get AWS credentials" )
157
+ }
158
+ env = creds
159
+ }
160
+ }
161
+ case providerEc2 :
162
+ _ , creds , err := input .GetAmazonCredentials ()
163
+ if err != nil {
164
+ return errors .WrapIf (err , "failed to get AWS credentials" )
165
+ }
166
+ env = creds
167
+ }
168
+
169
+ if options .terraformInit {
170
+ if err := initStateBackend (options .cpContext , values , env ); err != nil {
171
+ return errors .WrapIf (err , "failed to initialize state backend" )
172
+ }
173
+ }
174
+
150
175
switch values ["provider" ] {
151
176
case providerPke :
152
177
err := ensurePKECluster (banzaiCli , options .cpContext )
@@ -161,37 +186,19 @@ func runUp(options *createOptions, banzaiCli cli.Cli) error {
161
186
}
162
187
163
188
case providerEc2 :
164
- _ , creds , err := input .GetAmazonCredentials ()
165
- if err != nil {
166
- return errors .WrapIf (err , "failed to get AWS credentials" )
167
- }
168
-
169
189
useGeneratedKey := true
170
190
if pc , ok := values ["providerConfig" ]; ok {
171
191
if pc , ok := pc .(map [string ]interface {}); ok {
172
192
useGeneratedKey = pc ["key_name" ] != nil && pc ["key_name" ] != ""
173
193
}
174
194
}
175
195
176
- if err := ensureEC2Cluster (options .cpContext , creds , useGeneratedKey ); err != nil {
196
+ if err := ensureEC2Cluster (options .cpContext , env , useGeneratedKey ); err != nil {
177
197
return errors .WrapIf (err , "failed to create EC2 cluster" )
178
198
}
179
- env = creds
180
199
181
200
case providerCustom :
182
- creds := map [string ]string {}
183
- if pc , ok := values ["providerConfig" ]; ok {
184
- pc := cast .ToStringMap (pc )
185
- if _ , ok := pc ["accessKey" ]; ok {
186
- _ , awsCreds , err := input .GetAmazonCredentials ()
187
- if err != nil {
188
- return errors .WrapIf (err , "failed to get AWS credentials" )
189
- }
190
- creds = awsCreds
191
- }
192
- }
193
-
194
- if err := ensureCustomCluster (options .cpContext , creds ); err != nil {
201
+ if err := ensureCustomCluster (options .cpContext , env ); err != nil {
195
202
return errors .WrapIf (err , "failed to create Custom infrastructure" )
196
203
}
197
204
@@ -202,16 +209,6 @@ func runUp(options *createOptions, banzaiCli cli.Cli) error {
202
209
}
203
210
204
211
log .Info ("Deploying Banzai Cloud Pipeline to Kubernetes cluster..." )
205
- if values ["provider" ] == providerCustom {
206
- _ , creds , err := input .GetAmazonCredentials ()
207
- if err != nil {
208
- return errors .WrapIf (err , "failed to get AWS credentials" )
209
- }
210
- env = map [string ]string {}
211
- for k , v := range creds {
212
- env [k ] = v
213
- }
214
- }
215
212
216
213
if err := runTerraform ("apply" , options .cpContext , env ); err != nil {
217
214
return errors .WrapIf (err , "failed to deploy pipeline components" )
@@ -332,6 +329,65 @@ func imageFileExists(options *cpContext, source string) (bool, error) {
332
329
}
333
330
}
334
331
332
+ func stringifyMap (m interface {}) interface {} {
333
+ switch v := m .(type ) {
334
+ case map [string ]interface {}:
335
+ out := make (map [string ]interface {})
336
+ for k , v := range v {
337
+ out [k ] = stringifyMap (v )
338
+ }
339
+ return out
340
+ case map [interface {}]interface {}:
341
+ out := make (map [string ]interface {})
342
+ for k , v := range v {
343
+ out [fmt .Sprint (k )] = stringifyMap (v )
344
+ }
345
+ return out
346
+ default :
347
+ return v
348
+ }
349
+ }
350
+
351
+ func initStateBackend (options * cpContext , values map [string ]interface {}, env map [string ]string ) error {
352
+ var stateData []byte
353
+
354
+ if stateValues , ok := values ["state" ]; ok {
355
+ values := stringifyMap (stateValues )
356
+ var err error
357
+ stateData , err = json .MarshalIndent (values , "" , " " )
358
+ if err != nil {
359
+ return errors .WrapIf (err , "failed to marshal state backend configuration" )
360
+ }
361
+ } else {
362
+ stateData = []byte (fmt .Sprintf (localStateBackend , tfstateFilename ))
363
+ }
364
+
365
+ err := ioutil .WriteFile (options .workspace + "/state.tf.json" , stateData , 0600 )
366
+ if err != nil {
367
+ return errors .WrapIf (err , "failed to create state backend configuration" )
368
+ }
369
+
370
+ err = os .MkdirAll (options .workspace + "/.terraform" , 0700 )
371
+ if err != nil {
372
+ return errors .WrapIf (err , "failed to create state backend directory" )
373
+ }
374
+
375
+ stateFile , err := os .Create (options .workspace + "/.terraform/terraform.tfstate" )
376
+ if err != nil {
377
+ return errors .WrapIf (err , "failed to create state config file" )
378
+ }
379
+ _ = stateFile .Close ()
380
+
381
+ if err := runTerraform ("init" , options , env ); err != nil {
382
+ return errors .WrapIf (err , "failed to init state backend" )
383
+ }
384
+
385
+ // remove old state.tf if any
386
+ _ = os .Remove (options .workspace + "/state.tf" )
387
+
388
+ return nil
389
+ }
390
+
335
391
func defaultValuesExporter (source string , defaultValues * map [string ]interface {}) ExportedFilesHandler {
336
392
return ExportedFilesHandler (func (files map [string ][]byte ) error {
337
393
if valuesFileContent , ok := files [source ]; ok {
0 commit comments