ソースを参照

优化增强yfkj-upgrade.service,新增查看当前升级配置文件的rpc接口

niujiuru 2 時間 前
親
コミット
44b9abd8a6

+ 2 - 1
upgrade.service/main.go

@@ -17,7 +17,7 @@ import (
 )
 
 var (
-	Version   = "0.0.0.1"
+	Version   = "0.0.0.2"
 	BuildTime = ""
 )
 
@@ -62,6 +62,7 @@ func (p *program) Start(s service.Service) error {
 	_saveLogConf := servicelib.SaveLogConf
 	methods["basic.saveLogConf"] = _saveLogConf // 保存日志设置
 	//////////////////////////////////////////////////////////
+	methods["core.getUpgradeConf"] = getUpgradeConf //////////
 
 	err = p.core_server.RegisterMethods(methods)
 	if err != nil {

+ 25 - 0
upgrade.service/rpc_handler.go

@@ -0,0 +1,25 @@
+package main
+
+import (
+	"context"
+	"encoding/json"
+	"os"
+	"path/filepath"
+
+	"hnyfkj.com.cn/rtu/linux/baseapp"
+	"hnyfkj.com.cn/rtu/linux/utils/jsonrpc2"
+)
+
+func getUpgradeConf(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
+	data, err := os.ReadFile(filepath.Join(baseapp.CFG_DIR, "servers.json"))
+	if err != nil {
+		return jsonrpc2.BuildError(req, jsonrpc2.ErrInternal, err.Error())
+	}
+
+	var result json.RawMessage
+	if err := json.Unmarshal(data, &result); err != nil {
+		return jsonrpc2.BuildError(req, jsonrpc2.ErrInternal, err.Error())
+	}
+
+	return jsonrpc2.BuildResponse(req, result, nil)
+}

+ 3 - 0
upgrade.service/test.txt

@@ -14,3 +14,6 @@ curl -s -X POST http://127.0.0.1:7005/rpc -H "Content-Type: application/json" -d
 
 // 保存日志设置
 curl -s -X POST http://127.0.0.1:7005/rpc -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"basic.saveLogConf","params":{},"id":5}'
+
+// 获取升级配置
+curl -s -X POST http://127.0.0.1:7005/rpc -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"core.getUpgradeConf","params":{},"id":6}'

+ 23 - 15
upgrade.service/upgrade.go

@@ -29,31 +29,31 @@ type serviceUpgradeConfig struct {
 	ReplaceDirs []string `json:"replaceDirs"` /// 需要安装的目录 /
 }
 
-var upgradeServices = []serviceUpgradeConfig{
+var upgradeServices = []serviceUpgradeConfig{ // 内部默认的配置/
 	{ // 1, 网络管理服务
 		Name:        "networkd",
-		Enable:      true,
+		Enable:      false,
 		CheckVerURL: "",
 		ClonePkgURL: "",
 		InstallPath: "/opt/yfkj/networkd.service",
 	},
 	{ // 2, 时间同步服务
 		Name:        "timesyncd",
-		Enable:      true,
+		Enable:      false,
 		CheckVerURL: "",
 		ClonePkgURL: "",
 		InstallPath: "/opt/yfkj/timesyncd.service",
 	},
 	{ // 3, 地理位置服务
 		Name:        "gnss",
-		Enable:      true,
+		Enable:      false,
 		CheckVerURL: "",
 		ClonePkgURL: "",
 		InstallPath: "/opt/yfkj/gnss.service",
 	},
 	{ // 4, 相机采集服务
 		Name:        "camera-capture",
-		Enable:      true,
+		Enable:      false,
 		CheckVerURL: "",
 		ClonePkgURL: "",
 		InstallPath: "/opt/yfkj/camera-capture.service",
@@ -61,28 +61,28 @@ var upgradeServices = []serviceUpgradeConfig{
 	},
 	{ // 5, 应用安装服务
 		Name:        "app-install",
-		Enable:      true,
+		Enable:      false,
 		CheckVerURL: "",
 		ClonePkgURL: "",
 		InstallPath: "/opt/yfkj/app-install.service",
 	},
 	{ // 6, 远程运维服务
 		Name:        "sshd-mqtt-bridge",
-		Enable:      true,
+		Enable:      false,
 		CheckVerURL: "",
 		ClonePkgURL: "",
 		InstallPath: "/opt/yfkj/sshd-mqtt-bridge.service",
 	},
 	{ // 7, 管理后台服务
 		Name:        "web-ui",
-		Enable:      true,
+		Enable:      false,
 		CheckVerURL: "",
 		ClonePkgURL: "",
 		InstallPath: "/opt/yfkj/web-ui.service",
 	},
 	{ // 8, 自动升级服务
 		Name:        "upgrade",
-		Enable:      true,
+		Enable:      false,
 		CheckVerURL: "",
 		ClonePkgURL: "",
 		InstallPath: "/opt/yfkj/upgrade.service",
@@ -93,17 +93,25 @@ func (p *program) loadUpgradeConfig() {
 	configFile := filepath.Join(baseapp.CFG_DIR, "upgrade.json")
 
 	data, err := os.ReadFile(configFile)
-	if err != nil {
-		return
+	if err == nil {
+		var services []serviceUpgradeConfig
+		if err := json.Unmarshal(data, &services); err == nil {
+			upgradeServices = services
+			baseapp.Logger.Infof("[%s] 使用外部升级配置", p.name)
+			return
+		} else {
+			baseapp.Logger.Warnf("[%s] 解析外部配置失败: %v!", p.name, err)
+		}
 	}
 
-	var services []serviceUpgradeConfig
-	if err := json.Unmarshal(data, &services); err != nil {
+	data, err = json.MarshalIndent(upgradeServices, "", "  ") //默认的
+	if err != nil {
 		return
 	}
 
-	upgradeServices = services
-	baseapp.Logger.Infof("[%s] 使用外部升级配置", p.name)
+	if err := servicelib.WriteFileAtomic(configFile, data); err != nil {
+		baseapp.Logger.Errorf("[%s] 写入默认配置失败: %v!!", p.name, err)
+	}
 }
 
 func isNetworkOK() bool {