Skip to content

Instantly share code, notes, and snippets.

@microweber
Created July 12, 2013 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save microweber/5984087 to your computer and use it in GitHub Desktop.
Save microweber/5984087 to your computer and use it in GitHub Desktop.
Cpanel auto make db
http://forums.cpanel.net/f42/auto-creation-mysql-db-postwwwacct-166494.html
#!/usr/bin/php -q
<?php
//set error handling so any warnings are logged
ini_set('error_log','/usr/local/cpanel/logs/error_log');
ini_set('display_errors',0);
//include basic xmlapi client class
include('/home/cpanelscripthelpers/xmlapi.php');
/**
* extend the basic xmlapi class
* add the method for getting args
*/
Class cpScriptsXmlApi extends XMLAPI
{
public $cliargs = array();
/**
* constructor
*
*@param array $scriptargs cli $argv that will be parsed
*@param string $host
*@param string $user
*@param string $password
*@return cpScriptsXmlApi
*/
public function __construct($scriptargs = array(), $host = null, $user = null, $password = null)
{
parent::__construct($host,$user,$password);
$this->cliargs = $this->argv2array($scriptargs);
return $this;
}
/**
* Simple method to store args into an array
*
*@params array $argv shell array to be parsed
*@return array
*/
public function argv2array ($argv)
{
$opts = array();
$argv0 = array_shift($argv);
while(count($argv)) {
$key = array_shift($argv);
$value = array_shift($argv);
$opts[$key] = $value;
}
return $opts;
}
/**
* Create a database
*
*@param string $user cpanel user to create db as
*@param string $dbname name for database
*/
public function createUserDb($user,$dbname)
{
$args = array($dbname);
return $this->api1_query($user,'Mysql','adddb',$args);
}
/**
* Create a db virtuser
*
*@param string $user cpanel user to create virtuser as
*@param string $virtusername name for db virtuser
*@param string $password password for new db virtuser
*/
public function createDbVirtuser($user,$virtusername,$password){
$args = array($virtusername,$password);
return $this->api1_query($user,'Mysql','adduser',$args);
}
/**
* Assign user privs
*
*@param string $user cpanel user to work on behalf of
*@param string $dbname name of database
*@param string $virtusername receiver of privs
*@param array $privs array of privileges to assign.
*/
public function assignUserPrivs($user,$dbname,$virtusername,$privs = array())
{
$privs = (empty($privs))? array('all'): $privs; //not the best, you can change the default if you wish
$priv_str = implode(',',$privs);
$args = array($dbname, $virtusername, $priv_str);
return $this->api1_query($user,'Mysql','adduserdb',$args);
}
}
//create our xmlapi object and set it's params
$xmlapi = new cpScriptsXmlApi($argv,'**.**.**.***');
$xmlapi->set_port('2087');
$xmlapi->set_password("root", "**********************");
//generic vars for automation//
// db resource names should be small, no more than 7 chars for sure
$dbname = 'Hmail'; // "a database"
$virtusername = 'Hadmin'; // "a user"
$privs = array('all'); //you probably what to look into what you need; 'all' is very liberal
//determine if db mapping is on
$prefixing = 1; //default, may not be explicitly defined in config
$config = file('/var/cpanel/cpanel.config');
foreach($config as $key=>$value){
if(stripos($value,'database_prefix=') === 0){
$prefixing = substr(trim($value),-1); // bool/int
}
}
// make a prefix that allows max length of primary database username
// not perfect; possible collision, but that would happen even in non-automated
// db resource creation if usernames are long (cpanel softlimits to 8 char, so the point should be moot)
// NOTE: db users have a MySQL hardlimit of 16 char, our generic user name are 4 char 16 - 4 - 1 for userscore = 11 usable
$dbprefix = (strlen($xmlapi->cliargs['user']) > 11)? substr($xmlapi->cliargs['user'], 0,10) : $xmlapi->cliargs['user'];
if( (int)$prefixing === 0 ){
// prefixing is off
// double check the primary username. any prefix should be based on that name
if($xmlapi->cliargs['user'] != $xmlapi->cliargs['dbuser']){
$dbprefix = (strlen($xmlapi->cliargs['dbuser']) > 11)? substr($xmlapi->cliargs['dbuser'], 0,10) : $xmlapi->cliargs['dbuser'];
}
//since prefixing is off, we should use literal names in both creation and assignment
$xmlapi->createUserDb($xmlapi->cliargs['user'], $dbprefix.'_'.$dbname);
$xmlapi->createDbVirtuser($xmlapi->cliargs['user'],$dbprefix.'_'.$virtusername, $xmlapi->cliargs['pass']); //setting passwd same is not wise, but is done per commission request
$xmlapi->assignUserPrivs($xmlapi->cliargs['user'], $dbprefix.'_'.$dbname, $dbprefix.'_'.$virtusername, $privs);
$xmlapi->assignUserPrivs($xmlapi->cliargs['user'], $dbprefix.'_'.$dbname, $xmlapi->cliargs['dbuser'], $privs);
echo "Auto-generated database '".$dbprefix.'_'.$dbname."' for database user '".$dbprefix.'_'.$virtusername."'.\n";
}else{
// prefixing is ON, default on all cpanel systems
// prefix only on assignment
$xmlapi->createUserDb($xmlapi->cliargs['user'], $dbname);
$xmlapi->createDbVirtuser($xmlapi->cliargs['user'],$virtusername, $xmlapi->cliargs['pass']); //setting passwd same is not wise, but is done per commission request
$xmlapi->assignUserPrivs($xmlapi->cliargs['user'], $dbprefix.'_'.$dbname, $dbprefix.'_'.$virtusername, $privs);
echo "Auto-generated database '".$dbprefix.'_'.$dbname."' for database user '".$dbprefix.'_'.$virtusername."'.\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment