@@ -12,9 +12,11 @@ import (
12
12
"github.com/easysoft/zagent/internal/pkg/domain"
13
13
agentService "github.com/easysoft/zagent/internal/pkg/service"
14
14
agentTestingService "github.com/easysoft/zagent/internal/pkg/service/testing"
15
+ natHelper "github.com/easysoft/zagent/internal/pkg/utils/net"
15
16
requestUtils "github.com/easysoft/zagent/internal/pkg/utils/request"
16
17
_domain "github.com/easysoft/zagent/pkg/domain"
17
18
_commonUtils "github.com/easysoft/zagent/pkg/lib/common"
19
+ _dateUtils "github.com/easysoft/zagent/pkg/lib/date"
18
20
_httpUtils "github.com/easysoft/zagent/pkg/lib/http"
19
21
_i118Utils "github.com/easysoft/zagent/pkg/lib/i118"
20
22
_logUtils "github.com/easysoft/zagent/pkg/lib/log"
@@ -73,7 +75,11 @@ func (s *VmService) Register(isBusy bool) (ok bool) {
73
75
vm .Status = consts .VmReady
74
76
}
75
77
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" )
77
83
var err error
78
84
vm .Token , vm .Ip , vm .AgentPortOnHost , err = s .notifyHost ()
79
85
consts .AuthToken = vm .Token
@@ -82,37 +88,66 @@ func (s *VmService) Register(isBusy bool) (ok bool) {
82
88
_logUtils .Info (_i118Utils .I118Prt .Sprintf ("fail_to_notify" , "error or return empty value" ))
83
89
return
84
90
}
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
+ }
85
131
}
86
132
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
- // }
98
133
ok = true
99
134
if consts .AuthToken == "" {
100
135
ok = false
101
136
}
102
137
103
138
if ok {
104
- _logUtils .Info (_i118Utils .I118Prt .Sprintf ("success_to_register" , agentConf . Inst . Server ))
139
+ _logUtils .Info (_i118Utils .I118Prt .Sprintf ("success_to_register" , serverUrl ))
105
140
} else {
106
- _logUtils .Info (_i118Utils .I118Prt .Sprintf ("fail_to_register" , agentConf . Inst . Server ))
141
+ _logUtils .Info (_i118Utils .I118Prt .Sprintf ("fail_to_register" , serverUrl ))
107
142
}
108
143
109
144
return
110
145
}
111
146
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" )
114
149
115
- resp , err := _httpUtils .Post (url , host )
150
+ resp , err := _httpUtils .Post (url , vm )
116
151
ok = err == nil
117
152
118
153
return
0 commit comments