Choose a single backend with `--backend`

Note: for now there is only one backend available, `debug`
This commit is contained in:
Solomon Hykes 2014-05-01 18:13:55 -07:00
parent 142e43a447
commit c22a80a071
3 changed files with 59 additions and 20 deletions

19
backends/backends.go Normal file
View File

@ -0,0 +1,19 @@
package backends
import (
"github.com/dotcloud/docker/engine"
)
// New returns a new engine, with all backends
// registered but not activated.
// To activate a backend, call a job on the resulting
// engine, named after the desired backend.
//
// Example: `New().Job("debug").Run()`
func New() *engine.Engine {
back := engine.New()
back.Logging = false
// Register all backends here
Debug().Install(back)
return back
}

View File

@ -14,16 +14,19 @@ type debug struct {
} }
func (d *debug) Install(eng *engine.Engine) error { func (d *debug) Install(eng *engine.Engine) error {
eng.RegisterCatchall(func(job *engine.Job) engine.Status { eng.Register("debug", func(job *engine.Job) engine.Status {
fmt.Printf("--> %s %s\n", job.Name, strings.Join(job.Args, " ")) job.Eng.RegisterCatchall(func(job *engine.Job) engine.Status {
for k, v := range job.Env().Map() { fmt.Printf("--> %s %s\n", job.Name, strings.Join(job.Args, " "))
fmt.Printf(" %s=%s\n", k, v) for k, v := range job.Env().Map() {
} fmt.Printf(" %s=%s\n", k, v)
// This helps us detect the race condition if our time.Sleep }
// missed it. (see comment in main) // This helps us detect the race condition if our time.Sleep
if job.Name == "acceptconnections" { // missed it. (see comment in main)
panic("race condition in github.com/dotcloud/docker/api/server/ServeApi") if job.Name == "acceptconnections" {
} panic("race condition in github.com/dotcloud/docker/api/server/ServeApi")
}
return engine.StatusOK
})
return engine.StatusOK return engine.StatusOK
}) })
return nil return nil

View File

@ -2,13 +2,13 @@ package main
import ( import (
"fmt" "fmt"
"github.com/codegangsta/cli"
"github.com/docker/swarmd/backends" "github.com/docker/swarmd/backends"
"github.com/dotcloud/docker/api/server" "github.com/dotcloud/docker/api/server"
"github.com/dotcloud/docker/engine" "github.com/dotcloud/docker/engine"
"os" "os"
"time"
"github.com/codegangsta/cli"
"strings" "strings"
"time"
) )
func main() { func main() {
@ -17,6 +17,7 @@ func main() {
app.Usage = "Control a heterogenous distributed system with the Docker API" app.Usage = "Control a heterogenous distributed system with the Docker API"
app.Version = "0.0.1" app.Version = "0.0.1"
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
cli.StringFlag{"backend", "debug", "load a backend"},
} }
app.Action = cmdDaemon app.Action = cmdDaemon
app.Run(os.Args) app.Run(os.Args)
@ -26,20 +27,37 @@ func cmdDaemon(c *cli.Context) {
if len(c.Args()) == 0 { if len(c.Args()) == 0 {
Fatalf("Usage: %s <proto>://<address> [<proto>://<address>]...\n", c.App.Name) Fatalf("Usage: %s <proto>://<address> [<proto>://<address>]...\n", c.App.Name)
} }
eng := engine.New()
eng.Logging = false // Load backend
if err := backends.Debug().Install(eng); err != nil { // FIXME: allow for multiple backends to be loaded.
Fatalf("backend install: %v", err) // This could be done by instantiating 1 engine per backend,
// installing each backend in its respective engine,
// then registering a Catchall on the frontent engine which
// multiplexes across all backends (with routing / filtering
// logic along the way).
back := backends.New()
backendName := c.String("backend")
fmt.Printf("Loading backend '%s'\n", backendName)
if err := back.Job(backendName).Run(); err != nil {
Fatalf("%s: %v\n", backendName, err)
} }
// Register the API entrypoint // Register the API entrypoint
// (we register it as `argv[0]` so we can print usage messages straight from the job // (we register it as `argv[0]` so we can print usage messages straight from the job
// stderr. // stderr.
eng.Register(c.App.Name, server.ServeApi) front := engine.New()
front.Logging = false
// FIXME: server should expose an engine.Installer
front.Register(c.App.Name, server.ServeApi)
front.RegisterCatchall(func(job *engine.Job) engine.Status {
fw := back.Job(job.Name, job.Args...)
fw.Run()
return engine.Status(fw.StatusCode())
})
// Call the API entrypoint // Call the API entrypoint
go func() { go func() {
serve := eng.Job(c.App.Name, c.Args()...) serve := front.Job(c.App.Name, c.Args()...)
serve.Stdout.Add(os.Stdout) serve.Stdout.Add(os.Stdout)
serve.Stderr.Add(os.Stderr) serve.Stderr.Add(os.Stderr)
if err := serve.Run(); err != nil { if err := serve.Run(); err != nil {
@ -50,7 +68,7 @@ func cmdDaemon(c *cli.Context) {
// As a workaround we sleep to give it time to register 'acceptconnections'. // As a workaround we sleep to give it time to register 'acceptconnections'.
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
// Notify that we're ready to receive connections // Notify that we're ready to receive connections
if err := eng.Job("acceptconnections").Run(); err != nil { if err := front.Job("acceptconnections").Run(); err != nil {
Fatalf("acceptconnections: %v", err) Fatalf("acceptconnections: %v", err)
} }
// Inifinite loop // Inifinite loop
@ -61,7 +79,6 @@ func Fatalf(msg string, args ...interface{}) {
if !strings.HasSuffix(msg, "\n") { if !strings.HasSuffix(msg, "\n") {
msg = msg + "\n" msg = msg + "\n"
} }
panic(msg)
fmt.Fprintf(os.Stderr, msg, args...) fmt.Fprintf(os.Stderr, msg, args...)
os.Exit(1) os.Exit(1)
} }