From 0a6c8c28155b0e23585acff40f765d6502a1677e Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Mon, 6 Apr 2020 11:48:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(v1.0):=20support=20=E5=AE=9E=E6=97=B6?= =?UTF-8?q?=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/root.go | 7 ++--- go.mod | 2 ++ go.sum | 4 +-- pkg/cmd/watch.go | 58 -------------------------------------- pkg/filesize/filesize.go | 27 ------------------ pkg/sshutil/scp.go | 2 ++ pkg/sshutil/watch.go | 60 ---------------------------------------- 7 files changed, 9 insertions(+), 151 deletions(-) delete mode 100644 pkg/cmd/watch.go delete mode 100644 pkg/filesize/filesize.go delete mode 100644 pkg/sshutil/watch.go diff --git a/cmd/root.go b/cmd/root.go index e834523..a16fae7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -60,11 +60,9 @@ var rootCmd = &cobra.Command{ func exec(ssh *sshutil.SSH, mode, host string) { switch mode { - case "ssh": - ssh.Cmd(host, command) case "scp": ssh.CopyForMD5(host, localFilePath, remoteFilePath, "") - case "sshAsync": + case "ssh": _ = ssh.CmdAsync(host, command) } } @@ -113,6 +111,7 @@ func Execute() { } func init() { + logger.Cfg() // Here you will define your flags and configuration settings. rootCmd.Flags().StringVar(&user, "user", "root", "servers user name for ssh") rootCmd.Flags().StringVar(&password, "passwd", "", "password for ssh") @@ -121,5 +120,5 @@ func init() { rootCmd.Flags().StringVar(&command, "cmd", "", "exec shell") rootCmd.Flags().StringVar(&localFilePath, "local-path", "", "local path , ex /etc/local.txt") rootCmd.Flags().StringVar(&remoteFilePath, "remote-path", "", "local path , ex /etc/local.txt") - rootCmd.Flags().StringVar(&mode, "mode", "ssh", "mode type ,use | spilt . ex ssh sshAsync scp ssh|scp scp|ssh") + rootCmd.Flags().StringVar(&mode, "mode", "ssh", "mode type ,use | spilt . ex ssh scp ssh|scp scp|ssh") } diff --git a/go.mod b/go.mod index 9d0654c..f8ed3a3 100644 --- a/go.mod +++ b/go.mod @@ -9,3 +9,5 @@ require ( golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect ) + +replace github.com/wonderivan/logger => github.com/fanux/sealos/pkg/logger v0.0.0-20200406033522-73f3ef41977c diff --git a/go.sum b/go.sum index 8ebe8fd..c569222 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fanux/sealos/pkg/logger v0.0.0-20200406033522-73f3ef41977c h1:oegX8xgoZ4xCXK7o2D8tLQd4uyQ8EVhsozRKOc+z5bI= +github.com/fanux/sealos/pkg/logger v0.0.0-20200406033522-73f3ef41977c/go.mod h1:x9AaKiPmaa1lNJIu5XXJs1En2wZayGN6UA3dgYozYFk= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= @@ -37,8 +39,6 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= -github.com/wonderivan/logger v1.0.0 h1:Z6Nz+3SNcizolx3ARH11axdD4DXjFpb2J+ziGUVlv/U= -github.com/wonderivan/logger v1.0.0/go.mod h1:NObMfQ3WOLKfYEZuGeZQfuQfSPE5+QNgRddVMzsAT/k= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/pkg/cmd/watch.go b/pkg/cmd/watch.go deleted file mode 100644 index fa30a6d..0000000 --- a/pkg/cmd/watch.go +++ /dev/null @@ -1,58 +0,0 @@ -package cmd - -import ( - "fmt" - "github.com/wonderivan/logger" - "path" - "strconv" - "strings" - "time" -) - -const oneMBByte = 1024 * 1024 - -//WatchFileSize is -func LoggerFileSize(filename string, size int) { - t := time.NewTicker(3 * time.Second) //every 3s check file - defer t.Stop() - for { - select { - case <-t.C: - length := CmdToString("/bin/sh", "-c", "ls -l "+filename+" | awk '{print $5}'", "") - length = strings.Replace(length, "\n", "", -1) - length = strings.Replace(length, "\r", "", -1) - lengthByte, _ := strconv.Atoi(length) - if lengthByte == size { - t.Stop() - } - lengthFloat := float64(lengthByte) - value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", lengthFloat/oneMBByte), 64) - logger.Alert("[os][%s]transfer total size is: %.2f%s", filename, value, "MB") - } - } -} - -//RemoteFilExist is -func IsFilExist(filepath string) bool { - // if remote file is - // ls -l | grep aa | wc -l - fileName := path.Base(filepath) // aa - fileDirName := path.Dir(filepath) - fileCommand := fmt.Sprintf("ls -l %s | grep %s | wc -l", fileDirName, fileName) - data := strings.Replace(CmdToString("/bin/sh", "-c", fileCommand), "\r", "", -1) - data = strings.Replace(data, "\n", "", -1) - count, err := strconv.Atoi(strings.TrimSpace(data)) - defer func() { - if r := recover(); r != nil { - logger.Error("[os][%s]RemoteFilExist:%s", filepath, err) - } - }() - if err != nil { - panic(1) - } - if count == 0 { - return false - } else { - return true - } -} diff --git a/pkg/filesize/filesize.go b/pkg/filesize/filesize.go deleted file mode 100644 index c437be3..0000000 --- a/pkg/filesize/filesize.go +++ /dev/null @@ -1,27 +0,0 @@ -package filesize - -import ( - "crypto/tls" - "github.com/wonderivan/logger" - "net/http" -) - -//Do is fetch file size -func Do(url string) int64 { - tr := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } - - client := &http.Client{Transport: tr} - resp, err := client.Get(url) - defer func() { - if r := recover(); r != nil { - logger.Error("[globals] get file size is error: %s", r) - } - }() - if err != nil { - panic(err) - } - resp.Body.Close() - return resp.ContentLength -} diff --git a/pkg/sshutil/scp.go b/pkg/sshutil/scp.go index 1bdfae9..8025892 100644 --- a/pkg/sshutil/scp.go +++ b/pkg/sshutil/scp.go @@ -12,6 +12,8 @@ import ( "time" ) +const oneMBByte = 1024 * 1024 + //Copy is func (ss *SSH) CopyForMD5(host, localFilePath, remoteFilePath, md5 string) bool { //如果有md5则可以验证 diff --git a/pkg/sshutil/watch.go b/pkg/sshutil/watch.go deleted file mode 100644 index 15c5a09..0000000 --- a/pkg/sshutil/watch.go +++ /dev/null @@ -1,60 +0,0 @@ -package sshutil - -import ( - "fmt" - "github.com/wonderivan/logger" - "path" - "strconv" - "strings" - "time" -) - -const oneMBByte = 1024 * 1024 - -//WatchFileSize is -func (ss *SSH) LoggerFileSize(host, filename string, size int) { - t := time.NewTicker(3 * time.Second) //every 3s check file - defer t.Stop() - for { - select { - case <-t.C: - length := ss.CmdToString(host, "ls -l "+filename+" | awk '{print $5}'", "") - length = strings.Replace(length, "\n", "", -1) - length = strings.Replace(length, "\r", "", -1) - lengthByte, _ := strconv.Atoi(length) - if lengthByte == size { - t.Stop() - } - lengthFloat := float64(lengthByte) - value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", lengthFloat/oneMBByte), 64) - logger.Alert("[ssh][%s]transfer total size is: %.2f%s", host, value, "MB") - } - } -} - -//RemoteFilExist is -func (ss *SSH) IsFilExist(host, remoteFilePath string) bool { - // if remote file is - // ls -l | grep aa | wc -l - remoteFileName := path.Base(remoteFilePath) // aa - remoteFileDirName := path.Dir(remoteFilePath) - //it's bug: if file is aa.bak, `ls -l | grep aa | wc -l` is 1 ,should use `ll aa 2>/dev/null |wc -l` - //remoteFileCommand := fmt.Sprintf("ls -l %s| grep %s | grep -v grep |wc -l", remoteFileDirName, remoteFileName) - remoteFileCommand := fmt.Sprintf("ls -l %s/%s 2>/dev/null |wc -l", remoteFileDirName, remoteFileName) - - data := ss.CmdToString(host, remoteFileCommand, " ") - count, err := strconv.Atoi(strings.TrimSpace(data)) - defer func() { - if r := recover(); r != nil { - logger.Error("[ssh][%s]RemoteFilExist:%s", host, err) - } - }() - if err != nil { - panic(1) - } - if count == 0 { - return false - } else { - return true - } -}