github.com/alloyci/alloy-runner@v1.0.1-0.20180222164613-925503ccafd6/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 DefaultBuildsDir: "builds", 68 SharedBuildsDir: true, 69 Shell: common.ShellScriptInfo{ 70 Shell: "bash", 71 Type: common.LoginShell, 72 RunnerCommand: "gitlab-runner", 73 }, 74 ShowHostname: true, 75 } 76 77 creator := func() common.Executor { 78 return &executor{ 79 AbstractExecutor: executors.AbstractExecutor{ 80 ExecutorOptions: options, 81 }, 82 } 83 } 84 85 featuresUpdater := func(features *common.FeaturesInfo) { 86 features.Variables = true 87 features.Shared = true 88 } 89 90 common.RegisterExecutor("ssh", executors.DefaultExecutorProvider{ 91 Creator: creator, 92 FeaturesUpdater: featuresUpdater, 93 }) 94 }