forked from liamjbennett/sous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (128 loc) · 3.51 KB
/
main.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
// This tool sets up a Sous-specific QA environment. I think, actually, it
// could be made more generic by pushing configuration files into the
// test_registry directory, which might be worthwhile in idle moments
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"path"
"time"
"github.com/SeeSpotRun/coerce"
"github.com/docopt/docopt-go"
"github.com/opentable/sous/dev_support/sous_qa_setup/desc"
"github.com/opentable/sous/util/test_with_docker"
)
type (
parameters struct {
timeout string
shutdown bool
outPath string
composeDir string
}
options struct {
parameters
out io.Writer
timeDuration time.Duration
}
)
const (
docstring = `Set up a docker-based Sous QA environment
Usage: sous_qa_setup [options]
Options:
--compose-dir=<directory> The directory containing a 'docker-compose.yaml' file
--timeout=<timeout> Time allowed before a non-response by services is considered failure [default: 5m]
--out-path=<path> The path to write the description of the environment to, or - for stdout [default: -]
-K --shutdown Rather than set up the QA environment, shut it down
`
)
func parseOpts() (*options, error) {
parsed, err := docopt.Parse(docstring, nil, true, "", false)
if err != nil {
return nil, err
}
parms := parameters{}
err = coerce.Struct(&parms, parsed, "-%s", "--%s", "<%s>")
if err != nil {
return nil, err
}
if parms.composeDir == "" {
return nil, fmt.Errorf("--compose-dir is required")
}
opts := options{parameters: parms}
opts.timeDuration, err = time.ParseDuration(opts.timeout)
if err != nil {
return nil, err
}
opts.out = os.Stdout
if opts.outPath != "-" {
opts.out, err = os.Create(opts.outPath)
if err != nil {
return nil, err
}
}
err = checkCompDir(&opts)
if err != nil {
return nil, err
}
return &opts, nil
}
func checkCompDir(opts *options) error {
for _, composeName := range []string{"docker-compose.yaml", "docker-compose.yml"} {
info, err := os.Stat(path.Join(opts.composeDir, composeName))
if err == nil && !info.IsDir() {
return nil
}
}
return fmt.Errorf("No docker-compose.yaml in %q", opts.composeDir)
}
func main() {
log.SetFlags(0)
opts, err := parseOpts()
if err != nil {
log.Fatal(err)
}
testAgent := buildAgent(opts)
defer func() { testAgent.Cleanup() }()
if opts.shutdown {
teardownServices(testAgent, opts)
return
}
desc := setupServices(testAgent, opts)
writeOut(opts, desc)
}
func buildAgent(opts *options) test_with_docker.Agent {
testAgent, err := test_with_docker.NewAgentWithTimeout(opts.timeDuration)
if err != nil {
log.Fatal(err)
}
return testAgent
}
func teardownServices(testAgent test_with_docker.Agent, opts *options) {
testAgent.ShutdownNow()
}
func setupServices(testAgent test_with_docker.Agent, opts *options) *desc.EnvDesc {
desc := desc.EnvDesc{}
var err error
if desc.AgentIP, err = testAgent.IP(); err != nil {
log.Fatal(err)
}
if desc.AgentIP == nil {
log.Fatal(fmt.Errorf("Test agent returned nil IP"))
}
desc.RegistryName = fmt.Sprintf("%s:%d", desc.AgentIP, 5000)
desc.SingularityURL = fmt.Sprintf("http://%s:%d/singularity", desc.AgentIP, 7099)
if err := registryCerts(testAgent, opts.composeDir, desc); err != nil {
log.Fatal(err)
}
if _, err := testAgent.ComposeServices(opts.composeDir, map[string]uint{"Singularity": 7099, "Registry": 5000}); err != nil {
log.Fatal(err)
}
return &desc
}
func writeOut(opts *options, desc *desc.EnvDesc) {
enc := json.NewEncoder(opts.out)
enc.Encode(desc)
}