This repository has been archived by the owner on Jul 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdocker_hub.go
105 lines (87 loc) · 2.63 KB
/
docker_hub.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
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"time"
)
func (deployer *Deployer) RegisterDockerHubWebhook(path string) {
http.HandleFunc(path, deployer.DockerHubWebhookHandler)
go deployer.webhookWatchWorker()
}
type DockerHubWebhook struct {
CallbackURL string `json:"callback_url"`
Repository struct {
RepoName string `json:"repo_name"`
RepoURL string `json:"repo_url"`
} `json:"repository"`
}
type DockerHubCallback struct {
State string `json:"state"`
Description string `json:"description"`
Context string `json:"context"`
TargetURL string `json:"target_url"`
}
func (deployer *Deployer) DockerHubWebhookHandler(rw http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(req.Body)
var webhook DockerHubWebhook
if err := decoder.Decode(&webhook); err != nil {
return
}
repo := webhook.Repository.RepoName
repoURL := webhook.Repository.RepoURL
log.Println("Webhook received for", repo, repoURL)
now := time.Now().String()
callback := DockerHubCallback{
State: "error",
Description: "An unknown error occurred.",
Context: "etcd=" + *etcdNodes + " time=" + now,
TargetURL: "http://" + req.Host + "/",
}
if repo == "" {
callback.Description = "Error: webhook JSON repository.repo_name is empty."
} else {
key := deployer.etcdPrefix + "/" + repo
if _, err := deployer.etcd.Set(key, now, 0); err == nil {
callback.State = "success"
callback.Description = "Triggered etcd " + key
} else {
log.Println("Webhook couldn't etcd.Set", key, err)
callback.Description = "Error: etcd couldn't set " + key + ": " + err.Error()
}
}
deployer.DockerHubDoCallback(webhook.CallbackURL, callback)
}
func (deployer *Deployer) DockerHubDoCallback(callbackURL string, callback DockerHubCallback) {
if callbackURL == "" {
log.Println("webhook.CallbackURL not specified")
return
}
jsonCallback, err := json.Marshal(callback)
if err != nil {
log.Println("Webhook couldn't json.Marshal", err)
return
}
resp, err := http.Post(callbackURL, "application/json", bytes.NewBuffer(jsonCallback))
if err != nil {
log.Println("Webhook callback POST error:", err)
return
}
// Discard body
if err := resp.Body.Close(); err != nil {
log.Println("Webhook callback body.Close() error:", err)
return
}
if resp.StatusCode != 200 {
log.Println("Webhook callback POST status:", resp.StatusCode)
}
}
func (deployer *Deployer) webhookWatchWorker() {
watch := NewWatch(deployer.etcd, deployer.etcdPrefix, 100)
for node := range watch.C {
log.Println("Etcd watch received for", node.Key)
deployer.repoUpdate <- node.Key
}
log.Fatalln("Etcd watcher died.")
}