|
|
@@ -53,8 +53,8 @@ var (
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
- userAppInsDir = "/home/root/yfkj-userapp"
|
|
|
- userAppService = "/etc/systemd/system/yfkj-userapp.service"
|
|
|
+ userAppInsDir = "/home/root/yfkj-user-app"
|
|
|
+ userAppService = "/etc/systemd/system/yfkj-user-app.service"
|
|
|
|
|
|
ErrConflict jsonrpc2.ErrCode = -32099 ///////// 资源访问冲突
|
|
|
)
|
|
|
@@ -107,7 +107,7 @@ func pkgImport(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
|
|
|
}
|
|
|
|
|
|
savePkgFile := filepath.Join(userAppPkgDir, filepath.Base(file))
|
|
|
- if err := os.Rename(file, savePkgFile); err != nil {
|
|
|
+ if err := moveFile(file, savePkgFile); err != nil {
|
|
|
return jsonrpc2.BuildError(req, jsonrpc2.ErrInternal, err.Error())
|
|
|
}
|
|
|
|
|
|
@@ -136,7 +136,8 @@ func pkgImport(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
|
|
|
return jsonrpc2.BuildError(req, jsonrpc2.ErrInternal, err.Error())
|
|
|
}
|
|
|
|
|
|
- baseapp.Logger.Infof("[PkgImport] 导入用户应用安装包成功: %s", savePkgFile)
|
|
|
+ baseapp.Logger.Infof("[PkgImport] 导入用户应用安装包成功: %s, 版本: %s, 描述: %s",
|
|
|
+ appIns.App.Name, appIns.App.Version, appIns.App.Description)
|
|
|
|
|
|
return jsonrpc2.BuildResponse(req, "success", nil)
|
|
|
}
|
|
|
@@ -148,9 +149,9 @@ func pkgRemove(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
|
|
|
|
|
|
appInsFile := filepath.Join(userAppPkgDir, "appins.json")
|
|
|
|
|
|
+ var appIns AppIns
|
|
|
data, err := os.ReadFile(appInsFile)
|
|
|
if err == nil {
|
|
|
- var appIns AppIns
|
|
|
if err := json.Unmarshal(data, &appIns); err == nil {
|
|
|
if appIns.Ins.Installed { // 用户应用已安装
|
|
|
return jsonrpc2.BuildError(req, ErrConflict, "application is installed, please uninstall it first")
|
|
|
@@ -168,7 +169,8 @@ func pkgRemove(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
|
|
|
return jsonrpc2.BuildError(req, jsonrpc2.ErrInternal, err.Error())
|
|
|
}
|
|
|
|
|
|
- baseapp.Logger.Infof("[PkgRemove] 删除用户应用安装包成功")
|
|
|
+ baseapp.Logger.Infof("[PkgRemove] 删除用户应用安装包成功: %s, 版本: %s, 描述: %s",
|
|
|
+ appIns.App.Name, appIns.App.Version, appIns.App.Description)
|
|
|
|
|
|
return jsonrpc2.BuildResponse(req, "success", nil)
|
|
|
}
|
|
|
@@ -200,8 +202,8 @@ func appInstall(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
|
|
|
if !success {
|
|
|
baseapp.Logger.Warnf("[AppInstall] rollback: %s", userAppInsDir)
|
|
|
|
|
|
- systemctlIgnoreError("stop", "yfkj-userapp.service")
|
|
|
- systemctlIgnoreError("disable", "yfkj-userapp.service")
|
|
|
+ systemctlIgnoreError("stop", "yfkj-user-app.service")
|
|
|
+ systemctlIgnoreError("disable", "yfkj-user-app.service")
|
|
|
|
|
|
os.RemoveAll(userAppInsDir)
|
|
|
os.Remove(userAppService)
|
|
|
@@ -210,7 +212,7 @@ func appInstall(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
|
|
|
}
|
|
|
}()
|
|
|
|
|
|
- systemctlIgnoreError("stop", "yfkj-userapp.service")
|
|
|
+ systemctlIgnoreError("stop", "yfkj-user-app.service")
|
|
|
|
|
|
if err := os.MkdirAll(userAppInsDir, 0755); err != nil {
|
|
|
return jsonrpc2.BuildError(req, jsonrpc2.ErrInternal, err.Error())
|
|
|
@@ -245,11 +247,11 @@ func appInstall(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
|
|
|
return jsonrpc2.BuildError(req, jsonrpc2.ErrInternal, err.Error())
|
|
|
}
|
|
|
|
|
|
- if err := systemctl("enable", "yfkj-userapp.service"); err != nil {
|
|
|
+ if err := systemctl("enable", "yfkj-user-app.service"); err != nil {
|
|
|
return jsonrpc2.BuildError(req, jsonrpc2.ErrInternal, err.Error())
|
|
|
}
|
|
|
|
|
|
- if err := systemctl("start", "yfkj-userapp.service"); err != nil {
|
|
|
+ if err := systemctl("start", "yfkj-user-app.service"); err != nil {
|
|
|
return jsonrpc2.BuildError(req, jsonrpc2.ErrInternal, err.Error())
|
|
|
}
|
|
|
|
|
|
@@ -267,7 +269,8 @@ func appInstall(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response {
|
|
|
|
|
|
success = true
|
|
|
|
|
|
- baseapp.Logger.Infof("[AppInstall] 安装用户应用成功: %s", appIns.App.Name)
|
|
|
+ baseapp.Logger.Infof("[AppInstall] 安装用户应用成功: %s, 版本: %s, 描述: %s",
|
|
|
+ appIns.App.Name, appIns.App.Version, appIns.App.Description)
|
|
|
|
|
|
return jsonrpc2.BuildResponse(req, "success", nil)
|
|
|
}
|
|
|
@@ -293,9 +296,9 @@ func appUninstall(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response
|
|
|
return jsonrpc2.BuildError(req, ErrConflict, "application not installed")
|
|
|
}
|
|
|
|
|
|
- systemctlIgnoreError("stop", "yfkj-userapp.service")
|
|
|
+ systemctlIgnoreError("stop", "yfkj-user-app.service")
|
|
|
|
|
|
- systemctlIgnoreError("disable", "yfkj-userapp.service")
|
|
|
+ systemctlIgnoreError("disable", "yfkj-user-app.service")
|
|
|
|
|
|
if err := os.Remove(userAppService); err != nil && !os.IsNotExist(err) {
|
|
|
return jsonrpc2.BuildError(req, jsonrpc2.ErrInternal, err.Error())
|
|
|
@@ -321,7 +324,8 @@ func appUninstall(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Response
|
|
|
return jsonrpc2.BuildError(req, jsonrpc2.ErrInternal, err.Error())
|
|
|
}
|
|
|
|
|
|
- baseapp.Logger.Infof("[AppUninstall] 卸载用户应用成功: %s", appIns.App.Name)
|
|
|
+ baseapp.Logger.Infof("[AppUninstall] 卸载用户应用成功: %s, 版本: %s, 描述: %s",
|
|
|
+ appIns.App.Name, appIns.App.Version, appIns.App.Description)
|
|
|
|
|
|
return jsonrpc2.BuildResponse(req, "success", nil)
|
|
|
}
|
|
|
@@ -343,6 +347,15 @@ func getInstallInfo(ctx context.Context, req *jsonrpc2.Request) *jsonrpc2.Respon
|
|
|
return jsonrpc2.BuildError(req, jsonrpc2.ErrInternal, err.Error())
|
|
|
}
|
|
|
|
|
|
+ if appIns.App.ReadmeFile != "" {
|
|
|
+ readme, err := os.ReadFile(filepath.Join(userAppInsDir, appIns.App.ReadmeFile))
|
|
|
+ if err != nil {
|
|
|
+ appIns.App.ReadmeFile = ""
|
|
|
+ } else {
|
|
|
+ appIns.App.ReadmeFile = string(readme)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return jsonrpc2.BuildResponse(req, appIns, nil)
|
|
|
}
|
|
|
|
|
|
@@ -430,6 +443,37 @@ func fileMD5(file string) (string, error) {
|
|
|
return hex.EncodeToString(h.Sum(nil)), nil
|
|
|
}
|
|
|
|
|
|
+func moveFile(src, dst string) error {
|
|
|
+ if err := os.Rename(src, dst); err == nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ in, err := os.Open(src)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ defer in.Close()
|
|
|
+
|
|
|
+ out, err := os.Create(dst)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = io.Copy(out, in)
|
|
|
+ if err != nil {
|
|
|
+ out.Close()
|
|
|
+ os.Remove(dst)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := out.Close(); err != nil {
|
|
|
+ os.Remove(dst)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return os.Remove(src)
|
|
|
+}
|
|
|
+
|
|
|
func validRelativePath(p string) bool {
|
|
|
if p == "" {
|
|
|
return false
|
|
|
@@ -596,7 +640,7 @@ func createUserAppService(app AppInfo, installDir string) error {
|
|
|
}
|
|
|
|
|
|
content := fmt.Sprintf(`[Unit]
|
|
|
-Description=%s Service
|
|
|
+Description=%s
|
|
|
After=yfkj-networkd.service
|
|
|
Wants=yfkj-networkd.service
|
|
|
|
|
|
@@ -613,7 +657,7 @@ StandardError=journal
|
|
|
[Install]
|
|
|
WantedBy=multi-user.target
|
|
|
`,
|
|
|
- app.Name,
|
|
|
+ "Yunfei User Application",
|
|
|
libPaths,
|
|
|
installDir,
|
|
|
execFile,
|