Skip to content

Commit 7e11233

Browse files
committed
Merge pull request #72 from arschles/go-rewrite
ref(*): consolidate builder into fewer Go binaries
2 parents dc0c124 + 74ded2e commit 7e11233

39 files changed

Lines changed: 1351 additions & 888 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
rootfs/bin/boot
22
vendor/
33
rootfs/usr/bin/
4+
./builder

Makefile

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ BINARY_DEST_DIR := rootfs/usr/bin
1717
# Common flags passed into Go's linker.
1818
LDFLAGS := "-s -X main.version=${VERSION}"
1919
IMAGE_PREFIX ?= deis
20-
BINARIES := extract-types extract-version generate-buildhook get-app-config get-app-values publish-release-controller yaml2json-procfile
21-
STANDALONE := extract-types generate-buildhook yaml2json-procfile
2220
# Docker Root FS
2321
BINDIR := ./rootfs
2422

@@ -40,16 +38,8 @@ bootstrap:
4038
# the Docker environment. Other alternatives are cross-compiling, doing
4139
# the build as a `docker build`.
4240
build:
43-
${DEV_ENV_PREFIX} -e CGO_ENABLED=0 ${DEV_ENV_IMAGE} go build -a -installsuffix cgo -ldflags '-s' -o $(BINARY_DEST_DIR)/builder boot.go || exit 1
44-
${DEV_ENV_PREFIX} -e CGO_ENABLED=0 ${DEV_ENV_IMAGE} go build -a -installsuffix cgo -ldflags '-s' -o $(BINARY_DEST_DIR)/fetcher fetcher/fetcher.go || exit 1
45-
@$(call check-static-binary,$(BINARY_DEST_DIR)/builder)
46-
@$(call check-static-binary,$(BINARY_DEST_DIR)/fetcher)
47-
for i in $(BINARIES); do \
48-
${DEV_ENV_PREFIX} -e CGO_ENABLED=0 ${DEV_ENV_IMAGE} go build -a -installsuffix cgo -ldflags '-s' -o $(BINARY_DEST_DIR)/$$i pkg/src/$$i.go || exit 1; \
49-
done
50-
@for i in $(BINARIES); do \
51-
$(call check-static-binary,$(BINARY_DEST_DIR)/$$i); \
52-
done
41+
${DEV_ENV_PREFIX} -e CGO_ENABLED=0 ${DEV_ENV_IMAGE} go build -a -installsuffix cgo -ldflags ${LDFLAGS} -o ${BINARY_DEST_DIR}/boot boot.go || exit 1
42+
@$(call check-static-binary,$(BINARY_DEST_DIR)/boot)
5343

5444
test:
5545
${DEV_ENV_CMD} go test ./pkg && \

boot.go

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,68 @@ import (
44
"os"
55
"runtime"
66

7+
cookoolog "github.com/Masterminds/cookoo/log"
8+
"github.com/codegangsta/cli"
9+
"github.com/deis/builder/fetcher"
710
"github.com/deis/builder/pkg"
11+
"github.com/deis/builder/pkg/conf"
12+
"github.com/deis/builder/pkg/gitreceive"
13+
pkglog "github.com/deis/builder/pkg/gitreceive/log"
14+
"github.com/deis/builder/pkg/sshd"
815
)
916

10-
func main() {
17+
const (
18+
serverConfAppName = "deis-builder-server"
19+
gitReceiveConfAppName = "deis-builder-git-receive"
20+
)
21+
22+
func init() {
1123
runtime.GOMAXPROCS(runtime.NumCPU())
12-
os.Exit(pkg.Run("boot"))
24+
}
25+
26+
func main() {
27+
if os.Getenv("DEBUG") == "true" {
28+
pkglog.IsDebugging = true
29+
cookoolog.Level = cookoolog.LogDebug
30+
}
31+
pkglog.Debug("Running in debug mode")
32+
33+
app := cli.NewApp()
34+
35+
app.Commands = []cli.Command{
36+
{
37+
Name: "server",
38+
Aliases: []string{"srv"},
39+
Usage: "Run the git server",
40+
Action: func(c *cli.Context) {
41+
cnf := new(sshd.Config)
42+
if err := conf.EnvConfig(serverConfAppName, cnf); err != nil {
43+
pkglog.Err("getting config for %s [%s]", serverConfAppName, err)
44+
os.Exit(1)
45+
}
46+
pkglog.Info("starting fetcher on port %d", cnf.FetcherPort)
47+
go fetcher.Serve(cnf.FetcherPort)
48+
pkglog.Info("starting SSH server on %s:%d", cnf.SSHHostIP, cnf.SSHHostPort)
49+
os.Exit(pkg.Run(cnf.SSHHostIP, cnf.SSHHostPort, "boot"))
50+
},
51+
},
52+
{
53+
Name: "git-receive",
54+
Aliases: []string{"gr"},
55+
Usage: "Run the git-receive hook",
56+
Action: func(c *cli.Context) {
57+
cnf := new(gitreceive.Config)
58+
if err := conf.EnvConfig(gitReceiveConfAppName, cnf); err != nil {
59+
pkglog.Err("Error getting config for %s [%s]", gitReceiveConfAppName, err)
60+
os.Exit(1)
61+
}
62+
if err := gitreceive.Run(cnf); err != nil {
63+
pkglog.Err("running git receive hook [%s]", err)
64+
os.Exit(1)
65+
}
66+
},
67+
},
68+
}
69+
70+
app.Run(os.Args)
1371
}

fetcher/fetcher.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package fetcher
22

33
import (
44
"fmt"
@@ -18,19 +18,18 @@ const (
1818
cmdstring = "/tmp/builder/build.sh"
1919
)
2020

21-
func main() {
21+
// Serve will start the fetcher server and block until it stops. Since it blocks, it's a best practice to execute this func in a goroutine.
22+
func Serve(port int) {
2223
rtr := mux.NewRouter()
2324
rtr.HandleFunc("/git/home/{name}/tar", getTar).Methods("GET")
2425
rtr.HandleFunc("/git/home/{name}/slug", getSlug).Methods("GET")
2526
rtr.HandleFunc("/git/home/health", health).Methods("GET")
2627
rtr.HandleFunc("/git/home/{name}/push", putSlug).Methods("PUT")
27-
http.Handle("/", rtr)
28-
log.Println("Listening... on 3000")
29-
http.ListenAndServe(":3000", nil)
28+
hostStr := fmt.Sprintf(":%d", port)
29+
http.ListenAndServe(hostStr, rtr)
3030
}
3131

3232
func getTar(w http.ResponseWriter, r *http.Request) {
33-
fmt.Println("Hello, world! gettar")
3433
params := mux.Vars(r)
3534
name := strings.Split(params["name"], ":")[0]
3635
dat, err := ioutil.ReadFile(appdirectory + name + ".git/" + name + ".tar.gz")
@@ -45,7 +44,6 @@ func health(w http.ResponseWriter, r *http.Request) {
4544
}
4645

4746
func getSlug(w http.ResponseWriter, r *http.Request) {
48-
fmt.Println("Hello, world! getslug")
4947
params := mux.Vars(r)
5048
name := params["name"]
5149
dat, err := ioutil.ReadFile(slugdirectory + name + "/slug.tgz")
@@ -56,7 +54,6 @@ func getSlug(w http.ResponseWriter, r *http.Request) {
5654
}
5755

5856
func putSlug(w http.ResponseWriter, r *http.Request) {
59-
fmt.Println("Hello, world! putslug")
6057
params := mux.Vars(r)
6158
name := params["name"]
6259
log.Println(name)

0 commit comments

Comments
 (0)