executor.go 3.38 KB
Newer Older
1 2 3
package machine

import (
Kamil Trzciński's avatar
Kamil Trzciński committed
4
	"errors"
5
	"time"
Kamil Trzciński's avatar
Kamil Trzciński committed
6

7
	"github.com/sirupsen/logrus"
Kamil Trzciński's avatar
Kamil Trzciński committed
8

9
	"gitlab.com/gitlab-org/gitlab-runner/common"
10 11

	// Force to load docker executor
12
	_ "gitlab.com/gitlab-org/gitlab-runner/executors/docker"
13 14
)

15 16 17 18 19
const (
	DockerMachineExecutorStageUseMachine     common.ExecutorStage = "docker_machine_use_machine"
	DockerMachineExecutorStageReleaseMachine common.ExecutorStage = "docker_machine_release_machine"
)

20
type machineExecutor struct {
Tomasz Maczukin's avatar
Tomasz Maczukin committed
21
	provider *machineProvider
22
	executor common.Executor
23
	build    *common.Build
24 25
	data     common.ExecutorData
	config   common.RunnerConfig
26 27

	currentStage common.ExecutorStage
28 29
}

30 31 32
func (e *machineExecutor) log() (log *logrus.Entry) {
	log = e.build.Log()

33 34 35 36 37
	details, _ := e.build.ExecutorData.(*machineDetails)
	if details == nil {
		details, _ = e.data.(*machineDetails)
	}
	if details != nil {
38 39 40 41
		log = log.WithFields(logrus.Fields{
			"name":      details.Name,
			"usedcount": details.UsedCount,
			"created":   details.Created,
42
			"now":       time.Now(),
43 44
		})
	}
45
	if e.config.Docker != nil {
46
		log = log.WithField("docker", e.config.Docker.Host)
47
	}
48 49 50 51

	return
}

52 53 54 55 56 57 58
func (e *machineExecutor) Shell() *common.ShellScriptInfo {
	if e.executor == nil {
		return nil
	}
	return e.executor.Shell()
}

59 60
func (e *machineExecutor) Prepare(options common.ExecutorPrepareOptions) (err error) {
	e.build = options.Build
61

62 63 64 65
	if options.Config.Docker == nil {
		options.Config.Docker = &common.DockerConfig{}
	}

66
	// Use the machine
67
	e.SetCurrentStage(DockerMachineExecutorStageUseMachine)
68
	e.config, e.data, err = e.provider.Use(options.Config, options.Build.ExecutorData)
69 70 71
	if err != nil {
		return err
	}
72
	options.Config.Docker.DockerCredentials = e.config.Docker.DockerCredentials
73 74

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

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

84
	// Create original executor
Tomasz Maczukin's avatar
Tomasz Maczukin committed
85
	e.executor = e.provider.provider.Create()
86 87 88
	if e.executor == nil {
		return errors.New("failed to create an executor")
	}
89

90
	return e.executor.Prepare(options)
91 92
}

93 94 95 96 97 98 99
func (e *machineExecutor) Run(cmd common.ExecutorCommand) error {
	if e.executor == nil {
		return errors.New("missing executor")
	}
	return e.executor.Run(cmd)
}

100
func (e *machineExecutor) Finish(err error) {
101 102 103
	if e.executor != nil {
		e.executor.Finish(err)
	}
104
	e.log().Infoln("Finished docker-machine build:", err)
105 106 107
}

func (e *machineExecutor) Cleanup() {
108 109 110 111
	// Cleanup executor if were created
	if e.executor != nil {
		e.executor.Cleanup()
	}
112 113

	// Release allocated machine
114
	if e.data != nil {
115
		e.SetCurrentStage(DockerMachineExecutorStageReleaseMachine)
116 117 118 119 120
		e.provider.Release(&e.config, e.data)
		e.data = nil
	}
}

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
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)
}

138
func init() {
139 140
	common.RegisterExecutor("docker+machine", newMachineProvider("docker+machine", "docker"))
	common.RegisterExecutor("docker-ssh+machine", newMachineProvider("docker-ssh+machine", "docker-ssh"))
141
}