-
Notifications
You must be signed in to change notification settings - Fork 328
/
installer.go
158 lines (148 loc) · 4.46 KB
/
installer.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
package tke
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog"
platformv1 "tkestack.io/tke/api/platform/v1"
"tkestack.io/tke/cmd/tke-installer/app/installer/types"
"tkestack.io/tke/pkg/util/ssh"
"tkestack.io/tke/test/util/cloudprovider"
)
type Installer struct {
node cloudprovider.Instance
}
func InitInstaller(provider cloudprovider.Provider) *Installer {
nodes, err := provider.CreateInstances(1)
if err != nil {
panic(fmt.Errorf("create instance failed. %v", err))
}
return &Installer{node: nodes[0]}
}
func (installer *Installer) RunCMD(cmd string) (string, error) {
klog.Info("Run CMD: ", cmd)
s, err := ssh.New(&ssh.Config{
User: installer.node.Username,
Password: installer.node.Password,
Host: installer.node.InternalIP,
Port: int(installer.node.Port),
})
if err != nil {
return "", err
}
out, err := s.CombinedOutput(cmd)
klog.Info("CMD output: ", string(out))
return string(out), err
}
func (installer *Installer) InstallInstaller(os, arch, version string) error {
klog.Info("Download and install installer")
name := fmt.Sprintf("tke-installer-%s-%s-%s", os, arch, version)
cmd := fmt.Sprintf("wget https://tke-release-1251707795.cos.ap-guangzhou.myqcloud.com/%s.run{,.sha256} && sha256sum --check --status %s.run.sha256 && chmod +x %s.run && ./%s.run",
name, name, name, name)
_, err := installer.RunCMD(cmd)
return err
}
func (installer *Installer) CreateClusterParaTemplate(nodes []cloudprovider.Instance) *types.CreateClusterPara {
para := new(types.CreateClusterPara)
for _, one := range nodes {
para.Cluster.Spec.Machines = append(para.Cluster.Spec.Machines, platformv1.ClusterMachine{
IP: one.InternalIP,
Port: one.Port,
Username: one.Username,
Password: []byte(one.Password),
})
}
para.Config = types.Config{
Basic: &types.Basic{
Username: "admin",
Password: []byte("admin"),
},
Auth: types.Auth{
TKEAuth: &types.TKEAuth{},
},
Registry: types.Registry{
TKERegistry: &types.TKERegistry{
Domain: "registry.tke.com",
},
},
Business: &types.Business{},
Monitor: nil,
Logagent: &types.Logagent{},
HA: nil,
Gateway: &types.Gateway{
Domain: "console.tke.com",
Cert: &types.Cert{
SelfSignedCert: &types.SelfSignedCert{},
},
},
Application: &types.Application{
RegistryDomain: "registry.tke.com",
},
}
return para
}
func (installer *Installer) Install(createClusterPara *types.CreateClusterPara) error {
klog.Info("Start installing")
body, err := json.Marshal(createClusterPara)
if err != nil {
return fmt.Errorf("unmarshal CreateClusterPara failed. %v", err)
}
url := fmt.Sprintf("http://%s:8080/api/cluster", installer.node.InternalIP)
resp, err := http.Post(url, "application/json", bytes.NewReader(body))
if err != nil {
return fmt.Errorf("post data to install failed. %v", err)
}
resp.Body.Close()
klog.Info("Wait install finish")
var progress *types.ClusterProgress
err = wait.Poll(time.Minute, 90*time.Minute, func() (bool, error) {
progress, err = installer.GetInstallProgress()
if err != nil {
return false, err
}
klog.Info(progress.Status)
switch progress.Status {
case types.StatusUnknown, types.StatusDoing, types.StatusRetrying:
return false, nil
case types.StatusFailed:
return false, fmt.Errorf("install failed:\n%s", progress.Data)
case types.StatusSuccess:
return true, nil
default:
klog.Infof("Install again as we got an unknown install progress status: %s", progress.Status)
resp, err = http.Post(url, "application/json", bytes.NewReader(body))
if err != nil {
return false, fmt.Errorf("post data to install failed. %v", err)
}
return false, nil
}
})
if err == nil {
klog.Info("Install succeeded")
} else {
klog.Errorf("Install failed. %v", progress.Data)
}
return err
}
func (installer *Installer) GetInstallProgress() (*types.ClusterProgress, error) {
url := fmt.Sprintf("http://%s:8080/api/cluster/global/progress", installer.node.InternalIP)
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("get install progress failed. %v", err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read http response (install progress) body failed. %v", err)
}
progress := new(types.ClusterProgress)
err = json.Unmarshal(data, progress)
if err != nil {
return nil, fmt.Errorf("unmarshal ClusterProgress failed. %v", err)
}
return progress, nil
}