From 52e789c44b60a067e9bee6c8566c2e391bb1bea2 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Thu, 17 Nov 2016 16:41:44 -0800 Subject: [PATCH] Add server implementation for exec sync Signed-off-by: Mrunal Patel --- server/container.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/server/container.go b/server/container.go index 6672af10..1f48b2e8 100644 --- a/server/container.go +++ b/server/container.go @@ -577,7 +577,35 @@ func (s *Server) UpdateRuntimeConfig(ctx context.Context, req *pb.UpdateRuntimeC // ExecSync runs a command in a container synchronously. func (s *Server) ExecSync(ctx context.Context, req *pb.ExecSyncRequest) (*pb.ExecSyncResponse, error) { - return nil, nil + logrus.Debugf("ExecSyncRequest %+v", req) + c, err := s.getContainerFromRequest(req) + if err != nil { + return nil, err + } + + if err := s.runtime.UpdateStatus(c); err != nil { + return nil, err + } + + cState := s.runtime.ContainerStatus(c) + if !(cState.Status == oci.ContainerStateRunning || cState.Status == oci.ContainerStateCreated) { + return nil, fmt.Errorf("container is not created or running") + } + + cmd := req.GetCmd() + if cmd == nil { + return nil, fmt.Errorf("exec command cannot be empty") + } + + out, errout, rc, err := s.runtime.ExecSync(c, cmd, req.GetTimeout()) + + resp := &pb.ExecSyncResponse{ + Stdout: out, + Stderr: errout, + ExitCode: &rc, + } + logrus.Debugf("ExecSyncResponse: %+v", resp) + return resp, err } // Exec prepares a streaming endpoint to execute a command in the container.