Code

Comments
[gosa.git] / gosa-plugins / goto / admin / systems / goto / Device / class_InstallRecipe.inc
1 <?php
3 class InstallRecipe extends plugin
4 {
6     public $rpcErrorMessage = "";
8     // Template options
9     public $installTemplate = "";
10     public $installationTemplateNames = array();
11     public $templateToRelease = array();
12     public $installationTemplates = array();
13     
14     // The selected installation release
15     public $installRelease = "";
17     // Selected config management variant
18     public $installConfigManagement = '';
19     public $installConfigManagementList = array();
21     // Bootstrap method
22     public $installBootstrapMethod = '';
23     public $installBootstrapMethodList = array();
25     // Timezone settings
26     public $installTimezone = "";
27     public $installTimeUTC = "";
29     // Kernel packages
30     public $installKernelPackage = "";
31     public $installKernelPackageList = array();
33     // System Locale
34     public $installSystemLocale = "";
35     public $installSystemLocaleList = array();
37     // Selected NTP Server
38     public $installNTPServer = array();
39     public $installNTPServerList = NULL;
41     // Root password
42     public $installRootEnabled = "";
43     public $installRootPasswordHash = "";
44     public $setPasswordHash = FALSE;
46     // Partition table variables
47     public $installPartitionTable = "";
49     // Keyboard-layout attribute
50     public $installKeyboardlayout = "";
51     public $installKeyboardlayoutList = array();
53     // Mirror related attribute
54     public $installMirror = "";
55     public $installMirrorList = array();
57     public $objectclasses = array();
58     public $attributes = array("installRelease", "installTemplate","installKeyboardlayout","installSystemLocale",
59             "installTimezone","installTimeUTC","installNTPServer","installMirror",
60             "installRootEnabled","installRootPasswordHash","installKernelPackage",
61             "installPartitionTable","installConfigManagement","installBootstrapMethod");
64     /* Some plugin management related attributes 
65      * See class plugin for details.
66      */
67     public $view_logged = FALSE;
68     public $ignore_account = FALSE;
71     /*! \brief  Initialize the plugin and its widgets. 
72      *          Finally call init() to load values from the backend.
73      */ 
74     function __construct(&$config, $dn)
75     {
76         plugin::plugin($config, $dn);
78         // Set default password hash
79         $this->hash = $this->config->get_cfg_value("core","passwordDefaultHash"); 
81         // Get list of password hashes
82         $tmp = passwordMethod::get_available_methods();
83         $this->hashes = array();
84         foreach($tmp['name'] as $name){
85             $this->hashes[$name] = $name;
86         }
88         // Init the object values.
89         $this->init_static_info();
90         $this->init_client_info();
92         // Prepare NTP servers list 
93         $this->installNTPServerList= new sortableListing($this->installNTPServer);
94         $this->installNTPServerList->setDeleteable(true);
95         $this->installNTPServerList->setEditable(false);
96         $this->installNTPServerList->setColspecs(array('*'));
97         $this->installNTPServerList->setWidth("100%");
98         $this->installNTPServerList->setHeight("70px");
100         // Preset values for new devices
101         if(!$this->is_account){
102             $this->installTemplate = key($this->installationTemplates);
103             $this->installRelease = key($this->templateToRelease[$this->installTemplate]);
104         }else{
105             if(!isset($this->installationTemplates[$this->installTemplate])){
106                 print "<br>Invalid template selected {$this->installTemplate}";
107                 $this->installTemplate = key($this->installationTemplates);
108             }elseif(!isset($this->installationTemplates[$this->installTemplate]['method'])){
109                 print "<br>No bootstrap method found!";
110                 
111             }elseif(!in_array($this->installRelease, $this->templateToRelease[$this->installTemplate])){
112                 print "<br>Invalid release selected {$this->installRelease}";
113                 $this->installRelease = key($this->templateToRelease[$this->installTemplate]);
114             }
115         } 
116         $this->installBootstrapMethod = $this->installationTemplates[$this->installTemplate]['method'];
117         $this->reloadInstallationKernelPackages();
118         $this->reloadInstallationMirrors();
119     }
121    
122     /*! \brief   Load attribute values from the GOsa-backend using jsonRPC 
123      */ 
124     function init_client_info()
125     {
126         $uuid = "a0d17918-cfb2-11df-acbd-5452005f1250";
127         $rpc = $this->config->getRpcHandle();
128         $res = $rpc->systemGetBaseInstallParameters($uuid);
129        
130         // Failed to load backend values, keep reported error and return with FALSE; 
131         $this->rpcError = !$rpc->success();
132         if(!$rpc->success()){
133             $this->rpcErrorMessage = $rpc->get_error();
134             return(NULL);
135         }
137         // Apply received options
138         $this->is_account = $res == True;
139         if($this->is_account){
140             $this->installNTPServer = $res['ntp-servers'];
141             $this->installTemplate = $res['template'];
142             $this->installRelease = $res['release'][0];
143             $this->installTimezone = array_search($res['timezone'][0], $this->timezones);
144             $this->installTimeUTC = $res['utc'][0];
145             $this->installKernelPackage = $res['kernel'][0];;
146             $this->installSystemLocale = $res['system-locale'][0];
147             $this->installRootEnabled = $res['root-user'][0];
148             $this->installRootPasswordHash = $res['root-hash'][0];
149             $this->installPartitionTable = $res['disk-setup'][0];
150             $this->installKeyboardlayout = $res['keyboard-layout'];
151         }
153         return;
154     }
157     function init_static_info()
158     { 
159         // Load static values.
160         foreach(array(
161                 "timeozones" => "getTimezones",
162                 "distributions" => "loadDistributions",
163                 "keyboardLayouts" => "loadInstallationKeyboardLayouts",
164                 "installationTemplates" => "loadInstallationTemplates",
165                 "installationMethods" => "loadInstallationMethods",
166                 "locales" => "getSystemLocales"    ) as $target => $func){
167             $$target = $this->$func();
168             if($$target == NULL){
169                 $this->initFailed = TRUE;
170                 msg_dialog::display(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
171                 return;
172             }
173         }
175         // Assign fetchted values
176         $this->timezones = $timeozones;
177         $this->installBootstrapMethodList = $installationMethods['installBootstrapMethodList'];
178         $this->installConfigManagementList = $installationMethods['installConfigManagementList'];
179         $this->installSystemLocaleList = $locales;
180         $this->installKeyboardlayoutList = $keyboardLayouts;
181         $this->distributions = $distributions;
183         # Create template to relase map
184         $this->templateToRelease = array();
186         // Walk through installation templates and use the 'repositories' attribute
187         // to supported releases.
188         foreach($installationTemplates as $name => $tdata){
189             
190             $mName = isset($this->installBootstrapMethodList[$tdata['method']]) ? 
191                 $this->installBootstrapMethodList[$tdata['method']]: $tdata['method'];
193             $this->installationTemplateNames[$name] = $tdata['description']." (".$mName.")";
194             $this->installationTemplates[$name] = $tdata;
195             $this->templateToRelease[$name] = array();
197             // Check if the 'repository'-category of the template can be found in any 
198             // received release list.
199             if(isset($installationMethods[$tdata['method']]['repositories'])){
200                 $repos = $installationMethods[$tdata['method']]['repositories'];
201                 foreach($distributions as $dist){
202                     if(in_array($dist['type']['name'],$repos) && isset($dist['releases'])){
203                         foreach($dist['releases'] as $release){
204                             $rname = $dist['name'].'/'.$release['name'];
205                             $this->templateToRelease[$name][$rname] = $rname;
206                         }
207                     }
208                 }
209             }
210         }
211     }
213     
214     function execute()
215     {
216         // Log account access
217         if($this->is_account && !$this->view_logged){
218             $this->view_logged = TRUE;
219             new log("view","Device/".get_class($this),$this->dn);
220         }
222         /***************
223           Handle account state
224          ***************/
226         // Allow to add or remove the distribution list extension
227         if(isset($_POST['modify_state'])){
228             if($this->is_account && $this->acl_is_removeable()){
229                 $this->is_account= FALSE;
230             }elseif(!$this->is_account && $this->acl_is_createable()){
231                 $this->is_account= TRUE;
232             }
233         }
235         // Show account status-changer
236         $display = "";
237         if ($this->parent !== NULL){
238             if ($this->is_account){
239                 $display= $this->show_disable_header(_("Remove install profile"),
240                         msgPool::featuresEnabled(_("Install profile")));
241             } else {
242                 $display= $this->show_enable_header(_("Add install profile"),
243                         msgPool::featuresDisabled(_("Install profile")));
244                 return ($display);
245             }
246         }
248         /***************
249           Root password hash dialog
250          ***************/
252         if($this->setPasswordHash){
253             $this->dialog = TRUE;
254             $smarty = get_smarty();
255             $smarty->assign('hashes', set_post($this->hashes));
256             $smarty->assign('hash', set_post($this->hash));
257             return($smarty->fetch(get_template_path('goto/Device/SetPassword.tpl', TRUE)));
258         }
260         /***************
261           Generate HTML content
262          ***************/
264         $this->installNTPServerList->setAcl($this->getacl('installNTPServer'));
265         $this->installNTPServerList->update();
267         plugin::execute();    
268         $smarty = get_smarty();
269         $smarty->assign('installTemplateList', $this->installationTemplateNames);
270         $smarty->assign('installReleaseList', $this->templateToRelease[$this->installTemplate]);
273         $smarty->assign('timezones', $this->timezones);
274         $smarty->assign('installKeyboardlayoutList', $this->installKeyboardlayoutList);
275         $smarty->assign('installKernelPackageList', $this->installKernelPackageList);
276         $smarty->assign('installMirrorList', $this->installMirrorList);
277         $smarty->assign('installSystemLocaleList', $this->installSystemLocaleList);
278         $smarty->assign('installNTPServerList', $this->installNTPServerList->render());
280         $smarty->assign('installBootstrapMethod', $this->installBootstrapMethod);
281         $smarty->assign('installBootstrapMethodList', $this->installBootstrapMethodList);
282         $smarty->assign('installConfigManagement', $this->installConfigManagement);
284         // Assign list of available config-management types.
285         if(isset($this->installConfigManagementList[$this->installBootstrapMethod])){ 
286             $smarty->assign('installConfigManagementList', $this->installConfigManagementList[$this->installBootstrapMethod]);
287         }else{
288             $smarty->assign('installConfigManagementList', array());
289         }
291         foreach($this->attributes as $attr){
292             $smarty->assign($attr, $this->$attr);
293         }
294         $this->dialog = false;
295         return($display.$smarty->fetch(get_template_path('goto/Device/InstallRecipe.tpl', TRUE)));
296     }
299     function save_object()
300     {
301         if(isset($_POST['InstallRecipePosted'])){
303             $currentInstallMethod = $this->installConfigManagement;
305             $oldRelease = $this->installRelease;
306             plugin::save_object();
307             $this->installRootEnabled = isset($_POST['installRootEnabled']);
308             $this->installTimeUTC = isset($_POST['installTimeUTC']);
311             if(isset($_POST['setPasswordHash'])){
312                 $this->setPasswordHash = true;
313             }
315             $this->installNTPServerList->save_object();
316             $this->installNTPServer = $this->installNTPServerList->getMaintainedData();
317             if(isset($_POST['installNTPServer_Input']) && isset($_POST['installNTPServer_Add'])){
318                 $add = get_post('installNTPServer_Input');
319                 if(!in_array($add, $this->installNTPServer) && !empty($add)){
320                     $this->installNTPServer[] = $add;
321                 }
322             }
323             $this->installNTPServerList->setListData($this->installNTPServer);
324             $this->installBootstrapMethod = $this->installationTemplates[$this->installTemplate]['method'];
326             // Reload list of kernel packages and mirrors
327             if($oldRelease != $this->installRelease){
328                 $this->reloadInstallationKernelPackages();
329                 $this->reloadInstallationMirrors();
330             }
331         }
333         if(isset($_POST['cancelPassword'])) $this->setPasswordHash =false;
334         if(isset($_POST['setPassword'])) {
335             $this->setPasswordHash =false;
336             $hash = get_post('passwordHash');
338             // Not sure, why this is here, but maybe some encryption methods require it.
339             mt_srand((double) microtime()*1000000);
341             // Generate the requested hash
342             $methods = new passwordMethod($this->config, $this->dn);
343             $available = $methods->get_available_methods();
344             $test = new $available[$hash]($this->config,$this->dn);
345             $this->installRootPasswordHash = @$test->generate_hash(get_post('rootPassword'));
346         }
347     }
350     function save()
351     {
352         if(!$this->installRootEnabled) $this->installRootPasswordHash = "";
353         
354         $uuid = "a0d17918-cfb2-11df-acbd-5452005f1250";
355         $rpc = $this->config->getRpcHandle();
357         $data = array();
358         $res = $rpc->systemSetBaseInstallParameters($uuid, $data);
360         
362        
363         // Failed to load backend values, keep reported error and return with FALSE; 
364         $this->rpcError = !$rpc->success();
365         if(!$rpc->success()){
366             $this->rpcErrorMessage = $rpc->get_error();
367             return(NULL);
368         }
369         return($res);
371     }
373     
374     function remove_from_parent()
375     {
376        print "Missing"; 
377     }
378  
379     
380     /*! \brief  Refreshes the list of selectable kernel packages.
381      */
382     function reloadInstallationKernelPackages()
383     {
384         $rpc = $this->config->getRpcHandle();
385         $res = $rpc->getKernelPackages($this->installRelease);
386       
387         // Failed to load backend values, keep reported error and return with FALSE; 
388         $this->rpcError = !$rpc->success();
389         if(!$rpc->success()){
390             $this->rpcErrorMessage = $rpc->get_error();
391             msg_dialog::display(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
392         }else{
393             $this->installKernelPackageList = array();
394             foreach($res as $name){
395                 $this->installKernelPackageList[$name] = $name;
396             }
398             if(!isset($this->installKernelPackageList[$this->installKernelPackage])){
399                 $new = key($this->installKernelPackageList);
400                 msg_dialog::display(_("Warning"), sprintf(_("The selected kernel package '%s' is no longer available! The kernel package '%s' was selected as replacement!"), $this->installKernelPackage, $new), WARNING_DIALOG);
401                 $this->installKernelPackage = $new;
402             }
403         }
404     }
407     /*! \brief  Returns a list of distributions on success.
408      *          In case of an error, it returns NULL.
409      */
410     function loadDistributions()
411     {
412         $rpc = $this->config->getRpcHandle();
413         $res = $rpc->getDistributions();
414        
415         // Failed to load backend values, keep reported error and return with FALSE; 
416         $this->rpcError = !$rpc->success();
417         if(!$rpc->success()){
418             $this->rpcErrorMessage = $rpc->get_error();
419             return(NULL);
420         }
421         return($res);
422     }
425     /*! \brief  Returns a list of timezones on success.
426      *          In case of an error, it returns NULL.
427      */
428     function getTimezones()
429     {
430         $rpc = $this->config->getRpcHandle();
431         $res = $rpc->getTimezones();
432        
433         // Failed to load backend values, keep reported error and return with FALSE; 
434         $this->rpcError = !$rpc->success();
435         if(!$rpc->success()){
436             $this->rpcErrorMessage = $rpc->get_error();
437             return(NULL);
438         }
439         return($res);
440     }
441     
443     /*! \brief  Returns a list of installation mirrors on success.
444      *          In case of an error, it returns NULL.
445      */
446     function reloadInstallationMirrors()
447     {
448         $res = array(
449                 'dc=intranet,dc=gonicus,dc=de' => 'Intranet',
450                 'ou=systems,dc=intranet,dc=gonicus,dc=de' => 'System');
451         $this->installMirrorList =$res;
452     }
455     /*! \brief  Returns a list of installation templates on success.
456      *          In case of an error, it returns NULL.
457      */
458     function loadInstallationTemplates()
459     {
460         $rpc = $this->config->getRpcHandle();
461         $res = $rpc->installListTemplates();
462        
463         // Failed to load backend values, keep reported error and return with FALSE; 
464         $this->rpcError = !$rpc->success();
465         if(!$rpc->success()){
466             $this->rpcErrorMessage = $rpc->get_error();
467             return(NULL);
468         }
469         return($res);
470     } 
473     /*! \brief  Returns a list of keyboard layouts on success.
474      *          In case of an error, it returns NULL.
475      */
476     function loadInstallationKeyboardLayouts()
477     {
478         $rpc = $this->config->getRpcHandle();
479         $res = $rpc->getKeyboardModels();
480        
481         // Failed to load backend values, keep reported error and return with FALSE; 
482         $this->rpcError = !$rpc->success();
483         if(!$rpc->success()){
484             $this->rpcErrorMessage = $rpc->get_error();
485             return(NULL);
486         }
487         $lay = array();
488         foreach($res as $key => $data){
489             $lay[$key] = "{$data[0]} - {$data[2]}";
490         }
491         asort($lay);
492         return($lay);
493     } 
495     
496     /*! \brief  Returns a list of system locales on success.
497      *          In case of an error, it returns NULL.
498      */
499     function getSystemLocales()
500     {
501         $rpc = $this->config->getRpcHandle();
502         $res = $rpc->getSystemLocales();
503       
504         // Failed to load backend values, keep reported error and return with FALSE; 
505         $this->rpcError = !$rpc->success();
506         if(!$rpc->success()){
507             $this->rpcErrorMessage = $rpc->get_error();
508             return(NULL);
509         }
511         return($res);
512     }
513  
515     /*! \brief  Try to load install and bootstrap methods from the GOsa-backend
516      *          This is done via jsonRPC, if calling the backend method fails,
517      *           this method returns FALSE, in case of success it return TRUE; 
518      */ 
519     function loadInstallationMethods()
520     { 
521         $rpc = $this->config->getRpcHandle();
522         $res = $rpc->getSupportedBaseInstallMethods();
523       
524         // Failed to load backend values, keep reported error and return with FALSE; 
525         $this->rpcError = !$rpc->success();
526         if(!$rpc->success()){
527             $this->rpcErrorMessage = $rpc->get_error();
528             return(NULL);
529         }
531         // Walk through result and set bootstrap methods.
532         foreach($res as $name => $method){
533             $res['installBootstrapMethodList'][$name] = $method['name'];
534             foreach($method['methods'] as $m){
535                 $res['installConfigManagementList'][$name][$m] = $m; 
536             }
537         }
538         return($res);
539     }
540     
542     static function plInfo()
543     {
544         return (array(
545                     "plShortName"   => _("Device"),
546                     "plDescription" => _("Registered device"),
547                     "plSelfModify"  => FALSE,
548                     "plDepends"     => array(),
549                     "plPriority"    => 1,
550                     "plSection"     => array("administration"),
551                     "plCategory"    => array("Device"),
552                     "plProvidedAcls" => array(
553                         "member" => _("Member"),
554                         "installTemplate" => _("Installation template"),
555                         "installRelease" => _("Installation release"),
557                         "installBootstrapMethod" => _("Bootstrap method"),
558                         "installConfigManagement" => _("Config management"),
559                         "installKeyboardlayout" => _("Keyboard layout"),
560                         "installSystemLocale" => _("System locale"),
561                         "installTimezone" => _("Timezone"),
562                         "installTimeUTC" => _("Time"),
563                         "installNTPServer" => _("NTP-Server"),
564                         "installMirror" => _("Mirror"),
565                         "installRootEnabled" => _("Root login enabled"),
566                         "installRootPasswordHash" => _("Root password hash"),
567                         "installKernelPackage" => _("Kernel package"),
568                         "installPartitionTable" => _("Partition table")
569                         )
570                         )
571                         );
572     }
574 ?>