Code

Updated queue handling for gotomasses.
[gosa.git] / include / class_hostActionQueue.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2007  Fabian Hickert
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
22 /*! 
23   \brief   This class represents the host action queue.
24   \author  Fabian Hickert <hickert@gonicus.de>
25   \version 2.6
26   \date    24.10.2007
28   This class is a queue handler, which allows adding,removing,
29    priority settings,stop and resume actions... for each queue entry
30  */
31 class hostActionQueue {
33   private $o_config       = NULL;
34   private $s_queue_file   = ""; 
35   private $s_error        = "";
36   private $b_error        = FALSE;
38   private $a_queue        = array();
39   private $i_pointer      = 0;
40   private $b_queue_loaded = false;
41   private $i_fileversion  = 0;
43   public function __construct($config)
44   {
45     $this->o_config= $config;
47     /* Define source file */
48     $this->s_queue_file = CONFIG_DIR."/gotomasses_machines";
50     $file = $this->o_config->search("gotomasses", "STORAGE_FILE",array('menu'));
51     if(!empty($file)){
52       $this->s_queue_file = $file;
53     }
54   }
56   private function _reload_queue()
57   {
58     $this->_reset_error();
59     $this->b_queue_loaded = FALSE;
60     $this->b_error        = FALSE;
61     $this->s_error        = "";
62     $this->i_pointer      = 0;
63  
64     /* Check file accessibility */
65     if(!file_exists($this->s_queue_file)){
66       $this->set_error(sprintf(_("Can't locate gotomasses queue file '%s'."),$this->s_queue_file));
67       return(FALSE);
68     }
69     if(!is_readable($this->s_queue_file)){
70       $this->set_error(sprintf(_("Can't read gotomasses queue file '%s'."),$this->s_queue_file));
71       return(FALSE);
72     }
74     /* Check if file contents could be read */
75     $fp = @fopen($this->s_queue_file,"r");
76     if(!$fp){
77       $this->set_error(sprintf(_("Can't read gotomasses storage file '%s'."),$this->s_queue_file));
78       return(FALSE);
79     }
80     $this->i_fileversion = filemtime($this->s_queue_file);
81     echo $this->i_fileversion."reload<br>";
82  
83     /* Get file contents */
84     $data ="";
85     while(!feof($fp)){
86       $data.= fread($fp,512);
87     }
88     
89     /* Get lines from file */
90     $this->a_queue  = array();
91     $comment        = "";
92     $rows           = split("\n",$data);
93   
94     /* Walk trough rows and parse data */
95     foreach($rows as $row){
97       /* Skip empty lines */
98       $row = trim($row);
99       if(empty($row)){
100         continue;
101       }
103       /* Get comment, if available */
104       if(preg_match("/^#/",$row)){
105         $comment = preg_replace("/^#/","",$row);
106         continue;
107       }
109       /* Split row into minutes/ hours ...*/
110       $row    = preg_replace('/[\t ]/umi'," ",$row);
111       $row    = preg_replace('/  */umi'," ",$row);
112       $parts  = split(" ",$row);
114       if(count($parts) != 10){
115         print_red(_("Entry broken, skipped."));
116       }else{
118         $entry = array();
119         $entry['Minute']  = $parts[0];
120         $entry['Hour']    = $parts[1];
121         $entry['Day']     = $parts[2];
122         $entry['Month']   = $parts[3];
123         $entry['Weekday'] = $parts[4];
124         $entry['Action']  = $parts[5];
125         $entry['OGroup']  = $parts[6];
126         $entry['Zone']    = $parts[7];
127         $entry['Section'] = $parts[8];
128         if($entry['Action'] == "initial_install"){
129           $tmp2 = split(";",$parts[9]);
130           foreach($tmp2 as $target){
131             $tmp = split(",",$target);
132             $entry['Initial_Target'][]  = array(
133                           "MAC"     => $tmp[0],
134                           "IP"      => $tmp[1],
135                           "NAME"    => $tmp[2]);
136           }
137           $entry['Target']  = array();
138         }else{
139           $entry['Initial_Target']  = array();
140           $entry['Target']  = split(";",$parts[7]);
141         }
142         $entry['Comment'] = $comment;
143         $this->a_queue[]   = $entry;
144       }
145     }
146     return(TRUE);
147   }
150   private function _save_data()
151   {
152     $this->_reset_error();
154     /* Check file accessibility */
155     if(!file_exists($this->s_queue_file)){
156       $this->set_error(sprintf(_("Can't locate gotomasses queue file '%s'."),$this->s_queue_file));
157       return(FALSE);
158     }
159     if(!is_writeable($this->s_queue_file)){
160       $this->set_error(sprintf(_("Can't write gotomasses queue file '%s'."),$this->s_queue_file));
161       return(FALSE);
162     }
163     if($this->i_fileversion != filemtime($this->s_queue_file)){
164       $this->set_error(_("The queue file was modified since last reload. Can't save changes."));
165       return(FALSE);
166     }
167     $fp = @fopen($this->s_queue_file,"w");
168     if(!$fp){
169       $this->set_error(sprintf(_("Can't write gotomasses queue file '%s'."),$this->s_queue_file));
170       return(FALSE);
171     }
173     $str = "#GOsa generated file, please just modify if you really know what you do.";
174     foreach($this->a_queue as $task){
175       $str .= "\n#".trim($task['Comment']);
176       $str .= "\n";
177       if($task['Action'] == "initial_install"){
178         $str .= "*     *     *     *     *     ";
179       }else{
180         $str .= str_pad($task['Minute'] ,5," ")." ";
181         $str .= str_pad($task['Hour']   ,5," ")." ";
182         $str .= str_pad($task['Day']    ,5," ")." ";
183         $str .= str_pad($task['Month']  ,5," ")." ";
184         $str .= str_pad($task['Weekday'],5," ")." ";
185       }
186       $str .= str_pad($task['Action'] ,5," ")." ";
187       $str .= str_pad($task['OGroup'] ,5," ")." ";
188       $str .= str_pad($task['Zone']   ,5," ")." ";
189       $str .= str_pad($task['Section'],5," ")." ";
190       if($task['Action'] == "initial_install"){
191         foreach($task['Initial_Target'] as $target){
192           $str .= trim($target['MAC']).",".trim($target['IP']).",".trim($target['NAME']).";";
193         }
194       }else{
195         foreach($task['Target'] as $target){
196           $str .= $target.";";
197         }
198       }
199       $str = preg_replace("/;$/","",$str);
200     }
202     /* Write contents */
203     $str .= "\n";
204     fwrite($fp,$str);
205     fclose($fp);
206     $this->i_fileversion = filemtime($this->s_queue_file);
207     return(TRUE);
208   }
210   public function add($entry)
211   {
212     $this->a_queue[] = $entry;
213     return($this->_save_data());
214   }
216   public function fetch()
217   {
218     if(isset($this->a_queue[$this->i_pointer])){
219       $p = $this->a_queue[$this->i_pointer];
220       $this->i_pointer ++;
221       return($p);
222     }   
223     return(FALSE);    
224   }
226   public function load()
227   {
228     return($this->_reload_queue());    
229   }
231   public function save()
232   {
233     return($this->_save_data());    
234   }
236   private function _reset_error()
237   {
238     $this->b_error = FALSE;
239     $this->s_error = "";
240   }
241  
242   private function set_error($str)
243   {
244     $this->b_error = TRUE;
245     $this->s_error = $str;
246   }
248   public function is_error()
249   {
250     return($this->b_error);
251   }
253   public function get_error()
254   {
255     return($this->s_error);
256   }
259 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
260 ?>