This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
forked from cgrates/ansihook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
89 lines (80 loc) · 2.72 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
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package main
import (
"bytes"
"flag"
"fmt"
"log"
"net/http"
"os/exec"
"github.com/google/go-github/v38/github"
)
var (
secret = flag.String("secret", "", "The secret for webhook")
pattern = flag.String("http_path", "/webhooks", "The webhook path")
address = flag.String("address", ":8080", "The addres the server is created")
ansibleScriptPath = flag.String("path", "./main.yaml", "The path to the ansible script")
ansibleInventory = flag.String("inventory", "./hosts", "The path to the ansible inventory")
ansiblePath string
)
func handleWebhook(w http.ResponseWriter, r *http.Request) {
payload, err := github.ValidatePayload(r, []byte(*secret))
if err != nil {
log.Printf("error validating request body: err=%s\n", err)
return
}
event, err := github.ParseWebHook(github.WebHookType(r), payload)
r.Body.Close()
if err != nil {
log.Printf("could not parse webhook: err=%s\n", err)
return
}
switch event.(type) {
case *github.PushEvent:
log.Println("Received a push event")
go executeAnsible(ansiblePath, *ansibleScriptPath)
default:
log.Printf("unknown event type %s\n", github.WebHookType(r))
return
}
}
func main() {
flag.Parse()
var err error
if ansiblePath, err = exec.LookPath("ansible-playbook"); err != nil {
log.Fatalf("Unable to find ansible-playbook: %s", err)
}
log.Println("server started at: ", *address+*pattern)
http.HandleFunc(*pattern, handleWebhook)
if err = http.ListenAndServe(*address, nil); err != nil {
log.Fatalf("Unable to start server: %s", err)
}
}
func executeAnsible(ansiblePath, scriptPath string) (err error) {
cmd := exec.Command(ansiblePath, scriptPath, "-i", *ansibleInventory)
fmt.Println("Running:", cmd.String())
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
cmd.Stdout = stdout
cmd.Stderr = stderr
if err = cmd.Run(); err != nil {
fmt.Println(ansiblePath, scriptPath)
fmt.Print(stdout, stderr)
log.Printf("Failed to run ansible script because: %s", err)
}
fmt.Println(ansiblePath, scriptPath)
return
}