-
Notifications
You must be signed in to change notification settings - Fork 0
/
sftp.go
126 lines (111 loc) · 2.75 KB
/
sftp.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
package main
import (
"fmt"
"net"
"os"
"path"
"github.com/pkg/errors"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
type sftpMode struct {
remoteParentDir string
Host string
User string
Password string
SftpClient connectInterface
}
func NewSftp(host, user, password, remoteParentDir string) *sftpMode {
Info.Println("Remote Directory: ", remoteParentDir)
c := &sftpMode{
remoteParentDir: remoteParentDir,
Host: host,
User: user,
Password: password,
SftpClient: getSftpConn(host, user, password),
}
return c
}
func (c *sftpMode) prepareAllDir(subPath string) error {
err := c.createRemoteDir(subPath)
if err != nil {
return errors.Wrapf(err, "can't create %v directory", c.remoteParentDir+subPath)
}
return nil
}
func (c *sftpMode) Close() {
defer c.SftpClient.Close()
}
func (c *sftpMode) createRemoteDir(subPath string) error {
p := c.remoteParentDir + subPath
_, statErr := c.SftpClient.Stat(p)
if statErr != nil {
Info.Println("Create remote Directory: ", p)
if err := c.SftpClient.Mkdir(p); err != nil {
return err
}
}
return nil
}
func (c *sftpMode) upload(localFilePath, subDir, dbName string) error {
Info.Println("Start upload to sftp")
srcFile, err := os.Open(localFilePath)
if err != nil {
Error.Println(err)
}
defer srcFile.Close()
remoteFileName := fmt.Sprintf("%v.gz", dbName)
dstFile, err := c.SftpClient.Create(path.Join(c.remoteParentDir, subDir, remoteFileName))
if err != nil {
Error.Println(err)
}
defer dstFile.Close()
buf := make([]byte, 1024)
for {
n, _ := srcFile.Read(buf)
if n == 0 {
break
}
_, err = dstFile.Write(buf)
if err != nil {
Error.Println(err)
}
}
Info.Println("Upload successfully")
return nil
}
func getSSHConn(host, user, password string) (*ssh.Client, error) {
var auths []ssh.AuthMethod
if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))
}
if password != "" {
auths = append(auths, ssh.Password(password))
}
config := ssh.ClientConfig{
User: user,
Auth: auths,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}
conn, err := ssh.Dial("tcp", host, &config)
if err != nil {
Error.Fatalf("unable to connect to [%s]: %v", host, err)
return nil, err
}
Info.Println("connected to host: ", host)
return conn, nil
}
func getSftpConn(host, user, password string) *sftp.Client {
sshClient, err := getSSHConn(host, user, password)
if err != nil {
Error.Fatal(err)
}
sftpc, err := sftp.NewClient(sshClient)
if err != nil {
Error.Fatalf("unable to start sftp subsytem: %v", err)
}
return sftpc
}