gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+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 SharedBuildsDir: false, 90 Shell: common.ShellScriptInfo{ 91 Shell: "bash", 92 Type: common.LoginShell, 93 RunnerCommand: "gitlab-runner", 94 }, 95 ShowHostname: true, 96 Metadata: map[string]string{ 97 metadataOSType: osTypeLinux, 98 }, 99 } 100 101 creator := func() common.Executor { 102 e := &sshExecutor{ 103 executor: executor{ 104 AbstractExecutor: executors.AbstractExecutor{ 105 ExecutorOptions: options, 106 }, 107 volumeParser: parser.NewLinuxParser(), 108 }, 109 } 110 e.SetCurrentStage(common.ExecutorStageCreated) 111 return e 112 } 113 114 featuresUpdater := func(features *common.FeaturesInfo) { 115 features.Variables = true 116 features.Image = true 117 features.Services = true 118 } 119 120 common.RegisterExecutor("docker-ssh", executors.DefaultExecutorProvider{ 121 Creator: creator, 122 FeaturesUpdater: featuresUpdater, 123 DefaultShellName: options.Shell.Shell, 124 }) 125 }