-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
105 lines (89 loc) · 2.13 KB
/
utils.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
package utils
import (
"bufio"
"encoding/json"
"io"
"io/ioutil"
"net"
"os"
"strings"
"math/rand"
"time"
"golang.org/x/crypto/ssh"
"bytes")
func ExecSSHCmd(publicIP string, privateKey string, uname string, command string) (string, error) {
LogInfo("\nWaiting for server to start before adding ssh keys")
e := WaitForSSH(publicIP)
if e != nil {
return "", e
}
k, e := ssh.ParsePrivateKey([]byte(privateKey))
if e != nil {
return "", e
}
c := &ssh.ClientConfig{
User: uname,
Auth: []ssh.AuthMethod{ssh.PublicKeys(k)},
}
cn, _ := ssh.Dial("tcp", publicIP+":22", c)
s, _ := cn.NewSession()
defer s.Close()
var sOut bytes.Buffer
s.Stdout = &sOut
s.Run(command)
LogInfo(sOut.String())
LogInfo("\nCommand Complete")
return sOut.String(), nil
}
func WaitForSSH(publicIP string) error {
for i:=0; i < 10; i++ {
conn, e := net.Dial("tcp", publicIP+":22")
if e != nil {
time.Sleep(time.Second * 10)
continue
}
defer conn.Close()
if _, e = conn.Read(make([]byte, 1)); e != nil {
continue
}
break
}
return nil
}
func LoadJsonConfig() (map[string]string, error) {
var m map[string]string
c, e := ioutil.ReadFile("./config.json")
if e != nil {
return nil, e
}
if e = json.Unmarshal(c, &m); e != nil {
return nil, e
}
return m, nil
}
func LoadStdinToEnvAndKeys() error {
rd := bufio.NewReader(os.Stdin)
for {
ln := ""
ln, e := rd.ReadString('\n')
if e == io.EOF {
break
} else if e != nil {
return e
} else if strings.Contains(ln, "=") {
kv := strings.SplitN(ln, "=", 2)
SetKey(kv[0], strings.TrimSpace(kv[1]))
os.Setenv(kv[0], strings.TrimSpace(kv[1]))
}
}
return nil
}
func RandSeq(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyz")
rand.Seed(time.Now().UTC().UnixNano())
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}