This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
/
run_script.go
139 lines (114 loc) · 2.37 KB
/
run_script.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
package resource
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"code.cloudfoundry.org/garden"
)
const resourceResultPropertyName = "concourse:resource-result"
const TaskProcessID = "resource"
type ErrResourceScriptFailed struct {
Path string
Args []string
ExitStatus int
Stderr string
}
func (err ErrResourceScriptFailed) Error() string {
msg := fmt.Sprintf(
"resource script '%s %v' failed: exit status %d",
err.Path,
err.Args,
err.ExitStatus,
)
if len(err.Stderr) > 0 {
msg += "\n\nstderr:\n" + err.Stderr
}
return msg
}
func (resource *resource) runScript(
ctx context.Context,
path string,
args []string,
input interface{},
output interface{},
logDest io.Writer,
recoverable bool,
) error {
request, err := json.Marshal(input)
if err != nil {
return err
}
if recoverable {
result, err := resource.container.Property(resourceResultPropertyName)
if err == nil {
return json.Unmarshal([]byte(result), &output)
}
}
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
processIO := garden.ProcessIO{
Stdin: bytes.NewBuffer(request),
Stdout: stdout,
}
if logDest != nil {
processIO.Stderr = logDest
} else {
processIO.Stderr = stderr
}
var process garden.Process
if recoverable {
process, err = resource.container.Attach(TaskProcessID, processIO)
if err != nil {
process, err = resource.container.Run(garden.ProcessSpec{
ID: TaskProcessID,
Path: path,
Args: args,
}, processIO)
if err != nil {
return err
}
}
} else {
process, err = resource.container.Run(garden.ProcessSpec{
Path: path,
Args: args,
}, processIO)
if err != nil {
return err
}
}
processExited := make(chan struct{})
var processStatus int
var processErr error
go func() {
processStatus, processErr = process.Wait()
close(processExited)
}()
select {
case <-processExited:
if processErr != nil {
return processErr
}
if processStatus != 0 {
return ErrResourceScriptFailed{
Path: path,
Args: args,
ExitStatus: processStatus,
Stderr: stderr.String(),
}
}
if recoverable {
err := resource.container.SetProperty(resourceResultPropertyName, stdout.String())
if err != nil {
return err
}
}
return json.Unmarshal(stdout.Bytes(), output)
case <-ctx.Done():
resource.container.Stop(false)
<-processExited
return ctx.Err()
}
}