This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
/
ssh.go
161 lines (129 loc) · 3.03 KB
/
ssh.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
package commands
import (
"errors"
"fmt"
"io"
"os/user"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/bryanl/doit"
"github.com/Sirupsen/logrus"
"github.com/digitalocean/godo"
"github.com/spf13/cobra"
)
const (
sshNoAddress = "could not find droplet address"
)
var (
errSSHInvalidOptions = fmt.Errorf("neither id or name were supplied")
sshHostRE = regexp.MustCompile("^((?P<m1>\\w+)@)?(?P<m2>.*?)(:(?P<m3>\\d+))?$")
)
// SSH creates the ssh commands heirarchy
func SSH() *cobra.Command {
usr, err := user.Current()
if err != nil {
logrus.Fatal(err.Error())
}
path := filepath.Join(usr.HomeDir, ".ssh", "id_rsa")
cmdSSH := cmdBuilder(RunSSH, "ssh", "ssh to droplet", writer)
addStringFlag(cmdSSH, doit.ArgSSHUser, "root", "ssh user")
addStringFlag(cmdSSH, doit.ArgsSSHKeyPath, path, "path to private ssh key")
addIntFlag(cmdSSH, doit.ArgsSSHPort, 22, "port sshd is running on")
return cmdSSH
}
// RunSSH finds a droplet to ssh to given input parameters (name or id).
func RunSSH(ns string, config doit.Config, out io.Writer, args []string) error {
client := config.GetGodoClient()
if len(args) == 0 {
return doit.NewMissingArgsErr(ns)
}
dropletID := args[0]
if dropletID == "" {
return doit.NewMissingArgsErr(ns)
}
user, err := config.GetString(ns, doit.ArgSSHUser)
if err != nil {
return err
}
keyPath, err := config.GetString(ns, doit.ArgsSSHKeyPath)
if err != nil {
return err
}
port, err := config.GetInt(ns, doit.ArgsSSHPort)
if err != nil {
return err
}
var droplet *godo.Droplet
if id, err := strconv.Atoi(dropletID); err == nil {
// dropletID is an integer
droplet, err = getDropletByID(client, id)
} else {
// dropletID is a string
var droplets []godo.Droplet
droplets, err := listDroplets(client)
if err != nil {
return err
}
shi := extractHostInfo(dropletID)
user = shi.user
if i, err := strconv.Atoi(shi.port); shi.port != "" && err != nil {
port = i
}
for _, d := range droplets {
if d.Name == shi.host {
droplet = &d
break
}
}
if droplet == nil {
return errors.New("could not find droplet by name")
}
}
if user == "" {
user = defaultSSHUser(droplet)
}
publicIP := extractDropletPublicIP(droplet)
if len(publicIP) < 1 {
return errors.New(sshNoAddress)
}
runner := config.SSH(user, publicIP, keyPath, port)
return runner.Run()
}
func removeEmptyOptions(in []string) []string {
var out []string
if len(in) == 1 && in[0] == "[]" {
return out
}
for _, s := range in {
if len(s) > 0 {
out = append(out, s)
}
}
return out
}
func defaultSSHUser(droplet *godo.Droplet) string {
slug := strings.ToLower(droplet.Image.Slug)
if strings.Contains(slug, "coreos") {
return "core"
}
return "root"
}
type sshHostInfo struct {
user string
host string
port string
}
func extractHostInfo(in string) sshHostInfo {
m := sshHostRE.FindStringSubmatch(in)
r := map[string]string{}
for i, n := range sshHostRE.SubexpNames() {
r[n] = m[i]
}
return sshHostInfo{
user: r["m1"],
host: r["m2"],
port: r["m3"],
}
}