-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
299 lines (274 loc) · 8.39 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
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
/* Copyright (C) 2021 TR_SLimey - All Rights Reserved
* You may use, distribute and modify this code under the
* terms of the Apache 2.0 license, which can be found in
* the LICENSE file.
*/
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
cmdline "github.com/galdor/go-cmdline"
ini "github.com/vaughan0/go-ini"
)
const VERSION = "1.0.2"
var (
config ini.File
exitChan chan struct{}
)
type Repo struct {
Id int64
Uid string
User_id int64
Namespace string
Name string
Slug string
Scm string
Git_http_url string
Git_ssh_url string
Link string
Default_branch string
Private bool
Visibility string
Active bool
Config string
Trusted bool
Protected bool
Ignore_forks bool
Ignore_pulls bool
Cancel_pulls bool
Timeout int64
Counter int64
Synced int64
Created int64
Updated int64
Version int64
}
type Build struct {
Id int64
Repo_id int64
Number int64
Parent int64
Status string
Error string
Event string
Action string
Link string
Timestamp int64
Title string
Message string
Before string
After string
Ref string
Source_repo string
Source string
Target string
Author_login string
Author_name string
Author_email string
Author_avatar string
Sender string
Params map[string]string
Cron string
Deploy_to string
Deploy_id int64
Started int64
Finished int64
Created int64
Updated int64
Version int64
}
type droneRequest struct {
Repo Repo
Build Build
}
func reqHandler(w http.ResponseWriter, r *http.Request) {
// Keep track of status code and log message
respStatusCode := http.StatusNoContent
respMsg := ""
// Log request when done processing
defer func() {
log.Printf("[%d] HTTP %s %s from %s (%s)", respStatusCode, r.Method, r.RequestURI, r.RemoteAddr, respMsg)
}()
// Only accept POST requests
if r.Method != http.MethodPost {
// Unless...
if r.Method == "BREW" {
respMsg = "I am a teapot"
respStatusCode = http.StatusTeapot
w.WriteHeader(respStatusCode)
return
}
respMsg = fmt.Sprintf("rejected request due to invalid method: %s", r.Method)
respStatusCode = http.StatusMethodNotAllowed
w.WriteHeader(respStatusCode)
return
}
// Read request body
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
respMsg = fmt.Sprintf("error while reading body of request: %s", err.Error())
respStatusCode = http.StatusInternalServerError
w.WriteHeader(respStatusCode)
return
}
// Try to unmarshal request body
data := droneRequest{}
err = json.Unmarshal(reqBody, &data)
if err != nil {
respMsg = fmt.Sprintf("error while unmarshalling request data: %s", err.Error())
respStatusCode = http.StatusBadRequest
w.WriteHeader(respStatusCode)
return
}
// Get the full name of the repo and check against our map
repoName := data.Repo.Slug
if configLocation, exists := config["config-map"][repoName]; exists {
// Parse the URL pointing at the config
configLocationParsed, err := url.Parse(configLocation)
if err != nil {
// URL is invalid so fallback on standard config but log the error
respMsg = fmt.Sprintf("unable parse URL for repo %s (%s) due to error (%s) so falling back to config in repo", repoName, configLocation, err.Error())
respStatusCode = http.StatusNoContent
w.WriteHeader(respStatusCode)
return
}
// Attempt to fetch the config from its location
var response []byte
if configLocationParsed.Scheme == "http" || configLocationParsed.Scheme == "https" {
result, err := http.Get(configLocation)
if err != nil {
// We couldn't fetch the config so fall back to file in repo
respMsg = fmt.Sprintf("unable to retrieve file at URL for repo %s (%s) due to error (%s) so falling back to config in repo", repoName, configLocation, err.Error())
respStatusCode = http.StatusNoContent
w.WriteHeader(respStatusCode)
return
}
body, err := ioutil.ReadAll(result.Body)
result.Body.Close()
if err != nil {
// We couldn't fetch the config so fall back to file in repo
respMsg = fmt.Sprintf("unable to retrieve file at URL for repo %s (%s) due to error (%s) so falling back to config in repo", repoName, configLocation, err.Error())
respStatusCode = http.StatusNoContent
w.WriteHeader(respStatusCode)
return
}
response = body
} else if configLocationParsed.Scheme == "file" {
data, err := ioutil.ReadFile(configLocationParsed.Path)
if err != nil {
// We couldn't fetch the config so fall back to file in repo
respMsg = fmt.Sprintf("unable to retrieve file at URL for repo %s (%s) due to error (%s) so falling back to config in repo", repoName, configLocation, err.Error())
respStatusCode = http.StatusNoContent
w.WriteHeader(respStatusCode)
return
}
response = data
} else {
// The scheme is unsupported so fallback
respMsg = fmt.Sprintf("the URL scheme for repo %s (%s) in unsupported so falling back to config in repo", repoName, configLocationParsed.Scheme)
respStatusCode = http.StatusNoContent
w.WriteHeader(respStatusCode)
return
}
// Wrap the config in JSON
responseDict := map[string]string{"data": string(response)}
finaldata, err := json.Marshal(responseDict)
if err != nil {
respMsg = fmt.Sprintf("failed to wrap config in JSON due to error (%s)", err.Error())
respStatusCode = http.StatusInternalServerError
w.WriteHeader(respStatusCode)
return
}
// Return the resulting config to requester
respStatusCode = http.StatusOK
w.WriteHeader(respStatusCode)
_, err = w.Write(finaldata)
if err != nil {
respMsg = fmt.Sprintf("failed to send config back to requester due to error (%s)", err.Error())
} else {
respMsg = fmt.Sprintf("successfully sent config for repo %s (%s)", repoName, configLocation)
}
} else {
// Otherwise, return a 204 to fallback to a local config for the repo
respMsg = fmt.Sprintf("no matching config found for repo %s", repoName)
respStatusCode = http.StatusNoContent
w.WriteHeader(respStatusCode)
return
}
}
func main() {
// Handle command line options/arguments (-h/--help is implicit)
// TODO: Allow overriding config within commandline
// TODO: --version
cl := cmdline.New()
cl.AddOption("c", "conf", "file", "path to the config file")
cl.SetOptionDefault("conf", "/etc/DroneExternalConfig/config.ini")
cl.Parse(os.Args)
// Attempt to parse config file
configPath := cl.OptionValue("conf")
configFile, err := ini.LoadFile(configPath)
if err != nil {
log.Fatalf("I couldn't parse the config file because of an error: %s", err.Error())
}
config = configFile
// Check and get the important sections of the config file
serverConfig, serverConfigExists := config["server"]
if !serverConfigExists {
log.Fatalf("The config file seems to be missing the [server] section. Please add it and re-start DroneExternalConfig.")
}
_, mappingsConfigExists := config["config-map"]
if !mappingsConfigExists {
log.Fatalf("The config file seems to be missing the [config-map] section. Please add it and re-start DroneExternalConfig.")
}
// Log startup info
log.Printf("Starting DroneExternalConfig version %s", VERSION)
// Prepare server variables
serverLAddr := "0.0.0.0"
serverLPort := "80"
serverTLSCert := ""
serverTLSKey := ""
if setting, exists := serverConfig["listen-addr"]; exists {
serverLAddr = setting
}
if setting, exists := serverConfig["listen-port"]; exists {
serverLPort = setting
}
if settingTLSCert, exists := serverConfig["tls-cert"]; exists {
if settingTLSKey, exists := serverConfig["tls-key"]; exists {
serverTLSCert, serverTLSKey = settingTLSCert, settingTLSKey
}
}
// Set up and start the server
http.HandleFunc("/", reqHandler)
log.Printf("Starting HTTP listener on %s:%s", serverLAddr, serverLPort)
var serverErr error
if serverTLSCert == "" {
serverErr = http.ListenAndServe(
fmt.Sprintf("%s:%s",
serverLAddr,
serverLPort,
),
nil,
)
} else {
serverErr = http.ListenAndServeTLS(
fmt.Sprintf("%s:%s",
serverLAddr,
serverLPort,
),
serverTLSCert,
serverTLSKey,
nil,
)
}
if serverErr != nil {
log.Fatalf("The server failed to start because of the following error: %s", serverErr.Error())
} else {
<-exitChan
}
}