github.com/secure-build/gitlab-runner@v12.5.0+incompatible/executors/docker/machine/executor.go (about) 1 package machine 2 3 import ( 4 "errors" 5 "time" 6 7 "github.com/sirupsen/logrus" 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 const ( 16 DockerMachineExecutorStageUseMachine common.ExecutorStage = "docker_machine_use_machine" 17 DockerMachineExecutorStageReleaseMachine common.ExecutorStage = "docker_machine_release_machine" 18 ) 19 20 type machineExecutor struct { 21 provider *machineProvider 22 executor common.Executor 23 build *common.Build 24 data common.ExecutorData 25 config common.RunnerConfig 26 27 currentStage common.ExecutorStage 28 } 29 30 func (e *machineExecutor) log() (log *logrus.Entry) { 31 log = e.build.Log() 32 33 details, _ := e.build.ExecutorData.(*machineDetails) 34 if details == nil { 35 details, _ = e.data.(*machineDetails) 36 } 37 if details != nil { 38 log = log.WithFields(logrus.Fields{ 39 "name": details.Name, 40 "usedcount": details.UsedCount, 41 "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 return 50 } 51 52 func (e *machineExecutor) Shell() *common.ShellScriptInfo { 53 if e.executor == nil { 54 return nil 55 } 56 return e.executor.Shell() 57 } 58 59 func (e *machineExecutor) Prepare(options common.ExecutorPrepareOptions) (err error) { 60 e.build = options.Build 61 62 if options.Config.Docker == nil { 63 options.Config.Docker = &common.DockerConfig{} 64 } 65 66 // Use the machine 67 e.SetCurrentStage(DockerMachineExecutorStageUseMachine) 68 e.config, e.data, err = e.provider.Use(options.Config, options.Build.ExecutorData) 69 if err != nil { 70 return err 71 } 72 options.Config.Docker.DockerCredentials = e.config.Docker.DockerCredentials 73 74 // TODO: Currently the docker-machine doesn't support multiple builds 75 e.build.ProjectRunnerID = 0 76 if details, _ := options.Build.ExecutorData.(*machineDetails); details != nil { 77 options.Build.Hostname = details.Name 78 } else if details, _ := e.data.(*machineDetails); details != nil { 79 options.Build.Hostname = details.Name 80 } 81 82 e.log().Infoln("Starting docker-machine build...") 83 84 // Create original executor 85 e.executor = e.provider.provider.Create() 86 if e.executor == nil { 87 return errors.New("failed to create an executor") 88 } 89 90 return e.executor.Prepare(options) 91 } 92 93 func (e *machineExecutor) Run(cmd common.ExecutorCommand) error { 94 if e.executor == nil { 95 return errors.New("missing executor") 96 } 97 return e.executor.Run(cmd) 98 } 99 100 func (e *machineExecutor) Finish(err error) { 101 if e.executor != nil { 102 e.executor.Finish(err) 103 } 104 e.log().Infoln("Finished docker-machine build:", err) 105 } 106 107 func (e *machineExecutor) Cleanup() { 108 // Cleanup executor if were created 109 if e.executor != nil { 110 e.executor.Cleanup() 111 } 112 113 // Release allocated machine 114 if e.data != nil { 115 e.SetCurrentStage(DockerMachineExecutorStageReleaseMachine) 116 e.provider.Release(&e.config, e.data) 117 e.data = nil 118 } 119 } 120 121 func (e *machineExecutor) GetCurrentStage() common.ExecutorStage { 122 if e.executor == nil { 123 return common.ExecutorStage("") 124 } 125 126 return e.executor.GetCurrentStage() 127 } 128 129 func (e *machineExecutor) SetCurrentStage(stage common.ExecutorStage) { 130 if e.executor == nil { 131 e.currentStage = stage 132 return 133 } 134 135 e.executor.SetCurrentStage(stage) 136 } 137 138 func init() { 139 common.RegisterExecutor("docker+machine", newMachineProvider("docker+machine", "docker")) 140 common.RegisterExecutor("docker-ssh+machine", newMachineProvider("docker-ssh+machine", "docker-ssh")) 141 }