-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_sm_file.go
136 lines (111 loc) · 2.77 KB
/
script_sm_file.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
package smutje
import (
"crypto/md5"
"fmt"
"io"
"log"
"os"
"path/filepath"
"github.com/gfrey/gconn"
"github.com/pkg/errors"
)
type execWriteFileCmd struct {
Source string
Target string
Owner string
Umask string
Render bool
attrs Attributes
hash string
size int64
}
func newExecWriteFileCmd(path string, args []string) (*execWriteFileCmd, error) {
if len(args) < 2 || len(args) == 3 || len(args) > 4 {
return nil, errors.Errorf(`syntax error: write file/template usage ":write_file <source> <target> [<user> <umask>]?"`)
}
filename := args[0]
if filename[0] != '/' {
filename = filepath.Join(path, args[0])
if _, err := os.Stat(filename); err != nil {
return nil, err
}
}
cmd := &execWriteFileCmd{Source: filename, Target: args[1], Render: false}
if len(args) > 2 {
cmd.Owner = args[2]
cmd.Umask = args[3]
}
return cmd, nil
}
func newExecWriteTemplateCmd(path string, args []string) (*execWriteFileCmd, error) {
cmd, err := newExecWriteFileCmd(path, args)
if err != nil {
return nil, err
}
cmd.Render = true
return cmd, nil
}
func (a *execWriteFileCmd) Hash() string {
return a.hash
}
func (a *execWriteFileCmd) Prepare(attrs Attributes, prevHash string) (string, error) {
a.attrs = attrs
r, err := a.read()
if err != nil {
return "", err
}
defer r.Close()
hash := md5.New()
if _, err := hash.Write([]byte(prevHash + a.Target + a.Owner + a.Umask)); err != nil {
return "", err
}
size, err := io.Copy(hash, r)
if err != nil {
return "", err
}
a.size = size
a.hash = fmt.Sprintf("%x", hash.Sum(nil))
return a.hash, nil
}
func (a *execWriteFileCmd) read() (io.ReadCloser, error) {
if a.Render {
return renderFile(a.Source, a.attrs)
}
return os.Open(a.Source)
}
func (a *execWriteFileCmd) Exec(l *log.Logger, clients gconn.Client) error {
r, err := a.read()
if err != nil {
return err
}
defer r.Close()
l.Printf("writing file %q", a.Target)
rawCmd := "{ dir=$(dirname %[1]s); test -d ${dir} || mkdir -p ${dir}; } && cat - > %[1]s"
// TODO is possible to set only one of the both?
if a.Owner != "" && a.Umask != "" {
rawCmd += " && chown " + a.Owner + " %[1]s && chmod " + a.Umask + " %[1]s"
}
cmd := fmt.Sprintf("'"+rawCmd+"'", a.Target)
sess, err := gconn.NewLoggedClient(l, clients).NewSession("/usr/bin/env", "sh", "-c", cmd)
if err != nil {
return err
}
defer sess.Close()
stdin, err := sess.StdinPipe()
if err != nil {
return errors.Wrap(err, "failed to receive stdin pipe")
}
if err := sess.Start(); err != nil {
return err
}
if _, err := io.Copy(stdin, r); err != nil {
return errors.Wrap(err, "failed to send script to target")
}
stdin.Close()
// TODO validate all bytes written
// TODO use compression on the wire
return sess.Wait()
}
func (*execWriteFileCmd) MustExecute() bool {
return false
}