From aa91d7884e7fef55b47d6a4905596f72a5ef1ac3 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期六, 30 五月 2020 17:43:03 +0800
Subject: [PATCH] fix sdkType unTar

---
 service/SdkInstall.go |   86 ++++++++++++++++++++++++++++++++----------
 1 files changed, 65 insertions(+), 21 deletions(-)

diff --git a/service/SdkInstall.go b/service/SdkInstall.go
index 3ad4557..d397ee7 100644
--- a/service/SdkInstall.go
+++ b/service/SdkInstall.go
@@ -1,13 +1,14 @@
 package service
 
 import (
-	"basic.com/dbapi.git"
+	//"basic.com/dbapi.git"
 	"basic.com/valib/logger.git"
 	"encoding/json"
 	"errors"
 	"io/ioutil"
 	"os"
 	"path"
+	"path/filepath"
 	"webserver/extend/config"
 	"webserver/extend/util"
 )
@@ -31,8 +32,8 @@
 			return false,errors.New("鑾峰彇瀹夎鍖卪d5澶辫触")
 		}
 		if strMd5 == identifier {
-			if !installSdk(identifier, ext) {
-				return false,errors.New("鎵ц瀹夎杩囩▼寮傚父,璇风‘瀹氫笂浼犵殑瀹夎鍖呮槸tar.gz鏍煎紡")
+			if _,insE := installSdk(identifier, ext); insE != nil {
+				return false, insE
 			}
 			return true,nil
 
@@ -46,7 +47,7 @@
 }
 
 //瀹夎sdk
-func installSdk(identifier string, ext string) bool {
+func installSdk(identifier string, ext string) (bool, error) {
 	configPatchPath := ""
 	if config.Server.PatchPath != "" {
 		configPatchPath = config.Server.PatchPath
@@ -59,29 +60,46 @@
 		//姝ょ増鏈凡缁忔洿鏂拌繃
 		rmErr := os.RemoveAll(unPackPath)
 		if rmErr !=nil {
-			return false
+			return false,rmErr
 		}
 	}
 	if !util.CreateDirectory(unPackPath) {
-		return false
+		return false, errors.New("鍒涘缓鍘嬬缉鏂囦欢澶瑰け璐�")
 	}
 
 	unPackFilePath := configPatchPath+"/"+identifier+ext
-	err := util.UnTarGz(unPackFilePath, unPackPath)
+	logger.Debug("unPackFilePath:", unPackFilePath, "unPackPath:", unPackPath)
+	_,err := util.UnTarGzByCmd(unPackFilePath, unPackPath)
 	if err !=nil {
 		logger.Debug("UnPack err:",err,"unPackFile:",unPackFilePath)
-		return false
+		return false, err
 	}
+	targetFileName := ""
+	err = filepath.Walk(unPackPath, func(path string, f os.FileInfo, err error) error {
+		if f == nil {
+			return err
+		}
+		if f.IsDir() {
+			targetFileName = f.Name()
+		}
+		return nil
+	})
+	if err != nil {
+		return false, err
+	}
+	unPackPath = unPackPath + targetFileName +"/"
 	//瑙e帇瀹屾垚锛岃幏鍙栧畨瑁呭寘涓殑鏂囦欢锛屽紑濮嬪畨瑁�
 	//1.瑙f瀽瀹夎璇存槑ins.inc
 	incPath := unPackPath+"ins.inc"
-	ret := false
 	if util.Exists(incPath) {
 		if incB, err := ioutil.ReadFile(incPath);err == nil {
 			var ins InsInc
 			if err = json.Unmarshal(incB, &ins); err == nil {
 				//澶勭悊鎺堟潈淇℃伅
+				curMc := ""
+				if ins.MachineCode != curMc {
 
+				}
 				//2.瑙f瀽sdk.def,灏唖o鍜寊conf澶嶅埗鍒�/opt/vasystem/libs/鏂囦欢澶逛笅
 				defPath := unPackPath + "sdk.def"
 				if util.Exists(defPath) {
@@ -90,7 +108,8 @@
 						soM := make(map[string]SdkDef)
 						var skDefArr []SdkDef
 						if err = json.Unmarshal(defB, &skDefArr);err == nil {
-							var sdkApi dbapi.SdkApi
+							//娉ㄥ唽绠楁硶淇℃伅鍜岀畻娉曞弬鏁板埌dbserver
+							//var sdkApi dbapi.SdkApi
 							for _,skd := range skDefArr {
 								if _,ok := soM[skd.Def.SdkType];!ok {
 									soM[skd.Def.SdkType] = skd
@@ -121,11 +140,11 @@
 									srv.Args = append(srv.Args,  sra)
 								}
 								paramBody := util.Struct2Map(srv)
-								sdkApi.Register(paramBody) //灏嗙畻娉曟敞鍐屽埌鏁版嵁搴撲腑
+								logger.Debug("sdkApi.Register paramBody:", paramBody)
+								//sdkApi.Register(paramBody) //灏嗙畻娉曟敞鍐屽埌鏁版嵁搴撲腑
 							}
-
-							zconfPath := "/opt/vasystem/bin/zconf/"
-							libPath := "/opt/vasystem/libs/"
+							zconfPath := "/opt/vasystem/bin/zconft/"
+							libPath := "/opt/vasystem/libst/"
 							if !util.DirExists(zconfPath) {
 								os.MkdirAll(zconfPath, 0777)
 							}
@@ -133,22 +152,47 @@
 								os.MkdirAll(libPath, 0777)
 							}
 							for sdkType,_ := range soM {
-								//澶嶅埗json鍚姩鏂囦欢
-								util.CopyFile(unPackPath+sdkType+"/"+sdkType+".json", zconfPath)
-								util.CopyDir(unPackPath+sdkType+"/"+sdkType, libPath)
-								if util.DirExists(unPackPath+sdkType+"models") {
-									util.CopyDir(unPackPath+sdkType+"models", "/opt/vasystem/bin/")
+								//鍏堣В鍘婼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
+										}
+									}
+								} else {
+									logger.Debug("unTarGzE sdkType.tar.gz err:", unTarGzE)
 								}
 							}
-							ret = true
+
+						} else {
+							return false, errors.New("鍙嶅簭鍒楀寲绠楁硶瀹氫箟淇℃伅澶辫触")
 						}
+					} else {
+						return false, errors.New("璇诲彇绠楁硶瀹氫箟淇℃伅澶辫触")
 					}
+				} else {
+					return false, errors.New("绠楁硶瀹氫箟淇℃伅涓㈠け")
 				}
+			} else {
+				return false, errors.New("鍙嶅簭鍒楀寲鎺堟潈淇℃伅澶辫触")
 			}
+		} else {
+			return false, errors.New("璇诲彇鎺堟潈鏂囦欢澶辫触")
 		}
+	} else {
+		return false, errors.New("鎺堟潈鏂囦欢涓嶅瓨鍦�")
 	}
 
-	return ret
+	return true, nil
 }
 
 

--
Gitblit v1.8.0