Code

Added action install
[gosa.git] / gosa-plugins / opsi / admin / opsi / class_opsi.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2008  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 3 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, see <http://www.gnu.org/licenses/>.
18  */
21 /********
23   __construct($config)
24   get_hosts_for_system_management()
25   get_netboot_products($host = "")
26   get_local_products($host = "")
27   get_product_properties()
28   set_product_properties()
29   get_client_hardware()
30   get_client_software()
31   list_clients()
32   del_client()
33   job_opsi_install_client()
34   add_client()
35   add_product_to_client()
36   del_product_from_client()
38  ********/
41 /*! \brief  This is the opsi base class, it handles 
42   .          gosa daemon requests and prepares data for opsi plugins.
43  */
44 class opsi extends gosaSupportDaemon 
45 {
46   private $config = NULL;
47   protected $use_alternative_xml_parse_method = TRUE;
49   /*! \brief            Create opsi object.
50     @param
51     @return             
52    */
53   public function __construct($config)
54   {
55     $this->config = $config;
56     gosaSupportDaemon::__construct($config);
57     $this->target = "00:01:6c:9d:b9:fa";
58   }
61   /******************
62     Opsi handling 
63    ******************/
65   function get_hosts_for_system_management()
66   {
67     $res = $this->list_clients();
68     $data = array();
69     foreach($res as $entry){
70       if(!isset($entry['MAC'][0]['VALUE'])) $entry['MAC'][0]['VALUE'] = "";
71       $data[] = array(
72         "dn"          => "opsi:=".$entry['NAME'][0]['VALUE'].",".get_ou("winstations").$this->config->current['BASE'],
73         "objectClass" => array("gosa_opsi_client"),
74         "cn"          => array(0 => $entry['NAME'][0]['VALUE']),
75         "description" => array(0 => $entry['DESCRIPTION'][0]['VALUE']),
76         "macAddress"  => array(0 => $entry['MAC'][0]['VALUE']),
77         "opsi_notes"  => array(0 => $entry['NOTES'][0]['VALUE']));
78     }
79     return($data);
80   }
83   /*! \brief  Maps all xml to array conversion to an alternative method
84                 then used in the parent class 'gosaSupportDaemon'.
85               The alternative method is able to handle more complex data.
86    */
87   private function xml_to_array($xml,$alternative_method = FALSE)
88   {
89     return(gosaSupportDaemon::xml_to_array($xml,TRUE));
90   }
93   /*! \brief  Trigger an event like wake or install for a specific hostId. 
94    */
95   public function send_action($type,$hostId,$mac)
96   {
97     switch($type){
98       case 'install'  :  $this->job_opsi_install_client($hostId,$mac); break;
99       default         :  trigger_error('Unknown type '.$type.'.');
100     }
101   }
104   /******************
105     SI Communication functions
106    ******************/
110   /*! \brief            Returns a list of all opsi clients.
111     @param
112     @return             
113    */
114   public function list_clients( $hostId = "")
115   {
116     $data   = array();
117     $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
118     $items  = array();
119     if(isset($res['XML'][0]['ITEM'])){
120       $items = $res['XML'][0]['ITEM'];
121     }
122     return($items);
123   }
126   /*! \brief            Adds a new opsi client.
127     @param
128     @return             
129    */
130   public function add_client($hostId,$macaddress,$notes,$description)
131   {
132     $data = array("hostId" => $hostId,"macaddress" => $macaddress);
134     if(empty($hostId)){
135       trigger_error("No valid host id given, check parameter 1.");
136       return;
137     }
138   
139     /* Add optional attributes */ 
140     foreach(array("notes","description") as $attr) {
141       if(!empty($$attr)){
142         $data[$attr] = $$attr;
143       }
144     }
146     /* Query SI server */
147     $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
148   }
151   /*! \brief            Modify an opsi client.
152     @param
153     @return             
154    */
155   public function modify_client($hostId,$mac,$notes,$description)
156   {
157     $data = array("hostId" => $hostId,"mac" => $mac);
159     if(empty($hostId)){
160       trigger_error("No valid host id given, check parameter 1.");
161       return;
162     }
163   
164     /* Add optional attributes */ 
165     foreach(array("notes","description") as $attr) {
166       if(!empty($$attr)){
167         $data[$attr] = $$attr;
168       }
169     }
171     /* Query SI server */
172     $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
173   }
177   /*! \brief            Returns a list of netboot products.
178     @param
179     @return             
180    */
181   public function get_netboot_products($host = "")
182   {
183     /* Append host attribute to query data 
184      */
185     $data = array();
186     if(!empty($host)){
187       $data['hostId'] = trim($host);
188     }
190     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
191     $items = array();
192     if(isset($res['XML'][0]['ITEM'])){
193       foreach($res['XML'][0]['ITEM'] as $entry){
194         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
195                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
196         $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
197       }
198     }
199     return($items);
200   }
203   /*! \brief            Returns a list of all local products.
204     @param
205     @return             
206    */
207   public function get_local_products($host = "")
208   {
209     /* Append host attribute to query data 
210      */
211     $data = array();
212     if(!empty($host)){
213       $data['hostId'] = trim($host);
214     }
216     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
217     $items = array();
218     if(isset($res['XML'][0]['ITEM'])){
219       foreach($res['XML'][0]['ITEM'] as $entry){
220         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
221                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
222         $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
223       }
224     }
225     return($items);
226   }
229   /*! \brief            Returns a list of all product properties. \ 
230     .           Additionally you can specify the host parameter to \
231     .           get host specific product properties
232     @param
233     @return             
234    */
235   public function get_product_properties($productId,$hostId = "")
236   {
237     $data = array("productId" => $productId);
239     /* Append host attribute to query data 
240      */
241     if(!empty($hostId)){
242       $data['hostId'] = trim($hostId);
243     }
244    
245     /* Check parameter */ 
246     if(empty($productId)){
247       trigger_error("No valid product id given, check parameter 1.");
248       return(array());
249     }
251     /* Query SI server */
252     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
253     $items  = array();
254     if(isset($res['XML'][0]['ITEM'])){   
255       foreach($res['XML'][0]['ITEM'] as $entry){
256         foreach($entry as $name => $val){
257           $items[$name] = $val['0']['VALUE'];
258         }
259       }
260     }
261     return($items);
262   }
265   /*! \brief            Set product properties, globally or per host. 
266     @param
267     @return             
268    */
269   public function set_product_properties($productId,$cfg,$hostId = "")
270   {
271     $data = array("productId" => $productId);
273     /* Append host attribute to query data 
274      */
275     if(!empty($hostId)){
276       $data['hostId'] = trim($hostId);
277     }
278    
279     /* Check parameter */ 
280     if(empty($productId)){
281       trigger_error("No valid product id given, check parameter 1.");
282       return(array());
283     }
285     if(!count($cfg)) return;
286     
287     /* Add properties */
288     $data['item'] = array();
289     foreach($cfg as $name => $value){
290       $data['item'][] = "<name>".$name."</name><value>".$value."</value>";
291     }  
293     /* Query SI server */
294     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
295   }
298   /*! \brief            Adds a given product to a client.
299     @param
300     @return             
301    */
302   public function add_product_to_client($productId,$hostId)
303   {
304     $data = array("productId" => $productId,"hostId" => $hostId);
306     /* Check parameter */ 
307     if(empty($productId)){
308       trigger_error("No valid product id given, check parameter 1.");
309       return;
310     }
311     if(empty($hostId)){
312       trigger_error("No valid host id given, check parameter 2.");
313       return;
314     }
316     /* Query SI server */
317     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
318   }
321   /*! \brief      Removes a given product from a client.
322     @param
323     @return
324    */
325   public function del_product_from_client($productId,$hostId)
326   {
327     $data = array("productId" => $productId,"hostId" => $hostId);
329     /* Check parameter */ 
330     if(empty($productId)){
331       trigger_error("No valid product id given, check parameter 1.");
332       return;
333     }
334     if(empty($hostId)){
335       trigger_error("No valid host id given, check parameter 2.");
336       return;
337     }
339     /* Query SI server */
340     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
341   }
344   /*! \brief            Returns the clients hardware setup.
345     @param
346     @return             
347    */
348   public function get_client_hardware($hostId)
349   {
350     $data = array("hostId" => $hostId);
352     /* Check parameter */ 
353     if(empty($hostId)){
354       trigger_error("No valid host id given, check parameter 1.");
355       return;
356     }
358     /* Query SI server */
359     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
360     if(isset($res['XML'][0]['ITEM'])){
361       return($res['XML'][0]['ITEM']);
362     }
363     return(array());
364   }
367   /*! \brief            Returns the clients software setup.
368     @param
369     @return             
370    */
371   public function get_client_software($hostId)
372   {
373     $data = array("hostId" => $hostId);
375     /* Check parameter */ 
376     if(empty($hostId)){
377       trigger_error("No valid host id given, check parameter 1.");
378       return;
379     }
381     /* Query SI server */
382     $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
383     if(isset($res['XML'][0]['ITEM'])){
384       return($res['XML'][0]['ITEM']);
385     }
386     return(array());
387   }
391   /*! \brief            Deletes the given opsi client.
392     @param
393     @return             
394    */
395   public function del_client($hostId)
396   {
397     $data = array("hostId" => $hostId);
399     /* Check parameter */ 
400     if(empty($hostId)){
401       trigger_error("No valid host id given, check parameter 1.");
402       return;
403     }
405     /* Query SI server */
406     $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
407     if(isset($res['XML'][0]['ITEM'])){
408       return($res['XML'][0]['ITEM']);
409     }
410     return(array());
411   }
414   /*! \brief            Triggers install/reinstall of an opsi client.
415     @param
416     @return             
417    */
418   public function job_opsi_install_client($hostId,$mac)
419   {
420     $data = array("hostId" => $hostId,"macaddress"=>$mac);
422     /* Check parameter */ 
423     if(empty($hostId)){
424       trigger_error("No valid host id given, check parameter 1.");
425       return;
426     }
428     /* Query SI server */
429     $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
430   }
432 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
433 ?>