-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontroller.go
More file actions
148 lines (123 loc) · 3.83 KB
/
controller.go
File metadata and controls
148 lines (123 loc) · 3.83 KB
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
package gitreceive
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/deis/builder/pkg"
)
var (
errControllerNotFound = errors.New("Deis controller not found. Is it running?")
errControllerServiceUnavailable = errors.New("Deis controller was unavailable. Is it healthy?")
)
type unexpectedControllerStatusCode struct {
endpoint string
expected int
actual int
}
func newUnexpectedControllerStatusCode(endpoint string, expectedCode, actualCode int) unexpectedControllerStatusCode {
return unexpectedControllerStatusCode{endpoint: endpoint, expected: expectedCode, actual: actualCode}
}
func (u unexpectedControllerStatusCode) Error() string {
return fmt.Sprintf("Deis controller endpoint %s: expected status code %d, got %d", u.endpoint, u.expected, u.actual)
}
func controllerURLStr(conf *Config, additionalPath ...string) string {
return fmt.Sprintf("http://%s:%s/%s", conf.WorkflowHost, conf.WorkflowPort, strings.Join(additionalPath, "/"))
}
func setReqHeaders(builderKey string, req *http.Request) {
req.Header.Add("Content-Type", contentType)
req.Header.Add("Accept", contentType)
req.Header.Add("User-Agent", userAgent)
req.Header.Add("X-Deis-Builder-Auth", builderKey)
}
func getAppConfig(conf *Config, builderKey, userName, appName string) (*pkg.Config, error) {
data, err := json.Marshal(&pkg.ConfigHook{
ReceiveUser: userName,
ReceiveRepo: appName,
})
if err != nil {
return nil, err
}
b := bytes.NewReader(data)
url := controllerURLStr(conf, "v2", "hooks", "config")
req, err := http.NewRequest("POST", url, b)
if err != nil {
return nil, err
}
setReqHeaders(builderKey, req)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode == 404 {
return nil, errControllerNotFound
} else if res.StatusCode != 200 {
return nil, newUnexpectedControllerStatusCode(url, 200, res.StatusCode)
}
ret := &pkg.Config{}
if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
return nil, err
}
return ret, nil
}
func publishRelease(conf *Config, builderKey string, buildHook *pkg.BuildHook) (*pkg.BuildHookResponse, error) {
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(buildHook); err != nil {
return nil, err
}
url := controllerURLStr(conf, "v2", "hooks", "build")
req, err := http.NewRequest("POST", url, &b)
if err != nil {
return nil, err
}
setReqHeaders(builderKey, req)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode == http.StatusNotFound {
return nil, errControllerNotFound
} else if res.StatusCode == http.StatusServiceUnavailable {
return nil, errControllerServiceUnavailable
} else if res.StatusCode != 200 {
return nil, newUnexpectedControllerStatusCode(url, 200, res.StatusCode)
}
ret := new(pkg.BuildHookResponse)
if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
return nil, err
}
return ret, nil
}
func receive(conf *Config, builderKey, gitSha string) error {
urlStr := controllerURLStr(conf, "v2", "hooks", "push")
bodyMap := map[string]string{
"receive_user": conf.Username,
"receive_repo": conf.App(),
"sha": gitSha,
"fingerprint": conf.Fingerprint,
"ssh_connection": conf.SSHConnection,
"ssh_original_command": conf.SSHOriginalCommand,
}
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(bodyMap); err != nil {
return err
}
req, err := http.NewRequest("POST", urlStr, &body)
if err != nil {
return err
}
setReqHeaders(builderKey, req)
// TODO: use ctxhttp here (https://godoc.org/golang.org/x/net/context/ctxhttp)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 201 {
return newUnexpectedControllerStatusCode(urlStr, 201, resp.StatusCode)
}
return nil
}