Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

separate start and attach in the client side #482

Merged
merged 4 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ matrix:

before_install:
- sudo apt-get update -qq
- sudo apt-get install -y autoconf automake pkg-config libdevmapper-dev libsqlite3-dev libvirt-dev qemu libvirt-bin -qq
- sudo apt-get install -y autoconf automake pkg-config libdevmapper-dev libsqlite3-dev libvirt-dev qemu libvirt-bin aufs-tools -qq
- cd `mktemp -d`
- mkdir -p ${GOPATH}/src/github.com/hyperhq
- wget https://git.fedorahosted.org/cgit/lvm2.git/snapshot/lvm2-2_02_131.tar.xz
Expand Down
2 changes: 1 addition & 1 deletion client/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type APIInterface interface {

GetPodInfo(podName string) (*types.PodInfo, error)
CreatePod(spec interface{}) (string, int, error)
StartPod(podId, vmId string, attach, tty bool, stdin io.ReadCloser, stdout, stderr io.Writer) (string, error)
StartPod(podId string) error
StopPod(podId, stopVm string) (int, string, error)
RmPod(id string) error
PausePod(podId string) error
Expand Down
54 changes: 4 additions & 50 deletions client/api/start.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,18 @@
package api

import (
"fmt"
"io"
"net/url"

"github.com/hyperhq/hyperd/engine"
"github.com/hyperhq/runv/hypervisor/types"
)

func (cli *Client) StartPod(podId, vmId string, attach, tty bool, stdin io.ReadCloser, stdout, stderr io.Writer) (string, error) {
func (cli *Client) StartPod(podId string) error {
v := url.Values{}
v.Set("podId", podId)

if !attach {
return cli.startPodWithoutTty(&v)
} else {
v.Set("attach", "yes")

err := cli.hijackRequest("pod/start", &v, tty, stdin, stdout, stderr)
if err != nil {
fmt.Printf("StartPod failed: %s\n", err.Error())
return "", err
}

containerId, err := cli.GetContainerByPod(podId)
if err != nil {
return "", err
}

return "", cli.GetExitCode(containerId, "")
}
}

func (cli *Client) startPodWithoutTty(v *url.Values) (string, error) {

body, _, err := readBody(cli.call("POST", "/pod/start?"+v.Encode(), nil, nil))
_, _, err := readBody(cli.call("POST", "/pod/start?"+v.Encode(), nil, nil))
if err != nil {
return "", err
}
out := engine.NewOutput()
remoteInfo, err := out.AddEnv()
if err != nil {
return "", err
}

if _, err := out.Write(body); err != nil {
return "", fmt.Errorf("Error reading remote info: %s", err)
}
out.Close()
errCode := remoteInfo.GetInt("Code")
if errCode != types.E_OK {
if errCode != types.E_BAD_REQUEST &&
errCode != types.E_FAILED {
return "", fmt.Errorf("Error code is %d", errCode)
} else {
return "", fmt.Errorf("Cause is %s", remoteInfo.Get("Cause"))
}
return err
}
return remoteInfo.Get("ID"), nil
return nil
}

func (cli *Client) StartContainer(container string) error {
Expand Down
30 changes: 21 additions & 9 deletions client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ func (cli *HyperClient) HyperCmdRun(args ...string) (err error) {

var (
podId string
vmId string
spec apitype.UserPod
code int
tty = false
res chan error
)

if copt.IsContainer {
Expand Down Expand Up @@ -72,32 +72,44 @@ func (cli *HyperClient) HyperCmdRun(args ...string) (err error) {
}()
}

if copt.Attach {
if copt.Attach && len(spec.Containers) > 0 {
res = make(chan error, 1)
tty = spec.Tty || spec.Containers[0].Tty
p, err := cli.client.GetPodInfo(podId)
if err != nil {
fmt.Fprintf(cli.err, "failed to get pod info: %v", err)
return err
}
if tty {
p, err := cli.client.GetPodInfo(podId)
if err == nil {
cli.monitorTtySize(p.Spec.Containers[0].ContainerID, "")
}

cli.monitorTtySize(p.Spec.Containers[0].ContainerID, "")
oldState, err := term.SetRawTerminal(cli.inFd)
if err != nil {
return err
}
defer term.RestoreTerminal(cli.inFd, oldState)
}
cname := spec.Containers[0].Name

go func() {
res <- cli.client.Attach(cname, tty, cli.in, cli.out, cli.err)
}()
}

_, err = cli.client.StartPod(podId, vmId, copt.Attach, tty, cli.in, cli.out, cli.err)
err = cli.client.StartPod(podId)
if err != nil {
return
return err
}

if !copt.Attach {
t2 := time.Now()
fmt.Printf("Time to run a POD is %d ms\n", (t2.UnixNano()-t1.UnixNano())/1000000)
}

if res != nil {
err = <-res
return cli.client.GetExitCode(spec.Containers[0].Name, "")
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion client/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (cli *HyperClient) HyperCmdStart(args ...string) error {
)

if !opts.Container {
_, err = cli.client.StartPod(id, "", false, false, nil, nil, nil)
err = cli.client.StartPod(id)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonbuilder/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (d Docker) ContainerStart(cId string, hostConfig *containertypes.HostConfig
}
}()

if _, _, err = d.Daemon.StartPod(nil, nil, podId, false); err != nil {
if err = d.Daemon.StartPod(podId); err != nil {
return
}

Expand Down
27 changes: 4 additions & 23 deletions daemon/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package daemon

import (
"fmt"
"io"

"github.com/golang/glog"

Expand Down Expand Up @@ -43,39 +42,21 @@ func (daemon *Daemon) CreatePod(podId string, podSpec *apitypes.UserPod) (*pod.X
return p, nil
}

//TODO: remove the tty stream in StartPod API, now we could support attach after created
func (daemon *Daemon) StartPod(stdin io.ReadCloser, stdout io.WriteCloser, podId string, attach bool) (int, string, error) {
func (daemon *Daemon) StartPod(podId string) error {
p, ok := daemon.PodList.Get(podId)
if !ok {
return -1, "", fmt.Errorf("The pod(%s) can not be found, please create it first", podId)
}

var waitTty chan error

if attach {
glog.V(1).Info("Run pod with tty attached")

ids := p.ContainerIdsOf(apitypes.UserContainer_REGULAR)
for _, id := range ids {
waitTty = make(chan error, 1)
p.Attach(id, stdin, stdout, waitTty)
break
}
return fmt.Errorf("The pod(%s) can not be found, please create it first", podId)
}

glog.Infof("Starting pod %q in vm: %q", podId, p.SandboxName())

err := p.Start()
if err != nil {
glog.Infof("failed to start pod %s: %v", p.Id(), err)
return -1, err.Error(), err
}

if waitTty != nil {
<-waitTty
return err
}

return 0, "", err
return err
}

func (daemon *Daemon) WaitContainer(cid string, second int) (int, error) {
Expand Down
8 changes: 2 additions & 6 deletions daemon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,13 @@ func (daemon *Daemon) CmdSetPodLabels(podId string, override bool, labels map[st
return v, nil
}

func (daemon *Daemon) CmdStartPod(stdin io.ReadCloser, stdout io.WriteCloser, podId, vmId string, attach bool) (*engine.Env, error) {
code, cause, err := daemon.StartPod(stdin, stdout, podId, attach)
func (daemon *Daemon) CmdStartPod(podId string) (*engine.Env, error) {
err := daemon.StartPod(podId)
if err != nil {
return nil, err
}

v := &engine.Env{}
v.Set("ID", vmId)
v.SetInt("Code", code)
v.Set("Cause", cause)

return v, nil
}

Expand Down
33 changes: 5 additions & 28 deletions integration/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,36 +453,13 @@ func (c *HyperClient) ContainerExecStart(containerId, execId string, stdin io.Re
}

// StartPod starts a pod by podID
func (c *HyperClient) StartPod(podID, vmID string, attach bool) error {
stream, err := c.client.PodStart(context.Background())
if err != nil {
return err
}

req := types.PodStartMessage{
PodID: podID,
VmID: vmID,
Attach: attach,
}
if err := stream.Send(&req); err != nil {
return err
}

if attach {
if _, err := stream.Recv(); err != nil {
return err
}

return nil
func (c *HyperClient) StartPod(podID string) error {
req := &types.PodStartRequest{
PodID: podID,
}

cmd := types.PodStartMessage{
Data: []byte("ls\n"),
}
if err := stream.Send(&cmd); err != nil {
return err
}
if _, err := stream.Recv(); err != nil {
_, err := c.client.PodStart(c.ctx, req)
if err != nil {
return err
}

Expand Down
12 changes: 6 additions & 6 deletions integration/hyper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (s *TestSuite) TestPostAttach(c *C) {
c.Logf("Pod created: %s", pod)
defer s.client.RemovePod(pod)

err = s.client.StartPod(pod, "", false)
err = s.client.StartPod(pod)
c.Assert(err, IsNil)

podInfo, err := s.client.GetPodInfo(pod)
Expand Down Expand Up @@ -154,7 +154,7 @@ func (s *TestSuite) TestCreateAndStartPod(c *C) {
c.Errorf("Can't found pod %s", pod)
}

err = s.client.StartPod(pod, "", false)
err = s.client.StartPod(pod)
c.Assert(err, IsNil)

podInfo, err := s.client.GetPodInfo(pod)
Expand Down Expand Up @@ -296,7 +296,7 @@ func (s *TestSuite) TestAddListDeleteService(c *C) {

c.Log(" 2 ===> create pod")

err = s.client.StartPod(pod, "", false)
err = s.client.StartPod(pod)
c.Assert(err, IsNil)

updateService := []*types.UserService{
Expand Down Expand Up @@ -368,7 +368,7 @@ func (s *TestSuite) TestStartAndStopPod(c *C) {
c.Assert(err, IsNil)
c.Logf("Pod created: %s", pod)

err = s.client.StartPod(pod, "", false)
err = s.client.StartPod(pod)
c.Assert(err, IsNil)

podInfo, err := s.client.GetPodInfo(pod)
Expand Down Expand Up @@ -429,7 +429,7 @@ func (s *TestSuite) TestPauseAndUnpausePod(c *C) {
c.Assert(err, IsNil)
c.Logf("Pod created: %s", pod)

err = s.client.StartPod(pod, "", false)
err = s.client.StartPod(pod)
c.Assert(err, IsNil)

podInfo, err := s.client.GetPodInfo(pod)
Expand Down Expand Up @@ -479,7 +479,7 @@ func (s *TestSuite) TestGetPodStats(c *C) {
c.Assert(err, IsNil)
}()

err = s.client.StartPod(podID, "", false)
err = s.client.StartPod(podID)
c.Assert(err, IsNil)

stats, err := s.client.GetPodStats(podID)
Expand Down
4 changes: 1 addition & 3 deletions server/router/pod/backend.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package pod

import (
"io"

"github.com/hyperhq/hyperd/engine"
)

Expand All @@ -13,7 +11,7 @@ type Backend interface {
CmdGetPodStats(podId string) (interface{}, error)
CmdCreatePod(podArgs string) (*engine.Env, error)
CmdSetPodLabels(podId string, override bool, labels map[string]string) (*engine.Env, error)
CmdStartPod(in io.ReadCloser, out io.WriteCloser, podId, vmId string, attach bool) (*engine.Env, error)
CmdStartPod(podId string) (*engine.Env, error)
CmdPausePod(podId string) error
CmdUnpausePod(podId string) error
CmdList(item, podId, vmId string, auxiliary bool) (*engine.Env, error)
Expand Down
Loading