Code

Updated opsi handling. Only perform actions, if fai is activated/installed
[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   public function __construct($config)
24   public function enabled()
25   function get_hosts_for_system_management()
26   private function xml_to_array($xml,$alternative_method = FALSE)
27   public function send_action($type,$hostId,$mac)
28   public function list_clients( $hostId = "")
29   public function add_client($hostId,$macaddress,$notes,$description)
30   public function modify_client($hostId,$mac,$notes,$description)
31   public function get_netboot_products($host = "")
32   public function get_local_products($host = "")
33   public function get_product_properties($productId,$hostId = "")
34   public function set_product_properties($productId,$cfg,$hostId = "")
35   public function add_product_to_client($productId,$hostId)
36   public function del_product_from_client($productId,$hostId)
37   public function get_client_hardware($hostId)
38   public function get_client_software($hostId)
39   public function del_client($hostId)
40   public function job_opsi_install_client($hostId,$mac)
42  ********/
45 /*! \brief  This is the opsi base class, it handles 
46   .          gosa daemon requests and prepares data for opsi plugins.
47  */
48 class opsi extends gosaSupportDaemon 
49 {
50   private $config = NULL;
51   protected $use_alternative_xml_parse_method = TRUE;
52   protected $target = "";
54   /*! \brief            Create opsi object.
55     @param
56     @return             
57    */
58   public function __construct($config)
59   {
60     $this->config = $config;
61     gosaSupportDaemon::__construct($config);
63     /* Detect the target opsi host 
64      */
65     $tmp= $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
66     if(!empty($tmp) && class_available("faiManagement")){
67       $opsi_hosts = $this->get_hosts_with_module("opsi_com");
69       /* Just use the first result of the opsi hosts 
70        */
71       if(count($opsi_hosts) == 1 && isset($opsi_hosts[0])){
72         $this->target = $opsi_hosts[0];
73       }elseif(count($opsi_hosts) > 1){
74         $this->target = $opsi_hosts[0];
75         msg_dialog::display(_("Opsi"),sprintf(_("More than one Opsi server were found, using the first result '%s'."),$this->target));
76       }
77     }
78   }
80   
81   public function enabled()
82   {
83     $tmp= $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
84     if(!empty($tmp) && class_available("faiManagement") && !empty($this->target)){
85       return(TRUE);
86     }   
87     return(FALSE);
88   }
91   /******************
92     Opsi handling 
93    ******************/
95   function get_hosts_for_system_management()
96   {
97     $res = $this->list_clients();
98     $data = array();
99     $ui = get_userinfo();
100     foreach($res as $entry){
101       if(!isset($entry['MAC'][0]['VALUE'])) $entry['MAC'][0]['VALUE'] = "";
102       $obj = array(
103         "dn"          => "opsi:=".$entry['NAME'][0]['VALUE'].",".get_ou("sambaMachineAccountRDN").$this->config->current['BASE'],
104         "objectClass" => array("gosa_opsi_client"),
105         "cn"          => array(0 => $entry['NAME'][0]['VALUE']),
106         "macAddress"  => array(0 => $entry['MAC'][0]['VALUE']),
107         "opsi_notes"  => array(0 => $entry['NOTES'][0]['VALUE']));
109       /* Check permissions */
110       $opsi_acl = $ui->get_permissions($obj['dn'],"opsi/opsiGeneric");
111       if(preg_match("/r/",$opsi_acl)){
112         if(!empty($entry['DESCRIPTION'][0]['VALUE'])){ 
113           $obj["description"] =$entry['DESCRIPTION'][0]['VALUE'];
114         }
115         $data[] = $obj;
116       }
117     }
118     return($data);
119   }
122   /*! \brief  Maps all xml to array conversion to an alternative method
123                 then used in the parent class 'gosaSupportDaemon'.
124               The alternative method is able to handle more complex data.
125    */
126   private function xml_to_array($xml,$alternative_method = FALSE)
127   {
128     return(gosaSupportDaemon::xml_to_array($xml,TRUE));
129   }
132   /*! \brief  Trigger an event like wake or install for a specific hostId. 
133    */
134   public function send_action($type,$hostId,$mac)
135   {
136     switch($type){
137       case 'install'  :  $this->job_opsi_install_client($hostId,$mac); break;
138       default         :  trigger_error('Unknown type '.$type.'.');
139     }
140   }
143   /******************
144     SI Communication functions
145    ******************/
149   /*! \brief            Returns a list of all opsi clients.
150     @param
151     @return             
152    */
153   public function list_clients( $hostId = "")
154   {
155     $data   = array();
156     $res    = $this->send_data("gosa_opsi_list_clients",$this->target,$data,TRUE);
157     $items  = array();
158     if(isset($res['XML'][0]['ITEM'])){
159       $items = $res['XML'][0]['ITEM'];
160     }
161     return($items);
162   }
165   /*! \brief            Adds a new opsi client.
166     @param
167     @return             
168    */
169   public function add_client($hostId,$macaddress,$notes,$description)
170   {
171     $data = array("hostId" => $hostId,"macaddress" => $macaddress);
173     if(empty($hostId)){
174       trigger_error("No valid host id given, check parameter 1.");
175       return;
176     }
177   
178     /* Add optional attributes */ 
179     foreach(array("notes","description") as $attr) {
180       if(!empty($$attr)){
181         $data[$attr] = $$attr;
182       }
183     }
185     /* Query SI server */
186     $res    = $this->send_data("gosa_opsi_add_client",$this->target,$data,TRUE);
187   }
190   /*! \brief            Modify an opsi client.
191     @param
192     @return             
193    */
194   public function modify_client($hostId,$mac,$notes,$description)
195   {
196     $data = array("hostId" => $hostId,"mac" => $mac);
198     if(empty($hostId)){
199       trigger_error("No valid host id given, check parameter 1.");
200       return;
201     }
202   
203     /* Add optional attributes */ 
204     foreach(array("notes","description") as $attr) {
205       $data[$attr] = $$attr;
206     }
208     /* Query SI server */
209     $res = $this->send_data("gosa_opsi_modify_client",$this->target,$data,TRUE);
210   }
214   /*! \brief            Returns a list of netboot products.
215     @param
216     @return             
217    */
218   public function get_netboot_products($host = "")
219   {
220     /* Append host attribute to query data 
221      */
222     $data = array();
223     if(!empty($host)){
224       $data['hostId'] = trim($host);
225     }
227     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
228     $items = array();
229     if(isset($res['XML'][0]['ITEM'])){
230       foreach($res['XML'][0]['ITEM'] as $entry){
231         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
232                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
233         $items[$entry['PRODUCTID'][0]['VALUE']] = $e;
234       }
235     }
236     return($items);
237   }
240   /*! \brief            Returns a list of all local products.
241     @param
242     @return             
243    */
244   public function get_local_products($host = "")
245   {
246     /* Append host attribute to query data 
247      */
248     $data = array();
249     if(!empty($host)){
250       $data['hostId'] = trim($host);
251     }
253     $res    = $this->send_data("gosa_opsi_get_local_products",$this->target,$data,TRUE);
254     $items = array();
255     if(isset($res['XML'][0]['ITEM'])){
256       foreach($res['XML'][0]['ITEM'] as $entry){
257         $e = array("DESC" => $entry['DESCRIPTION'][0]['VALUE'],
258                    "NAME" => $entry['PRODUCTID'][0]['VALUE']);
259         $items[$entry['PRODUCTID'][0]['VALUE']] = $e; 
260       }
261     }
262     return($items);
263   }
266   /*! \brief            Returns a list of all product properties. \ 
267     .           Additionally you can specify the host parameter to \
268     .           get host specific product properties
269     @param
270     @return             
271    */
272   public function get_product_properties($productId,$hostId = "")
273   {
274     $data = array("productId" => $productId);
276     /* Append host attribute to query data 
277      */
278     if(!empty($hostId)){
279       $data['hostId'] = trim($hostId);
280     }
281    
282     /* Check parameter */ 
283     if(empty($productId)){
284       trigger_error("No valid product id given, check parameter 1.");
285       return(array());
286     }
288     /* Query SI server */
289     $res    = $this->send_data("gosa_opsi_get_product_properties",$this->target,$data,TRUE);
290     $items  = array();
291     if(isset($res['XML'][0]['ITEM'])){   
292       foreach($res['XML'][0]['ITEM'] as $entry){
293         foreach($entry as $name => $val){
295           foreach(array("DESCRIPTION","DEFAULT") as $attr){
296             $items[$name][$attr] = "";
297             if(isset($val[0][$attr])){
298               $items[$name][$attr] = $val[0][$attr][0]['VALUE'];
299             }
300           }
301           $items[$name]['VALUE'] = array();
302           if(isset($val['0']['VALUE'])){
303             foreach($val['0']['VALUE'] as $value){
304               $items[$name]['VALUE'][] = $value['VALUE'];
305             }
306           }
307           $items[$name]['VALUE_CNT'] = count($items[$name]['VALUE']);
308         }
309       }
310     }
311     return($items);
312   }
315   /*! \brief            Set product properties, globally or per host. 
316     @param
317     @return             
318    */
319   public function set_product_properties($productId,$cfg,$hostId = "")
320   {
321     $data = array("productId" => $productId);
323     /* Append host attribute to query data 
324      */
325     if(!empty($hostId)){
326       $data['hostId'] = trim($hostId);
327     }
328    
329     /* Check parameter */ 
330     if(empty($productId)){
331       trigger_error("No valid product id given, check parameter 1.");
332       return(array());
333     }
335     if(!count($cfg)) return;
336     
337     /* Add properties */
338     $data['item'] = array();
339     foreach($cfg as $name => $value){
340       $data['item'][] = "<name>".$name."</name><value>".$value['DEFAULT']."</value>";
341     }  
343     /* Query SI server */
344     $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
345   }
348   /*! \brief            Adds a given product to a client.
349     @param
350     @return             
351    */
352   public function add_product_to_client($productId,$hostId)
353   {
354     $data = array("productId" => $productId,"hostId" => $hostId);
356     /* Check parameter */ 
357     if(empty($productId)){
358       trigger_error("No valid product id given, check parameter 1.");
359       return;
360     }
361     if(empty($hostId)){
362       trigger_error("No valid host id given, check parameter 2.");
363       return;
364     }
366     /* Query SI server */
367     $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
368   }
371   /*! \brief      Removes a given product from a client.
372     @param
373     @return
374    */
375   public function del_product_from_client($productId,$hostId)
376   {
377     $data = array("productId" => $productId,"hostId" => $hostId);
379     /* Check parameter */ 
380     if(empty($productId)){
381       trigger_error("No valid product id given, check parameter 1.");
382       return;
383     }
384     if(empty($hostId)){
385       trigger_error("No valid host id given, check parameter 2.");
386       return;
387     }
389     /* Query SI server */
390     $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
391   }
394   /*! \brief            Returns the clients hardware setup.
395     @param
396     @return             
397    */
398   public function get_client_hardware($hostId)
399   {
400     $data = array("hostId" => $hostId);
402     /* Check parameter */ 
403     if(empty($hostId)){
404       trigger_error("No valid host id given, check parameter 1.");
405       return;
406     }
408     /* Query SI server */
409     $res    = $this->send_data("gosa_opsi_get_client_hardware",$this->target,$data,TRUE);
410     if(isset($res['XML'][0]['ITEM'])){
411       return($res['XML'][0]['ITEM']);
412     }
413     return(array());
414   }
417   /*! \brief            Returns the clients software setup.
418     @param
419     @return             
420    */
421   public function get_client_software($hostId)
422   {
423     $data = array("hostId" => $hostId);
425     /* Check parameter */ 
426     if(empty($hostId)){
427       trigger_error("No valid host id given, check parameter 1.");
428       return;
429     }
431     /* Query SI server */
432     $res    = $this->send_data("gosa_opsi_get_client_software",$this->target,$data,TRUE);
433     if(isset($res['XML'][0]['ITEM'])){
434       return($res['XML'][0]['ITEM']);
435     }
436     return(array());
437   }
441   /*! \brief            Deletes the given opsi client.
442     @param
443     @return             
444    */
445   public function del_client($hostId)
446   {
447     $data = array("hostId" => $hostId);
449     /* Check parameter */ 
450     if(empty($hostId)){
451       trigger_error("No valid host id given, check parameter 1.");
452       return;
453     }
455     /* Query SI server */
456     $res    = $this->send_data("gosa_opsi_del_client",$this->target,$data,TRUE);
457     if(isset($res['XML'][0]['ITEM'])){
458       return($res['XML'][0]['ITEM']);
459     }
460     return(array());
461   }
464   /*! \brief            Triggers install/reinstall of an opsi client.
465     @param
466     @return             
467    */
468   public function job_opsi_install_client($hostId,$mac)
469   {
470     $data = array("hostId" => $hostId,"macaddress"=>$mac);
472     /* Check parameter */ 
473     if(empty($hostId)){
474       trigger_error("No valid host id given, check parameter 1.");
475       return;
476     }
478     /* Query SI server */
479     $this->send_data("job_opsi_install_client",$this->target,$data,TRUE);
480   }
482 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
483 ?>