-
Notifications
You must be signed in to change notification settings - Fork 18
/
sftp.go
68 lines (59 loc) · 1.44 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
package remoteagent
import (
"context"
"github.com/cirruslabs/cirrus-cli/internal/executor/agent"
"github.com/cirruslabs/cirrus-cli/internal/executor/platform"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"io"
"os"
"path"
)
func uploadAgent(
ctx context.Context,
cli *ssh.Client,
agentOS string,
agentVersion string,
agentArchitecture string,
) (string, error) {
sftpCli, err := sftp.NewClient(cli)
if err != nil {
return "", err
}
defer sftpCli.Close()
// Ensure working directory exists
if err := sftpCli.MkdirAll(platform.NewUnix().CirrusDir()); err != nil {
return "", err
}
// Open agent's binary locally
localAgentPath, err := agent.RetrieveBinary(ctx, agentVersion, agentOS, agentArchitecture)
if err != nil {
return "", err
}
localAgentFile, err := os.Open(localAgentPath)
if err != nil {
return "", err
}
// Create agent's binary remotely
remoteAgentPath := path.Join(platform.NewUnix().CirrusDir(), "cirrus-ci-agent")
remoteAgentFile, err := sftpCli.Create(remoteAgentPath)
if err != nil {
return "", err
}
// Copy
if _, err := io.Copy(remoteAgentFile, localAgentFile); err != nil {
return "", err
}
// Close and flush
if err := remoteAgentFile.Close(); err != nil {
return "", err
}
if err := localAgentFile.Close(); err != nil {
return "", err
}
// Agent binary should be executable
if err := sftpCli.Chmod(remoteAgentPath, 0700); err != nil {
return "", err
}
return remoteAgentPath, nil
}