forked from bmorton/builder
/
main.go
105 lines (88 loc) · 3.38 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
package main
import (
"database/sql"
_ "expvar"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"github.com/bmorton/builder/api"
"github.com/bmorton/builder/builds"
"github.com/bmorton/builder/streams"
"github.com/fsouza/go-dockerclient"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
"github.com/namsral/flag"
)
func main() {
var listen string
var dockerHost string
var dockerTLSVerify bool
var dockerCertPath string
var debugMode bool
var debugListen string
var registryURL string
var cachePath string
var dsn string
var sqlAdapter string
flag.StringVar(&listen, "listen", ":3000", "host:port to listen on")
flag.StringVar(&dockerHost, "docker-host", "unix:///var/run/docker.sock", "address of Docker host")
flag.BoolVar(&dockerTLSVerify, "docker-tls-verify", false, "use TLS client for Docker")
flag.StringVar(&dockerCertPath, "docker-cert-path", "", "path to the cert.pem, key.pem, and ca.pem for authenticating to Docker")
flag.BoolVar(&debugMode, "debug", false, "enable /debug endpoints on DEBUG_LISTEN")
flag.StringVar(&debugListen, "debug-listen", ":3001", "host:port to listen on for debug requests")
flag.StringVar(®istryURL, "registry-url", "192.168.59.103:5000", "host:port of the registry for pushing images")
flag.StringVar(&cachePath, "cache-path", "cache/", "path to the directory where cached repos will be stored")
flag.StringVar(&dsn, "dsn", "file::memory:?cache=shared", "DSN string for connecting to the database")
flag.StringVar(&sqlAdapter, "sql-adapter", "sqlite3", "adapter to use for the DSN string (currently only supports sqlite3)")
flag.Parse()
gin.DisableBindValidation()
router := gin.Default()
router.StaticFile("/", "static/index.html")
router.Use(static.Serve("/", static.LocalFile("static", false)))
client := dockerClient(dockerHost, dockerTLSVerify, dockerCertPath)
db, err := sql.Open(sqlAdapter, dsn)
if err != nil {
panic(err)
}
buildRepo := builds.NewRepository(sqlAdapter, db)
buildRepo.Migrate()
logRepo := builds.NewLogRepository(sqlAdapter, db)
logRepo.Migrate()
streamRepo := streams.NewRepository()
builder := builds.NewBuilder(registryURL, client, cachePath)
buildQueue := builds.NewQueue(buildRepo, streamRepo, logRepo, builder)
webhookHandler := api.NewWebhookHandler(buildRepo, buildQueue)
router.POST("/webhooks/github", webhookHandler.Github)
buildsResource := api.NewBuildsResource(buildRepo, buildQueue)
router.GET("/builds", buildsResource.Index)
router.POST("/builds", buildsResource.Create)
router.GET("/builds/:id", buildsResource.Show)
streamsResource := api.NewStreamsResource(buildRepo, streamRepo)
router.GET("/builds/:id/streams/:type", streamsResource.Show)
logsResource := api.NewLogsResource(buildRepo, logRepo)
router.GET("/builds/:id/logs/:type", logsResource.Show)
go buildQueue.Run()
if debugMode {
log.Println("Starting debug server on :3001")
go http.ListenAndServe(debugListen, http.DefaultServeMux)
}
router.Run(listen)
}
func dockerClient(host string, tls bool, certPath string) *docker.Client {
var client *docker.Client
var err error
if tls {
cert := fmt.Sprintf("%s/cert.pem", certPath)
key := fmt.Sprintf("%s/key.pem", certPath)
ca := fmt.Sprintf("%s/ca.pem", certPath)
client, err = docker.NewTLSClient(host, cert, key, ca)
} else {
client, err = docker.NewClient(host)
}
if err != nil {
log.Fatal(err)
}
return client
}