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 management
24 {
26 var $plHeadline = "System deployment status";
27 var $plDescription = "System deployment status";
28 var $plIcon = "plugins/goto/images/goto.png";
30 var $current = FALSE;
31 var $dialog = FALSE;
32 var $ids_to_remove = array();
34 var $events = array();
35 var $event_tags = array();
36 var $recently_removed = array();
38 var $read_only = FALSE;
39 var $acl_base;
40 var $acl_category;
42 function __construct(&$config, $ui)
43 {
44 /* Include config object */
45 $this->config= &$config;
46 $this->o_queue = new gosaSupportDaemon(TRUE,5);
47 $this->events = DaemonEvent::get_event_types( SYSTEM_EVENT);
48 $this->acl_base = $config->current['BASE'];
49 $this->acl_category = "gotomasses/";
51 /* Get tags that will be used in queue searches */
52 $this->event_tags = array("none");
53 foreach($this->events['SCHEDULED'] as $evt){
54 $this->event_tags[] = $evt['s_Queued_Action'];
55 }
57 // Build filter
58 if (session::global_is_set(get_class($this)."_filter")){
59 $filter= session::global_get(get_class($this)."_filter");
60 } else {
61 $filter = new filter(get_template_path("deploy-filter.xml", true));
62 $filter->setObjectStorage($this->storagePoints);
63 }
64 $this->setFilter($filter);
66 // Build headpage
67 $headpage = new listing(get_template_path("deploy-list.xml", true));
68 $headpage->registerElementFilter("hostName", "gotomasses::filterHostName");
69 $headpage->registerElementFilter("filterTask","gotomasses::filterTask");
70 $headpage->registerElementFilter("filterPeriod","gotomasses::filterPeriod");
71 $headpage->registerElementFilter("filterSchedule","gotomasses::filterSchedule");
72 $headpage->registerElementFilter("filterStatus","gotomasses::filterStatus");
73 $headpage->setFilter($filter);
75 parent::__construct($config, $ui, "Events", $headpage);
77 $this->registerAction('prioDown', "prioDown");
78 $this->registerAction('prioUp', "prioUp");
79 $this->registerAction('prioPause', "prioPause");
80 $this->registerAction('prioResume', "prioResume");
81 $this->registerAction('processNow', "processNow");
82 $this->registerAction('viewLogs', "viewLogs");
83 $this->registerAction('abort', "abortEvent");
84 $this->registerAction('saveEventDialog', "saveEventDialog");
85 $this->registerAction('halt', 'newEntry');
86 $this->registerAction('reboot', 'newEntry');
87 $this->registerAction('wakeup', 'newEntry');
88 $this->registerAction('update', 'newEntry');
89 $this->registerAction('lock', 'newEntry');
90 $this->registerAction('activate', 'newEntry');
91 $this->registerAction('reinstall', 'newEntry');
92 $this->registerAction('import', 'importEvents');
93 }
95 function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
96 {
97 if($this->acl_is_writeable("")){
98 $type = "DaemonEvent_".$action;
99 if(isset($this->events['BY_CLASS'][$type])){
100 $e_data = $this->events['BY_CLASS'][$type];
101 $this->dialogObject = new $e_data['CLASS_NAME']($this->config);
102 }
103 }
104 }
106 function importEvents()
107 {
108 $this->dialogObject = new goto_import_file($this->config,$this);
109 }
111 static function filterHostName($mac, $name ="")
112 {
113 if(isset($name[0]) && $name[0] != "none"){
114 return($name[0]);
115 }
116 return($mac[0]);
117 }
119 static function filterTask($tag)
120 {
121 $tag = $tag[0];
122 $str = $tag;
124 /* Check if this event exists as Daemon class
125 * In this case, display a more accurate entry.
126 */
127 $events = DaemonEvent::get_event_types( SYSTEM_EVENT);
128 if(isset($events['QUEUED'][$tag])){
129 $evt_name = $events['QUEUED'][$tag];
130 $event_type = $events['BY_CLASS'][$evt_name];
131 $str = $event_type['s_Menu_Name'];
133 if(strlen($str) > 20){
134 $str = substr($str,0,18)."...";
135 }
137 if(isset($event_type['ListImage']) && !empty($event_type['ListImage'])){
138 $str = $event_type['ListImage']." ".$str;
139 }
140 }
141 return($str);
142 }
144 static function filterPeriod($periodic=array())
145 {
146 $period = " -";
147 if(isset($periodic[0]) && !preg_match("/none/i",$periodic[0])){
148 $tmp = explode("_", $periodic[0]);
149 if(count($tmp) == 2){
150 $period= $tmp[0]." "._($tmp[1]);
151 }
152 }
153 return($period);
154 }
156 static function filterSchedule($stamp)
157 {
158 if ($stamp['0'] == "19700101000000"){
159 return(_("immediately"));
160 } else {
161 return(date("d.m.Y H:i:s",strtotime($stamp[0])));
162 }
163 }
166 static function filterStatus($status, $mac,$headertag, $progress)
167 {
169 $mac = $mac[0];
170 $status = $status[0];
171 $progress = $progress[0];
172 $headertag = $headertag[0];
174 if($status == "waiting"){
175 $status = "<img class='center' src='plugins/goto/images/clock.png' alt=''> "._("Waiting");
176 }
177 if($status == "error"){
178 $status = "<img class='center' src='images/false.png' alt=''> "._("Error");
179 }
180 if($status == "processed"){
181 $status = "<img class='center' src='images/true.png' alt=''> "._("Processed");
182 }
184 /* Special handling for all entries that have
185 STATUS == "processing" && PROGRESS == NUMERIC
186 */
187 if($status == "processing" && $progress){
188 $percent = $progress;
190 /* Show activation? */
191 if ($percent == "goto-activation"){
192 $status = "<img class='center' src='images/lists/off.png' alt=''> "._("Locked");
194 /* Show hardware detect? */
195 } elseif ($percent == "goto-hardware-detection") {
196 $status = "<img class='center' src='plugins/goto/images/hardware.png' alt=''> "._("Detection");
198 /* Real percent */
199 } else {
200 if (preg_match('/install/', $headertag)){
201 $status= progressbar($progress, 80, 13, true, false, "progress_".preg_replace("/:/","_",$mac));
202 } else {
203 $status = preg_replace('/ /', ' ', _("in progress"));
204 }
205 }
206 }
207 return($status);
208 }
210 function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
211 {
212 if(count($target) == 1){
213 $headpage = $this->getHeadpage();
214 $entry = $headpage->getEntry($target[0]);
215 $event = $entry['EVENT'];
216 if($event['STATUS'] == "waiting" && isset($this->events['QUEUED'][$event['HEADERTAG']])){
217 $evt_name = $this->events['QUEUED'][$event['HEADERTAG']];
218 $type = $this->events['BY_CLASS'][$evt_name];
219 $this->dialogObject = new $type['CLASS_NAME']($this->config,$event);
220 }
221 }
222 }
224 function removeEntryRequested($action="",$target=array(),$all=array())
225 {
226 if(!$this->acl_is_removeable()){
227 msg_dialog::display(_("Permission"), msgPool::permDelete(), ERROR_DIALOG);
228 }else{
230 $deleteable_jobs = array();
231 $not_deleteable_jobs = array();
232 $headpage = $this->getHeadpage();
234 foreach($target as $dn){
236 $tmp = $headpage->getEntry($dn);
237 $task = $tmp['EVENT'];
239 /* Create a printable job name/description */
240 if(isset($this->events['QUEUED'][$task['HEADERTAG']])){
241 $evt_name = $this->events['QUEUED'][$task['HEADERTAG']];
242 $evt = $this->events['BY_CLASS'][$evt_name];
243 $j_name = $task['ID']." - ".$evt['s_Menu_Name']." ".$task['MACADDRESS'];
244 }else{
245 $j_name = $task['ID']." - ".$task['HEADERTAG']." ".$task['MACADDRESS'];
246 }
248 /* Only remove WAITING or ERROR entries */
249 if(in_array($task['STATUS'],array("waiting","error","processed")) ||
250 ($task['STATUS'] == "processing" && !preg_match("/install/",$task['HEADERTAG'])) ){
251 $this->ids_to_remove[] = $task['ID'];
252 $deleteable_jobs[] = $j_name;
253 }else{
254 $not_deleteable_jobs[] = $j_name;
255 }
256 }
257 if(count($not_deleteable_jobs)){
258 msg_dialog::display(_("Remove"),
259 sprintf(_("The following jobs couldn't be deleted, they have to be aborted: %s"),
260 "<br>".msgPool::buildList($not_deleteable_jobs)),INFO_DIALOG);
261 }
263 if(count($this->ids_to_remove)){
264 $smarty = get_smarty();
265 $smarty->assign("multiple", TRUE);
266 $smarty->assign("info",msgPool::deleteInfo($deleteable_jobs));
267 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
268 }
269 }
270 }
272 function removeEntryConfirmed($action="",$target=array(),$all=array(),$altTabClass="",$altTabType="",$altAclCategory="")
273 {
274 if($this->acl_is_removeable("")){
275 timezone::get_default_timezone();
276 foreach($this->ids_to_remove as $id){
277 $entry = $this->o_queue->get_entries_by_id(array($id));
278 if(isset($entry['ANSWER1'])){
279 $entry = $entry['ANSWER1'];
280 if( $entry['STATUS'] == "waiting" &&
281 $entry['HEADERTAG'] == "trigger_action_reinstall"){
282 $evt = new DaemonEvent_reinstall($this->config,$entry);
283 if($evt->get_timestamp(FALSE) < time()){
284 $r_evt = new DaemonEvent_localboot($this->config);
285 $r_evt->add_targets(array($entry['MACADDRESS']));
286 $r_evt->set_type(TRIGGERED_EVENT);
287 $this->o_queue->append($r_evt);
288 }
289 }
290 }
291 }
292 $this->o_queue->remove_entries($this->ids_to_remove);
293 $this->save();
294 }
295 }
298 /*! \brief Force queue job to be aborted.
299 */
300 function abortEvent($action="",$target=array(),$all=array())
301 {
302 /* Entries are paused by setting the status to
303 * something different from 'waiting'.
304 * We simply use 'paused'.
305 */
306 $data = array("status" => "paused");
308 /* Detect if the ids we got are valid and
309 * check if the status allows pausing.
310 */
311 $update_ids = array();
312 $headpage = $this->getHeadpage();
313 foreach($target as $id){
314 $tmp = $headpage->getEntry($id);
315 $update_ids[] = $tmp['MACADDRESS'][0];
316 }
318 if(class_available("DaemonEvent_faireboot")){
319 $tmp = new DaemonEvent_faireboot($this->config);
320 $tmp->add_targets($update_ids);
321 $tmp->set_type(TRIGGERED_EVENT);
322 $this->recently_removed = $update_ids;
323 if(!$this->o_queue->append($tmp)){
324 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
325 return(FALSE);
326 }
327 }else{
328 msg_dialog::display(_("Error"),
329 sprintf(_("Required class '%s' cannot be found: job not aborted!"),
330 "DaemonEvent_faireboot") , ERROR_DIALOG);
331 }
332 }
335 function prioDown($action="",$target=array(),$all=array())
336 {
337 if(count($target) == 1){
338 $this->update_priority($target[0], 'down');
339 }
340 }
342 function prioUp($action="",$target=array(),$all=array())
343 {
344 if(count($target) == 1){
345 $this->update_priority($target[0], 'up');
346 }
347 }
349 function prioPause($action="",$target=array(),$all=array())
350 {
351 $this->pause_queue_entries($target);
352 }
354 function prioResume($action="",$target=array(),$all=array())
355 {
356 $this->resume_queue_entries($target);
357 }
359 function processNow($action="",$target=array(),$all=array())
360 {
361 $this->execute_queue_entries($target);
362 }
364 function viewLogs($action="",$target=array(),$all=array())
365 {
366 if(count($target) == 1){
367 $id = $target[0];
368 $type = FALSE;
369 $headpage = $this->getHeadpage();
370 $tmp = $headpage->getEntry($id);
371 $entry = $tmp['EVENT'];
372 $this->dialogObject = new gotoLogView($this->config,"",$entry,$this);
373 }
374 }
376 function saveEventDialog()
377 {
378 if(is_object($this->dialogObject)){
379 $this->dialogObject->save_object();
380 if(!$this->o_queue->append($this->dialogObject)){
381 msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->o_queue->get_error()),ERROR_DIALOG);
382 }else{
383 $this->current = -1;
384 }
385 }
386 $this->closeDialogs();
387 }
392 /*! \brief Move an entry up or down in the queue, by updating its execution timestamp
393 @param $id Integer The ID of the entry which should be updated.
394 @param $type String "up" / "down"
395 @return boolean TRUE in case of success else FALSE
396 */
397 public function update_priority($id,$type = "up")
398 {
399 $headpage = $this->getHeadpage();
400 $entries = $headpage->getEntries();
401 $entry = $headpage->getEntry($id);
403 $map = array();
404 $last = 0;
405 $next = 0;
406 foreach($entries as $pa){
407 $map[$pa['TIMESTAMP'][0]] = $pa['TIMESTAMP'][0];
408 }
409 krsort($map);
410 $found = 0;
411 $cur = 0;
412 foreach($map as $ts){
413 if($found){
414 $next = $ts;
415 break;
416 }
417 if($ts == $entry['TIMESTAMP'][0]){
418 $found = TRUE;
419 $cur = $ts;
420 }else{
421 $last = $ts;
422 }
423 }
425 if($type == "up" && $next != 0){
426 return($this->o_queue->update_entries(array($id),array("timestamp" => $next)));
427 }elseif($type == "down" && $last != 0){
428 return($this->o_queue->update_entries(array($id),array("timestamp" => $last)));
429 }
430 }
433 function detectPostActions()
434 {
435 $action = management::detectPostActions();
436 if(isset($_POST['save_event_dialog'])) $action['action'] = "saveEventDialog";
437 if(isset($_POST['abort_event_dialog'])) $action['action'] = "cancel";
438 if(isset($_POST['delete_multiple_confirm'])) $action['action'] = "removeConfirmed";
439 if(isset($_POST['delete_cancel'])) $action['action'] = "cancel";
440 if(isset($_POST['import_abort'])) $action['action'] = "cancel";
441 return($action);
442 }
445 function closeDialogs()
446 {
447 $this->current = FALSE;
448 management::closeDialogs();
449 }
451 /*! \brief Resumes to status 'waiting'.
452 * @return Boolean TRUE in case of success, else FALSE.
453 */
454 private function resume_queue_entries($ids)
455 {
456 /* Entries are resumed by setting the status to
457 * 'waiting'
458 */
459 $data = array("status" => "waiting");
461 /* Check if given ids are valid and check if the status
462 * allows resuming.
463 */
464 $update_ids = array();
465 $headpage = $this->getHeadpage();
466 foreach($ids as $id){
467 $tmp = $headpage->getEntry($id);
468 $entry = $tmp['EVENT'];
469 if(isset($entry['STATUS']) && preg_match("/paused/",$entry['STATUS'])){
470 $update_ids[] = $entry['ID'];
471 }
472 }
474 /* Tell the daemon that we have entries to update.
475 */
476 if(count($update_ids)){
477 if(!$this->o_queue->update_entries($update_ids,$data)){
478 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
479 return(FALSE);
480 }
481 }
482 return(TRUE);
483 }
486 /*! \brief Force queue job to be done as far as possible.
487 * @return Boolean TRUE in case of success, else FALSE.
488 */
489 private function execute_queue_entries($ids)
490 {
491 /* Execution is forced by updating the status to
492 * waiting and setting the timestamp to current time.
493 */
494 $data = array( "timestamp" => date("YmdHis",time()),
495 "status" => "waiting");
497 /* Only allow execution of paused or waiting entries
498 */
499 $update_ids = array();
500 $headpage = $this->getHeadpage();
501 foreach($ids as $id){
502 $tmp = $headpage->getEntry($id);
503 $entry = $tmp['EVENT'];
504 if(in_array($entry['STATUS'],array("paused","waiting"))){
505 $update_ids[] = $entry['ID'];
506 }
507 }
509 /* Tell the daemon that we want to update some entries
510 */
511 if(count($update_ids)){
512 if(!$this->o_queue->update_entries($update_ids,$data)){
513 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entries.")) , ERROR_DIALOG);
514 return(FALSE);
515 }
516 }
517 return(TRUE);
518 }
521 /*! \brief Pauses the specified queue entry from execution.
522 * @return Boolean TRUE in case of success, else FALSE.
523 */
524 private function pause_queue_entries($ids)
525 {
526 /* Entries are paused by setting the status to
527 * something different from 'waiting'.
528 * We simply use 'paused'.
529 */
530 $data = array("status" => "paused");
532 /* Detect if the ids we got are valid and
533 * check if the status allows pausing.
534 */
535 $update_ids = array();
536 $headpage = $this->getHeadpage();
537 foreach($ids as $id){
538 $tmp = $headpage->getEntry($id);
539 $entry = $tmp['EVENT'];
540 if(isset($entry['STATUS']) && preg_match("/waiting/",$entry['STATUS'])){
541 $update_ids[] = $entry['ID'];
542 }
543 }
545 /* Tell the daemon that we want to update some entries
546 */
547 if(count($update_ids)){
548 if(!$this->o_queue->update_entries($update_ids,$data)){
549 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
550 return(FALSE);
551 }
552 }
553 return(TRUE);
554 }
556 function save_object(){}
558 function save(){}
560 static function plInfo()
561 {
562 return (array(
563 "plShortName" => _("System deployment"),
564 "plDescription" => _("Provide a mechanism to automatically activate systems"),
565 "plSelfModify" => FALSE,
566 "plDepends" => array(),
567 "plPriority" => 0,
568 "plSection" => array("addon"),
569 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System deployment"))),
570 "plProvidedAcls" => array("Comment" => _("Description"))
571 ));
572 }
575 function set_acl_base($base)
576 {
577 $this->acl_base= $base;
578 }
581 function set_acl_category($category)
582 {
583 $this->acl_category= "$category/";
584 }
587 function acl_is_writeable($attribute,$skip_write = FALSE)
588 {
589 if($this->read_only) return(FALSE);
590 $ui= get_userinfo();
591 return preg_match('/w/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute, $skip_write));
592 }
595 function acl_is_readable($attribute)
596 {
597 $ui= get_userinfo();
598 return preg_match('/r/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute));
599 }
602 function acl_is_createable($base ="")
603 {
604 if($this->read_only) return(FALSE);
605 $ui= get_userinfo();
606 if($base == "") $base = $this->acl_base;
607 return preg_match('/c/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
608 }
611 function acl_is_removeable($base ="")
612 {
613 if($this->read_only) return(FALSE);
614 $ui= get_userinfo();
615 if($base == "") $base = $this->acl_base;
616 return preg_match('/d/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
617 }
619 function acl_is_moveable($base = "")
620 {
621 if($this->read_only) return(FALSE);
622 $ui= get_userinfo();
623 if($base == "") $base = $this->acl_base;
624 return preg_match('/m/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
625 }
628 function getacl($attribute,$skip_write= FALSE)
629 {
630 $ui= get_userinfo();
631 $skip_write |= $this->read_only;
632 return $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute,$skip_write);
633 }
635 }
636 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
637 ?>