-
Notifications
You must be signed in to change notification settings - Fork 1
/
executor.go
113 lines (92 loc) · 2.14 KB
/
executor.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
package common
import (
"context"
log "github.com/sirupsen/logrus"
)
type ExecutorData interface{}
type ExecutorCommand struct {
Script string
Predefined bool
Context context.Context
}
type ExecutorStage string
const (
ExecutorStageCreated ExecutorStage = "created"
ExecutorStagePrepare ExecutorStage = "prepare"
ExecutorStageFinish ExecutorStage = "finish"
ExecutorStageCleanup ExecutorStage = "cleanup"
)
type ExecutorPrepareOptions struct {
Config *RunnerConfig
Build *Build
Trace JobTrace
User string
Context context.Context
}
type Executor interface {
Shell() *ShellScriptInfo
Prepare(options ExecutorPrepareOptions) error
Run(cmd ExecutorCommand) error
Finish(err error)
Cleanup()
GetCurrentStage() ExecutorStage
SetCurrentStage(stage ExecutorStage)
}
type ExecutorProvider interface {
CanCreate() bool
Create() Executor
Acquire(config *RunnerConfig) (ExecutorData, error)
Release(config *RunnerConfig, data ExecutorData) error
GetFeatures(features *FeaturesInfo)
}
type BuildError struct {
Inner error
}
func (b *BuildError) Error() string {
if b.Inner == nil {
return "error"
}
return b.Inner.Error()
}
var executors map[string]ExecutorProvider
func RegisterExecutor(executor string, provider ExecutorProvider) {
log.Debugln("Registering", executor, "executor...")
if executors == nil {
executors = make(map[string]ExecutorProvider)
}
if _, ok := executors[executor]; ok {
panic("Executor already exist: " + executor)
}
executors[executor] = provider
}
func GetExecutor(executor string) ExecutorProvider {
if executors == nil {
return nil
}
provider, _ := executors[executor]
return provider
}
func GetExecutors() []string {
names := []string{}
if executors != nil {
for name := range executors {
names = append(names, name)
}
}
return names
}
func GetExecutorProviders() (providers []ExecutorProvider) {
if executors != nil {
for _, executorProvider := range executors {
providers = append(providers, executorProvider)
}
}
return
}
func NewExecutor(executor string) Executor {
provider := GetExecutor(executor)
if provider != nil {
return provider.Create()
}
return nil
}