/
diff_deploy.go
154 lines (133 loc) · 4.16 KB
/
diff_deploy.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
package command
import (
"errors"
"fmt"
"log"
"os"
"path"
"strings"
"time"
"github.com/TimeIncOSS/ape-dev-rt/aws"
"github.com/TimeIncOSS/ape-dev-rt/commons"
"github.com/TimeIncOSS/ape-dev-rt/deploymentstate"
"github.com/TimeIncOSS/ape-dev-rt/hcl"
"github.com/TimeIncOSS/ape-dev-rt/terraform"
)
func DiffDeploy(c *commons.Context) error {
user, ok := c.CliContext.App.Metadata["user"].(*aws.User)
if !ok {
return fmt.Errorf("Unable to find AWS User in metadata")
}
ds, ok := c.CliContext.App.Metadata["ds"].(*deploymentstate.DeploymentState)
if !ok {
return fmt.Errorf("Unable to find Deployment State in metadata")
}
rs, ok := c.CliContext.App.Metadata["remote_state"].(*hcl.RemoteState)
if !ok {
return fmt.Errorf("Unable to find Remote State in metadata")
}
cfgPath, err := os.Getwd()
if err != nil {
return err
}
appData, exists, err := BeginApplicationOperation(c.String("env"), c.String("app"), ds)
if err != nil {
return err
}
if !exists {
return nil
}
slotId := c.String("slot-id")
slotPrefix := c.String("slot-prefix")
if slotId == "" && slotPrefix == "" {
return fmt.Errorf("'slot-id' or 'slot-prefix' is required parameter for %q (migrated app)", c.String("app"))
}
if slotId != "" && slotPrefix != "" {
return errors.New("You can specify either 'slot-id' or 'slot-prefix', not both.")
}
if c.CliContext.NArg() < 1 {
return fmt.Errorf("You need to supply a path to Terraform configs of %q.", slotId)
}
if slotPrefix != "" {
counter, prefixExists, err := ds.GetSlotCounter(slotPrefix, appData)
if err != nil {
return err
}
if !prefixExists {
return fmt.Errorf("Slot prefix %s does not exist", slotPrefix)
}
oldSlotId := fmt.Sprintf("%s%d", slotPrefix, counter)
counter, appData, err = ds.IncrementSlotCounter(slotPrefix, appData)
if err != nil {
return err
}
slotId = fmt.Sprintf("%s%d", slotPrefix, counter)
fmt.Printf("Last slot ID is %s, preparing deploy into %s\n", oldSlotId, colour.boldWhite(slotId))
}
rootDir := path.Join(cfgPath, c.CliContext.Args().First())
if rootDir == cfgPath {
return fmt.Errorf("Terraform configs for a slot have to be in a separate dir, not in %q!", cfgPath)
}
_, err = os.Stat(rootDir)
if os.IsNotExist(err) {
return fmt.Errorf("%q does not exist", rootDir)
}
namespace := user.AccountID
if c.String("namespace") != "default" {
namespace = c.String("namespace")
}
filesToCleanup := make([]string, 0)
templateVars := VersionTemplateVars{
AwsAccountId: namespace,
Environment: c.String("env"),
AppName: c.String("app"),
}
filesToCleanup, err = commons.ProcessTemplates(rootDir, "tpl", templateVars)
if err != nil {
return err
}
vars := c.StringSlice("var")
tfVariables := make(map[string]string, 0)
for _, v := range vars {
parts := strings.Split(v, "=")
key, value := parts[0], parts[1]
tfVariables[key] = value
}
tfVariables["app_name"] = c.String("app")
tfVariables["app_version"] = slotId
tfVariables["environment"] = c.String("env")
remoteState, err := terraform.GetRemoteStateForSlotId(&terraform.RemoteState{
Backend: rs.Backend,
Config: rs.Config,
}, user.AccountID, c.String("app"), slotId)
if err != nil {
return err
}
planStartTime := time.Now().UTC()
planFilePath := path.Join(rootDir, slotId+"-planfile")
filesToCleanup = append(filesToCleanup, path.Join(rootDir, ".terraform"))
filesToCleanup = append(filesToCleanup, path.Join(rootDir, "terraform.tfstate.backup"))
filesToCleanup = append(filesToCleanup, planFilePath)
out, err := terraform.FreshPlan(&terraform.FreshPlanInput{
RemoteState: remoteState,
RootPath: rootDir,
PlanFilePath: planFilePath,
Variables: tfVariables,
Refresh: true,
Target: c.String("target"),
Destroy: false,
XLegacy: c.Bool("x"),
})
filesToCleanup = append(filesToCleanup, terraform.GetBackendConfigFilename(rootDir))
planFinishTime := time.Now().UTC()
if err != nil {
return err
}
log.Printf("[DEBUG] Plan started %s, finished %s",
planStartTime.String(), planFinishTime.String())
if out.ExitCode != 0 {
return fmt.Errorf("Planning failed (exit code %d). Stderr:\n%s",
out.ExitCode, out.Stderr)
}
return cleanupFilePaths(filesToCleanup)
}