Skip to content

Commit

Permalink
- remove database instance
Browse files Browse the repository at this point in the history
  • Loading branch information
xrow committed Aug 23, 2017
1 parent 22bc25c commit f85bb85
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 48 deletions.
37 changes: 8 additions & 29 deletions src/xrow/eZCluster/ClusterNode.php
Expand Up @@ -8,8 +8,6 @@
use xrow\eZCluster\Resources\instance;
use xrow\eZCluster\Resources;
use Ssh;
use \ezcDbFactory;
use \ezcDbInstance;
use xrow\eZCluster\Resources\environment;

class ClusterNode extends Resources\instance
Expand Down Expand Up @@ -64,35 +62,16 @@ function __construct($id = null)
}
public function setupDatabase()
{
$xp = "/aws/cluster/instance[role = 'database']";

$xp = "/aws/cluster/environment/database[@dsn]";
$result = self::$config->xpath($xp);
if (is_array($result) and count($result) > 0) {
$masterdsn = 'mysql://root@localhost';
$masterdetails = ezcDbFactory::parseDSN($masterdsn);
}
if (! isset($masterdetails)) {
return false;
}
try {
$dbmaster = ezcDbFactory::create($masterdetails);
} catch (\Exception $e) {
return false;
}
ezcDbInstance::set($dbmaster);

if ($dbmaster) {
$xp = "/aws/cluster/environment/database[@dsn]";
$result = self::$config->xpath($xp);

foreach ($result as $db) {
db::initDB((string) $db['dsn'], $dbmaster);
}
$xp = "/aws/cluster/environment/storage[@dsn]";
$result = self::$config->xpath($xp);
foreach ($result as $db) {
db::initDB((string) $db['dsn'], $dbmaster);
}
foreach ($result as $db) {
db::initDB((string) $db['dsn'] );
}
$xp = "/aws/cluster/environment/storage[@dsn]";
$result = self::$config->xpath($xp);
foreach ($result as $db) {
db::initDB((string) $db['dsn'] );
}
}

Expand Down
54 changes: 35 additions & 19 deletions src/xrow/eZCluster/Resources/db.php
Expand Up @@ -4,8 +4,9 @@
use xrow\eZCluster\Resources;
use xrow\eZCluster\Abstracts;
use xrow\eZCluster;
use \ezcDbFactory;
use Ssh;
use \ezcDbFactory;
use \ezcDbInstance;

class db extends Abstracts\xrowEC2Resource
{
Expand Down Expand Up @@ -107,9 +108,16 @@ static public function migrateDatabase($dsnsource, $dsntarget, $session, $where
}
}

public static function initDB($dsn, $dbmaster)
public static function initDB($dsn)
{

$masterdetails = ezcDbFactory::parseDSN($dsn);
try {
$dbmaster = ezcDbFactory::create($masterdetails);
} catch (\Exception $e) {
return false;
}
ezcDbInstance::set($dbmaster);

$dbmaster->query("SET NAMES utf8");
$rows = $dbmaster->query('SHOW DATABASES');
$dbs_exists = array();
Expand All @@ -128,10 +136,7 @@ public static function initDB($dsn, $dbmaster)
$GLOBALS["database"]["users"][$dbdetails['username']] = $dbdetails['password'];
}
if( $GLOBALS["database"]["users"][$dbdetails['username']] != $dbdetails['password'] ){
throw new \Exception( "Database user " . $dbdetails['username'] . " with different password found." );
}
if (! in_array($dbdetails['database'], $dbs_exists)) {
$dbmaster->query('CREATE DATABASE IF NOT EXISTS ' . $dbdetails['database'] . ' CHARACTER SET utf8 COLLATE utf8_general_ci');
throw new \Exception( "Database user " . $dbdetails['username'] . " with different password found." );
}

//test if user has access else grant
Expand All @@ -141,23 +146,34 @@ public static function initDB($dsn, $dbmaster)
$grants = false;
}
if ( is_object( $grants ) and $grants->rowCount() > 0 ){
foreach( $grants->fetchAll() as $grant ){
$match = false;
if ( isset( $grant['grants for ' . $dbdetails['username'] . '@%'] ) and $grant['grants for ' . $dbdetails['username'] . '@%'] == "GRANT ALL PRIVILIEGES ON `" . $dbdetails['database'] . "`.* TO " . $dbdetails['username'] . "@'%'" ){
$match = true;
}
$grants = false;
}
if (!$match){
$grants = false;
}
foreach( $grants->fetchAll() as $grant ){
$match = false;
if ( isset( $grant['grants for ' . $dbdetails['username'] . '@%'] ) and $grant['grants for ' . $dbdetails['username'] . '@%'] == "GRANT ALL PRIVILIEGES ON `" . $dbdetails['database'] . "`.* TO " . $dbdetails['username'] . "@'%'" ){
$match = true;
}
$grants = false;
}
if (!$match){
$grants = false;
}
}
if( $grants === false or ( is_object( $grants ) and $grants->rowCount() === 0 ) )
{
$rootdsn = 'mysql://root@localhost';
$rootdetails = ezcDbFactory::parseDSN($rootdsn);
try {
$dbroot = ezcDbFactory::create($rootdetails);
} catch (\Exception $e) {
return false;
}
ezcDbInstance::set($dbmaster);
$grant = 'GRANT ALL ON ' . $dbdetails['database'] . '.* TO ' . $dbdetails['username'] . "@'%' IDENTIFIED BY '" . $dbdetails['password'] . "'";
$dbmaster->query($grant);
$dbroot->query($grant);
$grant = 'GRANT ALL ON ' . $dbdetails['database'] . '.* TO ' . $dbdetails['username'] . "@'localhost' IDENTIFIED BY '" . $dbdetails['password'] . "'";
$dbmaster->query($grant);
$dbroot->query($grant);
}
if (! in_array($dbdetails['database'], $dbs_exists)) {
$dbmaster->query('CREATE DATABASE IF NOT EXISTS ' . $dbdetails['database'] . ' CHARACTER SET utf8 COLLATE utf8_general_ci');
}
// test DB
if ( $dbdetails['hostspec'] == "localhost" ){
Expand Down

0 comments on commit f85bb85

Please sign in to comment.