-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresource.go
114 lines (94 loc) · 2.73 KB
/
resource.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
package forge
import (
"bytes"
"context"
"encoding/json"
"fmt"
"path/filepath"
"github.com/frantjc/forge/concourse"
xos "github.com/frantjc/x/os"
)
type Resource struct {
Method string
Version map[string]any
Params map[string]any
Resource *concourse.Resource
ResourceType *concourse.ResourceType
}
func (o *Resource) Run(ctx context.Context, containerRuntime ContainerRuntime, opts ...RunOpt) error {
opt := runOptsWithDefaults(opts...)
image, err := containerRuntime.PullImage(ctx, resourceTypeToImageReference(o.ResourceType))
if err != nil {
return err
}
containerConfig := resourceToConfig(o.Resource, o.ResourceType, o.Method, opt)
containerConfig.Mounts = overrideMounts(containerConfig.Mounts, opt.Mounts...)
container, err := createSleepingContainer(ctx, containerRuntime, image, containerConfig, opt)
if err != nil {
return err
}
defer container.Stop(ctx) //nolint:errcheck
defer container.Remove(ctx) //nolint:errcheck
streams, err := resourceStreams(opt.Streams, &concourse.Input{
Params: func() map[string]any {
if o.Method == concourse.MethodCheck {
return nil
}
return o.Params
}(),
Source: o.Resource.Source,
Version: func() map[string]any {
if o.Method == concourse.MethodPut {
return nil
}
return o.Version
}(),
})
if err != nil {
return err
}
if exitCode, err := container.Exec(ctx, containerConfig, streams); err != nil {
return err
} else if exitCode > 0 {
return xos.NewExitCodeError(ErrContainerExitedWithNonzeroExitCode, exitCode)
}
return nil
}
func resourceStreams(streams *Streams, input *concourse.Input) (*Streams, error) {
if streams.In != nil {
return nil, fmt.Errorf("stdin not supported for Concourse Resources")
}
in := new(bytes.Buffer)
if err := json.NewEncoder(in).Encode(input); err != nil {
return nil, err
}
return &Streams{
In: in,
Out: streams.Out,
Err: streams.Err,
Tty: streams.Tty,
DetachKeys: streams.DetachKeys,
}, nil
}
func resourceToConfig(resource *concourse.Resource, resourceType *concourse.ResourceType, method string, opt *RunOpts) *ContainerConfig {
return &ContainerConfig{
Entrypoint: concourse.GetEntrypoint(method),
Cmd: []string{filepath.Join(ConcourseResourceWorkingDir(opt.WorkingDir), resource.Name)},
Privileged: resourceType.Privileged,
Mounts: []Mount{
{
Destination: filepath.Join(ConcourseResourceWorkingDir(opt.WorkingDir), resource.Name),
},
},
}
}
func resourceTypeToImageReference(resourceType *concourse.ResourceType) string {
if resourceType != nil && resourceType.Source != nil {
tag := resourceType.Source.Tag
if tag == "" {
tag = "latest"
}
return resourceType.Source.Repository + ":" + tag
}
return ""
}