Support mysql storage backend
This commit is contained in:
		
							parent
							
								
									c3c0c7a9fd
								
							
						
					
					
						commit
						e018a8a41a
					
				@ -3,8 +3,7 @@ MAINTAINER Christian Lück <christian@lueck.tv>
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y \
 | 
					RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y \
 | 
				
			||||||
  nginx git supervisor php5-fpm php5-cli php5-curl php5-gd php5-json \
 | 
					  nginx git supervisor php5-fpm php5-cli php5-curl php5-gd php5-json \
 | 
				
			||||||
  php5-pgsql
 | 
					  php5-pgsql php5-mysql && apt-get clean
 | 
				
			||||||
# php5-mysql
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# add ttrss as the only nginx site
 | 
					# add ttrss as the only nginx site
 | 
				
			||||||
ADD ttrss.nginx.conf /etc/nginx/sites-available/ttrss
 | 
					ADD ttrss.nginx.conf /etc/nginx/sites-available/ttrss
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										31
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								README.md
									
									
									
									
									
								
							@ -110,6 +110,37 @@ following additional arguments:
 | 
				
			|||||||
-e DB_ENV_PASS=docker
 | 
					-e DB_ENV_PASS=docker
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#### Running with external database server
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					If you already have a PostgreSQL or MySQL server around off docker you also can go with
 | 
				
			||||||
 | 
					that.  Instead of linking docker containers you need to provide database hostname, port,
 | 
				
			||||||
 | 
					database name and user credentials manually like so:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					-e DB_HOST=172.17.42.1
 | 
				
			||||||
 | 
					-e DB_PORT=3306
 | 
				
			||||||
 | 
					-e DB_NAME=ttrss
 | 
				
			||||||
 | 
					-e DB_USER=ttrssuser
 | 
				
			||||||
 | 
					-e DB_PASS=ttrsspass
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					If your database is exposed on a non-standard port you also need to provide DB_TYPE set
 | 
				
			||||||
 | 
					to either "pgsql" or "mysql".
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#### Running with mysql database server
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					If you'd like to use ttrss with a mysql database backend, simply use the additional
 | 
				
			||||||
 | 
					database configuration arguments to docker mentioned above.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					You also might want to link ttrss container to a mysql container.  If the mysql server
 | 
				
			||||||
 | 
					is exposed on port 3306 it will be detected automatically, otherwise you need to specify
 | 
				
			||||||
 | 
					DB_TYPE env flag.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```bash
 | 
				
			||||||
 | 
					$ sudo docker run -name mysql -d sameersbn/mysql:latest
 | 
				
			||||||
 | 
					$ sudo docker run -it --link mysql:db -p 80:80 clue/ttrss
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#### Running ttrss daemonized
 | 
					#### Running ttrss daemonized
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Once you've confirmed everything works in the foreground, you can start your container
 | 
					Once you've confirmed everything works in the foreground, you can start your container
 | 
				
			||||||
 | 
				
			|||||||
@ -2,18 +2,49 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$ename = 'DB';
 | 
					$ename = 'DB';
 | 
				
			||||||
$eport = 5432;
 | 
					 | 
				
			||||||
$confpath = '/var/www/config.php';
 | 
					$confpath = '/var/www/config.php';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// check DB_NAME, which will be set automatically for a linked "db" container
 | 
					$config = array();
 | 
				
			||||||
if (!env($ename . '_PORT', '')) {
 | 
					
 | 
				
			||||||
    error('The env ' . $ename .'_PORT does not exist. Make sure to run with "--link mypostgresinstance:' . $ename . '"');
 | 
					if(getenv($ename . '_TYPE') !== false) {
 | 
				
			||||||
 | 
					    $config['DB_TYPE'] = getenv($ename . '_TYPE');
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					elseif(getenv($ename . '_PORT_5432_TCP_ADDR') !== false) {
 | 
				
			||||||
 | 
					    // postgres container linked
 | 
				
			||||||
 | 
					    $config['DB_TYPE'] = 'pgsql';
 | 
				
			||||||
 | 
					    $eport = 5432;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					elseif(getenv($ename . '_PORT_3306_TCP_ADDR') !== false) {
 | 
				
			||||||
 | 
					    // mysql container linked
 | 
				
			||||||
 | 
					    $config['DB_TYPE'] = 'mysql';
 | 
				
			||||||
 | 
					    $eport = 3306;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$config = array();
 | 
					if(!empty($eport)) {
 | 
				
			||||||
$config['DB_TYPE'] = 'pgsql';
 | 
					    $config['DB_HOST'] = env($ename . '_PORT_' . $eport . '_TCP_ADDR');
 | 
				
			||||||
$config['DB_HOST'] = env($ename . '_PORT_' . $eport . '_TCP_ADDR');
 | 
					    $config['DB_PORT'] = env($ename . '_PORT_' . $eport . '_TCP_PORT');
 | 
				
			||||||
$config['DB_PORT'] = env($ename . '_PORT_' . $eport . '_TCP_PORT');
 | 
					}
 | 
				
			||||||
 | 
					elseif(getenv($ename . '_PORT') === false) {
 | 
				
			||||||
 | 
					    error('The env ' . $ename .'_PORT does not exist. Make sure to run with "--link mypostgresinstance:' . $ename . '"');
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					elseif(is_numeric(getenv($ename . '_PORT')) && getenv($ename . '_HOST') !== false) {
 | 
				
			||||||
 | 
					    // numeric DB_PORT provided; assume port number passed directly
 | 
				
			||||||
 | 
					    $config['DB_HOST'] = env($ename . '_HOST');
 | 
				
			||||||
 | 
					    $config['DB_PORT'] = env($ename . '_PORT');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if(empty($config['DB_TYPE'])) {
 | 
				
			||||||
 | 
					        switch($config['DB_PORT']) {
 | 
				
			||||||
 | 
					        case 3306:
 | 
				
			||||||
 | 
					            $config['DB_TYPE'] = 'mysql';
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					        case 5432:
 | 
				
			||||||
 | 
					            $config['DB_TYPE'] = 'pgsql';
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					        default:
 | 
				
			||||||
 | 
					            error('Database on non-standard port '.$config['DB_PORT'].' and env ' . $ename .'_TYPE not present');
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// database credentials for this instance
 | 
					// database credentials for this instance
 | 
				
			||||||
//   database name (DB_NAME) can be supplied or detaults to "ttrss"
 | 
					//   database name (DB_NAME) can be supplied or detaults to "ttrss"
 | 
				
			||||||
@ -36,8 +67,16 @@ if (!dbcheck($config)) {
 | 
				
			|||||||
    $super['DB_PASS'] = env($ename . '_ENV_PASS', $super['DB_USER']);
 | 
					    $super['DB_PASS'] = env($ename . '_ENV_PASS', $super['DB_USER']);
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    $pdo = dbconnect($super);
 | 
					    $pdo = dbconnect($super);
 | 
				
			||||||
    $pdo->exec('CREATE ROLE ' . ($config['DB_USER']) . ' WITH LOGIN PASSWORD ' . $pdo->quote($config['DB_PASS']));
 | 
					
 | 
				
			||||||
    $pdo->exec('CREATE DATABASE ' . ($config['DB_NAME']) . ' WITH OWNER ' . ($config['DB_USER']));
 | 
					    if($super['DB_TYPE'] == 'mysql') {
 | 
				
			||||||
 | 
					        $pdo->exec('CREATE DATABASE ' . ($config['DB_NAME']));
 | 
				
			||||||
 | 
					        $pdo->exec('GRANT ALL PRIVILEGES ON ' . ($config['DB_NAME']) . '.* TO ' . $pdo->quote($config['DB_USER']) . '@"%" IDENTIFIED BY ' . $pdo->quote($config['DB_PASS']));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else {
 | 
				
			||||||
 | 
					        $pdo->exec('CREATE ROLE ' . ($config['DB_USER']) . ' WITH LOGIN PASSWORD ' . $pdo->quote($config['DB_PASS']));
 | 
				
			||||||
 | 
					        $pdo->exec('CREATE DATABASE ' . ($config['DB_NAME']) . ' WITH OWNER ' . ($config['DB_USER']));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    unset($pdo);
 | 
					    unset($pdo);
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    if (dbcheck($config)) {
 | 
					    if (dbcheck($config)) {
 | 
				
			||||||
@ -89,14 +128,14 @@ function error($text)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
function dbconnect($config)
 | 
					function dbconnect($config)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    $map = array('host' => 'HOST', 'port' => 'PORT', 'dbname' => 'NAME', 'user' => 'USER', 'password' => 'PASS');
 | 
					    $map = array('host' => 'HOST', 'port' => 'PORT', 'dbname' => 'NAME');
 | 
				
			||||||
    $dsn = $config['DB_TYPE'] . ':';
 | 
					    $dsn = $config['DB_TYPE'] . ':';
 | 
				
			||||||
    foreach ($map as $d => $h) {
 | 
					    foreach ($map as $d => $h) {
 | 
				
			||||||
        if (isset($config['DB_' . $h])) {
 | 
					        if (isset($config['DB_' . $h])) {
 | 
				
			||||||
            $dsn .= $d . '=' . $config['DB_' . $h] . ';';
 | 
					            $dsn .= $d . '=' . $config['DB_' . $h] . ';';
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    $pdo = new \PDO($dsn);
 | 
					    $pdo = new \PDO($dsn, $config['DB_USER'], $config['DB_PASS']);
 | 
				
			||||||
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 | 
					    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 | 
				
			||||||
    return $pdo;
 | 
					    return $pdo;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user