update at 2023/08/14
This commit is contained in:
88
tools/loadmconf/db.go
Normal file
88
tools/loadmconf/db.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"strings"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
var xEngine *xorm.Engine
|
||||
|
||||
func XormConnectDatabaseWithUri(sql string) (*xorm.Engine, error) {
|
||||
// sqlStr := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=true&loc=Local", db.User, db.Password, db.Host, db.Port, db.Name)
|
||||
sqlStr := fmt.Sprintf("%s?charset=utf8&parseTime=true&loc=Local", sql)
|
||||
// fmt.Printf("sqlStr:%s:******@tcp(%s:%s)/%s?charset=utf8&parseTime=true&loc=Local", db.User, db.Host, db.Port, db.Name)
|
||||
var err error
|
||||
xEngine, err = xorm.NewEngine("mysql", sqlStr) //1、Create xorm engine
|
||||
if err != nil {
|
||||
fmt.Println("Failed to connect database:", err)
|
||||
return nil, err
|
||||
}
|
||||
xEngine.ShowSQL(true)
|
||||
return xEngine, nil
|
||||
}
|
||||
|
||||
type MmlCommand struct {
|
||||
// Id int `json:"id" xorm:"pk 'id' autoincr"`
|
||||
NeType string `json:"neType"`
|
||||
Category string `json:"category"`
|
||||
CatDisplay string `json:"display" xorm:"cat_display"`
|
||||
Operation string `json:"operation"`
|
||||
Object string `json:"object"`
|
||||
MmlDisplay string `json:"display" xorm:"mml_display"`
|
||||
ParamJson string `json:"paramJson"`
|
||||
}
|
||||
|
||||
func XormInsertMMLConfig(mapJson *map[string]interface{}, tableName string) (int64, error) {
|
||||
var affected, a int64
|
||||
var err error
|
||||
mmlCommand := new(MmlCommand)
|
||||
|
||||
for n, d := range *mapJson {
|
||||
if d == nil {
|
||||
break
|
||||
}
|
||||
fmt.Println("n:", n)
|
||||
mmlCommand.NeType = strings.ToUpper(n)
|
||||
// for c, ma := range d.(map[string]interface{}) {
|
||||
for c, ci := range d.(map[string]interface{}) {
|
||||
fmt.Println("c:", c)
|
||||
fmt.Println("ci:", ci)
|
||||
mmlCommand.Category = c
|
||||
mmlCommand.CatDisplay = fmt.Sprintf("%v", ci.(map[string]interface{})["display"])
|
||||
mml := ci.(map[string]interface{})["mml"]
|
||||
for m, mi := range mml.([]interface{}) {
|
||||
fmt.Println("m:", m)
|
||||
fmt.Println("mi:", mi)
|
||||
mmlCommand.Operation = fmt.Sprintf("%v", mi.(map[string]interface{})["operation"])
|
||||
mmlCommand.Object = fmt.Sprintf("%v", mi.(map[string]interface{})["object"])
|
||||
mmlCommand.MmlDisplay = fmt.Sprintf("%v", mi.(map[string]interface{})["display"])
|
||||
pj, _ := json.Marshal(mi.(map[string]interface{})["params"])
|
||||
mmlCommand.ParamJson = string(pj)
|
||||
fmt.Println("mmlCommand:", mmlCommand)
|
||||
|
||||
session := xEngine.NewSession()
|
||||
defer session.Close()
|
||||
_, err = session.
|
||||
Table(tableName).
|
||||
Where("ne_type = ? and category = ? and operation = ? and object = ?", mmlCommand.NeType, mmlCommand.Category, mmlCommand.Operation, mmlCommand.Object).
|
||||
Delete()
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to delete %s: %v\n", tableName, err)
|
||||
}
|
||||
a, err = session.Table(tableName).Insert(mmlCommand)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to insert %s: %v\n", tableName, err)
|
||||
}
|
||||
affected += a
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return affected, err
|
||||
}
|
||||
131
tools/loadmconf/load.go
Normal file
131
tools/loadmconf/load.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Yaml struct of config
|
||||
type YamlConfig struct {
|
||||
Logger struct {
|
||||
File string `yaml:"file"`
|
||||
Level string `yaml:"level"`
|
||||
} `yaml:"logger"`
|
||||
|
||||
Rest struct {
|
||||
BindIP string `yaml:"bindip"`
|
||||
Port uint16 `yaml:"port"`
|
||||
} `yaml:"rest"`
|
||||
|
||||
Database struct {
|
||||
Type string `yaml:"type"`
|
||||
User string `yaml:"user"`
|
||||
Password string `yaml:"password"`
|
||||
Host string `yaml:"host"`
|
||||
Port string `yaml:"port"`
|
||||
Name string `yaml:"name"`
|
||||
} `yaml:"database"`
|
||||
|
||||
Mml struct {
|
||||
FileDir string `yaml:"filedir"`
|
||||
Table string `yaml:"table"`
|
||||
} `yaml:"mml"`
|
||||
}
|
||||
|
||||
var yamlConfig YamlConfig
|
||||
|
||||
func ReadConfig(configFile string) {
|
||||
yamlFile, err := ioutil.ReadFile(configFile)
|
||||
if err != nil {
|
||||
fmt.Printf("ioutil.ReadFile %s err %v", configFile, err)
|
||||
}
|
||||
// fmt.Println("yamlfile:", string(yamlFile))
|
||||
|
||||
err = yaml.Unmarshal(yamlFile, &yamlConfig)
|
||||
if err != nil {
|
||||
fmt.Printf("Unmarshal: %v when to struct", err)
|
||||
}
|
||||
}
|
||||
|
||||
func GetYamlConfig() *YamlConfig {
|
||||
return &yamlConfig
|
||||
}
|
||||
|
||||
var mapYaml map[string]interface{}
|
||||
|
||||
func ReadMMLConfig(fileName string) *map[string]interface{} {
|
||||
file, err := ioutil.ReadFile(fileName)
|
||||
if err != nil {
|
||||
fmt.Println("yamlFile.Get err", err)
|
||||
}
|
||||
|
||||
mapYaml = make(map[string]interface{})
|
||||
|
||||
err = yaml.Unmarshal(file, &mapYaml)
|
||||
if err != nil {
|
||||
fmt.Println("yaml.Unmarshal: %v when to struct", err)
|
||||
}
|
||||
// fmt.Println("mapYaml:", mapYaml)
|
||||
|
||||
return &mapYaml
|
||||
}
|
||||
|
||||
func GetAllFile(dir string, s []string) ([]string, error) {
|
||||
fmt.Println("GetAllFile processing...", dir)
|
||||
fmt.Println("dir:", dir)
|
||||
|
||||
rd, err := ioutil.ReadDir(dir)
|
||||
if err != nil {
|
||||
fmt.Println("read dir fail:", err)
|
||||
return s, err
|
||||
}
|
||||
|
||||
for _, fi := range rd {
|
||||
if !fi.IsDir() {
|
||||
fullName := dir + "/" + fi.Name()
|
||||
s = append(s, fullName)
|
||||
}
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
const defaultConfigFile = "./loadmconf.yaml"
|
||||
|
||||
var (
|
||||
version string
|
||||
buildTime string
|
||||
goVer string
|
||||
)
|
||||
|
||||
var pfiles []string
|
||||
|
||||
func init() {
|
||||
cfile := flag.String("c", defaultConfigFile, "config file")
|
||||
pfile := flag.String("m", "", "mmlconf file")
|
||||
pv := flag.Bool("v", false, "print version")
|
||||
ph := flag.Bool("h", false, "print help")
|
||||
|
||||
flag.Parse()
|
||||
if *pv {
|
||||
fmt.Printf("OMC loadmconf version: %s\n%s\n%s\n\n", version, buildTime, goVer)
|
||||
os.Exit(0)
|
||||
}
|
||||
if *ph {
|
||||
flag.Usage()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
ReadConfig(*cfile)
|
||||
|
||||
fmt.Println("pfile:", *pfile)
|
||||
if *pfile != "" {
|
||||
pfiles = append(pfiles, *pfile)
|
||||
} else {
|
||||
pfiles, _ = GetAllFile(yamlConfig.Mml.FileDir, pfiles)
|
||||
}
|
||||
fmt.Println("pfiles:", pfiles)
|
||||
}
|
||||
BIN
tools/loadmconf/loadmconf
Normal file
BIN
tools/loadmconf/loadmconf
Normal file
Binary file not shown.
21
tools/loadmconf/loadmconf.go
Normal file
21
tools/loadmconf/loadmconf.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("========================= OMC loadmconf startup =========================")
|
||||
fmt.Println("OMC loadmconf version: %s %s %s", version, buildTime, goVer)
|
||||
|
||||
for _, f := range pfiles {
|
||||
fmt.Println("f:", f)
|
||||
mmlMap := ReadMMLConfig(f)
|
||||
fmt.Println("mmlMap:", mmlMap)
|
||||
db := yamlConfig.Database
|
||||
sqlStr := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", db.User, db.Password, db.Host, db.Port, db.Name)
|
||||
XormConnectDatabaseWithUri(sqlStr)
|
||||
XormInsertMMLConfig(mmlMap, yamlConfig.Mml.Table)
|
||||
}
|
||||
|
||||
}
|
||||
19
tools/loadmconf/loadmconf.yaml
Normal file
19
tools/loadmconf/loadmconf.yaml
Normal file
@@ -0,0 +1,19 @@
|
||||
logger:
|
||||
file: ./loadmconf.log
|
||||
level: trace
|
||||
|
||||
rest:
|
||||
bindip: 0.0.0.0
|
||||
port: 3040
|
||||
|
||||
database:
|
||||
type: mysql
|
||||
user: root
|
||||
password: 1000omc@kp!
|
||||
host: 127.0.0.1
|
||||
port: 33066
|
||||
name: omc_db
|
||||
|
||||
mml:
|
||||
filedir: ../../config/mml/omc
|
||||
table: mml_command
|
||||
22
tools/loadmconf/makefile
Normal file
22
tools/loadmconf/makefile
Normal file
@@ -0,0 +1,22 @@
|
||||
# Makefile for rest agent project
|
||||
|
||||
PROJECT = OMC
|
||||
VERSION = 5GC16.1.1
|
||||
PLATFORM = amd64
|
||||
DEBDIR = ../../../deb
|
||||
ETCDIR = ../../../etc
|
||||
RELEASEDIR = ../../../release
|
||||
LIBDIR = ../lib
|
||||
BINNAME = loadmconf
|
||||
|
||||
.PHONY: build $(BINNAME)
|
||||
build $(BINNAME):
|
||||
go build -o $(BINNAME) -v -ldflags "-X 'main.version=$(VERSION)' \
|
||||
-X 'main.buildTime=`date`' \
|
||||
-X 'main.goVer=`go version`'"
|
||||
|
||||
run: $(BINNAME)
|
||||
./$(BINNAME)
|
||||
|
||||
clean:
|
||||
rm ./$(BINNAME)
|
||||
Reference in New Issue
Block a user