forked from womply/ecsman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (139 loc) · 5.85 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
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
/*
Package main implements the ECSMAN command-line utility for managing
Amazon Web Services' EC2 Container Service.
Womply, www.womply.com
*/
package main
import (
"flag"
"fmt"
"os"
"github.com/masonoise/ecsman/components"
"github.com/aws/aws-sdk-go/aws/credentials"
)
/*
Main entry point. Check command line arguments using the Flag module, determine
credentials to be used, set things up, then call the appropriate function handler
depending on what the user's asking for.
Usage:
ecsman ls == list clusters in the region
ecsman <options> ls clusterName == list services in the cluster
ecsman <options> ls clusterName serviceName == list service details
ecsman <options> check clusterName serviceName == check service tasks
ecsman <options> update clusterName serviceName imageURL == update the service with new image
ecsman <options> taskdefs == list task definitions
ecsman <options> register taskFile == register a task using the specified JSON file
ecsman <options> run clusterName taskName == run a task
*/
func main() {
const VERSION string = "1.0.2"
verboseFlag := flag.Bool("v", false, "Verbose printing with details")
versionFlag := flag.Bool("version", false, "Display version and exit")
regionFlag := flag.String("region", "us-west-2", "AWS region")
elbFlag := flag.Bool("elb", false, "Print ELB information")
credFlag := flag.String("cred", "", "AWS credential profile name (or use ECSCREDENTIAL env var)")
eventsFlag := flag.Int("events", 0, "List events for a service")
flag.Usage = usage
flag.Parse()
if *versionFlag == true {
fmt.Println("Version", VERSION)
os.Exit(0)
}
// Get the operation requested
if flag.NArg() < 1 {
usage()
os.Exit(0)
}
var operation = flag.Arg(0)
// What it calls "shared credentials" is the object that handles reading a user's credentials file from ~/.aws/credentials
// First, figure out whether we use a profile name passed in as a command-line argument, an environment variable,
// or the "default" profile. If the cred flag passed in is "env" then it will expect to find the environment
// vars AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to use. Otherwise the flag is expected to be the profile name.
var credProfile = "default"
if *credFlag == "" {
if os.Getenv("ECSCREDENTIAL") != "" {
credProfile = os.Getenv("ECSCREDENTIAL")
}
} else {
if *credFlag != "env" {
credProfile = *credFlag
}
}
if *verboseFlag {
if *credFlag == "env" {
fmt.Printf("--> Running with credentials from environment variables\n\n")
} else {
fmt.Printf("--> Running with credential profile %s\n\n", credProfile)
}
}
var creds = credentials.NewSharedCredentials("", credProfile)
// Okay, what do we want to do today?
switch {
case operation == "ls" && flag.NArg() < 2: // ls without a cluster name
components.ListClusters(creds, *regionFlag)
case operation == "ls" && flag.NArg() > 1: // ls with cluster name and maybe service name
var serviceName = ""
if flag.NArg() > 2 {
serviceName = flag.Arg(2)
}
var loadBalancers []*string // Save from printServices in case user wants the ELB info printed too.
loadBalancers = components.PrintServices(creds, *regionFlag, flag.Arg(1), serviceName, *verboseFlag, *eventsFlag)
if *elbFlag {
components.PrintElbs(creds, *regionFlag, loadBalancers)
}
case operation == "register":
if flag.NArg() < 2 { // Make sure there's a task.JSON filename provided
usageMsg("Must specify JSON file describing the task to register.")
}
components.CreateTask(creds, *regionFlag, flag.Arg(1))
case operation == "update":
if flag.NArg() < 4 { // Need cluster name, service name, and image URL
usageMsg("Must specify cluster name, service name, and image URL to update.")
}
components.UpdateService(creds, *regionFlag, flag.Arg(1), flag.Arg(2), flag.Arg(3))
case operation == "check":
if flag.NArg() < 3 { // Need cluster name and service name
usageMsg("Must specify cluster name and service name to check.")
}
components.CheckService(creds, *regionFlag, flag.Arg(1), flag.Arg(2), *verboseFlag)
case operation == "run":
if flag.NArg() < 3 { // Make sure there's a cluster name and task name provided
usageMsg("Must specify a cluster name and the task name to run.")
}
components.RunTask(creds, *regionFlag, flag.Arg(1), flag.Arg(2))
case operation == "taskdefs":
if flag.NArg() < 2 {
components.PrintTasks(creds, *regionFlag, "", "")
} else {
if flag.NArg() < 3 {
components.PrintTasks(creds, *regionFlag, flag.Arg(1), "")
} else {
components.PrintTasks(creds, *regionFlag, flag.Arg(1), flag.Arg(2))
}
}
case operation != "":
usageMsg(fmt.Sprintf("Unknown operation: %s", operation))
}
}
func usage() {
fmt.Println("Usage: ecsman <flags> <operation> <cluster> <service>")
fmt.Println("\n Operations: ls, update, check, register, run, taskdefs")
fmt.Println(" ls: list. Cluster, service are optional to limit the listing.")
fmt.Println(" update: update a service. Requires cluster, service, image URL.")
fmt.Println(" check: check a service healt. Requires cluster, service.")
fmt.Println(" register: register a task definition. Requires task def JSON file path.")
fmt.Println(" run: run a task. Requires cluster and task name.")
fmt.Println(" taskdefs: list task definitions. Task family name and revision are optional. See documentation.")
fmt.Println("\n Flags:")
fmt.Println(" -v For verbose listings with more details.")
fmt.Println(" -elb List ELB information with cluster. Defaults to false.")
fmt.Println(" -events <int> List <int> events for a service. Defaults to 0.")
fmt.Println(" -cred <profile> AWS credential profile name (or use ECSCREDENTIAL env var)")
fmt.Println(" -region <region> AWS region, defaults to us-west-2")
fmt.Println(" -version Print program version and exit.")
}
func usageMsg(msg string) {
fmt.Println(msg)
usage()
os.Exit(1)
}