Code

Backport from trunk
[gosa.git] / gosa-plugins / goto / addons / goto / class_gotomasses.inc
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']."&nbsp;".$str;
139             }
140         }
141         return($str);
142     }
144     static function filterPeriod($periodic=array())
145     {
146         $period = "&nbsp;-";
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]."&nbsp;"._($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=''>&nbsp;"._("Waiting");
176         }
177         if($status == "error"){
178             $status = "<img class='center' src='images/false.png' alt=''>&nbsp;"._("Error");
179         }
180         if($status == "processed"){
181             $status = "<img class='center' src='images/true.png' alt=''>&nbsp;"._("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=''>&nbsp;"._("Locked");
194                 /* Show hardware detect? */
195             } elseif ($percent == "goto-hardware-detection") {
196                 $status = "<img class='center' src='plugins/goto/images/hardware.png' alt=''>&nbsp;"._("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('/ /', '&nbsp;', _("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']."&nbsp;".$task['MACADDRESS'];
244                 }else{
245                     $j_name = $task['ID']." - ".$task['HEADERTAG']."&nbsp;".$task['MACADDRESS'];
246                 }
248                 /* Only remove WAITING or ERROR entries */
249                 if(in_array_strict($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     }
273     function removeEntryConfirmed($action="",$target=array(),$all=array(),
274             $altTabClass="",$altTabType="",  $altAclCategory="",$altAclPlugin="")
275     {
276         if($this->acl_is_removeable("")){
277             timezone::get_default_timezone();
278             foreach($this->ids_to_remove as $id){
279                 $entry = $this->o_queue->get_entries_by_id(array($id));
280                 if(isset($entry['ANSWER1'])){
281                     $entry = $entry['ANSWER1'];
282                     if( $entry['STATUS'] == "waiting" && 
283                             $entry['HEADERTAG'] == "trigger_action_reinstall"){
284                         $evt = new DaemonEvent_reinstall($this->config,$entry);
285                         if($evt->get_timestamp(FALSE)  < time()){
286                             $r_evt = new DaemonEvent_localboot($this->config);
287                             $r_evt->add_targets(array($entry['MACADDRESS']));
288                             $r_evt->set_type(TRIGGERED_EVENT);
289                             $this->o_queue->append($r_evt);
290                         }
291                     }
292                 }
293             }
294             $this->o_queue->remove_entries($this->ids_to_remove);
295             $this->save();
296         }
297     }
300     /*! \brief  Force queue job to be aborted.
301      */
302     function abortEvent($action="",$target=array(),$all=array())
303     {
304         /* Entries are paused by setting the status to
305          *  something different from 'waiting'.
306          * We simply use 'paused'.
307          */
308         $data = array("status"    => "paused");
310         /* Detect if the ids we got are valid and
311          *  check if the status allows pausing.
312          */
313         $update_ids = array();
314         $headpage = $this->getHeadpage();
315         foreach($target as $id){
316             $tmp = $headpage->getEntry($id);
317             $update_ids[] = $tmp['MACADDRESS'][0];
318         }
320         if(class_available("DaemonEvent_faireboot")){
321             $tmp = new DaemonEvent_faireboot($this->config);
322             $tmp->add_targets($update_ids);
323             $tmp->set_type(TRIGGERED_EVENT);
324             $this->recently_removed = $update_ids;
325             if(!$this->o_queue->append($tmp)){
326                 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
327                 return(FALSE);
328             }
329         }else{
330             msg_dialog::display(_("Error"),
331                     sprintf(_("Required class '%s' cannot be found: job not aborted!"),
332                         "DaemonEvent_faireboot") , ERROR_DIALOG);
333         }
334     }
337     function prioDown($action="",$target=array(),$all=array())
338     {
339         if(count($target) == 1){
340             $this->update_priority($target[0], 'down');
341         }
342     }
344     function prioUp($action="",$target=array(),$all=array())
345     {
346         if(count($target) == 1){
347             $this->update_priority($target[0], 'up');
348         }
349     }
351     function prioPause($action="",$target=array(),$all=array())
352     {
353         $this->pause_queue_entries($target);
354     }
356     function prioResume($action="",$target=array(),$all=array())
357     {
358         $this->resume_queue_entries($target);
359     }
361     function processNow($action="",$target=array(),$all=array())
362     {
363         $this->execute_queue_entries($target);
364     }
366     function viewLogs($action="",$target=array(),$all=array())
367     {
368         if(count($target) == 1){
369             $id =  $target[0];
370             $type = FALSE;
371             $headpage = $this->getHeadpage();
372             $tmp = $headpage->getEntry($id);
373             $entry = $tmp['EVENT'];
374             $this->dialogObject = new gotoLogView($this->config,"",$entry,$this);
375         }
376     }
378     function saveEventDialog()
379     {
380         if(is_object($this->dialogObject)){
381             $this->dialogObject->save_object();
382             if(!$this->o_queue->append($this->dialogObject)){
383                 msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->o_queue->get_error()),ERROR_DIALOG);
384             }else{
385                 $this->current = -1;
386             } 
387         }
388         $this->closeDialogs();
389     }
394     /*! \brief  Move an entry up or down in the queue, by updating its execution timestamp                             
395       @param  $id     Integer  The ID of the entry which should be updated.                                            
396       @param  $type   String   "up" / "down"                                                                           
397       @return boolean TRUE in case of success else FALSE                                                               
398      */                                                                                                                
399     public function update_priority($id,$type = "up")                                                                  
400     {                                          
401         $headpage = $this->getHeadpage();
402         $entries = $headpage->getEntries();
403         $entry = $headpage->getEntry($id);
405         $map = array();
406         $last = 0;
407         $next = 0;
408         foreach($entries as $pa){
409             $map[$pa['TIMESTAMP'][0]] = $pa['TIMESTAMP'][0];
410         }
411         krsort($map);
412         $found = 0;
413         $cur = 0;
414         foreach($map as $ts){
415             if($found){
416                 $next = $ts;
417                 break;
418             }
419             if($ts == $entry['TIMESTAMP'][0]){
420                 $found = TRUE;  
421                 $cur = $ts;
422             }else{
423                 $last = $ts;
424             }
425         }
427         if($type == "up" && $next != 0){
428             return($this->o_queue->update_entries(array($id),array("timestamp" => $next)));
429         }elseif($type == "down" && $last != 0){
430             return($this->o_queue->update_entries(array($id),array("timestamp" => $last)));
431         }
432     }
435     function detectPostActions()
436     {
437         $action = management::detectPostActions();
438         if(isset($_POST['save_event_dialog'])) $action['action'] = "saveEventDialog";
439         if(isset($_POST['abort_event_dialog'])) $action['action'] = "cancel";
440         if(isset($_POST['delete_multiple_confirm'])) $action['action'] = "removeConfirmed";
441         if(isset($_POST['delete_cancel'])) $action['action'] = "cancel";
442         if(isset($_POST['import_abort'])) $action['action'] = "cancel";
443         return($action);
444     }
447     function closeDialogs()
448     {
449         $this->current = FALSE;
450         management::closeDialogs();
451     }
453     /*! \brief  Resumes to status 'waiting'.
454      *  @return Boolean TRUE in case of success, else FALSE. 
455      */
456     private function resume_queue_entries($ids)
457     {
458         /* Entries are resumed by setting the status to 
459          *  'waiting'
460          */
461         $data = array("status"    => "waiting");
463         /* Check if given ids are valid and check if the status
464          *  allows resuming.
465          */
466         $update_ids = array();
467         $headpage = $this->getHeadpage();
468         foreach($ids as $id){
469             $tmp = $headpage->getEntry($id);
470             $entry = $tmp['EVENT'];
471             if(isset($entry['STATUS']) && preg_match("/paused/",$entry['STATUS'])){
472                 $update_ids[] = $entry['ID'];
473             }
474         }
476         /* Tell the daemon that we have entries to update.
477          */
478         if(count($update_ids)){
479             if(!$this->o_queue->update_entries($update_ids,$data)){
480                 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
481                 return(FALSE);
482             }
483         }
484         return(TRUE);
485     }
488     /*! \brief  Force queue job to be done as far as possible.
489      *  @return Boolean TRUE in case of success, else FALSE.
490      */
491     private function execute_queue_entries($ids)
492     {
493         /* Execution is forced by updating the status to 
494          *  waiting and setting the timestamp to current time.
495          */
496         $data = array(  "timestamp" => date("YmdHis",time()), 
497                 "status"    => "waiting");
499         /* Only allow execution of paused or waiting entries 
500          */
501         $update_ids = array();
502         $headpage = $this->getHeadpage();
503         foreach($ids as $id){
504             $tmp = $headpage->getEntry($id);
505             $entry = $tmp['EVENT'];
506             if(in_array_strict($entry['STATUS'],array("paused","waiting"))){
507                 $update_ids[] = $entry['ID'];
508             }
509         }
511         /* Tell the daemon that we want to update some entries
512          */
513         if(count($update_ids)){
514             if(!$this->o_queue->update_entries($update_ids,$data)){
515                 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entries.")) , ERROR_DIALOG);
516                 return(FALSE);
517             }
518         }
519         return(TRUE);
520     }
523     /*! \brief Pauses the specified queue entry from execution.
524      *  @return Boolean TRUE in case of success, else FALSE. 
525      */
526     private function pause_queue_entries($ids)
527     {
528         /* Entries are paused by setting the status to 
529          *  something different from 'waiting'.
530          * We simply use 'paused'.
531          */   
532         $data = array("status"    => "paused");
534         /* Detect if the ids we got are valid and
535          *  check if the status allows pausing.
536          */ 
537         $update_ids = array();
538         $headpage = $this->getHeadpage();
539         foreach($ids as $id){
540             $tmp = $headpage->getEntry($id);
541             $entry = $tmp['EVENT']; 
542             if(isset($entry['STATUS']) && preg_match("/waiting/",$entry['STATUS'])){
543                 $update_ids[] = $entry['ID'];
544             }
545         }
547         /* Tell the daemon that we want to update some entries
548          */
549         if(count($update_ids)){
550             if(!$this->o_queue->update_entries($update_ids,$data)){
551                 msg_dialog::display(_("Error"), sprintf(_("Cannot update queue entry: %s"),$id) , ERROR_DIALOG);
552                 return(FALSE);
553             }
554         }
555         return(TRUE);
556     }
558     function save_object(){}
560     function save(){}
562     static function plInfo()
563     {
564         return (array(
565                     "plShortName"   => _("System deployment"),
566                     "plDescription" => _("Provide a mechanism to automatically activate systems"),
567                     "plSelfModify"  => FALSE,
568                     "plDepends"     => array(),
569                     "plPriority"    => 0,
570                     "plSection"     => array("addon"),
571                     "plCategory"    => array("gotomasses" => array("description" => _("System deployment"))),
572                     "plProvidedAcls" => array("Comment"   => _("Description")) 
573                     ));
574     }
577     function set_acl_base($base)
578     {
579         $this->acl_base= $base;
580     }
583     function set_acl_category($category)
584     {
585         $this->acl_category= "$category/";
586     }
589     function acl_is_writeable($attribute,$skip_write = FALSE)
590     {
591         if($this->read_only) return(FALSE);
592         $ui= get_userinfo();
593         return preg_match('/w/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute, $skip_write));
594     }
597     function acl_is_readable($attribute)
598     {
599         $ui= get_userinfo();
600         return preg_match('/r/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute));
601     }
604     function acl_is_createable($base ="")
605     {
606         if($this->read_only) return(FALSE);
607         $ui= get_userinfo();
608         if($base == "") $base = $this->acl_base;
609         return preg_match('/c/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
610     }
613     function acl_is_removeable($base ="")
614     {
615         if($this->read_only) return(FALSE);
616         $ui= get_userinfo();
617         if($base == "") $base = $this->acl_base;
618         return preg_match('/d/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
619     }
621     function acl_is_moveable($base = "")
622     {
623         if($this->read_only) return(FALSE);
624         $ui= get_userinfo();
625         if($base == "") $base = $this->acl_base;
626         return preg_match('/m/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
627     }
630     function getacl($attribute,$skip_write= FALSE)
631     {
632         $ui= get_userinfo();
633         $skip_write |= $this->read_only;
634         return  $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute,$skip_write);
635     }
638 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
639 ?>