-
Notifications
You must be signed in to change notification settings - Fork 9
/
condor.go
411 lines (357 loc) · 11.8 KB
/
condor.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//
// condor-launcher launches jobs on an HTCondor cluster.
//
// This service connects to an AMQP broker's "jobs" exchange and waits for
// messages sent with the "jobs.launches" key. It then turns the request
// into an iplant.cmd, config, job, and irods_config file in /tmp/<user>/<UUID>
// and calls out to condor_submit to submit the job to the cluster.
//
// Since it launches jobs by executing the condor_submit command it shouldn't
// run inside a Docker container. Our Condor cluster is moderately large and
// requires a lot of ports to be opened up, which doesn't play nicely with
// Docker.
//
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/exec"
"path"
"text/template"
"time"
"github.com/cyverse-de/configurate"
"github.com/cyverse-de/version"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gopkg.in/cyverse-de/messaging.v6"
"gopkg.in/cyverse-de/model.v4"
"github.com/spf13/viper"
"github.com/streadway/amqp"
jobs "gopkg.in/cyverse-de/job-templates.v6"
)
var log = logrus.WithFields(logrus.Fields{
"service": "condor-launcher",
"art-id": "condor-launcher",
"group": "org.cyverse",
})
func init() {
var err error
logrus.SetFormatter(&logrus.JSONFormatter{})
IRODSConfigTemplate, err = template.New("irods_config").Parse(IRODSConfigTemplateText)
if err != nil {
log.Fatal(errors.Wrap(err, "failed to parse irods config template text"))
}
}
func ackDelivery(delivery amqp.Delivery, logMsgOnErr string) {
if err := delivery.Ack(false); err != nil {
log.Error(errors.Wrap(err, logMsgOnErr))
}
}
func rejectDelivery(delivery amqp.Delivery, requeue bool, logMsgOnErr string) {
if err := delivery.Reject(requeue); err != nil {
log.Error(errors.Wrap(err, logMsgOnErr))
}
}
// Messenger defines an interface for handling AMQP operations. This is the
// subset of functionality needed by job-status-recorder.
type Messenger interface {
AddConsumer(string, string, string, string, messaging.MessageHandler, int)
Close()
Listen()
Publish(string, []byte) error
SetupPublishing(string) error
PublishJobUpdate(*messaging.UpdateMessage) error
DeleteQueue(name string) error
}
// CondorLauncher contains the condor-launcher application state.
type CondorLauncher struct {
cfg *viper.Viper
client Messenger
fs fsys
condorSubmit string //path to the condor_submit executable
condorRm string // path to the condor_rm executable
}
// New returns a new *CondorLauncher
func New(c *viper.Viper, client Messenger, fs fsys, condorSubmit, condorRm string) *CondorLauncher {
return &CondorLauncher{
cfg: c,
client: client,
fs: fs,
condorSubmit: condorSubmit,
condorRm: condorRm,
}
}
func (cl *CondorLauncher) storeConfig(s *model.Job) error {
cfgData := &IRODSConfig{
IRODSHost: cl.cfg.GetString("irods.host"),
IRODSPort: cl.cfg.GetString("irods.port"),
IRODSUser: cl.cfg.GetString("irods.user"),
IRODSPass: cl.cfg.GetString("irods.pass"),
IRODSBase: cl.cfg.GetString("irods.base"),
IRODSResc: cl.cfg.GetString("irods.resc"),
IRODSZone: cl.cfg.GetString("irods.zone"),
}
fileContent, err := GenerateFile(IRODSConfigTemplate, cfgData)
if err != nil {
return err
}
log.Infof("generated the irods config for job %s", s.InvocationID)
sdir := s.CondorLogDirectory()
if path.Base(sdir) != "logs" {
sdir = path.Join(sdir, "logs")
}
fname := path.Join(sdir, "irods-config")
err = os.WriteFile(fname, fileContent.Bytes(), 0644)
if err != nil {
return errors.Wrapf(err, "failed to write to file %s", fname)
}
return nil
}
func (cl *CondorLauncher) launch(s *model.Job, condorPath, condorConfig string) (string, error) {
// Ensure that the logs directory exists for the job.
sdir := s.CondorLogDirectory()
if path.Base(sdir) != "logs" {
sdir = path.Join(sdir, "logs")
}
err := os.MkdirAll(sdir, 0755)
if err != nil {
return "", errors.Wrapf(err, "failed to create the directory %s", sdir)
}
if s.ExecutionTarget != "osg" {
// Write the irods configuration file to relevant locations
err = cl.storeConfig(s)
if err != nil {
return "", err
}
}
// Create a copy of the configuration to use for job submission
cfgCopy := CopyConfig(cl.cfg)
// Generate the submission files, always using the condor job submission format for now.
jobSubmissionBuilder, err := jobs.NewJobSubmissionBuilder(s.ExecutionTarget, cfgCopy)
if err != nil {
return "", err
}
submissionPath, err := jobSubmissionBuilder.Build(s, sdir)
if err != nil {
return "", err
}
// Submit the job to Condor.
cmd := exec.Command(cl.condorSubmit, submissionPath)
cmd.Dir = path.Dir(submissionPath)
cmd.Env = []string{
fmt.Sprintf("PATH=%s", condorPath),
fmt.Sprintf("CONDOR_CONFIG=%s", condorConfig),
}
output, err := cmd.CombinedOutput()
log.Infof("Output of condor_submit:\n%s\n", output)
if err != nil {
return "", errors.Wrapf(err, "failed to execute %s", cl.condorSubmit)
}
// Log the Condor job ID.
id := string(model.ExtractJobID(output))
log.Infof("Condor job id is %s\n", id)
return id, err
}
// handleLaunchRequests triggers Condor jobs in response to launch request messages.
func (cl *CondorLauncher) handleLaunchRequests(condorPath, condorConfig string) func(d amqp.Delivery) {
return func(delivery amqp.Delivery) {
body := delivery.Body
requeueOnErr := !delivery.Redelivered
req := messaging.JobRequest{}
err := json.Unmarshal(body, &req)
if err != nil {
log.Errorf("%+v\n", errors.Wrap(err, "failed to unmarshal launch request json"))
log.Error(string(body[:]))
rejectDelivery(delivery, requeueOnErr, "failed to Reject amqp Launch request delivery")
return
}
switch req.Command {
case messaging.Launch:
jobID, err := cl.launch(req.Job, condorPath, condorConfig)
if err != nil {
log.Errorf("%+v\n", err)
if !requeueOnErr {
err = cl.client.PublishJobUpdate(&messaging.UpdateMessage{
Job: req.Job,
State: messaging.FailedState,
Message: fmt.Sprintf("condor-launcher failed to launch job:\n %s", err),
})
if err != nil {
log.Errorf("%+v\n", errors.Wrap(err, "failed to publish launch failure job update"))
}
}
rejectDelivery(delivery, requeueOnErr, "failed to Reject amqp Launch request delivery")
} else {
log.Infof("Launched Condor ID %s", jobID)
err = cl.client.PublishJobUpdate(&messaging.UpdateMessage{
Job: req.Job,
State: messaging.SubmittedState,
Message: fmt.Sprintf("Launched Condor ID %s", jobID),
})
if err != nil {
log.Errorf("%+v\n", errors.Wrap(err, "failed to publish successful launch job update"))
}
ackDelivery(delivery, "failed to ACK amqp Launch request delivery")
}
default:
log.Errorf("condor_launches message handler got unrecognized command: %+v\n", req.Command)
ackDelivery(delivery, "failed to ACK amqp Launch request delivery")
}
}
}
func (cl *CondorLauncher) stopJob(invocationID, condorPath, condorConfig string) error {
var (
condorRMOutput []byte
err error
)
log.Infof("Running condor_rm for %s", invocationID)
if condorRMOutput, err = ExecCondorRm(invocationID, condorPath, condorConfig); err != nil {
log.Errorf("%+v\n", errors.Wrapf(err, "failed to run 'condor_rm %s'", invocationID))
return err
}
fauxJob := model.New(cl.cfg)
fauxJob.InvocationID = invocationID
update := &messaging.UpdateMessage{
Job: fauxJob,
State: messaging.FailedState,
Message: "Job was killed",
}
if err = cl.client.PublishJobUpdate(update); err != nil {
log.Errorf("%+v\n", errors.Wrap(err, "failed to publish job update for a stopped job"))
}
log.Infof("condor_rm output for job %s:\n%s", invocationID, condorRMOutput)
if err = cl.client.DeleteQueue(messaging.StopQueueName(invocationID)); err != nil {
log.Errorf("%+v\n", errors.Wrap(err, "failed to delete queue"))
}
return nil
}
func (cl *CondorLauncher) stopHandler(condorPath, condorConfig string) func(d amqp.Delivery) {
return func(d amqp.Delivery) {
var (
requeueOnErr bool
invID string
err error
)
requeueOnErr = !d.Redelivered
stopRequest := &messaging.StopRequest{}
if err = json.Unmarshal(d.Body, stopRequest); err != nil {
log.Errorf("%+v\n", errors.Wrap(err, "failed to unmarshal the stop request body"))
rejectDelivery(d, requeueOnErr, "failed to Reject StopRequest")
return
}
invID = stopRequest.InvocationID
if err = cl.stopJob(invID, condorPath, condorConfig); err != nil {
rejectDelivery(d, requeueOnErr, fmt.Sprintf("failed to Reject StopRequest for %s", invID))
} else {
ackDelivery(d, fmt.Sprintf("failed to ACK StopRequest for %s", invID))
}
}
}
func killHeldJobs(launcher *CondorLauncher, condorPath, condorConfig string) {
var (
err error
cmdOutput []byte
heldEntries []string
)
log.Infoln("Looking for jobs in the held state...")
if cmdOutput, err = ExecCondorQHeldIDs(condorPath, condorConfig); err != nil {
log.Errorf("%+v\n", errors.Wrap(err, "error running condor_q"))
return
}
heldEntries = heldQueueInvocationIDs(cmdOutput)
log.Infof("There are %d jobs in the held state", len(heldEntries))
for _, invocationID := range heldEntries {
if invocationID != "" {
log.Infof("Sending stop request for invocation id %s", invocationID)
if err = launcher.stopJob(invocationID, condorPath, condorConfig); err != nil {
log.Errorf("%+v\n", errors.Wrap(err, "error sending stop request"))
}
}
}
}
// startHeldTicker starts up the code that periodically fires and clean up held
// jobs
func startHeldTicker(launcher *CondorLauncher, condorPath, condorConfig string) (*time.Ticker, error) {
d, err := time.ParseDuration("30s")
if err != nil {
return nil, errors.Wrap(err, "failed to parse duration '30s'")
}
t := time.NewTicker(d)
go func(t *time.Ticker, launcher *CondorLauncher) {
for {
<-t.C
killHeldJobs(launcher, condorPath, condorConfig)
}
}(t, launcher)
return t, nil
}
func main() {
var (
cfgPath = flag.String("config", "", "Path to the config file. Required.")
showVersion = flag.Bool("version", false, "Print the version information")
err error
)
flag.Parse()
if *showVersion {
version.AppVersion()
os.Exit(0)
}
if *cfgPath == "" {
fmt.Println("Error: --config must be set.")
flag.PrintDefaults()
os.Exit(-1)
}
csPath := findExecPath("condor_submit")
crPath := findExecPath("condor_rm")
cfg, err := configurate.InitDefaults(*cfgPath, configurate.JobServicesDefaults)
if err != nil {
log.Fatalf("%+v\n", errors.Wrap(err, "failed to initialize configuration defaults"))
}
log.Infoln("Done reading config.")
uri := cfg.GetString("amqp.uri")
exchangeName := cfg.GetString("amqp.exchange.name")
exchangeType := cfg.GetString("amqp.exchange.type")
client, err := messaging.NewClient(uri, true)
if err != nil {
log.Fatalf("%+v\n", errors.Wrap(err, "failed to create new AMQP client"))
}
defer client.Close()
launcher := New(cfg, client, &osys{}, csPath, crPath)
err = launcher.client.SetupPublishing(exchangeName)
if err != nil {
log.Fatalf("%+v\n", errors.Wrap(err, "failed to setup publishing"))
}
go launcher.client.Listen()
condorPath := cfg.GetString("condor.path_env_var")
condorConfig := cfg.GetString("condor.condor_config")
ticker, err := startHeldTicker(
launcher,
condorPath,
condorConfig,
)
if err != nil {
log.Fatalf("%+v\n", err)
}
log.Infof("Started up the held state ticker: %#v", ticker)
launcher.client.AddConsumer(
exchangeName,
exchangeType,
"condor-launcher-stops",
messaging.StopRequestKey("*"),
launcher.stopHandler(condorPath, condorConfig),
cfg.GetInt("amqp.prefetch.stops"),
)
// Accept and handle messages sent out with the jobs.launches routing key.
launcher.client.AddConsumer(
exchangeName,
exchangeType,
"condor_launches",
messaging.LaunchesKey,
launcher.handleLaunchRequests(condorPath, condorConfig),
cfg.GetInt("amqp.prefetch.launches"),
)
spin := make(chan int)
<-spin
}