2014-03-22 00:30:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-03-26 02:50:44 +00:00
|
|
|
"bufio"
|
2014-04-22 23:09:42 +00:00
|
|
|
"flag"
|
2014-03-22 00:30:15 +00:00
|
|
|
"fmt"
|
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-26 02:50:44 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/dockerscript"
|
2014-03-24 03:46:31 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/term"
|
2014-03-26 02:50:44 +00:00
|
|
|
"io"
|
2014-03-22 02:23:13 +00:00
|
|
|
"net"
|
2014-03-26 02:50:44 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2014-03-22 02:23:13 +00:00
|
|
|
"path"
|
2014-03-26 02:50:44 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2014-03-22 00:30:15 +00:00
|
|
|
)
|
|
|
|
|
2014-03-28 07:01:35 +00:00
|
|
|
var rootPlugins = []string{
|
|
|
|
"stdio",
|
|
|
|
}
|
|
|
|
|
2014-03-28 17:15:20 +00:00
|
|
|
var (
|
2014-04-22 23:09:42 +00:00
|
|
|
flX bool
|
|
|
|
flPing bool
|
2014-04-03 04:58:47 +00:00
|
|
|
introspect beam.ReceiveSender = beam.Devnull()
|
2014-03-28 17:15:20 +00:00
|
|
|
)
|
|
|
|
|
2014-03-22 00:30:15 +00:00
|
|
|
func main() {
|
2014-04-03 04:58:47 +00:00
|
|
|
fd3 := os.NewFile(3, "beam-introspect")
|
|
|
|
if introsp, err := beam.FileConn(fd3); err == nil {
|
|
|
|
introspect = introsp
|
|
|
|
Logf("introspection enabled\n")
|
|
|
|
} else {
|
|
|
|
Logf("introspection disabled\n")
|
|
|
|
}
|
|
|
|
fd3.Close()
|
2014-03-28 17:15:20 +00:00
|
|
|
flag.BoolVar(&flX, "x", false, "print commands as they are being executed")
|
|
|
|
flag.Parse()
|
2014-04-22 23:09:42 +00:00
|
|
|
if flag.NArg() == 0 {
|
2014-03-27 22:54:18 +00:00
|
|
|
if term.IsTerminal(0) {
|
2014-03-28 22:42:57 +00:00
|
|
|
// No arguments, stdin is terminal --> interactive mode
|
2014-03-27 22:54:18 +00:00
|
|
|
input := bufio.NewScanner(os.Stdin)
|
|
|
|
for {
|
2014-04-03 04:58:47 +00:00
|
|
|
fmt.Printf("[%d] beamsh> ", os.Getpid())
|
2014-03-27 22:54:18 +00:00
|
|
|
if !input.Scan() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
line := input.Text()
|
|
|
|
if len(line) != 0 {
|
|
|
|
cmd, err := dockerscript.Parse(strings.NewReader(line))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
2014-03-28 07:01:35 +00:00
|
|
|
if err := executeRootScript(cmd); err != nil {
|
2014-03-27 22:54:18 +00:00
|
|
|
Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := input.Err(); err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
Fatal(err)
|
2014-03-24 03:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-27 22:54:18 +00:00
|
|
|
} else {
|
2014-03-28 22:42:57 +00:00
|
|
|
// No arguments, stdin not terminal --> batch mode
|
2014-03-27 22:54:18 +00:00
|
|
|
script, err := dockerscript.Parse(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
Fatal("parse error: %v\n", err)
|
|
|
|
}
|
2014-03-28 07:01:35 +00:00
|
|
|
if err := executeRootScript(script); err != nil {
|
2014-03-24 03:46:31 +00:00
|
|
|
Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2014-03-28 22:42:57 +00:00
|
|
|
// 1+ arguments: parse them as script files
|
2014-03-28 17:15:20 +00:00
|
|
|
for _, scriptpath := range flag.Args() {
|
2014-03-27 22:54:18 +00:00
|
|
|
f, err := os.Open(scriptpath)
|
|
|
|
if err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
}
|
|
|
|
script, err := dockerscript.Parse(f)
|
|
|
|
if err != nil {
|
|
|
|
Fatal("parse error: %v\n", err)
|
|
|
|
}
|
2014-03-28 07:01:35 +00:00
|
|
|
if err := executeRootScript(script); err != nil {
|
2014-03-27 22:54:18 +00:00
|
|
|
Fatal(err)
|
|
|
|
}
|
2014-03-24 03:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 07:01:35 +00:00
|
|
|
func executeRootScript(script []*dockerscript.Command) error {
|
|
|
|
if len(rootPlugins) > 0 {
|
2014-03-28 23:57:48 +00:00
|
|
|
// If there are root plugins, wrap the script inside them
|
2014-03-28 07:01:35 +00:00
|
|
|
var (
|
|
|
|
rootCmd *dockerscript.Command
|
|
|
|
lastCmd *dockerscript.Command
|
|
|
|
)
|
|
|
|
for _, plugin := range rootPlugins {
|
|
|
|
pluginCmd := &dockerscript.Command{
|
|
|
|
Args: []string{plugin},
|
|
|
|
}
|
|
|
|
if rootCmd == nil {
|
|
|
|
rootCmd = pluginCmd
|
|
|
|
} else {
|
|
|
|
lastCmd.Children = []*dockerscript.Command{pluginCmd}
|
|
|
|
}
|
|
|
|
lastCmd = pluginCmd
|
|
|
|
}
|
|
|
|
lastCmd.Children = script
|
|
|
|
script = []*dockerscript.Command{rootCmd}
|
|
|
|
}
|
2014-04-03 04:58:47 +00:00
|
|
|
handlers, err := Handlers(introspect)
|
2014-03-31 19:04:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer handlers.Close()
|
2014-04-03 04:58:47 +00:00
|
|
|
var tasks sync.WaitGroup
|
|
|
|
defer func() {
|
|
|
|
Debugf("Waiting for introspection...\n")
|
|
|
|
tasks.Wait()
|
|
|
|
Debugf("DONE Waiting for introspection\n")
|
|
|
|
}()
|
|
|
|
if introspect != nil {
|
|
|
|
tasks.Add(1)
|
|
|
|
go func() {
|
|
|
|
Debugf("starting introspection\n")
|
|
|
|
defer Debugf("done with introspection\n")
|
|
|
|
defer tasks.Done()
|
|
|
|
introspect.Send(data.Empty().Set("cmd", "log", "stdout").Set("message", "introspection worked!").Bytes(), nil)
|
|
|
|
Debugf("XXX starting reading introspection messages\n")
|
|
|
|
r := beam.NewRouter(handlers)
|
|
|
|
r.NewRoute().All().Handler(func(p []byte, a *os.File) error {
|
|
|
|
Logf("[INTROSPECTION] %s\n", beam.MsgDesc(p, a))
|
|
|
|
return handlers.Send(p, a)
|
|
|
|
})
|
|
|
|
n, err := beam.Copy(r, introspect)
|
|
|
|
Debugf("XXX done reading %d introspection messages: %v\n", n, err)
|
|
|
|
}()
|
|
|
|
}
|
2014-03-31 19:04:39 +00:00
|
|
|
if err := executeScript(handlers, script); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2014-03-28 07:01:35 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 23:57:48 +00:00
|
|
|
func executeScript(out beam.Sender, script []*dockerscript.Command) error {
|
2014-03-26 01:11:06 +00:00
|
|
|
Debugf("executeScript(%s)\n", scriptString(script))
|
|
|
|
defer Debugf("executeScript(%s) DONE\n", scriptString(script))
|
2014-03-28 01:26:42 +00:00
|
|
|
var background sync.WaitGroup
|
|
|
|
defer background.Wait()
|
2014-03-26 01:11:06 +00:00
|
|
|
for _, cmd := range script {
|
2014-03-28 01:26:42 +00:00
|
|
|
if cmd.Background {
|
|
|
|
background.Add(1)
|
2014-03-28 23:57:48 +00:00
|
|
|
go func(out beam.Sender, cmd *dockerscript.Command) {
|
|
|
|
executeCommand(out, cmd)
|
2014-03-28 01:26:42 +00:00
|
|
|
background.Done()
|
2014-03-28 23:57:48 +00:00
|
|
|
}(out, cmd)
|
2014-03-28 01:26:42 +00:00
|
|
|
} else {
|
2014-03-28 23:57:48 +00:00
|
|
|
if err := executeCommand(out, cmd); err != nil {
|
2014-03-28 01:26:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-03-22 02:23:13 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-26 01:11:06 +00:00
|
|
|
return nil
|
2014-03-22 02:23:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-26 01:11:06 +00:00
|
|
|
// 1) Find a handler for the command (if no handler, fail)
|
|
|
|
// 2) Attach new in & out pair to the handler
|
|
|
|
// 3) [in the background] Copy handler output to our own output
|
|
|
|
// 4) [in the background] Run the handler
|
|
|
|
// 5) Recursively executeScript() all children commands and wait for them to complete
|
|
|
|
// 6) Wait for handler to return and (shortly afterwards) output copy to complete
|
2014-03-28 23:57:48 +00:00
|
|
|
// 7) Profit
|
|
|
|
func executeCommand(out beam.Sender, cmd *dockerscript.Command) error {
|
2014-03-28 17:15:20 +00:00
|
|
|
if flX {
|
|
|
|
fmt.Printf("+ %v\n", strings.Replace(strings.TrimRight(cmd.String(), "\n"), "\n", "\n+ ", -1))
|
|
|
|
}
|
2014-03-26 01:11:06 +00:00
|
|
|
Debugf("executeCommand(%s)\n", strings.Join(cmd.Args, " "))
|
|
|
|
defer Debugf("executeCommand(%s) DONE\n", strings.Join(cmd.Args, " "))
|
2014-03-28 17:15:20 +00:00
|
|
|
if len(cmd.Args) == 0 {
|
|
|
|
return fmt.Errorf("empty command")
|
|
|
|
}
|
2014-03-31 19:04:39 +00:00
|
|
|
Debugf("[executeCommand] sending job '%s'\n", strings.Join(cmd.Args, " "))
|
2014-04-03 03:05:00 +00:00
|
|
|
job, err := beam.SendConn(out, data.Empty().Set("cmd", cmd.Args...).Set("type", "job").Bytes())
|
2014-03-22 03:54:05 +00:00
|
|
|
if err != nil {
|
2014-03-31 19:04:39 +00:00
|
|
|
return fmt.Errorf("%v\n", err)
|
2014-03-22 03:54:05 +00:00
|
|
|
}
|
2014-03-26 01:11:06 +00:00
|
|
|
var tasks sync.WaitGroup
|
2014-03-31 19:04:39 +00:00
|
|
|
tasks.Add(1)
|
|
|
|
Debugf("[executeCommand] spawning background copy of the output of '%s'\n", strings.Join(cmd.Args, " "))
|
2014-03-26 01:11:06 +00:00
|
|
|
go func() {
|
2014-03-28 23:57:48 +00:00
|
|
|
if out != nil {
|
2014-03-31 19:04:39 +00:00
|
|
|
Debugf("[executeCommand] background copy of the output of '%s'\n", strings.Join(cmd.Args, " "))
|
|
|
|
n, err := beam.Copy(out, job)
|
2014-03-28 07:01:35 +00:00
|
|
|
if err != nil {
|
2014-03-31 19:04:39 +00:00
|
|
|
Fatalf("[executeCommand] [%s] error during background copy: %v\n", strings.Join(cmd.Args, " "), err)
|
2014-03-28 07:01:35 +00:00
|
|
|
}
|
2014-03-31 19:04:39 +00:00
|
|
|
Debugf("[executeCommand] background copy done of the output of '%s': copied %d messages\n", strings.Join(cmd.Args, " "), n)
|
2014-03-26 22:46:02 +00:00
|
|
|
}
|
2014-03-26 01:11:06 +00:00
|
|
|
tasks.Done()
|
|
|
|
}()
|
|
|
|
// depth-first execution of children commands
|
|
|
|
// executeScript() blocks until all commands are completed
|
2014-03-31 19:04:39 +00:00
|
|
|
Debugf("[executeCommand] recursively running children of '%s'\n", strings.Join(cmd.Args, " "))
|
|
|
|
executeScript(job, cmd.Children)
|
|
|
|
Debugf("[executeCommand] DONE recursively running children of '%s'\n", strings.Join(cmd.Args, " "))
|
|
|
|
job.CloseWrite()
|
|
|
|
Debugf("[executeCommand] closing the input of '%s' (all children are completed)\n", strings.Join(cmd.Args, " "))
|
|
|
|
Debugf("[executeCommand] waiting for background copy of '%s' to complete...\n", strings.Join(cmd.Args, " "))
|
2014-03-26 01:11:06 +00:00
|
|
|
tasks.Wait()
|
2014-03-31 19:04:39 +00:00
|
|
|
Debugf("[executeCommand] background copy of '%s' complete! This means the job completed.\n", strings.Join(cmd.Args, " "))
|
2014-03-26 01:11:06 +00:00
|
|
|
return nil
|
2014-03-22 02:23:13 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 02:01:34 +00:00
|
|
|
type Handler func([]string, io.Writer, io.Writer, beam.Receiver, beam.Sender)
|
2014-03-28 06:58:06 +00:00
|
|
|
|
2014-04-03 04:58:47 +00:00
|
|
|
func Handlers(sink beam.Sender) (*beam.UnixConn, error) {
|
2014-03-31 19:04:39 +00:00
|
|
|
var tasks sync.WaitGroup
|
|
|
|
pub, priv, err := beam.USocketPair()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
Debugf("[handlers] closewrite() on endpoint\n")
|
|
|
|
// FIXME: this is not yet necessary but will be once
|
|
|
|
// there is synchronization over standard beam messages
|
|
|
|
priv.CloseWrite()
|
|
|
|
Debugf("[handlers] done closewrite() on endpoint\n")
|
|
|
|
}()
|
2014-04-03 04:58:47 +00:00
|
|
|
r := beam.NewRouter(sink)
|
|
|
|
r.NewRoute().HasAttachment().KeyIncludes("type", "job").Handler(func(payload []byte, attachment *os.File) error {
|
|
|
|
conn, err := beam.FileConn(attachment)
|
2014-03-31 19:04:39 +00:00
|
|
|
if err != nil {
|
2014-04-03 04:58:47 +00:00
|
|
|
attachment.Close()
|
|
|
|
return err
|
2014-03-31 19:04:39 +00:00
|
|
|
}
|
2014-04-03 04:58:47 +00:00
|
|
|
// attachment.Close()
|
2014-03-31 19:04:39 +00:00
|
|
|
tasks.Add(1)
|
2014-04-03 04:58:47 +00:00
|
|
|
go func() {
|
2014-03-31 19:04:39 +00:00
|
|
|
defer tasks.Done()
|
|
|
|
defer func() {
|
|
|
|
Debugf("[handlers] '%s' closewrite\n", payload)
|
|
|
|
conn.CloseWrite()
|
|
|
|
Debugf("[handlers] '%s' done closewrite\n", payload)
|
|
|
|
}()
|
|
|
|
cmd := data.Message(payload).Get("cmd")
|
|
|
|
Debugf("[handlers] received %s\n", strings.Join(cmd, " "))
|
|
|
|
if len(cmd) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
handler := GetHandler(cmd[0])
|
|
|
|
if handler == nil {
|
|
|
|
return
|
|
|
|
}
|
2014-05-09 22:39:55 +00:00
|
|
|
stdout, err := beam.SendRPipe(conn, data.Empty().Set("cmd", "log", "stdout").Set("fromcmd", cmd...).Bytes())
|
2014-04-03 02:01:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer stdout.Close()
|
2014-05-09 22:39:55 +00:00
|
|
|
stderr, err := beam.SendRPipe(conn, data.Empty().Set("cmd", "log", "stderr").Set("fromcmd", cmd...).Bytes())
|
2014-04-03 02:01:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer stderr.Close()
|
2014-03-31 19:04:39 +00:00
|
|
|
Debugf("[handlers] calling %s\n", strings.Join(cmd, " "))
|
2014-04-03 02:01:34 +00:00
|
|
|
handler(cmd, stdout, stderr, beam.Receiver(conn), beam.Sender(conn))
|
2014-03-31 19:04:39 +00:00
|
|
|
Debugf("[handlers] returned: %s\n", strings.Join(cmd, " "))
|
2014-04-03 04:58:47 +00:00
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
beam.Copy(r, priv)
|
2014-03-31 19:04:39 +00:00
|
|
|
Debugf("[handlers] waiting for all tasks\n")
|
|
|
|
tasks.Wait()
|
|
|
|
Debugf("[handlers] all tasks returned\n")
|
|
|
|
}()
|
|
|
|
return pub, nil
|
|
|
|
}
|
|
|
|
|
2014-03-26 01:11:06 +00:00
|
|
|
func GetHandler(name string) Handler {
|
2014-03-31 19:45:39 +00:00
|
|
|
if name == "logger" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdLogger
|
2014-03-28 22:36:29 +00:00
|
|
|
} else if name == "render" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdRender
|
2014-03-28 17:15:20 +00:00
|
|
|
} else if name == "devnull" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdDevnull
|
2014-03-31 19:45:12 +00:00
|
|
|
} else if name == "prompt" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdPrompt
|
2014-03-28 06:58:06 +00:00
|
|
|
} else if name == "stdio" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdStdio
|
2014-03-28 06:58:06 +00:00
|
|
|
} else if name == "echo" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdEcho
|
2014-03-28 06:58:06 +00:00
|
|
|
} else if name == "pass" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdPass
|
2014-03-27 18:00:18 +00:00
|
|
|
} else if name == "in" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdIn
|
2014-03-27 18:00:18 +00:00
|
|
|
} else if name == "exec" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdExec
|
2014-03-26 02:50:44 +00:00
|
|
|
} else if name == "trace" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdTrace
|
2014-03-26 01:11:06 +00:00
|
|
|
} else if name == "emit" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdEmit
|
2014-03-26 01:11:06 +00:00
|
|
|
} else if name == "print" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdPrint
|
2014-03-26 02:50:44 +00:00
|
|
|
} else if name == "multiprint" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdMultiprint
|
2014-03-26 02:50:44 +00:00
|
|
|
} else if name == "listen" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdListen
|
2014-03-26 22:48:12 +00:00
|
|
|
} else if name == "beamsend" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdBeamsend
|
2014-03-27 21:54:53 +00:00
|
|
|
} else if name == "beamreceive" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdBeamreceive
|
2014-03-26 19:24:12 +00:00
|
|
|
} else if name == "connect" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdConnect
|
2014-03-26 01:11:06 +00:00
|
|
|
} else if name == "openfile" {
|
2014-04-03 02:18:00 +00:00
|
|
|
return CmdOpenfile
|
2014-04-03 04:58:47 +00:00
|
|
|
} else if name == "spawn" {
|
|
|
|
return CmdSpawn
|
2014-04-05 00:30:03 +00:00
|
|
|
} else if name == "chdir" {
|
|
|
|
return CmdChdir
|
2014-03-22 03:54:05 +00:00
|
|
|
}
|
2014-03-26 01:11:06 +00:00
|
|
|
return nil
|
2014-03-22 03:54:05 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 22:42:57 +00:00
|
|
|
// VARIOUS HELPER FUNCTIONS:
|
|
|
|
|
2014-03-26 19:24:28 +00:00
|
|
|
func connToFile(conn net.Conn) (f *os.File, err error) {
|
2014-04-22 23:09:42 +00:00
|
|
|
if connWithFile, ok := conn.(interface {
|
|
|
|
File() (*os.File, error)
|
|
|
|
}); !ok {
|
2014-03-26 19:24:28 +00:00
|
|
|
return nil, fmt.Errorf("no file descriptor available")
|
|
|
|
} else {
|
|
|
|
f, err = connWithFile.File()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f, err
|
|
|
|
}
|
2014-03-26 01:11:06 +00:00
|
|
|
|
2014-03-25 17:14:27 +00:00
|
|
|
type Msg struct {
|
2014-04-22 23:09:42 +00:00
|
|
|
payload []byte
|
|
|
|
attachment *os.File
|
2014-03-25 17:14:27 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 02:23:13 +00:00
|
|
|
func Logf(msg string, args ...interface{}) (int, error) {
|
2014-04-22 23:09:42 +00:00
|
|
|
if len(msg) == 0 || msg[len(msg)-1] != '\n' {
|
2014-03-22 02:23:13 +00:00
|
|
|
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{}) {
|
2014-03-26 01:11:06 +00:00
|
|
|
if os.Getenv("BEAMDEBUG") != "" {
|
2014-03-24 23:21:55 +00:00
|
|
|
Logf(msg, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 23:09:42 +00:00
|
|
|
func Fatalf(msg string, args ...interface{}) {
|
2014-03-31 19:01:06 +00:00
|
|
|
Logf(msg, args...)
|
2014-03-22 02:23:13 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fatal(args ...interface{}) {
|
|
|
|
Fatalf("%v", args[0])
|
2014-03-22 00:30:15 +00:00
|
|
|
}
|
2014-03-28 22:42:57 +00:00
|
|
|
|
|
|
|
func scriptString(script []*dockerscript.Command) string {
|
|
|
|
lines := make([]string, 0, len(script))
|
|
|
|
for _, cmd := range script {
|
|
|
|
line := strings.Join(cmd.Args, " ")
|
|
|
|
if len(cmd.Children) > 0 {
|
|
|
|
line += fmt.Sprintf(" { %s }", scriptString(cmd.Children))
|
|
|
|
} else {
|
|
|
|
line += " {}"
|
|
|
|
}
|
|
|
|
lines = append(lines, line)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("'%s'", strings.Join(lines, "; "))
|
|
|
|
}
|
|
|
|
|
|
|
|
func dialer(addr string) (chan net.Conn, error) {
|
|
|
|
u, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
connections := make(chan net.Conn)
|
|
|
|
go func() {
|
|
|
|
defer close(connections)
|
|
|
|
for {
|
|
|
|
conn, err := net.Dial(u.Scheme, u.Host)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-04-22 23:09:42 +00:00
|
|
|
connections <- conn
|
2014-03-28 22:42:57 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return connections, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func listener(addr string) (chan net.Conn, error) {
|
|
|
|
u, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
l, err := net.Listen(u.Scheme, u.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
connections := make(chan net.Conn)
|
|
|
|
go func() {
|
|
|
|
defer close(connections)
|
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Logf("new connection\n")
|
2014-04-22 23:09:42 +00:00
|
|
|
connections <- conn
|
2014-03-28 22:42:57 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return connections, nil
|
|
|
|
}
|
|
|
|
|
2014-03-28 23:57:48 +00:00
|
|
|
func SendToConn(connections chan net.Conn, src beam.Receiver) error {
|
2014-03-28 22:42:57 +00:00
|
|
|
var tasks sync.WaitGroup
|
|
|
|
defer tasks.Wait()
|
|
|
|
for {
|
2014-03-28 23:57:48 +00:00
|
|
|
payload, attachment, err := src.Receive()
|
2014-03-28 22:42:57 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn, ok := <-connections
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
Logf("Sending %s\n", msgDesc(payload, attachment))
|
|
|
|
tasks.Add(1)
|
|
|
|
go func(payload []byte, attachment *os.File, conn net.Conn) {
|
|
|
|
defer tasks.Done()
|
|
|
|
if _, err := conn.Write([]byte(data.EncodeString(string(payload)))); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if attachment == nil {
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var iotasks sync.WaitGroup
|
|
|
|
iotasks.Add(2)
|
|
|
|
go func(attachment *os.File, conn net.Conn) {
|
|
|
|
defer iotasks.Done()
|
|
|
|
Debugf("copying the connection to [%d]\n", attachment.Fd())
|
|
|
|
io.Copy(attachment, conn)
|
|
|
|
attachment.Close()
|
|
|
|
Debugf("done copying the connection to [%d]\n", attachment.Fd())
|
|
|
|
}(attachment, conn)
|
|
|
|
go func(attachment *os.File, conn net.Conn) {
|
|
|
|
defer iotasks.Done()
|
|
|
|
Debugf("copying [%d] to the connection\n", attachment.Fd())
|
|
|
|
io.Copy(conn, attachment)
|
|
|
|
conn.Close()
|
|
|
|
Debugf("done copying [%d] to the connection\n", attachment.Fd())
|
|
|
|
}(attachment, conn)
|
|
|
|
iotasks.Wait()
|
|
|
|
}(payload, attachment, conn)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-02 23:38:36 +00:00
|
|
|
func msgDesc(payload []byte, attachment *os.File) string {
|
|
|
|
return beam.MsgDesc(payload, attachment)
|
2014-03-28 22:42:57 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 23:57:48 +00:00
|
|
|
func ReceiveFromConn(connections chan net.Conn, dst beam.Sender) error {
|
2014-03-28 22:42:57 +00:00
|
|
|
for conn := range connections {
|
2014-04-22 23:09:42 +00:00
|
|
|
err := func() error {
|
2014-03-28 22:42:57 +00:00
|
|
|
Logf("parsing message from network...\n")
|
|
|
|
defer Logf("done parsing message from network\n")
|
|
|
|
buf := make([]byte, 4098)
|
|
|
|
n, err := conn.Read(buf)
|
|
|
|
if n == 0 {
|
|
|
|
conn.Close()
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Logf("decoding message from '%s'\n", buf[:n])
|
|
|
|
header, skip, err := data.DecodeString(string(buf[:n]))
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pub, priv, err := beam.SocketPair()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
Logf("decoded message: %s\n", data.Message(header).Pretty())
|
|
|
|
go func(skipped []byte, conn net.Conn, f *os.File) {
|
|
|
|
// this closes both conn and f
|
|
|
|
if len(skipped) > 0 {
|
|
|
|
if _, err := f.Write(skipped); err != nil {
|
|
|
|
Logf("ERROR: %v\n", err)
|
|
|
|
f.Close()
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bicopy(conn, f)
|
|
|
|
}(buf[skip:n], conn, pub)
|
2014-03-28 23:57:48 +00:00
|
|
|
if err := dst.Send([]byte(header), priv); err != nil {
|
2014-03-28 22:42:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
Logf("Error reading from connection: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func bicopy(a, b io.ReadWriteCloser) {
|
|
|
|
var iotasks sync.WaitGroup
|
|
|
|
oneCopy := func(dst io.WriteCloser, src io.Reader) {
|
|
|
|
defer iotasks.Done()
|
|
|
|
io.Copy(dst, src)
|
|
|
|
dst.Close()
|
|
|
|
}
|
|
|
|
iotasks.Add(2)
|
|
|
|
go oneCopy(a, b)
|
|
|
|
go oneCopy(b, a)
|
|
|
|
iotasks.Wait()
|
|
|
|
}
|