-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (117 loc) · 4.47 KB
/
main.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
package main
import (
"github.com/in4it/ecs-deploy/api"
"github.com/in4it/ecs-deploy/provider/ecs"
"github.com/in4it/ecs-deploy/util"
"github.com/juju/loggo"
"github.com/spf13/pflag"
"fmt"
"os"
)
func startup_checks() {
mandatoryEnvVars := []string{
"AWS_REGION",
"JWT_SECRET",
"DEPLOY_PASSWORD",
}
paramstore := ecs.Paramstore{}
err := paramstore.RetrieveKeys()
if err != nil {
fmt.Printf("Couldn't retrieve variables from parameter store\n")
os.Exit(1)
}
for _, envVar := range mandatoryEnvVars {
if !util.EnvExists(envVar) {
fmt.Printf("Environment variable missing: %v\n", envVar)
os.Exit(1)
}
}
// start controller, check database and pick up any remaining work
controller := api.Controller{}
err = controller.Resume()
if err != nil {
fmt.Printf("Couldn't start controller: %v\n", err.Error())
os.Exit(1)
}
}
func addFlags(f *api.Flags, fs *pflag.FlagSet) {
fs.BoolVar(&f.Bootstrap, "bootstrap", f.Bootstrap, "bootstrap ECS cluster")
fs.StringVar(&f.Profile, "profile", f.Profile, "AWS Profile")
fs.StringVar(&f.Region, "region", f.Region, "AWS Region")
fs.StringVar(&f.ClusterName, "cluster-name", f.ClusterName, "name of the cluster")
fs.StringVar(&f.Environment, "environment", f.Environment, "environment (dev/test/staging/uat/prod)")
fs.StringVar(&f.AlbSecurityGroups, "alb-security-groups", f.AlbSecurityGroups, "security groups to attach to the Application Load Balancer")
fs.StringVar(&f.EcsSubnets, "ecs-subnets", f.EcsSubnets, "subnets to use for AWS ECS")
fs.StringVar(&f.CloudwatchLogsPrefix, "cloudwatch-logs-prefix", f.CloudwatchLogsPrefix, "prefix for cloudwatch logs (e.g. mycompany)")
fs.BoolVar(&f.CloudwatchLogsEnabled, "cloudwatch-logs-enabled", f.CloudwatchLogsEnabled, "enable cloudwatch logs")
fs.StringVar(&f.KeyName, "key-name", f.KeyName, "ssh key name")
fs.StringVar(&f.InstanceType, "instance-type", f.InstanceType, "AWS instance type (e.g. t2.micro)")
fs.StringVar(&f.EcsSecurityGroups, "ecs-security-groups", f.EcsSecurityGroups, "ECS security groups to use")
fs.StringVar(&f.EcsMinSize, "ecs-min-size", f.EcsMinSize, "ECS minimal size")
fs.StringVar(&f.EcsMaxSize, "ecs-max-size", f.EcsMaxSize, "ECS maxium size")
fs.StringVar(&f.EcsDesiredSize, "ecs-desired-size", f.EcsDesiredSize, "ECS desired size")
fs.BoolVar(&f.ParamstoreEnabled, "paramstore-enabled", f.ParamstoreEnabled, "enable AWS paramater store")
fs.StringVar(&f.ParamstoreKmsArn, "paramstore-kms-arn", f.ParamstoreKmsArn, "AWS parameter store KMS key ARN")
fs.StringVar(&f.ParamstorePrefix, "paramstore-prefix", f.ParamstorePrefix, "AWS parameter store prefix (e.g. mycompany)")
fs.StringVar(&f.LoadbalancerDomain, "loadbalancer-domain", f.LoadbalancerDomain, "domain to access ECS cluster")
fs.BoolVar(&f.Server, "server", f.Server, "start server")
fs.StringVar(&f.DeleteCluster, "delete-cluster", f.DeleteCluster, "delete-cluster <cluster name>")
fs.BoolVar(&f.DisableEcsDeploy, "disable-ecs-deploy", f.DisableEcsDeploy, "disable ecs deploy during bootstrap")
fs.MarkHidden("disable-ecs-deploy")
}
// @title ecs-deploy
// @version 0.0.1
// @description ecs-deploy is the glue between your CI and ECS. It automates deploys based a simple JSON file Edit
// @contact.name Edward Viaene
// @contact.url https://github.com/in4it/ecs-deploy
// @contact.email ward@in4it.io
// license.name Apache 2.0
func main() {
// set logging to debug
if util.GetEnv("DEBUG", "") == "true" {
loggo.ConfigureLoggers(`<root>=DEBUG`)
} else {
loggo.ConfigureLoggers(`<root>=INFO`)
}
// parse flags
flags := api.NewFlags()
addFlags(flags, pflag.CommandLine)
pflag.Parse()
// examine flags
if flags.Profile != "" {
os.Setenv("AWS_PROFILE", flags.Profile)
}
if flags.Profile != "" {
os.Setenv("AWS_REGION", flags.Region)
}
if flags.Bootstrap {
if ok, _ := util.AskForConfirmation("Bootstrap ECS Cluster?"); ok {
controller := api.Controller{}
err := controller.Bootstrap(flags)
if err != nil {
fmt.Printf("Error: %v\n", err.Error())
}
}
} else if flags.DeleteCluster != "" {
if ok, _ := util.AskForConfirmation("This will delete cluster " + flags.DeleteCluster); ok {
controller := api.Controller{}
err := controller.DeleteCluster(flags)
if err != nil {
fmt.Printf("Error: %v\n", err.Error())
}
}
} else if flags.Server {
// startup checks
startup_checks()
// Launch API
api := api.API{}
err := api.Launch()
if err != nil {
panic(err)
}
} else {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
pflag.PrintDefaults()
}
os.Exit(0)
}