-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
355 lines (302 loc) · 9.12 KB
/
main.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*cloud-proxy is a utility for creating multiple DO droplets
and starting socks proxies via SSH after creation.*/
package main
import (
"context"
"encoding/pem"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/terminal"
"github.com/aerissecure/csrange"
"github.com/digitalocean/godo"
"github.com/fatih/color"
)
// TODO: there is a 10 host limit for a single droplet creation call. for larger, split into multiple calls.
var (
ports = flag.String("ports", "", "nmap compliant port list. It's devided into buckets, one for each droplet and made available in the cmd template as {{.ports}}")
cmd = flag.String("cmd", "", "templated command to run on droplets")
pkg = flag.String("pkg", "nmap", "packages to install, separated by comma")
token = flag.String("token", "", "DO API key; Or use DOTOKEN env var")
sshLocation = flag.String("key-location", "~/.ssh/id_rsa", "SSH key location")
count = flag.Int("count", 5, "Amount of droplets to deploy")
name = flag.String("name", "cloud-proxy", "Droplet name prefix")
out = flag.String("out", "out-%v.xml", "filename template used for output files ('%v' is replaced by index)")
regions = flag.String("regions", "*", "Comma separated list of regions to deploy droplets to, defaults to all.")
force = flag.Bool("force", false, "Bypass built-in protections that prevent you from deploying more than 50 droplets")
showversion = flag.Bool("v", false, "Print version and exit")
version = "1.0.0"
yellow = color.New(color.FgYellow)
green = color.New(color.FgGreen)
red = color.New(color.FgRed)
)
func main() {
flag.Parse()
if *showversion {
fmt.Println(version)
os.Exit(0)
}
if *token == "" {
envToken := os.Getenv("DOTOKEN")
if envToken != "" {
*token = envToken
} else {
log.Fatalln("-token required")
}
}
sshSigner, err := openSSHKey(*sshLocation)
if err != nil {
log.Fatalln(err)
}
keyID := ssh.FingerprintLegacyMD5(sshSigner.PublicKey())
if *count > 50 && !*force {
log.Fatalln("-count greater than 50")
}
client := newDOClient(*token)
availableRegions, err := doRegions(client)
if err != nil {
log.Fatalf("There was an error getting a list of regions:\nError: %v\n", err)
}
regionCountMap, err := regionMap(availableRegions, *regions, *count)
if err != nil {
log.Fatalf("%v\n", err)
}
droplets := []godo.Droplet{}
log.Printf("creating %d droplets\n", *count)
for region, c := range regionCountMap {
log.Printf("Creating %d droplets in region %s", c, region)
drops, _, err := client.Droplets.CreateMultiple(context.Background(), newDropLetMultiCreateRequest(*name, region, keyID, c))
droplets = append(droplets, drops...)
if err != nil {
log.Printf("There was an error creating the droplets: %v\n", err)
log.Println("Attempting cleanup...")
machines := dropletsToMachines(droplets)
cleanup(machines, client)
log.Fatalln("You may need to do some manual clean up!")
}
}
machines := dropletsToMachines(droplets)
log.Println("Droplets deployed.")
sig := make(chan os.Signal)
done := make(chan bool)
signal.Notify(sig, os.Interrupt)
go func(sig chan os.Signal, done chan bool, machines []Machine, client *godo.Client) {
<-sig
fmt.Println()
log.Println(color.BlueString("Terminating droplets..."))
cleanup(machines, client)
log.Println("Exiting...")
os.Exit(0)
}(sig, done, machines, client)
log.Println("Please CTRL-C to destroy droplets")
log.Println("Provisioning droplets")
var readyMachines []*Machine
for i := range machines {
m := &machines[i]
m.Index = zeroPad(*count, i+1)
m.Template = *cmd
sshConfig := &ssh.ClientConfig{
User: "root",
Auth: []ssh.AuthMethod{
ssh.PublicKeys(sshSigner),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
m.SSHConfig = sshConfig
// get IPv4 address, waiting until available
for {
if err := m.GetIPs(client); err != nil {
m.Printf("Error getting the IPv4 address of droplet: %v", err)
}
if !m.IsReady() {
m.Colorln(yellow, "Droplet not ready yet, sleeping 5s")
time.Sleep(5 * time.Second)
continue
}
break
}
readyMachines = append(readyMachines, m)
m.Printf("IPv4 Address: %s", m.IPv4)
m.Colorln(green, "Droplet ready")
}
var portBuckets []string
if *ports != "" {
// portBuckets, err = csrange.SplitString(len(readyMachines), *ports) // use non-contiguous csr's
portBuckets, err = csrange.SplitStringContig(len(readyMachines), *ports)
if err != nil {
log.Fatalf("Error parsing -ports: %v\n", err)
}
}
var wg sync.WaitGroup
send := []chan string{}
log.Println("Establishing SSH connections...")
for i, m := range readyMachines {
wg.Add(1)
c := make(chan string)
send = append(send, c)
go func(idx int, m *Machine, c <-chan string) {
for {
client, err := ssh.Dial("tcp", m.IPv4+":22", m.SSHConfig)
if err != nil {
fmt.Println("ERROR:", err)
m.Colorln(yellow, "Error establishing SSH connection, retrying...")
time.Sleep(10 * time.Second)
// wg.Done() // don't call it quits here, keep trying to connect
// return
continue
}
m.Colorln(green, "SSH connection established.")
m.SSHClient = client
break
}
if *pkg != "" {
m.Println("Installing packages")
m.InstallPackages(strings.Split(*pkg, ","))
}
if *ports != "" {
m.Ports = portBuckets[idx]
}
command, err := m.Command()
if err != nil {
m.Printf("Error preparing command template: %v\n", err)
}
m.Printf("Running command: %v\n", command)
fname := fmt.Sprintf(*out, m.Index)
err = m.RunCommand(fname, c)
if err != nil {
m.Printf("Error running command: %v", err)
}
m.Printf("Results: %s\n", fname)
m.Colorln(green, "Done. Exiting...")
wg.Done()
}(i, m, c)
}
// Send types characters to stdin of every machine
// Note: nmap 7.70 will only print status lines after 1% complete
// You can also specify Nmap flag: --stats-every
stdin := make(chan string, 1)
go readStdin(stdin)
go func() {
// TODO: why aren't commands sent in the order the machines were created?
for char := range stdin {
// will block until receiver machine is ready
for _, ch := range send {
ch <- char
}
}
}()
wg.Wait()
log.Println("Done. All commands have been run.")
log.Println("Please CTRL-C to destroy droplets")
<-done
}
func readStdin(out chan string) {
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
defer exec.Command("stty", "-F", "/dev/tty", "echo")
var b []byte = make([]byte, 1)
for {
os.Stdin.Read(b)
// fmt.Printf(">>> %v: ", b)
out <- string(b)
}
}
// zeroPad zero pads a number, idx, with the correct number of zeros given the
// total.
func zeroPad(total, idx int) string {
ndigits := len(fmt.Sprintf("%d", total))
tpl := "%0" + fmt.Sprintf("%d", ndigits) + "d"
return fmt.Sprintf(tpl, idx)
}
func regionMap(slugs []string, regions string, count int) (map[string]int, error) {
allowedSlugs := strings.Split(regions, ",")
regionCountMap := make(map[string]int)
if regions != "*" {
for _, s := range slugs {
for _, a := range allowedSlugs {
if s == a {
if len(regionCountMap) == count {
break
}
regionCountMap[s] = 0
}
}
}
} else {
for _, s := range slugs {
if len(regionCountMap) == count {
break
}
regionCountMap[s] = 0
}
}
if len(regionCountMap) == 0 {
return regionCountMap, errors.New("There are no regions to use")
}
perRegionCount := count / len(regionCountMap)
perRegionCountRemainder := count % len(regionCountMap)
for k := range regionCountMap {
regionCountMap[k] = perRegionCount
}
if perRegionCountRemainder != 0 {
c := 0
for k, v := range regionCountMap {
if c >= perRegionCountRemainder {
break
}
regionCountMap[k] = v + 1
c++
}
}
return regionCountMap, nil
}
func openSSHKey(privKeyPath string) (ssh.Signer, error) {
pemBytes, err := ioutil.ReadFile(privKeyPath)
if err != nil {
return nil, fmt.Errorf("unable to read ssh key file: %v", err)
}
// Check if encrypted
block, _ := pem.Decode(pemBytes)
if block == nil {
return nil, errors.New("PEM formatted block not found")
}
encrypted := strings.Contains(block.Headers["Proc-Type"], "ENCRYPTED")
var signer ssh.Signer
if encrypted {
fmt.Printf("Enter Password (%v): ", privKeyPath)
pw, err := terminal.ReadPassword(int(syscall.Stdin))
fmt.Println()
if err != nil {
return nil, fmt.Errorf("error capturing password: %v\n", err)
}
signer, err = ssh.ParsePrivateKeyWithPassphrase(pemBytes, pw)
if err != nil {
return nil, fmt.Errorf("error parsing encrypted private key: %v\n", err)
}
} else {
signer, err = ssh.ParsePrivateKey(pemBytes)
if err != nil {
return nil, fmt.Errorf("error parsing private key: %v\n", err)
}
}
return signer, nil
}
func cleanup(machines []Machine, client *godo.Client) {
for _, m := range machines {
if err := m.Destroy(client); err != nil {
log.Println(red.Sprintf("Could not delete droplet name: %s", m.Name))
} else {
log.Println(green.Sprintf("Deleted droplet name: %s", m.Name))
}
}
}