/
file.go
314 lines (270 loc) · 9.02 KB
/
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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package plugins
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"github.com/WeBankPartners/wecube-plugins-saltstack/common/log"
)
var FileActions = make(map[string]Action)
func init() {
FileActions["copy"] = new(FileCopyAction)
}
type FilePlugin struct {
}
func (plugin *FilePlugin) GetActionByName(actionName string) (Action, error) {
action, found := FileActions[actionName]
if !found {
return nil, fmt.Errorf("File plugin,action = %s not found", actionName)
}
return action, nil
}
type FileCopyInputs struct {
Inputs []FileCopyInput `json:"inputs,omitempty"`
}
type FileCopyInput struct {
CallBackParameter
EndPoint string `json:"endpoint,omitempty"`
Guid string `json:"guid,omitempty"`
Target string `json:"target,omitempty"`
DestinationPath string `json:"destinationPath,omitempty"`
Unpack string `json:"unpack,omitempty"`
FileOwner string `json:"fileOwner,omitempty"`
}
type FileCopyOutputs struct {
Outputs []FileCopyOutput `json:"outputs,omitempty"`
}
type FileCopyOutput struct {
CallBackParameter
Result
Guid string `json:"guid,omitempty"`
Detail string `json:"detail,omitempty"`
}
type FileCopyThreadObj struct {
Data FileCopyOutput
Err error
Index int
}
type FileCopyAction struct{ Language string }
func (action *FileCopyAction) ReadParam(param interface{}) (interface{}, error) {
var inputs FileCopyInputs
err := UnmarshalJson(param, &inputs)
if err != nil {
return nil, err
}
return inputs, nil
}
func (action *FileCopyAction) SetAcceptLanguage(language string) {
action.Language = language
}
func (action *FileCopyAction) CheckParam(input FileCopyInput) error {
if input.EndPoint == "" {
return getParamEmptyError(action.Language, "endpoint")
}
if input.Target == "" {
return getParamEmptyError(action.Language, "target")
}
if input.DestinationPath == "" {
return getParamEmptyError(action.Language, "destinationPath")
}
if input.Unpack == "true" {
if input.FileOwner == "" {
return getParamEmptyError(action.Language, "fileOwner")
}
}
return nil
}
func buildFileDestinationPath(endpoint string, destPath string) string {
index := strings.LastIndexAny(destPath, "/")
if index != len([]rune(destPath))-1 {
return destPath
}
packageName, _ := getPackageNameFromEndpoint(endpoint)
return destPath + packageName
}
func (action *FileCopyAction) changeDirectoryOwner(input *FileCopyInput) error {
request := SaltApiRequest{}
request.Client = "local"
request.TargetType = "ipcidr"
request.Target = input.Target
request.Function = "cmd.run"
if !strings.Contains(input.FileOwner, ":") {
input.FileOwner = fmt.Sprintf("%s:%s", input.FileOwner, input.FileOwner)
}
directory := ""
if lastIndex := strings.LastIndex(input.DestinationPath, "/"); lastIndex >= 0 {
directory = input.DestinationPath[0:lastIndex]
} else {
return fmt.Errorf("destinationPath:%s illegal with absolute path check ", input.DestinationPath)
}
//directory := input.DestinationPath[0:strings.LastIndex(input.DestinationPath, "/")]
cmdRun := "chown -R " + input.FileOwner + " " + directory
request.Args = append(request.Args, cmdRun)
output, err := CallSaltApi("https://127.0.0.1:8080", request, action.Language)
if err != nil {
return err
}
log.Logger.Debug("Change dir owner", log.String("command", cmdRun), log.String("output", output))
if strings.Contains(output, "chown") {
return fmt.Errorf(output)
}
return nil
}
func (action *FileCopyAction) copyFile(input *FileCopyInput) (output FileCopyOutput, err error) {
defer func() {
output.Guid = input.Guid
output.CallBackParameter.Parameter = input.CallBackParameter.Parameter
if err == nil {
output.Result.Code = RESULT_CODE_SUCCESS
} else {
output.Result.Code = RESULT_CODE_ERROR
output.Result.Message = err.Error()
}
}()
err = action.CheckParam(*input)
if err != nil {
return output, err
}
if input.FileOwner != "" {
userExist, errOut := checkRunUserIsExists(input.Target, input.FileOwner, action.Language)
if !userExist {
err = fmt.Errorf(errOut)
return output, err
}
}
fileName, tmpErr := downloadS3File(input.EndPoint, DefaultS3Key, DefaultS3Password, true, action.Language)
if tmpErr != nil {
log.Logger.Error("Download s3 file", log.String("path", input.EndPoint), log.Error(tmpErr))
err = tmpErr
return output, err
}
input.DestinationPath = buildFileDestinationPath(input.EndPoint, input.DestinationPath)
savePath, tmpErr := saveFileToSaltMasterBaseDir(fileName)
os.Remove(fileName)
if tmpErr != nil {
err = getS3DownloadError(action.Language, input.EndPoint, fmt.Sprintf("move download file to salt-dir error:%s", tmpErr.Error()))
return output, err
}
//copy file
copyRequest, err := action.deriveCopyFileRequest("salt://base/"+filepath.Base(savePath), input)
_, err = CallSaltApi("https://127.0.0.1:8080", *copyRequest, action.Language)
os.Remove(savePath)
if err != nil {
return output, err
}
md5SumRequest, _ := action.deriveMd5SumRequest(input)
md5sum, err := CallSaltApi("https://127.0.0.1:8080", *md5SumRequest, action.Language)
if err != nil {
return output, err
}
var unpackRequest *SaltApiRequest
if input.Unpack == "true" {
unpackRequest, err = action.deriveUnpackRequest(input)
if err != nil {
return output, err
}
unpackOutput, unpackErr := CallSaltApi("https://127.0.0.1:8080", *unpackRequest, action.Language)
if unpackErr != nil {
err = unpackErr
return output, err
}
if strings.Contains(unpackOutput, "'archive.cmd_unzip' not found") || strings.Contains(unpackOutput, "'archive.tar' not found") {
err = fmt.Errorf("can not find unzip or tar command in target host")
return output, err
}
if _, err = CallSaltApi("https://127.0.0.1:8080", *unpackRequest, action.Language); err != nil {
return output, err
}
}
if input.FileOwner != "" {
if err = action.changeDirectoryOwner(input); err != nil {
return output, err
}
}
output.Detail = md5sum
return output, err
}
func (action *FileCopyAction) deriveMd5SumRequest(input *FileCopyInput) (*SaltApiRequest, error) {
request := SaltApiRequest{}
request.Client = "local"
request.TargetType = "ipcidr"
request.Target = input.Target
request.Function = "file.get_hash"
request.Args = append(request.Args, input.DestinationPath)
request.Args = append(request.Args, "md5")
return &request, nil
}
func (action *FileCopyAction) deriveCopyFileRequest(basePath string, input *FileCopyInput) (*SaltApiRequest, error) {
request := SaltApiRequest{}
request.Client = "local"
request.TargetType = "ipcidr"
request.Target = input.Target
request.Function = "cp.get_file"
request.Args = append(request.Args, basePath)
request.Args = append(request.Args, input.DestinationPath)
request.Args = append(request.Args, "makedirs=true")
request.Args = append(request.Args, "gzip=5")
return &request, nil
}
func (action *FileCopyAction) deriveUnpackRequest(input *FileCopyInput) (*SaltApiRequest, error) {
request := SaltApiRequest{}
request.Client = "local"
request.TargetType = "ipcidr"
request.Target = input.Target
lowerFilepath := strings.ToLower(input.DestinationPath)
currentDirectory := ""
if lastIndex := strings.LastIndex(input.DestinationPath, "/"); lastIndex >= 0 {
currentDirectory = input.DestinationPath[0:lastIndex]
} else {
return &request, fmt.Errorf("destinationPath:%s illegal with absolute path check ", input.DestinationPath)
}
//currentDirectory := input.DestinationPath[0:strings.LastIndex(input.DestinationPath, "/")]
if strings.HasSuffix(lowerFilepath, ".zip") {
request.Function = "archive.cmd_unzip"
request.Args = append(request.Args, input.DestinationPath)
request.Args = append(request.Args, currentDirectory)
request.Args = append(request.Args, "options=-o")
} else if strings.HasSuffix(lowerFilepath, ".tar.gz") || strings.HasSuffix(lowerFilepath, ".tgz") {
request.Function = "archive.tar"
request.Args = append(request.Args, "zxf")
// request.Args = append(request.Args, "--overwrite")
request.Args = append(request.Args, input.DestinationPath)
request.Args = append(request.Args, "dest="+currentDirectory)
} else {
return &request, getDecompressSuffixError(action.Language, lowerFilepath)
}
return &request, nil
}
func (action *FileCopyAction) Do(input interface{}) (interface{}, error) {
files, _ := input.(FileCopyInputs)
outputs := FileCopyOutputs{}
var finalErr error
outputChan := make(chan FileCopyThreadObj, len(files.Inputs))
concurrentChan := make(chan int, ApiConcurrentNum)
wg := sync.WaitGroup{}
for i, file := range files.Inputs {
concurrentChan <- 1
wg.Add(1)
go func(tmpInput FileCopyInput, index int) {
output, err := action.copyFile(&tmpInput)
outputChan <- FileCopyThreadObj{Data: output, Err: err, Index: index}
wg.Done()
<-concurrentChan
}(file, i)
outputs.Outputs = append(outputs.Outputs, FileCopyOutput{})
}
wg.Wait()
for {
if len(outputChan) == 0 {
break
}
tmpOutput := <-outputChan
if tmpOutput.Err != nil {
log.Logger.Error("File copy action", log.Error(tmpOutput.Err))
finalErr = tmpOutput.Err
}
outputs.Outputs[tmpOutput.Index] = tmpOutput.Data
}
return &outputs, finalErr
}