Skip to content

Commit

Permalink
add active api (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
Meteriox committed Jun 27, 2023
1 parent 7cb19cd commit f18e4d8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
19 changes: 10 additions & 9 deletions initz/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (active *Activate) activating() error {
}

// Report reports info
func (active *Activate) activate() {
func (active *Activate) activate() error {
info := v1.ActiveRequest{
BatchName: active.batch.name,
Namespace: active.batch.namespace,
Expand All @@ -147,17 +147,17 @@ func (active *Activate) activate() {
fv, err := active.collect()
if err != nil {
active.log.Error("failed to get fingerprint value", log.Error(err))
return
return err
}
if fv == "" {
active.log.Error("fingerprint value is null", log.Error(err))
return
return errors.New("fingerprint value is null")
}
info.FingerprintValue = fv
data, err := json.Marshal(info)
if err != nil {
active.log.Error("failed to marshal activate info", log.Error(err))
return
return err
}
active.log.Debug("active", log.Any("info data", string(data)))

Expand All @@ -168,33 +168,34 @@ func (active *Activate) activate() {
resp, err := active.http.PostURL(url, bytes.NewReader(data), headers)
if err != nil {
active.log.Error("failed to send activate data", log.Error(err))
return
return err
}
data, err = http.HandleResponse(resp)
if err != nil {
active.log.Error("failed to send activate data", log.Error(err))
return
return err
}
var res v1.ActiveResponse
err = json.Unmarshal(data, &res)
if err != nil {
active.log.Error("failed to unmarshal activate response data returned", log.Error(err))
return
return err
}

if err = genCert(&active.cfg.Node, res.Certificate); err != nil {
active.log.Error("failed to create cert file", log.Error(err))
return
return err
}

if active.cfg.Plugin.Link == LinkMqtt {
if err = genCert(&active.cfg.MqttLink.Cert, res.MqttCert); err != nil {
active.log.Error("failed to create mqtt cert file", log.Error(err))
return
return err
}
}

active.sig <- true
return nil
}

func genCert(path *v2utils.Certificate, c v2utils.Certificate) error {
Expand Down
53 changes: 53 additions & 0 deletions initz/activate_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (active *Activate) startServer() error {
mux := http.NewServeMux()
mux.HandleFunc("/", active.handleView)
mux.HandleFunc("/update", active.handleUpdate)
mux.HandleFunc("/active", active.handleActive)
srv := &http.Server{}
srv.Handler = mux
srv.Addr = active.cfg.Init.Active.Collector.Server.Listen
Expand Down Expand Up @@ -112,3 +113,55 @@ func (active *Activate) handleUpdate(w http.ResponseWriter, req *http.Request) {
return
}
}

func (active *Activate) handleActive(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(w, "post only", http.StatusMethodNotAllowed)
return
}
err := req.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
attributes := make(map[string]string)
for _, attr := range active.cfg.Init.Active.Collector.Attributes {
val := req.Form.Get(attr.Name)
if val == "" {
attributes[attr.Name] = attr.Value
} else {
attributes[attr.Name] = val
}
}
for _, ni := range active.cfg.Init.Active.Collector.NodeInfo {
val := req.Form.Get(ni.Name)
attributes[ni.Name] = val
}
for _, si := range active.cfg.Init.Active.Collector.Serial {
val := req.Form.Get(si.Name)
attributes[si.Name] = val
}
active.log.Info("active", log.Any("server attrs", attributes))
active.attrs = attributes

if batchName, ok := attributes["batch"]; ok {
active.batch.name = batchName
}
if ns, ok := attributes["namespace"]; ok {
active.batch.namespace = ns
}
if initAddr, ok := attributes["initAddr"]; ok {
active.cfg.Init.Active.Address = initAddr

}
if syncAddr, ok := attributes["syncAddr"]; ok {
os.Setenv(KeyBaetylSyncAddr, syncAddr)
}

err = active.activate()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte("active success"))
}

0 comments on commit f18e4d8

Please sign in to comment.