1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class gotomasses extends plugin
24 {
25 /* Definitions */
26 var $plHeadline = "Deployment status";
27 var $plDescription = "System deployment status";
28 var $plIcon = "plugins/goto/images/goto.png";
30 /* attribute list for save action */
31 var $attributes= array();
32 var $objectclasses= array();
34 /* Queue tasks */
35 var $current = FALSE;
36 var $dialog = FALSE;
37 var $ids_to_remove = array();
38 var $divlist = NULL;
40 var $events = array();
41 var $event_tags = array();
43 var $sort_by = "Schedule";
44 var $sort_dir = "up";
45 var $entries = array();
46 var $range = 25;
47 var $start = 0;
49 var $recently_removed = array();
51 function gotomasses(&$config, $dn= NULL)
52 {
53 /* Include config object */
54 $this->config= &$config;
55 $this->o_queue = new gosaSupportDaemon(TRUE,5);
56 $this->events = DaemonEvent::get_event_types( SYSTEM_EVENT);
58 /* Get tags that will be used in queue searches */
59 $this->event_tags = array("none");
60 foreach($this->events['SCHEDULED'] as $evt){
61 $this->event_tags[] = $evt['s_Queued_Action'];
62 }
64 /* Load filter settings */
65 if(!session::is_set("gotomasses_filter")){
66 $gotomasses_filter =
67 array(
68 "range" => $this->range,
69 "sort_by" => $this->sort_by,
70 "sort_dir" => $this->sort_dir);
71 session::set("gotomasses_filter",$gotomasses_filter);
72 }
73 $gotomasses_filter = session::get("gotomasses_filter");
74 foreach(array("range","sort_by","sort_dir") as $attr) {
75 $this->$attr = $gotomasses_filter[$attr];
76 }
77 }
80 function execute()
81 {
82 $smarty = get_smarty();
84 /************
85 * Handle posts
86 ************/
88 $s_entry = $s_action = "";
89 $arr = array(
91 "/^pause_/" => "pause",
92 "/^resume_/" => "resume",
93 "/^execute_process_/" => "execute_process",
94 "/^abort_process_/" => "abort_process",
96 "/^prio_up_/" => "prio_up",
97 "/^prio_down_/" => "prio_down",
99 "/^edit_task_/" => "edit",
100 "/^log_view_/" => "logview",
101 "/^remove_task_/" => "remove",
102 "/^new_task_/" => "new_task");;
105 foreach($arr as $regex => $action){
106 foreach($_POST as $name => $value){
107 if(preg_match($regex,$name)){
108 $s_action = $action;
109 $s_entry = preg_replace($regex,"",$name);
110 $s_entry = preg_replace("/_(x|y)$/","",$s_entry);
111 }
112 }
113 }
115 /* Menu actions */
116 if(isset($_POST['menu_action']) && !empty($_POST['menu_action'])){
117 $s_action = $_POST['menu_action'];
118 }
120 /* Edit posted from list link */
121 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
122 $s_action = "edit";
123 $s_entry = $_GET['id'];
124 }
127 /************
128 * Import CSV file
129 ************/
131 if($s_action == "import_file"){
132 $this->dialog = new goto_import_file($this->config,$this);
133 }
135 if(isset($_POST['import_abort'])){
136 $this->dialog = FALSE;
137 }
140 /************
141 * Handle Priority modifications
142 ************/
144 if(preg_match("/^prio_/",$s_action)){
145 switch($s_action){
146 case 'prio_down' : $this->update_priority($s_entry,"down");break;
147 case 'prio_up' : $this->update_priority($s_entry,"up");break;
148 }
149 }
151 /************
152 * Handle pause/resume/execute modifications
153 ************/
155 if(preg_match("/^resume/",$s_action) ||
156 preg_match("/^pause/",$s_action) ||
157 preg_match("/^abort_process/",$s_action) ||
158 preg_match("/^execute_process/",$s_action)){
160 switch($s_action){
161 case 'resume' : $this->resume_queue_entries (array($s_entry));break;
162 case 'pause' : $this->pause_queue_entries (array($s_entry));break;
163 case 'execute_process': $this->execute_queue_entries (array($s_entry));break;
164 case 'abort_process' : $this->abort_queue_entries (array($s_entry));break;
165 case 'resume_all' : $this->resume_queue_entries ($this->list_get_selected_items());break;
166 case 'pause_all' : $this->pause_queue_entries ($this->list_get_selected_items());break;
167 case 'execute_process_all': $this->execute_queue_entries ($this->list_get_selected_items());break;
168 case 'abort_process_all' : $this->abort_queue_entries ($this->list_get_selected_items());break;
170 default : trigger_error("Undefined action setting used (".$s_action.").");
171 }
172 if($this->o_queue->is_error()){
173 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
174 }
175 }
177 /************
178 * ADD
179 ************/
181 if(preg_match("/^add_event_/",$s_action)){
182 $type = preg_replace("/^add_event_/","",$s_action);
183 if(isset($this->events['BY_CLASS'][$type])){
184 $e_data = $this->events['BY_CLASS'][$type];
185 $this->dialog = new $e_data['CLASS_NAME']($this->config);
186 }
187 }
189 /************
190 * EDIT
191 ************/
193 if($s_action == "edit"){
194 $id = $s_entry;
195 $type = FALSE;
196 if(isset($this->entries[$id])){
197 $event = $this->entries[$s_entry];
198 if($event['STATUS'] == "waiting" && isset($this->events['QUEUED'][$event['HEADERTAG']])){
199 $evt_name = $this->events['QUEUED'][$event['HEADERTAG']];
200 $type = $this->events['BY_CLASS'][$evt_name];
201 $this->dialog = new $type['CLASS_NAME']($this->config,$event);
202 }
203 }
204 }
207 /************
208 * LOG VIEW
209 ************/
211 if($s_action == "logview"){
212 $id = $s_entry;
213 $type = FALSE;
214 if(isset($this->entries[$id])){
215 $event = $this->entries[$s_entry];
216 $this->dialog = new gotoLogView($this->config,"",$event,$this);
217 }
218 }
221 /************
222 * REMOVE
223 ************/
225 /* Remove multiple */
226 if($s_action == "remove_multiple" || $s_action == "remove"){
228 if(!$this->acl_is_removeable()){
229 msg_dialog::display(_("Permission"), msgPool::permDelete(), ERROR_DIALOG);
230 }else{
232 if($s_action == "remove"){
233 $ids = array($s_entry);
234 }else{
235 $ids = $this->list_get_selected_items();
236 }
238 $this->ids_to_remove = array();
240 if(count($ids)){
241 $ret = $this->o_queue->ids_exist($ids);
242 $ret = $this->o_queue->get_entries_by_id($ret);
243 $tmp = "";
245 $deleteable_jobs = array();
246 $not_deleteable_jobs = array();
247 foreach($ret as $task){
249 /* Create a printable job name/description */
250 if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
251 $evt_name = $this->events['QUEUED'][$task['HEADERTAG']];
252 $evt = $this->events['BY_CLASS'][$evt_name];
253 $j_name = $task['ID']." - ".$evt['s_Menu_Name']." ".$task['MACADDRESS'];
254 }else{
255 $j_name = $task['ID']." - ".$task['HEADERTAG']." ".$task['MACADDRESS'];
256 }
258 /* Only remove WAITING or ERROR entries */
259 if(in_array($task['STATUS'],array("waiting","error","processed")) ||
260 ($task['STATUS'] == "processing" && !preg_match("/install/",$task['HEADERTAG'])) ){
261 $this->ids_to_remove[] = $task['ID'];
262 $deleteable_jobs[] = $j_name;
263 }else{
264 $not_deleteable_jobs[] = $j_name;
265 }
266 }
267 if(count($not_deleteable_jobs)){
268 msg_dialog::display(_("Remove"),
269 sprintf(_("The following jobs couldn't be deleted, they have to be aborted: %s"),
270 "<br>".msgPool::buildList($not_deleteable_jobs)),INFO_DIALOG);
271 }
273 if(count($this->ids_to_remove)){
274 $smarty->assign("multiple", TRUE);
275 $smarty->assign("info",msgPool::deleteInfo($deleteable_jobs));
276 $this->current = $s_entry;
277 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
278 }
279 }
280 }
281 }
283 /* Remove specified tasks */
284 if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
286 /* Reboot hosts with not yet startet installations and timestamps in the past
287 */
288 timezone::get_default_timezone();
289 foreach($this->ids_to_remove as $id){
290 $entry = $this->o_queue->get_entries_by_id(array($id));
291 if(isset($entry['ANSWER1'])){
292 $entry = $entry['ANSWER1'];
293 if( $entry['STATUS'] == "waiting" &&
294 $entry['HEADERTAG'] == "trigger_action_reinstall"){
295 $evt = new DaemonEvent_reinstall($this->config,$entry);
296 if($evt->get_timestamp(FALSE) < time()){
297 $r_evt = new DaemonEvent_localboot($this->config);
298 $r_evt->add_targets(array($entry['MACADDRESS']));
299 $r_evt->set_type(TRIGGERED_EVENT);
300 $this->o_queue->append($r_evt);
301 }
302 }
303 }
304 }
306 $this->o_queue->remove_entries($this->ids_to_remove);
307 $this->save();
308 }
310 /* Remove aborted */
311 if(isset($_POST['delete_cancel'])){
312 $this->ids_to_remove = array();;
313 }
316 /************
317 * EDIT
318 ************/
320 /* Close dialog */
321 if(isset($_POST['save_event_dialog'])){
322 if(is_object($this->dialog)){
323 $this->dialog->save_object();
324 if(!$this->o_queue->append($this->dialog)){
325 msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->o_queue->get_error()),ERROR_DIALOG);
326 }else{
327 $this->dialog = FALSE;
328 $this->current = -1;
329 }
330 }
331 }
334 /* Close dialog */
335 if(isset($_POST['abort_event_dialog'])){
336 $this->dialog = FALSE;
337 $this->current = -1;
338 }
340 /* Display dialogs if currently opened */
341 if(is_object($this->dialog)){
342 $this->dialog->save_object();
343 $display = $this->dialog->execute();
345 if($this->dialog instanceOf goto_import_file && $this->dialog->import_successful){
346 $this->dialog = FALSE;
347 }else{
348 return($display);
349 }
350 }
352 /************
353 * Handle Divlist
354 ************/
356 $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
357 $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa tasks."));
358 $divlist->SetSummary(_("List of queued jobs"));
359 $divlist->EnableCloseButton(FALSE);
360 $divlist->EnableSaveButton(FALSE);
361 $divlist->SetHeadpageMode();
362 $s = ".|"._("Actions")."|\n";
363 $s.= "..|<img src='images/lists/new.png' alt='' border='0' class='center'> "._("Create")."\n";
365 foreach($this->events['SCHEDULED'] as $name => $event){
366 $s.= "...|".$event['MenuImage']." ".$event['s_Menu_Name']."|add_event_".$name."\n";
367 }
368 if($this->acl_is_removeable()){
369 $s.= "..|---|\n";
370 $s.= "..|<img src='images/lists/import.png' alt='' border='0' class='center'> "._("Import")."|import_file\n";
371 $s.= "..|<img src='images/lists/trash.png' alt='' border='0' class='center'> "._("Remove")."|remove_multiple\n";
372 }
373 if(preg_match("/w/",$this->getacl(""))){
374 $s.= "..|---|\n";
375 $s.= "..|<img alt='"._("Resume")."' src='images/status_start.png' border='0' class='center'> "._("Resume")."|resume_all\n";
376 $s.= "..|<img alt='"._("Pause")."' src='images/status_pause.png' border='0' class='center'> "._("Pause")."|pause_all\n";
377 $s.= "..|<img alt='"._("Abort")."' src='images/small_error.png' border='0' class='center'> "._("Abort")."|abort_process_all\n";
378 $s.= "..|<img alt='"._("Execute")."' src='images/rocket.png' border='0' class='center'> "._("Execute")."|execute_process_all\n";
379 }
381 $divlist->SetDropDownHeaderMenu($s);
383 if($this->sort_dir == "up"){
384 $sort_img = "<img src='images/lists/sort-up.png' alt='/\' border=0>";
385 }else{
386 $sort_img = "<img src='images/lists/sort-down.png' alt='\/' border=0>";
387 }
389 if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
390 if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
391 if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
392 if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
394 /* Create divlist */
395 $divlist->SetListHeader("<input type='image' src='images/lists/reload.png' title='"._("Reload")."'>");
397 $plug = $_GET['plug'];
398 $chk = "<input type='checkbox' id='select_all' name='select_all'
399 onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
401 /* set Page header */
402 $divlist->AddHeader(array("string"=> $chk, "attach"=>"style='width:20px;'"));
403 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=TargetName'>"._("Target").$sort_img_1."</a>"));
404 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=TaskID'>"._("Task").$sort_img_2."</a>",
405 "attach"=>"style='width:120px;'"));
406 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
407 "attach"=>"style='width:140px;'"));
408 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Action'>"._("Status").$sort_img_4."</a>",
409 "attach"=>"style='width:80px;'"));
410 $divlist->AddHeader(array("string"=>_("Action"),
411 "attach"=>"style='border-right:0px;width:140px;'"));
414 /* Reload the list of entries */
415 $this->reload();
417 foreach($this->entries as $key => $task){
419 $prio_actions="";
420 $action = "";
423 /* If WAITING add priority action
424 */
425 if(in_array($task['STATUS'],array("waiting"))){
426 $prio_actions.= "<input class='center' type='image' src='plugins/goto/images/prio_increase.png'
427 title='"._("Move up")."' name='prio_up_".$key."'> ";
428 $prio_actions.= "<input class='center' type='image' src='plugins/goto/images/prio_decrease.png'
429 title='"._("Move down")."' name='prio_down_".$key."'> ";
430 }
432 /* If WAITING add pause action
433 */
434 if(in_array($task['STATUS'],array("waiting"))){
435 $prio_actions.= "<input class='center' type='image' src='images/status_pause.png'
436 title='"._("Pause job")."' name='pause_".$key."'> ";
437 }
439 /* If PAUSED add resume action
440 */
441 if(in_array($task['STATUS'],array("paused"))){
442 $prio_actions.= "<input class='center' type='image' src='images/status_start.png'
443 title='"._("Resume job")."' name='resume_".$key."'> ";
444 }
446 /* If PAUSED or WAITING add execution action
447 */
448 if(in_array($task['STATUS'],array("paused","waiting"))){
449 $prio_actions.= "<input class='center' type='image' src='images/rocket.png'
450 title='"._("Execute now")."' name='execute_process_".$key."'> ";
451 }
453 /* Add logview button, currently ever.
454 */
455 if(TRUE){
456 $action .= "<input type='image' src='plugins/goto/images/view_logs.png' name='log_view_".$key."'
457 class='center' title='"._("View logs")."' alt='"._("View logs")."'> ";
458 }
460 /* If PAUSED or WAITING add edit action
461 */
462 if(in_array($task['STATUS'],array("waiting"))){
463 $action.= "<input type='image' src='images/lists/edit.png' name='edit_task_".$key."'
464 class='center' title='"._("Edit")."' alt='"._("Edit")."'>";
465 }
467 /* If PROCESSING add abort action
468 */
469 if(in_array($task['STATUS'],array("processing")) && preg_match("/install/",$task['HEADERTAG'])){
470 $action.= "<img src='images/empty.png' alt=''>";
471 $action.= "<input class='center' type='image' src='images/small_error.png'
472 title='"._("Abort job")."' name='abort_process_".$key."'>";
473 }
475 /* If WAITING or ERROR add remove action
476 */
477 if( $this->acl_is_removeable() && in_array($task['STATUS'],array("waiting","error","processed"))){
478 $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."'
479 class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
480 }
481 if(in_array($task['STATUS'],array("processing")) && !preg_match("/install/",$task['HEADERTAG'])){
482 $action.= "<input type='image' src='images/lists/trash.png' name='remove_task_".$key."'
483 class='center' title='"._("Remove")."' alt='"._("Remove")."'>";
484 }
486 /* Create entry display name and tooltip */
487 $color = "";
488 $display = $task['MACADDRESS'];
489 $tooltip = "";
490 if(isset($task['PLAINNAME']) && !preg_match("/none/i",$task['PLAINNAME'])){
491 $display = $task['PLAINNAME'];
492 $tooltip = " title='".$task['MACADDRESS']."' ";
493 }
494 $display2= $task['HEADERTAG'];
496 /* Check if this event exists as Daemon class
497 * In this case, display a more accurate entry.
498 */
499 if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
500 $evt_name = $this->events['QUEUED'][$task['HEADERTAG']];
501 $event_type = $this->events['BY_CLASS'][$evt_name];
502 $display2 = $event_type['s_Menu_Name'];
504 if(strlen($display2) > 20){
505 $display2 = substr($display2,0,18)."...";
506 }
508 if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
509 $display2 = $event_type['ListImage']." ".$display2;
510 }
511 }
513 $status = $task['STATUS'];
515 if($status == "waiting"){
516 $status = "<img class='center' src='plugins/goto/images/clock.png' alt=''> "._("Waiting");
517 }
518 if($status == "error"){
519 $status = "<img class='center' src='images/false.png' alt=''> "._("Error");
520 }
521 if($status == "processed"){
522 $status = "<img class='center' src='images/true.png' alt=''> "._("Processed");
523 }
525 /* Special handling for all entries that have
526 STATUS == "processing" && PROGRESS == NUMERIC
527 */
528 if($status == "processing" && isset($task['PROGRESS'])){
529 $percent = $task['PROGRESS'];
531 /* Show activation? */
532 if ($percent == "goto-activation"){
533 $status = "<img class='center' src='images/lists/off.png' alt=''> "._("Locked");
535 /* Show hardware detect? */
536 } elseif ($percent == "goto-hardware-detection") {
537 $status = "<img class='center' src='plugins/goto/images/hardware.png' alt=''> "._("Detection");
539 /* Real percent */
540 } else {
541 if (preg_match('/install/', $task['HEADERTAG'])){
542 $status = "<img src='progress.php?x=80&y=13&p=".$task['PROGRESS']."' alt=''
543 id='progress_".preg_replace("/:/","_",$task['MACADDRESS'])."'>";
544 } else {
545 $status = preg_replace('/ /', ' ', _("in progress"));
546 }
547 }
548 }
550 /* Create each field */
551 $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$key."'>" ,
552 "attach" => "style='width:20px;".$color."'");
553 $field1 = array("string" => $display,
554 "attach" => $tooltip."style='".$color."'");
555 $field1a= array("string" => $display2,
556 "attach" => "style='".$color.";width:120px;'");
557 $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:140px;'");
558 $field3 = array("string" => $status,"attach" => "style='".$color.";width:80px;'");
559 $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:140px;border-right:0px;'");
560 $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
561 }
563 $smarty = get_smarty();
564 $smarty->assign("events",$this->events);
565 $smarty->assign("start",$this->start);
566 $smarty->assign("start_real", ($this->start + 1));
567 $smarty->assign("ranges", array("10" => "10",
568 "20" => "20",
569 "25" => "25",
570 "50" => "50",
571 "100"=> "100",
572 "200"=> "200",
573 "9999" => "*"));
575 $count = $this->o_queue->number_of_queued_entries($this->event_tags);
576 if(!$count) $count = $this->range;
577 $divlist->SetListFooter(range_selector($count, $this->start, $this->range,"range"));
578 $smarty->assign("range",$this->range);
579 $smarty->assign("div",$divlist->Draw());
580 return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
581 }
584 /*! \brief Move an entry up or down in the queue, by updating its execution timestamp
585 @param $id Integer The ID of the entry which should be updated.
586 @param $type String "up" / "down"
587 @return boolean TRUE in case of success else FALSE
588 */
589 public function update_priority($id,$type = "up")
590 {
591 if($type == "up"){
592 $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp DESC");
593 }else{
594 $tmp = $this->o_queue->get_queued_entries($this->event_tags,-1,-1,"timestamp ASC");
595 }
596 $last = array();
597 foreach($tmp as $entry){
598 if($entry['ID'] == $id){
599 if(count($last)){
600 $time = strtotime($last['TIMESTAMP']);
601 if($type == "up"){
602 $time ++;
603 }else{
604 $time --;
605 }
606 $time_str = date("YmdHis",$time);
607 return($this->o_queue->update_entries(array($id),array("timestamp" => $time_str)));
608 }else{
609 return(FALSE);
610 }
611 }
612 $last = $entry;
613 }
614 return(FALSE);
615 }
618 /*! \brief Resumes to status 'waiting'.
619 * @return Boolean TRUE in case of success, else FALSE.
620 */
621 private function resume_queue_entries($ids)
622 {
623 if(!count($ids)){
624 return;
625 }
627 /* Entries are resumed by setting the status to
628 * 'waiting'
629 */
630 $data = array("status" => "waiting");
632 /* Check if given ids are valid and check if the status
633 * allows resuming.
634 */
635 $update_ids = array();
636 foreach($this->o_queue->get_entries_by_id($ids) as $entry){
637 if(isset($entry['STATUS']) && preg_match("/paused/",$entry['STATUS'])){
638 $update_ids[] = $entry['ID'];
639 }
640 }
642 /* Tell the daemon that we have entries to update.
643 */
644 if(count($update_ids)){
645 if(!$this->o_queue->update_entries($update_ids,$data)){
646 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
647 return(FALSE);
648 }
649 }
650 return(TRUE);
651 }
654 /*! \brief Force queue job to be done as far as possible.
655 * @return Boolean TRUE in case of success, else FALSE.
656 */
657 private function execute_queue_entries($ids)
658 {
659 if(!count($ids)){
660 return;
661 }
663 /* Execution is forced by updating the status to
664 * waiting and setting the timestamp to current time.
665 */
666 $data = array( "timestamp" => date("YmdHis",time()),
667 "status" => "waiting");
669 /* Only allow execution of paused or waiting entries
670 */
671 $update_ids = array();
672 foreach($this->o_queue->get_entries_by_id($ids) as $entry){
673 if(in_array($entry['STATUS'],array("paused","waiting"))){
674 $update_ids[] = $entry['ID'];
675 }
676 }
678 /* Tell the daemon that we want to update some entries
679 */
680 if(count($update_ids)){
681 if(!$this->o_queue->update_entries($update_ids,$data)){
682 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entries.")) , ERROR_DIALOG);
683 return(FALSE);
684 }
685 }
686 return(TRUE);
687 }
690 /*! \brief Force queue job to be done as far as possible.
691 * @return Boolean TRUE in case of success, else FALSE.
692 */
693 private function abort_queue_entries($ids)
694 {
695 if(!count($ids)){
696 return;
697 }
699 /* Entries are paused by setting the status to
700 * something different from 'waiting'.
701 * We simply use 'paused'.
702 */
703 $data = array("status" => "paused");
705 /* Detect if the ids we got are valid and
706 * check if the status allows pausing.
707 */
708 $update_ids = array();
709 foreach($this->o_queue->get_entries_by_id($ids) as $entry){
710 if(isset($entry['STATUS']) && preg_match("/processing/",$entry['STATUS'])){
711 if(isset($entry['MACADDRESS'])){
712 $update_ids[] = $entry['MACADDRESS'];
713 }else{
714 trigger_error("No mac address found in event.");
715 }
716 }
717 }
719 if(class_available("DaemonEvent_faireboot")){
720 $tmp = new DaemonEvent_faireboot($this->config);
721 $tmp->add_targets($update_ids);
722 $tmp->set_type(TRIGGERED_EVENT);
723 $this->recently_removed = $update_ids;
725 if(!$this->o_queue->append($tmp)){
726 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
727 return(FALSE);
728 }
729 }else{
730 msg_dialog::display(_("Error"),
731 sprintf(_("The job could not be aborted, the required class '%s' was not found."),
732 "DaemonEvent_faireboot") , ERROR_DIALOG);
733 }
734 }
737 /*! \brief Pauses the specified queue entry from execution.
738 * @return Boolean TRUE in case of success, else FALSE.
739 */
740 private function pause_queue_entries($ids)
741 {
742 if(!count($ids)){
743 return;
744 }
746 /* Entries are paused by setting the status to
747 * something different from 'waiting'.
748 * We simply use 'paused'.
749 */
750 $data = array("status" => "paused");
752 /* Detect if the ids we got are valid and
753 * check if the status allows pausing.
754 */
755 $update_ids = array();
756 foreach($this->o_queue->get_entries_by_id($ids) as $entry){
757 if(isset($entry['STATUS']) && preg_match("/waiting/",$entry['STATUS'])){
758 $update_ids[] = $entry['ID'];
759 }
760 }
762 /* Tell the daemon that we want to update some entries
763 */
764 if(count($update_ids)){
765 if(!$this->o_queue->update_entries($update_ids,$data)){
766 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
767 return(FALSE);
768 }
769 }
770 return(TRUE);
771 }
774 /*! \brief Request list of queued jobs.
775 * @return Returns an array of all queued jobs.
776 */
777 function reload()
778 {
780 /* Sort map html-post-name => daemon-col-name
781 */
782 $map = array(
783 "QueuePosition" => "id",
784 "Action" => "status",
785 "TaskID" => "headertag",
786 "TargetName" => "macaddress",
787 "Schedule" => "timestamp");
789 /* Create sort header
790 */
791 if(!isset($map[$this->sort_by])){
792 $sort = "id DESC";
793 }else{
794 $sort = $map[$this->sort_by];
795 if($this->sort_dir == "up"){
796 $sort.= " ASC";
797 }else{
798 $sort.= " DESC";
799 }
800 }
802 /* Get entries. */
803 $start = $this->start;
804 $stop = $this->range;
805 $entries = $this->o_queue->get_queued_entries($this->event_tags,$start,$stop,$sort);
806 if ($this->o_queue->is_error()){
807 msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
808 }
810 /* Assign entries by id.
811 */
812 $this->entries = array();
814 foreach($entries as $entry){
816 /* Skip entries which will be removed within the next seconds */
817 if(isset($entry['MACADDRESS']) && in_array($entry['MACADDRESS'],$this->recently_removed)){
818 continue;
819 }
820 $this->entries[$entry['ID']]= $entry;
821 }
822 $this->recently_removed = array();
823 }
826 /*! \brief Handle post jobs, like sorting.
827 */
828 function save_object()
829 {
830 /* Check for sorting changes
831 */
832 $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
833 if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
834 $sort = $_GET['sort'];
835 if($this->sort_by == $sort){
836 if($this->sort_dir == "up"){
837 $this->sort_dir = "down";
838 }else{
839 $this->sort_dir = "up";
840 }
841 }
842 $this->sort_by = $sort;
843 }
845 /* Range selection used? */
846 if(isset($_POST['range']) && is_numeric($_POST['range'])){
847 $this->range = $_POST['range'];
848 }
850 /* Save filter settings */
851 $gotomasses_filter = session::get("gotomasses_filter");
852 foreach(array("range","sort_by","sort_dir") as $attr){
853 $gotomasses_filter[$attr] = $this->$attr;
854 }
855 session::set("gotomasses_filter",$gotomasses_filter);
857 /* Page changed. */
858 if(isset($_GET['start'])){
859 $start = $_GET['start'];
860 if(is_numeric($start) || $start == 0){
861 $this->start = $start;
862 }
863 }
865 /* Check start stop and reset if necessary */
866 $count = $this->o_queue->number_of_queued_entries($this->event_tags);
867 if($this->start >= $count){
868 $this->start = $count -1;
869 }
870 if($this->start < 0){
871 $this->start = 0;
872 }
873 }
876 function save()
877 {
878 // We do not save anything here.
879 }
882 /*! \brief Return a list of all selected items.
883 @return Array Returns an array containing all selected item ids.
884 */
885 function list_get_selected_items()
886 {
887 $ids = array();
888 foreach($_POST as $name => $value){
889 if(preg_match("/^item_selected_[0-9]*$/",$name)){
890 $id = preg_replace("/^item_selected_/","",$name);
891 $ids[$id] = $id;
892 }
893 }
894 return($ids);
895 }
898 static function plInfo()
899 {
900 return (array(
901 "plShortName" => _("System mass deployment"),
902 "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
903 "plSelfModify" => FALSE,
904 "plDepends" => array(),
905 "plPriority" => 0,
906 "plSection" => array("addon"),
907 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
908 "plProvidedAcls" => array("Comment" => _("Description"))
909 ));
910 }
911 }
912 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
913 ?>