From 81a91cf3834cd351128fa7efa2b3c894af0be32e Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Tue, 20 Jul 2021 19:46:32 +0800
Subject: [PATCH 2/9] fix some bugs

Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
 VERSION                                       |  2 +-
 cmd/configs.go                                |  7 ++-
 .../binary/infrastructure/infrastructure.go   | 63 +++++++++----------
 pkg/clusterdeployment/runtime/runtime.go      |  2 +-
 4 files changed, 37 insertions(+), 37 deletions(-)

diff --git a/VERSION b/VERSION
index 899f24f..f514a2f 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.9.0
\ No newline at end of file
+0.9.1
\ No newline at end of file
diff --git a/cmd/configs.go b/cmd/configs.go
index e85ba70..19a182c 100644
--- a/cmd/configs.go
+++ b/cmd/configs.go
@@ -119,9 +119,7 @@ func fillEtcdsIfNotExist(cc *deployConfig) {
 		return
 	}
 
-	for _, h := range cc.Masters {
-		cc.Etcds = append(cc.Etcds, h)
-	}
+	cc.Etcds = append(cc.Etcds, cc.Masters...)
 }
 
 func loadDeployConfig(file string) (*deployConfig, error) {
@@ -234,6 +232,9 @@ func appendSoftware(software, packageConfig, defaultPackage []*api.PackageConfig
 
 	result := software
 	for _, p := range packages {
+		if p == nil {
+			continue
+		}
 		splitSoftware := strings.Split(p.Name, ",")
 		for _, s := range splitSoftware {
 			result = append(result, &api.PackageConfig{
diff --git a/pkg/clusterdeployment/binary/infrastructure/infrastructure.go b/pkg/clusterdeployment/binary/infrastructure/infrastructure.go
index 698c16a..53c07c7 100644
--- a/pkg/clusterdeployment/binary/infrastructure/infrastructure.go
+++ b/pkg/clusterdeployment/binary/infrastructure/infrastructure.go
@@ -37,7 +37,9 @@ import (
 )
 
 var (
-	pmd *packageMD5 = &packageMD5{}
+	pmd *packageMD5 = &packageMD5{
+		MD5s: make(map[string]string),
+	}
 )
 
 type SetupInfraTask struct {
@@ -166,14 +168,15 @@ func copyPackage(r runner.Runner, hcg *api.HostConfig, pcfg *api.PackageSrcConfi
 	}
 
 	// 1. calculate package MD5
-	if err := pmd.getMD5(src); err != nil {
+	md5, err := pmd.getMD5(src)
+	if err != nil {
 		return fmt.Errorf("get MD5 failed: %v", err)
 	}
 
 	// 2. package exist on remote host
 	file, dstDir := filepath.Base(src), pcfg.GetPkgDstPath()
 	dstPath := filepath.Join(dstDir, file)
-	if pmd.checkMD5(r, dstPath) {
+	if checkMD5(r, md5, dstPath) {
 		logrus.Warnf("package already exist on remote host")
 		return nil
 	}
@@ -187,7 +190,7 @@ func copyPackage(r runner.Runner, hcg *api.HostConfig, pcfg *api.PackageSrcConfi
 	}
 
 	// 4. check package MD5
-	if !pmd.checkMD5(r, dstPath) {
+	if !checkMD5(r, md5, dstPath) {
 		return fmt.Errorf("%s MD5 has changed after copy, maybe it is corrupted", file)
 	}
 
@@ -220,6 +223,17 @@ func setHostname(r runner.Runner, hcg *api.HostConfig) error {
 	return nil
 }
 
+func checkMD5(r runner.Runner, md5, path string) bool {
+	output, err := r.RunCommand(fmt.Sprintf("sudo -E /bin/sh -c \"md5sum %s | awk '{print \\$1}'\"", path))
+	if err != nil {
+		logrus.Warnf("get %s MD5 failed: %v", path, err)
+		return false
+	}
+
+	logrus.Debugf("package MD5 value: local %s, remote: %s", md5, output)
+	return md5 == output
+}
+
 func NodeInfrastructureSetup(config *api.ClusterConfig, nodeID string, role uint16) error {
 	if config == nil {
 		return fmt.Errorf("empty cluster config")
@@ -362,48 +376,33 @@ func NodeInfrastructureDestroy(config *api.ClusterConfig, hostconfig *api.HostCo
 }
 
 type packageMD5 struct {
-	MD5  string
+	MD5s map[string]string
 	Lock sync.RWMutex
 }
 
-func (pm *packageMD5) getMD5(path string) error {
+func (pm *packageMD5) getMD5(path string) (string, error) {
 	pm.Lock.RLock()
-	md5str := pm.MD5
-	pm.Lock.RUnlock()
+	defer func() {
+		pm.Lock.RUnlock()
+	}()
 
-	if md5str != "" {
-		return nil
+	md5str, ok := pm.MD5s[path]
+	if ok {
+		return md5str, nil
 	}
 
 	f, err := os.Open(path)
 	if err != nil {
-		return err
+		return "", err
 	}
 	defer f.Close()
 
 	h := md5.New()
 	if _, err := io.Copy(h, f); err != nil {
-		return err
-	}
-
-	pm.Lock.Lock()
-	pm.MD5 = fmt.Sprintf("%x", h.Sum(nil))
-	pm.Lock.Unlock()
-
-	return nil
-}
-
-func (pm *packageMD5) checkMD5(r runner.Runner, path string) bool {
-	pm.Lock.RLock()
-	md5str := pm.MD5
-	pm.Lock.RUnlock()
-
-	output, err := r.RunCommand(fmt.Sprintf("sudo -E /bin/sh -c \"md5sum %s | awk '{print \\$1}'\"", path))
-	if err != nil {
-		logrus.Warnf("get %s MD5 failed: %v", path, err)
-		return false
+		return "", err
 	}
+	md5str = fmt.Sprintf("%x", h.Sum(nil))
+	pm.MD5s[path] = md5str
 
-	logrus.Debugf("package MD5 value: local %s, remote: %s", md5str, output)
-	return md5str == output
+	return md5str, nil
 }
diff --git a/pkg/clusterdeployment/runtime/runtime.go b/pkg/clusterdeployment/runtime/runtime.go
index 8295dd8..29fab7d 100644
--- a/pkg/clusterdeployment/runtime/runtime.go
+++ b/pkg/clusterdeployment/runtime/runtime.go
@@ -149,7 +149,7 @@ func (dr *dockerRuntime) PrepareRuntimeJson(r runner.Runner, WorkerConfig *api.W
 
 	var sb strings.Builder
 	jsonBase64 := base64.StdEncoding.EncodeToString([]byte(json))
-	sb.WriteString(fmt.Sprintf("sudo -E /bin/sh -c \"echo %s | base64 -d > %s\"", jsonBase64, "/etc/docker/daemon.json"))
+	sb.WriteString(fmt.Sprintf("sudo -E /bin/sh -c \"mkdir -p /etc/docker && echo %s | base64 -d > %s\"", jsonBase64, "/etc/docker/daemon.json"))
 	_, err = r.RunCommand(sb.String())
 	if err != nil {
 		return err
-- 
2.25.1

