From 0d858e8610a79b15cb337daf134c4a00962dd477 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期四, 04 六月 2020 16:44:29 +0800
Subject: [PATCH] add sdk upload

---
 service/SdkInstall.go |   98 +++++++++++++++++++++++++++++--------------------
 1 files changed, 58 insertions(+), 40 deletions(-)

diff --git a/service/SdkInstall.go b/service/SdkInstall.go
index e759454..b930aab 100644
--- a/service/SdkInstall.go
+++ b/service/SdkInstall.go
@@ -1,7 +1,7 @@
 package service
 
 import (
-	//"basic.com/dbapi.git"
+	"basic.com/dbapi.git"
 	"basic.com/valib/logger.git"
 	"encoding/json"
 	"errors"
@@ -13,8 +13,12 @@
 	"webserver/extend/util"
 )
 
+type SdkInstallService struct {
+
+}
+
 //绠楁硶瀹夎鍖呭畨瑁�
-func (sv SysService) SdkInstall(identifier string,filename string) (bool,error) {
+func (sv SdkInstallService) SdkInstall(identifier string,filename string) (bool,error) {
 	configPatchPath := ""
 	if config.Server.PatchPath != "" {
 		configPatchPath = config.Server.PatchPath
@@ -29,6 +33,7 @@
 	if util.Exists(zipFilePath) {
 		strMd5, e := util.FileMd5(zipFilePath)
 		if e !=nil || strMd5 == "" {
+			go os.Remove(zipFilePath)
 			return false,errors.New("鑾峰彇瀹夎鍖卪d5澶辫触")
 		}
 		if strMd5 == identifier {
@@ -38,6 +43,7 @@
 			return true,nil
 
 		} else {
+			go os.Remove(zipFilePath)
 			logger.Debug("strMd5 is", strMd5,"identifier is",identifier,"not equal")
 			return false,errors.New("鏍¢獙瀹夎鏂囦欢澶辫触")
 		}
@@ -55,27 +61,36 @@
 		configPatchPath = "/opt/vasystem/patch"
 	}
 	//1.瑙e帇缂╂洿鏂板寘
-	unPackPath := configPatchPath+"/"+identifier+"_basic/"
-	if util.Exists(unPackPath) {
+	unPackTargetPath := configPatchPath+"/"+identifier+"_basic/"
+	unPackFilePath := configPatchPath+"/"+identifier+ext
+	defer func() {
+		if util.Exists(unPackTargetPath) {
+			os.RemoveAll(unPackTargetPath)
+		}
+		if util.Exists(unPackFilePath) {
+			os.RemoveAll(unPackFilePath)
+		}
+	}()
+
+	if util.Exists(unPackTargetPath) {
 		//姝ょ増鏈凡缁忔洿鏂拌繃
-		rmErr := os.RemoveAll(unPackPath)
+		rmErr := os.RemoveAll(unPackTargetPath)
 		if rmErr !=nil {
 			return false,rmErr
 		}
 	}
-	if !util.CreateDirectory(unPackPath) {
+	if !util.CreateDirectory(unPackTargetPath) {
 		return false, errors.New("鍒涘缓鍘嬬缉鏂囦欢澶瑰け璐�")
 	}
 
-	unPackFilePath := configPatchPath+"/"+identifier+ext
-	logger.Debug("unPackFilePath:", unPackFilePath, "unPackPath:", unPackPath)
-	_,err := util.UnTarGzByCmd(unPackFilePath, unPackPath)
+	logger.Debug("unPackFilePath:", unPackFilePath, "unPackPath:", unPackTargetPath)
+	_,err := util.UnTarGzByCmd(unPackFilePath, unPackTargetPath)
 	if err !=nil {
 		logger.Debug("UnPack err:",err,"unPackFile:",unPackFilePath)
 		return false, err
 	}
 	targetFileName := ""
-	err = filepath.Walk(unPackPath, func(path string, f os.FileInfo, err error) error {
+	err = filepath.Walk(unPackTargetPath, func(path string, f os.FileInfo, err error) error {
 		if f == nil {
 			return err
 		}
@@ -87,7 +102,7 @@
 	if err != nil {
 		return false, err
 	}
-	unPackPath = unPackPath + targetFileName +"/"
+	unPackPath := unPackTargetPath + targetFileName +"/"
 	//瑙e帇瀹屾垚锛岃幏鍙栧畨瑁呭寘涓殑鏂囦欢锛屽紑濮嬪畨瑁�
 	//1.瑙f瀽瀹夎璇存槑ins.inc
 	incPath := unPackPath+"ins.inc"
@@ -108,35 +123,8 @@
 						soM := make(map[string]SdkDef)
 						var skDefArr []SdkDef
 						if err = json.Unmarshal(defB, &skDefArr);err == nil {
-							zconfPath := "/opt/vasystem/bin/zconft/"
-							libPath := "/opt/vasystem/libst/"
-							if !util.DirExists(zconfPath) {
-								os.MkdirAll(zconfPath, 0777)
-							}
-							if !util.DirExists(libPath) {
-								os.MkdirAll(libPath, 0777)
-							}
-							for sdkType,_ := range soM {
-								//鍏堣В鍘婼dkType.tar.gz鏂囦欢
-								if unTarGzE := util.UnTarGz(unPackPath+sdkType+".tar.gz", unPackPath);unTarGzE == nil {
-									//澶嶅埗json鍚姩鏂囦欢
-									if util.Exists(unPackPath+sdkType+"/zconf/"+sdkType+".json") {
-										util.CopyFile(unPackPath+sdkType+"/zconf/"+sdkType+".json", zconfPath)
-									}
-									if util.DirExists(unPackPath+sdkType+"/"+sdkType) {
-										if _,cE := util.CopyDirByCmd(unPackPath+sdkType+"/"+sdkType, libPath);cE != nil {
-											return false, cE
-										}
-									}
-									if util.DirExists(unPackPath+sdkType+"/models") {
-										if _,cE := util.CopyDirByCmd(unPackPath+sdkType+"/models", "/opt/vasystem/bin/zconft"); cE != nil {
-											return false, cE
-										}
-									}
-								}
-							}
 							//娉ㄥ唽绠楁硶淇℃伅鍜岀畻娉曞弬鏁板埌dbserver
-							//var sdkApi dbapi.SdkApi
+							var sdkApi dbapi.SdkApi
 							for _,skd := range skDefArr {
 								if _,ok := soM[skd.Def.SdkType];!ok {
 									soM[skd.Def.SdkType] = skd
@@ -168,8 +156,38 @@
 								}
 								paramBody := util.Struct2Map(srv)
 								logger.Debug("sdkApi.Register paramBody:", paramBody)
-								//sdkApi.Register(paramBody) //灏嗙畻娉曟敞鍐屽埌鏁版嵁搴撲腑
+								sdkApi.Register(paramBody) //灏嗙畻娉曟敞鍐屽埌鏁版嵁搴撲腑
 							}
+							zconfPath := "/opt/vasystem/bin/zconf/"
+							libPath := "/opt/vasystem/libs/"
+							if !util.DirExists(zconfPath) {
+								os.MkdirAll(zconfPath, 0777)
+							}
+							if !util.DirExists(libPath) {
+								os.MkdirAll(libPath, 0777)
+							}
+							for sdkType,_ := range soM {
+								//鍏堣В鍘婼dkType.tar.gz鏂囦欢
+								if unTarGzE := util.UnTarGz(unPackPath+sdkType+".tar.gz", unPackPath);unTarGzE == nil {
+									//澶嶅埗json鍚姩鏂囦欢
+									if util.Exists(unPackPath+sdkType+"/zconf/"+sdkType+".json") {
+										util.CopyFile(unPackPath+sdkType+"/zconf/"+sdkType+".json", zconfPath+sdkType+".json")
+									}
+									if util.DirExists(unPackPath+sdkType) {
+										if _,cE := util.CopyDirByCmd(unPackPath+sdkType, libPath);cE != nil {
+											return false, cE
+										}
+									}
+									if util.DirExists(unPackPath+sdkType+"/models") {
+										if _,cE := util.CopyDirByCmd(unPackPath+sdkType+"/models", "/opt/vasystem/bin/zconf"); cE != nil {
+											return false, cE
+										}
+									}
+								} else {
+									logger.Debug("unTarGzE sdkType.tar.gz err:", unTarGzE)
+								}
+							}
+
 						} else {
 							return false, errors.New("鍙嶅簭鍒楀寲绠楁硶瀹氫箟淇℃伅澶辫触")
 						}

--
Gitblit v1.8.0