gopkg.in/openshift/source-to-image.v1@v1.2.0/pkg/run/run.go (about)

     1  // Package run supports running images produced by S2I. It is used by the
     2  // --run=true command line option.
     3  package run
     4  
     5  import (
     6  	"io"
     7  
     8  	"github.com/openshift/source-to-image/pkg/api"
     9  	"github.com/openshift/source-to-image/pkg/docker"
    10  	s2ierr "github.com/openshift/source-to-image/pkg/errors"
    11  	utillog "github.com/openshift/source-to-image/pkg/util/log"
    12  )
    13  
    14  var (
    15  	log = utillog.StderrLog
    16  )
    17  
    18  // A DockerRunner allows running a Docker image as a new container, streaming
    19  // stdout and stderr with glog.
    20  type DockerRunner struct {
    21  	ContainerClient docker.Docker
    22  }
    23  
    24  // New creates a DockerRunner for executing the methods associated with running
    25  // the produced image in a docker container for verification purposes.
    26  func New(client docker.Client, config *api.Config) *DockerRunner {
    27  	d := docker.New(client, config.PullAuthentication)
    28  	return &DockerRunner{d}
    29  }
    30  
    31  // Run invokes the Docker API to run the image defined in config as a new
    32  // container. The container's stdout and stderr will be logged with glog.
    33  func (b *DockerRunner) Run(config *api.Config) error {
    34  	log.V(4).Infof("Attempting to run image %s \n", config.Tag)
    35  
    36  	outReader, outWriter := io.Pipe()
    37  	errReader, errWriter := io.Pipe()
    38  
    39  	opts := docker.RunContainerOptions{
    40  		Image:        config.Tag,
    41  		Stdout:       outWriter,
    42  		Stderr:       errWriter,
    43  		TargetImage:  true,
    44  		CGroupLimits: config.CGroupLimits,
    45  		CapDrop:      config.DropCapabilities,
    46  	}
    47  
    48  	docker.StreamContainerIO(errReader, nil, func(s string) { log.Error(s) })
    49  	docker.StreamContainerIO(outReader, nil, func(s string) { log.Info(s) })
    50  
    51  	err := b.ContainerClient.RunContainer(opts)
    52  	// If we get a ContainerError, the original message reports the
    53  	// container name. The container is temporary and its name is
    54  	// meaningless, therefore we make the error message more helpful by
    55  	// replacing the container name with the image tag.
    56  	if e, ok := err.(s2ierr.ContainerError); ok {
    57  		return s2ierr.NewContainerError(config.Tag, e.ErrorCode, e.Output)
    58  	}
    59  	return err
    60  }