Skip to content

Commit 8ab0f48

Browse files
committed
Add support physics machine as vmnode
1 parent e685915 commit 8ab0f48

File tree

8 files changed

+99
-42
lines changed

8 files changed

+99
-42
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
/down/upload/*
1111
/cmd/test/_const/const.go
1212
/cmd/test/_const/const_secret.go
13+
.vscode/launch.json

cmd/host/router/v1/register.go

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type VmNotifyResp struct {
2020

2121
type VmRegisterReq struct {
2222
Token string `json:"token"`
23+
Secret string `json:"secret"`
2324
MacAddress string `json:"macAddress"`
2425
Ip string `json:"ip"`
2526
AgentPortOnHost int `json:"agentPortOnHost"`

cmd/vm/router/handler/service.go

+27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package vmHandler
22

33
import (
4+
hostV1 "github.com/easysoft/zagent/cmd/host/router/v1"
45
v1 "github.com/easysoft/zagent/cmd/vm/router/v1"
6+
agentConf "github.com/easysoft/zagent/internal/pkg/conf"
57
consts "github.com/easysoft/zagent/internal/pkg/const"
68
vmAgentService "github.com/easysoft/zagent/internal/vm/service"
79
_httpUtils "github.com/easysoft/zagent/pkg/lib/http"
@@ -60,3 +62,28 @@ func (c *ServiceCtrl) Setup(ctx iris.Context) {
6062
ctx.JSON(_httpUtils.RespData(consts.ResultPass, "success", resp))
6163
return
6264
}
65+
66+
// @summary ztf心跳并请求Token
67+
// @Accept json
68+
// @Produce json
69+
// @Param VmNotifyReq body v1.VmNotifyReq true "Vm Notify Request Object"
70+
// @Success 200 {object} _domain.Response{data=v1.VmNotifyResp} "code = success | fail"
71+
// @Router /api/v1/virtual/notifyHost [post]
72+
func (c *ServiceCtrl) Heartbeat(ctx iris.Context) {
73+
req := hostV1.VmNotifyReq{}
74+
if err := ctx.ReadJSON(&req); err != nil {
75+
ctx.JSON(_httpUtils.RespData(consts.ResultFail, err.Error(), nil))
76+
return
77+
}
78+
79+
if req.MacAddress != agentConf.Inst.MacAddress {
80+
return
81+
}
82+
data := hostV1.VmNotifyResp{
83+
Token: consts.AuthToken,
84+
}
85+
86+
data.Server = agentConf.Inst.Server
87+
88+
ctx.JSON(_httpUtils.RespData(consts.ResultPass, "success to refresh token", data))
89+
}

cmd/vm/router/router.go

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func (r *Router) App() {
2525
{
2626
v1 := app.Party("/v1")
2727
{
28+
v1.PartyFunc("/virtual", func(client iris.Party) {
29+
client.Post("/notifyHost", r.ServiceCtrl.Heartbeat).Name = "ztf心跳&获取token"
30+
})
2831
// v1.Use(core.Auth())
2932

3033
v1.PartyFunc("/service", func(client iris.Party) {

internal/vm/service/tool.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,16 @@ func (s *ToolService) startProcess(name, execPath, uuid, ip, server, secret stri
215215
}
216216
execDir := _fileUtils.GetAbsolutePath(filepath.Dir(execPath))
217217

218+
hostServerUrl := fmt.Sprintf("http://%s:%d", consts.KvmHostIpInNatNetwork, consts.AgentHostServicePort)
219+
if agentConf.Inst.Secret != "" {
220+
hostServerUrl = fmt.Sprintf("http://127.0.0.1:%d", consts.AgentVmServicePort)
221+
}
218222
cmdStr := ""
219223
var cmd *exec.Cmd
220224
if _commonUtils.IsWin() {
221225
if name == "ztf" {
222-
tmpl := `start cmd /c %s -p %d -h http://%s:%d -i %s -uuid %s ^1^> %snohup.%s.log ^2^>^&^1`
223-
cmdStr = fmt.Sprintf(tmpl, execPath, consts.ZtfServicePort, consts.KvmHostIpInNatNetwork, consts.AgentHostServicePort, ip, uuid, consts.WorkDir, name)
226+
tmpl := `start cmd /c %s -p %d -h %s -i %s -uuid %s ^1^> %snohup.%s.log ^2^>^&^1`
227+
cmdStr = fmt.Sprintf(tmpl, execPath, consts.ZtfServicePort, hostServerUrl, ip, uuid, consts.WorkDir, name)
224228
} else if name == "zd" { // set root for workdir
225229
tmpl := `start cmd /c %s -p %d -b %s -uuid %s ^1^> %snohup.%s.log ^2^>^&^1`
226230
cmdStr = fmt.Sprintf(tmpl, execPath, consts.ZdServicePort, ip, uuid, consts.WorkDir, name)
@@ -231,7 +235,7 @@ func (s *ToolService) startProcess(name, execPath, uuid, ip, server, secret stri
231235
} else {
232236
if name == "ztf" {
233237
cmd = exec.Command("nohup", execPath, "-uuid", uuid,
234-
"-h", "http://"+consts.KvmHostIpInNatNetwork+":"+strconv.Itoa(consts.AgentHostServicePort), "-i", ip, "-p", strconv.Itoa(consts.ZtfServicePort))
238+
"-h", hostServerUrl, "-i", ip, "-p", strconv.Itoa(consts.ZtfServicePort))
235239
} else if name == "zd" {
236240
cmd = exec.Command("nohup", execPath, "-uuid", uuid, "-b", ip, "-p", strconv.Itoa(consts.ZdServicePort))
237241
}

internal/vm/service/vm.go

+52-17
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import (
1212
"github.com/easysoft/zagent/internal/pkg/domain"
1313
agentService "github.com/easysoft/zagent/internal/pkg/service"
1414
agentTestingService "github.com/easysoft/zagent/internal/pkg/service/testing"
15+
natHelper "github.com/easysoft/zagent/internal/pkg/utils/net"
1516
requestUtils "github.com/easysoft/zagent/internal/pkg/utils/request"
1617
_domain "github.com/easysoft/zagent/pkg/domain"
1718
_commonUtils "github.com/easysoft/zagent/pkg/lib/common"
19+
_dateUtils "github.com/easysoft/zagent/pkg/lib/date"
1820
_httpUtils "github.com/easysoft/zagent/pkg/lib/http"
1921
_i118Utils "github.com/easysoft/zagent/pkg/lib/i118"
2022
_logUtils "github.com/easysoft/zagent/pkg/lib/log"
@@ -73,7 +75,11 @@ func (s *VmService) Register(isBusy bool) (ok bool) {
7375
vm.Status = consts.VmReady
7476
}
7577

76-
if consts.AuthSecret == "" || consts.ExpiredDate.Unix() < time.Now().Unix() { // re-apply token using secret
78+
serverUrl := ""
79+
if agentConf.Inst.Secret == "" { // re-apply token using secret
80+
serverUrl = _httpUtils.GenUrl(
81+
fmt.Sprintf("http://%s:%d/", consts.KvmHostIpInNatNetwork, consts.AgentHostServicePort),
82+
"virtual/notifyHost")
7783
var err error
7884
vm.Token, vm.Ip, vm.AgentPortOnHost, err = s.notifyHost()
7985
consts.AuthToken = vm.Token
@@ -82,37 +88,66 @@ func (s *VmService) Register(isBusy bool) (ok bool) {
8288
_logUtils.Info(_i118Utils.I118Prt.Sprintf("fail_to_notify", "error or return empty value"))
8389
return
8490
}
91+
} else {
92+
host := domain.HostNode{
93+
Node: domain.Node{
94+
Ip: agentConf.Inst.NodeIp,
95+
Port: agentConf.Inst.NodePort,
96+
},
97+
Status: consts.HostOnline,
98+
Vms: []domain.Vm{},
99+
}
100+
if consts.AuthToken == "" || consts.ExpiredDate.Unix() < time.Now().Unix() { // re-apply token using secret
101+
host.Secret = agentConf.Inst.Secret
102+
}
103+
104+
domainVm := domain.Vm{
105+
Port: agentConf.Inst.NodePort,
106+
Status: consts.VmRunning,
107+
MacAddress: vm.MacAddress,
108+
AgentPortOnHost: agentConf.Inst.NodePort,
109+
VncPortOnHost: 0,
110+
ZtfPortOnHost: 0,
111+
ZdPortOnHost: 0,
112+
SshPortOnHost: 0,
113+
Ip: vm.Ip,
114+
}
115+
domainVm.ZtfPortOnHost, _ = natHelper.GetUsedPortByKeyword("ztf", agentConf.Inst.ZtfPort)
116+
host.Vms = append(host.Vms, domainVm)
117+
118+
serverUrl = requestUtils.GenUrl(agentConf.Inst.Server, "api.php/v1/host/heartbeat")
119+
120+
respBytes, ok := s.register(host)
121+
122+
if ok {
123+
respObj := v1.RegisterResp{}
124+
err := json.Unmarshal(respBytes, &respObj)
125+
if err == nil && respObj.Token != "" {
126+
respObj.TokenTime, _ = _dateUtils.UnitToDate(respObj.TokenTimeUnix)
127+
consts.AuthToken = respObj.Token
128+
consts.ExpiredDate = respObj.TokenTime
129+
}
130+
}
85131
}
86132

87-
// respBytes, ok := s.register(vm)
88-
89-
// if ok {
90-
// respObj := v1.RegisterResp{}
91-
// err := json.Unmarshal(respBytes, &respObj)
92-
// if err == nil && respObj.Token != "" {
93-
// respObj.TokenTime, _ = _dateUtils.UnitToDate(respObj.TokenTimeUnix)
94-
// consts.AuthToken = respObj.Token
95-
// consts.ExpiredDate = respObj.TokenTime
96-
// }
97-
// }
98133
ok = true
99134
if consts.AuthToken == "" {
100135
ok = false
101136
}
102137

103138
if ok {
104-
_logUtils.Info(_i118Utils.I118Prt.Sprintf("success_to_register", agentConf.Inst.Server))
139+
_logUtils.Info(_i118Utils.I118Prt.Sprintf("success_to_register", serverUrl))
105140
} else {
106-
_logUtils.Info(_i118Utils.I118Prt.Sprintf("fail_to_register", agentConf.Inst.Server))
141+
_logUtils.Info(_i118Utils.I118Prt.Sprintf("fail_to_register", serverUrl))
107142
}
108143

109144
return
110145
}
111146

112-
func (s *VmService) register(host interface{}) (resp []byte, ok bool) {
113-
url := requestUtils.GenUrl(agentConf.Inst.Server, "api.php/v1/zanode/heartbeat")
147+
func (s *VmService) register(vm interface{}) (resp []byte, ok bool) {
148+
url := requestUtils.GenUrl(agentConf.Inst.Server, "api.php/v1/host/heartbeat")
114149

115-
resp, err := _httpUtils.Post(url, host)
150+
resp, err := _httpUtils.Post(url, vm)
116151
ok = err == nil
117152

118153
return

pkg/lib/http/http.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ func IsSuccessCode(code int) (success bool) {
140140

141141
func GenUrl(server string, path string) string {
142142
server = AddUrlPostFixIfNeeded(server)
143-
url := fmt.Sprintf("%sapi/v1/%s", server, path)
143+
url := ""
144+
if strings.Contains(path, "api.php") {
145+
url = fmt.Sprintf("%s%s", server, path)
146+
} else {
147+
url = fmt.Sprintf("%sapi/v1/%s", server, path)
148+
}
144149
url = AddUrlPostFixIfNeeded(url)
145150
return url
146151
}

res/setup/zagent.sh

+2-21
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ print_res(){
1616
if $is_install_zvm && $is_success_zvm;then
1717
echo -e "\033[32m Install zagent success \033[0m"
1818
fi
19-
if $is_install_ztf && $is_success_ztf;then
20-
echo -e "\033[32m Install ztf success \033[0m"
21-
fi
2219
if $is_install_nginx && $is_success_nginx;then
2320
echo -e "\033[32m Install nginx success \033[0m"
2421
fi
@@ -251,17 +248,7 @@ download_zvm()
251248

252249
return 1
253250
}
254-
install_ztf(){
255-
sleep 3s
256-
result=$(curl http://127.0.0.1:55201/api/v1/service/setup -X POST -d '{"name":"ztf","secret":"'"$secret"'","server":"'"$zentaoSite"'"}' --header "Content-Type:application/json" | grep success)
257-
if [[ "$result" != "" ]]
258-
then
259-
is_success_ztf=true
260-
else
261-
echo -e "\033[31m Install ztf error. \033[0m"
262-
exit 1
263-
fi
264-
}
251+
265252
install_zvm()
266253
{
267254
cd ${HOME}/zagent
@@ -274,7 +261,6 @@ install_zvm()
274261
if service_is_inactive zagent-vm;then
275262
sudo systemctl start zagent-vm
276263
fi
277-
install_ztf
278264
return
279265
fi
280266
fi
@@ -301,7 +287,7 @@ Wants=network-online.target
301287
[Service]
302288
User=${USER}
303289
Type=forking
304-
ExecStart=/bin/bash -c "${HOME}/zagent/zagent-vm -p 55201 -s ${zentaoSite} > /dev/null 2>&1 &"
290+
ExecStart=/bin/bash -c "${HOME}/zagent/zagent-vm -p 55201 -secret ${secret} -s ${zentaoSite} > /dev/null 2>&1 &"
305291
306292
[Install]
307293
WantedBy=multi-user.target
@@ -319,8 +305,6 @@ EOF
319305
sudo systemctl start zagent-vm
320306
ck_ok "Start Zagent-vm"
321307
fi
322-
323-
install_ztf
324308
}
325309

326310
download_novnc()
@@ -776,7 +760,6 @@ fi
776760

777761
is_install_zagent=true
778762
is_install_zvm=false
779-
is_install_ztf=false
780763
is_install_nginx=true
781764
is_install_kvm=true
782765
is_install_novnc=true
@@ -806,7 +789,6 @@ do
806789
soft=$OPTARG
807790
is_install_zagent=false
808791
is_install_zvm=false
809-
is_install_ztf=false
810792
is_install_nginx=false
811793
is_install_kvm=false
812794
is_install_novnc=false
@@ -816,7 +798,6 @@ do
816798
fi
817799
if [[ $OPTARG =~ zvm ]];then
818800
is_install_zvm=true
819-
is_install_ztf=true
820801
fi
821802
if [[ $OPTARG =~ nginx ]];then
822803
is_install_nginx=true

0 commit comments

Comments
 (0)