/
ecspresso.go
168 lines (123 loc) · 3.36 KB
/
ecspresso.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
package ecspresso
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"regexp"
jsonpatch "github.com/evanphx/json-patch"
"github.com/mattn/go-shellwords"
"github.com/winebarrel/demitas2/definition"
"github.com/winebarrel/demitas2/utils"
)
const (
taskDefFile = "task-def.json"
serviceDefFile = "service-def.json"
ecspressoCfgFile = "ecspresso.yml"
)
type Ecspresso struct {
path string
options string
}
func NewEcspresso(path string, opts string) (*Ecspresso, error) {
out, err := exec.Command(path, "version").CombinedOutput()
if err != nil {
return nil, fmt.Errorf("faild to execute ecspresso: %w: %s", err, out)
}
return &Ecspresso{
path: path,
options: opts,
}, nil
}
func (ecsp *Ecspresso) RunUntilRunning(def *definition.Definition, dryRun bool) (taskId string, interrupted bool, err error) {
return ecsp.run(def, dryRun, true)
}
func (ecsp *Ecspresso) RunUntilStopped(def *definition.Definition, dryRun bool) (taskId string, interrupted bool, err error) {
return ecsp.run(def, dryRun, false)
}
func (ecsp *Ecspresso) run(def *definition.Definition, dryRun bool, untilRunning bool) (taskId string, interrupted bool, err error) {
opts := ecsp.options
if untilRunning {
opts += " --wait-until=running"
}
if dryRun {
opts += " --dry-run"
}
var stdout string
runInTempDir(func() {
err = writeTemporaryConfigs(def.EcspressoConfig, def.Service, def.Task)
if err != nil {
return
}
cmdWithArgs := []string{ecsp.path, "run"}
args, err := shellwords.Parse(opts)
if err != nil {
return
}
if len(args) > 0 {
cmdWithArgs = append(cmdWithArgs, args...)
}
stdout, _, interrupted, err = utils.RunCommand(cmdWithArgs, false)
if err != nil {
return
}
})
if dryRun {
def.Print()
fmt.Println()
return
}
taskId = findTaskIdFromLog(stdout)
return
}
func runInTempDir(callback func()) {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
tmp, err := ioutil.TempDir("", "demitas2")
if err != nil {
panic(err)
}
defer func() {
_ = os.Chdir(pwd)
os.RemoveAll(tmp)
}()
err = os.Chdir(tmp)
if err != nil {
panic(err)
}
callback()
}
func writeTemporaryConfigs(ecsConf *definition.EcspressoConfig, svrDef *definition.ServiceDefinition, taskDef *definition.TaskDefinition) error {
err := ioutil.WriteFile(taskDefFile, taskDef.Content, os.FileMode(0o644))
if err != nil {
return fmt.Errorf("failed to write ECS task definition: %w", err)
}
err = ioutil.WriteFile(serviceDefFile, svrDef.Content, os.FileMode(0o644))
if err != nil {
return fmt.Errorf("failed to write ECS service definition: %w", err)
}
ecsConfOverrides := `{"service_definition":"` + serviceDefFile + `","task_definition":"` + taskDefFile + `"}`
ecsConfJson, err := jsonpatch.MergePatch(ecsConf.Content, []byte(ecsConfOverrides))
if err != nil {
return fmt.Errorf("failed to update temporary ecspresso config: %w", err)
}
ecsConfYaml, err := utils.JSONToYAML(ecsConfJson)
if err != nil {
return fmt.Errorf("failed to convert temporary ecspresso config to yaml: %w", err)
}
err = ioutil.WriteFile(ecspressoCfgFile, ecsConfYaml, os.FileMode(0o644))
if err != nil {
return fmt.Errorf("failed to write ecspresso config: %w", err)
}
return nil
}
func findTaskIdFromLog(log string) string {
r := regexp.MustCompile(`(?s)Waiting for task ID (\S+)`)
m := r.FindStringSubmatch(log)
if len(m) < 2 {
return ""
}
return m[1]
}