forked from convox/rack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstances.go
152 lines (123 loc) · 3 KB
/
instances.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"golang.org/x/crypto/ssh/terminal"
"github.com/convox/rack/cmd/convox/stdcli"
"gopkg.in/urfave/cli.v1"
)
func init() {
stdcli.RegisterCommand(cli.Command{
Name: "instances",
Description: "list your Convox rack's instances",
Usage: "",
Action: cmdInstancesList,
Flags: []cli.Flag{rackFlag},
Subcommands: []cli.Command{
{
Name: "keyroll",
Description: "generate and replace the ec2 keypair used for SSH",
Usage: "",
Action: cmdInstancesKeyroll,
Flags: []cli.Flag{rackFlag},
},
{
Name: "ssh",
Description: "establish secure shell with EC2 instance",
Usage: "<id> [command]",
Action: cmdInstancesSSH,
Flags: []cli.Flag{rackFlag},
SkipFlagParsing: true,
},
{
Name: "terminate",
Description: "terminate an EC2 instance",
Usage: "<id>",
Flags: []cli.Flag{rackFlag},
Action: cmdInstancesTerminate,
},
},
})
}
func cmdInstancesList(c *cli.Context) error {
if len(c.Args()) > 0 {
return stdcli.Error(fmt.Errorf("`convox instances` does not take arguments. Perhaps you meant `convox instances ssh`?"))
}
if c.Bool("help") {
stdcli.Usage(c, "")
return nil
}
instances, err := rackClient(c).GetInstances()
if err != nil {
return stdcli.Error(err)
}
t := stdcli.NewTable("ID", "AGENT", "STATUS", "STARTED", "PS", "CPU", "MEM")
for _, i := range instances {
agent := "off"
if i.Agent {
agent = "on"
}
t.AddRow(i.Id, agent, i.Status,
humanizeTime(i.Started),
strconv.Itoa(i.Processes),
fmt.Sprintf("%0.2f%%", i.Cpu*100),
fmt.Sprintf("%0.2f%%", i.Memory*100))
}
t.Print()
return nil
}
func cmdInstancesKeyroll(c *cli.Context) error {
fmt.Print("Rolling SSH keys... ")
err := rackClient(c).InstanceKeyroll()
if err != nil {
return stdcli.Error(err)
}
fmt.Println("OK")
return nil
}
func cmdInstancesTerminate(c *cli.Context) error {
if len(c.Args()) != 1 {
stdcli.Usage(c, "terminate")
return nil
}
id := c.Args()[0]
fmt.Printf("Terminating %s... ", id)
err := rackClient(c).TerminateInstance(id)
if err != nil {
return stdcli.Error(err)
}
fmt.Println("OK")
return nil
}
func cmdInstancesSSH(c *cli.Context) error {
if len(c.Args()) < 1 {
stdcli.Usage(c, "ssh")
return nil
}
id := c.Args()[0]
cmd := strings.Join(c.Args()[1:], " ")
code, err := sshWithRestore(c, id, cmd)
if err != nil {
return stdcli.Error(err)
}
return cli.NewExitError("", code)
}
func sshWithRestore(c *cli.Context, id, cmd string) (int, error) {
fd := os.Stdin.Fd()
isTerm := terminal.IsTerminal(int(fd))
var h, w int
if isTerm {
stdinState, err := terminal.GetState(int(fd))
if err != nil {
return -1, err
}
h, w, err = terminal.GetSize(int(fd))
if err != nil {
return -1, err
}
defer terminal.Restore(int(fd), stdinState)
}
return rackClient(c).SSHInstance(id, cmd, h, w, isTerm, os.Stdin, os.Stdout)
}