-
Notifications
You must be signed in to change notification settings - Fork 18
/
volume.go
169 lines (144 loc) · 5.4 KB
/
volume.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
package volume
import (
"context"
"errors"
"fmt"
"github.com/cirruslabs/cirrus-cli/internal/executor/instance/containerbackend"
"github.com/cirruslabs/cirrus-cli/internal/executor/instance/runconfig"
"github.com/cirruslabs/cirrus-cli/internal/executor/options"
"github.com/cirruslabs/cirrus-cli/internal/executor/platform"
"github.com/cirruslabs/cirrus-cli/internal/executor/pullhelper"
"github.com/google/uuid"
"runtime"
)
var (
ErrVolumeCreationFailed = errors.New("working volume creation failed")
ErrVolumeCleanupFailed = errors.New("failed to clean up working volume")
)
type Volume struct {
name string
}
// CreateWorkingVolumeFromConfig returns name of the working volume created according to the specification in config.
func CreateWorkingVolumeFromConfig(
ctx context.Context,
config *runconfig.RunConfig,
platform platform.Platform,
) (*Volume, *Volume, error) {
initLogger := config.Logger().Scoped("Preparing execution environment...")
initLogger.Infof("Preparing volume to work with...")
identifier := uuid.New().String()
agentVolumeName := fmt.Sprintf("cirrus-agent-volume-%s", identifier)
workingVolumeName := fmt.Sprintf("cirrus-working-volume-%s", identifier)
backend, err := config.GetContainerBackend()
if err != nil {
return nil, nil, err
}
agentVolume, workingVolume, err := CreateWorkingVolume(ctx, backend, config.ContainerOptions,
agentVolumeName, workingVolumeName, config.ProjectDir, config.DirtyMode, config.GetAgentVersion(), platform)
if err != nil {
initLogger.Warnf("Failed to create a volume from working directory: %v", err)
initLogger.Finish(false)
return nil, nil, err
}
initLogger.Finish(true)
return agentVolume, workingVolume, err
}
// CreateWorkingVolume returns name of the working volume created according to the specification in arguments.
func CreateWorkingVolume(
ctx context.Context,
backend containerbackend.ContainerBackend,
containerOptions options.ContainerOptions,
agentVolumeName string,
workingVolumeName string,
projectDir string,
dontPopulate bool,
agentVersion string,
platform platform.Platform,
) (agentVolume *Volume, vol *Volume, err error) {
agentImage := platform.ContainerAgentImage(agentVersion)
if err := pullhelper.PullHelper(ctx, agentImage, backend, containerOptions, nil); err != nil {
return nil, nil, fmt.Errorf("%w: when pulling agent image %s: %v",
ErrVolumeCreationFailed, agentImage, err)
}
if err := backend.VolumeCreate(ctx, agentVolumeName); err != nil {
return nil, nil, fmt.Errorf("%w: when creating agent volume: %v", ErrVolumeCreationFailed, err)
}
if err := backend.VolumeCreate(ctx, workingVolumeName); err != nil {
return nil, nil, fmt.Errorf("%w: when creating working volume: %v", ErrVolumeCreationFailed, err)
}
defer func() {
if err != nil {
_ = backend.VolumeDelete(ctx, agentVolumeName)
_ = backend.VolumeDelete(ctx, workingVolumeName)
}
}()
copyCommand := platform.ContainerCopyCommand(!dontPopulate)
// Create and start a helper container that will copy the project directory (if needed) and the agent
// into the working volume
input := &containerbackend.ContainerCreateInput{
Image: agentImage,
Command: copyCommand.Command,
Mounts: []containerbackend.ContainerMount{
{
Type: containerbackend.MountTypeVolume,
Source: agentVolumeName,
Target: copyCommand.CopiesAgentToDir,
},
},
}
// When using non-dirty mode we need to do a full copy of the project directory
if !dontPopulate {
input.Mounts = append(input.Mounts, containerbackend.ContainerMount{
Type: containerbackend.MountTypeBind,
Source: projectDir,
Target: copyCommand.CopiesProjectFromDir,
ReadOnly: true,
})
input.Mounts = append(input.Mounts, containerbackend.ContainerMount{
Type: containerbackend.MountTypeVolume,
Source: workingVolumeName,
Target: copyCommand.CopiesProjectToDir,
})
if runtime.GOOS == "linux" {
// Disable SELinux confinement for this container, otherwise
// the rsync might fail when accessing the project directory
input.DisableSELinux = true
}
}
containerName := fmt.Sprintf("cirrus-helper-container-%s", uuid.New().String())
cont, err := backend.ContainerCreate(ctx, input, containerName)
if err != nil {
return nil, nil, fmt.Errorf("%w: when creating helper container: %v", ErrVolumeCreationFailed, err)
}
defer func() {
removeErr := backend.ContainerDelete(ctx, cont.ID)
if removeErr != nil {
err = fmt.Errorf("%w: %v", ErrVolumeCreationFailed, removeErr)
}
}()
err = backend.ContainerStart(ctx, cont.ID)
if err != nil {
return nil, nil, fmt.Errorf("%w: when starting helper container: %v", ErrVolumeCreationFailed, err)
}
// Wait for the container to finish copying
waitChan, errChan := backend.ContainerWait(ctx, cont.ID)
select {
case res := <-waitChan:
if res.StatusCode != 0 {
return nil, nil, fmt.Errorf("%w: helper container exited with %v error and exit code %d",
ErrVolumeCreationFailed, res.Error, res.StatusCode)
}
case err := <-errChan:
return nil, nil, fmt.Errorf("%w: while waiting for helper container: %v", ErrVolumeCreationFailed, err)
}
return &Volume{agentVolumeName}, &Volume{workingVolumeName}, nil
}
func (volume *Volume) Name() string {
return volume.name
}
func (volume *Volume) Close(backend containerbackend.ContainerBackend) error {
if err := backend.VolumeDelete(context.Background(), volume.name); err != nil {
return fmt.Errorf("%w: %v", ErrVolumeCleanupFailed, err)
}
return nil
}