2014-04-24 03:46:32 -04:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/dotcloud/docker/pkg/beam"
|
|
|
|
"github.com/dotcloud/docker/pkg/beam/data"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Sender struct {
|
|
|
|
beam.Sender
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSender(s beam.Sender) *Sender {
|
|
|
|
return &Sender{s}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sender) Install(eng *Engine) error {
|
|
|
|
// FIXME: this doesn't exist yet.
|
|
|
|
eng.RegisterCatchall(s.Handle)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sender) Handle(job *Job) Status {
|
2014-05-09 20:10:33 -04:00
|
|
|
cmd := append([]string{job.Name}, job.Args...)
|
|
|
|
env := data.Encode(job.Env().MultiMap())
|
|
|
|
msg := data.Empty().Set("cmd", cmd...).Set("env", env)
|
2014-04-24 03:46:32 -04:00
|
|
|
peer, err := beam.SendConn(s, msg.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return job.Errorf("beamsend: %v", err)
|
|
|
|
}
|
|
|
|
defer peer.Close()
|
|
|
|
var tasks sync.WaitGroup
|
|
|
|
defer tasks.Wait()
|
|
|
|
r := beam.NewRouter(nil)
|
|
|
|
r.NewRoute().KeyStartsWith("cmd", "log", "stdout").HasAttachment().Handler(func(p []byte, stdout *os.File) error {
|
|
|
|
tasks.Add(1)
|
2014-05-03 23:29:55 -04:00
|
|
|
go func() {
|
|
|
|
io.Copy(job.Stdout, stdout)
|
|
|
|
stdout.Close()
|
|
|
|
tasks.Done()
|
|
|
|
}()
|
2014-04-24 03:46:32 -04:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
r.NewRoute().KeyStartsWith("cmd", "log", "stderr").HasAttachment().Handler(func(p []byte, stderr *os.File) error {
|
|
|
|
tasks.Add(1)
|
2014-05-03 23:29:55 -04:00
|
|
|
go func() {
|
|
|
|
io.Copy(job.Stderr, stderr)
|
|
|
|
stderr.Close()
|
|
|
|
tasks.Done()
|
|
|
|
}()
|
2014-04-24 03:46:32 -04:00
|
|
|
return nil
|
|
|
|
})
|
2014-04-25 19:47:03 -04:00
|
|
|
r.NewRoute().KeyStartsWith("cmd", "log", "stdin").HasAttachment().Handler(func(p []byte, stdin *os.File) error {
|
2014-05-03 23:29:55 -04:00
|
|
|
go func() {
|
|
|
|
io.Copy(stdin, job.Stdin)
|
|
|
|
stdin.Close()
|
|
|
|
}()
|
2014-04-25 19:47:03 -04:00
|
|
|
return nil
|
|
|
|
})
|
2014-04-24 03:46:32 -04:00
|
|
|
var status int
|
|
|
|
r.NewRoute().KeyStartsWith("cmd", "status").Handler(func(p []byte, f *os.File) error {
|
|
|
|
cmd := data.Message(p).Get("cmd")
|
2014-04-26 21:24:39 -04:00
|
|
|
if len(cmd) != 2 {
|
2014-04-24 03:46:32 -04:00
|
|
|
return fmt.Errorf("usage: %s <0-127>", cmd[0])
|
|
|
|
}
|
2014-04-26 21:24:39 -04:00
|
|
|
s, err := strconv.ParseUint(cmd[1], 10, 8)
|
2014-04-24 03:46:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("usage: %s <0-127>", cmd[0])
|
|
|
|
}
|
|
|
|
status = int(s)
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
if _, err := beam.Copy(r, peer); err != nil {
|
|
|
|
return job.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
return Status(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Receiver struct {
|
|
|
|
*Engine
|
|
|
|
peer beam.Receiver
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewReceiver(peer beam.Receiver) *Receiver {
|
|
|
|
return &Receiver{Engine: New(), peer: peer}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rcv *Receiver) Run() error {
|
|
|
|
r := beam.NewRouter(nil)
|
|
|
|
r.NewRoute().KeyExists("cmd").Handler(func(p []byte, f *os.File) error {
|
|
|
|
// Use the attachment as a beam return channel
|
|
|
|
peer, err := beam.FileConn(f)
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
return err
|
|
|
|
}
|
2014-05-03 20:21:53 -04:00
|
|
|
f.Close()
|
|
|
|
defer peer.Close()
|
2014-05-09 20:10:33 -04:00
|
|
|
msg := data.Message(p)
|
|
|
|
cmd := msg.Get("cmd")
|
2014-04-24 03:46:32 -04:00
|
|
|
job := rcv.Engine.Job(cmd[0], cmd[1:]...)
|
2014-05-09 20:10:33 -04:00
|
|
|
// Decode env
|
|
|
|
env, err := data.Decode(msg.GetOne("env"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error decoding 'env': %v", err)
|
|
|
|
}
|
|
|
|
job.Env().InitMultiMap(env)
|
2014-05-09 18:39:55 -04:00
|
|
|
stdout, err := beam.SendRPipe(peer, data.Empty().Set("cmd", "log", "stdout").Bytes())
|
2014-04-24 03:46:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
job.Stdout.Add(stdout)
|
2014-05-09 18:39:55 -04:00
|
|
|
stderr, err := beam.SendRPipe(peer, data.Empty().Set("cmd", "log", "stderr").Bytes())
|
2014-04-24 03:46:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
job.Stderr.Add(stderr)
|
2014-05-09 18:39:55 -04:00
|
|
|
stdin, err := beam.SendWPipe(peer, data.Empty().Set("cmd", "log", "stdin").Bytes())
|
2014-04-25 19:47:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
job.Stdin.Add(stdin)
|
2014-04-24 03:46:32 -04:00
|
|
|
// ignore error because we pass the raw status
|
|
|
|
job.Run()
|
|
|
|
err = peer.Send(data.Empty().Set("cmd", "status", fmt.Sprintf("%d", job.status)).Bytes(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
_, err := beam.Copy(r, rcv.peer)
|
|
|
|
return err
|
|
|
|
}
|