-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (155 loc) · 4.17 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
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
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"
"github.com/briandowns/spinner"
"github.com/mingrammer/cfmt"
"github.com/urfave/cli"
)
func main() {
var name string
var zone string
var project string
app := cli.NewApp()
app.Name = "sgk"
app.Usage = "Quickly manage scratch GCP K8s clusters to test Sourcegraph on"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "name, n",
Value: "geoffrey-cluster-test",
Usage: "name of the k8s cluster",
EnvVar: "NAME",
Destination: &name,
},
cli.StringFlag{
Name: "zone",
Value: "us-central1-a",
Usage: "zone to create the cluster in",
EnvVar: "ZONE",
Destination: &zone,
},
cli.StringFlag{
Name: "project",
Value: "sourcegraph-server",
Usage: "GCP project to create the cluster in",
EnvVar: "PROJECT",
Destination: &project,
},
}
app.Commands = []cli.Command{
{
Name: "create",
Aliases: []string{"up", "start"},
Usage: "create a new k8s cluster in GCP",
Flags: []cli.Flag{
cli.IntFlag{
Name: "num-nodes",
Value: 3,
Usage: "number of machines to use in the cluster",
EnvVar: "NUM_NODES",
},
cli.StringFlag{
Name: "machine-type",
Value: "n1-standard-8",
Usage: "name of the machine type to use in the cluster",
EnvVar: "MACHINE_TYPE",
},
cli.BoolTFlag{
Name: "activate",
Usage: "Activate the k8s credentials for the cluster after creation",
EnvVar: "ACTIVATE",
},
},
Action: func(c *cli.Context) error {
numNodes := c.Int("num-nodes")
machine := c.String("machine-type")
err := runGCloud("container", "clusters", "create",
name,
"--image-type=COS",
fmt.Sprintf("--num-nodes=%d", numNodes),
fmt.Sprintf("--machine-type=%s", machine),
fmt.Sprintf("--project=%s", project),
fmt.Sprintf("--zone=%s", zone),
)
if err != nil {
return cli.NewExitError(err, 1)
}
cfmt.Successf("Created %q!\n", name)
if !c.BoolT("activate") {
return nil
}
err = runGCloud("container", "clusters", "get-credentials",
name,
fmt.Sprintf("--project=%s", project),
fmt.Sprintf("--zone=%s", zone),
)
if err != nil {
return cli.NewExitError(err, 1)
}
cfmt.Infof("Fetched credentials for %q.\n", name)
return nil
},
},
{
Name: "delete",
Aliases: []string{"destroy", "down", "stop"},
Usage: "delete a k8s cluster from GCP",
Action: func(c *cli.Context) error {
confirmed, err := confirm(fmt.Sprintf("[%s] in [%s] will be deleted.\nDo you want to continue?", name, project))
if err != nil {
return cli.NewExitError(cfmt.Serrorf("error when trying to confirm deletion, err: %s", err), 1)
}
if !confirmed {
cfmt.Infoln("Aborted.")
return nil
}
err = runGCloud("container", "clusters", "delete",
name,
fmt.Sprintf("--project=%s", project),
fmt.Sprintf("--zone=%s", zone),
"--quiet",
)
if err != nil {
return cli.NewExitError(err, 1)
}
cfmt.Successf("Deleted %q!\n", name)
url := fmt.Sprintf("https://console.cloud.google.com/compute/disks?organizationId=1006954638239&project=%s", project)
cfmt.Infof("Visit %q to delete the disks that were used by %q\n", url, name)
return nil
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func confirm(prompt string) (bool, error) {
cfmt.Warningf("%s [y/n]: ", prompt)
r := bufio.NewReader(os.Stdin)
response, err := r.ReadString('\n')
if err != nil {
return false, err
}
response = strings.ToLower(strings.TrimSpace(response))
return response == "y" || response == "yes", nil
}
func runGCloud(args ...string) error {
s := spinner.New(spinner.CharSets[12], 150*time.Millisecond)
s.Color("bgBlack", "white")
cmdStr := fmt.Sprintf("gcloud %s", strings.Join(args, " "))
s.Prefix = cfmt.Sinfof("Running %q: ", cmdStr)
s.Start()
defer s.Stop()
c := exec.Command("gcloud", args...)
out, err := c.CombinedOutput()
if err != nil {
return fmt.Errorf(cfmt.Serrorf("%q failed\nerr: %s\noutput:\n%s", cmdStr, err, string(out)))
}
return nil
}