Forum | Documentation | Website | Blog

Skip to content
Snippets Groups Projects
executor.go 3.38 KiB
Newer Older
Kamil Trzcinski's avatar
Kamil Trzcinski committed
	"errors"
Kamil Trzcinski's avatar
Kamil Trzcinski committed

	"github.com/sirupsen/logrus"
Kamil Trzcinski's avatar
Kamil Trzcinski committed

	"gitlab.com/gitlab-org/gitlab-runner/common"

	// Force to load docker executor
	_ "gitlab.com/gitlab-org/gitlab-runner/executors/docker"
const (
	DockerMachineExecutorStageUseMachine     common.ExecutorStage = "docker_machine_use_machine"
	DockerMachineExecutorStageReleaseMachine common.ExecutorStage = "docker_machine_release_machine"
)

Tomasz Maczukin's avatar
Tomasz Maczukin committed
	provider *machineProvider
	data     common.ExecutorData
	config   common.RunnerConfig

	currentStage common.ExecutorStage
func (e *machineExecutor) log() (log *logrus.Entry) {
	log = e.build.Log()

	details, _ := e.build.ExecutorData.(*machineDetails)
	if details == nil {
		details, _ = e.data.(*machineDetails)
	}
	if details != nil {
		log = log.WithFields(logrus.Fields{
			"name":      details.Name,
			"usedcount": details.UsedCount,
			"created":   details.Created,
		log = log.WithField("docker", e.config.Docker.Host)
func (e *machineExecutor) Shell() *common.ShellScriptInfo {
	if e.executor == nil {
		return nil
	}
	return e.executor.Shell()
}

func (e *machineExecutor) Prepare(options common.ExecutorPrepareOptions) (err error) {
	e.build = options.Build
	if options.Config.Docker == nil {
		options.Config.Docker = &common.DockerConfig{}
	}

	e.SetCurrentStage(DockerMachineExecutorStageUseMachine)
	e.config, e.data, err = e.provider.Use(options.Config, options.Build.ExecutorData)
	options.Config.Docker.DockerCredentials = e.config.Docker.DockerCredentials

	// TODO: Currently the docker-machine doesn't support multiple builds
	e.build.ProjectRunnerID = 0
	if details, _ := options.Build.ExecutorData.(*machineDetails); details != nil {
		options.Build.Hostname = details.Name
Kamil Trzcinski's avatar
Kamil Trzcinski committed
	} else if details, _ := e.data.(*machineDetails); details != nil {
		options.Build.Hostname = details.Name
Kamil Trzcinski's avatar
Kamil Trzcinski committed
	}

	e.log().Infoln("Starting docker-machine build...")

Tomasz Maczukin's avatar
Tomasz Maczukin committed
	e.executor = e.provider.provider.Create()
	if e.executor == nil {
		return errors.New("failed to create an executor")
	}
	return e.executor.Prepare(options)
func (e *machineExecutor) Run(cmd common.ExecutorCommand) error {
	if e.executor == nil {
		return errors.New("missing executor")
	}
	return e.executor.Run(cmd)
}

func (e *machineExecutor) Finish(err error) {
	e.log().Infoln("Finished docker-machine build:", err)
}

func (e *machineExecutor) Cleanup() {
	// Cleanup executor if were created
	if e.executor != nil {
		e.executor.Cleanup()
	}
		e.SetCurrentStage(DockerMachineExecutorStageReleaseMachine)
		e.provider.Release(&e.config, e.data)
		e.data = nil
	}
}

func (e *machineExecutor) GetCurrentStage() common.ExecutorStage {
	if e.executor == nil {
		return common.ExecutorStage("")
	}

	return e.executor.GetCurrentStage()
}

func (e *machineExecutor) SetCurrentStage(stage common.ExecutorStage) {
	if e.executor == nil {
		e.currentStage = stage
		return
	}

	e.executor.SetCurrentStage(stage)
}

	common.RegisterExecutor("docker+machine", newMachineProvider("docker_machines", "docker"))
	common.RegisterExecutor("docker-ssh+machine", newMachineProvider("ssh_docker_machines", "docker-ssh"))