/
docker.go
143 lines (131 loc) · 3.9 KB
/
docker.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
package driver
import (
"context"
"errors"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"io"
"os"
"time"
)
type DockerClient interface {
ImagePull(ctx context.Context, refStr string, options image.PullOptions) (io.ReadCloser, error)
ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error)
ContainerStart(ctx context.Context, containerID string, options container.StartOptions) error
ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error)
ContainerLogs(ctx context.Context, container string, options container.LogsOptions) (io.ReadCloser, error)
ContainerRemove(ctx context.Context, containerID string, options container.RemoveOptions) error
}
type Docker struct {
Client DockerClient
}
func (d Docker) Start(job *Job) (*Job, error) {
// Clone the git repo
dir, err := cloneGitRepo(
job.Spec.Run.Id,
job.Spec.Workspace.Vcs.Source,
job.Spec.Workspace.Vcs.Branch,
job.Spec.Credentials,
)
if err != nil {
return nil, err
}
// Pull the image
_, err = d.Client.ImagePull(context.Background(), job.Spec.Image, image.PullOptions{})
if err != nil {
return nil, err
}
variables := []string{
fmt.Sprintf("CHUSHI_URL=%s", os.Getenv("CHUSHI_URL")),
fmt.Sprintf("CHUSHI_ORGANIZATION=%s", job.Spec.OrganizationId),
fmt.Sprintf("CHUSHI_RUN_ID=%s", job.Spec.Run.Id),
fmt.Sprintf("CHUSHI_ACCESS_TOKEN=%s", job.Spec.Token),
fmt.Sprintf("TF_HTTP_PASSWORD=%s", job.Spec.Token),
fmt.Sprintf("TF_HTTP_USERNAME=%s", "runner"),
}
for _, variable := range job.Spec.Variables {
if variable.Type == "environment" {
variables = append(variables, fmt.Sprintf("%s=%s", variable.Key, variable.Value))
}
}
cont, err := d.Client.ContainerCreate(
context.Background(),
&container.Config{
Image: job.Spec.Image,
Cmd: []string{
"runner",
fmt.Sprintf("-d=/workspace/%s", job.Spec.Workspace.Vcs.WorkingDirectory),
"-v=1.6.2",
"--grpc-url=http://host.docker.internal:5002",
job.Spec.Run.Operation,
},
Env: variables,
},
&container.HostConfig{
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: dir,
Target: "/workspace/",
},
},
},
nil,
nil,
job.Spec.Run.Id,
)
if err != nil {
return nil, err
}
if err := d.Client.ContainerStart(context.Background(), cont.ID, container.StartOptions{}); err != nil {
return nil, err
}
job.Status.Metadata["container_id"] = cont.ID
return job, nil
}
func (d Docker) Wait(job *Job) (*Job, error) {
containerId, ok := job.Status.Metadata["container_id"]
if !ok {
return nil, errors.New("no container ID found")
}
// TODO: We shouldn't sleep, instead opting
// to check for creating / initializing states
time.Sleep(time.Second * 10)
for {
data, err := d.Client.ContainerInspect(context.TODO(), containerId)
if err != nil {
return nil, err
}
if !data.State.Running {
if data.State.ExitCode == 0 {
return job, nil
}
return job, errors.New(fmt.Sprintf("Exited with code %d", data.State.ExitCode))
}
time.Sleep(time.Second * 1)
}
}
func (d Docker) Cleanup(job *Job) error {
containerId, ok := job.Status.Metadata["container_id"]
if !ok {
return errors.New("no container ID found")
}
logs, err := d.Client.ContainerLogs(context.TODO(), containerId, container.LogsOptions{
ShowStderr: true,
ShowStdout: true,
})
if err != nil {
fmt.Println("Failed getting container logs")
} else {
io.Copy(os.Stdout, logs)
}
return d.Client.ContainerRemove(context.TODO(), containerId, container.RemoveOptions{
RemoveVolumes: true,
RemoveLinks: true,
})
}