This repository has been archived by the owner on Apr 14, 2024. It is now read-only.
/
docker.go
155 lines (128 loc) · 4.26 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
144
145
146
147
148
149
150
151
152
153
154
155
package core
import (
"code.gitea.io/sdk/gitea"
"context"
"fmt"
"github.com/codemicro/workboat/workboat/config"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
dockerClient "github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"net/url"
"time"
)
type dockerJob struct {
RepoOwner string
RepoName string
CommitSha string
CloneURL *url.URL
ManifestEntry *workflowManifestEntry
}
var jobQueue = make(chan *dockerJob, 512)
func enqueueDockerJob(job *dockerJob) {
jobQueue <- job
}
func StartJobConsumer() {
go func() {
for job := range jobQueue {
err := runDockerJob(job)
if err != nil {
log.Error().Err(err).Stack().Msg("failed to run Docker job")
if err := ReportRepoStatus(job.RepoOwner, job.RepoName, job.CommitSha, &gitea.CreateStatusOption{
State: gitea.StatusError,
Description: "experienced an internal error and aborted",
}); err != nil {
log.Warn().Err(err).Stack().Msg("unable to report status back to Gitea")
}
}
}
}()
}
func runDockerJob(job *dockerJob) error {
log.Info().Msgf("starting job for %s/%s@%s", job.RepoOwner, job.RepoName, job.ManifestEntry.Ref)
if err := ReportRepoStatus(job.RepoOwner, job.RepoName, job.CommitSha, &gitea.CreateStatusOption{
State: gitea.StatusPending,
Description: "running",
}); err != nil {
log.Warn().Err(err).Stack().Msg("unable to report status back to Gitea")
}
client, err := dockerClient.NewClientWithOpts(
dockerClient.WithHost(config.Docker.Socket),
dockerClient.WithAPIVersionNegotiation(),
)
if err != nil {
return errors.WithStack(err)
}
defer client.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*10)
defer cancel()
// Arguments to the container should be: clone URL, auth string for that domain, user command
authURL := &url.URL{
Scheme: job.CloneURL.Scheme,
User: url.UserPassword(job.RepoOwner, config.Gitea.AccessToken),
Host: job.CloneURL.Host,
}
log.Info().Msg("creating container")
// Spin up Docker container
container, err := client.ContainerCreate(ctx, &container.Config{
AttachStdout: true,
AttachStderr: true,
Env: nil,
Cmd: []string{job.CloneURL.String(), authURL.String(), job.ManifestEntry.Command},
Image: config.Docker.Image,
}, nil, nil, nil, "")
if err != nil {
return errors.WithStack(err)
}
shortContainerID := container.ID
if len(shortContainerID) > 12 {
shortContainerID = shortContainerID[:12]
}
log.Debug().Str("container_id", container.ID).Send()
log.Info().Msg("starting container")
if err := client.ContainerStart(ctx, container.ID, types.ContainerStartOptions{}); err != nil {
_ = client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{})
return errors.WithStack(err)
}
if err := ReportRepoStatus(job.RepoOwner, job.RepoName, job.CommitSha, &gitea.CreateStatusOption{
State: gitea.StatusPending,
Description: fmt.Sprintf("running (container ID: %s)", shortContainerID),
}); err != nil {
log.Warn().Err(err).Stack().Msg("unable to report status back to Gitea")
}
log.Info().Msg("waiting for container to end")
okc, errc := client.ContainerWait(ctx, container.ID, "not-running")
var exitStatusCode int64
select {
case err := <-errc:
return errors.WithStack(err)
case kc := <-okc:
exitStatusCode = kc.StatusCode
}
log.Info().Msgf("finished %d", exitStatusCode)
var statusOpt *gitea.CreateStatusOption
if exitStatusCode == 250 {
return errors.New("runtime error, code 250")
}
if exitStatusCode == 0 {
log.Info().Msgf("removing container %s", container.ID)
if err := client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{}); err != nil {
return errors.WithStack(err)
}
log.Info().Msg("done")
statusOpt = &gitea.CreateStatusOption{
State: gitea.StatusSuccess,
Description: "finished",
}
} else {
statusOpt = &gitea.CreateStatusOption{
State: gitea.StatusFailure,
Description: fmt.Sprintf("failed with status code %d (container ID: %s)", exitStatusCode, shortContainerID),
}
}
if err := ReportRepoStatus(job.RepoOwner, job.RepoName, job.CommitSha, statusOpt); err != nil {
log.Warn().Err(err).Stack().Msg("unable to report status back to Gitea")
}
return nil
}