2014-04-26 15:24:57 +00:00
#!/usr/bin/env php
< ? php
$confpath = '/var/www/config.php' ;
2014-10-02 23:23:20 +00:00
$config = array ();
2015-02-21 14:48:03 +00:00
// path to ttrss
$config [ 'SELF_URL_PATH' ] = env ( 'SELF_URL_PATH' , 'http://localhost' );
2014-11-09 01:22:11 +00:00
if ( getenv ( 'DB_TYPE' ) !== false ) {
$config [ 'DB_TYPE' ] = getenv ( 'DB_TYPE' );
} elseif ( getenv ( 'DB_PORT_5432_TCP_ADDR' ) !== false ) {
2014-10-02 23:23:20 +00:00
// postgres container linked
$config [ 'DB_TYPE' ] = 'pgsql' ;
$eport = 5432 ;
2014-11-09 01:22:11 +00:00
} elseif ( getenv ( 'DB_PORT_3306_TCP_ADDR' ) !== false ) {
2014-10-02 23:23:20 +00:00
// mysql container linked
$config [ 'DB_TYPE' ] = 'mysql' ;
$eport = 3306 ;
}
2014-11-08 23:23:17 +00:00
if ( ! empty ( $eport )) {
2014-11-09 01:22:11 +00:00
$config [ 'DB_HOST' ] = env ( 'DB_PORT_' . $eport . '_TCP_ADDR' );
$config [ 'DB_PORT' ] = env ( 'DB_PORT_' . $eport . '_TCP_PORT' );
} elseif ( getenv ( 'DB_PORT' ) === false ) {
error ( 'The env DB_PORT does not exist. Make sure to run with "--link mypostgresinstance:DB"' );
} elseif ( is_numeric ( getenv ( 'DB_PORT' )) && getenv ( 'DB_HOST' ) !== false ) {
2014-10-02 23:23:20 +00:00
// numeric DB_PORT provided; assume port number passed directly
2014-11-09 01:22:11 +00:00
$config [ 'DB_HOST' ] = env ( 'DB_HOST' );
$config [ 'DB_PORT' ] = env ( 'DB_PORT' );
2014-04-26 15:24:57 +00:00
2014-11-08 23:23:17 +00:00
if ( empty ( $config [ 'DB_TYPE' ])) {
switch ( $config [ 'DB_PORT' ]) {
case 3306 :
$config [ 'DB_TYPE' ] = 'mysql' ;
break ;
case 5432 :
$config [ 'DB_TYPE' ] = 'pgsql' ;
break ;
default :
2014-11-09 01:22:11 +00:00
error ( 'Database on non-standard port ' . $config [ 'DB_PORT' ] . ' and env DB_TYPE not present' );
2014-10-02 23:23:20 +00:00
}
}
}
2014-04-26 15:24:57 +00:00
// database credentials for this instance
// database name (DB_NAME) can be supplied or detaults to "ttrss"
// database user (DB_USER) can be supplied or defaults to database name
// database pass (DB_PASS) can be supplied or defaults to database user
2014-11-09 01:22:11 +00:00
$config [ 'DB_NAME' ] = env ( 'DB_NAME' , 'ttrss' );
$config [ 'DB_USER' ] = env ( 'DB_USER' , $config [ 'DB_NAME' ]);
$config [ 'DB_PASS' ] = env ( 'DB_PASS' , $config [ 'DB_USER' ]);
2014-04-26 15:24:57 +00:00
if ( ! dbcheck ( $config )) {
echo 'Database login failed, trying to create...' . PHP_EOL ;
// superuser account to create new database and corresponding user account
// username (SU_USER) can be supplied or defaults to "docker"
// password (SU_PASS) can be supplied or defaults to username
$super = $config ;
$super [ 'DB_NAME' ] = null ;
2014-11-09 01:22:11 +00:00
$super [ 'DB_USER' ] = env ( 'DB_ENV_USER' , 'docker' );
$super [ 'DB_PASS' ] = env ( 'DB_ENV_PASS' , $super [ 'DB_USER' ]);
2016-11-28 03:10:49 +00:00
2014-04-26 15:24:57 +00:00
$pdo = dbconnect ( $super );
2014-10-02 23:23:20 +00:00
2014-11-08 23:23:17 +00:00
if ( $super [ 'DB_TYPE' ] === 'mysql' ) {
2014-10-02 23:23:20 +00:00
$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' ]));
2014-11-08 23:23:17 +00:00
} else {
2014-10-02 23:23:20 +00:00
$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' ]));
}
2014-04-26 15:24:57 +00:00
unset ( $pdo );
2016-11-28 03:10:49 +00:00
2014-04-26 15:24:57 +00:00
if ( dbcheck ( $config )) {
echo 'Database login created and confirmed' . PHP_EOL ;
} else {
error ( 'Database login failed, trying to create login failed as well' );
}
}
$pdo = dbconnect ( $config );
try {
$pdo -> query ( 'SELECT 1 FROM ttrss_feeds' );
// reached this point => table found, assume db is complete
}
catch ( PDOException $e ) {
echo 'Database table not found, applying schema... ' . PHP_EOL ;
$schema = file_get_contents ( 'schema/ttrss_schema_' . $config [ 'DB_TYPE' ] . '.sql' );
$schema = preg_replace ( '/--(.*?);/' , '' , $schema );
$schema = preg_replace ( '/[\r\n]/' , ' ' , $schema );
$schema = trim ( $schema , ' ;' );
foreach ( explode ( ';' , $schema ) as $stm ) {
$pdo -> exec ( $stm );
}
unset ( $pdo );
}
2016-11-28 03:10:49 +00:00
$config_prefix = 'CONFIG_' ;
foreach ( $_SERVER as $name => $value ) {
if ( strpos ( $name , $config_prefix ) === 0 ) {
$name = substr ( $name , strlen ( $config_prefix ));
echo 'Getting config from env: ' . $name . PHP_EOL ;
$config [ $name ] = $value ;
}
}
2014-04-26 15:24:57 +00:00
$contents = file_get_contents ( $confpath );
foreach ( $config as $name => $value ) {
$contents = preg_replace ( '/(define\s*\(\'' . $name . '\',\s*)(.*)(\);)/' , '$1"' . $value . '"$3' , $contents );
}
file_put_contents ( $confpath , $contents );
function env ( $name , $default = null )
{
$v = getenv ( $name ) ? : $default ;
2016-11-28 03:10:49 +00:00
2014-04-26 15:24:57 +00:00
if ( $v === null ) {
error ( 'The env ' . $name . ' does not exist' );
}
2016-11-28 03:10:49 +00:00
2014-04-26 15:24:57 +00:00
return $v ;
}
function error ( $text )
{
echo 'Error: ' . $text . PHP_EOL ;
exit ( 1 );
}
function dbconnect ( $config )
{
2014-10-02 23:23:20 +00:00
$map = array ( 'host' => 'HOST' , 'port' => 'PORT' , 'dbname' => 'NAME' );
2014-04-26 15:24:57 +00:00
$dsn = $config [ 'DB_TYPE' ] . ':' ;
foreach ( $map as $d => $h ) {
if ( isset ( $config [ 'DB_' . $h ])) {
$dsn .= $d . '=' . $config [ 'DB_' . $h ] . ';' ;
}
}
2014-10-02 23:23:20 +00:00
$pdo = new \PDO ( $dsn , $config [ 'DB_USER' ], $config [ 'DB_PASS' ]);
2014-04-26 15:24:57 +00:00
$pdo -> setAttribute ( PDO :: ATTR_ERRMODE , PDO :: ERRMODE_EXCEPTION );
return $pdo ;
}
function dbcheck ( $config )
{
try {
dbconnect ( $config );
return true ;
}
catch ( PDOException $e ) {
return false ;
}
}