-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmdInstance.go
334 lines (310 loc) · 9.37 KB
/
cmdInstance.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"context"
"fmt"
"os"
"text/tabwriter"
"time"
apic "github.com/protosio/protos/apic/proto"
"github.com/urfave/cli/v2"
)
var cloudName string
var cloudLocation string
var protosVersion string
var devImg string
var machineType string
var cmdInstance *cli.Command = &cli.Command{
Name: "instance",
Usage: "Manage Protos instances",
Subcommands: []*cli.Command{
{
Name: "ls",
Usage: "List instances",
Action: func(c *cli.Context) error {
return listInstances()
},
},
{
Name: "info",
ArgsUsage: "<name>",
Usage: "Display information about an instance",
Action: func(c *cli.Context) error {
name := c.Args().Get(0)
if name == "" {
cli.ShowSubcommandHelp(c)
os.Exit(1)
}
return infoInstance(name)
},
},
{
Name: "deploy",
ArgsUsage: "<name>",
Usage: "Deploy a new Protos instance",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cloud",
Usage: "Specify which `CLOUD` to deploy the instance on",
Required: true,
Destination: &cloudName,
},
&cli.StringFlag{
Name: "location",
Usage: "Specify one of the supported `LOCATION`s to deploy the instance in (cloud specific)",
Required: true,
Destination: &cloudLocation,
},
&cli.StringFlag{
Name: "version",
Usage: "Specify Protosd `VERSION` to deploy",
Required: false,
Destination: &protosVersion,
},
&cli.StringFlag{
Name: "devimg",
Usage: "Use a dev image uploaded to your cloud accoun",
Required: false,
Destination: &devImg,
},
&cli.StringFlag{
Name: "type",
Usage: "Specify cloud machine type `TYPE` to deploy. Get it from 'cloud info' subcommand",
Required: true,
Destination: &machineType,
},
},
Action: func(c *cli.Context) error {
name := c.Args().Get(0)
if name == "" {
cli.ShowSubcommandHelp(c)
os.Exit(1)
}
return deployInstance(name, cloudName, cloudLocation, protosVersion, machineType, devImg)
},
},
{
Name: "delete",
ArgsUsage: "<name>",
Usage: "Delete instance",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "local",
Usage: "Deletes the instance from the db and ignores any cloud resources",
},
},
Action: func(c *cli.Context) error {
name := c.Args().Get(0)
if name == "" {
cli.ShowSubcommandHelp(c)
os.Exit(1)
}
local := c.Bool("local")
return deleteInstance(name, local)
},
},
{
Name: "start",
ArgsUsage: "<name>",
Usage: "Power on instance",
Action: func(c *cli.Context) error {
name := c.Args().Get(0)
if name == "" {
cli.ShowSubcommandHelp(c)
os.Exit(1)
}
return startInstance(name)
},
},
{
Name: "stop",
ArgsUsage: "<name>",
Usage: "Power off instance",
Action: func(c *cli.Context) error {
name := c.Args().Get(0)
if name == "" {
cli.ShowSubcommandHelp(c)
os.Exit(1)
}
return stopInstance(name)
},
},
{
Name: "key",
ArgsUsage: "<name>",
Usage: "Prints to stdout the SSH key associated with the instance",
Action: func(c *cli.Context) error {
name := c.Args().Get(0)
if name == "" {
cli.ShowSubcommandHelp(c)
os.Exit(1)
}
return getInstanceKey(name)
},
},
{
Name: "init",
ArgsUsage: "<instance name> <ip>",
Usage: "Initiate an instance",
Action: func(c *cli.Context) error {
name := c.Args().Get(0)
if name == "" {
cli.ShowSubcommandHelp(c)
os.Exit(1)
}
ip := c.Args().Get(1)
if ip == "" {
cli.ShowSubcommandHelp(c)
os.Exit(1)
}
return instanceInit(name, ip)
},
},
{
Name: "logs",
ArgsUsage: "<instance name>",
Usage: "Pulls and displays Protos logs for instance",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "f",
Usage: "Follow logs",
Required: false,
Value: false,
},
},
Action: func(c *cli.Context) error {
name := c.Args().Get(0)
if name == "" {
cli.ShowSubcommandHelp(c)
os.Exit(1)
}
follow := c.Bool("f")
return getInstanceLogs(name, follow)
},
},
},
}
//
// Instance methods
//
func listInstances() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.GetInstances(ctx, &apic.GetInstancesRequest{})
if err != nil {
return err
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 0, 2, ' ', 0)
defer w.Flush()
fmt.Fprintf(w, " %s\t%s\t%s\t%s\t%s\t%s\t", "Name", "Public IP", "Cloud", "VM ID", "Location", "Status")
fmt.Fprintf(w, "\n %s\t%s\t%s\t%s\t%s\t%s\t", "----", "---------", "-----", "-----", "--------", "------")
for _, instance := range resp.Instances {
fmt.Fprintf(w, "\n %s\t%s\t%s\t%s\t%s\t%s\t", instance.Name, instance.PublicIp, instance.CloudName, instance.VmId, instance.Location, instance.Status)
}
fmt.Fprint(w, "\n")
return nil
}
func infoInstance(instanceName string) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.GetInstance(ctx, &apic.GetInstanceRequest{Name: instanceName})
if err != nil {
return fmt.Errorf("could not retrieve instance '%s': %w", instanceName, err)
}
instance := resp.Instance
fmt.Printf("Name: %s\n", instance.Name)
fmt.Printf("VM ID: %s\n", instance.VmId)
fmt.Printf("Public Key: %s\n", instance.PublicKey)
fmt.Printf("Public Key (wireguard): %s\n", instance.PublicKeyWireguard)
fmt.Printf("Public IP: %s\n", instance.PublicIp)
fmt.Printf("Internal IP: %s\n", instance.InternalIp)
fmt.Printf("Cloud type: %s\n", instance.CloudType)
fmt.Printf("Cloud name: %s\n", instance.CloudName)
fmt.Printf("Architecture: %s\n", instance.Architecture)
fmt.Printf("Location: %s\n", instance.Location)
fmt.Printf("Protosd version: %s\n", instance.ProtosVersion)
fmt.Printf("Status: %s\n", instance.Status)
fmt.Println("Peers:")
for name, status := range instance.Peers {
fmt.Printf(" | %s - %s\n", name, status)
}
return nil
}
func deployInstance(instanceName string, cloudName string, cloudLocation string, protosVersion string, machineType string, devImage string) error {
ctx, cancel := context.WithTimeout(context.Background(), 1200*time.Second)
defer cancel()
resp, err := client.DeployInstance(ctx, &apic.DeployInstanceRequest{Name: instanceName, CloudName: cloudName, CloudLocation: cloudLocation, ProtosVersion: protosVersion, MachineType: machineType, DevImg: devImage})
if err != nil {
return fmt.Errorf("could not deploy instance '%s': %w", instanceName, err)
}
instance := resp.Instance
fmt.Printf("Name: %s\n", instance.Name)
fmt.Printf("VM ID: %s\n", instance.VmId)
fmt.Printf("Public Key: %s\n", instance.PublicKey)
fmt.Printf("Public Key (wireguard): %s\n", instance.PublicKeyWireguard)
fmt.Printf("Public IP: %s\n", instance.PublicIp)
fmt.Printf("Internal IP: %s\n", instance.InternalIp)
fmt.Printf("Cloud type: %s\n", instance.CloudType)
fmt.Printf("Cloud name: %s\n", instance.CloudName)
fmt.Printf("Architecture: %s\n", instance.Architecture)
fmt.Printf("Location: %s\n", instance.Location)
fmt.Printf("Protosd version: %s\n", instance.ProtosVersion)
fmt.Printf("Status: %s\n", instance.Status)
return nil
}
func deleteInstance(instanceName string, localOnly bool) error {
ctx, cancel := context.WithTimeout(context.Background(), 1200*time.Second)
defer cancel()
_, err := client.RemoveInstance(ctx, &apic.RemoveInstanceRequest{Name: instanceName, LocalOnly: localOnly})
if err != nil {
return fmt.Errorf("could not remove instance '%s': %w", instanceName, err)
}
return nil
}
func startInstance(instanceName string) error {
ctx, cancel := context.WithTimeout(context.Background(), 600*time.Second)
defer cancel()
_, err := client.StartInstance(ctx, &apic.StartInstanceRequest{Name: instanceName})
if err != nil {
return fmt.Errorf("could not start instance '%s': %w", instanceName, err)
}
return nil
}
func stopInstance(instanceName string) error {
ctx, cancel := context.WithTimeout(context.Background(), 600*time.Second)
defer cancel()
_, err := client.StopInstance(ctx, &apic.StopInstanceRequest{Name: instanceName})
if err != nil {
return fmt.Errorf("could not stop instance '%s': %w", instanceName, err)
}
return nil
}
func getInstanceKey(instanceName string) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.GetInstanceKey(ctx, &apic.GetInstanceKeyRequest{Name: instanceName})
if err != nil {
return fmt.Errorf("could not get instance '%s' key: %w", instanceName, err)
}
fmt.Print(resp.Key)
return nil
}
func getInstanceLogs(instanceName string, follow bool) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.GetInstanceLogs(ctx, &apic.GetInstanceLogsRequest{Name: instanceName})
if err != nil {
return fmt.Errorf("could not get instance '%s' logs: %w", instanceName, err)
}
fmt.Print(resp.Logs)
return nil
}
func instanceInit(instanceName string, ipString string) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := client.InitInstance(ctx, &apic.InitInstanceRequest{Name: instanceName, Ip: ipString})
if err != nil {
return fmt.Errorf("could not initialize instance '%s' key: %w", instanceName, err)
}
return nil
}