/
scp.go
211 lines (195 loc) · 5.95 KB
/
scp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright © 2021 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ssh
import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"github.com/schollz/progressbar/v3"
"github.com/labring/sealvm/pkg/utils/file"
"github.com/labring/sealvm/pkg/utils/hash"
"github.com/labring/sealvm/pkg/utils/iputils"
"github.com/labring/sealvm/pkg/utils/logger"
"github.com/labring/sealvm/pkg/utils/progress"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
func (s *SSH) RemoteSha256Sum(host, remoteFilePath string) string {
cmd := fmt.Sprintf("sha256sum %s | cut -d\" \" -f1", remoteFilePath)
remoteHash, err := s.CmdToString(host, cmd, "")
if err != nil {
logger.Error("calculate remote sha256 sum failed %s %s %v", host, remoteFilePath, err)
}
return remoteHash
}
// CmdToString is in host exec cmd and replace to spilt str
func (s *SSH) CmdToString(host, cmd, spilt string) (string, error) {
data, err := s.Cmd(host, cmd)
str := string(data)
if err != nil {
return str, fmt.Errorf("exec remote command failed %s %s %v", host, cmd, err)
}
if data != nil {
str = strings.ReplaceAll(str, "\r\n", spilt)
str = strings.ReplaceAll(str, "\n", spilt)
return str, nil
}
return str, fmt.Errorf("command %s %s return nil", host, cmd)
}
// SftpConnect is
func (s *SSH) sftpConnect(host string) (*ssh.Client, *sftp.Client, error) {
sshClient, err := s.connect(host)
if err != nil {
return nil, nil, err
}
// create sftp client
sftpClient, err := sftp.NewClient(sshClient)
return sshClient, sftpClient, err
}
// Copy is copy file or dir to remotePath, add md5 validate
func (s *SSH) Copy(host, localPath, remotePath string) error {
if iputils.IsLocalIP(host, s.LocalAddress) {
logger.Debug("local %s copy files src %s to dst %s", host, localPath, remotePath)
return file.RecursionCopy(localPath, remotePath)
}
logger.Debug("remote copy files src %s to dst %s", localPath, remotePath)
sshClient, sftpClient, err := s.sftpConnect(host)
if err != nil {
return fmt.Errorf("new sftp client failed %s", err)
}
defer func() {
_ = sftpClient.Close()
_ = sshClient.Close()
}()
f, err := os.Stat(localPath)
if err != nil {
return fmt.Errorf("get file stat failed %s", err)
}
baseRemoteFilePath := filepath.Dir(remotePath)
_, err = sftpClient.ReadDir(baseRemoteFilePath)
if err != nil {
if err = sftpClient.MkdirAll(baseRemoteFilePath); err != nil {
return err
}
}
number := 1
if f.IsDir() {
number = file.CountDirFiles(localPath)
}
// no file in dir, do need to send
if number == 0 {
return nil
}
var (
bar = progress.Simple("copying files to "+host, number)
)
defer func() {
_ = bar.Close()
}()
if f.IsDir() {
s.copyLocalDirToRemote(host, sftpClient, localPath, remotePath, bar)
} else {
err = s.copyLocalFileToRemote(host, sftpClient, localPath, remotePath)
if err != nil {
errMsg := fmt.Sprintf("copy local file to remote failed %v %s %s %s", err, host, localPath, remotePath)
logger.Error(errMsg)
}
_ = bar.Add(1)
}
return nil
}
func (s *SSH) copyLocalDirToRemote(host string, sftpClient *sftp.Client, localPath, remotePath string, epu *progressbar.ProgressBar) {
localFiles, err := os.ReadDir(localPath)
if err != nil {
logger.Error("read local path dir failed %s %s", host, localPath)
return
}
if err = sftpClient.MkdirAll(remotePath); err != nil {
logger.Error("failed to create remote path %s:%v", remotePath, err)
return
}
for _, file := range localFiles {
lfp := path.Join(localPath, file.Name())
rfp := path.Join(remotePath, file.Name())
if file.IsDir() {
if err = sftpClient.MkdirAll(rfp); err != nil {
logger.Error("failed to create remote path %s:%v", rfp, err)
return
}
s.copyLocalDirToRemote(host, sftpClient, lfp, rfp, epu)
} else {
err := s.copyLocalFileToRemote(host, sftpClient, lfp, rfp)
if err != nil {
errMsg := fmt.Sprintf("copy local file to remote failed %v %s %s %s", err, host, lfp, rfp)
logger.Error(errMsg)
return
}
_ = epu.Add(1)
}
}
}
// check the remote file existence before copying
// solve the sesion
func (s *SSH) copyLocalFileToRemote(host string, sftpClient *sftp.Client, localPath, remotePath string) error {
var (
srcHash, dstHash string
)
srcHash = hash.FileDigest(localPath)
if s.remoteFileExist(host, remotePath) {
dstHash = s.RemoteSha256Sum(host, remotePath)
if srcHash == dstHash {
logger.Debug("remote dst %s already exists and is the latest version, skip copying process", remotePath)
return nil
}
}
srcFile, err := os.Open(filepath.Clean(localPath))
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := sftpClient.Create(remotePath)
if err != nil {
return err
}
fileStat, err := srcFile.Stat()
if err != nil {
return fmt.Errorf("get file stat failed %v", err)
}
// TODO seems not work
//if fileStat.Mode()&os.ModeSymlink != 0 {
// target, err := os.Readlink(filepath.Clean(localPath))
// if err != nil {
// return err
// }
// // NOTE: os.Chmod and os.Chtimes don't recoganize symbolic link,
// // which will lead "no such file or directory" error.
// return os.Symlink(target, dest)
//}
if err := dstFile.Chmod(fileStat.Mode()); err != nil {
return fmt.Errorf("chmod remote file failed %v", err)
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}
dstHash = s.RemoteSha256Sum(host, remotePath)
if srcHash != dstHash {
return fmt.Errorf("[ssh][%s] validate sha256 sum failed %s != %s", host, srcHash, dstHash)
}
return nil
}