-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
78 lines (66 loc) · 2.15 KB
/
builder.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
// Package builder provides libraries for the Deis builder.
//
// The Deis builder is responsible for packaging Docker images for consumers.
//
// The builder/cli package contains command line clients for this library.
package builder
import (
"github.com/Masterminds/cookoo"
clog "github.com/Masterminds/cookoo/log"
"github.com/deis/deis/builder/sshd"
"log"
"os"
)
// Return codes that will be sent to the shell.
const (
StatusOk = iota
StatusLocalError
)
// Run starts the Builder service.
//
// The Builder service is responsible for setting up the local container
// environment and then listening for new builds. The main listening service
// is SSH. Builder listens for new Git commands and then sends those on to
// Git.
//
// Run returns on of the Status* status code constants.
func Run(cmd string) int {
reg, router, ocxt := cookoo.Cookoo()
log.SetFlags(0) // Time is captured elsewhere.
// We layer the context to add better logging and also synchronize
// access so that goroutines don't get into race conditions.
cxt := cookoo.SyncContext(ocxt)
cxt.Put("cookoo.Router", router)
cxt.AddLogger("stdout", os.Stdout)
// Build the routes. See routes.go.
routes(reg)
// Bootstrap the background services. If this fails, we stop.
if err := router.HandleRequest("boot", cxt, false); err != nil {
clog.Errf(cxt, "Fatal errror on boot: %s", err)
return StatusLocalError
}
// Set up the SSH service.
ip := os.Getenv("SSH_HOST_IP")
if ip == "" {
ip = "0.0.0.0"
}
port := os.Getenv("SSH_HOST_PORT")
if port == "" {
port = "2223"
}
cxt.Put(sshd.Address, ip+":"+port)
// Supply route names for handling various internal routing. While this
// isn't necessary for Cookoo, it makes it easy for us to mock these
// routes in tests. c.f. sshd/server.go
cxt.Put("route.sshd.pubkeyAuth", "pubkeyAuth")
cxt.Put("route.sshd.sshPing", "sshPing")
cxt.Put("route.sshd.sshGitReceive", "sshGitReceive")
// Start the SSH service.
// TODO: We could refactor Serve to be a command, and then run this as
// a route.
if err := sshd.Serve(reg, router, cxt); err != nil {
clog.Errf(cxt, "SSH server failed: %s", err)
return StatusLocalError
}
return StatusOk
}