-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdockerhub-webhook-server.go
150 lines (123 loc) · 2.51 KB
/
dockerhub-webhook-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
package main
import (
"bufio"
"log"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"sync"
"time"
)
type Queue struct {
items []*int
size int
mutex *sync.Mutex
}
func (q *Queue) Push(item *int) {
q.mutex.Lock()
defer q.mutex.Unlock()
q.items = append(q.items, item)
q.size++
}
func (q *Queue) Shift() *int {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.size == 0 {
return nil
}
top := q.items[0]
q.items[0] = nil
q.items = q.items[1:]
q.size--
return top
}
func (q *Queue) Size() int {
q.mutex.Lock()
defer q.mutex.Unlock()
return q.size
}
var q = &Queue{
items: make([]*int, 0),
size: 0,
mutex: &sync.Mutex{},
}
var jobs = 0
func handler(w http.ResponseWriter, r *http.Request) {
// Remove preceding slash from the URL and compare the rest against
// AUTH_TOKEN value, if WEBHOOK_AUTH_TOKEN variable was not set Getenv
// returns an empty string
if r.URL.Path[1:] == os.Getenv("WEBHOOK_AUTH_TOKEN") {
w.WriteHeader(http.StatusOK)
// backlog size for the queue is 0; that means only one job can be
// waiting in the queue
if q.Size() == 0 {
jobs++
q.Push(&jobs)
}
} else {
w.WriteHeader(http.StatusNotFound)
}
}
func execAndStreamOutput(cmdName string, cmdArgs []string) {
cmd := exec.Command(cmdName, cmdArgs...)
cmdReader, err := cmd.StdoutPipe()
if err != nil {
log.Fatal("Error creating StdoutPipe for Cmd", err)
}
scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
log.Println(scanner.Text())
}
}()
err = cmd.Start()
if err != nil {
log.Fatal("Error starting Cmd", err)
}
err = cmd.Wait()
if err != nil {
log.Fatal("Error waiting for Cmd", err)
}
}
func setup() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
log.Println(dir + " <we are here")
return path.Join(dir, "setup-helper.sh")
}
func processQueue() {
for {
if q.Size() == 0 {
continue
}
job := *q.Shift()
log.Printf("Processing job #%d\n", job)
execAndStreamOutput("/bin/sh", []string{
setup(),
})
log.Printf("Finished Processing job #%d\n", job)
time.Sleep(3 * time.Second)
}
}
func envError(name string) {
log.Fatal(name + " environment variable must be set.")
}
func main() {
// check environment variables
requiredEnvVars := []string{
"WEBHOOK_AUTH_TOKEN",
}
for _, item := range requiredEnvVars {
if os.Getenv(item) == "" {
envError(item)
}
}
go processQueue()
http.HandleFunc("/", handler)
http.ListenAndServe(":8008", nil)
log.Printf("Listening on port: 8080......")
}