-
Notifications
You must be signed in to change notification settings - Fork 400
/
Copy pathdoit.go
237 lines (197 loc) · 5.27 KB
/
doit.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
package doit
import (
"fmt"
"io"
"io/ioutil"
"os"
"github.com/bryanl/doit/Godeps/_workspace/src/github.com/Sirupsen/logrus"
"github.com/bryanl/doit/Godeps/_workspace/src/github.com/digitalocean/godo"
"github.com/bryanl/doit/Godeps/_workspace/src/github.com/docker/docker/pkg/term"
"github.com/bryanl/doit/Godeps/_workspace/src/github.com/spf13/viper"
"github.com/bryanl/doit/Godeps/_workspace/src/golang.org/x/crypto/ssh"
"github.com/bryanl/doit/Godeps/_workspace/src/golang.org/x/crypto/ssh/terminal"
"github.com/bryanl/doit/Godeps/_workspace/src/golang.org/x/oauth2"
)
var (
// DoitConfig holds the app's current configuration.
DoitConfig Config = &LiveConfig{}
)
// Config is an interface that represent doit's config.
type Config interface {
GetGodoClient() *godo.Client
SSH(user, host, keyPath string, port int) Runner
Set(ns, key string, val interface{})
GetString(ns, key string) (string, error)
GetBool(ns, key string) (bool, error)
GetInt(ns, key string) (int, error)
GetStringSlice(ns, key string) ([]string, error)
}
// LiveConfig is an implementation of Config for live values.
type LiveConfig struct{}
var _ Config = &LiveConfig{}
// GetGodoClient returns a GodoClient.
func (c *LiveConfig) GetGodoClient() *godo.Client {
token := viper.GetString("access-token")
tokenSource := &TokenSource{AccessToken: token}
oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
return godo.NewClient(oauthClient)
}
func sshConnect(user string, host string, method ssh.AuthMethod) error {
sshc := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{method},
}
conn, err := ssh.Dial("tcp", host, sshc)
if err != nil {
return err
}
defer conn.Close()
session, err := conn.NewSession()
if err != nil {
return err
}
defer session.Close()
session.Stdout = os.Stdout
session.Stderr = os.Stderr
session.Stdin = os.Stdin
var (
termWidth, termHeight int
)
fd := os.Stdin.Fd()
oldState, err := term.MakeRaw(fd)
if err != nil {
return err
}
defer term.RestoreTerminal(fd, oldState)
winsize, err := term.GetWinsize(fd)
if err != nil {
termWidth = 80
termHeight = 24
} else {
termWidth = int(winsize.Width)
termHeight = int(winsize.Height)
}
modes := ssh.TerminalModes{
ssh.ECHO: 1,
}
if err := session.RequestPty("xterm", termHeight, termWidth, modes); err != nil {
return err
}
if err == nil {
err = session.Shell()
}
if err != nil {
return err
}
err = session.Wait()
if _, ok := err.(*ssh.ExitError); ok {
return nil
}
if err == io.EOF {
return nil
}
return err
}
type sshRunner struct {
user string
host string
keyPath string
port int
}
var _ Runner = &sshRunner{}
func (r *sshRunner) Run() error {
logrus.WithFields(logrus.Fields{
"user": r.user,
"host": r.host,
"port": r.port,
}).Info("ssh")
sshHost := fmt.Sprintf("%s:%d", r.host, r.port)
// Key Auth
key, err := ioutil.ReadFile(r.keyPath)
if err != nil {
return err
}
privateKey, err := ssh.ParsePrivateKey(key)
if err != nil {
return err
}
if err := sshConnect(r.user, sshHost, ssh.PublicKeys(privateKey)); err != nil {
// Password Auth if Key Auth Fails
fd := os.Stdin.Fd()
state, err := terminal.MakeRaw(int(fd))
if err != nil {
return err
}
defer terminal.Restore(int(fd), state)
t := terminal.NewTerminal(os.Stdout, ">")
password, err := t.ReadPassword("Password: ")
if err != nil {
return err
}
if err := sshConnect(r.user, sshHost, ssh.Password(string(password))); err != nil {
return err
}
}
return err
}
// SSH creates a ssh connection to a host.
func (c *LiveConfig) SSH(user, host, keyPath string, port int) Runner {
return &sshRunner{
user: user,
host: host,
keyPath: keyPath,
port: port,
}
}
// Set sets a config key.
func (c *LiveConfig) Set(ns, key string, val interface{}) {
nskey := fmt.Sprintf("%s-%s", ns, key)
viper.Set(nskey, val)
}
// GetString returns a config value as a string.
func (c *LiveConfig) GetString(ns, key string) (string, error) {
if ns == NSRoot {
return viper.GetString(key), nil
}
nskey := fmt.Sprintf("%s.%s", ns, key)
if _, ok := viper.AllSettings()[fmt.Sprintf("%s.required", nskey)]; ok {
if viper.GetString(nskey) == "" {
return "", NewMissingArgsErr(nskey)
}
}
return viper.GetString(nskey), nil
}
// GetBool returns a config value as a bool.
func (c *LiveConfig) GetBool(ns, key string) (bool, error) {
if ns == NSRoot {
return viper.GetBool(key), nil
}
nskey := fmt.Sprintf("%s.%s", ns, key)
return viper.GetBool(nskey), nil
}
// GetInt returns a config value as an int.
func (c *LiveConfig) GetInt(ns, key string) (int, error) {
if ns == NSRoot {
return viper.GetInt(key), nil
}
nskey := fmt.Sprintf("%s.%s", ns, key)
if _, ok := viper.AllSettings()[fmt.Sprintf("%s.required", nskey)]; ok {
if viper.GetInt(nskey) < 0 {
return 0, NewMissingArgsErr(nskey)
}
}
return viper.GetInt(nskey), nil
}
// GetStringSlice returns a config value as a string slice.
func (c *LiveConfig) GetStringSlice(ns, key string) ([]string, error) {
if ns == NSRoot {
return viper.GetStringSlice(key), nil
}
nskey := fmt.Sprintf("%s-%s", ns, key)
if _, ok := viper.AllSettings()[fmt.Sprintf("%s.required", nskey)]; ok {
if viper.GetStringSlice(nskey) == nil {
return nil, NewMissingArgsErr(nskey)
}
}
return viper.GetStringSlice(nskey), nil
}