2016-11-22 16:49:54 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ExecSync runs a command in a container synchronously.
|
|
|
|
func (s *Server) ExecSync(ctx context.Context, req *pb.ExecSyncRequest) (*pb.ExecSyncResponse, error) {
|
|
|
|
logrus.Debugf("ExecSyncRequest %+v", req)
|
2017-02-03 14:41:28 +00:00
|
|
|
c, err := s.getContainerFromRequest(req.ContainerId)
|
2016-11-22 16:49:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:25:32 +00:00
|
|
|
if err = s.Runtime().UpdateStatus(c); err != nil {
|
2016-11-22 16:49:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:25:32 +00:00
|
|
|
cState := s.Runtime().ContainerStatus(c)
|
2016-11-22 16:49:54 +00:00
|
|
|
if !(cState.Status == oci.ContainerStateRunning || cState.Status == oci.ContainerStateCreated) {
|
|
|
|
return nil, fmt.Errorf("container is not created or running")
|
|
|
|
}
|
|
|
|
|
2017-02-03 14:41:28 +00:00
|
|
|
cmd := req.Cmd
|
2016-11-22 16:49:54 +00:00
|
|
|
if cmd == nil {
|
|
|
|
return nil, fmt.Errorf("exec command cannot be empty")
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:25:32 +00:00
|
|
|
execResp, err := s.Runtime().ExecSync(c, cmd, req.Timeout)
|
2016-11-24 09:57:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-22 16:49:54 +00:00
|
|
|
resp := &pb.ExecSyncResponse{
|
|
|
|
Stdout: execResp.Stdout,
|
|
|
|
Stderr: execResp.Stderr,
|
2017-02-03 14:41:28 +00:00
|
|
|
ExitCode: execResp.ExitCode,
|
2016-11-22 16:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("ExecSyncResponse: %+v", resp)
|
2016-11-24 09:57:10 +00:00
|
|
|
return resp, nil
|
2016-11-22 16:49:54 +00:00
|
|
|
}
|