Skip to content

Commit 13aa3a4

Browse files
arschlesAaron Schlesinger
authored andcommitted
ref(*): beginning rewrite for all of builder to Go
1 parent dc0c124 commit 13aa3a4

4 files changed

Lines changed: 31 additions & 21 deletions

File tree

boot.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
package main
22

33
import (
4+
"log"
45
"os"
56
"runtime"
67

8+
"github.com/arschles/builder/fetcher"
79
"github.com/deis/builder/pkg"
10+
"github.com/kelseyhightower/envconfig"
811
)
912

10-
func main() {
13+
func init() {
1114
runtime.GOMAXPROCS(runtime.NumCPU())
15+
}
16+
17+
type Config struct {
18+
FetcherPort int `envconfig:"fetcher_port"`
19+
SSHHostIP string `envconfig:"ssh_host_ip"`
20+
SSHHostPort int `envconfig:"ssh_host_port"`
21+
}
22+
23+
func main() {
24+
var conf Config
25+
if err := envconfig.Process("builder", &config); err != nil {
26+
log.Fatalf("error fetching config [%s]", err)
27+
os.Exit(1)
28+
}
29+
log.Printf("starting fetcher on port %d", conf.FetcherPort)
30+
go fetcher.Serve(conf.FetcherPort)
1231
os.Exit(pkg.Run("boot"))
1332
}

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)

glide.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
parent: null
12
package: main
23
import:
4+
- package: github.com/kelseyhightower/envconfig
35
- package: github.com/coreos/go-etcd
46
subpackages:
57
- /etcd

pkg/builder.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package pkg
55

66
import (
7+
"fmt"
8+
79
"github.com/Masterminds/cookoo"
810
clog "github.com/Masterminds/cookoo/log"
911
"github.com/deis/builder/pkg/sshd"
@@ -26,7 +28,7 @@ const (
2628
// Git.
2729
//
2830
// Run returns on of the Status* status code constants.
29-
func Run(cmd string) int {
31+
func Run(sshHostIP string, sshHostPort int, cmd string) int {
3032
reg, router, ocxt := cookoo.Cookoo()
3133
log.SetFlags(0) // Time is captured elsewhere.
3234

@@ -45,17 +47,7 @@ func Run(cmd string) int {
4547
return StatusLocalError
4648
}
4749

48-
// Set up the SSH service.
49-
ip := os.Getenv("SSH_HOST_IP")
50-
if ip == "" {
51-
ip = "0.0.0.0"
52-
}
53-
port := os.Getenv("SSH_HOST_PORT")
54-
if port == "" {
55-
port = "2223"
56-
}
57-
58-
cxt.Put(sshd.Address, ip+":"+port)
50+
cxt.Put(sshd.Address, fmt.Sprintf("%s:%d", sshHostIP, sshHostPort))
5951

6052
// Supply route names for handling various internal routing. While this
6153
// isn't necessary for Cookoo, it makes it easy for us to mock these

0 commit comments

Comments
 (0)