/
taint_untaint_infra_resource.go
136 lines (111 loc) · 3.45 KB
/
taint_untaint_infra_resource.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
package command
import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"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 TaintUntaintInfraResource(c *commons.Context) error {
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")
}
user, ok := c.CliContext.App.Metadata["user"].(*aws.User)
if !ok {
return fmt.Errorf("Unable to find AWS User in metadata")
}
namespace := user.AccountID
if c.String("namespace") != "default" {
namespace = c.String("namespace")
}
actionMap := map[string]string{
"taint-infra-resource": "taint",
"untaint-infra-resource": "untaint",
}
action, ok := actionMap[c.CliContext.Command.Name]
if !ok {
return fmt.Errorf("Unexpected command name: %s", c.CliContext.Command.Name)
}
if c.CliContext.NArg() < 1 {
return fmt.Errorf("You need to supply a resource to %s", action)
}
resource := c.CliContext.Args().First()
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
}
if appData.UseCentralGitRepo {
return deprecatedGitError()
}
rootDir := cfgPath
templateVars := ApplicationTemplateVars{
AwsAccountId: namespace,
AppName: c.String("app"),
Environment: c.String("env"),
}
filesToCleanup := make([]string, 0)
filesToCleanup, err = commons.ProcessTemplates(rootDir, "tpl", templateVars)
if err != nil {
return err
}
remoteState, err := terraform.GetRemoteStateForApp(&terraform.RemoteState{
Backend: rs.Backend,
Config: rs.Config,
}, user.AccountID, c.String("app"))
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["environment"] = c.String("env")
filesToCleanup = append(filesToCleanup, path.Join(rootDir, ".terraform"))
filesToCleanup = append(filesToCleanup, path.Join(rootDir, "terraform.tfstate.backup"))
filesToCleanup = append(filesToCleanup, terraform.GetBackendConfigFilename(rootDir))
_, err = terraform.ReenableRemoteState(remoteState, rootDir)
if err != nil {
return err
}
args := []string{}
var moduleMsg string
if c.CliContext.IsSet("module") {
module := fmt.Sprintf("module.%s", c.String("module"))
args = append(args, module)
moduleMsg = fmt.Sprintf(" in module %s", c.String("module"))
}
fmt.Printf("%sing infra resource %s%s for application %s in %s/%s.\n",
strings.Title(action), resource, moduleMsg, c.String("app"), namespace, c.String("env"))
out, err := terraform.Cmd(action, args, rootDir, ioutil.Discard, ioutil.Discard)
if err != nil {
return err
}
if out.ExitCode != 0 {
return fmt.Errorf("Failed to %s a resource (exit code %d). Stderr:\n%s",
action, out.ExitCode, out.Stderr)
}
filesToCleanup = append(filesToCleanup, terraform.GetBackendConfigFilename(rootDir))
fmt.Printf("%s\n", out.Stdout)
return cleanupFilePaths(filesToCleanup)
}