fix: 补充缺失代码
This commit is contained in:
@@ -5,7 +5,9 @@ package cm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os/exec"
|
||||
"time"
|
||||
|
||||
"ems.agt/lib/log"
|
||||
)
|
||||
@@ -64,3 +66,30 @@ func ExecOsCmd(command, os string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func StartSSHCmdWithTimeout(duration int, sshHost, cmdStr string) error {
|
||||
timeout := time.Duration(duration) * time.Second
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout) // 设置超时
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "ssh", sshHost, cmdStr)
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func RunSSHCmd(sshHost, cmdStr string) error {
|
||||
cmd := exec.Command("ssh", sshHost, cmdStr)
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,7 +4,10 @@
|
||||
package cm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os/exec"
|
||||
"time"
|
||||
|
||||
"ems.agt/lib/log"
|
||||
)
|
||||
@@ -51,3 +54,30 @@ func ExecOsCmd(command, os string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func StartSSHCmdWithTimeout(duration int, sshHost, cmdStr string) error {
|
||||
timeout := time.Duration(duration) * time.Second
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout) // 设置超时
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "ssh", sshHost, cmdStr)
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func RunSSHCmd(sshHost, cmdStr string) error {
|
||||
cmd := exec.Command("ssh", sshHost, cmdStr)
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"ems.agt/lib/log"
|
||||
"ems.agt/lib/services"
|
||||
"ems.agt/restagent/config"
|
||||
tokenConst "ems.agt/src/framework/constants/token"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/gorilla/mux"
|
||||
@@ -22,6 +23,7 @@ import (
|
||||
|
||||
var (
|
||||
UriParamOmcNeConfig = config.DefaultUriPrefix + "/systemManagement/v1/elementType/%s/objectType/config/omcNeConfig"
|
||||
UriOmcNeConfig = config.DefaultUriPrefix + "/systemManagement/v1/elementType/omc/objectType/config/omcNeConfig"
|
||||
// NE CM export/import
|
||||
NeCmUri = config.DefaultUriPrefix + "/systemManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/cm"
|
||||
// NE info
|
||||
@@ -34,6 +36,7 @@ var (
|
||||
UriNeInstance = config.DefaultUriPrefix + "/systemManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/instance/{action}"
|
||||
|
||||
CustomUriParamOmcNeConfig = config.UriPrefix + "/systemManagement/v1/elementType/%s/objectType/config/omcNeConfig"
|
||||
CustomUriOmcNeConfig = config.UriPrefix + "/systemManagement/v1/elementType/omc/objectType/config/omcNeConfig"
|
||||
CustomNeCmUri = config.UriPrefix + "/systemManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/cm"
|
||||
CustomUriNeInfo = config.UriPrefix + "/systemManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/neInfo"
|
||||
CustomUriNeCmFile = config.UriPrefix + "/systemManagement/{apiVersion}/{neType}/neBackup/{fileName}"
|
||||
@@ -125,6 +128,7 @@ func PostNeInfo(w http.ResponseWriter, r *http.Request) {
|
||||
services.ResponseNotFound404UriNotExist(w, r)
|
||||
return
|
||||
}
|
||||
neTypeUpper := strings.ToUpper(neType)
|
||||
syncFlag := services.GetUriParamString(r, "sync2ne", ",", false, false)
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen))
|
||||
if err != nil {
|
||||
@@ -145,7 +149,7 @@ func PostNeInfo(w http.ResponseWriter, r *http.Request) {
|
||||
log.Debug("NE info:", neInfo)
|
||||
|
||||
//if !config.GetYamlConfig().OMC.Chk2Ne {
|
||||
if syncFlag == "false" {
|
||||
if syncFlag == "false" || neTypeUpper == config.GetYamlConfig().OMC.NeType {
|
||||
neInfo.Status = NEStatusMaintain
|
||||
affected, err := dborm.XormInsertNeInfo(neInfo)
|
||||
if err != nil {
|
||||
@@ -231,6 +235,8 @@ func PutNeInfo(w http.ResponseWriter, r *http.Request) {
|
||||
services.ResponseNotFound404UriNotExist(w, r)
|
||||
return
|
||||
}
|
||||
neTypeLower := strings.ToLower(neType)
|
||||
neTypeUpper := strings.ToUpper(neType)
|
||||
syncFlag := services.GetUriParamString(r, "sync2ne", ",", false, false)
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen))
|
||||
if err != nil {
|
||||
@@ -246,7 +252,7 @@ func PutNeInfo(w http.ResponseWriter, r *http.Request) {
|
||||
log.Debug("NE info:", neInfo)
|
||||
|
||||
//if !config.GetYamlConfig().OMC.Chk2Ne {
|
||||
if syncFlag == "false" {
|
||||
if syncFlag == "false" || neTypeUpper == config.GetYamlConfig().OMC.NeType {
|
||||
neInfo.Status = NEStatusMaintain
|
||||
affected, err := dborm.XormUpdateNeInfo(neInfo)
|
||||
if err != nil {
|
||||
@@ -263,7 +269,7 @@ func PutNeInfo(w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
hostUri := global.CombineHostUri(neInfo.Ip, neInfo.Port)
|
||||
//hostUri := fmt.Sprintf("http://%s:%v", neInfo.Ip, neInfo.Port)
|
||||
apiUri := fmt.Sprintf(UriParamOmcNeConfig, strings.ToLower(neType))
|
||||
apiUri := fmt.Sprintf(UriParamOmcNeConfig, neTypeLower)
|
||||
requestURI2NF := fmt.Sprintf("%s%s", hostUri, apiUri)
|
||||
log.Debug("requestURI2NF:", requestURI2NF)
|
||||
|
||||
@@ -279,6 +285,7 @@ func PutNeInfo(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ = json.Marshal(omcNeConfig)
|
||||
response, err := client.R().
|
||||
EnableTrace().
|
||||
SetHeaders(map[string]string{tokenConst.HEADER_KEY: r.Header.Get(tokenConst.HEADER_KEY)}).
|
||||
SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
|
||||
SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
|
||||
SetBody(body).
|
||||
@@ -316,6 +323,28 @@ func PutNeInfo(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func PutOMCNeConfig(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
neType := vars["elementTypeValue"]
|
||||
if neType == "" {
|
||||
log.Error("elementTypeValue is empty")
|
||||
services.ResponseNotFound404UriNotExist(w, r)
|
||||
return
|
||||
}
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen))
|
||||
if err != nil {
|
||||
log.Error("Failed to io.ReadAll:", err)
|
||||
services.ResponseNotFound404UriNotExist(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
neInfo := new(dborm.NeInfo)
|
||||
_ = json.Unmarshal(body, neInfo)
|
||||
neInfo.NeType = strings.ToUpper(neType)
|
||||
|
||||
services.ResponseStatusOK204NoContent(w)
|
||||
}
|
||||
|
||||
func DeleteNeInfo(w http.ResponseWriter, r *http.Request) {
|
||||
log.Debug("DeleteNeInfo processing... ")
|
||||
|
||||
|
||||
@@ -48,9 +48,9 @@ func GetParamConfigFromNF(w http.ResponseWriter, r *http.Request) {
|
||||
getNeInfoURI := restHostPort + getNeInfoPattern
|
||||
neId := services.GetUriParamString(r, "ne_id", ",", true, true)
|
||||
if neId == "" {
|
||||
getNeInfoURI = getNeInfoURI + fmt.Sprintf("?WHERE=status='0'+and+ne_type='%s'", neType)
|
||||
getNeInfoURI = getNeInfoURI + fmt.Sprintf("?WHERE=status+in+('0','3')+and+ne_type='%s'", neType)
|
||||
} else {
|
||||
getNeInfoURI = getNeInfoURI + fmt.Sprintf("?WHERE=status='0'+and+ne_type='%v'+and+ne_id+in+%v", neType, neId)
|
||||
getNeInfoURI = getNeInfoURI + fmt.Sprintf("?WHERE=status+in+('0','3')+and+ne_type='%v'+and+ne_id+in+%v", neType, neId)
|
||||
}
|
||||
log.Debug("getNeInfoURI:", getNeInfoURI)
|
||||
|
||||
@@ -63,11 +63,11 @@ func GetParamConfigFromNF(w http.ResponseWriter, r *http.Request) {
|
||||
SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
|
||||
Get(getNeInfoURI)
|
||||
if err != nil {
|
||||
log.Error("Get from database is failure!")
|
||||
services.ResponseInternalServerError500NFConnectRefused(w)
|
||||
log.Error("Failed to Get:", err)
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
return
|
||||
}
|
||||
log.Debug("NE info:", string(resp.Body()))
|
||||
log.Trace("NE info:", string(resp.Body()))
|
||||
|
||||
// var neList []dborm.NeInfo
|
||||
neList, _ := dborm.XormParseResult(resp.Body())
|
||||
|
||||
@@ -699,14 +699,32 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
} else if fileType == 2 {
|
||||
dpkgCmd := fmt.Sprintf("sudo dpkg -i --force-all '%s'", filePath)
|
||||
cmd := exec.Command("ssh", sshHost, dpkgCmd)
|
||||
out, err := cmd.CombinedOutput()
|
||||
log.Debugf("Exec output: %v", string(out))
|
||||
err := RunSSHCmd(sshHost, dpkgCmd)
|
||||
if err != nil {
|
||||
log.Error("Faile to execute dpkg command:", err)
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
return
|
||||
}
|
||||
// timeout := time.Duration(config.GetYamlConfig().OMC.CmdTimeout) * time.Second
|
||||
// ctx, cancel := context.WithTimeout(context.Background(), timeout) // 设置超时
|
||||
// defer cancel()
|
||||
// cmd := exec.CommandContext(ctx, "ssh", sshHost, dpkgCmd)
|
||||
// var stdout, stderr bytes.Buffer
|
||||
// cmd.Stdout = &stdout
|
||||
// cmd.Stderr = &stderr
|
||||
// err := cmd.Start()
|
||||
// if err != nil {
|
||||
// log.Error("Faile to execute dpkg command: %v, err: %s", err, stderr.String())
|
||||
// services.ResponseInternalServerError500ProcessError(w, err)
|
||||
// return
|
||||
// }
|
||||
// out, err := cmd.CombinedOutput()
|
||||
// log.Debugf("Exec output: %v", string(out))
|
||||
// if err != nil {
|
||||
// log.Error("Faile to execute dpkg command:", err)
|
||||
// services.ResponseInternalServerError500ProcessError(w, err)
|
||||
// return
|
||||
// }
|
||||
} else {
|
||||
err := global.ErrCMUnknownSoftwareFormat
|
||||
log.Error(err)
|
||||
|
||||
Reference in New Issue
Block a user