package slownet
import (
"fmt"
"os"
"path/filepath"
"ascend-common/common-utils/hwlog"
"ascend-common/common-utils/utils"
)
const (
rasNetRootPathKey = "RAS_NET_ROOT_PATH"
netFaultSubPath = "cluster"
detectConf = "cathelper.conf"
)
func GetRasNetRootPath() (string, error) {
rootPath := os.Getenv(rasNetRootPathKey)
if len(rootPath) == 0 {
return "", fmt.Errorf("env %s not exists, please config it before starting", rasNetRootPathKey)
}
if !utils.IsDir(rootPath) {
return "", fmt.Errorf("env %s=%s, which is not dir", rasNetRootPathKey, rootPath)
}
safeRootPath, err := utils.CheckPath(rootPath)
if err != nil {
return "", fmt.Errorf("env %s=%s, which is invalid, err: %v", rasNetRootPathKey, rootPath, err)
}
return safeRootPath, nil
}
func GetPingListFilePath(superPodId, serverIndex string) (string, error) {
rootPath, err := GetRasNetRootPath()
if err != nil {
return "", err
}
return filepath.Join(rootPath, netFaultSubPath, fmt.Sprintf("super-pod-%s", superPodId),
fmt.Sprintf("ping_list_%s.json", serverIndex)), nil
}
func GetSuperPodInfoFilePath(superPodID, superPodPrefix string) (string, error) {
rootPath, err := GetRasNetRootPath()
if err != nil {
hwlog.RunLog.Errorf("get ras net root path failed, err : %v", err)
return "", err
}
superPodPathName := fmt.Sprintf("%s-%s", superPodPrefix, superPodID)
fileName := fmt.Sprintf("%s.json", superPodPathName)
filePath := filepath.Join(rootPath, netFaultSubPath, superPodPathName, fileName)
if _, errInfo := utils.CheckPath(filePath); errInfo != nil {
hwlog.RunLog.Errorf("file path is invalid, err: %v", errInfo)
return "", errInfo
}
return filePath, nil
}
func GetConfigPathForDetect(superPodId string) (string, error) {
rasNetRootPath, err := GetRasNetRootPath()
if err != nil {
hwlog.RunLog.Errorf("get ras net root path failed, err: %v", err)
return "", err
}
confPath := filepath.Join(rasNetRootPath, netFaultSubPath, fmt.Sprintf("super-pod-%s", superPodId), detectConf)
if _, errInfo := utils.CheckPath(confPath); errInfo != nil {
hwlog.RunLog.Errorf("file path is invalid, err: %v", errInfo)
return "", errInfo
}
return confPath, nil
}