Beispiel #1
0
// ExecSync executes a command in the container, and returns the stdout output.
// If command exits with a non-zero exit code, an error is returned.
func (ds *dockerService) ExecSync(containerID string, cmd []string, timeout time.Duration) (stdout []byte, stderr []byte, err error) {
	var stdoutBuffer, stderrBuffer bytes.Buffer
	err = ds.streamingRuntime.exec(containerID, cmd,
		nil, // in
		ioutils.WriteCloserWrapper(&stdoutBuffer),
		ioutils.WriteCloserWrapper(&stderrBuffer),
		false, // tty
		nil,   // resize
		timeout)
	return stdoutBuffer.Bytes(), stderrBuffer.Bytes(), err
}
Beispiel #2
0
func (hr *HandlerRunner) Run(containerID kubecontainer.ContainerID, pod *api.Pod, container *api.Container, handler *api.Handler) (string, error) {
	switch {
	case handler.Exec != nil:
		var (
			buffer bytes.Buffer
			msg    string
		)
		output := ioutils.WriteCloserWrapper(&buffer)
		err := hr.commandRunner.ExecInContainer(containerID, handler.Exec.Command, nil, output, output, false, nil)
		if err != nil {
			msg := fmt.Sprintf("Exec lifecycle hook (%v) for Container %q in Pod %q failed - %q", handler.Exec.Command, container.Name, format.Pod(pod), buffer.String())
			glog.V(1).Infof(msg)
		}
		return msg, err
	case handler.HTTPGet != nil:
		msg, err := hr.runHTTPHandler(pod, container, handler)
		if err != nil {
			msg := fmt.Sprintf("Http lifecycle hook (%s) for Container %q in Pod %q failed - %q", handler.HTTPGet.Path, container.Name, format.Pod(pod), msg)
			glog.V(1).Infof(msg)
		}
		return msg, err
	default:
		err := fmt.Errorf("Invalid handler: %v", handler)
		msg := fmt.Sprintf("Cannot run handler: %v", err)
		glog.Errorf(msg)
		return msg, err
	}
}
func (r *FakeRuntime) ExecSync(containerID string, cmd []string, timeout time.Duration) (stdout []byte, stderr []byte, err error) {
	c, ok := r.Containers[containerID]
	if !ok {
		return nil, nil, ErrContainerNotFound
	}

	// TODO(tmrts): Validate the assumption that container has to be running for exec to work.
	if c.State != runtimeApi.ContainerState_CONTAINER_RUNNING {
		return nil, nil, ErrInvalidContainerStateTransition
	}

	var stdoutBuffer, stderrBuffer bytes.Buffer
	err = c.Exec(cmd, nil,
		ioutils.WriteCloserWrapper(&stdoutBuffer),
		ioutils.WriteCloserWrapper(&stderrBuffer))
	return stdoutBuffer.Bytes(), stderrBuffer.Bytes(), err
}
Beispiel #4
0
func (r *containerCommandRunnerWrapper) RunInContainer(id ContainerID, cmd []string, timeout time.Duration) ([]byte, error) {
	var buffer bytes.Buffer
	output := ioutils.WriteCloserWrapper(&buffer)
	err := r.ExecInContainer(id, cmd, nil, output, output, false, nil, timeout)
	// Even if err is non-nil, there still may be output (e.g. the exec wrote to stdout or stderr but
	// the command returned a nonzero exit code). Therefore, always return the output along with the
	// error.
	return buffer.Bytes(), err
}
Beispiel #5
0
func (p *prober) newExecInContainer(container api.Container, containerID kubecontainer.ContainerID, cmd []string) exec.Cmd {
	return execInContainer{func() ([]byte, error) {
		var buffer bytes.Buffer
		output := ioutils.WriteCloserWrapper(&buffer)
		err := p.runner.ExecInContainer(containerID, cmd, nil, output, output, false, nil)
		// Even if err is non-nil, there still may be output (e.g. the exec wrote to stdout or stderr but
		// the command returned a nonzero exit code). Therefore, always return the output along with the
		// error.
		return buffer.Bytes(), err
	}}
}
Beispiel #6
0
func (p *prober) newExecInContainer(container api.Container, containerID kubecontainer.ContainerID, cmd []string) exec.Cmd {
	return execInContainer{func() ([]byte, error) {
		var buffer bytes.Buffer
		output := ioutils.WriteCloserWrapper(&buffer)
		err := p.runner.ExecInContainer(containerID, cmd, nil, output, output, false, nil)
		if err != nil {
			return nil, err
		}

		return buffer.Bytes(), nil
	}}
}
Beispiel #7
0
func (hr *HandlerRunner) Run(containerID kubecontainer.ContainerID, pod *api.Pod, container *api.Container, handler *api.Handler) error {
	switch {
	case handler.Exec != nil:
		var buffer bytes.Buffer
		output := ioutils.WriteCloserWrapper(&buffer)
		err := hr.commandRunner.ExecInContainer(containerID, handler.Exec.Command, nil, output, output, false)
		return err
	case handler.HTTPGet != nil:
		return hr.runHTTPHandler(pod, container, handler)
	default:
		err := fmt.Errorf("Invalid handler: %v", handler)
		glog.Errorf("Cannot run handler: %v", err)
		return err
	}
}
Beispiel #8
0
// Run a command in a container, returns the combined stdout, stderr as an array of bytes
func (kl *Kubelet) RunInContainer(podFullName string, podUID types.UID, containerName string, cmd []string) ([]byte, error) {
	podUID = kl.podManager.TranslatePodUID(podUID)

	container, err := kl.findContainer(podFullName, podUID, containerName)
	if err != nil {
		return nil, err
	}
	if container == nil {
		return nil, fmt.Errorf("container not found (%q)", containerName)
	}

	var buffer bytes.Buffer
	output := ioutils.WriteCloserWrapper(&buffer)
	err = kl.runner.ExecInContainer(container.ID, cmd, nil, output, output, false, nil)
	// Even if err is non-nil, there still may be output (e.g. the exec wrote to stdout or stderr but
	// the command returned a nonzero exit code). Therefore, always return the output along with the
	// error.
	return buffer.Bytes(), err
}