2013-10-21 12:04:42 -04:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2014-05-09 14:09:59 -04:00
|
|
|
"bytes"
|
2013-11-13 14:25:55 -05:00
|
|
|
"fmt"
|
2013-11-07 15:19:24 -05:00
|
|
|
"io"
|
|
|
|
"strings"
|
2015-03-10 18:10:00 -04:00
|
|
|
"sync"
|
2013-11-20 02:37:03 -05:00
|
|
|
"time"
|
2014-10-24 14:20:17 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2013-10-21 12:04:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// A job is the fundamental unit of work in the docker engine.
|
|
|
|
// Everything docker can do should eventually be exposed as a job.
|
|
|
|
// For example: execute a process in a container, create a new container,
|
|
|
|
// download an archive from the internet, serve the http api, etc.
|
|
|
|
//
|
|
|
|
// The job API is designed after unix processes: a job has a name, arguments,
|
2015-03-26 05:48:07 -04:00
|
|
|
// environment variables, standard streams for input, output and error.
|
2013-10-21 12:04:42 -04:00
|
|
|
type Job struct {
|
2013-11-13 14:25:55 -05:00
|
|
|
Eng *Engine
|
|
|
|
Name string
|
|
|
|
Args []string
|
2013-11-20 02:37:03 -05:00
|
|
|
env *Env
|
2013-11-20 02:37:03 -05:00
|
|
|
Stdout *Output
|
|
|
|
Stderr *Output
|
|
|
|
Stdin *Input
|
|
|
|
handler Handler
|
|
|
|
end time.Time
|
2014-09-16 02:43:43 -04:00
|
|
|
closeIO bool
|
2015-03-10 18:10:00 -04:00
|
|
|
|
|
|
|
// When closed, the job has been cancelled.
|
|
|
|
// Note: not all jobs implement cancellation.
|
|
|
|
// See Job.Cancel() and Job.WaitCancelled()
|
|
|
|
cancelled chan struct{}
|
|
|
|
cancelOnce sync.Once
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes the job and blocks until the job completes.
|
2015-03-25 03:44:12 -04:00
|
|
|
// If the job fails it returns an error
|
2015-03-26 05:48:07 -04:00
|
|
|
func (job *Job) Run() (err error) {
|
|
|
|
defer func() {
|
|
|
|
// Wait for all background tasks to complete
|
|
|
|
if job.closeIO {
|
|
|
|
if err := job.Stdout.Close(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
if err := job.Stderr.Close(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
if err := job.Stdin.Close(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-10-16 19:54:55 -04:00
|
|
|
if job.Eng.IsShutdown() && !job.GetenvBool("overrideShutdown") {
|
2014-08-05 06:00:05 -04:00
|
|
|
return fmt.Errorf("engine is shutdown")
|
|
|
|
}
|
2014-08-13 17:32:20 -04:00
|
|
|
// FIXME: this is a temporary workaround to avoid Engine.Shutdown
|
|
|
|
// waiting 5 seconds for server/api.ServeApi to complete (which it never will)
|
|
|
|
// everytime the daemon is cleanly restarted.
|
|
|
|
// The permanent fix is to implement Job.Stop and Job.OnStop so that
|
|
|
|
// ServeApi can cooperate and terminate cleanly.
|
|
|
|
if job.Name != "serveapi" {
|
|
|
|
job.Eng.l.Lock()
|
|
|
|
job.Eng.tasks.Add(1)
|
|
|
|
job.Eng.l.Unlock()
|
|
|
|
defer job.Eng.tasks.Done()
|
|
|
|
}
|
2013-11-20 02:37:03 -05:00
|
|
|
// FIXME: make this thread-safe
|
|
|
|
// FIXME: implement wait
|
|
|
|
if !job.end.IsZero() {
|
|
|
|
return fmt.Errorf("%s: job has already completed", job.Name)
|
2013-10-27 22:20:00 -04:00
|
|
|
}
|
2013-11-20 02:37:03 -05:00
|
|
|
// Log beginning and end of the job
|
2014-10-24 14:20:17 -04:00
|
|
|
if job.Eng.Logging {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Infof("+job %s", job.CallString())
|
2014-10-24 14:20:17 -04:00
|
|
|
defer func() {
|
2015-03-26 05:48:07 -04:00
|
|
|
okerr := "OK"
|
|
|
|
if err != nil {
|
|
|
|
okerr = fmt.Sprintf("ERR: %s", err)
|
|
|
|
}
|
|
|
|
logrus.Infof("-job %s %s", job.CallString(), okerr)
|
2014-10-24 14:20:17 -04:00
|
|
|
}()
|
|
|
|
}
|
2015-03-26 05:48:07 -04:00
|
|
|
|
2013-10-21 12:04:42 -04:00
|
|
|
if job.handler == nil {
|
2015-03-26 05:48:07 -04:00
|
|
|
return fmt.Errorf("%s: command not found", job.Name)
|
2014-05-03 23:29:55 -04:00
|
|
|
}
|
2014-09-16 02:43:43 -04:00
|
|
|
|
2015-03-26 05:48:07 -04:00
|
|
|
var errorMessage = bytes.NewBuffer(nil)
|
|
|
|
job.Stderr.Add(errorMessage)
|
|
|
|
|
|
|
|
err = job.handler(job)
|
|
|
|
job.end = time.Now()
|
|
|
|
|
|
|
|
return
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
|
|
|
|
2013-10-27 22:20:00 -04:00
|
|
|
func (job *Job) CallString() string {
|
|
|
|
return fmt.Sprintf("%s(%s)", job.Name, strings.Join(job.Args, ", "))
|
|
|
|
}
|
|
|
|
|
2014-02-15 18:05:28 -05:00
|
|
|
func (job *Job) Env() *Env {
|
|
|
|
return job.env
|
|
|
|
}
|
|
|
|
|
2014-01-30 21:21:59 -05:00
|
|
|
func (job *Job) EnvExists(key string) (value bool) {
|
|
|
|
return job.env.Exists(key)
|
|
|
|
}
|
|
|
|
|
2013-10-21 12:04:42 -04:00
|
|
|
func (job *Job) Getenv(key string) (value string) {
|
2013-12-08 01:06:05 -05:00
|
|
|
return job.env.Get(key)
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (job *Job) GetenvBool(key string) (value bool) {
|
2013-12-08 01:06:05 -05:00
|
|
|
return job.env.GetBool(key)
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (job *Job) SetenvBool(key string, value bool) {
|
2013-12-08 01:06:05 -05:00
|
|
|
job.env.SetBool(key, value)
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
|
|
|
|
2015-03-10 14:25:47 -04:00
|
|
|
func (job *Job) GetenvTime(key string) (value time.Time, err error) {
|
|
|
|
return job.env.GetTime(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (job *Job) SetenvTime(key string, value time.Time) {
|
|
|
|
job.env.SetTime(key, value)
|
|
|
|
}
|
|
|
|
|
2014-01-27 22:26:24 -05:00
|
|
|
func (job *Job) GetenvSubEnv(key string) *Env {
|
|
|
|
return job.env.GetSubEnv(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (job *Job) SetenvSubEnv(key string, value *Env) error {
|
|
|
|
return job.env.SetSubEnv(key, value)
|
|
|
|
}
|
|
|
|
|
2013-12-12 16:35:50 -05:00
|
|
|
func (job *Job) GetenvInt64(key string) int64 {
|
|
|
|
return job.env.GetInt64(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (job *Job) GetenvInt(key string) int {
|
2013-12-08 01:06:05 -05:00
|
|
|
return job.env.GetInt(key)
|
2013-10-27 22:20:00 -04:00
|
|
|
}
|
|
|
|
|
2013-12-12 16:35:50 -05:00
|
|
|
func (job *Job) SetenvInt64(key string, value int64) {
|
|
|
|
job.env.SetInt64(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (job *Job) SetenvInt(key string, value int) {
|
2013-12-08 01:06:05 -05:00
|
|
|
job.env.SetInt(key, value)
|
2013-10-27 22:20:00 -04:00
|
|
|
}
|
|
|
|
|
2013-11-18 20:05:05 -05:00
|
|
|
// Returns nil if key not found
|
2013-10-21 12:04:42 -04:00
|
|
|
func (job *Job) GetenvList(key string) []string {
|
2013-12-08 01:06:05 -05:00
|
|
|
return job.env.GetList(key)
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
|
|
|
|
2013-12-13 18:01:54 -05:00
|
|
|
func (job *Job) GetenvJson(key string, iface interface{}) error {
|
|
|
|
return job.env.GetJson(key, iface)
|
2013-12-11 20:03:48 -05:00
|
|
|
}
|
|
|
|
|
2013-11-14 01:08:08 -05:00
|
|
|
func (job *Job) SetenvJson(key string, value interface{}) error {
|
2013-12-08 01:06:05 -05:00
|
|
|
return job.env.SetJson(key, value)
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
|
|
|
|
2013-11-14 01:08:08 -05:00
|
|
|
func (job *Job) SetenvList(key string, value []string) error {
|
2013-12-08 01:06:05 -05:00
|
|
|
return job.env.SetJson(key, value)
|
2013-11-14 01:08:08 -05:00
|
|
|
}
|
|
|
|
|
2013-10-21 12:04:42 -04:00
|
|
|
func (job *Job) Setenv(key, value string) {
|
2013-12-08 01:06:05 -05:00
|
|
|
job.env.Set(key, value)
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
2013-10-26 22:24:01 -04:00
|
|
|
|
|
|
|
// DecodeEnv decodes `src` as a json dictionary, and adds
|
|
|
|
// each decoded key-value pair to the environment.
|
|
|
|
//
|
2013-11-18 20:05:05 -05:00
|
|
|
// If `src` cannot be decoded as a json dictionary, an error
|
2013-10-26 22:24:01 -04:00
|
|
|
// is returned.
|
|
|
|
func (job *Job) DecodeEnv(src io.Reader) error {
|
2013-12-08 01:06:05 -05:00
|
|
|
return job.env.Decode(src)
|
2013-10-26 22:24:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (job *Job) EncodeEnv(dst io.Writer) error {
|
2013-12-08 01:06:05 -05:00
|
|
|
return job.env.Encode(dst)
|
2013-10-26 22:24:01 -04:00
|
|
|
}
|
|
|
|
|
2013-10-27 22:20:00 -04:00
|
|
|
func (job *Job) ImportEnv(src interface{}) (err error) {
|
2013-12-08 01:06:05 -05:00
|
|
|
return job.env.Import(src)
|
2013-10-26 22:24:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (job *Job) Environ() map[string]string {
|
2013-12-08 01:06:05 -05:00
|
|
|
return job.env.Map()
|
2013-10-26 22:24:01 -04:00
|
|
|
}
|
2013-10-27 03:01:15 -04:00
|
|
|
|
2013-10-27 22:20:00 -04:00
|
|
|
func (job *Job) Printf(format string, args ...interface{}) (n int, err error) {
|
|
|
|
return fmt.Fprintf(job.Stdout, format, args...)
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:12:36 -04:00
|
|
|
func (job *Job) Errorf(format string, args ...interface{}) (n int, err error) {
|
|
|
|
return fmt.Fprintf(job.Stderr, format, args...)
|
|
|
|
}
|
|
|
|
|
2014-09-16 02:43:43 -04:00
|
|
|
func (job *Job) SetCloseIO(val bool) {
|
|
|
|
job.closeIO = val
|
|
|
|
}
|
2015-03-10 18:10:00 -04:00
|
|
|
|
|
|
|
// When called, causes the Job.WaitCancelled channel to unblock.
|
|
|
|
func (job *Job) Cancel() {
|
|
|
|
job.cancelOnce.Do(func() {
|
|
|
|
close(job.cancelled)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a channel which is closed ("never blocks") when the job is cancelled.
|
|
|
|
func (job *Job) WaitCancelled() <-chan struct{} {
|
|
|
|
return job.cancelled
|
|
|
|
}
|