2014-03-22 00:30:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-03-22 02:23:13 +00:00
|
|
|
"io"
|
2014-03-22 00:30:15 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2014-03-25 17:14:52 +00:00
|
|
|
"os/exec"
|
2014-03-22 01:18:42 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/dockerscript"
|
2014-03-22 02:23:13 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/beam"
|
2014-03-25 00:32:26 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/beam/data"
|
2014-03-24 03:46:31 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/term"
|
2014-03-22 02:23:13 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"net"
|
|
|
|
"path"
|
2014-03-22 03:54:05 +00:00
|
|
|
"bufio"
|
|
|
|
"strconv"
|
2014-03-22 00:30:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2014-03-22 02:23:13 +00:00
|
|
|
client, engine, err := beam.USocketPair()
|
|
|
|
if err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
}
|
2014-03-24 03:46:31 +00:00
|
|
|
defer client.Close()
|
2014-03-22 02:23:13 +00:00
|
|
|
go func() {
|
2014-03-24 03:46:31 +00:00
|
|
|
Serve(engine, builtinsHandler)
|
2014-03-24 23:21:55 +00:00
|
|
|
Debugf("Shutting down engine\n")
|
2014-03-22 02:23:13 +00:00
|
|
|
engine.Close()
|
|
|
|
}()
|
2014-03-24 03:46:31 +00:00
|
|
|
if term.IsTerminal(0) {
|
|
|
|
input := bufio.NewScanner(os.Stdin)
|
|
|
|
for {
|
|
|
|
os.Stdout.Write([]byte("beamsh> "))
|
|
|
|
if !input.Scan() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
line := input.Text()
|
|
|
|
if len(line) != 0 {
|
|
|
|
cmd, err := dockerscript.Parse(strings.NewReader(line))
|
|
|
|
if err != nil {
|
2014-03-24 23:21:55 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
2014-03-24 03:46:31 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
executeScript(client, cmd)
|
|
|
|
}
|
|
|
|
if err := input.Err(); err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
script, err := dockerscript.Parse(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
Fatal("parse error: %v\n", err)
|
|
|
|
}
|
|
|
|
executeScript(client, script)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeScript(client *net.UnixConn, script []*dockerscript.Command) {
|
2014-03-24 23:21:55 +00:00
|
|
|
Debugf("%d commands:\n", len(script))
|
2014-03-22 02:23:13 +00:00
|
|
|
for _, cmd := range script {
|
2014-03-25 00:32:26 +00:00
|
|
|
job, err := beam.SendPipe(client, data.Empty().Set("cmd", cmd.Args...).Bytes())
|
2014-03-22 02:23:13 +00:00
|
|
|
if err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
}
|
2014-03-25 01:18:00 +00:00
|
|
|
// Recursively execute child-commands as commands to the new job
|
|
|
|
// executeScript blocks until commands are done, so this is depth-first recursion.
|
|
|
|
executeScript(job, cmd.Children)
|
2014-03-24 03:46:31 +00:00
|
|
|
// TODO: pass a default handler to deal with 'status'
|
|
|
|
// --> use beam chaining?
|
2014-03-25 21:10:30 +00:00
|
|
|
Debugf("[%s] Listening for reply messages\n", strings.Join(cmd.Args, " "))
|
2014-03-24 03:46:31 +00:00
|
|
|
Serve(job, builtinsHandler)
|
2014-03-25 21:10:30 +00:00
|
|
|
Debugf("[%s] done listening for reply messages\n", strings.Join(cmd.Args, " "))
|
2014-03-22 02:23:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 03:54:05 +00:00
|
|
|
func parseMsgPayload(payload []byte) ([]string, error) {
|
2014-03-25 00:32:26 +00:00
|
|
|
msg, err := data.Decode(string(payload))
|
2014-03-22 03:54:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-25 00:32:26 +00:00
|
|
|
var cmd []string
|
|
|
|
if c, exists := msg["cmd"]; exists {
|
|
|
|
cmd = c
|
|
|
|
}
|
|
|
|
if len(cmd) == 0 {
|
2014-03-22 03:54:05 +00:00
|
|
|
return nil, fmt.Errorf("empty command")
|
|
|
|
}
|
2014-03-25 00:32:26 +00:00
|
|
|
return cmd, nil
|
2014-03-22 03:54:05 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 02:23:13 +00:00
|
|
|
func CmdCat(args []string, f *os.File) {
|
|
|
|
for _, name := range args[1:] {
|
|
|
|
f, err := os.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
io.Copy(os.Stdout, f)
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CmdEcho(args []string, f *os.File) {
|
2014-03-22 03:54:05 +00:00
|
|
|
resp, err := beam.FdConn(int(f.Fd()))
|
|
|
|
if err != nil {
|
2014-03-24 03:45:44 +00:00
|
|
|
Fatal(err)
|
2014-03-22 03:54:05 +00:00
|
|
|
return
|
|
|
|
}
|
2014-03-24 03:45:44 +00:00
|
|
|
defer resp.Close()
|
2014-03-22 03:54:05 +00:00
|
|
|
r, w, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-03-24 23:21:55 +00:00
|
|
|
Debugf("[CmdEcho] stdout pipe() r=%d w=%d\n", r.Fd(), w.Fd())
|
2014-03-25 00:32:26 +00:00
|
|
|
if err := beam.Send(resp, data.Empty().Set("cmd", "log", "stdout").Bytes(), r); err != nil {
|
2014-03-22 03:54:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, strings.Join(args[1:], " "))
|
|
|
|
w.Close()
|
2014-03-22 02:23:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 03:54:05 +00:00
|
|
|
func CmdExit(args []string, f *os.File) {
|
|
|
|
var status int
|
|
|
|
if len(args) > 1 {
|
|
|
|
val, err := strconv.ParseInt(args[1], 10, 32)
|
|
|
|
if err == nil {
|
|
|
|
status = int(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os.Exit(status)
|
|
|
|
}
|
|
|
|
|
2014-03-24 03:46:31 +00:00
|
|
|
// 'status' is a notification of a job's status.
|
|
|
|
//
|
|
|
|
func parseEnv(args []string) ([]string, map[string]string) {
|
|
|
|
var argsOut []string
|
|
|
|
env := make(map[string]string)
|
|
|
|
for _, word := range args[1:] {
|
|
|
|
if strings.Contains(word, "=") {
|
|
|
|
kv := strings.SplitN(word, "=", 2)
|
|
|
|
key := kv[0]
|
|
|
|
var val string
|
|
|
|
if len(kv) == 2 {
|
|
|
|
val = kv[1]
|
|
|
|
}
|
|
|
|
env[key] = val
|
|
|
|
} else {
|
|
|
|
argsOut = append(argsOut, word)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return argsOut, env
|
|
|
|
}
|
|
|
|
|
2014-03-25 01:18:00 +00:00
|
|
|
func CmdTrace(args []string, f *os.File) {
|
|
|
|
resp, err := beam.FdConn(int(f.Fd()))
|
|
|
|
if err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Close()
|
|
|
|
for {
|
|
|
|
payload, attachment, err := beam.Receive(resp)
|
|
|
|
if err != nil {
|
|
|
|
Logf("[trace] error waiting for message\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msg, err := data.Decode(string(payload))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("===> %s\n", payload)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("===> %v\n", msg)
|
|
|
|
}
|
|
|
|
if err := beam.Send(resp, payload, attachment); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-25 17:14:52 +00:00
|
|
|
func CmdExec(args []string, f *os.File) {
|
|
|
|
resp, err := beam.FdConn(int(f.Fd()))
|
|
|
|
if err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Close()
|
|
|
|
cmd := exec.Command(args[1], args[2:]...)
|
|
|
|
Logf("EXEC %s %s\n", cmd.Path, cmd.Args)
|
|
|
|
stdoutR, stdoutW, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cmd.Stdout = stdoutW
|
|
|
|
stderrR, stderrW, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cmd.Stderr = stderrW
|
|
|
|
if err := beam.Send(resp, data.Empty().Set("cmd", "log", "stdout").Bytes(), stdoutR); err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
}
|
|
|
|
if err := beam.Send(resp, data.Empty().Set("cmd", "log", "stderr").Bytes(), stderrR); err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
}
|
|
|
|
cmd.Run()
|
|
|
|
}
|
|
|
|
|
2014-03-22 03:54:05 +00:00
|
|
|
func CmdLog(args []string, f *os.File) {
|
2014-03-24 23:21:55 +00:00
|
|
|
defer Debugf("CmdLog done\n")
|
2014-03-25 01:17:16 +00:00
|
|
|
var name string
|
|
|
|
if len(args) > 0 {
|
|
|
|
name = args[1]
|
|
|
|
}
|
2014-03-22 03:54:05 +00:00
|
|
|
input := bufio.NewScanner(f)
|
|
|
|
for input.Scan() {
|
|
|
|
line := input.Text()
|
|
|
|
if len(line) > 0 {
|
|
|
|
fmt.Printf("[%s] %s\n", name, line)
|
|
|
|
}
|
|
|
|
if err := input.Err(); err != nil {
|
|
|
|
fmt.Printf("[%s:%s]\n", name, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 02:23:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 17:14:27 +00:00
|
|
|
type Msg struct {
|
|
|
|
payload []byte
|
|
|
|
attachment *os.File
|
|
|
|
}
|
|
|
|
|
2014-03-24 03:46:31 +00:00
|
|
|
func Serve(endpoint *net.UnixConn, handler func([]string, *os.File)) error {
|
2014-03-24 23:21:55 +00:00
|
|
|
Debugf("[Serve %#v]\n", handler)
|
|
|
|
defer Debugf("[Serve %#v] done\n", handler)
|
2014-03-22 02:23:13 +00:00
|
|
|
var tasks sync.WaitGroup
|
|
|
|
defer tasks.Wait()
|
2014-03-25 17:14:27 +00:00
|
|
|
in := make(chan *Msg)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
Debugf("[Serve] waiting for next message on endpoint...\n")
|
|
|
|
payload, attachment, err := beam.Receive(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
in<-&Msg{payload, attachment}
|
2014-03-22 02:23:13 +00:00
|
|
|
}
|
2014-03-25 17:14:27 +00:00
|
|
|
Debugf("[Serve] endpoint closed. Waiting for tasks to complete\n")
|
|
|
|
tasks.Wait()
|
|
|
|
Debugf("[Serve] endpoint closed AND tasks complete\n")
|
|
|
|
close(in)
|
|
|
|
}()
|
|
|
|
for msg := range in {
|
2014-03-22 02:23:13 +00:00
|
|
|
tasks.Add(1)
|
2014-03-24 03:46:31 +00:00
|
|
|
// Handle new message
|
2014-03-22 02:23:13 +00:00
|
|
|
go func(payload []byte, attachment *os.File) {
|
2014-03-24 23:21:55 +00:00
|
|
|
Debugf("---> Handling '%s' [fd=%d]\n", payload, attachment.Fd())
|
2014-03-22 02:23:13 +00:00
|
|
|
defer tasks.Done()
|
2014-03-22 03:54:05 +00:00
|
|
|
args, err := parseMsgPayload(payload)
|
2014-03-22 02:23:13 +00:00
|
|
|
if err != nil {
|
|
|
|
Logf("error parsing beam message: %s\n", err)
|
|
|
|
if attachment != nil {
|
|
|
|
attachment.Close()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2014-03-24 23:21:55 +00:00
|
|
|
Debugf("---> calling handler for '%s'\n", args[0])
|
2014-03-25 17:14:27 +00:00
|
|
|
handlerAttachment := attachment
|
|
|
|
var iotasks sync.WaitGroup
|
|
|
|
if attachment != nil {
|
|
|
|
if caller, err := beam.FdConn(int(attachment.Fd())); err == nil {
|
|
|
|
Debugf("[Serve] message '%s' has a valid beam endpoint as attachment. Setting up indirection\n", payload)
|
|
|
|
defer caller.Close()
|
|
|
|
jobpub, jobpriv, err := beam.USocketPair()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer jobpub.Close()
|
|
|
|
if f, err := jobpriv.File(); err != nil {
|
|
|
|
jobpriv.Close()
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
handlerAttachment = f
|
|
|
|
defer attachment.Close()
|
|
|
|
}
|
|
|
|
jobpriv.Close()
|
|
|
|
// Read messages from the job and re-insert them for handling
|
|
|
|
iotasks.Add(1)
|
|
|
|
go func(job *net.UnixConn) {
|
|
|
|
defer iotasks.Done()
|
|
|
|
for {
|
|
|
|
payload, attachment, err := beam.Receive(job)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var fd int = -1
|
|
|
|
if attachment != nil {
|
|
|
|
fd = int(attachment.Fd())
|
|
|
|
}
|
|
|
|
Debugf("[Serve] received introspection message '%s'[%d]\n", payload, fd)
|
|
|
|
// Send messages back in for introspection
|
|
|
|
// Note that we don't scope introspection: jobs have full access to the
|
|
|
|
// context in which they were called.
|
|
|
|
in <-&Msg{payload, attachment}
|
|
|
|
}
|
|
|
|
}(jobpub)
|
|
|
|
// Read messages from the caller to the job
|
|
|
|
go func(caller *net.UnixConn, job *net.UnixConn) {
|
|
|
|
for {
|
|
|
|
payload, f, err := beam.Receive(caller)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := beam.Send(job, payload, f); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(caller, jobpub)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handler(args, handlerAttachment)
|
2014-03-24 23:21:55 +00:00
|
|
|
Debugf("---> handler returned for '%s'\n", args[0])
|
2014-03-25 17:14:27 +00:00
|
|
|
if handlerAttachment != nil {
|
|
|
|
handlerAttachment.Close()
|
|
|
|
}
|
|
|
|
Debugf("---> waiting for iotasks to complete for '%s'\n", args[0])
|
|
|
|
iotasks.Wait()
|
|
|
|
Debugf("---> iotasks complete for '%s'\n", args[0])
|
|
|
|
}(msg.payload, msg.attachment)
|
2014-03-22 02:23:13 +00:00
|
|
|
}
|
2014-03-25 17:14:27 +00:00
|
|
|
Debugf("[Serve] main serve loop completed\n")
|
2014-03-22 02:23:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-24 03:46:31 +00:00
|
|
|
func builtinsHandler(args []string, attachment *os.File) {
|
|
|
|
if args[0] == "exit" {
|
|
|
|
CmdExit(args, attachment)
|
|
|
|
} else if args[0] == "cat" {
|
|
|
|
CmdCat(args, attachment)
|
|
|
|
} else if args[0] == "echo" {
|
|
|
|
CmdEcho(args, attachment)
|
|
|
|
} else if args[0] == "log" {
|
|
|
|
CmdLog(args, attachment)
|
2014-03-25 01:18:00 +00:00
|
|
|
} else if args[0] == "trace" {
|
|
|
|
CmdTrace(args, attachment)
|
2014-03-25 17:14:52 +00:00
|
|
|
} else if args[0] == "exec" {
|
|
|
|
CmdExec(args, attachment)
|
2014-03-24 03:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 02:23:13 +00:00
|
|
|
|
|
|
|
func Logf(msg string, args ...interface{}) (int, error) {
|
|
|
|
if len(msg) == 0 || msg[len(msg) - 1] != '\n' {
|
|
|
|
msg = msg + "\n"
|
2014-03-22 00:30:15 +00:00
|
|
|
}
|
2014-03-22 02:23:13 +00:00
|
|
|
msg = fmt.Sprintf("[%v] [%v] %s", os.Getpid(), path.Base(os.Args[0]), msg)
|
|
|
|
return fmt.Printf(msg, args...)
|
|
|
|
}
|
|
|
|
|
2014-03-24 23:21:55 +00:00
|
|
|
func Debugf(msg string, args ...interface{}) {
|
|
|
|
if os.Getenv("DEBUG") != "" {
|
|
|
|
Logf(msg, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 02:23:13 +00:00
|
|
|
func Fatalf(msg string, args ...interface{}) {
|
|
|
|
Logf(msg, args)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fatal(args ...interface{}) {
|
|
|
|
Fatalf("%v", args[0])
|
2014-03-22 00:30:15 +00:00
|
|
|
}
|