-
Notifications
You must be signed in to change notification settings - Fork 899
/
ExecutionController.groovy
676 lines (635 loc) · 25.9 KB
/
ExecutionController.groovy
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
import com.dtolabs.rundeck.core.common.Framework
import java.text.SimpleDateFormat
import grails.converters.JSON
import com.dtolabs.client.utils.Constants
/**
* ExecutionController
*/
class ExecutionController {
FrameworkService frameworkService
ExecutionService executionService
ScheduledExecutionService scheduledExecutionService
def index ={
redirect(controller:'menu',action:'index')
}
def follow ={
return render(view:'show',model:show())
}
def followFragment ={
return render(view:'showFragment',model:show())
}
def show ={
def Execution e = Execution.get(params.id)
if(!e){
log.error("Execution not found for id: "+params.id)
flash.error = "Execution not found for id: "+params.id
return render(template:"/common/error")
}
def filesize=-1
if(null!=e.outputfilepath){
def file = new File(e.outputfilepath)
if (file.exists()) {
filesize = file.length()
}
}
Framework framework = frameworkService.getFrameworkFromUserSession(session,request)
if(e.scheduledExecution){
def ScheduledExecution scheduledExecution = e.scheduledExecution //ScheduledExecution.get(e.scheduledExecutionId)
return [scheduledExecution: scheduledExecution, execution:e, filesize:filesize]
}else{
return [execution:e, filesize:filesize]
}
}
def mail ={
def Execution e = Execution.get(params.id)
if(!e){
log.error("Execution not found for id: "+params.id)
flash.error = "Execution not found for id: "+params.id
return render(template:"/common/error")
}
def file = new File(e.outputfilepath)
def filesize=-1
if (file.exists()) {
filesize = file.length()
}
Framework framework = frameworkService.getFrameworkFromUserSession(session,request)
if(e.scheduledExecution){
def ScheduledExecution se = e.scheduledExecution //ScheduledExecution.get(e.scheduledExecutionId)
return render(view:"mailNotification/status" ,model: [scheduledExecution: se, execution:e, filesize:filesize])
}else{
return render(view:"mailNotification/status" ,model: [execution:e, filesize:filesize])
}
}
def xmlerror={
render(contentType:"text/xml",encoding:"UTF-8"){
result(error:"true"){
delegate.'error'{
if(flash.error){
response.setHeader(Constants.X_RUNDECK_RESULT_HEADER,flash.error)
delegate.'message'(flash.error)
}
if(flash.errors){
def p = delegate
flash.errors.each{ msg ->
p.'message'(msg)
}
}
}
}
}
}
def cancelExecution = {
def Execution e = Execution.get(params.id)
if(!e){
log.error("Execution not found for id: "+params.id)
flash.error = "Execution not found for id: "+params.id
return withFormat {
json{
render(contentType:"text/json"){
delegate.cancelled(false)
delegate.status(statusStr?statusStr:(didcancel?'killed':'failed'))
}
}
xml {
xmlerror.call()
}
}
}
def ScheduledExecution se = e.scheduledExecution
def ident = scheduledExecutionService.getJobIdent(se,e)
def didcancel
def statusStr
if(scheduledExecutionService.existsJob(ident.jobname, ident.groupname)){
didcancel=scheduledExecutionService.interruptJob(ident.jobname, ident.groupname)
}else if(null==e.dateCompleted){
executionService.saveExecutionState(
se?se.id:null,
e.id,
[
status:String.valueOf(false),
dateCompleted:new Date(),
cancelled:true
]
)
didcancel=true
}else{
didcancel=e.cancelled || 'true'!=e.status
statusStr='previously '+('true'==e.status?'success':e.cancelled?'killed':'failed')
}
withFormat{
json{
render(contentType:"text/json"){
delegate.cancelled(didcancel)
delegate.status(statusStr?statusStr:(didcancel?'killed':'failed'))
}
}
xml {
render(contentType:"text/xml",encoding:"UTF-8"){
result(error:false,success:didcancel){
success{
message("Job status: ${statusStr?statusStr:(didcancel?'killed': 'failed')}")
}
}
}
}
}
}
def downloadOutput = {
Execution e = Execution.get(Long.parseLong(params.id))
if(!e){
log.error("Execution with id "+params.id+" not found")
flash.error="No Execution found for id: " + params.id
flash.message="No Execution found for id: " + params.id
return
}
def jobcomplete = e.dateCompleted!=null
def file = new File(e.outputfilepath)
if (! file.exists()) {
log.error("File not found: "+e.outputfilepath)
return
} else if (file.size() < 1) {
log.error("File is empty")
return
}
def offset = 0
def initoffset=offset
def lastoff=offset
def lastmesgoff=offset
def storeoffset=offset
def entry=[]
StringBuffer buf = new StringBuffer();
def completed=false
def lines=0
def chars=0;
def tot=file.length()
//start at offset byte.
RandomAccessFile raf = new RandomAccessFile(file,"r")
def totsize = raf.getChannel().size()
def size = raf.getChannel().size()
def tstart=System.currentTimeMillis();
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyyMMdd-HHmmss",Locale.US);
def dateStamp= dateFormater.format(e.dateStarted);
response.setContentType("text/plain")
if("inline"!=params.view){
response.setHeader("Content-Disposition","attachment; filename=\"${e.scheduledExecution?e.scheduledExecution.jobName:e.name}-${dateStamp}.txt\"")
}
def isFormatted = "true"==servletContext.getAttribute("output.download.formatted")
if(params.formatted){
isFormatted = "true"==params.formatted
}
def Map result = parseOutput(file,0,-1,null){ Map msgbuf ->
response.outputStream << (isFormatted?"${msgbuf.time} [${msgbuf.user}@${msgbuf.node} ${msgbuf.context} ${msgbuf.command}][${msgbuf.level}] ${msgbuf.mesg}" : msgbuf.mesg)
true
}
storeoffset=result.storeoffset
completed = result.completed
}
/**
* tailExecutionOutput action, used by execution/show.gsp view to display output inline
*/
def tailExecutionOutput = {
Execution e = Execution.get(Long.parseLong(params.id))
if(!e){
render(contentType:"text/json"){
error("Execution with id "+params.id+" not found")
id(params.id.toString())
dataoffset("0")
iscompleted(false)
entries(){
}
}
return;
}
def long start = System.currentTimeMillis()
def jobcomplete = e.dateCompleted!=null
def failednodes = e.failedNodeList?true:false
def jobstat = e.status
def jobcanc = e.cancelled
def execDuration = 0
if(null==e.outputfilepath){
//execution has not be started yet
render(contentType:"text/json"){
delegate.message("Unmodified")
delegate.id(params.id.toString())
delegate.dataoffset(params.offset.toString())
delegate.iscompleted(false)
delegate.jobcompleted(jobcomplete)
delegate.failednodes(failednodes)
delegate.jobstatus(jobstat)
delegate.jobcancelled(jobcanc)
delegate.duration(execDuration)
delegate.entries(){
}
}
return;
}
def file = new File(e.outputfilepath)
execDuration=(e.dateCompleted?e.dateCompleted.getTime():System.currentTimeMillis()) - e.dateStarted.getTime()
if (! file.exists()) {
log.error("File not found: "+e.outputfilepath)
render(contentType:"text/json"){
if(e.dateCompleted){
delegate.error("File not found: "+e.outputfilepath)
}else{
delegate.message("File not found: "+e.outputfilepath)
}
delegate.id(params.id.toString())
delegate.dataoffset("0")
delegate.iscompleted(false)
delegate.jobcompleted(jobcomplete)
delegate.failednodes(failednodes)
delegate.jobstatus(jobstat)
delegate.jobcancelled(jobcanc)
delegate.duration(execDuration)
delegate.entries(){
}
}
return
} else if (file.size() < 1) {
render(contentType:"text/json"){
if(e.dateCompleted){
delegate.error("File is empty")
}else{
delegate.message("File is empty")
}
delegate.id(params.id.toString())
delegate.dataoffset("0")
delegate.iscompleted(false)
delegate.jobcompleted(jobcomplete)
delegate.failednodes(failednodes)
delegate.jobstatus(jobstat)
delegate.jobcancelled(jobcanc)
delegate.duration(execDuration)
delegate.entries(){
}
}
return
}
if(params.lastmod){
def ll = Long.parseLong(params.lastmod);
if(file.lastModified() <= ll){
render(contentType:"text/json"){
delegate.message("Unmodified")
delegate.id(params.id.toString())
delegate.dataoffset(params.offset.toString())
delegate.iscompleted(false)
delegate.jobcompleted(jobcomplete)
delegate.failednodes(failednodes)
delegate.jobstatus(jobstat)
delegate.jobcancelled(jobcanc)
delegate.duration(execDuration)
delegate.entries(){
}
}
}
}
def Long offset = ((params.offset) ? Long.parseLong(params.offset) : 0)
def storeoffset=offset
def entry=[]
def completed=false
def totsize=file.length()
def isfollowoption=false
def lastlines=0
def max=0
if(params.lastlines){
//load only the last X lines of the file, by going to the end and searching backwards for the
//line-end textual format X times, then reset the offset to that point.
//we actually search for X+1 line-ends to find the one prior to the Xth line back
//and we add 1 more to account for the final ^^^END^^^\n line.
//TODO: modify seekBack to allow rewind beyond the ^^^END^^^\n at the end prior to doing reverse search
isfollowoption=true
lastlines=Integer.parseInt(params.lastlines)
def String lSep = System.getProperty("line.separator");
def seekoffset = com.dtolabs.rundeck.core.utils.Utility.seekBack(file,lastlines+2,"^^^${lSep}")
if(seekoffset>=0){
//we found X+2 line ends. now skip past the line-end at seekoffset, to get to the start
//of the next line (Xth message).
if(seekoffset>0){
seekoffset+="^^^${lSep}".length()
}
offset=seekoffset
max=lastlines+1
}
}
def String bufsizestr= servletContext.getAttribute("execution.follow.buffersize");
def Long bufsize= (bufsizestr?bufsizestr.toInteger():0);
if(bufsize<(25*1024)){
bufsize=25*1024
}
def Map result = parseOutput(file,offset,bufsize,null){ data ->
entry << data
(0==max || entry.size()<max)
}
storeoffset=result.storeoffset
completed = result.completed || (jobcomplete && storeoffset==totsize)
if("true" == servletContext.getAttribute("output.markdown.enabled") && !params.disableMarkdown){
entry.each{
if(it.mesg){
try{
it.mesghtml = it.mesg.decodeMarkdown()
}catch (Exception exc){
log.error("Markdown error: "+exc.getMessage(),exc)
}
}
}
}
long marktime=System.currentTimeMillis()
def lastmodl = file.lastModified()
def percent=100.0 * (((float)storeoffset)/((float)totsize))
render(contentType:"text/json"){
delegate.id(e.id.toString())
delegate.dataoffset(storeoffset.toString())
delegate.iscompleted(completed)
delegate.jobcompleted(jobcomplete)
delegate.failednodes(failednodes)
delegate.jobstatus(jobstat)
delegate.jobcancelled(jobcanc)
delegate.lastmod(lastmodl.toString())
delegate.duration(execDuration)
delegate.percentLoaded(percent)
delegate.totalsize(totsize)
delegate.entries(){
entry.each{
if(it.mesghtml){
delegate.entries(time: it.time.toString(), level:it.level, mesghtml:it.mesghtml, user:it.user, module:it.module, command:it.command, node:it.node, context:it.context)
}else{
delegate.entries(time: it.time.toString(), level:it.level, mesg:it.mesg, user:it.user, module:it.module, command:it.command, node:it.node, context:it.context)
}
}
}
}
}
/**
* parseOutput parses the output file starting at the given offset and with the given buffersize
* Calls the callback closure with a map of data each time an entry is completed.
* @param file the file to parse
* @param offset offset to start from
* @param bufsize maximum amount of data to read if greater than 0, otherwise read until EOF
* @param callback a closure that has 1 parameter, the map of data to read. if it returns false, reading will stop
* @return Map containing two keys, 'storeoffset': next offset to start at, if buffersize is defined, and 'completed': boolean value if the log file has been completely read
*/
public Map parseOutput ( File file, Long offset, Long bufsize = -1 ,def String encoding=null, Closure callback){
def initoffset=offset
def lastoff=offset
def lastmesgoff=offset
def Long storeoffset=offset
def entry=[]
def msgbuf =[:]
StringBuffer buf = new StringBuffer();
def completed=false
def lines=0
def chars=0;
def tot=file.length()
//start at offset byte.
//we use RandomAccessFile to call readLine and record the byte-oriented offset
RandomAccessFile raf = new RandomAccessFile(file,'r')
//we use InputStreamReader to read bytes to chars given encoding option
//in this case readLine may buffer bytes for decoding purposes, so the offset is not correct
final FileInputStream stream = new FileInputStream(file)
InputStreamReader fr
if(null!=encoding){
fr= new InputStreamReader(stream,encoding)
}else{
//use default encoding
fr= new InputStreamReader(stream)
}
def totsize = stream.getChannel().size()
def size = stream.getChannel().size()
if(offset>0){
stream.getChannel().position(offset)
raf.seek(offset)
}
// log.info("starting tailExecutionOutput: offset: "+offset+", completed: "+completed)
def String lSep = System.getProperty("line.separator");
def tstart=System.currentTimeMillis();
def String line = fr.readLine()
def oline=raf.readLine() //discarded content
def diff = System.currentTimeMillis()-tstart;
if(bufsize > 0 && size-offset > bufsize){
size = offset+bufsize
}
boolean done=false
while(offset<size && null != line && !done){
// log.info("readLine waited: "+diff);
lastoff=offset
offset=raf.getFilePointer()
if(line =~ /^\^\^\^END\^\^\^/){
completed=true;
storeoffset=offset;
if(msgbuf){
msgbuf.mesg+=buf.toString()
if(!callback(msgbuf)){
done=true
}
buf = new StringBuffer()
msgbuf=[:]
}
}
if (line =~ /^\^\^\^/){
if(msgbuf){
msgbuf.mesg+=buf.toString()
if(!callback(msgbuf)){
done=true
}
buf = new StringBuffer()
msgbuf=[:]
}
def temp = line.substring(3,line.length())
def boolean full=false;
if(temp =~ /\^\^\^$/ || temp == ""){
if(temp.length()>=3){
temp = temp.substring(0,temp.length()-3)
}
full=true
}
def arr = temp.split("\\|",ExecutionService.EXEC_FORMAT_SEQUENCE.size()+1)
if (arr.size() >= 3) {
def time = arr[0].trim()
def level = arr[1].trim()
def mesg;
def list = []
list.addAll(Arrays.asList(arr))
if(list.size()>=ExecutionService.EXEC_FORMAT_SEQUENCE.size()){
if(list.size() > ExecutionService.EXEC_FORMAT_SEQUENCE.size()){
//join last sections into one message.
def sb = new StringBuffer()
sb.append(list.get(ExecutionService.EXEC_FORMAT_SEQUENCE.size()).trim())
for(def i=ExecutionService.EXEC_FORMAT_SEQUENCE.size()+1;i<list.size();i++){
sb.append('|')
sb.append(list.get(i).trim())
}
mesg = sb.toString()
}else{
mesg=''
list<<mesg
}
}else if(list.size()>3 && list.size()<ExecutionService.EXEC_FORMAT_SEQUENCE.size()){
def sb = new StringBuffer()
sb.append(list.get(2).trim())
for(def i=3;i<list.size();i++){
sb.append('|')
sb.append(list.get(i).trim())
}
mesg = sb.toString()
}else{
mesg = list[list.size()-1].trim()
}
msgbuf= [time:time, level:level, mesg:mesg+lSep]
if(list.size()>=ExecutionService.EXEC_FORMAT_SEQUENCE.size() ){
for(def i=2;i<list.size()-1;i++){
msgbuf[ExecutionService.EXEC_FORMAT_SEQUENCE[i]]=list[i].trim()
}
}
if(full){
if(!callback(msgbuf)){
done=true
}
msgbuf=[:]
buf=new StringBuffer()
}
lastmesgoff=lastoff
}
if(full){
storeoffset=offset
}
}else if(line=~/\^\^\^$/ && msgbuf){
def temp = line.substring(0,line.length()-3)
buf << temp + lSep
msgbuf.mesg+=buf.toString()
if(!callback(msgbuf)){
done=true
}
buf = new StringBuffer()
msgbuf=[:]
storeoffset=offset
}else{
buf << line + lSep
}
tstart=System.currentTimeMillis();
line = fr.readLine()//kept
oline=raf.readLine()//discarded
diff=System.currentTimeMillis()-tstart;
}
fr.close()
raf.close()
if(msgbuf){
//incomplete message entry. We leave it until next time unless completed==true
if(completed){
msgbuf.mesg+=buf.toString()
callback(msgbuf)
buf = new StringBuffer()
}
}
long parsetime=System.currentTimeMillis()
return [storeoffset:storeoffset, completed:completed]
}
/**
* API actions
*/
public static String EXECUTION_RUNNING = "running"
public static String EXECUTION_SUCCEEDED = "succeeded"
public static String EXECUTION_FAILED = "failed"
public static String EXECUTION_ABORTED = "aborted"
public String getExecutionState(Execution e){
return null==e.dateCompleted?EXECUTION_RUNNING:"true"==e.status?EXECUTION_SUCCEEDED:e.cancelled?EXECUTION_ABORTED:EXECUTION_FAILED
}
/**
* Utility, render xml response for a list of executions
*/
public def renderApiExecutionListResultXML={execlist ->
return new ApiController().success{ delegate->
delegate.'executions'(count:execlist.size()){
execlist.each {Execution e ->
execution(
/** attributes **/
id: e.id,
href: g.createLink(controller: 'execution', action: 'follow', id: e.id, absolute: true),
status: getExecutionState(e)
) {
/** elements */
user(e.user)
delegate.'date-started'(unixtime: e.dateStarted.time, g.w3cDateValue(date: e.dateStarted))
if (null != e.dateCompleted) {
delegate.'date-ended'(unixtime: e.dateCompleted.time, g.w3cDateValue(date: e.dateCompleted))
}
if (e.scheduledExecution) {
job(id: e.scheduledExecution.id) {
name(e.scheduledExecution.jobName)
group(e.scheduledExecution.groupPath ?: '')
description(e.scheduledExecution.description)
}
}
description(ExecutionService.summarizeJob(e.scheduledExecution, e))
}
}
}
}
}
/**
* API: /api/execution/{id} , version 1.2
*/
def apiExecution={
def Execution e = Execution.get(params.id)
if (!e) {
flash.errorCode = "api.error.item.doesnotexist"
flash.errorArgs = ['Execution ID',params.id]
return chain(controller: 'api', action: 'renderError')
}
def filesize=-1
if(null!=e.outputfilepath){
def file = new File(e.outputfilepath)
if (file.exists()) {
filesize = file.length()
}
}
return renderApiExecutionListResultXML([e])
}
public static String ABORT_PENDING="pending"
public static String ABORT_ABORTED="aborted"
public static String ABORT_FAILED="failed"
/**
* API: /api/execution/{id}/abort, version 1.2
*/
def apiExecutionAbort={
def Execution e = Execution.get(params.id)
if (!e) {
flash.errorCode = "api.error.item.doesnotexist"
flash.errorArgs = ['Execution ID',params.id]
return chain(controller: 'api', action: 'renderError')
}
def ScheduledExecution se = e.scheduledExecution
def ident = scheduledExecutionService.getJobIdent(se,e)
def statusStr
def abortstate
def jobstate
if(scheduledExecutionService.existsJob(ident.jobname, ident.groupname)){
def didcancel=scheduledExecutionService.interruptJob(ident.jobname, ident.groupname)
abortstate=didcancel?ABORT_PENDING:ABORT_FAILED
jobstate=EXECUTION_RUNNING
}else if(null==e.dateCompleted){
executionService.saveExecutionState(
se?se.id:null,
e.id,
[
status:String.valueOf(false),
dateCompleted:new Date(),
cancelled:true
]
)
abortstate=ABORT_ABORTED
jobstate=EXECUTION_ABORTED
}else{
jobstate=getExecutionState(e)
statusStr='previously '+jobstate
abortstate=ABORT_FAILED
}
return new ApiController().success{ delegate->
delegate.'success'{
message("Execution status: ${statusStr?statusStr:jobstate}")
}
delegate.'abort'(status:abortstate){
execution(id:params.id, status: jobstate)
}
}
}
}