This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
host.go
198 lines (188 loc) · 5.9 KB
/
host.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
190
191
192
193
194
195
196
197
198
package main
import (
"context"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/sirupsen/logrus"
)
const (
defaultStackName = "integration-cli-on-swarm"
defaultVolumeName = "integration-cli-on-swarm"
defaultMasterImageName = "integration-cli-master"
defaultWorkerImageName = "integration-cli-worker"
)
func main() {
rc, err := xmain()
if err != nil {
logrus.Fatalf("fatal error: %v", err)
}
os.Exit(rc)
}
func xmain() (int, error) {
// Should we use cobra maybe?
replicas := flag.Int("replicas", 1, "Number of worker service replica")
chunks := flag.Int("chunks", 0, "Number of test chunks executed in batch (0 == replicas)")
pushWorkerImage := flag.String("push-worker-image", "", "Push the worker image to the registry. Required for distributed execution. (empty == not to push)")
shuffle := flag.Bool("shuffle", false, "Shuffle the input so as to mitigate makespan nonuniformity")
// flags below are rarely used
randSeed := flag.Int64("rand-seed", int64(0), "Random seed used for shuffling (0 == current time)")
filtersFile := flag.String("filters-file", "", "Path to optional file composed of `-check.f` filter strings")
dryRun := flag.Bool("dry-run", false, "Dry run")
keepExecutor := flag.Bool("keep-executor", false, "Do not auto-remove executor containers, which is used for running privileged programs on Swarm")
flag.Parse()
if *chunks == 0 {
*chunks = *replicas
}
if *randSeed == int64(0) {
*randSeed = time.Now().UnixNano()
}
cli, err := client.NewEnvClient()
if err != nil {
return 1, err
}
if hasStack(cli, defaultStackName) {
logrus.Infof("Removing stack %s", defaultStackName)
removeStack(cli, defaultStackName)
}
if hasVolume(cli, defaultVolumeName) {
logrus.Infof("Removing volume %s", defaultVolumeName)
removeVolume(cli, defaultVolumeName)
}
if err = ensureImages(cli, []string{defaultWorkerImageName, defaultMasterImageName}); err != nil {
return 1, err
}
workerImageForStack := defaultWorkerImageName
if *pushWorkerImage != "" {
logrus.Infof("Pushing %s to %s", defaultWorkerImageName, *pushWorkerImage)
if err = pushImage(cli, *pushWorkerImage, defaultWorkerImageName); err != nil {
return 1, err
}
workerImageForStack = *pushWorkerImage
}
compose, err := createCompose("", cli, composeOptions{
Replicas: *replicas,
Chunks: *chunks,
MasterImage: defaultMasterImageName,
WorkerImage: workerImageForStack,
Volume: defaultVolumeName,
Shuffle: *shuffle,
RandSeed: *randSeed,
DryRun: *dryRun,
KeepExecutor: *keepExecutor,
})
if err != nil {
return 1, err
}
filters, err := filtersBytes(*filtersFile)
if err != nil {
return 1, err
}
logrus.Infof("Creating volume %s with input data", defaultVolumeName)
if err = createVolumeWithData(cli,
defaultVolumeName,
map[string][]byte{"/input": filters},
defaultMasterImageName); err != nil {
return 1, err
}
logrus.Infof("Deploying stack %s from %s", defaultStackName, compose)
defer func() {
logrus.Infof("NOTE: You may want to inspect or clean up following resources:")
logrus.Infof(" - Stack: %s", defaultStackName)
logrus.Infof(" - Volume: %s", defaultVolumeName)
logrus.Infof(" - Compose file: %s", compose)
logrus.Infof(" - Master image: %s", defaultMasterImageName)
logrus.Infof(" - Worker image: %s", workerImageForStack)
}()
if err = deployStack(cli, defaultStackName, compose); err != nil {
return 1, err
}
logrus.Infof("The log will be displayed here after some duration."+
"You can watch the live status via `docker service logs %s_worker`",
defaultStackName)
masterContainerID, err := waitForMasterUp(cli, defaultStackName)
if err != nil {
return 1, err
}
rc, err := waitForContainerCompletion(cli, os.Stdout, os.Stderr, masterContainerID)
if err != nil {
return 1, err
}
logrus.Infof("Exit status: %d", rc)
return int(rc), nil
}
func ensureImages(cli *client.Client, images []string) error {
for _, image := range images {
_, _, err := cli.ImageInspectWithRaw(context.Background(), image)
if err != nil {
return fmt.Errorf("could not find image %s, please run `make build-integration-cli-on-swarm`: %v",
image, err)
}
}
return nil
}
func filtersBytes(optionalFiltersFile string) ([]byte, error) {
var b []byte
if optionalFiltersFile == "" {
tests, err := enumerateTests(".")
if err != nil {
return b, err
}
b = []byte(strings.Join(tests, "\n") + "\n")
} else {
var err error
b, err = ioutil.ReadFile(optionalFiltersFile)
if err != nil {
return b, err
}
}
return b, nil
}
func waitForMasterUp(cli *client.Client, stackName string) (string, error) {
// FIXME(AkihiroSuda): it should retry until master is up, rather than pre-sleeping
time.Sleep(10 * time.Second)
fil := filters.NewArgs()
fil.Add("label", "com.docker.stack.namespace="+stackName)
// FIXME(AkihiroSuda): we should not rely on internal service naming convention
fil.Add("label", "com.docker.swarm.service.name="+stackName+"_master")
masters, err := cli.ContainerList(context.Background(), types.ContainerListOptions{
All: true,
Filters: fil,
})
if err != nil {
return "", err
}
if len(masters) == 0 {
return "", fmt.Errorf("master not running in stack %s?", stackName)
}
return masters[0].ID, nil
}
func waitForContainerCompletion(cli *client.Client, stdout, stderr io.Writer, containerID string) (int64, error) {
stream, err := cli.ContainerLogs(context.Background(),
containerID,
types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
})
if err != nil {
return 1, err
}
stdcopy.StdCopy(stdout, stderr, stream)
stream.Close()
resultC, errC := cli.ContainerWait(context.Background(), containerID, "")
select {
case err := <-errC:
return 1, err
case result := <-resultC:
return result.StatusCode, nil
}
}