Repository: kubeclipper/kubeclipper
Pull request #677 (2023-10-11 02:54:24)

[release-1.4] fix: kcctl optimization
This is an automated cherry-pick of #674

```release-note
None
```


Patch:
diff --git a/pkg/cli/registry/registry.go b/pkg/cli/registry/registry.go
index fdc7e84..2294182 100644
--- a/pkg/cli/registry/registry.go
+++ b/pkg/cli/registry/registry.go
@@ -60,6 +60,8 @@ const (
 	registryExample = `
   # Deploy docker registry
   kcctl registry deploy --pk-file key --node 10.0.0.111 --pkg kc.tar.gz
+  # Deploy docker registry without image load
+  kcctl registry deploy --pk-file key --node 10.0.0.111 --pkg kc.tar.gz --skip-image-load
   # Clean docker registry
   kcctl registry clean --pk-file key --node 10.0.0.111
   # Push docker image to registry
@@ -134,10 +136,11 @@ type RegistryOptions struct {
 	DataRoot     string
 	RegistryPort int
 
-	Type   string
-	Name   string
-	Tag    string
-	Number int
+	Type          string
+	Name          string
+	Tag           string
+	Number        int
+	SkipImageLoad bool
 }
 
 var (
@@ -197,6 +200,7 @@ func NewCmdRegistryDeploy(o *RegistryOptions) *cobra.Command {
 	cmd.Flags().StringVar(&o.Pkg, "pkg", o.Pkg, "registry service and images pkg.")
 	cmd.Flags().StringVar(&o.DataRoot, "data-root", o.DataRoot, "set registry data root directory.")
 	cmd.Flags().IntVar(&o.RegistryPort, "registry-port", o.RegistryPort, "set registry port")
+	cmd.Flags().BoolVar(&o.SkipImageLoad, "skip-image-load", o.SkipImageLoad, "set to skip image load,if set true will skip image load when deploy registry")
 
 	utils.CheckErr(cmd.MarkFlagRequired("node"))
 	utils.CheckErr(cmd.MarkFlagRequired("pkg"))
@@ -453,6 +457,7 @@ func (o *RegistryOptions) Install() error {
 	if err := o.deployRegistry(); err != nil {
 		return fmt.Errorf("deploy registry error: %s", err.Error())
 	}
+
 	// load images
 	if err := o.loadImages(); err != nil {
 		return fmt.Errorf("load images error: %s", err.Error())
@@ -676,6 +681,10 @@ func (o *RegistryOptions) processPackage() error {
 }
 
 func (o *RegistryOptions) loadImages() error {
+	if o.SkipImageLoad {
+		logger.Info("skip image load")
+		return nil
+	}
 	// TODO: for historical reasons and version stability, kcctl temporarily skips the load of the kc-extension image when deploying a private repository
 	// e.g. find /tmp/kc/resource -name images*.tar.gz ｜ grep -v kc-extension  | awk '{print}'
 
diff --git a/pkg/utils/sshutils/cmd.go b/pkg/utils/sshutils/cmd.go
index aeaf84f..02d51c4 100644
--- a/pkg/utils/sshutils/cmd.go
+++ b/pkg/utils/sshutils/cmd.go
@@ -25,6 +25,8 @@ import (
 	"os/exec"
 	"path"
 	"strings"
+
+	"github.com/kubeclipper/kubeclipper/pkg/cli/logger"
 )
 
 func IsFileExist(filepath string) (bool, error) {
@@ -57,7 +59,9 @@ func CmdToString(name string, arg ...string) (Result, error) {
 	}
 	ret.Stdout = bout.String()
 	ret.Stderr = berr.String()
-	return ret, nil
+
+	logger.V(5).Info(ret.Table())
+	return ret, ret.error()
 }
 
 func RunCmdAsSSH(cmdStr string) (Result, error) {
@@ -86,7 +90,8 @@ func RunCmdAsSSH(cmdStr string) (Result, error) {
 		ret.ExitCode = exitCode
 		return ret, nil
 	}
-	return ret, nil
+	logger.V(5).Info(ret.Table())
+	return ret, ret.error()
 }
 
 func Whoami() string {
diff --git a/pkg/utils/sshutils/ssh_v2.go b/pkg/utils/sshutils/ssh_v2.go
index a0507ee..7aa2fdb 100644
--- a/pkg/utils/sshutils/ssh_v2.go
+++ b/pkg/utils/sshutils/ssh_v2.go
@@ -21,9 +21,14 @@ package sshutils
 import (
 	"bytes"
 	"fmt"
+	"strconv"
 	"strings"
 	"sync"
 
+	"github.com/olekukonko/tablewriter"
+
+	"github.com/kubeclipper/kubeclipper/pkg/cli/logger"
+
 	"golang.org/x/crypto/ssh"
 )
 
@@ -41,16 +46,43 @@ func (r Result) Short() string {
 	return fmt.Sprintf("(run `%s` on %s@%s)", r.PrintCmd, r.User, r.Host)
 }
 
+// Deprecated
+// need not use this method to check error,will automic check when command run.
 func (r Result) Error() error {
+	return r.error()
+}
+
+func (r Result) error() error {
 	if r.ExitCode == 0 {
 		return nil
 	}
-	return fmt.Errorf("%s err: %s", r.Short(), r.Stderr)
+	if r.Stderr != "" {
+		return fmt.Errorf("%s exitcode %v msg: %s", r.Short(), r.ExitCode, r.Stderr)
+	}
+	return fmt.Errorf("%s exitcode %v msg: %s", r.Short(), r.ExitCode, r.Stdout)
 }
 
+// Deprecated
+// use Table to replace
 func (r Result) String() string {
-	return fmt.Sprintf("(run `%s` on %s@%s) \nstdout: \n%s\nstderr: %s\nexitcode: %v\n", r.PrintCmd, r.User, r.Host,
-		r.Stdout, r.Stderr, r.ExitCode)
+	return r.Table()
+}
+
+// Table format as table
+func (r Result) Table() string {
+	buff := new(bytes.Buffer)
+
+	table := tablewriter.NewWriter(buff)
+	table.SetHeader([]string{"USER", "HOST", "COMMAND", "STDOUT", "STDERR", "EXIT CODE"})
+	data := [][]string{
+		{r.User, r.Host, r.PrintCmd, r.Stdout, r.Stderr, strconv.Itoa(r.ExitCode)},
+	}
+
+	for _, v := range data {
+		table.Append(v)
+	}
+	table.Render()
+	return "\n" + buff.String() + "\n"
 }
 
 func (r Result) StdoutToString(place string) string {
@@ -99,7 +131,13 @@ func SSHCmd(sshConfig *SSH, host, cmd string) (Result, error) {
 		Stderr:   stderr,
 		ExitCode: code,
 	}
-	return result, err
+	logger.V(5).Infof(result.Table())
+
+	if err != nil {
+		return result, err
+	}
+	// check exit code again when run cmd success
+	return result, result.error()
 }
 
 // runSSHCommand returns the stdout, stderr, and exit code from running cmd on


Test patch:
diff --git a/pkg/utils/ipvsutil/ipvs_linux_test.go b/pkg/utils/ipvsutil/ipvs_linux_test.go
index bd6514d..1585ee5 100644
--- a/pkg/utils/ipvsutil/ipvs_linux_test.go
+++ b/pkg/utils/ipvsutil/ipvs_linux_test.go
@@ -23,6 +23,8 @@ import (
 )
 
 func TestClear(t *testing.T) {
+	t.Skip("can't run in github action")
+
 	type args struct {
 		dryRun bool
 	}
@@ -47,6 +49,8 @@ func TestClear(t *testing.T) {
 }
 
 func TestCreateIPVS(t *testing.T) {
+	t.Skip("can't run in github action")
+
 	type args struct {
 		vs     *VirtualServer
 		dryRun bool
@@ -82,6 +86,8 @@ func TestCreateIPVS(t *testing.T) {
 }
 
 func TestDeleteIPVS(t *testing.T) {
+	t.Skip("can't run in github action")
+
 	type args struct {
 		vs     *VirtualServer
 		dryRun bool
diff --git a/pkg/utils/sshutils/ssh_v2_test.go b/pkg/utils/sshutils/ssh_v2_test.go
index a42a999..15ee68c 100644
--- a/pkg/utils/sshutils/ssh_v2_test.go
+++ b/pkg/utils/sshutils/ssh_v2_test.go
@@ -55,3 +55,11 @@ func TestSSHCmd(t *testing.T) {
 	}
 	assert.Equal(t, "54321", ret.StdoutToString(""))
 }
+
+func TestSSHTable(t *testing.T) {
+	ret, err := SSHCmdWithSudo(nil, "", "ls .")
+	if err != nil {
+		t.Fatal(err)
+	}
+	t.Log(ret.Table())
+}