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-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"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"net"
|
|
|
|
"bytes"
|
|
|
|
"path"
|
2014-03-22 03:54:05 +00:00
|
|
|
"bufio"
|
|
|
|
"strconv"
|
2014-03-22 00:30:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2014-03-22 01:18:42 +00:00
|
|
|
script, err := dockerscript.Parse(os.Stdin)
|
2014-03-22 00:30:15 +00:00
|
|
|
if err != nil {
|
2014-03-22 02:23:13 +00:00
|
|
|
Fatal("parse error: %v\n", err)
|
2014-03-22 00:30:15 +00:00
|
|
|
}
|
2014-03-22 02:23:13 +00:00
|
|
|
Logf("%d commands:\n", len(script))
|
|
|
|
client, engine, err := beam.USocketPair()
|
|
|
|
if err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
Serve(engine)
|
|
|
|
engine.Close()
|
|
|
|
}()
|
|
|
|
for _, cmd := range script {
|
|
|
|
job, err := beam.SendPipe(client, []byte(strings.Join(cmd.Args, " ")))
|
|
|
|
if err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
}
|
2014-03-22 03:54:05 +00:00
|
|
|
Serve(job)
|
2014-03-22 02:23:13 +00:00
|
|
|
Logf("[%s] done\n", strings.Join(cmd.Args, " "))
|
|
|
|
}
|
|
|
|
client.Close()
|
|
|
|
}
|
|
|
|
|
2014-03-22 03:54:05 +00:00
|
|
|
func parseMsgPayload(payload []byte) ([]string, error) {
|
|
|
|
// FIXME: send structured message instead of a text script
|
|
|
|
cmds, err := dockerscript.Parse(bytes.NewReader(payload))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(cmds) == 0 {
|
|
|
|
return nil, fmt.Errorf("empty command")
|
|
|
|
}
|
|
|
|
// We don't care about multiple commands or children
|
|
|
|
return cmds[0].Args, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if err := beam.Send(resp, []byte("log stdout"), r); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, strings.Join(args[1:], " "))
|
|
|
|
fmt.Printf("waiting 5 seconds...\n")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CmdLog(args []string, f *os.File) {
|
|
|
|
name := args[1]
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func Serve(endpoint *net.UnixConn) error {
|
2014-03-22 03:54:05 +00:00
|
|
|
Logf("[Serve]\n")
|
|
|
|
defer Logf("[Serve] done\n")
|
2014-03-22 02:23:13 +00:00
|
|
|
var tasks sync.WaitGroup
|
|
|
|
defer tasks.Wait()
|
|
|
|
for {
|
2014-03-22 03:54:05 +00:00
|
|
|
Logf("[Serve] waiting for next message...\n")
|
2014-03-22 02:23:13 +00:00
|
|
|
payload, attachment, err := beam.Receive(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tasks.Add(1)
|
|
|
|
go func(payload []byte, attachment *os.File) {
|
|
|
|
defer tasks.Done()
|
2014-03-22 03:54:05 +00:00
|
|
|
if attachment != nil {
|
|
|
|
defer fmt.Printf("closing request attachment %d\n", attachment.Fd())
|
|
|
|
attachment.Close()
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
Logf("beam message: %v\n", args)
|
2014-03-22 03:54:05 +00:00
|
|
|
if args[0] == "exit" {
|
|
|
|
CmdExit(args, attachment)
|
2014-03-22 02:23:13 +00:00
|
|
|
} else if args[0] == "cat" {
|
|
|
|
CmdCat(args, attachment)
|
|
|
|
} else if args[0] == "echo" {
|
|
|
|
CmdEcho(args, attachment)
|
2014-03-22 03:54:05 +00:00
|
|
|
} else if args[0] == "log" {
|
|
|
|
CmdLog(args, attachment)
|
2014-03-22 02:23:13 +00:00
|
|
|
}
|
|
|
|
}(payload, attachment)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|