forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
189 lines (153 loc) · 3.69 KB
/
get.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
package cessna
import (
"bytes"
"encoding/json"
"archive/tar"
"os"
"code.cloudfoundry.org/garden"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/baggageclaim"
uuid "github.com/nu7hatch/gouuid"
"github.com/tedsuo/ifrit"
)
type ResourceGet struct {
Resource
Version atc.Version
Params atc.Params
}
func (r ResourceGet) Get(logger lager.Logger, worker Worker) (baggageclaim.Volume, error) {
rootFSPath, err := r.ResourceType.RootFSPathFor(logger, worker)
if err != nil {
return nil, err
}
// Empty Volume for Get
spec := baggageclaim.VolumeSpec{
Strategy: baggageclaim.EmptyStrategy{},
Privileged: true,
}
handle, err := uuid.NewV4()
if err != nil {
return nil, err
}
volumeForGet, err := worker.BaggageClaimClient().CreateVolume(logger, handle.String(), spec)
if err != nil {
return nil, err
}
// Turn into Container
mountPath := "/tmp/resource/get"
mount := garden.BindMount{
SrcPath: volumeForGet.Path(),
DstPath: mountPath,
Mode: garden.BindMountModeRW,
}
bindMounts := []garden.BindMount{mount}
gardenSpec := garden.ContainerSpec{
Privileged: true,
RootFSPath: rootFSPath,
BindMounts: bindMounts,
}
container, err := worker.GardenClient().Create(gardenSpec)
if err != nil {
logger.Error("failed-to-create-gardenContainer-in-garden", err)
return nil, err
}
// Stream fake tar into container to make sure directory exists
// stream into baseDirectory
emptyTar := new(bytes.Buffer)
err = tar.NewWriter(emptyTar).Close()
if err != nil {
return nil, err
}
err = container.StreamIn(garden.StreamInSpec{
Path: mountPath,
TarStream: emptyTar,
})
runner, err := r.newGetCommandProcess(container, mountPath)
if err != nil {
logger.Error("failed-to-create-get-command-process", err)
}
getting := ifrit.Invoke(runner)
err = <-getting.Wait()
if err != nil {
return nil, err
}
return volumeForGet, nil
}
func (r ResourceGet) RootFSPathFor(logger lager.Logger, worker Worker) (string, error) {
v, err := r.Get(logger, worker)
if err != nil {
return "", err
}
// COW of RootFS Volume
spec := baggageclaim.VolumeSpec{
Strategy: baggageclaim.COWStrategy{
Parent: v,
},
Privileged: true,
}
handle, err := uuid.NewV4()
if err != nil {
return "", err
}
rootFSVolume, err := worker.BaggageClaimClient().CreateVolume(logger, handle.String(), spec)
if err != nil {
return "", err
}
return "raw://" + rootFSVolume.Path(), nil
}
func (r ResourceGet) newGetCommandProcess(container garden.Container, mountPath string) (*getCommandProcess, error) {
p := &ContainerProcess{
Container: container,
ProcessSpec: garden.ProcessSpec{
Path: "/opt/resource/in",
Args: []string{mountPath},
},
}
i := InRequest{
Source: r.Source,
Params: r.Params,
Version: r.Version,
}
input, err := json.Marshal(i)
if err != nil {
return nil, err
}
var (
stdout bytes.Buffer
stderr bytes.Buffer
)
p.ProcessIO.Stdin = bytes.NewBuffer(input)
p.ProcessIO.Stdout = &stdout
p.ProcessIO.Stderr = &stderr
return &getCommandProcess{
ContainerProcess: p,
out: &stdout,
err: &stderr,
}, nil
}
type getCommandProcess struct {
*ContainerProcess
out *bytes.Buffer
err *bytes.Buffer
}
func (g *getCommandProcess) Response() (InResponse, error) {
var r InResponse
err := json.NewDecoder(g.out).Decode(&r)
if err != nil {
return InResponse{}, err
}
return r, nil
}
func (g *getCommandProcess) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
err := g.ContainerProcess.Run(signals, ready)
if err != nil {
switch e := err.(type) {
case ErrScriptFailed:
e.Stderr = string(g.err.Bytes())
e.Stdout = string(g.out.Bytes())
err = e
}
}
return err
}