-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsecsactions.go
272 lines (235 loc) Β· 9.8 KB
/
awsecsactions.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package task
import (
"context"
"fmt"
"github.com/Excoriate/stiletto/internal/cloud/adapters/clients"
"github.com/Excoriate/stiletto/internal/cloud/awscloud"
"github.com/Excoriate/stiletto/internal/common"
"github.com/Excoriate/stiletto/internal/errors"
"github.com/Excoriate/stiletto/internal/filesystem"
"github.com/Excoriate/stiletto/internal/tui"
"github.com/Excoriate/stiletto/pkg/config"
)
type AWSECSDeployAction struct {
Task CoreTasker
prefix string // How the UX messages should be prefixed
Id string // The ID of the task
Name string // The name of the task
Ctx context.Context
}
type AWSECSDeployActionOptions struct {
AWSAccessKey string
AWSSecretKey string
AWSRegion string
ScanAWSCredentialsFromEnv bool
}
type AWSECSDeployActionArgs struct {
AWSRegion string
AWSAccessKey string
AWSSecretKey string
ClusterName string
ServiceName string
TaskDefinition string
ImageTagOrReleaseVersion string
Image string
EnvVarsToSetInContainerDef map[string]string
}
type AWSECSDeployActions interface {
DeployTask() (Output, error)
}
func getDeployActionArgs(log tui.TUIMessenger) (AWSECSDeployActionArgs, error) {
awsCredentialsCfg, err := awscloud.GetCredentials()
actionPrefix := "AWS:ECS:DEPLOY"
if err != nil {
msg := "Failed to execute ECS action. Pre-requirements could not be satisfied"
log.ShowError(actionPrefix, msg, err)
return AWSECSDeployActionArgs{}, errors.NewActionCfgError(msg, err)
}
cfg := config.Cfg{}
ecsService, err := cfg.GetFromAny("ecs-service")
if err != nil {
errMsg := fmt.Sprintf("Failed to get 'ecsDeployAction' arguments, " +
"'ecs-service' could not be met")
log.ShowError(actionPrefix, errMsg, err)
return AWSECSDeployActionArgs{}, errors.NewActionCfgError(errMsg, err)
}
ecsCluster, err := cfg.GetFromAny("ecs-cluster")
if err != nil {
errMsg := fmt.Sprintf("Failed to get 'ecsDeployAction' arguments, " +
"'ecs-service' could not be met")
log.ShowError(actionPrefix, errMsg, err)
return AWSECSDeployActionArgs{}, errors.NewActionCfgError(errMsg, err)
}
ecsTaskDefName, err := cfg.GetFromAny("task-definition")
if err != nil {
errMsg := fmt.Sprintf("Failed to get 'ecsDeployAction' arguments, " +
"'ecs-service' could not be met")
log.ShowError(actionPrefix, errMsg, err)
return AWSECSDeployActionArgs{}, errors.NewActionCfgError(errMsg, err)
}
imageUrl, err := cfg.GetFromAny("image-url")
if err != nil {
log.ShowWarning(actionPrefix, "No 'image-url' found, "+
"using 'container-image' field that's set in the task definition as the default value")
imageUrl.Value = "use-task-def"
}
tag, err := cfg.GetFromAny("release-version")
if err != nil {
log.ShowWarning(actionPrefix, "No 'image-tag' found, the value 'latest' will be used")
tag.Value = "latest"
}
// Env specific options.
var contDefVarsTotal map[string]string
// Allowed scan/set options to update the task definition/container def.
//with environment variables.
var contDefEnvVarsScannedFromHost map[string]string
var contDefEnvVarsScannedFromKeys map[string]string
var contDefEnvVarsSetCustom map[string]string
var contDefEnvVarScannedByPrefix map[string]string
// 1. Scan from host.
envVarsScanFromHostCfg, err := cfg.GetBoolFromViper("set-env-from-host")
if err != nil {
log.ShowInfo(actionPrefix, "No 'set-env-from-host' found, "+
"no environment variables will be scanned from host")
} else {
isEnvVarsScanFromHostEnabled := envVarsScanFromHostCfg.Value.(bool)
if isEnvVarsScanFromHostEnabled {
log.ShowWarning(actionPrefix, "The 'set-env-from-host' is set. "+
"All the host environment variables will be scanned and set in the task definition/container def.")
contDefEnvVarsScannedFromHost, err = filesystem.FetchAllEnvVarsFromHost()
if err != nil {
log.ShowError(actionPrefix, "Failed to scan the host environment variables", err)
return AWSECSDeployActionArgs{}, errors.NewActionCfgError("Failed to scan the host environment variables", err)
}
} else {
log.ShowInfo(actionPrefix, "The option 'set-env-from-host' is disabled, "+
"no environment variables will be scanned from host")
}
}
// 2. Scan from specific keys passed.
envVarsScanFromKeysCfg, err := cfg.GetStringSliceFromViper("set-env-from-keys")
if err != nil {
log.ShowInfo(actionPrefix, "No 'set-env-from-keys' found, "+
"no environment variables will be scanned from keys")
} else {
keysToScan := envVarsScanFromKeysCfg.Value.([]string)
if len(keysToScan) == 0 {
log.ShowInfo(actionPrefix, "The option 'set-env-from-keys' is set, "+
"however no keys were passed, no environment variables will be scanned from keys")
} else {
scannedFromKeys, err := filesystem.FetchEnvVarsAsMap(keysToScan, []string{})
if err != nil {
log.ShowError(actionPrefix, "Failed to scan the environment variables from keys", err)
return AWSECSDeployActionArgs{}, errors.NewActionCfgError("Failed to scan the environment variables from keys", err)
}
contDefEnvVarsScannedFromKeys = scannedFromKeys
}
}
// 3. Scan from prefix.
envVarsScanFromPrefixCfg, err := cfg.GetStringFromViper("set-env-vars-with-prefix")
if err != nil {
log.ShowInfo(actionPrefix, "The option 'set-env-vars-with-prefix' is not set, no environment variables will be scanned from prefix")
} else {
prefix := envVarsScanFromPrefixCfg.Value.(string)
prefix = common.NormaliseNoSpaces(prefix)
if prefix == "" {
log.ShowInfo(actionPrefix, "The option 'set-env-vars-with-prefix' is set, "+
"however the prefix is empty")
} else {
log.ShowInfo(actionPrefix, fmt.Sprintf("Scanning the environment variables with the prefix '%s'", prefix))
scannedEnvVarsWithPrefix, err := filesystem.FetchEnvVarsWithPrefix(prefix)
if err != nil {
log.ShowError(actionPrefix, "Failed to scan the environment variables with the prefix", err)
return AWSECSDeployActionArgs{}, errors.NewActionCfgError("Failed to scan the environment variables with the prefix", err)
}
contDefEnvVarScannedByPrefix = scannedEnvVarsWithPrefix
}
}
// 4. Set custom and directly passed environment variables
envVarsSetCustomCfg, err := cfg.GetStringMapFromViper("set-env-vars-custom")
if err != nil {
log.ShowInfo(actionPrefix, "No 'set-env-vars-custom' found, so no custom environment variables will be set")
} else {
envVarsSetCustomValue := envVarsSetCustomCfg.Value
if len(envVarsSetCustomValue.(map[string]interface{})) == 0 {
log.ShowInfo(actionPrefix, "The option 'set-env-vars-custom' is set, "+
"however no custom environment variables were passed, no environment variables will be set")
} else {
log.ShowInfo(actionPrefix, "Setting the custom environment variables")
contDefEnvVarsSetCustom = envVarsSetCustomValue.(map[string]string)
}
}
contDefVarsTotal = filesystem.MergeEnvVars(contDefEnvVarsScannedFromHost,
contDefEnvVarsScannedFromKeys, contDefEnvVarScannedByPrefix, contDefEnvVarsSetCustom)
return AWSECSDeployActionArgs{
AWSRegion: awsCredentialsCfg.Region,
AWSAccessKey: awsCredentialsCfg.AccessKeyID,
AWSSecretKey: awsCredentialsCfg.SecretAccessKey,
ClusterName: ecsCluster.Value.(string),
ServiceName: ecsService.Value.(string),
TaskDefinition: ecsTaskDefName.Value.(string),
ImageTagOrReleaseVersion: tag.Value.(string),
Image: imageUrl.Value.(string),
EnvVarsToSetInContainerDef: contDefVarsTotal,
}, nil
}
func (a *AWSECSDeployAction) DeployTask() (Output, error) {
// Getting all the requirements.
uxLog := a.Task.GetPipelineUXLog()
opts, err := getDeployActionArgs(uxLog)
if err != nil {
errMsg := fmt.Sprintf("Failed to get 'ecsDeployAction' arguments")
uxLog.ShowError(a.prefix, errMsg, err)
return Output{}, errors.NewActionCfgError(errMsg, err)
}
// Getting the AWS Client, to perform the actual deployment.
ecsClient, err := clients.GetAWSECSClient(opts.AWSRegion)
if err != nil {
errMsg := fmt.Sprintf("Failed to get AWS ECS client")
uxLog.ShowError(a.prefix, errMsg, err)
return Output{}, errors.NewActionCfgError(errMsg, err)
}
// Get the target task definition.
taskDef, err := awscloud.GetECSTaskDefinition(ecsClient, opts.TaskDefinition)
if err != nil {
errMsg := fmt.Sprintf("Failed to get AWS ECS task definition")
uxLog.ShowError(a.prefix, errMsg, err)
return Output{}, errors.NewActionCfgError(errMsg, err)
}
// Update the task definition.
updateTaskARN, err := awscloud.UpdateECSTaskContainerDefinition(ecsClient, taskDef,
awscloud.ECSTaskDefContainerDefUpdateOptions{
ImageURL: opts.Image,
Version: opts.ImageTagOrReleaseVersion,
EnvironmentVariables: opts.EnvVarsToSetInContainerDef,
})
if err != nil {
errMsg := fmt.Sprintf("Failed to update AWS ECS task definition")
uxLog.ShowError(a.prefix, errMsg, err)
return Output{}, errors.NewActionCfgError(errMsg, err)
}
// Update the service, and perform the actual deployment.
err = awscloud.UpdateECSService(ecsClient, awscloud.ECSUpdateServiceOptions{
Cluster: opts.ClusterName,
Service: opts.ServiceName,
TaskDefARN: updateTaskARN,
ForceNewDeployment: true,
})
if err != nil {
errMsg := fmt.Sprintf("Failed to update AWS ECS service")
uxLog.ShowError(a.prefix, errMsg, err)
return Output{}, errors.NewActionCfgError(errMsg, err)
}
uxLog.ShowSuccess(a.prefix,
fmt.Sprintf("Successfully deployed new task to AWS ECS service '%s' - Task definition"+
" ARN deployed %s", opts.ServiceName, updateTaskARN))
return Output{}, nil
}
func NewAWSECSAction(task CoreTasker, prefix string) AWSECSDeployActions {
return &AWSECSDeployAction{
Task: task,
prefix: prefix,
Id: common.GetUUID(),
Name: "Deploy, or manage ECS configurations, tasks and others",
}
}