REL-0.10 Adding MongoDB

This commit is contained in:
cytopia 2017-05-22 23:20:20 +02:00
parent 0c643f3632
commit 05cdb4c3c1
No known key found for this signature in database
GPG Key ID: 6D56EDB8695128A2
8 changed files with 439 additions and 5 deletions

View File

@ -10,7 +10,7 @@ error_reporting(-1);
putenv('RES_OPTIONS=retrans:1 retry:1 timeout:1 attempts:1');
$DEVILBOX_VERSION = 'v0.9';
$DEVILBOX_VERSION = 'v0.10';
$DEVILBOX_DATE = '2017-05-20';
$DEVILBOX_API_PAGE = 'devilbox-api/status.json';
@ -41,6 +41,7 @@ $MYSQL_HOST_NAME = 'mysql';
$PGSQL_HOST_NAME = 'pgsql';
$REDIS_HOST_NAME = 'redis';
$MEMCD_HOST_NAME = 'memcd';
$MONGO_HOST_NAME = 'mongo';
//
@ -132,6 +133,11 @@ function loadClass($class) {
$_LOADED_LIBS[$class] = \devilbox\Memcd::getInstance($GLOBALS['MEMCD_HOST_NAME']);
break;
case 'Mongo':
loadFile($class, $cnt_dir);
$_LOADED_LIBS[$class] = \devilbox\Mongo::getInstance($GLOBALS['MONGO_HOST_NAME']);
break;
// Get optional docker classes
default:
// Redis

View File

@ -0,0 +1,50 @@
<?php require '../config.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php echo loadClass('Html')->getHead(); ?>
</head>
<body>
<?php echo loadClass('Html')->getNavbar(); ?>
<div class="container">
<h1>MongoDB Databases</h1>
<br/>
<br/>
<div class="row">
<div class="col-md-12">
<?php if (!loadClass('Mongo')->isAvailable()): ?>
<p>MongoDB container is not running.</p>
<?php else: ?>
<table class="table table-striped ">
<thead class="thead-inverse ">
<tr>
<th>Name</th>
<th>Size</th>
<th>Empty</th>
</th>
</thead>
<tbody>
<?php foreach (loadClass('Mongo')->getDatabases() as $db): ?>
<tr>
<td><?php echo $db['name'];?></td>
<td><?php echo round($db['size']/(1024*1024), 2);?> MB</td>
<td><?php echo $db['empty'];?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
</div><!-- /.container -->
<?php echo loadClass('Html')->getFooter(); ?>
</body>
</html>

View File

@ -18,6 +18,7 @@ $avail_mysql = loadClass('Mysql')->isAvailable();
$avail_pgsql = loadClass('Pgsql')->isAvailable();
$avail_redis = loadClass('Redis')->isAvailable();
$avail_memcd = loadClass('Memcd')->isAvailable();
$avail_mongo = loadClass('Mongo')->isAvailable();
/*************************************************************
@ -150,6 +151,32 @@ if ($avail_memcd) {
);
}
// ---- MONGO ----
if ($avail_mongo) {
$host = $GLOBALS['MONGO_HOST_NAME'];
$succ = loadClass('Mongo')->canConnect($error, $host);
$connection['MongoDB'][$host] = array(
'error' => $error,
'host' => $host,
'succ' => $succ
);
$host = loadClass('Mongo')->getIpAddress();
$succ = loadClass('Mongo')->canConnect($error, $host);
$connection['MongoDB'][$host] = array(
'error' => $error,
'host' => $host,
'succ' => $succ
);
$host = '127.0.0.1';
$succ = loadClass('Mongo')->canConnect($error, $host);
$connection['MongoDB'][$host] = array(
'error' => $error,
'host' => $host,
'succ' => $succ
);
}
// ---- BIND (required)----
$host = $GLOBALS['DNS_HOST_NAME'];
$succ = loadClass('Dns')->canConnect($error, $host);
@ -288,7 +315,7 @@ $HEALTH_PERCENT = 100 - ceil(100 * $HEALTH_FAILS / $HEALTH_TOTAL);
<?php echo loadClass('Html')->getCirle('memcd'); ?>
</div>
<div class="col-xl-4 col-lg-6 col-md-6 col-sm-12 col-xs-4" style="margin-bottom:15px;">
<?php echo loadClass('Html')->getCirle('mongodb'); ?>
<?php echo loadClass('Html')->getCirle('mongo'); ?>
</div>
</div>
</div>
@ -516,6 +543,13 @@ $HEALTH_PERCENT = 100 - ceil(100 * $HEALTH_FAILS / $HEALTH_TOTAL);
<td><?php echo loadClass('Memcd')->getIpAddress(); ?></td>
</tr>
<?php endif; ?>
<?php if ($avail_mongo): ?>
<tr>
<th>mongo</th>
<td><?php echo $GLOBALS['MONGO_HOST_NAME']; ?></td>
<td><?php echo loadClass('Mongo')->getIpAddress(); ?></td>
</tr>
<?php endif; ?>
<?php if ($avail_dns): ?>
<tr>
<th>bind</th>
@ -585,6 +619,13 @@ $HEALTH_PERCENT = 100 - ceil(100 * $HEALTH_FAILS / $HEALTH_TOTAL);
<td>11211</td>
</tr>
<?php endif; ?>
<?php if ($avail_mongo): ?>
<tr>
<th>mongo</th>
<td><?php echo loadClass('Helper')->getEnv('LOCAL_LISTEN_ADDR').loadClass('Helper')->getEnv('HOST_PORT_MONGO');?></td>
<td>27017</td>
</tr>
<?php endif; ?>
<?php if ($avail_dns): ?>
<tr>
<th>bind</th>
@ -656,6 +697,13 @@ $HEALTH_PERCENT = 100 - ceil(100 * $HEALTH_FAILS / $HEALTH_TOTAL);
<td>-</td>
</tr>
<?php endif; ?>
<?php if ($avail_mongo): ?>
<tr>
<th>mongo</th>
<td><?php echo loadClass('Helper')->getEnv('HOST_PATH_MONGO_DATADIR'); ?></td>
<td>/data/db</td>
</tr>
<?php endif; ?>
<?php if ($avail_dns): ?>
<tr>
<th>bind</th>
@ -724,6 +772,13 @@ $HEALTH_PERCENT = 100 - ceil(100 * $HEALTH_FAILS / $HEALTH_TOTAL);
<td>-</td>
</tr>
<?php endif; ?>
<?php if ($avail_mongo): ?>
<tr>
<th>mongo</th>
<td>-</td>
<td>-</td>
</tr>
<?php endif; ?>
<?php if ($avail_dns): ?>
<tr>
<th>bind</th>
@ -792,6 +847,13 @@ $HEALTH_PERCENT = 100 - ceil(100 * $HEALTH_FAILS / $HEALTH_TOTAL);
<td>/var/log/memcached</td>
</tr>
<?php endif; ?>
<?php if ($avail_mongo): ?>
<tr>
<th>mongo</th>
<td>-</td>
<td>-</td>
</tr>
<?php endif; ?>
<?php if ($avail_dns): ?>
<tr>
<th>bind</th>

View File

@ -0,0 +1,48 @@
<?php require '../config.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php echo loadClass('Html')->getHead(); ?>
</head>
<body>
<?php echo loadClass('Html')->getNavbar(); ?>
<div class="container">
<h1>MongoDB Info</h1>
<br/>
<br/>
<div class="row">
<div class="col-md-12">
<?php if (!loadClass('Mongo')->isAvailable()): ?>
<p>MongoDB container is not running.</p>
<?php else: ?>
<table class="table table-striped">
<thead class="thead-inverse">
<tr>
<th>Variable</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<?php foreach (loadClass('Mongo')->getInfo() as $key => $val): ?>
<tr>
<td><?php print_r($key);?></td>
<td class="break-word"><pre><?php print_r($val);?></pre></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
</div><!-- /.container -->
<?php echo loadClass('Html')->getFooter(); ?>
</body>
</html>

View File

@ -34,6 +34,10 @@ class Html
'name' => 'PgSQL DB',
'path' => '/db_pgsql.php'
),
array(
'name' => 'MongoDB DB',
'path' => '/db_mongo.php'
),
array(
'name' => 'Redis DB',
'path' => '/db_redis.php'
@ -63,6 +67,10 @@ class Html
'name' => 'PgSQL Info',
'path' => '/info_pgsql.php'
),
array(
'name' => 'MongoDB Info',
'path' => '/info_mongo.php'
),
array(
'name' => 'Redis Info',
'path' => '/info_redis.php'
@ -271,6 +279,12 @@ HTML;
$available = loadClass('Memcd')->isAvailable();
$name = loadClass('Memcd')->getName();
break;
case 'mongo':
$class = 'bg-danger';
$version = loadClass('Mongo')->getVersion();
$available = loadClass('Mongo')->isAvailable();
$name = loadClass('Mongo')->getName();
break;
default:
$available = false;
$version = '';

View File

@ -0,0 +1,224 @@
<?php
namespace devilbox;
/**
* @requires devilbox::Logger
*/
class Mongo extends BaseClass implements BaseInterface
{
/*********************************************************************************
*
* Private Variables
*
*********************************************************************************/
/**
* MongoDB manager instance
* @var object|null
*/
private $_mongo = null;
/*********************************************************************************
*
* Constructor Overwrite
*
*********************************************************************************/
/**
* Use singleton getInstance() instead.
*
* @param string $user Username
* @param string $pass Password
* @param string $host Host
*/
public function __construct($hostname, $data = array())
{
parent::__construct($hostname, $data);
// Faster check if mongo is not loaded
if (!$this->isAvailable()) {
return;
}
$mongo = new \MongoDB\Driver\Manager('mongodb://'.$hostname);
// MongoDB uses lazy loading of server list
// so just execute an arbitrary command in order
// to make it populate the server list
$command = new \MongoDB\Driver\Command(array('ping' => 1));
try {
$mongo->executeCommand('admin', $command);
} catch (\MongoDB\Driver\Exception\ConnectionTimeoutException $e) {
$this->_connect_error = $e;
$this->_connect_errno = 1;
return;
}
// retrieve server list
$servers = $mongo->getServers();
if (!isset($servers[0])) {
$this->_connect_error = 'Failed to connect to MongoDB host on '.$hostname.' (No host info available)';
$this->_connect_errno = 2;
return;
} else if ($servers[0]->getHost() != $hostname) {
$this->_connect_error = 'Failed to connect to MongoDB host on '.$hostname.' (servername does not match: '.$servers[0]->getHost().')';
$this->_connect_errno = 3;
return;
}
$this->_mongo = $mongo;
}
/*********************************************************************************
*
* Select functions
*
*********************************************************************************/
private function command($command)
{
$cmd = new \MongoDB\Driver\Command($command);
if ($this->_mongo) {
try {
$cursor = $this->_mongo->executeCommand('admin', $cmd);
return $cursor->toArray();
} catch(\MongoDB\Driver\Exception $e) {
loadClass('Logger')->error($e->getMessage().'. Could not execute MongoDB command: '.print_r($command, true));
}
}
return array();
}
private function query($query)
{
}
/**
* Get all MongoDB Databases.
* @return mixed[] Array of databases
*/
public function getDatabases()
{
$databases = array();
$tmp = $this->command(array('listDatabases' => true));
if (isset($tmp[0])) {
foreach ($tmp[0]->databases as $db) {
$databases[] = array(
'name' => $db->name,
'size' => $db->sizeOnDisk,
'empty' => $db->empty
);
}
}
return $databases;
}
public function getInfo()
{
$info = array();
$tmp = $this->command(array('serverStatus' => true));
if (isset($tmp[0])) {
$info = $tmp[0];
}
return $info;
}
/*********************************************************************************
*
* Interface required functions
*
*********************************************************************************/
private $_can_connect = array();
private $_can_connect_err = array();
private $_name = null;
private $_version = null;
public function canConnect(&$err, $hostname, $data = array())
{
$err = false;
// Return if already cached
if (isset($this->_can_connect[$hostname])) {
// Assume error for unset error message
$err = isset($this->_can_connect_err[$hostname]) ? $this->_can_connect_err[$hostname] : true;
return $this->_can_connect[$hostname];
}
$manager = new \MongoDB\Driver\Manager('mongodb://'.$hostname);
// MongoDB uses lazy loading of server list
// so just execute an arbitrary command in order
// to make it populate the server list
$command = new \MongoDB\Driver\Command(array('ping' => 1));
$manager->executeCommand('admin', $command);
// retrieve server list
$servers = $manager->getServers();
if (!isset($servers[0])) {
$err = 'Failed to connect to MongoDB host on '.$hostname.' (No host info available)';
$this->_can_connect[$hostname] = false;
} else if ($servers[0]->getHost() != $hostname) {
$err = 'Failed to connect to MongoDB host on '.$hostname.' (servername does not match: '.$servers[0]->getHost().')';
$this->_can_connect[$hostname] = false;
}
else {
$this->_can_connect[$hostname] = true;
}
$this->_can_connect_err[$hostname] = $err;
return $this->_can_connect[$hostname];
}
public function getName($default = 'MongoDB')
{
return $default;
}
public function getVersion()
{
// Return if already cached
if ($this->_version !== null) {
return $this->_version;
}
// Return empty if not available
if (!$this->isAvailable()) {
$this->_version = '';
return $this->_version;
}
if ($this->_mongo) {
$info = $this->getInfo();
if (!isset($info->version)) {
loadClass('Logger')->error('Could not get MongoDB version');
$this->_version = '';
} else {
$this->_version = $info->version;
}
}
return $this->_version;
}
}

View File

@ -383,7 +383,7 @@ devilbox_test() {
### Variables
###
_ret=0 # Final exit code
_oks=17 # Require this many [OK]'s on the page
_oks=20 # Require this many [OK]'s on the page

View File

@ -62,7 +62,7 @@ services:
# ----------------------------------------
php:
#image: cytopia/${PHP_SERVER:-php-fpm-7.0}:latest
image: cytopia/${PHP_SERVER:-php-fpm-7.0}:0.9
image: cytopia/${PHP_SERVER:-php-fpm-7.0}:release-0.10
restart: always
# Manually build via `docker-compose build`
@ -109,7 +109,7 @@ services:
##
## Enable 127.0.0.1 Port-forwarding
##
- FORWARD_PORTS_TO_LOCALHOST=3306:mysql:3306,5432:pgsql:5432,6379:redis:6379,11211:memcd:11211
- FORWARD_PORTS_TO_LOCALHOST=3306:mysql:3306,5432:pgsql:5432,6379:redis:6379,11211:memcd:11211,27017:mongo:27017
##
## Additional variables to announce to intranet/php-container
@ -122,10 +122,12 @@ services:
- HOST_PORT_PGSQL
- HOST_PORT_REDIS
- HOST_PORT_MEMCD
- HOST_PORT_MONGO
# Data dir paths
- HOST_PATH_HTTPD_DATADIR
- HOST_PATH_MYSQL_DATADIR
- HOST_PATH_PGSQL_DATADIR
- HOST_PATH_MONGO_DATADIR
# Database credentials
- PGSQL_ROOT_USER
- PGSQL_ROOT_PASSWORD
@ -137,6 +139,7 @@ services:
- MYSQL_SERVER
- REDIS_SERVER
- MEMCD_SERVER
- MONGO_SERVER
networks:
app_net:
@ -415,6 +418,33 @@ services:
- httpd
# ----------------------------------------
# Mongo
# ----------------------------------------
mongo:
image: mongo:${MONGO_SERVER:-latest}
ports:
# [local-machine:]local-port:docker-port
- "${LOCAL_LISTEN_ADDR}${HOST_PORT_MONGO}:27017"
networks:
app_net:
ipv4_address: 172.16.238.16
volumes:
# ---- Format: ----
# HOST-DIRECTORY : DOCKER-DIRECTORY
# Mount MySQL Data directory
- ${HOST_PATH_MONGO_DATADIR}:/data/db
depends_on:
- bind
- php
- httpd
################################################################################
# NETWORK
################################################################################