github.com/dustinrc/deis@v1.10.1-0.20150917223407-0894a5fb979e/builder/builder.go (about)

     1  // Package builder provides libraries for the Deis builder.
     2  //
     3  // The Deis builder is responsible for packaging Docker images for consumers.
     4  //
     5  // The builder/cli package contains command line clients for this library.
     6  package builder
     7  
     8  import (
     9  	"github.com/Masterminds/cookoo"
    10  	clog "github.com/Masterminds/cookoo/log"
    11  	"github.com/deis/deis/builder/sshd"
    12  
    13  	"log"
    14  	"os"
    15  )
    16  
    17  // Return codes that will be sent to the shell.
    18  const (
    19  	StatusOk = iota
    20  	StatusLocalError
    21  )
    22  
    23  // Run starts the Builder service.
    24  //
    25  // The Builder service is responsible for setting up the local container
    26  // environment and then listening for new builds. The main listening service
    27  // is SSH. Builder listens for new Git commands and then sends those on to
    28  // Git.
    29  //
    30  // Run returns on of the Status* status code constants.
    31  func Run(cmd string) int {
    32  	reg, router, ocxt := cookoo.Cookoo()
    33  	log.SetFlags(0) // Time is captured elsewhere.
    34  
    35  	// We layer the context to add better logging and also synchronize
    36  	// access so that goroutines don't get into race conditions.
    37  	cxt := cookoo.SyncContext(ocxt)
    38  	cxt.Put("cookoo.Router", router)
    39  	cxt.AddLogger("stdout", os.Stdout)
    40  
    41  	// Build the routes. See routes.go.
    42  	routes(reg)
    43  
    44  	// Bootstrap the background services. If this fails, we stop.
    45  	if err := router.HandleRequest("boot", cxt, false); err != nil {
    46  		clog.Errf(cxt, "Fatal errror on boot: %s", err)
    47  		return StatusLocalError
    48  	}
    49  
    50  	// Set up the SSH service.
    51  	ip := os.Getenv("SSH_HOST_IP")
    52  	if ip == "" {
    53  		ip = "0.0.0.0"
    54  	}
    55  	port := os.Getenv("SSH_HOST_PORT")
    56  	if port == "" {
    57  		port = "2223"
    58  	}
    59  
    60  	cxt.Put(sshd.Address, ip+":"+port)
    61  
    62  	// Supply route names for handling various internal routing. While this
    63  	// isn't necessary for Cookoo, it makes it easy for us to mock these
    64  	// routes in tests. c.f. sshd/server.go
    65  	cxt.Put("route.sshd.pubkeyAuth", "pubkeyAuth")
    66  	cxt.Put("route.sshd.sshPing", "sshPing")
    67  	cxt.Put("route.sshd.sshGitReceive", "sshGitReceive")
    68  
    69  	// Start the SSH service.
    70  	// TODO: We could refactor Serve to be a command, and then run this as
    71  	// a route.
    72  	if err := sshd.Serve(reg, router, cxt); err != nil {
    73  		clog.Errf(cxt, "SSH server failed: %s", err)
    74  		return StatusLocalError
    75  	}
    76  
    77  	return StatusOk
    78  }