-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver_deploy_filesystem.go
72 lines (59 loc) · 1.87 KB
/
server_deploy_filesystem.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
package sync
import (
"os"
"fmt"
"errors"
"strings"
"github.com/webdevops/go-shell"
"github.com/webdevops/go-shell/commandbuilder"
)
// General sync
func (filesystem *Filesystem) Deploy() {
switch filesystem.Connection.GetInstance().GetType() {
case "local":
fallthrough
case "ssh":
filesystem.deployRsync()
case "docker":
errors.New("Docker not supported")
}
}
// Sync filesystem using rsync
func (filesystem *Filesystem) deployRsync() {
connection := filesystem.Connection.GetInstance()
args := []string{"-rlptD", "--delete-after", "--progress", "--human-readable"}
// add custom options
if filesystem.Options.Rsync != nil {
args = append(args, filesystem.Options.Rsync.Array()...)
}
if filesystem.Connection.GetInstance().IsSsh() {
args = append(args, "-e", shell.Quote("ssh " + strings.Join(commandbuilder.ConnectionSshArguments, " ")))
}
// include filter
if len(filesystem.Filter.Include) > 0 {
includeTempFile := CreateTempfileWithContent(filesystem.Filter.Include...)
args = append(args, fmt.Sprintf("--files-from=%s", includeTempFile.Name()))
// remove file after run
defer os.Remove(includeTempFile.Name())
}
// exclude filter
if len(filesystem.Filter.Exclude) > 0 {
excludeTempFile := CreateTempfileWithContent(filesystem.Filter.Exclude...)
args = append(args, fmt.Sprintf("--exclude-from=%s", excludeTempFile.Name()))
// remove file after run
defer os.Remove(excludeTempFile.Name())
}
// build source and target paths
sourcePath := filesystem.localPath()
targetPath := ""
switch connection.GetType() {
case "ssh":
targetPath = fmt.Sprintf("%s:%s", connection.SshConnectionHostnameString(), filesystem.Path)
case "local":
targetPath = filesystem.Path
}
// make sure source/target paths are using suffix slash
args = append(args, RsyncPath(sourcePath), RsyncPath(targetPath))
cmd := shell.NewCmd("rsync", args...)
cmd.Run()
}