forked from teamhephy/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboot.go
154 lines (141 loc) · 4.47 KB
/
boot.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
package main
import (
"log"
"os"
"runtime"
"github.com/codegangsta/cli"
storagedriver "github.com/docker/distribution/registry/storage/driver"
_ "github.com/docker/distribution/registry/storage/driver/azure"
"github.com/docker/distribution/registry/storage/driver/factory"
_ "github.com/docker/distribution/registry/storage/driver/gcs"
_ "github.com/docker/distribution/registry/storage/driver/s3-aws"
_ "github.com/docker/distribution/registry/storage/driver/swift"
"github.com/kelseyhightower/envconfig"
"github.com/teamhephy/builder/pkg"
"github.com/teamhephy/builder/pkg/cleaner"
"github.com/teamhephy/builder/pkg/conf"
"github.com/teamhephy/builder/pkg/gitreceive"
"github.com/teamhephy/builder/pkg/healthsrv"
"github.com/teamhephy/builder/pkg/sshd"
"github.com/teamhephy/builder/pkg/sys"
pkglog "github.com/teamhephy/pkg/log"
kcl "k8s.io/kubernetes/pkg/client/unversioned"
)
const (
serverConfAppName = "deis-builder-server"
gitReceiveConfAppName = "deis-builder-git-receive"
gitHomeDir = "/home/git"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
if os.Getenv("DEIS_DEBUG") == "true" {
pkglog.DefaultLogger.SetDebug(true)
log.Printf("Running in debug mode")
}
app := cli.NewApp()
app.Commands = []cli.Command{
{
Name: "server",
Aliases: []string{"srv"},
Usage: "Run the git server",
Action: func(c *cli.Context) {
cnf := new(sshd.Config)
if err := envconfig.Process(serverConfAppName, cnf); err != nil {
pkglog.Err("getting config for %s [%s]", serverConfAppName, err)
os.Exit(1)
}
fs := sys.RealFS()
env := sys.RealEnv()
pushLock := sshd.NewInMemoryRepositoryLock(cnf.GitLockTimeout())
circ := sshd.NewCircuit()
storageParams, err := conf.GetStorageParams(env)
if err != nil {
log.Printf("Error getting storage parameters (%s)", err)
os.Exit(1)
}
var storageDriver storagedriver.StorageDriver
if cnf.StorageType == "minio" {
storageDriver, err = factory.Create("s3", storageParams)
} else {
storageDriver, err = factory.Create(cnf.StorageType, storageParams)
}
if err != nil {
log.Printf("Error creating storage driver (%s)", err)
os.Exit(1)
}
kubeClient, err := kcl.NewInCluster()
if err != nil {
log.Printf("Error getting kubernetes client [%s]", err)
os.Exit(1)
}
log.Printf("Starting health check server on port %d", cnf.HealthSrvPort)
healthSrvCh := make(chan error)
go func() {
if err := healthsrv.Start(cnf, kubeClient.Namespaces(), storageDriver, circ); err != nil {
healthSrvCh <- err
}
}()
log.Printf("Starting deleted app cleaner")
cleanerErrCh := make(chan error)
go func() {
if err := cleaner.Run(gitHomeDir, kubeClient.Namespaces(), fs, cnf.CleanerPollSleepDuration(), storageDriver); err != nil {
cleanerErrCh <- err
}
}()
log.Printf("Starting SSH server on %s:%d", cnf.SSHHostIP, cnf.SSHHostPort)
sshCh := make(chan int)
go func() {
sshCh <- pkg.RunBuilder(cnf, gitHomeDir, circ, pushLock)
}()
select {
case err := <-healthSrvCh:
log.Printf("Error running health server (%s)", err)
os.Exit(1)
case i := <-sshCh:
log.Printf("Unexpected SSH server stop with code %d", i)
os.Exit(i)
case err := <-cleanerErrCh:
log.Printf("Error running the deleted app cleaner (%s)", err)
os.Exit(1)
}
},
},
{
Name: "git-receive",
Aliases: []string{"gr"},
Usage: "Run the git-receive hook",
Action: func(c *cli.Context) {
cnf := new(gitreceive.Config)
if err := envconfig.Process(gitReceiveConfAppName, cnf); err != nil {
log.Printf("Error getting config for %s [%s]", gitReceiveConfAppName, err)
os.Exit(1)
}
cnf.CheckDurations()
fs := sys.RealFS()
env := sys.RealEnv()
storageParams, err := conf.GetStorageParams(env)
if err != nil {
log.Printf("Error getting storage parameters (%s)", err)
os.Exit(1)
}
var storageDriver storagedriver.StorageDriver
if cnf.StorageType == "minio" {
storageDriver, err = factory.Create("s3", storageParams)
} else {
storageDriver, err = factory.Create(cnf.StorageType, storageParams)
}
if err != nil {
log.Printf("Error creating storage driver (%s)", err)
os.Exit(1)
}
if err := gitreceive.Run(cnf, fs, env, storageDriver); err != nil {
log.Printf("Error running git receive hook [%s]", err)
os.Exit(1)
}
},
},
}
app.Run(os.Args)
}