/
run.go
209 lines (171 loc) · 4.61 KB
/
run.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package runner
import (
"fmt"
"log"
"github.com/taskcluster/taskcluster/v44/tools/worker-runner/cfg"
"github.com/taskcluster/taskcluster/v44/tools/worker-runner/errorreport"
"github.com/taskcluster/taskcluster/v44/tools/worker-runner/exit"
"github.com/taskcluster/taskcluster/v44/tools/worker-runner/files"
"github.com/taskcluster/taskcluster/v44/tools/worker-runner/logging"
loggingProtocol "github.com/taskcluster/taskcluster/v44/tools/worker-runner/logging/protocol"
"github.com/taskcluster/taskcluster/v44/tools/worker-runner/provider"
"github.com/taskcluster/taskcluster/v44/tools/worker-runner/registration"
"github.com/taskcluster/taskcluster/v44/tools/worker-runner/run"
"github.com/taskcluster/taskcluster/v44/tools/worker-runner/secrets"
"github.com/taskcluster/taskcluster/v44/tools/worker-runner/worker"
"github.com/taskcluster/taskcluster/v44/tools/workerproto"
)
// Run the worker. This embodies the execution of the start-worker command.
func Run(configFile string) (state run.State, err error) {
// load configuration
log.Printf("Loading worker-runner configuration from %s", configFile)
runnercfg, err := cfg.LoadRunnerConfig(configFile)
if err != nil {
err = fmt.Errorf("Error loading runner config file %s: %s", configFile, err)
return
}
logging.Configure(runnercfg)
runCached := false
if runnercfg.CacheOverRestarts != "" {
runCached, err = run.ReadCacheFile(&state, runnercfg.CacheOverRestarts)
if err != nil {
return
}
}
state.Lock()
state.WorkerConfig = state.WorkerConfig.Merge(runnercfg.WorkerConfig)
state.Unlock()
// initialize provider and (re)register the worker
provider, err := provider.New(runnercfg)
if err != nil {
return
}
reg := registration.New(runnercfg, &state)
er := errorreport.New(&state)
em := exit.New(runnercfg, &state)
if !runCached {
log.Printf("Configuring with provider %s", runnercfg.Provider.ProviderType)
err = provider.ConfigureRun(&state)
if err != nil {
return
}
workerIdentityProof, err2 := provider.GetWorkerIdentityProof()
if err2 != nil {
err = err2
return
}
if workerIdentityProof != nil {
err = reg.RegisterWorker(workerIdentityProof)
if err != nil {
return
}
}
} else {
err = provider.UseCachedRun(&state)
if err != nil {
return
}
err = reg.UseCachedRun()
if err != nil {
return
}
}
err = state.CheckProviderResults()
if err != nil {
return
}
// log the worker identity; this is useful for finding the worker in logfiles
state.Lock()
log.Printf("Identified as worker %s/%s", state.WorkerGroup, state.WorkerID)
state.Unlock()
// fetch secrets
if !runCached && runnercfg.GetSecrets {
log.Println("Getting secrets from secrets service")
err = secrets.ConfigureRun(runnercfg, &state)
if err != nil {
return
}
}
// initialize worker
worker, err := worker.New(runnercfg)
if err != nil {
return
}
if !runCached {
log.Printf("Configuring for worker implementation %s", runnercfg.WorkerImplementation.Implementation)
err = worker.ConfigureRun(&state)
if err != nil {
return
}
} else {
err = worker.UseCachedRun(&state)
if err != nil {
return
}
}
// cache the state if we might end up restarting
if !runCached && runnercfg.CacheOverRestarts != "" {
err = state.WriteCacheFile(runnercfg.CacheOverRestarts)
if err != nil {
return
}
}
// extract files
if !runCached {
log.Printf("Writing files")
state.Lock()
err = files.ExtractAll(state.Files)
state.Unlock()
if err != nil {
return
}
}
// start
log.Printf("Starting worker")
transp, err := worker.StartWorker(&state)
if err != nil {
return
}
// set up protocol
proto := workerproto.NewProtocol(transp)
// inform other components about the protocol
loggingProtocol.SetProtocol(proto)
provider.SetProtocol(proto)
worker.SetProtocol(proto)
reg.SetProtocol(proto)
er.SetProtocol(proto)
em.SetProtocol(proto)
// call the WorkerStarted methods before starting the proto so that there
// are no race conditions around the capabilities negotiation
err = reg.WorkerStarted()
if err != nil {
return
}
err = provider.WorkerStarted(&state)
if err != nil {
return
}
proto.Start(false)
// wait for the worker to terminate, first reading everything from the
// protocol to capture any output just before the process exited
proto.WaitForEOF()
err = worker.Wait()
if err != nil {
return
}
// shut things down
// NOTE: this section is not reached for generic-worker reboots.
err = provider.WorkerFinished(&state)
if err != nil {
return
}
err = reg.WorkerFinished()
if err != nil {
return
}
err = em.WorkerFinished()
if err != nil {
return
}
return
}