gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+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 SharedBuildsDir: true, 70 Shell: common.ShellScriptInfo{ 71 Shell: "bash", 72 Type: common.LoginShell, 73 RunnerCommand: "gitlab-runner", 74 }, 75 ShowHostname: true, 76 } 77 78 creator := func() common.Executor { 79 return &executor{ 80 AbstractExecutor: executors.AbstractExecutor{ 81 ExecutorOptions: options, 82 }, 83 } 84 } 85 86 featuresUpdater := func(features *common.FeaturesInfo) { 87 features.Variables = true 88 features.Shared = true 89 } 90 91 common.RegisterExecutor("ssh", executors.DefaultExecutorProvider{ 92 Creator: creator, 93 FeaturesUpdater: featuresUpdater, 94 DefaultShellName: options.Shell.Shell, 95 }) 96 }