forked from gitlabhq/gitlab-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor_docker_ssh.go
117 lines (99 loc) · 2.54 KB
/
executor_docker_ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package docker
import (
"errors"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/executors"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers/ssh"
)
type sshExecutor struct {
executor
sshCommand ssh.Client
}
func (s *sshExecutor) Prepare(globalConfig *common.Config, config *common.RunnerConfig, build *common.Build) error {
err := s.executor.Prepare(globalConfig, config, build)
if err != nil {
return err
}
if s.Config.SSH == nil {
return errors.New("Missing SSH configuration")
}
s.Debugln("Starting SSH command...")
imageName, err := s.getImageName()
if err != nil {
return err
}
// Start build container which will run actual build
container, err := s.createContainer("build", imageName, []string{})
if err != nil {
return err
}
s.Debugln("Starting container", container.ID, "...")
err = s.client.StartContainer(container.ID, nil)
if err != nil {
return err
}
containerData, err := s.client.InspectContainer(container.ID)
if err != nil {
return err
}
// Create SSH command
s.sshCommand = ssh.Client{
Config: *s.Config.SSH,
Stdout: s.BuildTrace,
Stderr: s.BuildTrace,
}
s.sshCommand.Host = containerData.NetworkSettings.IPAddress
s.Debugln("Connecting to SSH server...")
err = s.sshCommand.Connect()
if err != nil {
return err
}
return nil
}
func (s *sshExecutor) Run(cmd common.ExecutorCommand) error {
err := s.sshCommand.Run(ssh.Command{
Environment: s.BuildShell.Environment,
Command: s.BuildShell.GetCommandWithArguments(),
Stdin: cmd.Script,
Abort: cmd.Abort,
})
if _, ok := err.(*ssh.ExitError); ok {
err = &common.BuildError{Inner: err}
}
return err
}
func (s *sshExecutor) Cleanup() {
s.sshCommand.Cleanup()
s.executor.Cleanup()
}
func init() {
options := executors.ExecutorOptions{
DefaultBuildsDir: "builds",
SharedBuildsDir: false,
Shell: common.ShellScriptInfo{
Shell: "bash",
Type: common.LoginShell,
RunnerCommand: "gitlab-runner",
},
ShowHostname: true,
SupportedOptions: []string{"image", "services"},
}
creator := func() common.Executor {
return &sshExecutor{
executor: executor{
AbstractExecutor: executors.AbstractExecutor{
ExecutorOptions: options,
},
},
}
}
featuresUpdater := func(features *common.FeaturesInfo) {
features.Variables = true
features.Image = true
features.Services = true
}
common.RegisterExecutor("docker-ssh", executors.DefaultExecutorProvider{
Creator: creator,
FeaturesUpdater: featuresUpdater,
})
}