-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
327 lines (262 loc) · 7.89 KB
/
webhook.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
// SPDX-FileCopyrightText: 2023 Isaak Tsalicoglou <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
var mu sync.Mutex
var wg sync.WaitGroup
var firstRun bool
var isAlreadyRunning bool
func expandTilde(path string) (string, error) {
if path[:2] == "~/" {
usr, err := user.Current()
if err != nil {
return "", err
}
path = filepath.Join(usr.HomeDir, path[2:])
}
return path, nil
}
func genTimestamp() string {
now := time.Now()
return now.Format(time.RFC3339) + "\t"
}
func getGitRepoPath() (string, error) {
gitRepoPath := os.Getenv("GIT_REPO_PATH")
return expandTilde(gitRepoPath)
}
func authenticate(w http.ResponseWriter, r *http.Request) error {
apiKeyHeader := r.Header.Get("X-Gitlab-Token")
fmt.Println(genTimestamp() + "🤝 Received a valid secret token from Gitlab")
apiKey := os.Getenv("WEBHOOK_SECRET_TOKEN")
if apiKeyHeader != apiKey {
http.Error(w, genTimestamp()+"💩 Invalid secret token", http.StatusForbidden)
return fmt.Errorf(genTimestamp() + "🚫 Invalid secret token")
}
return nil
}
func gitPull(gitRepoPath string) (bool, error) {
fmt.Println(genTimestamp() + "📡 Performing 'git pull'…")
cmdGitPull := exec.Command("git", "pull")
cmdGitPull.Dir = gitRepoPath
stdout, err := cmdGitPull.StdoutPipe()
if err != nil {
return false, fmt.Errorf(genTimestamp()+"💩 Error creating StdoutPipe for 'git pull': %v", err)
}
err = cmdGitPull.Start()
if err != nil {
return false, fmt.Errorf(genTimestamp()+"💩 Error starting 'git pull': %v", err)
}
outputBytes, err := io.ReadAll(stdout)
if err != nil {
return false, fmt.Errorf(genTimestamp()+"💩 Error reading 'git pull' output: %v", err)
}
err = cmdGitPull.Wait()
if err != nil {
return false, fmt.Errorf(genTimestamp()+"💩 Error waiting for 'git pull': %v", err)
}
output := string(outputBytes)
if strings.Contains(output, "Already up to date.") {
fmt.Println(genTimestamp() + "🤷 Already up to date; no further actions required")
return false, nil
}
return true, nil
}
func npmStart(gitRepoPath string) error {
cmd := exec.Command("npm", "start")
cmd.Dir = gitRepoPath
wg.Add(1)
go func() {
defer wg.Done()
err := cmd.Run()
if err != nil {
fmt.Println(genTimestamp()+"💩 Error running 'npm start':", err)
} else {
fmt.Println(genTimestamp() + "🚀 'npm start' is running")
}
}()
return nil
}
func webhookHandler(w http.ResponseWriter, r *http.Request) {
err := authenticate(w, r)
if err != nil {
return
}
fmt.Fprintln(w, genTimestamp()+" Webhook request received")
go func() {
fmt.Println(genTimestamp() + "⚠️ 'git push' detected")
err := updatePipeline()
if err != nil {
fmt.Println(genTimestamp()+"💩 Error during update pipeline:", err)
return
}
}()
}
func killNpmStartIfRunning() error {
port := getNpmPort()
fmt.Println(genTimestamp() + "💣 Killing 'npm start'…")
killProcessOnPort(port)
isAlreadyRunning = false
return nil
}
func getProcessIDOnPort(port string) (int, error) {
// Extract PID of the running NextJS app from the output of netstat
cmd := exec.Command("sh", "-c", fmt.Sprintf("netstat -tlpn | grep ':%s' | sed -E 's/^.* ([^\\/]+)\\/.*/\\1/'", port))
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Printf(genTimestamp()+"💩 Error creating StdoutPipe: %v\n", err)
return 0, err
}
if err := cmd.Start(); err != nil {
fmt.Printf(genTimestamp()+"💩 Error starting command: %v\n", err)
return 0, err
}
output, err := io.ReadAll(stdout)
if err != nil {
fmt.Printf(genTimestamp()+"💩 Error reading output: %v\n", err)
return 0, err
}
if err := cmd.Wait(); err != nil {
fmt.Printf(genTimestamp()+"💩 Error waiting for command: %v\n", err)
return 0, err
}
if strings.TrimSpace(string(output)) == "" {
fmt.Printf(genTimestamp()+"🤷 No process found on port %s\n", port)
return 0, nil
}
var pid int
pid, err = strconv.Atoi(strings.TrimSpace(string(output)))
if err != nil {
fmt.Printf(genTimestamp()+"💩 Error parsing PID: %v\n", err)
return 0, err
}
fmt.Println(genTimestamp() + "✅ Found PID of process running on port " + port + ": " + strconv.Itoa(pid))
return pid, nil
}
func killProcessOnPort(port string) error {
pid, err := getProcessIDOnPort(port)
if err != nil {
return err
}
if pid != 0 {
fmt.Println(genTimestamp()+"💣 Killing process with PID", pid)
return exec.Command("kill", fmt.Sprintf("%d", pid)).Run()
}
fmt.Printf(genTimestamp()+"🤷 No process found on port %s\n", port)
return nil
}
func runNpmInstall(gitRepoPath string) error {
fmt.Println(genTimestamp() + "🛠️ Running 'npm install'…")
cmd := exec.Command("npm", "install")
cmd.Dir = gitRepoPath
err := cmd.Run()
if err != nil {
return fmt.Errorf(genTimestamp()+"💩 Error running 'npm install': %v", err)
} else {
fmt.Println(genTimestamp() + "✅ 'npm install' completed")
}
return nil
}
func runNpmBuild(gitRepoPath string) error {
fmt.Println(genTimestamp() + "🏗️ Running 'npm run build'…")
cmd := exec.Command("npm", "run", "build")
cmd.Dir = gitRepoPath
err := cmd.Run()
if err != nil {
return fmt.Errorf(genTimestamp()+"💩 Error running 'npm run build': %v", err)
} else {
fmt.Println(genTimestamp() + "✅ 'npm run build' completed")
}
return nil
}
func updatePipeline() error {
mu.Lock()
defer mu.Unlock()
gitRepoPath, err := getGitRepoPath()
if err != nil {
return fmt.Errorf(genTimestamp()+"💩 Error getting Git repository path: %v", err)
}
thereAreGitChanges, err := gitPull(gitRepoPath)
if err != nil {
return fmt.Errorf(genTimestamp()+"💩 Error during 'git pull': %v", err)
}
fmt.Println(genTimestamp() + "✅ 'git pull' completed")
buildRequired := thereAreGitChanges || (firstRun && !isAlreadyRunning)
if buildRequired {
fmt.Println(genTimestamp() + "🔃 Rebuilding required")
err = killNpmStartIfRunning()
if err != nil {
return fmt.Errorf(genTimestamp()+"💩 Error killing already-running 'npm start': %v", err)
}
err = runNpmInstall(gitRepoPath)
if err != nil {
return fmt.Errorf(genTimestamp()+"💩 Error running 'npm install': %v", err)
}
err = runNpmBuild(gitRepoPath)
if err != nil {
return fmt.Errorf(genTimestamp()+"💩 Error running 'npm run build': %v", err)
}
firstRun = false
} else {
fmt.Println(genTimestamp() + "😴 No changes on git since the last 'npm run build', thus skipping update")
return nil
}
err = npmStart(gitRepoPath)
if err != nil {
return fmt.Errorf(genTimestamp()+"💩 Error in 'npm start': %v", err)
}
fmt.Println(genTimestamp() + "🥳 Update completed and 'npm start' issued")
return nil
}
func getNpmPort() string {
port := os.Getenv("NEXTJS_PORT")
if port == "" {
port = "3000"
}
return port
}
func getWebhookPort() string {
port := os.Getenv("WEBHOOK_PORT")
if port == "" {
port = "8000"
}
return port
}
func main() {
firstRun = true
fmt.Println(genTimestamp() + "🛋️ Performing initial setup…")
npmPort := getNpmPort()
fmt.Println(genTimestamp()+"🔎 Checking whether 'npm start' is already running on port", npmPort)
pid, _ := getProcessIDOnPort(npmPort)
isAlreadyRunning = (pid != 0)
if !isAlreadyRunning {
fmt.Println(genTimestamp() + "🤔 The app was not running already; time to update and start it")
err := updatePipeline()
if err != nil {
fmt.Println(genTimestamp()+"💩 Error during initial setup:", err)
return
}
} else {
fmt.Println(genTimestamp() + "🤷 The app was running already; will not update")
}
webhookPort := getWebhookPort()
fmt.Println(genTimestamp()+"🚀 Starting the webhook server on port", webhookPort)
http.HandleFunc("/webhook", webhookHandler)
err := http.ListenAndServe(":"+webhookPort, nil)
if err != nil {
fmt.Println(genTimestamp()+"💩 Error starting the webhook server:", err)
} else {
fmt.Println(genTimestamp() + "📟 Webhook server started")
}
}