2013-10-21 12:04:42 -04:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2014-02-15 18:05:28 -05:00
|
|
|
"bufio"
|
2013-10-21 12:04:42 -04:00
|
|
|
"fmt"
|
2013-12-05 19:22:49 -05:00
|
|
|
"io"
|
2013-11-07 15:19:24 -05:00
|
|
|
"os"
|
2014-02-15 18:06:21 -05:00
|
|
|
"sort"
|
2013-10-27 03:01:15 -04:00
|
|
|
"strings"
|
2014-08-05 06:00:05 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
2014-05-09 14:09:59 -04:00
|
|
|
|
2014-08-12 12:10:43 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/utils"
|
2013-10-21 12:04:42 -04:00
|
|
|
)
|
|
|
|
|
2014-04-22 19:51:06 -04:00
|
|
|
// Installer is a standard interface for objects which can "install" themselves
|
|
|
|
// on an engine by registering handlers.
|
|
|
|
// This can be used as an entrypoint for external plugins etc.
|
|
|
|
type Installer interface {
|
|
|
|
Install(*Engine) error
|
|
|
|
}
|
|
|
|
|
2013-11-20 02:37:03 -05:00
|
|
|
type Handler func(*Job) Status
|
2013-10-21 12:04:42 -04:00
|
|
|
|
|
|
|
var globalHandlers map[string]Handler
|
|
|
|
|
2013-10-26 22:24:01 -04:00
|
|
|
func init() {
|
|
|
|
globalHandlers = make(map[string]Handler)
|
|
|
|
}
|
|
|
|
|
2013-10-21 12:04:42 -04:00
|
|
|
func Register(name string, handler Handler) error {
|
2013-10-27 02:54:51 -04:00
|
|
|
_, exists := globalHandlers[name]
|
|
|
|
if exists {
|
|
|
|
return fmt.Errorf("Can't overwrite global handler for command %s", name)
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
|
|
|
globalHandlers[name] = handler
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-24 15:26:56 -05:00
|
|
|
func unregister(name string) {
|
|
|
|
delete(globalHandlers, name)
|
|
|
|
}
|
|
|
|
|
2013-10-21 12:04:42 -04:00
|
|
|
// The Engine is the core of Docker.
|
|
|
|
// It acts as a store for *containers*, and allows manipulation of these
|
|
|
|
// containers by executing *jobs*.
|
|
|
|
type Engine struct {
|
2014-08-05 06:00:05 -04:00
|
|
|
handlers map[string]Handler
|
|
|
|
catchall Handler
|
|
|
|
hack Hack // data for temporary hackery (see hack.go)
|
|
|
|
id string
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
|
|
|
Stdin io.Reader
|
|
|
|
Logging bool
|
|
|
|
tasks sync.WaitGroup
|
|
|
|
l sync.RWMutex // lock for shutdown
|
|
|
|
shutdown bool
|
|
|
|
onShutdown []func() // shutdown handlers
|
2013-10-27 03:01:15 -04:00
|
|
|
}
|
|
|
|
|
2013-10-27 02:54:51 -04:00
|
|
|
func (eng *Engine) Register(name string, handler Handler) error {
|
|
|
|
_, exists := eng.handlers[name]
|
|
|
|
if exists {
|
|
|
|
return fmt.Errorf("Can't overwrite handler for command %s", name)
|
|
|
|
}
|
|
|
|
eng.handlers[name] = handler
|
|
|
|
return nil
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
|
|
|
|
2014-04-24 03:36:21 -04:00
|
|
|
func (eng *Engine) RegisterCatchall(catchall Handler) {
|
|
|
|
eng.catchall = catchall
|
|
|
|
}
|
|
|
|
|
2014-03-14 19:53:43 -04:00
|
|
|
// New initializes a new engine.
|
2014-04-23 14:54:35 -04:00
|
|
|
func New() *Engine {
|
2013-10-21 12:04:42 -04:00
|
|
|
eng := &Engine{
|
2013-11-13 14:25:55 -05:00
|
|
|
handlers: make(map[string]Handler),
|
|
|
|
id: utils.RandomString(),
|
2013-12-05 19:22:49 -05:00
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stderr,
|
|
|
|
Stdin: os.Stdin,
|
2014-02-15 17:11:15 -05:00
|
|
|
Logging: true,
|
2013-10-27 02:54:51 -04:00
|
|
|
}
|
2014-02-15 18:06:21 -05:00
|
|
|
eng.Register("commands", func(job *Job) Status {
|
|
|
|
for _, name := range eng.commands() {
|
|
|
|
job.Printf("%s\n", name)
|
|
|
|
}
|
|
|
|
return StatusOK
|
|
|
|
})
|
2013-10-27 02:54:51 -04:00
|
|
|
// Copy existing global handlers
|
|
|
|
for k, v := range globalHandlers {
|
|
|
|
eng.handlers[k] = v
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
2014-04-23 14:54:35 -04:00
|
|
|
return eng
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
|
|
|
|
2013-10-27 03:01:15 -04:00
|
|
|
func (eng *Engine) String() string {
|
2014-03-14 19:53:43 -04:00
|
|
|
return fmt.Sprintf("%s", eng.id[:8])
|
2013-10-27 03:01:15 -04:00
|
|
|
}
|
|
|
|
|
2014-02-15 18:06:21 -05:00
|
|
|
// Commands returns a list of all currently registered commands,
|
|
|
|
// sorted alphabetically.
|
|
|
|
func (eng *Engine) commands() []string {
|
|
|
|
names := make([]string, 0, len(eng.handlers))
|
|
|
|
for name := range eng.handlers {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2013-10-21 12:04:42 -04:00
|
|
|
// Job creates a new job which can later be executed.
|
|
|
|
// This function mimics `Command` from the standard os/exec package.
|
2013-10-26 20:49:16 -04:00
|
|
|
func (eng *Engine) Job(name string, args ...string) *Job {
|
2013-10-21 12:04:42 -04:00
|
|
|
job := &Job{
|
2013-11-13 14:25:55 -05:00
|
|
|
Eng: eng,
|
|
|
|
Name: name,
|
|
|
|
Args: args,
|
2013-11-20 02:37:03 -05:00
|
|
|
Stdin: NewInput(),
|
|
|
|
Stdout: NewOutput(),
|
|
|
|
Stderr: NewOutput(),
|
2013-12-08 01:06:05 -05:00
|
|
|
env: &Env{},
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
2014-02-15 17:11:15 -05:00
|
|
|
if eng.Logging {
|
2014-08-12 12:10:43 -04:00
|
|
|
job.Stderr.Add(ioutils.NopWriteCloser(eng.Stderr))
|
2014-02-15 17:11:15 -05:00
|
|
|
}
|
2014-05-01 19:10:20 -04:00
|
|
|
|
|
|
|
// Catchall is shadowed by specific Register.
|
|
|
|
if handler, exists := eng.handlers[name]; exists {
|
|
|
|
job.handler = handler
|
2014-05-01 21:39:46 -04:00
|
|
|
} else if eng.catchall != nil && name != "" {
|
|
|
|
// empty job names are illegal, catchall or not.
|
2014-04-24 03:36:21 -04:00
|
|
|
job.handler = eng.catchall
|
2013-10-26 20:49:16 -04:00
|
|
|
}
|
|
|
|
return job
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
|
|
|
|
2014-08-05 06:00:05 -04:00
|
|
|
// OnShutdown registers a new callback to be called by Shutdown.
|
|
|
|
// This is typically used by services to perform cleanup.
|
|
|
|
func (eng *Engine) OnShutdown(h func()) {
|
|
|
|
eng.l.Lock()
|
|
|
|
eng.onShutdown = append(eng.onShutdown, h)
|
|
|
|
eng.l.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown permanently shuts down eng as follows:
|
|
|
|
// - It refuses all new jobs, permanently.
|
|
|
|
// - It waits for all active jobs to complete (with no timeout)
|
|
|
|
// - It calls all shutdown handlers concurrently (if any)
|
|
|
|
// - It returns when all handlers complete, or after 15 seconds,
|
|
|
|
// whichever happens first.
|
|
|
|
func (eng *Engine) Shutdown() {
|
|
|
|
eng.l.Lock()
|
|
|
|
if eng.shutdown {
|
|
|
|
eng.l.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
eng.shutdown = true
|
|
|
|
eng.l.Unlock()
|
|
|
|
// We don't need to protect the rest with a lock, to allow
|
|
|
|
// for other calls to immediately fail with "shutdown" instead
|
|
|
|
// of hanging for 15 seconds.
|
|
|
|
// This requires all concurrent calls to check for shutdown, otherwise
|
|
|
|
// it might cause a race.
|
|
|
|
|
2014-08-06 11:52:11 -04:00
|
|
|
// Wait for all jobs to complete.
|
|
|
|
// Timeout after 5 seconds.
|
|
|
|
tasksDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
eng.tasks.Wait()
|
|
|
|
close(tasksDone)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
case <-tasksDone:
|
|
|
|
}
|
2014-08-05 06:00:05 -04:00
|
|
|
|
|
|
|
// Call shutdown handlers, if any.
|
2014-08-06 11:52:11 -04:00
|
|
|
// Timeout after 10 seconds.
|
2014-08-05 06:00:05 -04:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, h := range eng.onShutdown {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(h func()) {
|
|
|
|
defer wg.Done()
|
|
|
|
h()
|
|
|
|
}(h)
|
|
|
|
}
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
select {
|
2014-08-06 11:52:11 -04:00
|
|
|
case <-time.After(time.Second * 10):
|
2014-08-05 06:00:05 -04:00
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsShutdown returns true if the engine is in the process
|
|
|
|
// of shutting down, or already shut down.
|
|
|
|
// Otherwise it returns false.
|
|
|
|
func (eng *Engine) IsShutdown() bool {
|
|
|
|
eng.l.RLock()
|
|
|
|
defer eng.l.RUnlock()
|
|
|
|
return eng.shutdown
|
|
|
|
}
|
|
|
|
|
2014-02-15 18:05:28 -05:00
|
|
|
// ParseJob creates a new job from a text description using a shell-like syntax.
|
|
|
|
//
|
|
|
|
// The following syntax is used to parse `input`:
|
|
|
|
//
|
|
|
|
// * Words are separated using standard whitespaces as separators.
|
|
|
|
// * Quotes and backslashes are not interpreted.
|
|
|
|
// * Words of the form 'KEY=[VALUE]' are added to the job environment.
|
|
|
|
// * All other words are added to the job arguments.
|
|
|
|
//
|
|
|
|
// For example:
|
|
|
|
//
|
|
|
|
// job, _ := eng.ParseJob("VERBOSE=1 echo hello TEST=true world")
|
|
|
|
//
|
|
|
|
// The resulting job will have:
|
|
|
|
// job.Args={"echo", "hello", "world"}
|
|
|
|
// job.Env={"VERBOSE":"1", "TEST":"true"}
|
|
|
|
//
|
|
|
|
func (eng *Engine) ParseJob(input string) (*Job, error) {
|
|
|
|
// FIXME: use a full-featured command parser
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(input))
|
|
|
|
scanner.Split(bufio.ScanWords)
|
|
|
|
var (
|
|
|
|
cmd []string
|
|
|
|
env Env
|
|
|
|
)
|
|
|
|
for scanner.Scan() {
|
|
|
|
word := scanner.Text()
|
|
|
|
kv := strings.SplitN(word, "=", 2)
|
|
|
|
if len(kv) == 2 {
|
|
|
|
env.Set(kv[0], kv[1])
|
|
|
|
} else {
|
|
|
|
cmd = append(cmd, word)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(cmd) == 0 {
|
|
|
|
return nil, fmt.Errorf("empty command: '%s'", input)
|
|
|
|
}
|
|
|
|
job := eng.Job(cmd[0], cmd[1:]...)
|
|
|
|
job.Env().Init(&env)
|
|
|
|
return job, nil
|
|
|
|
}
|
|
|
|
|
2013-10-27 03:01:15 -04:00
|
|
|
func (eng *Engine) Logf(format string, args ...interface{}) (n int, err error) {
|
2014-02-15 17:11:15 -05:00
|
|
|
if !eng.Logging {
|
|
|
|
return 0, nil
|
2014-01-22 16:35:35 -05:00
|
|
|
}
|
2014-02-15 17:11:15 -05:00
|
|
|
prefixedFormat := fmt.Sprintf("[%s] %s\n", eng, strings.TrimRight(format, "\n"))
|
|
|
|
return fmt.Fprintf(eng.Stderr, prefixedFormat, args...)
|
2013-10-27 03:01:15 -04:00
|
|
|
}
|