-
Notifications
You must be signed in to change notification settings - Fork 1
/
cdk.go
104 lines (97 loc) · 3.42 KB
/
cdk.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
package cdk
import (
"fmt"
"os"
"os/exec"
"strings"
)
// Clienter is interface of CDK client
type Clienter interface {
Setup(repoPath string) error
List(repoPath string, contexts map[string]string) ([]string, error)
Diff(repoPath string, stacks []string, contexts map[string]string) (string, bool, error)
Deploy(repoPath string, stacks []string, contexts map[string]string) (string, error)
}
// Client is CDK client
type Client struct{}
// Setup env to run cdk commands
func (*Client) Setup(repoPath string) error {
if err := os.Setenv("NPM_CONFIG_USERCONFIG", "/opt/nodejs/.npmrc"); err != nil {
return err
}
// Currently, CDK writes cache at $HOME so it needs to change it.
// https://github.com/aws/aws-cdk/blob/a357bdef775ad30d726090150d496bcb24d576be/packages/aws-cdk/lib/api/util/account-cache.ts#L24
if err := os.Setenv("HOME", "/tmp"); err != nil {
return err
}
cmd := exec.Command("npm", "install")
cmd.Dir = repoPath
out, err := cmd.CombinedOutput()
if err != nil || cmd.ProcessState.ExitCode() != 0 {
return fmt.Errorf("npm install failed: %s %v", string(out), err)
}
return nil
}
// List stack
func (*Client) List(repoPath string, contexts map[string]string) ([]string, error) {
args := []string{"run", "cdk", "--", "list"}
for k, v := range contexts {
args = append(args, "-c", fmt.Sprintf("%s=%s", k, v))
}
cmd := exec.Command("npm", args...)
cmd.Dir = repoPath
out, err := cmd.CombinedOutput()
if err != nil || cmd.ProcessState.ExitCode() != 0 {
return nil, fmt.Errorf("cdk list failed: %s %v", string(out), err)
}
lists := strings.Split(strings.Trim(string(out), "\n"), "\n")[3:]
return lists, nil
}
// Diff stack and returns (diff, hasDiff, error)
func (*Client) Diff(repoPath string, stacks []string, contexts map[string]string) (string, bool, error) {
args := []string{"run", "cdk", "--", "diff"}
for _, stack := range stacks {
args = append(args, stack)
}
for k, v := range contexts {
args = append(args, "-c", fmt.Sprintf("%s=%s", k, v))
}
cmd := exec.Command("npm", args...)
cmd.Dir = repoPath
out, err := cmd.CombinedOutput()
// If the error code is 0, there is no diff, if it is 1, there is diff, otherwise it is an error
if cmd.ProcessState.ExitCode() != 0 && cmd.ProcessState.ExitCode() != 1 {
return "failed!", true, fmt.Errorf("cdk diff failed: %s %v", string(out), err)
}
lines := []string{}
for _, line := range strings.Split(strings.Trim(string(out), "\n"), "\n")[3:] {
if !strings.HasPrefix(line, "npm ERR!") {
lines = append(lines, line)
}
}
return strings.Trim(strings.Join(lines, "\n"), "\n"), cmd.ProcessState.ExitCode() != 0, nil
}
// Deploy stack
func (*Client) Deploy(repoPath string, stacks []string, contexts map[string]string) (string, error) {
args := []string{"run", "cdk", "--", "deploy"}
for _, stack := range stacks {
args = append(args, stack)
}
args = append(args, []string{"--require-approval", "never"}...)
for k, v := range contexts {
args = append(args, "-c", fmt.Sprintf("%s=%s", k, v))
}
cmd := exec.Command("npm", args...)
cmd.Dir = repoPath
out, err := cmd.CombinedOutput()
if err != nil || cmd.ProcessState.ExitCode() != 0 {
return "failed!", fmt.Errorf("cdk deploy failed: %s %v", string(out), err)
}
lines := []string{}
for _, line := range strings.Split(strings.Trim(string(out), "\n"), "\n")[3:] {
if !strings.HasPrefix(line, "npm ERR!") {
lines = append(lines, line)
}
}
return strings.Trim(strings.Join(lines, "\n"), "\n"), err
}