Code

Backport from trunk
[gosa.git] / gosa-plugins / goto / admin / systems / goto / Device / class_InstallRecipe.inc
index b8edebc7f7558d0c5e91c9a95fc8e3cb3cd9449d..f7916dc73335a2193e0d98354ead46975adb786a 100644 (file)
 class InstallRecipe extends plugin
 {
 
-    public $__missingBoostrap = 'debian';
-    public $__missingBoostrapList = array('weißnicht' => 'Irgendwas','debian' => 'debian');
-
-    public $__missingConfigManagement = 'puppet';
-    public $__missingConfigManagementList = array('fai' => 'FAI','puppet' => 'Puppet');
-
-    public $kickstartTemplateDN = "";
-    public $kickstartTemplateDNList = array(
-        'dc=intranet,dc=gonicus,dc=de' => 'Intranet-Tpl',
-        'ou=systems,dc=intranet,dc=gonicus,dc=de' => 'System-Tpl');
-
-    public $kickstartMirrorDN = "";
-    public $kickstartMirrorDNList = array(
-        'dc=intranet,dc=gonicus,dc=de' => 'Intranet',
-        'ou=systems,dc=intranet,dc=gonicus,dc=de' => 'System');
-    public $kickstartKernelPackage = "";
-    public $kickstartKernelPackageList = array('kernel1','kk1');
-    public $kickstartKeyboardlayout = "";
-    public $kickstartKeyboardlayoutList = array("104","105");
-    public $kickstartSystemLocale = "";
-    public $kickstartSystemLocaleList = array('de','en_EN.UTF-8');
-    public $kickstartTimezone = "";
-    public $kickstartTimeUTC = "";
-    public $kickstartNTPServer = array();
-    public $kickstartRootEnabled = "";
-    public $kickstartRootPasswordHash = "";
-    public $kickstartPartitionTable = "";
+    /* This has to be replaced by valid attributes, currently they are just 
+     *  fake variables. #FIXME: I do not know the real attribute names yet.
+     */
+    public $installBootstrapMethod = '';
+    public $installBootstrapMethodList = NULL;
 
-    public $objectclasses = array('installRecipe');
-    public $attributes = array("kickstartTemplateDN","kickstartKeyboardlayout","kickstartSystemLocale",
-            "kickstartTimezone","kickstartTimeUTC","kickstartNTPServer","kickstartMirrorDN",
-            "kickstartRootEnabled","kickstartRootPasswordHash","kickstartKernelPackage","kickstartPartitionTable");
+    public $installConfigManagement = '';
+    public $installConfigManagementList = NULL;
+
+    /* Plugin attributes
+     */
+    public $installTemplateDN = "";
+    public $installTemplateDNList = array();
+
+    public $installMirrorDN = "";
+    public $installMirrorDNList = array();
+
+    public $installKernelPackage = "";
+    public $installKernelPackageList = array();
+
+    public $installKeyboardlayout = "";
+    public $installKeyboardlayoutList = array();
+
+    public $installSystemLocale = "";
+    public $installSystemLocaleList = array();
+
+    public $installTimezone = "";
+    public $installTimeUTC = "";
+
+    public $installNTPServer = array();
+    public $installRootEnabled = "";
+    public $installRootPasswordHash = "";
 
     public $setKickstartRootPasswordHash = FALSE;
+    public $installPartitionTable = "";
+    private $installNTPServerList = NULL;
+
+    public $objectclasses = array('installRecipe');
+    public $attributes = array("installTemplateDN","installKeyboardlayout","installSystemLocale",
+            "installTimezone","installTimeUTC","installNTPServer","installMirrorDN",
+            "installRootEnabled","installRootPasswordHash","installKernelPackage",
+            "installPartitionTable","installConfigManagement","installBootstrapMethod");
+
+    private $map = array('puppet' => array('objectClass' => 'puppetClient', 'type' => 'puppet', 'bootstrap' => 'preseed'));
+
+    /* Some plugin management related attributes 
+     * See class plugin for details.
+     */
+    public $view_logged = FALSE;
+    public $ignore_account = FALSE;
 
-    private $kickstartNTPServerList = NULL;
 
+    /*! \brief  Initialize the plugin and its widgets. 
+     *          Finally call init() to load values from the backend.
+     */ 
     function __construct(&$config, $dn)
     {
         plugin::plugin($config, $dn);
 
+        // Set default password hash
+        $this->hash = $this->config->get_cfg_value("core","passwordDefaultHash"); 
+
+        // Prepare list of timezones
+        $this->timezones = $this->getTimezones();
+
+        // Get list of password hashes
+        $tmp = passwordMethod::get_available_methods();
+        $this->hashes = array();
+        foreach($tmp['name'] as $name){
+            $this->hashes[$name] = $name;
+        }
+
+        // Prepare NTP servers list 
+        $this->installNTPServerList= new sortableListing($this->installNTPServer);
+        $this->installNTPServerList->setDeleteable(true);
+        $this->installNTPServerList->setEditable(false);
+        $this->installNTPServerList->setColspecs(array('*'));
+        $this->installNTPServerList->setWidth("100%");
+        $this->installNTPServerList->setHeight("70px");
+
+        // Init the object values.
+        $this->init();
+    }
+
+   
+    /*! \brief   Load attribute values from the GOsa-backend using jsonRPC 
+     */ 
+    function init()
+    {
+        // Detect account status by checking the used objectClasses
+        $this->is_account = FALSE;
+        foreach($this->map as $type){
+            if(in_array_strict($type['objectClass'], $this->attrs['objectClass'])){
+                $this->installBootstrapMethod = $type['bootstrap'];
+                $this->installConfigManagement = $type['type'];
+                $this->is_account = TRUE;
+            }
+        }
+
+        // Start without error.
+        $this->initFailed = FALSE;
+
+        // Get list of NTP servers
+        $this->installNTPServer = array();
+        if(isset($this->attrs['installNTPServer']['count'])){
+            for($i = 0; $i < $this->attrs['installNTPServer']['count']; $i++){
+                $this->installNTPServer[] = $this->attrs['installNTPServer'][$i];
+            }
+        }
+
+        // Load list of bootstrap methods.
+        $res = $this->loadInstallationMethods();
+        if($res == NULL){
+            $this->initFailed = TRUE;
+            msg_dialog(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
+            return;
+        }
+        $this->installBootstrapMethodList = $res['installBootstrapMethodList'];
+        $this->installConfigManagementList = $res['installConfigManagementList'];
+
+
+        // Load system locales 
+        $locales = $this->getSystemLocales();
+        if($locales == NULL){
+            $this->initFailed = TRUE;
+            msg_dialog(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
+            return;
+        }
+        $this->installSystemLocaleList = $locales;
+
+
+        // Load installation templates
+        $res = $this->loadInstallationTemplates();
+        if($res == NULL){
+            $this->initFailed = TRUE;
+            msg_dialog(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
+            return;
+        }
+        $this->installTemplateDNList = $res;
+
+
+        // Load installation keyboard layouts
+        $res = $this->loadInstallationKeyboardLayouts();
+        if($res == NULL){
+            $this->initFailed = TRUE;
+            msg_dialog(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
+            return;
+        }
+        $this->installKeyboardlayoutList = $res;
+
+
+        // Load kernel package list
+        $res = $this->loadInstallationKernelPackages();
+        if($res == NULL){
+            $this->initFailed = TRUE;
+            msg_dialog(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
+            return;
+        }
+        $this->installKernelPackageList = $res;
+
+
+        // Load installation mirrors
+        $res = $this->loadInstallationMirrors();
+        if($res == NULL){
+            $this->initFailed = TRUE;
+            msg_dialog(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
+            return;
+        }
+        $this->installMirrorDNList = $res;
+
+
         // Preset some values for new accounts
         if(!$this->is_account){
 
             // Preset the device timezone
             $tz = timezone::get_default_timezone();
-            $this->kickstartTimezone = $tz['name'];
+            $this->installTimezone = $tz['name'];
+
+            // Set initial bootstrap and install method.
+            $this->installBootstrapMethod = key($this->installBootstrapMethodList);
+            $this->installConfigManagement = key($this->installConfigManagementList[$this->installBootstrapMethod]);
         }
 
-        // Prepare list of timezones
+
+        // Check if we've a valid bootstrap and configMangement method selected
+        if(!isset($this->installConfigManagementList[$this->installBootstrapMethod])){
+            $newMethod = key($this->installBootstrapMethodList);
+            msg_dialog::display(_("Error"), 
+                sprintf(_("The selected bootstrap method %s is invalid. Using %s instead!"),
+                bold($this->installBootstrapMethod), bold($newMethod)), ERROR_DIALOG);
+            $this->installBootstrapMethod = $newMethod;
+        }
+        if(!isset($this->installConfigManagementList[$this->installBootstrapMethod][$this->installConfigManagement])){
+            $newMethod = key($this->installConfigManagementList[$this->installBootstrapMethod]);
+            msg_dialog::display(_("Error"), 
+                sprintf(_("The selected config management method %s is invalid. Using %s instead!"),
+                bold($this->installConfigManagement),bold($newMethod)), ERROR_DIALOG);
+            $this->installConfigManagement = $newMethod;
+        }
+    }
+
+
+    function getTimezones()
+    {
         $tmp = timezone::_get_tz_zones();
         $list = array();
         foreach($tmp['TIMEZONES'] as $name => $offset){
@@ -64,54 +218,112 @@ class InstallRecipe extends plugin
             }
         }  
         uksort($list, 'strnatcasecmp'); 
-        $this->timezones = $list;
+        return($list);
+    }
+    
+    function loadInstallationKernelPackages()
+    {
+        return(array('kernel1','kk1'));
+    }
+    
 
-        // Get list of password hashes
-        $tmp = passwordMethod::get_available_methods();
-        $this->hashes = array();
-        foreach($tmp['name'] as $name){
-            $this->hashes[$name] = $name;
-        }
-        $this->hash = $this->config->get_cfg_value("core","passwordDefaultHash"); 
+    function loadInstallationMirrors()
+    {
+        $res = array(
+                'dc=intranet,dc=gonicus,dc=de' => 'Intranet',
+                'ou=systems,dc=intranet,dc=gonicus,dc=de' => 'System');
+        return($res);
+    }
 
-        // Get list of NTP servers
-        $this->kickstartNTPServer = array();
-        if(isset($this->attrs['kickstartNTPServer']['count'])){
-            for($i = 0; $i < $this->attrs['kickstartNTPServer']['count']; $i++){
-                $this->kickstartNTPServer[] = $this->attrs['kickstartNTPServer'][$i];
-            }
-        }
+    function loadInstallationTemplates()
+    {
+        $res = array(
+                'dc=intranet,dc=gonicus,dc=de' => 'Intranet-Tpl',
+                'ou=systems,dc=intranet,dc=gonicus,dc=de' => 'System-Tpl');
+        return($res);
+    } 
 
-        // Prepare NTP servers list 
-        $this->kickstartNTPServerList= new sortableListing($this->kickstartNTPServer);
-        $this->kickstartNTPServerList->setDeleteable(true);
-        $this->kickstartNTPServerList->setEditable(false);
-        $this->kickstartNTPServerList->setColspecs(array('*'));
-        $this->kickstartNTPServerList->setWidth("100%");
-        $this->kickstartNTPServerList->setHeight("70px");
+    function loadInstallationKeyboardLayouts()
+    {
+        return(array('104','105'));
+    } 
 
-        // Load list of bootstrap methods.
-        $this->__missingBoostrapList = $this->getBootstrapMethods();
+    function getSystemLocales()
+    {
+        $locales = json_decode('{"hu_HU.UTF-8": "Hungarian", 
+                "es_DO.UTF-8": "Spanish (Dominican Republic)", 
+                "es_CL.UTF-8": "Spanish (Chile)"}');
+
+        return($locales);
     }
+
+    /*! \brief  Try to load install and bootstrap methods from the GOsa-backend
+     *          This is done via jsonRPC, if calling the backend method fails,
+     *           this method returns FALSE, in case of success it return TRUE; 
+     */ 
+    function loadInstallationMethods()
+    { 
+        $rpc = $this->config->getRpcHandle();
+        $res = $rpc->getSupportedBaseInstallMethods();
+       
+        // Failed to load backend values, keep reported error and return with FALSE; 
+        $this->rpcError = !$rpc->success();
+        if(!$rpc->success()){
+            $this->rpcErrorMessage = $rpc->get_error();
+            return(NULL);
+        }
 
-    
-    function getBootstrapMethods()
-    {
-        $str = file_get_contents('/home/hickert/bootstrap.json');
-        $items = json_decode($str, TRUE);
-        if(is_array($items)){
-            $ret = array();
-            foreach($items as $bst){
-                $ret[$bst['name']]= $bst['title'];
+        // Walk through result and set bootstrap methods.
+        foreach($res as $name => $method){
+            $res['installBootstrapMethodList'][$name] = $method['name'];
+            foreach($method['methods'] as $m){
+                $res['installConfigManagementList'][$name][$m] = $m; 
             }
-            return($ret);
         }
-        return(NULL);
+        return($res);
     }
-
+    
 
     function execute()
     {
+        // Log account access
+        if($this->is_account && !$this->view_logged){
+            $this->view_logged = TRUE;
+            new log("view","Device/".get_class($this),$this->dn);
+        }
+
+        /***************
+          Handle account state
+         ***************/
+
+        // Allow to add or remove the distribution list extension
+        if(isset($_POST['modify_state'])){
+            if($this->is_account && $this->acl_is_removeable()){
+                $this->is_account= FALSE;
+            }elseif(!$this->is_account && $this->acl_is_createable()){
+                $this->is_account= TRUE;
+            }
+        }
+
+        // Show account status-changer
+        $display = "";
+        if ($this->parent !== NULL){
+            if ($this->is_account){
+                $display= $this->show_disable_header(_("Remove install profile"),
+                        msgPool::featuresEnabled(_("Install profile")));
+            } else {
+                $display= $this->show_enable_header(_("Add install profile"),
+                        msgPool::featuresDisabled(_("Install profile")));
+                return ($display);
+            }
+        }
+
+        /***************
+          Root password hash dialog
+         ***************/
+
         if($this->setKickstartRootPasswordHash){
             $this->dialog = TRUE;
             $smarty = get_smarty();
@@ -120,52 +332,60 @@ class InstallRecipe extends plugin
             return($smarty->fetch(get_template_path('goto/Device/SetPassword.tpl', TRUE)));
         }
 
-        $this->kickstartNTPServerList->setAcl($this->getacl('kickstartNTPServer'));
-        $this->kickstartNTPServerList->update();
+        /***************
+          Generate HTML content
+         ***************/
+
+        $this->installNTPServerList->setAcl($this->getacl('installNTPServer'));
+        $this->installNTPServerList->update();
 
         plugin::execute();    
         $smarty = get_smarty();
         $smarty->assign('timezones', $this->timezones);
-        $smarty->assign('kickstartKeyboardlayoutList', $this->kickstartKeyboardlayoutList);
-        $smarty->assign('kickstartKernelPackageList', $this->kickstartKernelPackageList);
-        $smarty->assign('kickstartTemplateDNList', $this->kickstartTemplateDNList);
-        $smarty->assign('kickstartMirrorDNList', $this->kickstartMirrorDNList);
-        $smarty->assign('kickstartSystemLocaleList', $this->kickstartSystemLocaleList);
-        $smarty->assign('kickstartNTPServerList', $this->kickstartNTPServerList->render());
-
-        $smarty->assign('__missingBoostrap', $this->__missingBoostrap);
-        $smarty->assign('__missingBoostrapList', $this->__missingBoostrapList);
-        $smarty->assign('__missingConfigManagement', $this->__missingConfigManagement);
-        $smarty->assign('__missingConfigManagementList', $this->__missingConfigManagementList);
+        $smarty->assign('installKeyboardlayoutList', $this->installKeyboardlayoutList);
+        $smarty->assign('installKernelPackageList', $this->installKernelPackageList);
+        $smarty->assign('installTemplateDNList', $this->installTemplateDNList);
+        $smarty->assign('installMirrorDNList', $this->installMirrorDNList);
+        $smarty->assign('installSystemLocaleList', $this->installSystemLocaleList);
+        $smarty->assign('installNTPServerList', $this->installNTPServerList->render());
+
+        $smarty->assign('installBootstrapMethod', $this->installBootstrapMethod);
+        $smarty->assign('installBootstrapMethodList', $this->installBootstrapMethodList);
+        $smarty->assign('installConfigManagement', $this->installConfigManagement);
+
+        $cfgList = $this->installConfigManagementList[$this->installBootstrapMethod];
+        $smarty->assign('installConfigManagementList', $cfgList);
         foreach($this->attributes as $attr){
             $smarty->assign($attr, $this->$attr);
         }
         $this->dialog = false;
-        return($smarty->fetch(get_template_path('goto/Device/InstallRecipe.tpl', TRUE)));
+        return($display.$smarty->fetch(get_template_path('goto/Device/InstallRecipe.tpl', TRUE)));
     }
 
 
     function save_object()
     {
         if(isset($_POST['InstallRecipePosted'])){
+
+            $currentInstallMethod = $this->installConfigManagement;
             plugin::save_object();
-            $this->kickstartRootEnabled = isset($_POST['kickstartRootEnabled']);
-            $this->kickstartTimeUTC = isset($_POST['kickstartTimeUTC']);
+            $this->installRootEnabled = isset($_POST['installRootEnabled']);
+            $this->installTimeUTC = isset($_POST['installTimeUTC']);
 
 
             if(isset($_POST['setKickstartRootPasswordHash'])){
                 $this->setKickstartRootPasswordHash = true;
             }
 
-            $this->kickstartNTPServerList->save_object();
-            $this->kickstartNTPServer = $this->kickstartNTPServerList->getMaintainedData();
-            if(isset($_POST['kickstartNTPServer_Input']) && isset($_POST['kickstartNTPServer_Add'])){
-                $add = get_post('kickstartNTPServer_Input');
-                if(!in_array($add, $this->kickstartNTPServer) && !empty($add)){
-                    $this->kickstartNTPServer[] = $add;
+            $this->installNTPServerList->save_object();
+            $this->installNTPServer = $this->installNTPServerList->getMaintainedData();
+            if(isset($_POST['installNTPServer_Input']) && isset($_POST['installNTPServer_Add'])){
+                $add = get_post('installNTPServer_Input');
+                if(!in_array_strict($add, $this->installNTPServer) && !empty($add)){
+                    $this->installNTPServer[] = $add;
                 }
             }
-            $this->kickstartNTPServerList->setListData($this->kickstartNTPServer);
+            $this->installNTPServerList->setListData($this->installNTPServer);
         }
         if(isset($_POST['cancelPassword'])) $this->setKickstartRootPasswordHash =false;
         if(isset($_POST['setPassword'])) {
@@ -179,21 +399,34 @@ class InstallRecipe extends plugin
             $methods = new passwordMethod($this->config, $this->dn);
             $available = $methods->get_available_methods();
             $test = new $available[$hash]($this->config,$this->dn);
-            $this->kickstartRootPasswordHash = @$test->generate_hash(get_post('rootPassword'));
+            $this->installRootPasswordHash = @$test->generate_hash(get_post('rootPassword'));
         }
+
     }
 
 
     function save()
     {
         // 
-        if(!$this->kickstartRootEnabled) $this->kickstartRootPasswordHash = "";
-        $this->kickstartRootEnabled = ($this->kickstartRootEnabled)?'TRUE':'FALSE';
-        $this->kickstartTimeUTC = ($this->kickstartTimeUTC)?'TRUE':'FALSE';
+        if(!$this->installRootEnabled) $this->installRootPasswordHash = "";
+        $this->installRootEnabled = ($this->installRootEnabled)?'TRUE':'FALSE';
+        $this->installTimeUTC = ($this->installTimeUTC)?'TRUE':'FALSE';
         
-
         plugin::save();
 
+        unset($this->attrs['installConfigManagement']);
+        unset($this->attrs['installBootstrapMethod']);
+        echo "Skipp saving of 'installConfigManagement' and 'installBootstrapMethod', i've no idea where to store them.";
+
+        // Remove all objectClasses which belongs to a bootstrap method, 
+        //  and then add the selected one.
+        foreach($this->map as $type){
+            $this->attrs['objectClass'] = array_remove_entries_ics(array($type['objectClass']), $this->attrs['objectClass']);
+        }
+        $this->attrs['objectClass'][] = $this->map[$this->installConfigManagement]['objectClass'];
+        
+        print_a($this->attrs);
+
         $this->cleanup();
         $ldap=$this->config->get_ldap_link();
         $ldap->cd($this->config->current['BASE']);
@@ -207,6 +440,28 @@ class InstallRecipe extends plugin
         }
     }
 
+    
+    function remove_from_parent()
+    {
+        plugin::remove_from_parent();
+        unset($this->attrs['installConfigManagement']);
+        unset($this->attrs['installBootstrapMethod']);
+        echo "Skipp saving of 'installConfigManagement' and 'installBootstrapMethod', i've no idea where to store them.";
+
+        $ldap=$this->config->get_ldap_link();
+        $ldap->cd($this->config->current['BASE']);
+        $ldap->cd($this->dn);
+        $ldap->modify($this->attrs);
+        new log("remove","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
+        if (!$ldap->success()){
+            msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
+        }else{
+            $this->handle_post_events("remove");
+        }
+        
+    }
+    
+
     static function plInfo()
     {
         return (array(
@@ -219,17 +474,19 @@ class InstallRecipe extends plugin
                     "plCategory"    => array("Device"),
                     "plProvidedAcls" => array(
                         "member" => _("Member"),
-                        "kickstartTemplateDN" => _("Template"),
-                        "kickstartKeyboardlayout" => _("Keyboard layout"),
-                        "kickstartSystemLocale" => _("System locale"),
-                        "kickstartTimezone" => _("Timezone"),
-                        "kickstartTimeUTC" => _("Time"),
-                        "kickstartNTPServer" => _("NTP-Server"),
-                        "kickstartMirrorDN" => _("Kickstart mirror"),
-                        "kickstartRootEnabled" => _("Root login enabled"),
-                        "kickstartRootPasswordHash" => _("Root password hash"),
-                        "kickstartKernelPackage" => _("Kernal package"),
-                        "kickstartPartitionTable" => _("Partition table")
+                        "installTemplateDN" => _("Template"),
+                        "installBootstrapMethod" => _("Bootstrap method"),
+                        "installConfigManagement" => _("Config management"),
+                        "installKeyboardlayout" => _("Keyboard layout"),
+                        "installSystemLocale" => _("System locale"),
+                        "installTimezone" => _("Timezone"),
+                        "installTimeUTC" => _("Time"),
+                        "installNTPServer" => _("NTP-Server"),
+                        "installMirrorDN" => _("Kickstart mirror"),
+                        "installRootEnabled" => _("Root login enabled"),
+                        "installRootPasswordHash" => _("Root password hash"),
+                        "installKernelPackage" => _("Kernel package"),
+                        "installPartitionTable" => _("Partition table")
                         )
                         )
                         );