/
server.go
396 lines (330 loc) · 7.84 KB
/
server.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"sync"
"github.com/google/go-github/github"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var dbSession *mgo.Session
var users *mgo.Collection
var repos *mgo.Collection
var specialBranch string
var specialRef string
var handlers map[int]*comm
var handlersMtx *sync.RWMutex
var secretKey []byte
// Repo is a github repo
type Repo struct {
ID string `bson:"_id,omitempty"`
Username string
EncAccessToken []byte
LastBuildOutput string
LastBuildStatus string
}
// Comm is used to communicate to a worker goroutine
// stop is sent 'true' to ask the goroutine to stop
// stopped is sent 'true' when it stops
type comm struct {
Stop chan bool
Stopped chan bool
}
func main() {
config := loadConfig()
config.print()
var err error
fmt.Println(config.SecretKey)
secretKey, err = hex.DecodeString(config.SecretKey)
fmt.Println(secretKey)
specialBranch = config.SpecialBranch
specialRef = "refs/heads/" + config.SpecialBranch
dbSession, err = mgo.Dial(config.MongoURL)
if err != nil {
panic(err)
}
defer dbSession.Close()
users = dbSession.DB("hugo-pages").C("users")
repos = dbSession.DB("hugo-pages").C("repos")
handlers = make(map[int]*comm)
handlersMtx = &sync.RWMutex{}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
payload, err := validate(r)
if len(err) > 0 {
log.Println(err)
http.Error(w, err, http.StatusBadRequest)
return
}
if payload == nil {
return
}
fullname := *payload.Repo.FullName
id := *payload.Repo.ID
url := *payload.Repo.URL
go func() {
log.Printf("Got: %s", fullname)
workerComm := synchronize(id)
w := newWorker(id, url, workerComm)
w.work()
change := bson.M{
"$set": bson.M{
"lastbuildoutput": w.log,
"lastbuildstatus": w.status,
},
}
repos.UpdateId(id, change)
}()
})
log.Println("Listening")
log.Fatal(http.ListenAndServe(config.Address, nil))
}
// synchronize is a step to see if another goroutine is handling a request
// from the same repo
func synchronize(id int) *comm {
handlersMtx.RLock()
currentComm := handlers[id]
handlersMtx.RUnlock()
if currentComm == nil {
// There is no goroutine handling a request for this repo
handlersMtx.Lock()
currentComm = handlers[id]
if currentComm == nil {
// Still there is no goroutine handling a request for this repo
currentComm = &comm{
Stop: make(chan bool, 3), // Can buffer stop signals upto 3
Stopped: make(chan bool, 1),
}
handlers[id] = currentComm
handlersMtx.Unlock()
} else {
// Some goroutine has entered while we were creating a new channel
handlersMtx.Unlock()
_waitAndStart(currentComm, id)
}
} else {
_waitAndStart(currentComm, id)
}
return currentComm
}
func _waitAndStart(currentComm *comm, id int) {
currentComm.Stop <- true
val := <-currentComm.Stopped
if val == true {
// This means previous goroutine does not know it was interrupted
// So clear the signal it was sent
<-currentComm.Stop
currentComm.Stopped <- true
//and resynchronize
synchronize(id)
}
}
type worker struct {
stop chan bool
stopped chan bool
id int
url string
path string
log string
status string
}
func newWorker(id int, url string, workerComm *comm) *worker {
return &worker{
stop: workerComm.Stop,
stopped: workerComm.Stopped,
id: id,
path: fmt.Sprintf("/tmp/%d", id),
url: url,
log: "",
status: "incomplete",
}
}
func (w *worker) work() {
if !w.checkAndContinue("Starting build") {
return
}
repo := getRepo(w.id)
if repo == nil {
w.checkAndStop("Repo not known")
return
}
_, err := Clone(w.url, w.path, specialBranch)
if err != nil {
log.Println(err.Error())
w.checkAndStop("Could not clone")
return
}
if !w.checkAndContinue("building") {
return
}
out, err := HugoBuild(w.path)
if err != nil {
cleanUp(w.path)
w.checkAndStop("Could not build")
return
}
log.Printf(out)
if !w.checkAndContinue(out) {
return
}
pushBranch := getPushBranch(w.url)
log.Println("will push to:", pushBranch)
subrepo, err := Checkout(w.path+"/public/", pushBranch) // trailing / important
if err != nil {
cleanUp(w.path)
w.checkAndStop("Could not checkout: " + err.Error())
return
}
if !w.checkAndContinue("pushing") {
return
}
tokenString, err := decrypt(repo.EncAccessToken)
if err != nil {
cleanUp(w.path)
w.checkAndStop("Could not push")
log.Println(err.Error())
return
}
err = Push(formatPushURL(string(tokenString), repo.Username, w.url), pushBranch, subrepo)
if err != nil {
cleanUp(w.path)
w.checkAndStop("Could not push: " + err.Error())
log.Printf("Could not push: %s", err)
return
}
cleanUp(w.path)
w.checkAndStop("Finishing up")
w.status = "complete"
}
func (w *worker) checkAndContinue(msg string) bool {
select {
case <-w.stop:
log.Printf("Stopping: %s %s", msg, w.url)
w.log = w.log + "Stopping: " + msg + "\n"
cleanUp(w.path)
w.stopped <- false
return false
default:
log.Printf("Continuing %s", msg)
w.log = w.log + msg + "\n"
return true
}
}
func (w *worker) checkAndStop(msg string) {
select {
case <-w.stop:
log.Printf("Stopping: %s %s", msg, w.url)
w.log = w.log + "Stopping: " + msg + "\n"
cleanUp(w.path)
w.stopped <- false
default:
handlersMtx.Lock()
delete(handlers, w.id)
handlersMtx.Unlock()
log.Printf("Stopping (completed): %s", msg)
w.log = w.log + "Stopping (completed): " + msg + "\n"
cleanUp(w.path)
w.stopped <- true
}
}
func validate(r *http.Request) (*github.PushEvent, string) {
if r.Method != "POST" {
return nil, "Not POST"
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, "Can't read"
}
var t github.PushEvent
err = json.Unmarshal(b, &t)
if err != nil {
fmt.Println(err)
return nil, "Can't unmarshal"
}
if t.Ref == nil {
return nil, "No ref"
}
if *t.Ref != specialRef {
return nil, "Not hugo pages:" + *t.Ref
}
mac := hmac.New(sha1.New, []byte("hugopagessecret"))
mac.Write(b)
if err != nil {
return nil, err.Error()
}
expectedMAC := mac.Sum(nil)
signature := r.Header["X-Hub-Signature"][0]
expected := "sha1=" + hex.EncodeToString(expectedMAC)
if !hmac.Equal([]byte(signature), []byte(expected)) {
fmt.Println(expected)
return nil, "Unknown Origin"
}
return &t, ""
}
func formatPushURL(accessToken, username, url string) string {
// TODO: optimize string concat
parts := strings.Split(url, "://")
return parts[0] + "://" + username + ":" + accessToken + "@" + parts[1]
}
func getPushBranch(url string) string {
parts := strings.Split(url, "://")
parts = strings.Split(parts[1], "/")
if parts[2] == parts[1]+".github.io" {
return "master"
}
fmt.Println(parts)
return "gh-pages"
}
func getRepo(id int) *Repo {
result := &Repo{}
err := repos.FindId(id).One(result)
if err != nil {
log.Println(err)
return nil
}
return result
}
func cleanUp(path string) {
err := os.RemoveAll(path)
if err != nil {
log.Println(err)
}
}
func encrypt(text []byte) (ciphertext []byte, err error) {
var block cipher.Block
if block, err = aes.NewCipher(secretKey); err != nil {
return nil, err
}
ivSource := []byte("abcdef1234567890")
iv := ivSource[:aes.BlockSize] // const BlockSize = 16
cfb := cipher.NewCFBEncrypter(block, iv)
ciphertext = make([]byte, len(text))
cfb.XORKeyStream(ciphertext, text)
return
}
func decrypt(ciphertext []byte) (plaintext []byte, err error) {
fmt.Println(secretKey)
var block cipher.Block
if block, err = aes.NewCipher(secretKey); err != nil {
return
}
if len(ciphertext) < aes.BlockSize {
err = errors.New("ciphertext too short")
return
}
iv := []byte("abcdef1234567890")
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(ciphertext, ciphertext)
plaintext = ciphertext
return
}