2016-11-22 17:49:54 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-11-09 11:10:35 +01:00
|
|
|
"time"
|
2016-11-22 17:49:54 +01:00
|
|
|
|
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2017-08-05 07:40:46 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-11-22 17:49:54 +01:00
|
|
|
"golang.org/x/net/context"
|
2018-02-12 12:13:07 -08:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
|
2016-11-22 17:49:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ExecSync runs a command in a container synchronously.
|
2017-11-09 11:10:35 +01:00
|
|
|
func (s *Server) ExecSync(ctx context.Context, req *pb.ExecSyncRequest) (resp *pb.ExecSyncResponse, err error) {
|
|
|
|
const operation = "exec_sync"
|
|
|
|
defer func() {
|
|
|
|
recordOperation(operation, time.Now())
|
|
|
|
recordError(operation, err)
|
|
|
|
}()
|
2016-11-22 17:49:54 +01:00
|
|
|
logrus.Debugf("ExecSyncRequest %+v", req)
|
2017-07-31 19:23:20 -04:00
|
|
|
c, err := s.GetContainerFromRequest(req.ContainerId)
|
2016-11-22 17:49:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-17 08:25:32 -04:00
|
|
|
if err = s.Runtime().UpdateStatus(c); err != nil {
|
2016-11-22 17:49:54 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-17 08:25:32 -04:00
|
|
|
cState := s.Runtime().ContainerStatus(c)
|
2016-11-22 17:49:54 +01:00
|
|
|
if !(cState.Status == oci.ContainerStateRunning || cState.Status == oci.ContainerStateCreated) {
|
|
|
|
return nil, fmt.Errorf("container is not created or running")
|
|
|
|
}
|
|
|
|
|
2017-02-03 15:41:28 +01:00
|
|
|
cmd := req.Cmd
|
2016-11-22 17:49:54 +01:00
|
|
|
if cmd == nil {
|
|
|
|
return nil, fmt.Errorf("exec command cannot be empty")
|
|
|
|
}
|
|
|
|
|
2017-07-17 08:25:32 -04:00
|
|
|
execResp, err := s.Runtime().ExecSync(c, cmd, req.Timeout)
|
2016-11-24 10:57:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-09 11:10:35 +01:00
|
|
|
resp = &pb.ExecSyncResponse{
|
2016-11-22 17:49:54 +01:00
|
|
|
Stdout: execResp.Stdout,
|
|
|
|
Stderr: execResp.Stderr,
|
2017-02-03 15:41:28 +01:00
|
|
|
ExitCode: execResp.ExitCode,
|
2016-11-22 17:49:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("ExecSyncResponse: %+v", resp)
|
2016-11-24 10:57:10 +01:00
|
|
|
return resp, nil
|
2016-11-22 17:49:54 +01:00
|
|
|
}
|