-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.go
190 lines (169 loc) · 5.72 KB
/
console.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
package main
import (
"fmt"
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util"
"github.com/BytemarkHosting/bytemark-client/lib"
"github.com/BytemarkHosting/bytemark-client/util/log"
"github.com/urfave/cli"
"os"
"os/exec"
"strings"
"syscall"
)
func init() {
commands = append(commands, cli.Command{
Name: "console",
Usage: "connect to a server's serial or graphical console - as though physically plugging in",
UsageText: "bytemark console [--serial | --vnc | --panel] [--no-connect] <server>",
Description: `Out-of-band access to a server's serial or graphical (VNC) console.
Under systems with no GUI, sometimes errors will output to the graphical console and not the serial console.
Defaults to connecting to the serial console for the given server.`,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "serial",
Usage: "Connect to the serial console. Cannot be set at same time as --vnc or --panel. You must have an ssh client on your computer.",
},
cli.BoolFlag{
Name: "vnc",
Usage: "Connect to the graphical console. Cannot be set at the same time as --serial or --panel. You must have a openssh client and a VNC client set up on your computer.",
},
cli.BoolFlag{
Name: "panel",
Usage: "Connect to the graphical console via the Bytemark panel. Cannot be set at the same time as --serial or --vnc. You must have a graphical browser installed on your computer.",
},
cli.BoolFlag{
Name: "no-connect",
Usage: "Output connection instructions, rather than directly connecting.",
},
cli.StringFlag{
Name: "ssh-args",
Usage: "Arguments that will be passed to SSH (only applies to --serial).",
},
},
Action: With(VirtualMachineNameProvider, AuthProvider, func(ctx *Context) error {
if ctx.Context.Bool("serial") && ctx.Context.Bool("panel") {
return ctx.Help("You must only specify one of --serial and --panel!")
}
vm, err := global.Client.GetVirtualMachine(ctx.VirtualMachineName)
if err != nil {
return err
}
if ctx.Context.Bool("no_connect") {
console_serial_instructions(vm)
log.Log()
console_vnc_instructions(vm)
return nil
} else {
if ctx.Context.Bool("panel") {
return console_panel(vm)
} else {
return console_serial(vm, ctx.String("ssh-args"))
}
}
}),
})
}
func shortEndpoint(endpoint string) string {
return strings.Split(endpoint, ".")[0]
}
func getExitCode(cmd *exec.Cmd) (exitCode int, err error) {
err = cmd.Wait()
if exitErr, ok := err.(*exec.ExitError); ok {
if waitStatus, ok := exitErr.Sys().(*syscall.WaitStatus); ok {
return waitStatus.ExitStatus(), err
}
}
return 0, err
}
func console_vnc_instructions(vm *lib.VirtualMachine) {
mgmtAddress := vm.ManagementAddress.String()
if vm.ManagementAddress.To4() == nil {
mgmtAddress = "[" + mgmtAddress + "]"
}
log.Log("VNC graphical console connection information for", vm.Hostname)
log.Log()
log.Logf("Ensure that your public key (contained in %s/.ssh/id_rsa.pub or %s/.ssh/id_dsa.pub) is present in your Bytemark user's keys (see `bytemark show keys`, `bytemark add key`)", os.Getenv("HOME"), os.Getenv("HOME"))
log.Log()
log.Logf("Then set up a tunnel using SSH: ssh -L 9999:%s:5900 %s@%s\r\n", mgmtAddress, global.Client.GetSessionUser(), mgmtAddress)
log.Log()
log.Log("You will then be able to connect to vnc://localhost:9999/")
log.Log("Any port may be substituted for 9999 as long as the same port is used in both commands")
}
func console_serial_instructions(vm *lib.VirtualMachine) {
mgmtAddress := vm.ManagementAddress.String()
if vm.ManagementAddress.To4() == nil {
mgmtAddress = "[" + mgmtAddress + "]"
}
log.Log("Serial console connection information for", vm.Hostname)
log.Log()
log.Logf("Ensure that your public key (contained in %s/.ssh/id_rsa.pub or %s/.ssh/id_dsa.pub) is present in your Bytemark user's keys (see `bytemark show keys`, `bytemark add key`)", os.Getenv("HOME"), os.Getenv("HOME"))
log.Log()
log.Logf("Then connect to %s@%s\r\n", global.Client.GetSessionUser(), mgmtAddress)
}
func console_panel(vm *lib.VirtualMachine) error {
ep := global.Config.EndpointName()
token := global.Config.GetIgnoreErr("token")
url := fmt.Sprintf("%s/vnc/?auth_token=%s&endpoint=%s&management_ip=%s", global.Config.PanelURL(), token, shortEndpoint(ep), vm.ManagementAddress)
log.Logf("Opening %s in a browser.\r\n", url)
return util.CallBrowser(url)
}
func collect_args(args string) (slice []string) {
in_dbl := false
in_sgl := false
slice = make([]string, 0)
cur := make([]rune, 0)
if args == "" {
return
}
for _, ch := range args {
if in_dbl && ch == '"' {
in_dbl = false
continue
} else if in_sgl && ch == '\'' {
in_sgl = false
continue
}
if !in_sgl && ch == '"' {
in_dbl = true
continue
} else if !in_dbl && ch == '\'' {
in_sgl = true
continue
}
if !in_dbl && !in_sgl && ch == ' ' {
slice = append(slice, string(cur))
cur = make([]rune, 0)
continue
}
cur = append(cur, ch)
}
slice = append(slice, string(cur))
return
}
func console_serial(vm *lib.VirtualMachine, sshargs string) error {
host := fmt.Sprintf("%s@%s", global.Client.GetSessionUser(), vm.ManagementAddress)
log.Logf("ssh %s\r\n", host)
bin, err := exec.LookPath("ssh")
if err != nil {
return err
}
sshargsli := collect_args(sshargs)
args := make([]string, len(sshargsli)+2)
copy(args[1:], sshargsli[0:])
args[0] = "ssh"
args[len(args)-1] = host
log.Debugf(5, "%+v\r\n", args)
err = syscall.Exec(bin, args, os.Environ())
if err != nil {
if errno, ok := err.(syscall.Errno); ok {
if errno != 0 {
log.Log("Attempting to exec ssh failed. Please file a bug report.")
return err
}
} else {
log.Log("Couldn't connect to the management address.")
return err
}
}
return nil
}