/
utils.go
267 lines (238 loc) · 6 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
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
package install
import (
"bytes"
"crypto/tls"
"fmt"
"html/template"
"net"
"net/http"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/pkg/sftp"
"github.com/wonderivan/logger"
"golang.org/x/crypto/ssh"
)
//username
var (
User string
Passwd string
KubeadmFile string
Version string
)
const oneMBByte = 1024 * 1024
func AddrReformat(host string) (string) {
if strings.Index(host, ":") == -1 {
host = fmt.Sprintf("%s:22", host)
}
return host
}
func ReturnCmd(host, cmd string) string {
session, _ := Connect(User, Passwd, host)
defer session.Close()
b, _ := session.CombinedOutput(cmd)
return string(b)
}
func GetFileSize(url string) int {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Get(url)
if err != nil {
panic(err)
}
resp.Body.Close()
return int(resp.ContentLength)
}
func WatchFileSize(host, filename string, size int) {
t := time.NewTicker(3 * time.Second) //every 3s check file
defer t.Stop()
for {
select {
case <-t.C:
length := ReturnCmd(host, "ls -l "+filename+" | awk '{print $5}'")
length = strings.Replace(length, "\n", "", -1)
length = strings.Replace(length, "\r", "", -1)
lengthByte, _ := strconv.Atoi(length)
if lengthByte == size {
t.Stop()
}
lengthFloat := float64(lengthByte)
value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", lengthFloat/oneMBByte), 64)
logger.Alert("transfer total size is:", value, "MB")
}
}
}
//Cmd is
func Cmd(host string, cmd string) []byte {
logger.Info(host, " ", cmd)
session, err := Connect(User, Passwd, host)
if err != nil {
logger.Error(" Error create ssh session failed", err)
panic(1)
}
defer session.Close()
b, err := session.CombinedOutput(cmd)
logger.Debug("command result is:", string(b))
if err != nil {
logger.Error(" Error exec command failed", err)
panic(1)
}
return b
}
func RemoteFilExist(host, remoteFilePath string) bool {
// if remote file is
// ls -l | grep aa | wc -l
remoteFileName := path.Base(remoteFilePath) // aa
remoteFileDirName := path.Dir(remoteFilePath)
remoteFileCommand := fmt.Sprintf("ls -l %s | grep %s | wc -l", remoteFileDirName, remoteFileName)
data := bytes.Replace(Cmd(host, remoteFileCommand), []byte("\r"), []byte(""), -1)
data = bytes.Replace(data, []byte("\n"), []byte(""), -1)
count, err := strconv.Atoi(string(data))
if err != nil {
logger.Error("RemoteFilExist:", err)
panic(1)
}
if count == 0 {
return false
} else {
return true
}
}
//Copy is
func Copy(host, localFilePath, remoteFilePath string) {
sftpClient, err := SftpConnect(User, Passwd, host)
if err != nil {
logger.Error("scpCopy:", err)
panic(1)
}
defer sftpClient.Close()
srcFile, err := os.Open(localFilePath)
if err != nil {
logger.Error("scpCopy:", err)
panic(1)
}
defer srcFile.Close()
dstFile, err := sftpClient.Create(remoteFilePath)
if err != nil {
logger.Error("scpCopy:", err)
panic(1)
}
defer dstFile.Close()
buf := make([]byte, 100*oneMBByte) //100mb
totalMB := 0
for {
n, _ := srcFile.Read(buf)
if n == 0 {
break
}
length, _ := dstFile.Write(buf[0:n])
totalMB += length / oneMBByte
logger.Alert("transfer total size is:", totalMB, "MB")
}
}
//Connect is
func Connect(user, passwd, host string) (*ssh.Session, error) {
auth := []ssh.AuthMethod{ssh.Password(passwd)}
config := ssh.Config{
Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"},
}
clientConfig := &ssh.ClientConfig{
User: user,
Auth: auth,
Timeout: time.Duration(5) * time.Minute,
Config: config,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}
addr := AddrReformat(host)
client, err := ssh.Dial("tcp", addr, clientConfig)
if err != nil {
return nil, err
}
session, err := client.NewSession()
if err != nil {
return nil, err
}
modes := ssh.TerminalModes{
ssh.ECHO: 0, // disable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
return nil, err
}
return session, nil
}
//SftpConnect is
func SftpConnect(user, password, host string) (*sftp.Client, error) {
var (
auth []ssh.AuthMethod
addr string
clientConfig *ssh.ClientConfig
sshClient *ssh.Client
sftpClient *sftp.Client
err error
)
// get auth method
auth = make([]ssh.AuthMethod, 0)
auth = append(auth, ssh.Password(password))
clientConfig = &ssh.ClientConfig{
User: user,
Auth: auth,
Timeout: 30 * time.Second,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}
// connet to ssh
addr = AddrReformat(host)
if sshClient, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
return nil, err
}
// create sftp client
if sftpClient, err = sftp.NewClient(sshClient); err != nil {
return nil, err
}
return sftpClient, nil
}
//Template is
func Template(masters []string, vip string, version string) []byte {
var templateText = string(`apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
kubernetesVersion: {{.Version}}
controlPlaneEndpoint: "apiserver.cluster.local:6443"
networking:
podSubnet: 100.64.0.0/10
apiServer:
certSANs:
- 127.0.0.1
- apiserver.cluster.local
{{range .Masters -}}
- {{.}}
{{end -}}
- {{.VIP}}
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: "ipvs"
ipvs:
excludeCIDRs:
- "{{.VIP}}/32"`)
tmpl, err := template.New("text").Parse(templateText)
if err != nil {
logger.Error("template parse failed:", err)
panic(1)
}
var envMap = make(map[string]interface{})
envMap["VIP"] = vip
envMap["Masters"] = masters
envMap["Version"] = version
var buffer bytes.Buffer
_ = tmpl.Execute(&buffer, envMap)
return buffer.Bytes()
}