This repository has been archived by the owner on Aug 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.go
125 lines (105 loc) · 2.96 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/Sirupsen/logrus"
"github.com/docker/leeroy/jenkins"
)
const (
// VERSION is the version
VERSION = "v0.1.0"
// DEFAULTCONTEXT is the default github context for a build
DEFAULTCONTEXT = "janky"
)
var (
certFile string
keyFile string
port string
configFile string
debug bool
version bool
config Config
)
// Config describes the leeroy config file
type Config struct {
Jenkins jenkins.Client `json:"jenkins"`
BuildCommits string `json:"build_commits"`
GHToken string `json:"github_token"`
GHUser string `json:"github_user"`
Builds []Build `json:"builds"`
User string `json:"user"`
Pass string `json:"pass"`
}
// Build describes the paramaters for a build
type Build struct {
Repo string `json:"github_repo"`
Job string `json:"jenkins_job_name"`
Context string `json:"context"`
Custom bool `json:"custom"`
HandleIssues bool `json:"handle_issues"`
IsPipeline bool `json:"is_pipeline"`
}
func init() {
// parse flags
flag.BoolVar(&version, "version", false, "print version and exit")
flag.BoolVar(&version, "v", false, "print version and exit (shorthand)")
flag.BoolVar(&debug, "d", false, "run in debug mode")
flag.StringVar(&certFile, "cert", "", "path to ssl certificate")
flag.StringVar(&keyFile, "key", "", "path to ssl key")
flag.StringVar(&port, "port", "80", "port to use")
flag.StringVar(&configFile, "config", "/etc/leeroy/config.json", "path to config file")
flag.Parse()
}
func main() {
// set log level
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
if version {
fmt.Println(VERSION)
return
}
// read the config file
if _, err := os.Stat(configFile); os.IsNotExist(err) {
logrus.Errorf("config file does not exist: %s", configFile)
return
}
c, err := ioutil.ReadFile(configFile)
if err != nil {
logrus.Errorf("could not read config file: %v", err)
return
}
if err := json.Unmarshal(c, &config); err != nil {
logrus.Errorf("error parsing config file as json: %v", err)
return
}
// create mux server
mux := http.NewServeMux()
// ping endpoint
mux.HandleFunc("/ping", pingHandler)
// jenkins notification endpoint
mux.HandleFunc("/notification/jenkins", jenkinsHandler)
// github webhooks endpoint
mux.HandleFunc("/notification/github", githubHandler)
// retry build endpoint
mux.HandleFunc("/build/retry", customBuildHandler)
// custom build endpoint
mux.HandleFunc("/build/custom", customBuildHandler)
// cron endpoint to reschedule bulk jobs
mux.HandleFunc("/build/cron", cronBuildHandler)
// set up the server
server := &http.Server{
Addr: ":" + port,
Handler: mux,
}
logrus.Printf("Starting server on port %q", port)
if certFile != "" && keyFile != "" {
logrus.Fatal(server.ListenAndServeTLS(certFile, keyFile))
} else {
logrus.Fatal(server.ListenAndServe())
}
}