This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
main.go
276 lines (242 loc) · 5.63 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"text/template"
"code.google.com/p/go.crypto/ssh"
"github.com/flynn/flynn-test/arg"
"github.com/flynn/flynn-test/cluster"
"github.com/flynn/flynn-test/util"
"gopkg.in/check.v1"
)
var sshWrapper = template.Must(template.New("ssh").Parse(`
#!/bin/bash
ssh -o LogLevel=FATAL -o IdentitiesOnly=yes -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i {{.SSHKey}} "$@"
`[1:]))
var gitEnv []string
var args *arg.Args
var flynnrc string
func init() {
args = arg.Parse()
log.SetFlags(log.Lshortfile)
}
func main() {
flynnrc = args.Flynnrc
if flynnrc == "" {
c := cluster.New(args.BootConfig, os.Stdout)
dockerfs := args.DockerFS
if dockerfs == "" {
var err error
if dockerfs, err = c.BuildFlynn("", util.Repos); err != nil {
log.Fatal("could not build flynn:", err)
}
if !args.KeepDockerFS {
defer os.RemoveAll(dockerfs)
}
}
if err := c.Boot(dockerfs, 1); err != nil {
log.Fatal("could not boot cluster: ", err)
}
if args.Kill {
defer c.Shutdown()
}
if err := createFlynnrc(c); err != nil {
log.Fatal(err)
}
defer os.RemoveAll(flynnrc)
}
ssh, err := genSSHKey()
if err != nil {
log.Fatal(err)
}
defer ssh.Cleanup()
gitEnv = ssh.Env
keyAdd := flynn("", "key-add", ssh.Pub)
if keyAdd.Err != nil {
log.Fatalf("Error during `%s`:\n%s%s", strings.Join(keyAdd.Cmd, " "), keyAdd.Output, keyAdd.Err)
}
res := check.RunAll(&check.RunConf{
Stream: true,
Verbose: true,
KeepWorkDir: args.Debug,
})
fmt.Println(res)
}
type sshData struct {
Key string
Pub string
Env []string
Cleanup func()
}
func genSSHKey() (*sshData, error) {
keyFile, err := ioutil.TempFile("", "")
if err != nil {
return nil, err
}
defer keyFile.Close()
rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, err
}
pem.Encode(keyFile, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(rsaKey),
})
pubFile, err := ioutil.TempFile("", "")
if err != nil {
return nil, err
}
defer pubFile.Close()
rsaPubKey, err := ssh.NewPublicKey(&rsaKey.PublicKey)
if err != nil {
return nil, err
}
if _, err := pubFile.Write(ssh.MarshalAuthorizedKey(rsaPubKey)); err != nil {
return nil, err
}
wrapperFile, err := ioutil.TempFile("", "")
if err != nil {
return nil, err
}
defer wrapperFile.Close()
if err := sshWrapper.Execute(wrapperFile, map[string]string{"SSHKey": keyFile.Name()}); err != nil {
return nil, err
}
if err := wrapperFile.Chmod(0700); err != nil {
return nil, err
}
return &sshData{
Key: keyFile.Name(),
Pub: pubFile.Name(),
Env: []string{"GIT_SSH=" + wrapperFile.Name()},
Cleanup: func() {
os.RemoveAll(keyFile.Name())
os.RemoveAll(pubFile.Name())
os.RemoveAll(wrapperFile.Name())
},
}, nil
}
func createFlynnrc(c *cluster.Cluster) error {
tmpfile, err := ioutil.TempFile("", "flynnrc-")
if err != nil {
return err
}
flynnrc = tmpfile.Name()
githost := fmt.Sprintf("%s:2222", c.ControllerDomain)
url := fmt.Sprintf("https://%s:443", c.ControllerDomain)
return flynn("", "server-add", "-g", githost, "-p", c.ControllerPin, "default", url, c.ControllerKey).Err
}
type CmdResult struct {
Cmd []string
Output string
Err error
}
func flynn(dir string, cmdArgs ...string) *CmdResult {
cmd := exec.Command(args.CLI, cmdArgs...)
cmd.Env = append(os.Environ(), "FLYNNRC="+flynnrc)
cmd.Dir = dir
return run(cmd)
}
func git(dir string, args ...string) *CmdResult {
cmd := exec.Command("git", args...)
cmd.Env = append(os.Environ(), gitEnv...)
cmd.Dir = dir
return run(cmd)
}
func run(cmd *exec.Cmd) *CmdResult {
var out bytes.Buffer
if args.Debug {
fmt.Println("++", cmd.Path, strings.Join(cmd.Args[1:], " "))
cmd.Stdout = io.MultiWriter(os.Stdout, &out)
cmd.Stderr = io.MultiWriter(os.Stderr, &out)
} else {
cmd.Stdout = &out
cmd.Stderr = &out
}
err := cmd.Run()
res := &CmdResult{
Cmd: cmd.Args,
Err: err,
Output: out.String(),
}
return res
}
var Outputs check.Checker = outputChecker{
&check.CheckerInfo{
Name: "Outputs",
Params: []string{"result", "output"},
},
}
type outputChecker struct {
*check.CheckerInfo
}
func (outputChecker) Check(params []interface{}, names []string) (bool, string) {
ok, msg, s, res := checkCmdResult(params, names)
if !ok {
return ok, msg
}
return s == res.Output, ""
}
func checkCmdResult(params []interface{}, names []string) (ok bool, msg, s string, res *CmdResult) {
res, ok = params[0].(*CmdResult)
if !ok {
msg = "result must be a *CmdResult"
return
}
switch v := params[1].(type) {
case []byte:
s = string(v)
case string:
s = v
default:
msg = "output must be a []byte or string"
return
}
if res.Err != nil {
return false, "", "", nil
}
ok = true
return
}
var OutputContains check.Checker = outputContainsChecker{
&check.CheckerInfo{
Name: "OutputContains",
Params: []string{"result", "contains"},
},
}
type outputContainsChecker struct {
*check.CheckerInfo
}
func (outputContainsChecker) Check(params []interface{}, names []string) (bool, string) {
ok, msg, s, res := checkCmdResult(params, names)
if !ok {
return ok, msg
}
return strings.Contains(res.Output, s), ""
}
var Succeeds check.Checker = succeedsChecker{
&check.CheckerInfo{
Name: "Succeeds",
Params: []string{"result"},
},
}
type succeedsChecker struct {
*check.CheckerInfo
}
func (succeedsChecker) Check(params []interface{}, names []string) (bool, string) {
res, ok := params[0].(*CmdResult)
if !ok {
return false, "result must be a *CmdResult"
}
return res.Err == nil, ""
}