From fb31a0596857b2fe0daa824a37027a906a45ca57 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Mon, 29 Nov 2021 19:39:56 +0800
Subject: [PATCH 13/17] add cleanup last step

Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
 pkg/api/types.go                              |  2 ++
 pkg/clusterdeployment/binary/binary.go        | 10 +++++++++
 .../binary/cleanupcluster/cleanupcommon.go    | 22 +++++++++++++++++++
 pkg/clusterdeployment/clusterdeploy.go        | 14 ++++++++++++
 pkg/utils/runner/runner.go                    | 14 ------------
 5 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/pkg/api/types.go b/pkg/api/types.go
index fea3f26..e5e1958 100644
--- a/pkg/api/types.go
+++ b/pkg/api/types.go
@@ -305,6 +305,8 @@ type ClusterManagerAPI interface {
 	ClusterStatus() (*ClusterStatus, error)
 	AddonsSetup() error
 	AddonsDestroy() error
+
+	CleanupLastStep(nodeName string) error
 }
 
 type LoadBalancerAPI interface {
diff --git a/pkg/clusterdeployment/binary/binary.go b/pkg/clusterdeployment/binary/binary.go
index 246f547..363de0e 100644
--- a/pkg/clusterdeployment/binary/binary.go
+++ b/pkg/clusterdeployment/binary/binary.go
@@ -563,3 +563,13 @@ func (bcp *BinaryClusterDeployment) PostNodeCleanupHooks(node *api.HostConfig) {
 		logrus.Warnf("Ignore: Delete Node PostHook failed: %v", err)
 	}
 }
+
+func (bcp *BinaryClusterDeployment) CleanupLastStep(nodeName string) error {
+	itask := task.NewTaskInstance(&cleanupcluster.CleanupTempDirTask{})
+
+	if err := nodemanager.RunTaskOnNodes(itask, []string{nodeName}); err != nil {
+		return fmt.Errorf("cleanup user temp dir failed: %v", err)
+	}
+
+	return nil
+}
diff --git a/pkg/clusterdeployment/binary/cleanupcluster/cleanupcommon.go b/pkg/clusterdeployment/binary/cleanupcluster/cleanupcommon.go
index e681b63..f282d94 100644
--- a/pkg/clusterdeployment/binary/cleanupcluster/cleanupcommon.go
+++ b/pkg/clusterdeployment/binary/cleanupcluster/cleanupcommon.go
@@ -21,6 +21,7 @@ import (
 
 	"github.com/sirupsen/logrus"
 
+	"isula.org/eggo/pkg/api"
 	"isula.org/eggo/pkg/utils"
 	"isula.org/eggo/pkg/utils/runner"
 )
@@ -49,3 +50,24 @@ func stopServices(r runner.Runner, services []string) error {
 	}
 	return nil
 }
+
+type CleanupTempDirTask struct {
+}
+
+func (c *CleanupTempDirTask) Name() string {
+	return "CleanupTempDirTask"
+}
+
+func (c *CleanupTempDirTask) Run(r runner.Runner, hostConfig *api.HostConfig) error {
+	if hostConfig == nil {
+		return fmt.Errorf("empty host config")
+	}
+
+	dir := api.GetUserTempDir(hostConfig.UserName)
+	_, err := r.RunCommand(utils.AddSudo("rm -rf " + dir))
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
diff --git a/pkg/clusterdeployment/clusterdeploy.go b/pkg/clusterdeployment/clusterdeploy.go
index d8941ee..138d584 100644
--- a/pkg/clusterdeployment/clusterdeploy.go
+++ b/pkg/clusterdeployment/clusterdeploy.go
@@ -217,6 +217,7 @@ func rollbackFailedNoeds(handler api.ClusterDeploymentAPI, nodes []*api.HostConf
 		// do best to cleanup, if error, just ignore
 		handler.ClusterNodeCleanup(n, n.Type)
 		handler.MachineInfraDestroy(n)
+		handler.CleanupLastStep(n.Name)
 		rollIDs = append(rollIDs, n.Address)
 	}
 
@@ -457,6 +458,11 @@ func doDeleteNode(handler api.ClusterDeploymentAPI, cc *api.ClusterConfig, h *ap
 		return err
 	}
 
+	if err := handler.CleanupLastStep(h.Name); err != nil {
+		logrus.Warnf("cleanup user temp dir for node %s failed: %v", h.Name, err)
+		return err
+	}
+
 	if err := nodemanager.WaitNodesFinishWithProgress([]string{h.Address}, time.Minute*5); err != nil {
 		logrus.Warnf("wait cleanup finish failed: %v", err)
 	}
@@ -581,6 +587,14 @@ func doRemoveCluster(handler api.ClusterDeploymentAPI, cc *api.ClusterConfig) {
 		}
 	}
 
+	// Step9: cleanup user temp dir
+	for _, n := range cc.Nodes {
+		err = handler.CleanupLastStep(n.Name)
+		if err != nil {
+			logrus.Warnf("[cluster] cleanup user temp dir for node: %s failed: %v", n.Name, err)
+		}
+	}
+
 	if err = nodemanager.WaitNodesFinishWithProgress(allNodes, time.Minute*5); err != nil {
 		logrus.Warnf("[cluster] wait all cleanup finish failed: %v", err)
 	}
diff --git a/pkg/utils/runner/runner.go b/pkg/utils/runner/runner.go
index 83a81e9..9a739ca 100644
--- a/pkg/utils/runner/runner.go
+++ b/pkg/utils/runner/runner.go
@@ -145,8 +145,6 @@ func NewSSHRunner(hcfg *api.HostConfig) (Runner, error) {
 }
 
 func (ssh *SSHRunner) Close() {
-	// cleanup resources
-	clearUserTempDir(ssh.Conn, ssh.Host)
 	// TODO: wait kubekey support close for Connection
 	logrus.Debugf("TODO: wait kubekey support close for Connection")
 }
@@ -160,18 +158,6 @@ func (ssh *SSHRunner) Reconnect() error {
 	return nil
 }
 
-func clearUserTempDir(conn ssh.Connection, host *kkv1alpha1.HostCfg) {
-	tmpShell := "/tmp/" + RunnerShellPrefix + "*"
-	// scp to tmp file
-	dir := api.GetUserTempDir(host.User)
-	_, err := conn.Exec(fmt.Sprintf("sudo -E /bin/sh -c \"rm -rf %s; rm -rf %s\"", dir, tmpShell), host)
-	if err != nil {
-		logrus.Warnf("[%s] remove temp dir: %s failed: %v", host.Name, dir, err)
-		return
-	}
-	logrus.Debugf("[%s] remove temp dir: %s success", host.Name, dir)
-}
-
 func prepareUserTempDir(conn ssh.Connection, host *kkv1alpha1.HostCfg) error {
 	// scp to tmp file
 	dir := api.GetUserTempDir(host.User)
-- 
2.25.1

