github.com/secure-build/gitlab-runner@v12.5.0+incompatible/executors/ssh/executor_ssh.go (about)

     1  package ssh
     2  
     3  import (
     4  	"errors"
     5  
     6  	"gitlab.com/gitlab-org/gitlab-runner/common"
     7  	"gitlab.com/gitlab-org/gitlab-runner/executors"
     8  	"gitlab.com/gitlab-org/gitlab-runner/helpers/ssh"
     9  )
    10  
    11  type executor struct {
    12  	executors.AbstractExecutor
    13  	sshCommand ssh.Client
    14  }
    15  
    16  func (s *executor) Prepare(options common.ExecutorPrepareOptions) error {
    17  	err := s.AbstractExecutor.Prepare(options)
    18  	if err != nil {
    19  		return err
    20  	}
    21  
    22  	s.Println("Using SSH executor...")
    23  	if s.BuildShell.PassFile {
    24  		return errors.New("SSH doesn't support shells that require script file")
    25  	}
    26  
    27  	if s.Config.SSH == nil {
    28  		return errors.New("Missing SSH configuration")
    29  	}
    30  
    31  	s.Debugln("Starting SSH command...")
    32  
    33  	// Create SSH command
    34  	s.sshCommand = ssh.Client{
    35  		Config: *s.Config.SSH,
    36  		Stdout: s.Trace,
    37  		Stderr: s.Trace,
    38  	}
    39  
    40  	s.Debugln("Connecting to SSH server...")
    41  	err = s.sshCommand.Connect()
    42  	if err != nil {
    43  		return err
    44  	}
    45  	return nil
    46  }
    47  
    48  func (s *executor) Run(cmd common.ExecutorCommand) error {
    49  	err := s.sshCommand.Run(cmd.Context, ssh.Command{
    50  		Environment: s.BuildShell.Environment,
    51  		Command:     s.BuildShell.GetCommandWithArguments(),
    52  		Stdin:       cmd.Script,
    53  	})
    54  	if _, ok := err.(*ssh.ExitError); ok {
    55  		err = &common.BuildError{Inner: err}
    56  	}
    57  	return err
    58  }
    59  
    60  func (s *executor) Cleanup() {
    61  	s.sshCommand.Cleanup()
    62  	s.AbstractExecutor.Cleanup()
    63  }
    64  
    65  func init() {
    66  	options := executors.ExecutorOptions{
    67  		DefaultCustomBuildsDirEnabled: false,
    68  		DefaultBuildsDir:              "builds",
    69  		DefaultCacheDir:               "cache",
    70  		SharedBuildsDir:               true,
    71  		Shell: common.ShellScriptInfo{
    72  			Shell:         "bash",
    73  			Type:          common.LoginShell,
    74  			RunnerCommand: "gitlab-runner",
    75  		},
    76  		ShowHostname: true,
    77  	}
    78  
    79  	creator := func() common.Executor {
    80  		return &executor{
    81  			AbstractExecutor: executors.AbstractExecutor{
    82  				ExecutorOptions: options,
    83  			},
    84  		}
    85  	}
    86  
    87  	featuresUpdater := func(features *common.FeaturesInfo) {
    88  		features.Variables = true
    89  		features.Shared = true
    90  	}
    91  
    92  	common.RegisterExecutor("ssh", executors.DefaultExecutorProvider{
    93  		Creator:          creator,
    94  		FeaturesUpdater:  featuresUpdater,
    95  		DefaultShellName: options.Shell.Shell,
    96  	})
    97  }