github.com/alloyci/alloy-runner@v1.0.1-0.20180222164613-925503ccafd6/executors/docker/executor_docker_ssh.go (about)

     1  package docker
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/docker/docker/api/types"
     7  
     8  	"gitlab.com/gitlab-org/gitlab-runner/common"
     9  	"gitlab.com/gitlab-org/gitlab-runner/executors"
    10  	"gitlab.com/gitlab-org/gitlab-runner/helpers/ssh"
    11  )
    12  
    13  type sshExecutor struct {
    14  	executor
    15  	sshCommand ssh.Client
    16  }
    17  
    18  func (s *sshExecutor) Prepare(options common.ExecutorPrepareOptions) error {
    19  	err := s.executor.Prepare(options)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	s.Warningln("Since GitLab Runner 10.0 docker-ssh and docker-ssh+machine executors are marked as DEPRECATED and will be removed in one of the upcoming releases")
    25  
    26  	if s.Config.SSH == nil {
    27  		return errors.New("Missing SSH configuration")
    28  	}
    29  
    30  	s.Debugln("Starting SSH command...")
    31  
    32  	// Start build container which will run actual build
    33  	container, err := s.createContainer("build", s.Build.Image, []string{}, []string{})
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	s.Debugln("Starting container", container.ID, "...")
    39  	err = s.client.ContainerStart(s.Context, container.ID, types.ContainerStartOptions{})
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	containerData, err := s.client.ContainerInspect(s.Context, container.ID)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	// Create SSH command
    50  	s.sshCommand = ssh.Client{
    51  		Config: *s.Config.SSH,
    52  		Stdout: s.Trace,
    53  		Stderr: s.Trace,
    54  	}
    55  	s.sshCommand.Host = containerData.NetworkSettings.IPAddress
    56  
    57  	s.Debugln("Connecting to SSH server...")
    58  	err = s.sshCommand.Connect()
    59  	if err != nil {
    60  		return err
    61  	}
    62  	return nil
    63  }
    64  
    65  func (s *sshExecutor) Run(cmd common.ExecutorCommand) error {
    66  	s.SetCurrentStage(DockerExecutorStageRun)
    67  
    68  	err := s.sshCommand.Run(cmd.Context, ssh.Command{
    69  		Environment: s.BuildShell.Environment,
    70  		Command:     s.BuildShell.GetCommandWithArguments(),
    71  		Stdin:       cmd.Script,
    72  	})
    73  	if _, ok := err.(*ssh.ExitError); ok {
    74  		err = &common.BuildError{Inner: err}
    75  	}
    76  	return err
    77  }
    78  
    79  func (s *sshExecutor) Cleanup() {
    80  	s.sshCommand.Cleanup()
    81  	s.executor.Cleanup()
    82  }
    83  
    84  func init() {
    85  	options := executors.ExecutorOptions{
    86  		DefaultBuildsDir: "builds",
    87  		SharedBuildsDir:  false,
    88  		Shell: common.ShellScriptInfo{
    89  			Shell:         "bash",
    90  			Type:          common.LoginShell,
    91  			RunnerCommand: "gitlab-runner",
    92  		},
    93  		ShowHostname: true,
    94  	}
    95  
    96  	creator := func() common.Executor {
    97  		e := &sshExecutor{
    98  			executor: executor{
    99  				AbstractExecutor: executors.AbstractExecutor{
   100  					ExecutorOptions: options,
   101  				},
   102  			},
   103  		}
   104  		e.SetCurrentStage(common.ExecutorStageCreated)
   105  		return e
   106  	}
   107  
   108  	featuresUpdater := func(features *common.FeaturesInfo) {
   109  		features.Variables = true
   110  		features.Image = true
   111  		features.Services = true
   112  	}
   113  
   114  	common.RegisterExecutor("docker-ssh", executors.DefaultExecutorProvider{
   115  		Creator:         creator,
   116  		FeaturesUpdater: featuresUpdater,
   117  	})
   118  }