Changeset 11948

Show
Ignore:
Timestamp:
08/31/07 12:46:34 (1 year ago)
Author:
piotras
Message:

Added set_admin_name method.
create_database method creates db and grant privileges in one step

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/midgard/data/midgard_setup_database.php

    r11772 r11948  
    2222    private $config_name; 
    2323    private $connected; 
     24    private $mysql_admin_name; 
     25    private $mysql_admin_password; 
    2426 
    2527    /** 
     
    9799        $midgard_dir = MIDGARD_SETUP_DIRECTORY_VAR."/lib/midgard"; 
    98100        if(!is_dir($midgard_dir)) 
    99        
    100                    if(!mkdir($midgard_dir, 0711, TRUE)) 
    101                
     101       
     102            if(!mkdir($midgard_dir, 0711, TRUE)) 
     103           
    102104                umask($old_umask); 
    103                        return self::warn_ifnotdir($midgard_dir); 
    104                    } 
    105            } 
     105                return self::warn_ifnotdir($midgard_dir); 
     106            } 
     107        } 
    106108 
    107109        /* check root directory for all blobs */ 
    108            $blobdir_root = $midgard_dir."/blobs"; 
    109            if(!is_dir($blobdir_root)) 
    110        
    111                    if(!mkdir($blobdir_root, 0711, TRUE)) 
    112                
     110        $blobdir_root = $midgard_dir."/blobs"; 
     111        if(!is_dir($blobdir_root)) 
     112       
     113            if(!mkdir($blobdir_root, 0711, TRUE)) 
     114           
    113115                umask($old_umask); 
    114                        return self::warn_ifnotdir($blobdir_root); 
    115                    } 
    116            } 
     116                return self::warn_ifnotdir($blobdir_root); 
     117            } 
     118        } 
    117119 
    118120        $blobdir_db = $blobdir_root."/".$this->midgard_config->database; 
     
    179181    } 
    180182 
     183    final public function set_admin_name($name) 
     184    { 
     185        $this->mysql_admin_name = $name; 
     186    } 
     187 
    181188    final public function create_database() 
    182189    { 
    183          
    184         $value = midgard_setup_cli::get_text(_("What is MySQL admin username?")); 
    185  
    186         if(!$value && $value == '') 
    187             $value = "root"; 
    188  
    189             midgard_setup_cli::message( 
    190             _("Preparing to create database. You will be asked for password if needed.")); 
    191  
     190        if(!isset($this->mysql_admin_name)) 
     191        { 
     192            $value = midgard_setup_cli::get_text(_("What is MySQL admin username?"), "root"); 
     193             
     194            if(!$value && $value == '') 
     195                $value = "root"; 
     196                 
     197            midgard_setup_cli::message( 
     198                _("Preparing to create database. You will be asked for password if needed.")); 
     199         
     200        } else { 
     201             
     202            $value = $this->mysql_admin_name; 
     203            midgard_setup_cli::message(_("Preparing to create database with default admin account ") . "'{$value}'"); 
     204        } 
     205 
     206        /* Try to create database and grant privileges in one run. 
     207         * This way we avoid asking for MySQL's admin password twice */ 
    192208        $m = MIDGARD_SETUP_MYSQL_CMD; 
    193209        $m .= " --host {$this->midgard_config->host}"; 
    194         $m .= " --user {$value} -p "; 
    195         $m .= " -e \"CREATE DATABASE {$this->midgard_config->database} CHARACTER SET utf8\""; 
    196  
    197             $output = 0; 
     210        $m .= " --user {$value} -p "; 
     211         
     212        /* Create database */ 
     213        $m .= " -e \"CREATE DATABASE {$this->midgard_config->database} CHARACTER SET utf8;"; 
     214         
     215        /* Grant all privileges */ 
     216        $m .= " GRANT all ON {$this->midgard_config->database}.* "; 
     217        $m .= " to '{$this->midgard_config->dbuser}'@'{$this->midgard_config->host}'"; 
     218        $m .= " identified by '{$this->midgard_config->dbpass}';"; 
     219 
     220        /* flush privileges */ 
     221        $m .= " FLUSH PRIVILEGES \""; 
     222 
     223        $output = 0; 
    198224        $retval = array(); 
    199225         
    200            exec($m, &$output, &$retval); 
     226        exec($m, &$output, &$retval); 
    201227 
    202228        unset($m); 
     
    204230        if($retval != 0)  
    205231        { 
    206                 midgard_setup_cli::error(_("Couldn't create {$this->midgard_config->database} database")); 
    207  
    208         } 
    209  
    210         /* Database is created, let's create midgard user */ 
    211          
    212         unset($output); 
    213         $output = array(); 
    214         unset($retval); 
    215         midgard_setup_cli::message(_("Preparing to create database user. You will be asked for password if needed.")); 
    216  
    217         $m = MIDGARD_SETUP_MYSQL_CMD; 
    218         $m .= " --host {$this->midgard_config->host}"; 
    219         $m .= " --user {$value} -p "; 
    220             $m .= " -e \"GRANT all ON {$this->midgard_config->database}.* "; 
    221         $m .= " to '{$this->midgard_config->dbuser}'@'{$this->midgard_config->host}'"; 
    222         $m .= " identified by '{$this->midgard_config->dbpass}'"; 
    223         $m .= "; FLUSH PRIVILEGES \""; 
    224      
    225         exec($m, &$output, &$retval); 
    226      
    227         if($retval != 0) 
    228         { 
    229             midgard_setup_cli::error(_("Couldn't create user for {$this->midgard_config->database} database")); 
    230         } 
    231  
     232            midgard_setup_cli::error(_("Couldn't create {$this->midgard_config->database} database")); 
     233        } 
     234 
     235        /* Connect to midgard database */       
    232236        $_cnf_name = ""; 
    233237        $this->config_name == NULL  
     
    250254         
    251255        /* I am not sure how to handle errors here */ 
     256        $msg = _("Creating database ") . $this->midgard_config->database . _(", please wait..."); 
     257        midgard_setup_cli::message($msg); 
    252258        self::update_database(); 
     259        midgard_setup_cli::message(_("Successfully created database ") . $this->midgard_config->database); 
    253260    } 
    254261