示例#1
0
func child(t *testing.T, server *httptest.Server, i *beam.Object, name string) *beam.Object {
	_, child, err := i.Attach(name)
	if err != nil {
		t.Fatal(err)
	}
	return child
}
示例#2
0
func newAggregator(allBackends *beam.Object, server *beam.Server, args []string) (*aggregator, error) {
	a := &aggregator{server: server}

	for _, argString := range args {
		args, err := shlex.Split(argString)
		if err != nil {
			return nil, err
		}
		if len(args) == 0 {
			return nil, fmt.Errorf("empty backend string")
		}
		log.Printf("aggregator: spawning %s(%#v)\n", args[0], args[1:])
		_, b, err := allBackends.Attach(args[0])
		if err != nil {
			return nil, err
		}
		i, err := b.Spawn(args[1:]...)
		if err != nil {
			return nil, err
		}
		a.backends = append(a.backends, i)
	}

	return a, nil
}
示例#3
0
func doCmd(instance *beam.Object, args []string) error {
	if len(args) == 0 {
		return fmt.Errorf("no command supplied")
	}
	if args[0] == "ps" {
		if len(args) != 1 {
			return fmt.Errorf("usage: ps")
		}
		names, err := instance.Ls()
		if err != nil {
			return err
		}
		fmt.Println(strings.Join(names, "\n"))
		return nil
	}
	if args[0] == "run" {
		if len(args) < 3 {
			return fmt.Errorf("usage: run IMAGE COMMAND...")
		}
		containerJson, err := json.Marshal(&runconfig.Config{
			Image:        args[1],
			Cmd:          args[2:],
			AttachStdin:  false,
			AttachStdout: true,
			AttachStderr: true,
		})
		if err != nil {
			return err
		}
		container, err := instance.Spawn(string(containerJson))
		if err != nil {
			return fmt.Errorf("spawn: %v", err)
		}
		logs, _, err := container.Attach("")
		if err != nil {
			return fmt.Errorf("attach: %v", err)
		}
		if err = container.Start(); err != nil {
			return fmt.Errorf("start: %v", err)
		}
		for {
			msg, err := logs.Receive(beam.Ret)
			if err != nil {
				if err.Error() == "EOF" {
					break
				}
				return fmt.Errorf("error reading from container: %v", err)
			}
			if msg.Verb != beam.Log {
				return fmt.Errorf("unexpected message reading from container: %v", msg)
			}
			if len(msg.Args) != 2 {
				return fmt.Errorf("expected exactly 2 args to log message, got %d", len(msg.Args))
			}
			tag, chunk := msg.Args[0], msg.Args[1]
			var stream io.Writer
			if tag == "stdout" {
				stream = os.Stdout
			} else if tag == "stderr" {
				stream = os.Stderr
			} else {
				return fmt.Errorf("unrecognised tag: %s", tag)
			}
			fmt.Fprint(stream, chunk)
		}
		return nil
	}
	if args[0] == "inspect" {
		if len(args) != 2 {
			return fmt.Errorf("usage: inspect CONTAINER")
		}
		_, container, err := instance.Attach(args[1])
		if err != nil {
			return fmt.Errorf("attach: %v", err)
		}
		json, err := container.Get()
		if err != nil {
			return fmt.Errorf("get: %v", err)
		}
		fmt.Println(json)
		return nil
	}
	return fmt.Errorf("unrecognised command: %s", args[0])
}