2013-10-21 12:04:42 -04:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2013-11-13 14:25:55 -05:00
|
|
|
"fmt"
|
2013-11-07 15:19:24 -05:00
|
|
|
"io"
|
|
|
|
"strings"
|
2013-11-20 02:37:03 -05:00
|
|
|
"time"
|
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,
|
|
|
|
// environment variables, standard streams for input, output and error, and
|
|
|
|
// an exit status which can indicate success (0) or error (anything else).
|
|
|
|
//
|
|
|
|
// One slight variation is that jobs report their status as a string. The
|
|
|
|
// string "0" indicates success, and any other strings indicates an error.
|
|
|
|
// This allows for richer error reporting.
|
2013-11-07 15:19:24 -05:00
|
|
|
//
|
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
|
|
|
|
status Status
|
|
|
|
end time.Time
|
2013-11-13 14:25:55 -05:00
|
|
|
onExit []func()
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
|
|
|
|
2013-11-20 02:37:03 -05:00
|
|
|
type Status int
|
|
|
|
|
|
|
|
const (
|
|
|
|
StatusOK Status = 0
|
|
|
|
StatusErr Status = 1
|
|
|
|
StatusNotFound Status = 127
|
|
|
|
)
|
|
|
|
|
2013-10-21 12:04:42 -04:00
|
|
|
// Run executes the job and blocks until the job completes.
|
|
|
|
// If the job returns a failure status, an error is returned
|
|
|
|
// which includes the status.
|
|
|
|
func (job *Job) Run() error {
|
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
|
2013-10-27 22:20:00 -04:00
|
|
|
job.Eng.Logf("+job %s", job.CallString())
|
|
|
|
defer func() {
|
|
|
|
job.Eng.Logf("-job %s%s", job.CallString(), job.StatusString())
|
2013-10-27 03:01:15 -04:00
|
|
|
}()
|
2013-11-20 02:37:03 -05:00
|
|
|
var errorMessage string
|
|
|
|
job.Stderr.AddString(&errorMessage)
|
2013-10-21 12:04:42 -04:00
|
|
|
if job.handler == nil {
|
2013-11-20 02:37:03 -05:00
|
|
|
job.Errorf("%s: command not found", job.Name)
|
|
|
|
job.status = 127
|
2013-10-26 20:17:45 -04:00
|
|
|
} else {
|
|
|
|
job.status = job.handler(job)
|
2013-11-20 02:37:03 -05:00
|
|
|
job.end = time.Now()
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
2013-11-20 02:37:03 -05:00
|
|
|
// Wait for all background tasks to complete
|
|
|
|
if err := job.Stdout.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := job.Stderr.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if job.status != 0 {
|
|
|
|
return fmt.Errorf("%s: %s", job.Name, errorMessage)
|
2013-10-21 12:04:42 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-27 22:20:00 -04:00
|
|
|
func (job *Job) CallString() string {
|
|
|
|
return fmt.Sprintf("%s(%s)", job.Name, strings.Join(job.Args, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (job *Job) StatusString() string {
|
2013-11-20 02:37:03 -05:00
|
|
|
// If the job hasn't completed, status string is empty
|
|
|
|
if job.end.IsZero() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var okerr string
|
|
|
|
if job.status == StatusOK {
|
|
|
|
okerr = "OK"
|
|
|
|
} else {
|
|
|
|
okerr = "ERR"
|
2013-10-27 03:01:15 -04:00
|
|
|
}
|
2013-11-20 02:37:03 -05:00
|
|
|
return fmt.Sprintf(" = %s (%d)", okerr, job.status)
|
2013-10-27 22:20:00 -04:00
|
|
|
}
|
|
|
|
|
2013-10-26 20:17:45 -04:00
|
|
|
// String returns a human-readable description of `job`
|
|
|
|
func (job *Job) String() string {
|
2013-10-27 22:20:00 -04:00
|
|
|
return fmt.Sprintf("%s.%s%s", job.Eng, job.CallString(), job.StatusString())
|
2013-10-26 20:17:45 -04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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 03:15:22 -04:00
|
|
|
func (job *Job) ExportEnv(dst interface{}) (err error) {
|
2013-12-08 01:06:05 -05:00
|
|
|
return job.env.Export(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
|
|
|
|
|
|
|
func (job *Job) Logf(format string, args ...interface{}) (n int, err error) {
|
|
|
|
prefixedFormat := fmt.Sprintf("[%s] %s\n", job, strings.TrimRight(format, "\n"))
|
2013-10-27 22:20:00 -04:00
|
|
|
return fmt.Fprintf(job.Stderr, prefixedFormat, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (job *Job) Printf(format string, args ...interface{}) (n int, err error) {
|
|
|
|
return fmt.Fprintf(job.Stdout, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (job *Job) Errorf(format string, args ...interface{}) (n int, err error) {
|
|
|
|
return fmt.Fprintf(job.Stderr, format, args...)
|
2013-11-20 02:37:03 -05:00
|
|
|
}
|
2013-10-27 22:20:00 -04:00
|
|
|
|
2013-11-20 02:37:03 -05:00
|
|
|
func (job *Job) Error(err error) (int, error) {
|
|
|
|
return fmt.Fprintf(job.Stderr, "%s", err)
|
2013-10-27 03:01:15 -04:00
|
|
|
}
|