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 = "Deployment status";
27 var $plDescription = "Monitor and schedule system deployment and update processes";
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="",
273 $altTabType="",$altAclCategory="", $aclPlugin="")
274 {
275 if($this->acl_is_removeable("")){
276 timezone::get_default_timezone();
277 foreach($this->ids_to_remove as $id){
278 $entry = $this->o_queue->get_entries_by_id(array($id));
279 if(isset($entry['ANSWER1'])){
280 $entry = $entry['ANSWER1'];
281 if( $entry['STATUS'] == "waiting" &&
282 $entry['HEADERTAG'] == "trigger_action_reinstall"){
283 $evt = new DaemonEvent_reinstall($this->config,$entry);
284 if($evt->get_timestamp(FALSE) < time()){
285 $r_evt = new DaemonEvent_localboot($this->config);
286 $r_evt->add_targets(array($entry['MACADDRESS']));
287 $r_evt->set_type(TRIGGERED_EVENT);
288 $this->o_queue->append($r_evt);
289 }
290 }
291 }
292 }
293 $this->o_queue->remove_entries($this->ids_to_remove);
294 $this->save();
295 }
296 }
299 /*! \brief Force queue job to be aborted.
300 */
301 function abortEvent($action="",$target=array(),$all=array())
302 {
303 /* Entries are paused by setting the status to
304 * something different from 'waiting'.
305 * We simply use 'paused'.
306 */
307 $data = array("status" => "paused");
309 /* Detect if the ids we got are valid and
310 * check if the status allows pausing.
311 */
312 $update_ids = array();
313 $headpage = $this->getHeadpage();
314 foreach($target as $id){
315 $tmp = $headpage->getEntry($id);
316 $update_ids[] = $tmp['MACADDRESS'][0];
317 }
319 if(class_available("DaemonEvent_faireboot")){
320 $tmp = new DaemonEvent_faireboot($this->config);
321 $tmp->add_targets($update_ids);
322 $tmp->set_type(TRIGGERED_EVENT);
323 $this->recently_removed = $update_ids;
324 if(!$this->o_queue->append($tmp)){
325 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
326 return(FALSE);
327 }
328 }else{
329 msg_dialog::display(_("Error"),
330 sprintf(_("Required class '%s' cannot be found: job not aborted!"),
331 "DaemonEvent_faireboot") , ERROR_DIALOG);
332 }
333 }
336 function prioDown($action="",$target=array(),$all=array())
337 {
338 if(count($target) == 1){
339 $this->update_priority($target[0], 'down');
340 }
341 }
343 function prioUp($action="",$target=array(),$all=array())
344 {
345 if(count($target) == 1){
346 $this->update_priority($target[0], 'up');
347 }
348 }
350 function prioPause($action="",$target=array(),$all=array())
351 {
352 $this->pause_queue_entries($target);
353 }
355 function prioResume($action="",$target=array(),$all=array())
356 {
357 $this->resume_queue_entries($target);
358 }
360 function processNow($action="",$target=array(),$all=array())
361 {
362 $this->execute_queue_entries($target);
363 }
365 function viewLogs($action="",$target=array(),$all=array())
366 {
367 if(count($target) == 1){
368 $id = $target[0];
369 $type = FALSE;
370 $headpage = $this->getHeadpage();
371 $tmp = $headpage->getEntry($id);
372 $entry = $tmp['EVENT'];
373 $this->dialogObject = new gotoLogView($this->config,"",$entry,$this);
374 }
375 }
377 function saveEventDialog()
378 {
379 if(is_object($this->dialogObject)){
380 $this->dialogObject->save_object();
381 if(!$this->o_queue->append($this->dialogObject)){
382 msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->o_queue->get_error()),ERROR_DIALOG);
383 }else{
384 $this->current = -1;
385 }
386 }
387 $this->closeDialogs();
388 }
393 /*! \brief Move an entry up or down in the queue, by updating its execution timestamp
394 @param $id Integer The ID of the entry which should be updated.
395 @param $type String "up" / "down"
396 @return boolean TRUE in case of success else FALSE
397 */
398 public function update_priority($id,$type = "up")
399 {
400 $headpage = $this->getHeadpage();
401 $entries = $headpage->getEntries();
402 $entry = $headpage->getEntry($id);
404 $map = array();
405 $last = 0;
406 $next = 0;
407 foreach($entries as $pa){
408 $map[$pa['TIMESTAMP'][0]] = $pa['TIMESTAMP'][0];
409 }
410 krsort($map);
411 $found = 0;
412 $cur = 0;
413 foreach($map as $ts){
414 if($found){
415 $next = $ts;
416 break;
417 }
418 if($ts == $entry['TIMESTAMP'][0]){
419 $found = TRUE;
420 $cur = $ts;
421 }else{
422 $last = $ts;
423 }
424 }
426 if($type == "up" && $next != 0){
427 return($this->o_queue->update_entries(array($id),array("timestamp" => $next)));
428 }elseif($type == "down" && $last != 0){
429 return($this->o_queue->update_entries(array($id),array("timestamp" => $last)));
430 }
431 }
434 function detectPostActions()
435 {
436 $action = management::detectPostActions();
437 if(isset($_POST['save_event_dialog'])) $action['action'] = "saveEventDialog";
438 if(isset($_POST['abort_event_dialog'])) $action['action'] = "cancel";
439 if(isset($_POST['delete_multiple_confirm'])) $action['action'] = "removeConfirmed";
440 if(isset($_POST['delete_cancel'])) $action['action'] = "cancel";
441 if(isset($_POST['import_abort'])) $action['action'] = "cancel";
442 return($action);
443 }
446 function closeDialogs()
447 {
448 $this->current = FALSE;
449 management::closeDialogs();
450 }
452 /*! \brief Resumes to status 'waiting'.
453 * @return Boolean TRUE in case of success, else FALSE.
454 */
455 private function resume_queue_entries($ids)
456 {
457 /* Entries are resumed by setting the status to
458 * 'waiting'
459 */
460 $data = array("status" => "waiting");
462 /* Check if given ids are valid and check if the status
463 * allows resuming.
464 */
465 $update_ids = array();
466 $headpage = $this->getHeadpage();
467 foreach($ids as $id){
468 $tmp = $headpage->getEntry($id);
469 $entry = $tmp['EVENT'];
470 if(isset($entry['STATUS']) && preg_match("/paused/",$entry['STATUS'])){
471 $update_ids[] = $entry['ID'];
472 }
473 }
475 /* Tell the daemon that we have entries to update.
476 */
477 if(count($update_ids)){
478 if(!$this->o_queue->update_entries($update_ids,$data)){
479 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
480 return(FALSE);
481 }
482 }
483 return(TRUE);
484 }
487 /*! \brief Force queue job to be done as far as possible.
488 * @return Boolean TRUE in case of success, else FALSE.
489 */
490 private function execute_queue_entries($ids)
491 {
492 /* Execution is forced by updating the status to
493 * waiting and setting the timestamp to current time.
494 */
495 $data = array( "timestamp" => date("YmdHis",time()),
496 "status" => "waiting");
498 /* Only allow execution of paused or waiting entries
499 */
500 $update_ids = array();
501 $headpage = $this->getHeadpage();
502 foreach($ids as $id){
503 $tmp = $headpage->getEntry($id);
504 $entry = $tmp['EVENT'];
505 if(in_array($entry['STATUS'],array("paused","waiting"))){
506 $update_ids[] = $entry['ID'];
507 }
508 }
510 /* Tell the daemon that we want to update some entries
511 */
512 if(count($update_ids)){
513 if(!$this->o_queue->update_entries($update_ids,$data)){
514 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entries.")) , ERROR_DIALOG);
515 return(FALSE);
516 }
517 }
518 return(TRUE);
519 }
522 /*! \brief Pauses the specified queue entry from execution.
523 * @return Boolean TRUE in case of success, else FALSE.
524 */
525 private function pause_queue_entries($ids)
526 {
527 /* Entries are paused by setting the status to
528 * something different from 'waiting'.
529 * We simply use 'paused'.
530 */
531 $data = array("status" => "paused");
533 /* Detect if the ids we got are valid and
534 * check if the status allows pausing.
535 */
536 $update_ids = array();
537 $headpage = $this->getHeadpage();
538 foreach($ids as $id){
539 $tmp = $headpage->getEntry($id);
540 $entry = $tmp['EVENT'];
541 if(isset($entry['STATUS']) && preg_match("/waiting/",$entry['STATUS'])){
542 $update_ids[] = $entry['ID'];
543 }
544 }
546 /* Tell the daemon that we want to update some entries
547 */
548 if(count($update_ids)){
549 if(!$this->o_queue->update_entries($update_ids,$data)){
550 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
551 return(FALSE);
552 }
553 }
554 return(TRUE);
555 }
557 function save_object(){}
559 function save(){}
561 static function plInfo()
562 {
563 return (array(
564 "plShortName" => _("System deployment"),
565 "plDescription" => _("Provide a mechanism to automatically activate systems"),
566 "plSelfModify" => FALSE,
567 "plDepends" => array(),
568 "plPriority" => 0,
569 "plSection" => array("addon"),
570 "plCategory" => array("gotomasses" => array("description" => _("System deployment"))),
571 "plProvidedAcls" => array("Comment" => _("Description"))
572 ));
573 }
576 function set_acl_base($base)
577 {
578 $this->acl_base= $base;
579 }
582 function set_acl_category($category)
583 {
584 $this->acl_category= "$category/";
585 }
588 function acl_is_writeable($attribute,$skip_write = FALSE)
589 {
590 if($this->read_only) return(FALSE);
591 $ui= get_userinfo();
592 return preg_match('/w/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute, $skip_write));
593 }
596 function acl_is_readable($attribute)
597 {
598 $ui= get_userinfo();
599 return preg_match('/r/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute));
600 }
603 function acl_is_createable($base ="")
604 {
605 if($this->read_only) return(FALSE);
606 $ui= get_userinfo();
607 if($base == "") $base = $this->acl_base;
608 return preg_match('/c/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
609 }
612 function acl_is_removeable($base ="")
613 {
614 if($this->read_only) return(FALSE);
615 $ui= get_userinfo();
616 if($base == "") $base = $this->acl_base;
617 return preg_match('/d/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
618 }
620 function acl_is_moveable($base = "")
621 {
622 if($this->read_only) return(FALSE);
623 $ui= get_userinfo();
624 if($base == "") $base = $this->acl_base;
625 return preg_match('/m/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
626 }
629 function getacl($attribute,$skip_write= FALSE)
630 {
631 $ui= get_userinfo();
632 $skip_write |= $this->read_only;
633 return $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute,$skip_write);
634 }
636 }
637 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
638 ?>