github.com/ssube/gitlab-ci-multi-runner@v1.2.1-0.20160607142738-b8d1285632e6/executors/docker/executor_docker_command.go (about)

     1  package docker
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  
     7  	"github.com/fsouza/go-dockerclient"
     8  	"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
     9  	"gitlab.com/gitlab-org/gitlab-ci-multi-runner/executors"
    10  )
    11  
    12  type commandExecutor struct {
    13  	executor
    14  	predefinedContainer *docker.Container
    15  	buildContainer      *docker.Container
    16  }
    17  
    18  func (s *commandExecutor) Prepare(globalConfig *common.Config, config *common.RunnerConfig, build *common.Build) error {
    19  	err := s.executor.Prepare(globalConfig, config, build)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	s.Debugln("Starting Docker command...")
    25  
    26  	if len(s.BuildScript.DockerCommand) == 0 {
    27  		return errors.New("Script is not compatible with Docker")
    28  	}
    29  
    30  	imageName, err := s.getImageName()
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	options, err := s.prepareBuildContainer()
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	buildImage, err := s.getPrebuiltImage("build")
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	// Start pre-build container which will git clone changes
    46  	s.predefinedContainer, err = s.createContainer("predefined", buildImage.ID, nil, *options)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	// Start build container which will run actual build
    52  	s.buildContainer, err = s.createContainer("build", imageName, s.BuildScript.DockerCommand, *options)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	return nil
    57  }
    58  
    59  func (s *commandExecutor) Run(cmd common.ExecutorCommand) error {
    60  	var container *docker.Container
    61  
    62  	if cmd.Predefined {
    63  		container = s.predefinedContainer
    64  	} else {
    65  		container = s.buildContainer
    66  	}
    67  
    68  	s.Debugln("Executing on", container.Name, "the", cmd.Script)
    69  
    70  	return s.watchContainer(container, bytes.NewBufferString(cmd.Script), cmd.Abort)
    71  }
    72  
    73  func init() {
    74  	options := executors.ExecutorOptions{
    75  		DefaultBuildsDir: "/builds",
    76  		DefaultCacheDir:  "/cache",
    77  		SharedBuildsDir:  false,
    78  		Shell: common.ShellScriptInfo{
    79  			Shell:         "bash",
    80  			Type:          common.NormalShell,
    81  			RunnerCommand: "/usr/bin/gitlab-runner-helper",
    82  		},
    83  		ShowHostname:     true,
    84  		SupportedOptions: []string{"image", "services"},
    85  	}
    86  
    87  	creator := func() common.Executor {
    88  		return &commandExecutor{
    89  			executor: executor{
    90  				AbstractExecutor: executors.AbstractExecutor{
    91  					ExecutorOptions: options,
    92  				},
    93  			},
    94  		}
    95  	}
    96  
    97  	featuresUpdater := func(features *common.FeaturesInfo) {
    98  		features.Variables = true
    99  		features.Image = true
   100  		features.Services = true
   101  	}
   102  
   103  	common.RegisterExecutor("docker", executors.DefaultExecutorProvider{
   104  		Creator:         creator,
   105  		FeaturesUpdater: featuresUpdater,
   106  	})
   107  }