forked from turnerlabs/fargate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.go
280 lines (231 loc) · 6.82 KB
/
root.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
273
274
275
276
277
278
279
280
package cmd
import (
"bufio"
"fmt"
"os"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/spf13/cobra"
"github.com/turnerlabs/fargate/console"
ECS "github.com/turnerlabs/fargate/ecs"
"golang.org/x/crypto/ssh/terminal"
)
const (
defaultClusterName = "fargate"
defaultRegion = "us-east-1"
mebibytesInGibibyte = 1024
protocolHttp = "HTTP"
protocolHttps = "HTTPS"
protocolTcp = "TCP"
runtimeMacOS = "darwin"
typeApplication = "application"
typeNetwork = "network"
validRuleTypesPattern = "(?i)^host|path$"
describeRequestLimitRate = 10
)
var InvalidCpuAndMemoryCombination = fmt.Errorf(`Invalid CPU and Memory settings
CPU (CPU Units) Memory (MiB)
--------------- ------------
256 512, 1024, or 2048
512 1024 through 4096 in 1GiB increments
1024 2048 through 8192 in 1GiB increments
2048 4096 through 16384 in 1GiB increments
4096 8192 through 30720 in 1GiB increments
`)
var validRegions = []string{
"us-east-1",
"us-east-2",
"us-west-1",
"us-west-2",
"ca-central-1",
"eu-west-1",
"eu-west-2",
"eu-central-1",
"ap-southeast-1",
"ap-southeast-2",
"ap-northeast-1",
"ap-northheast-2",
"ap-south-1",
}
var (
clusterName string
noColor bool
noEmoji bool
output ConsoleOutput
region string
sess *session.Session
verbose bool
identifier *regexp.Regexp
)
var rootCmd = &cobra.Command{
Use: "fargate",
Short: "Deploy serverless containers onto the cloud from your command line",
Long: `Deploy serverless containers onto the cloud from your command line
fargate is a command-line interface to deploy containers to AWS Fargate that
makes it easy to run containers in AWS as one-off tasks or managed, highly
available services secured by free TLS certificates. It bundles the power of AWS
including Amazon Elastic Container Service (ECS), Amazon Elastic Container
Registry (ECR), Elastic Load Balancing, AWS Certificate Manager, Amazon
CloudWatch Logs, and Amazon Route 53 into an easy-to-use CLI.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
output = ConsoleOutput{}
if cmd.Parent().Name() == "fargate" {
return
}
console.Verbose = getVerbose()
output.Verbose = getVerbose()
if terminal.IsTerminal(int(os.Stdout.Fd())) {
console.Color = !getNoColor()
output.Color = !getNoColor()
if runtime.GOOS == runtimeMacOS && !noEmoji {
output.Emoji = true
}
}
envAwsDefaultRegion := os.Getenv("AWS_DEFAULT_REGION")
envAwsRegion := os.Getenv("AWS_REGION")
if region == "" {
if envAwsDefaultRegion != "" {
region = envAwsDefaultRegion
} else if envAwsRegion != "" {
region = envAwsRegion
} else {
region = defaultRegion
}
}
if err := validateRegion(region); err != nil {
console.IssueExit(err.Error())
}
config := &aws.Config{
Region: aws.String(region),
}
if getVerbose() {
config.LogLevel = aws.LogLevel(aws.LogDebugWithHTTPBody)
}
sess = session.Must(
session.NewSession(config),
)
_, err := sess.Config.Credentials.Get()
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case "NoCredentialProviders":
console.Issue("Could not find your AWS credentials")
console.Info("Your AWS credentials could not be found. Please configure your environment with your access key")
console.Info(" ID and secret access key using either the shared configuration file or environment variables.")
console.Info(" See http://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html#specifying-credentials")
console.Info(" for more details.")
console.Exit(1)
default:
console.ErrorExit(err, "Could not create create AWS session")
}
}
},
}
// Execute ...
func Execute(version string) {
rootCmd.Version = version
rootCmd.Execute()
}
func init() {
rootCmd.PersistentFlags().StringVar(®ion, "region", "", `AWS region (default "us-east-1")`)
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output")
rootCmd.PersistentFlags().BoolVar(&noColor, "nocolor", false, "Disable color output")
rootCmd.PersistentFlags().StringVarP(&clusterName, "cluster", "c", "", `ECS cluster name`)
if runtime.GOOS == runtimeMacOS {
rootCmd.PersistentFlags().BoolVar(&noEmoji, "no-emoji", false, "Disable emoji output")
}
initConfig(rootCmd)
}
//converts array of KEY=VALUE to array of EnvVar types
func extractEnvVars(inputEnvVars []string) []ECS.EnvVar {
var envVars []ECS.EnvVar
if identifier == nil {
identifier = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
}
if len(inputEnvVars) == 0 {
return envVars
}
for _, inputEnvVar := range inputEnvVars {
splitInputEnvVar := strings.SplitN(inputEnvVar, "=", 2)
if len(splitInputEnvVar) != 2 {
console.ErrorExit(fmt.Errorf("%s must be in the form of KEY=value", inputEnvVar), "Invalid environment variable")
}
key, value := splitInputEnvVar[0], splitInputEnvVar[1]
// make sure the key portion is a valid identifier
if !identifier.MatchString(key) {
console.ErrorExit(fmt.Errorf("Environment variable name %s must contain only letters, underscores, and digits", key), "Invalid environment variable")
}
envVar := ECS.EnvVar{
Key: key,
Value: value,
}
envVars = append(envVars, envVar)
}
return envVars
}
func readVarFile(filename string) []string {
var result []string
file, err := os.Open(filename)
if err != nil {
return result
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
result = append(result, scanner.Text())
}
return result
}
func validateCpuAndMemory(inputCpuUnits, inputMebibytes string) error {
cpuUnits, err := strconv.ParseInt(inputCpuUnits, 10, 16)
if err != nil {
return err
}
mebibytes, err := strconv.ParseInt(inputMebibytes, 10, 16)
if err != nil {
return err
}
switch cpuUnits {
case 256:
if mebibytes == 512 || validateMebibytes(mebibytes, 1024, 2048) {
return nil
}
case 512:
if validateMebibytes(mebibytes, 1024, 4096) {
return nil
}
case 1024:
if validateMebibytes(mebibytes, 2048, 8192) {
return nil
}
case 2048:
if validateMebibytes(mebibytes, 4096, 16384) {
return nil
}
case 4096:
if validateMebibytes(mebibytes, 8192, 30720) {
return nil
}
}
return InvalidCpuAndMemoryCombination
}
func validateMebibytes(mebibytes, min, max int64) bool {
return mebibytes >= min && mebibytes <= max && mebibytes%mebibytesInGibibyte == 0
}
func validateRegion(region string) error {
found := false
for _, validRegion := range validRegions {
if region == validRegion {
found = true
break
}
}
if !found {
return fmt.Errorf("Invalid region: %s [valid regions: %s]", region, strings.Join(validRegions, ", "))
}
return nil
}