Commit e437e5b8 authored by Kamil Trzciński's avatar Kamil Trzciński 🔴

Introduce cancelable prepare contexes

parent c7c183b7
......@@ -292,6 +292,13 @@ func (b *Build) run(executor Executor) (err error) {
}
func (b *Build) retryCreateExecutor(globalConfig *Config, provider ExecutorProvider, logger BuildLogger) (executor Executor, err error) {
options := ExecutorPrepareOptions{
Config: b.Runner,
Build: b,
Trace: b.Trace,
User: globalConfig.User,
}
for tries := 0; tries < PreparationRetries; tries++ {
executor = provider.Create()
if executor == nil {
......@@ -301,7 +308,7 @@ func (b *Build) retryCreateExecutor(globalConfig *Config, provider ExecutorProvi
b.executorStageResolver = executor.GetCurrentStage
err = executor.Prepare(globalConfig, b.Runner, b)
err = executor.Prepare(options)
if err == nil {
break
}
......
......@@ -21,9 +21,16 @@ const (
ExecutorStageCleanup ExecutorStage = "cleanup"
)
type ExecutorPrepareOptions struct {
Config *RunnerConfig
Build *Build
Trace JobTrace
User string
}
type Executor interface {
Shell() *ShellScriptInfo
Prepare(globalConfig *Config, config *RunnerConfig, build *Build) error
Prepare(options ExecutorPrepareOptions) error
Run(cmd ExecutorCommand) error
Finish(err error)
Cleanup()
......
package docker
import "time"
const DockerAPIVersion = "1.18"
const dockerLabelPrefix = "com.gitlab.gitlab-runner"
const prebuiltImageName = "gitlab/gitlab-runner-helper"
const prebuiltImageExtension = ".tar.xz"
const dockerCleanupTimeout = 5 * time.Minute
This diff is collapsed.
......@@ -15,8 +15,8 @@ type commandExecutor struct {
buildContainer *types.ContainerJSON
}
func (s *commandExecutor) Prepare(globalConfig *common.Config, config *common.RunnerConfig, build *common.Build) error {
err := s.executor.Prepare(globalConfig, config, build)
func (s *commandExecutor) Prepare(options common.ExecutorPrepareOptions) error {
err := s.executor.Prepare(options)
if err != nil {
return err
}
......
......@@ -8,8 +8,6 @@ import (
"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"
"golang.org/x/net/context"
)
type sshExecutor struct {
......@@ -17,8 +15,8 @@ type sshExecutor struct {
sshCommand ssh.Client
}
func (s *sshExecutor) Prepare(globalConfig *common.Config, config *common.RunnerConfig, build *common.Build) error {
err := s.executor.Prepare(globalConfig, config, build)
func (s *sshExecutor) Prepare(options common.ExecutorPrepareOptions) error {
err := s.executor.Prepare(options)
if err != nil {
return err
}
......@@ -41,12 +39,12 @@ func (s *sshExecutor) Prepare(globalConfig *common.Config, config *common.Runner
}
s.Debugln("Starting container", container.ID, "...")
err = s.client.ContainerStart(context.TODO(), container.ID, types.ContainerStartOptions{})
err = s.client.ContainerStart(s.context, container.ID, types.ContainerStartOptions{})
if err != nil {
return err
}
containerData, err := s.client.ContainerInspect(context.TODO(), container.ID)
containerData, err := s.client.ContainerInspect(s.context, container.ID)
if err != nil {
return err
}
......
......@@ -56,22 +56,22 @@ func (e *machineExecutor) Shell() *common.ShellScriptInfo {
return e.executor.Shell()
}
func (e *machineExecutor) Prepare(globalConfig *common.Config, config *common.RunnerConfig, build *common.Build) (err error) {
e.build = build
func (e *machineExecutor) Prepare(options common.ExecutorPrepareOptions) (err error) {
e.build = options.Build
// Use the machine
e.SetCurrentStage(DockerMachineExecutorStageUseMachine)
e.config, e.data, err = e.provider.Use(config, build.ExecutorData)
e.config, e.data, err = e.provider.Use(options.Config, options.Build.ExecutorData)
if err != nil {
return err
}
// TODO: Currently the docker-machine doesn't support multiple builds
build.ProjectRunnerID = 0
if details, _ := build.ExecutorData.(*machineDetails); details != nil {
build.Hostname = details.Name
e.build.ProjectRunnerID = 0
if details, _ := options.Build.ExecutorData.(*machineDetails); details != nil {
options.Build.Hostname = details.Name
} else if details, _ := e.data.(*machineDetails); details != nil {
build.Hostname = details.Name
options.Build.Hostname = details.Name
}
e.log().Infoln("Starting docker-machine build...")
......@@ -81,7 +81,7 @@ func (e *machineExecutor) Prepare(globalConfig *common.Config, config *common.Ru
if e.executor == nil {
return errors.New("failed to create an executor")
}
return e.executor.Prepare(globalConfig, &e.config, build)
return e.executor.Prepare(options)
}
func (e *machineExecutor) Run(cmd common.ExecutorCommand) error {
......
......@@ -71,12 +71,12 @@ func (e *AbstractExecutor) Shell() *common.ShellScriptInfo {
return &e.ExecutorOptions.Shell
}
func (e *AbstractExecutor) Prepare(globalConfig *common.Config, config *common.RunnerConfig, build *common.Build) error {
func (e *AbstractExecutor) Prepare(options common.ExecutorPrepareOptions) error {
e.currentStage = common.ExecutorStagePrepare
e.Config = *config
e.Build = build
e.BuildTrace = build.Trace
e.BuildLogger = common.NewBuildLogger(build.Trace, build.Log())
e.Config = *options.Config
e.Build = options.Build
e.BuildTrace = options.Trace
e.BuildLogger = common.NewBuildLogger(options.Build.Trace, options.Build.Log())
err := e.startBuild()
if err != nil {
......
......@@ -83,8 +83,8 @@ func (s *executor) setupResources() error {
return nil
}
func (s *executor) Prepare(globalConfig *common.Config, config *common.RunnerConfig, job *common.Build) (err error) {
if err = s.AbstractExecutor.Prepare(globalConfig, config, job); err != nil {
func (s *executor) Prepare(options common.ExecutorPrepareOptions) (err error) {
if err = s.AbstractExecutor.Prepare(options); err != nil {
return err
}
......@@ -92,7 +92,7 @@ func (s *executor) Prepare(globalConfig *common.Config, config *common.RunnerCon
return fmt.Errorf("kubernetes doesn't support shells that require script file")
}
if s.kubeClient, err = getKubeClient(config.Kubernetes); err != nil {
if s.kubeClient, err = getKubeClient(options.Config.Kubernetes); err != nil {
return fmt.Errorf("error connecting to Kubernetes: %s", err.Error())
}
......@@ -104,11 +104,11 @@ func (s *executor) Prepare(globalConfig *common.Config, config *common.RunnerCon
return err
}
if err = s.overwriteNamespace(job); err != nil {
if err = s.overwriteNamespace(options.Build); err != nil {
return err
}
s.prepareOptions(job)
s.prepareOptions(options.Build)
if err = s.checkDefaults(); err != nil {
return err
......
......@@ -153,8 +153,8 @@ func (s *executor) createVM() error {
return nil
}
func (s *executor) Prepare(globalConfig *common.Config, config *common.RunnerConfig, build *common.Build) error {
err := s.AbstractExecutor.Prepare(globalConfig, config, build)
func (s *executor) Prepare(options common.ExecutorPrepareOptions) error {
err := s.AbstractExecutor.Prepare(options)
if err != nil {
return err
}
......
......@@ -21,9 +21,9 @@ type executor struct {
executors.AbstractExecutor
}
func (s *executor) Prepare(globalConfig *common.Config, config *common.RunnerConfig, build *common.Build) error {
if globalConfig != nil {
s.Shell().User = globalConfig.User
func (s *executor) Prepare(options common.ExecutorPrepareOptions) error {
if options.User != "" {
s.Shell().User = options.User
}
// expand environment variables to have current directory
......@@ -45,7 +45,7 @@ func (s *executor) Prepare(globalConfig *common.Config, config *common.RunnerCon
s.DefaultCacheDir = os.Expand(s.DefaultCacheDir, mapping)
// Pass control to executor
err = s.AbstractExecutor.Prepare(globalConfig, config, build)
err = s.AbstractExecutor.Prepare(options)
if err != nil {
return err
}
......
......@@ -13,8 +13,8 @@ type executor struct {
sshCommand ssh.Client
}
func (s *executor) Prepare(globalConfig *common.Config, config *common.RunnerConfig, build *common.Build) error {
err := s.AbstractExecutor.Prepare(globalConfig, config, build)
func (s *executor) Prepare(options common.ExecutorPrepareOptions) error {
err := s.AbstractExecutor.Prepare(options)
if err != nil {
return err
}
......
......@@ -147,8 +147,8 @@ func (s *executor) createVM(vmName string) (err error) {
return nil
}
func (s *executor) Prepare(globalConfig *common.Config, config *common.RunnerConfig, build *common.Build) error {
err := s.AbstractExecutor.Prepare(globalConfig, config, build)
func (s *executor) Prepare(options common.ExecutorPrepareOptions) error {
err := s.AbstractExecutor.Prepare(options)
if err != nil {
return err
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment