Skip to content

Commit

Permalink
Add .env file for database configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Bernaszuk committed Sep 9, 2024
1 parent 685b1e3 commit b9745f3
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Thumbs.db
.vscode
ui
composer.lock
.env
.env.local

test.php
core/config/common.config.php
Expand Down
47 changes: 47 additions & 0 deletions core/config/common.config.env.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

$envData = [] + readEnv('.env.local') + readEnv('.env');

$databaseDsn = $envData['DATABASE_DSN'] ?? null;
if (null === $databaseDsn) {
$configFile = __DIR__ . '/common.config.php';
if (!is_readable($configFile)) {
throw new \RuntimeException('Missing database configuration. Please set DATABASE_DSN in .env file.');
}
trigger_error(sprintf('The "%s" file is deprecated. Please use .env instead.', $configFile), E_USER_DEPRECATED);
require_once $configFile;
return;
}

define('DEBUG', (int) ($envData['DEBUG'] ?? false));
$databaseConfig = parse_url($databaseDsn);
foreach (['scheme', 'host', 'path', 'user', 'pass'] as $key) {
if (!isset($databaseConfig[$key])) {
throw new \RuntimeException(sprintf('Incomplete database configuration, %s is missing.', $key));
}
}

if ($databaseConfig['scheme'] !== 'mysql') {
throw new \RuntimeException(sprintf('Unsupported database configuration, %s is not supported. Please use mysql instead.', $databaseConfig['scheme']));
}

global $CONFIG;
$CONFIG = [
'db' => [
'host' => $databaseConfig['host'],
'port' => (string) ($databaseConfig['port'] ?? 3306),
'dbname' => ltrim($databaseConfig['path'], '/'),
'username' => $databaseConfig['user'],
'password' => $databaseConfig['pass'],
],
];

function readEnv(string $name): array
{
$dotenv = dirname(__DIR__, 2) . '/'. $name;
if (!is_readable($dotenv)) {
return [];
}

return parse_ini_file($dotenv, false) ?: [];
}
2 changes: 1 addition & 1 deletion core/php/core.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
date_default_timezone_set('Europe/Brussels');
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../config/common.config.php';
require_once __DIR__ . '/../config/common.config.env.php';
require_once __DIR__ . '/../class/DB.class.php';
require_once __DIR__ . '/../class/config.class.php';
require_once __DIR__ . '/../class/jeedom.class.php';
Expand Down
13 changes: 7 additions & 6 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
* along with Jeedom. If not, see <http://www.gnu.org/licenses/>.
*/
try {
//no config, install Jeedom!
if (!file_exists(__DIR__ . '/core/config/common.config.php')) {
echo 'Jeedom not configure, no common.config.php found';
die();
}
require_once __DIR__ . '/core/config/common.config.env.php';
} catch (\RuntimeException $e) {
echo 'Jeedom not configure, no common.config.php found';
die();
}

require_once __DIR__ . "/core/php/core.inc.php";
try {
require_once __DIR__ . "/core/php/core.inc.php";

if ((!isset($_GET['ajax']) || $_GET['ajax'] != 1) && count(system::ps('install/restore.php', 'sudo')) > 0) {
require_once __DIR__.'/restoring.php';
Expand Down
8 changes: 7 additions & 1 deletion install/OS_specific/Docker/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ echo 'Start init'

# $WEBSERVER_HOME and $VERSION env variables comes from Dockerfile

if [ -f ${WEBSERVER_HOME}/core/config/common.config.php ]; then
if [ -f ${WEBSERVER_HOME}/.env ]; then
echo 'Jeedom is already install'
JEEDOM_INSTALL=1
elif [ -f ${WEBSERVER_HOME}/core/config/common.config.php ]; then
echo 'Jeedom is already install'
JEEDOM_INSTALL=1
else
Expand All @@ -95,12 +98,15 @@ else
echo "DROP DATABASE IF EXISTS jeedom;" | mysql
echo "CREATE DATABASE jeedom;" | mysql
echo "GRANT ALL PRIVILEGES ON jeedom.* TO 'jeedom'@'localhost';" | mysql
# Part to remove
cp ${WEBSERVER_HOME}/core/config/common.config.sample.php ${WEBSERVER_HOME}/core/config/common.config.php
sed -i "s/#PASSWORD#/${MYSQL_JEEDOM_PASSWD}/g" ${WEBSERVER_HOME}/core/config/common.config.php
sed -i "s/#DBNAME#/jeedom/g" ${WEBSERVER_HOME}/core/config/common.config.php
sed -i "s/#USERNAME#/jeedom/g" ${WEBSERVER_HOME}/core/config/common.config.php
sed -i "s/#PORT#/3306/g" ${WEBSERVER_HOME}/core/config/common.config.php
sed -i "s/#HOST#/localhost/g" ${WEBSERVER_HOME}/core/config/common.config.php

echo "DATABASE_DSN=mysql://jeedom:${MYSQL_JEEDOM_PASSWD}@localhost/jeedom" > ${WEBSERVER_HOME}/.env
/root/install.sh -s 10 -v ${VERSION} -w ${WEBSERVER_HOME}
/root/install.sh -s 11 -v ${VERSION} -w ${WEBSERVER_HOME}
fi
Expand Down
2 changes: 2 additions & 0 deletions install/backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@
'.gitignore',
'node_modules',
'.log',
'.env',
'.env.local',
'core/config/common.config.php',
'data/imgOs',
'python_venv',
Expand Down
2 changes: 1 addition & 1 deletion install/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

require_once dirname(__DIR__).'/core/php/console.php';
require_once __DIR__ . '/../core/config/common.config.php';
require_once __DIR__ . '/../core/config/common.config.env.php';
require_once __DIR__ . '/../core/class/DB.class.php';
echo "[START CHECK AND FIX DB]\n";
try {
Expand Down
2 changes: 1 addition & 1 deletion install/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
try {
date_default_timezone_set('Europe/Brussels');
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../core/config/common.config.php';
require_once __DIR__ . '/../core/config/common.config.env.php';
require_once __DIR__ . '/../core/class/DB.class.php';
require_once __DIR__ . '/../core/class/system.class.php';
if (count(system::ps('install/install.php', 'sudo')) > 1) {
Expand Down
3 changes: 3 additions & 0 deletions install/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,15 @@ step_9_jeedom_configuration() {
mariadb_sql "CREATE DATABASE jeedom;"
mariadb_sql "GRANT ALL PRIVILEGES ON jeedom.* TO 'jeedom'@'localhost';"

# Path to remove
cp ${WEBSERVER_HOME}/core/config/common.config.sample.php ${WEBSERVER_HOME}/core/config/common.config.php
sed -i "s/#PASSWORD#/${MARIADB_JEEDOM_PASSWD}/g" ${WEBSERVER_HOME}/core/config/common.config.php
sed -i "s/#DBNAME#/jeedom/g" ${WEBSERVER_HOME}/core/config/common.config.php
sed -i "s/#USERNAME#/jeedom/g" ${WEBSERVER_HOME}/core/config/common.config.php
sed -i "s/#PORT#/3306/g" ${WEBSERVER_HOME}/core/config/common.config.php
sed -i "s/#HOST#/localhost/g" ${WEBSERVER_HOME}/core/config/common.config.php

echo "DATABASE_DSN=\"mysql://jeedom:${MARIADB_JEEDOM_PASSWD}@localhost:3306/jeedom\"" > ${WEBSERVER_HOME}/.env
fi
chmod 775 -R ${WEBSERVER_HOME}
chown -R www-data:www-data ${WEBSERVER_HOME}
Expand Down
2 changes: 1 addition & 1 deletion install/packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

require_once dirname(__DIR__).'/core/php/console.php';
require_once __DIR__ . '/../core/config/common.config.php';
require_once __DIR__ . '/../core/config/common.config.env.php';
require_once __DIR__ . '/../core/class/system.class.php';
echo "[START CHECK AND FIX PACKAGES]\n";
try {
Expand Down
26 changes: 24 additions & 2 deletions install/restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,15 @@
if (!copy(__DIR__ . '/../core/config/common.config.php', '/tmp/common.config.php')) {
echo 'Cannot copy ' . __DIR__ . "/../core/config/common.config.php\n";
}


if (!copy(__DIR__ . '/../.env', '/tmp/.env')) {
echo 'Cannot copy ' . dirname(__DIR__) . ".env\n";
}

if (!copy(__DIR__ . '/../.env', '/tmp/.env.local')) {
echo 'Cannot copy ' . dirname(__DIR__) . ".env.local\n";
}

echo "OK\n";

try {
Expand All @@ -111,6 +119,8 @@
'.git',
'.log',
'core/config/common.config.php',
'.env',
'.env.local',
'/vendor',
config::byKey('backup::path'),
);
Expand Down Expand Up @@ -187,7 +197,19 @@
copy('/tmp/common.config.php', __DIR__ . '/../core/config/common.config.php');
echo "OK\n";
}


if (!file_exists(dirname(__DIR__) . '/.env')) {
echo "Restoring .env file...";
copy('/tmp/.env', dirname(__DIR__) . '/.env');
echo "OK\n";
}

if (!file_exists(dirname(__DIR__) . '/.env.local')) {
echo "Restoring .env.local file...";
copy('/tmp/.env.local', dirname(__DIR__) . '/.env.local');
echo "OK\n";
}

echo "Restoring cache...";
try {
cache::restore();
Expand Down
10 changes: 5 additions & 5 deletions sick.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@
echo "Driver mysql disponible.\n";
}
// check database configuration
if(!file_exists(__DIR__ . '/core/config/common.config.php')){
echo 'Configuration manquante ! core/config/common.config.php non généré.';
exit(1);
try {
require_once __DIR__ . '/core/config/common.config.env.php';
} catch (\RuntimeException $e) {
echo 'Configuration manquante : ' . $e->getMessage();
exit(1);
}

require_once __DIR__ . '/core/config/common.config.php';

// check local socket if localhost is configured
if(isset($CONFIG['db']['unix_socket']) || (isset($CONFIG['db']['host']) && $CONFIG['db']['host'] == 'localhost')) {

Expand Down

0 comments on commit b9745f3

Please sign in to comment.