1
0
Fork 0

Simplify accessing ENV by always assuming DB prefix

pull/9/head
Christian Lück 10 years ago
parent eb88b7c261
commit fe6070a664

@ -1,32 +1,31 @@
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
$ename = 'DB';
$confpath = '/var/www/config.php'; $confpath = '/var/www/config.php';
$config = array(); $config = array();
if (getenv($ename . '_TYPE') !== false) { if (getenv('DB_TYPE') !== false) {
$config['DB_TYPE'] = getenv($ename . '_TYPE'); $config['DB_TYPE'] = getenv('DB_TYPE');
} elseif (getenv($ename . '_PORT_5432_TCP_ADDR') !== false) { } elseif (getenv('DB_PORT_5432_TCP_ADDR') !== false) {
// postgres container linked // postgres container linked
$config['DB_TYPE'] = 'pgsql'; $config['DB_TYPE'] = 'pgsql';
$eport = 5432; $eport = 5432;
} elseif (getenv($ename . '_PORT_3306_TCP_ADDR') !== false) { } elseif (getenv('DB_PORT_3306_TCP_ADDR') !== false) {
// mysql container linked // mysql container linked
$config['DB_TYPE'] = 'mysql'; $config['DB_TYPE'] = 'mysql';
$eport = 3306; $eport = 3306;
} }
if (!empty($eport)) { if (!empty($eport)) {
$config['DB_HOST'] = env($ename . '_PORT_' . $eport . '_TCP_ADDR'); $config['DB_HOST'] = env('DB_PORT_' . $eport . '_TCP_ADDR');
$config['DB_PORT'] = env($ename . '_PORT_' . $eport . '_TCP_PORT'); $config['DB_PORT'] = env('DB_PORT_' . $eport . '_TCP_PORT');
} elseif (getenv($ename . '_PORT') === false) { } elseif (getenv('DB_PORT') === false) {
error('The env ' . $ename . '_PORT does not exist. Make sure to run with "--link mypostgresinstance:' . $ename . '"'); error('The env DB_PORT does not exist. Make sure to run with "--link mypostgresinstance:DB"');
} elseif (is_numeric(getenv($ename . '_PORT')) && getenv($ename . '_HOST') !== false) { } elseif (is_numeric(getenv('DB_PORT')) && getenv('DB_HOST') !== false) {
// numeric DB_PORT provided; assume port number passed directly // numeric DB_PORT provided; assume port number passed directly
$config['DB_HOST'] = env($ename . '_HOST'); $config['DB_HOST'] = env('DB_HOST');
$config['DB_PORT'] = env($ename . '_PORT'); $config['DB_PORT'] = env('DB_PORT');
if (empty($config['DB_TYPE'])) { if (empty($config['DB_TYPE'])) {
switch ($config['DB_PORT']) { switch ($config['DB_PORT']) {
@ -37,7 +36,7 @@ if (!empty($eport)) {
$config['DB_TYPE'] = 'pgsql'; $config['DB_TYPE'] = 'pgsql';
break; break;
default: default:
error('Database on non-standard port ' . $config['DB_PORT'] . ' and env ' . $ename . '_TYPE not present'); error('Database on non-standard port ' . $config['DB_PORT'] . ' and env DB_TYPE not present');
} }
} }
} }
@ -46,9 +45,9 @@ if (!empty($eport)) {
// database name (DB_NAME) can be supplied or detaults to "ttrss" // database name (DB_NAME) can be supplied or detaults to "ttrss"
// database user (DB_USER) can be supplied or defaults to database name // database user (DB_USER) can be supplied or defaults to database name
// database pass (DB_PASS) can be supplied or defaults to database user // database pass (DB_PASS) can be supplied or defaults to database user
$config['DB_NAME'] = env($ename . '_NAME', 'ttrss'); $config['DB_NAME'] = env('DB_NAME', 'ttrss');
$config['DB_USER'] = env($ename . '_USER', $config['DB_NAME']); $config['DB_USER'] = env('DB_USER', $config['DB_NAME']);
$config['DB_PASS'] = env($ename . '_PASS', $config['DB_USER']); $config['DB_PASS'] = env('DB_PASS', $config['DB_USER']);
if (!dbcheck($config)) { if (!dbcheck($config)) {
echo 'Database login failed, trying to create...' . PHP_EOL; echo 'Database login failed, trying to create...' . PHP_EOL;
@ -59,8 +58,8 @@ if (!dbcheck($config)) {
$super = $config; $super = $config;
$super['DB_NAME'] = null; $super['DB_NAME'] = null;
$super['DB_USER'] = env($ename . '_ENV_USER', 'docker'); $super['DB_USER'] = env('DB_ENV_USER', 'docker');
$super['DB_PASS'] = env($ename . '_ENV_PASS', $super['DB_USER']); $super['DB_PASS'] = env('DB_ENV_PASS', $super['DB_USER']);
$pdo = dbconnect($super); $pdo = dbconnect($super);

Loading…
Cancel
Save