-
Notifications
You must be signed in to change notification settings - Fork 180
/
shell.go
269 lines (223 loc) · 6.77 KB
/
shell.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
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package cmd
import (
"context"
"io"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/errors"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/jobs"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/views"
"github.com/pydio/cells/scheduler/actions"
)
type ShellAction struct {
Client client.Client
Router *views.Router
CmdBin string
CmdParameters []string
TemporaryFolder string
ExitOnError bool
// Input via tmp file or via StdIn
StreamToStdIn bool
UseTemporaryFolder bool
// Output (if any) from tmp file or from StdOut
OutputNodePlaceHolder string
StreamFromStdOut bool
// Output result
ResultPlaceHolder string
}
var (
shellActionName = "actions.cmd.shell"
)
// Unique identifier
func (c *ShellAction) GetName() string {
return shellActionName
}
// Pass parameters
func (c *ShellAction) Init(job *jobs.Job, cl client.Client, action *jobs.Action) error {
c.TemporaryFolder = os.TempDir()
c.Router = views.NewStandardRouter(views.RouterOptions{AdminView: true})
if command, ok := action.Parameters["cmd"]; ok {
c.CmdBin = command
} else {
return errors.BadRequest(common.SERVICE_JOBS, "Invalid parameters for action Shell")
}
c.CmdParameters = strings.Split(action.Parameters["parameters"], " ")
c.UseTemporaryFolder, _ = strconv.ParseBool(action.Parameters["inputTempFile"])
return nil
}
// Run the actual action code
func (c *ShellAction) Run(ctx context.Context, channels *actions.RunnableChannels, input jobs.ActionMessage) (jobs.ActionMessage, error) {
var stdIn io.Reader
var tempFileIn string
var outputNode *tree.Node
var stdOut io.Writer
var tempFileOut string
output := input
// Read Input File
if c.UseTemporaryFolder || c.StreamToStdIn {
if len(input.Nodes) == 0 {
return input.WithIgnore(), nil
}
if c.UseTemporaryFolder {
reader, e := c.Router.GetObject(ctx, input.Nodes[0], &views.GetRequestData{StartOffset: 0, Length: -1})
if e != nil {
return input.WithError(e), e
}
defer reader.Close()
// Create tmp file
file, e := ioutil.TempFile(c.TemporaryFolder, "pydio-cmd-input-")
if e != nil {
return input.WithError(e), e
}
defer file.Close()
defer os.Remove(file.Name())
written, e := io.Copy(file, reader)
if e != nil {
return input.WithError(e), e
}
if written != input.Nodes[0].Size {
err := errors.InternalServerError(common.SERVICE_JOBS, "Written number of bytes differ from original node Size, this is weird")
return input.WithError(err), err
}
tempFileIn = file.Name()
} else {
reader, e := c.Router.GetObject(ctx, input.Nodes[0], &views.GetRequestData{StartOffset: 0, Length: -1})
if e != nil {
return input.WithError(e), e
}
defer reader.Close()
stdIn = reader
}
}
if len(c.OutputNodePlaceHolder) > 0 {
// TODO: Compute from placeholder pattern
if len(input.Nodes) != 0 {
outputNode = &tree.Node{
Path: input.Nodes[0].Path + ".cmd-output",
}
} else {
outputNode = &tree.Node{
Path: c.OutputNodePlaceHolder,
}
}
output.Nodes[0] = outputNode
if c.StreamFromStdOut {
var pipeR *io.PipeReader
pipeR, stdOut = io.Pipe()
go func() {
_, e := c.Router.PutObject(ctx, outputNode, pipeR, &views.PutRequestData{})
if e != nil {
log.Logger(ctx).Error("Error while copying output", zap.Error(e))
}
}()
} else {
// Create a temporary filename for writing output
file, e := ioutil.TempFile(c.TemporaryFolder, "pydio-cmd-output-")
if e != nil {
return input.WithError(e), e
}
tempFileOut = file.Name()
file.Close()
}
}
params := c.CmdParameters
if len(tempFileIn) > 0 || len(tempFileOut) > 0 {
oldNew := []string{}
if len(tempFileIn) > 0 {
oldNew = append(oldNew, "PYDIO_INPUT_FILE")
oldNew = append(oldNew, tempFileIn)
}
if len(tempFileOut) > 0 {
oldNew = append(oldNew, "PYDIO_OUTPUT_FILE")
oldNew = append(oldNew, tempFileOut)
}
replacer := strings.NewReplacer(oldNew...)
for index, param := range c.CmdParameters {
params[index] = replacer.Replace(param)
}
}
log.Logger(ctx).Debug("Running command:", zap.String("bin", c.CmdBin), zap.Strings("params", params))
command := exec.Command(c.CmdBin, params...)
if stdIn != nil {
command.Stdin = stdIn
}
if stdOut != nil {
command.Stdout = stdOut
}
var exitStatus int
var cmdError *exec.ExitError
out, e := command.Output()
if e != nil {
if c.ExitOnError {
return input.WithError(e), e
}
// Did the command fail because of an unsuccessful exit code ?
if exitError, ok := e.(*exec.ExitError); ok {
cmdError = exitError
waitStatus := exitError.Sys().(syscall.WaitStatus)
exitStatus = waitStatus.ExitStatus()
} else {
log.Logger(ctx).Error("Error Running command, without exit status", zap.String("bin", c.CmdBin), zap.Strings("params", params), zap.Error(e))
return input.WithError(e), e
}
} else {
// Command was successful
waitStatus := command.ProcessState.Sys().(syscall.WaitStatus)
exitStatus = waitStatus.ExitStatus()
}
// DO SOMETHING WITH OUTPUT
log.Logger(ctx).Debug("Command Output was", zap.ByteString("out", out), zap.Int("exit", exitStatus))
if exitStatus > 0 && cmdError != nil { // If we are there, error but no ExitOnError => pass the error to next action(s)
output.AppendOutput(&jobs.ActionOutput{
Success: false,
StringBody: string(out),
ErrorString: cmdError.Error(),
})
return output, nil
}
output.AppendOutput(&jobs.ActionOutput{
Success: true,
StringBody: string(out),
})
if len(tempFileOut) > 0 {
// Read back temp file from system
readFile, e := os.Open(tempFileOut)
if e == nil {
defer readFile.Close()
defer os.Remove(tempFileOut)
fInfo, _ := readFile.Stat()
c.Router.PutObject(ctx, outputNode, readFile, &views.PutRequestData{
Size: fInfo.Size(),
})
}
}
return output, nil
}