forked from docker-archive/docker-bb
/
main.go
206 lines (177 loc) · 4.93 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
"path"
"syscall"
"time"
"github.com/Sirupsen/logrus"
"github.com/bitly/go-nsq"
"github.com/crowdmob/goamz/aws"
"github.com/crowdmob/goamz/s3"
"github.com/drone/go-github/github"
)
const (
// VERSION is the binary version.
VERSION = "v0.1.0"
)
var (
lookupd string
topic string
channel string
bucket string
region string
debug bool
version bool
)
func init() {
// parse flags
flag.BoolVar(&version, "version", false, "print version and exit")
flag.BoolVar(&version, "v", false, "print version and exit (shorthand)")
flag.BoolVar(&debug, "d", false, "run in debug mode")
flag.StringVar(&lookupd, "lookupd-addr", "nsqlookupd:4161", "nsq lookupd address")
flag.StringVar(&topic, "topic", "hooks-docker", "nsq topic")
flag.StringVar(&channel, "channel", "binaries", "nsq channel")
flag.StringVar(&bucket, "s3bucket", "s3://master.dockerproject.com/", "s3 bucket to push binaries")
flag.StringVar(®ion, "s3region", "us-east-1", "s3 region where bucket lives")
flag.Parse()
}
func main() {
// set log level
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
if version {
fmt.Println(VERSION)
return
}
bb := &Handler{}
if err := ProcessQueue(bb, QueueOptsFromContext(topic, channel, lookupd)); err != nil {
logrus.Fatal(err)
}
}
// Handler is the message processing interface for the consumer to nsq.
type Handler struct{}
// QueueOpts are the options for the nsq queue.
type QueueOpts struct {
LookupdAddr string
Topic string
Channel string
Concurrent int
Signals []os.Signal
}
// QueueOptsFromContext returns a QueueOpts object from the given settings.
func QueueOptsFromContext(topic, channel, lookupd string) QueueOpts {
return QueueOpts{
Signals: []os.Signal{syscall.SIGTERM, syscall.SIGINT},
LookupdAddr: lookupd,
Topic: topic,
Channel: channel,
Concurrent: 1,
}
}
// ProcessQueue sets up the handler to process the nsq queue with the given options.
func ProcessQueue(handler nsq.Handler, opts QueueOpts) error {
if opts.Concurrent == 0 {
opts.Concurrent = 1
}
s := make(chan os.Signal, 64)
signal.Notify(s, opts.Signals...)
consumer, err := nsq.NewConsumer(opts.Topic, opts.Channel, nsq.NewConfig())
if err != nil {
return err
}
consumer.AddConcurrentHandlers(handler, opts.Concurrent)
if err := consumer.ConnectToNSQLookupd(opts.LookupdAddr); err != nil {
return err
}
for {
select {
case <-consumer.StopChan:
return nil
case sig := <-s:
logrus.WithField("signal", sig).Debug("received signal")
consumer.Stop()
}
}
}
// HandleMessage reads the nsq message body and parses it as a github webhook,
// checks out the source for the repository & builds/uploads the binaries.
func (h *Handler) HandleMessage(m *nsq.Message) error {
hook, err := github.ParseHook(m.Body)
if err != nil {
// Errors will most likely occur because not all GH
// hooks are the same format
// we care about those that are pushes to master
logrus.Debugf("Error parsing hook: %v", err)
return nil
}
shortSha := hook.After[0:7]
// checkout the code in a temp dir
temp, err := ioutil.TempDir("", fmt.Sprintf("commit-%s", shortSha))
if err != nil {
return err
}
defer os.RemoveAll(temp)
if err := checkout(temp, hook.Repo.Url, hook.After); err != nil {
logrus.Warn(err)
return err
}
logrus.Debugf("Checked out %s for %s", hook.After, hook.Repo.Url)
var (
image = fmt.Sprintf("docker:commit-%s", shortSha)
container = fmt.Sprintf("build-%s", shortSha)
)
logrus.Infof("image=%s container=%s\n", image, container)
// build the image
if err := build(temp, image); err != nil {
logrus.Warn(err)
return err
}
logrus.Debugf("Successfully built image %s", image)
// make the binary
defer removeContainer(container)
if err = makeBinary(temp, image, container, 20*time.Minute); err != nil {
logrus.Warn(err)
return err
}
logrus.Debugf("Successfully built binaries for %s", hook.After)
// read the version
version, err := getBinaryVersion(temp)
if err != nil {
logrus.Warnf("Getting binary version failed: %v", err)
return err
}
bundlesPath := path.Join(temp, "bundles", version, "cross")
// create commit file
if err := ioutil.WriteFile(path.Join(bundlesPath, "commit"), []byte(hook.After), 0755); err != nil {
return err
}
// create version file
if err := ioutil.WriteFile(path.Join(bundlesPath, "version"), []byte(version), 0755); err != nil {
return err
}
// use env variables to connect to s3
auth, err := aws.EnvAuth()
if err != nil {
return fmt.Errorf("AWS Auth failed: %v", err)
}
// connect to s3 bucket
s := s3.New(auth, aws.GetRegion(region))
bucketname, bucketpath := bucketParts(bucket)
bucket := s.Bucket(bucketname)
// push to s3
if err = pushToS3(bucket, bucketpath, bundlesPath); err != nil {
logrus.Warn(err)
return err
}
// add html to template
if err := createIndexFile(bucket, bucketpath); err != nil {
logrus.Warn(err)
return err
}
return nil
}