github.com/secure-build/gitlab-runner@v12.5.0+incompatible/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/executors/docker/internal/volumes/parser"
    11  	"gitlab.com/gitlab-org/gitlab-runner/helpers/ssh"
    12  )
    13  
    14  type sshExecutor struct {
    15  	executor
    16  	sshCommand ssh.Client
    17  }
    18  
    19  func (s *sshExecutor) Prepare(options common.ExecutorPrepareOptions) error {
    20  	err := s.executor.Prepare(options)
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	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")
    26  
    27  	if s.Config.SSH == nil {
    28  		return errors.New("Missing SSH configuration")
    29  	}
    30  
    31  	s.Debugln("Starting SSH command...")
    32  
    33  	// Start build container which will run actual build
    34  	container, err := s.createContainer("build", s.Build.Image, []string{}, []string{})
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	s.Debugln("Starting container", container.ID, "...")
    40  	err = s.client.ContainerStart(s.Context, container.ID, types.ContainerStartOptions{})
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	containerData, err := s.client.ContainerInspect(s.Context, container.ID)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	// Create SSH command
    51  	s.sshCommand = ssh.Client{
    52  		Config: *s.Config.SSH,
    53  		Stdout: s.Trace,
    54  		Stderr: s.Trace,
    55  	}
    56  	s.sshCommand.Host = containerData.NetworkSettings.IPAddress
    57  
    58  	s.Debugln("Connecting to SSH server...")
    59  	err = s.sshCommand.Connect()
    60  	if err != nil {
    61  		return err
    62  	}
    63  	return nil
    64  }
    65  
    66  func (s *sshExecutor) Run(cmd common.ExecutorCommand) error {
    67  	s.SetCurrentStage(DockerExecutorStageRun)
    68  
    69  	err := s.sshCommand.Run(cmd.Context, ssh.Command{
    70  		Environment: s.BuildShell.Environment,
    71  		Command:     s.BuildShell.GetCommandWithArguments(),
    72  		Stdin:       cmd.Script,
    73  	})
    74  	if _, ok := err.(*ssh.ExitError); ok {
    75  		err = &common.BuildError{Inner: err}
    76  	}
    77  	return err
    78  }
    79  
    80  func (s *sshExecutor) Cleanup() {
    81  	s.sshCommand.Cleanup()
    82  	s.executor.Cleanup()
    83  }
    84  
    85  func init() {
    86  	options := executors.ExecutorOptions{
    87  		DefaultCustomBuildsDirEnabled: true,
    88  		DefaultBuildsDir:              "builds",
    89  		DefaultCacheDir:               "cache",
    90  		SharedBuildsDir:               false,
    91  		Shell: common.ShellScriptInfo{
    92  			Shell:         "bash",
    93  			Type:          common.LoginShell,
    94  			RunnerCommand: "gitlab-runner",
    95  		},
    96  		ShowHostname: true,
    97  		Metadata: map[string]string{
    98  			metadataOSType: osTypeLinux,
    99  		},
   100  	}
   101  
   102  	creator := func() common.Executor {
   103  		e := &sshExecutor{
   104  			executor: executor{
   105  				AbstractExecutor: executors.AbstractExecutor{
   106  					ExecutorOptions: options,
   107  				},
   108  				volumeParser: parser.NewLinuxParser(),
   109  			},
   110  		}
   111  		e.SetCurrentStage(common.ExecutorStageCreated)
   112  		return e
   113  	}
   114  
   115  	featuresUpdater := func(features *common.FeaturesInfo) {
   116  		features.Variables = true
   117  		features.Image = true
   118  		features.Services = true
   119  	}
   120  
   121  	common.RegisterExecutor("docker-ssh", executors.DefaultExecutorProvider{
   122  		Creator:          creator,
   123  		FeaturesUpdater:  featuresUpdater,
   124  		DefaultShellName: options.Shell.Shell,
   125  	})
   126  }