Skip to content
This repository was archived by the owner on May 1, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions resources/stacks/drupal7/settings.local.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => $port,
'username' => 'mysql',
'password' => 'mysql',
'username' => '{{ mysql_user }}',
'password' => '{{ mysql_password }}',
'database' => 'data',
'prefix' => '',
);
Expand Down
4 changes: 2 additions & 2 deletions resources/stacks/drupal8/settings.local.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => $port,
'username' => 'mysql',
'password' => 'mysql',
'username' => '{{ mysql_user }}',
'password' => '{{ mysql_password }}',
'database' => 'data',
'prefix' => '',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
Expand Down
11 changes: 8 additions & 3 deletions src/Docker/ComposeContainers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


use mglaman\PlatformDocker\Config;
use mglaman\PlatformDocker\Mysql\Mysql;
use mglaman\PlatformDocker\Platform;
use Symfony\Component\Yaml\Yaml;

Expand Down Expand Up @@ -91,12 +92,16 @@ public function addDatabase()
],
'environment' => [
'MYSQL_DATABASE' => 'data',
'MYSQL_USER' => 'mysql',
'MYSQL_PASSWORD' => 'mysql',
'MYSQL_ALLOW_EMPTY_PASSWORD' => 'yes',
'MYSQL_ROOT_PASSWORD' => 'root,'
'MYSQL_ROOT_PASSWORD' => Mysql::getMysqlRootPassword(),
],
];

$user = Mysql::getMysqlUser();
if (strcasecmp($user, 'root') !== 0) {
$this->config['mariadb']['environment']['MYSQL_USER'] = $user;
$this->config['mariadb']['environment']['MYSQL_PASSWORD'] = Mysql::getMysqlPassword();
}
}

/**
Expand Down
79 changes: 79 additions & 0 deletions src/Mysql/Mysql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace mglaman\PlatformDocker\Mysql;

/**
* Static methods to get info about the local mysql environment.
*
* We need to read .my.cnf files if they exist and use the user information from it. This is because of a bug in the
* most recent versions of Drush that is unlikely to be ported to Drush 8 or 7.
*
* @see https://github.com/drush-ops/drush/pull/2387
*/
class Mysql
{

/**
* The mysql user to use.
*
* @return string
*/
public static function getMysqlUser() {
$my_cnf = [];
$my_cnf_file = static::getUserDirectory() . '/.my.cnf';
if (file_exists($my_cnf_file)) {
$my_cnf = parse_ini_file($my_cnf_file);
}
return isset($my_cnf['user']) && $my_cnf['user'] !== 'root' ? $my_cnf['user'] : 'mysql';
}

/**
* The mysql password to use.
*
* @return string
*/
public static function getMysqlPassword() {
$my_cnf = [];
$my_cnf_file = static::getUserDirectory() . '/.my.cnf';
if (file_exists($my_cnf_file)) {
$my_cnf = parse_ini_file($my_cnf_file);
}
return isset($my_cnf['password'], $my_cnf['user']) && $my_cnf['user'] !== 'root' ? $my_cnf['password'] : 'mysql';
}

/**
* The mysql root password to use.
*
* @return string
*/
public static function getMysqlRootPassword() {
$password = 'root,';
$my_cnf_file = static::getUserDirectory() . '/.my.cnf';
if (file_exists($my_cnf_file)) {
$my_cnf = parse_ini_file($my_cnf_file);
if (isset($my_cnf['password'], $my_cnf['user']) && $my_cnf['user'] === 'root') {
$password = $my_cnf['password'];
}
}
return $password;
}

/**
* @return string The formal user home as detected from environment parameters
* @throws \RuntimeException If the user home could not reliably be determined
*/
public static function getUserDirectory()
{
if (false !== ($home = getenv('HOME'))) {
return $home;
}
if (defined('PHP_WINDOWS_VERSION_BUILD') && false !== ($home = getenv('USERPROFILE'))) {
return $home;
}
if (function_exists('posix_getuid') && function_exists('posix_getpwuid')) {
$info = posix_getpwuid(posix_getuid());
return $info['dir'];
}
throw new \RuntimeException('Could not determine user directory');
}
}
5 changes: 3 additions & 2 deletions src/Stacks/Drupal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace mglaman\PlatformDocker\Stacks;


use mglaman\PlatformDocker\Mysql\Mysql;
use mglaman\Toolstack\Toolstack;
use mglaman\Toolstack\Stacks\Drupal as DrupalStackHelper;
use mglaman\PlatformDocker\Platform;
Expand Down Expand Up @@ -92,7 +92,8 @@ protected function ensureLocalSettings() {
$localSettings = str_replace('{{ container_name }}', $this->containerName, $localSettings);
$localSettings = str_replace('{{ redis_container_name }}', $this->redisContainerName, $localSettings);
$localSettings = str_replace('{{ project_domain }}', $this->projectName . '.' . $this->projectTld, $localSettings);
$localSettings = str_replace('{{ project_domain }}', $this->projectName . '.' . $this->projectTld, $localSettings);
$localSettings = str_replace('{{ mysql_user }}', Mysql::getMysqlUser(), $localSettings);
$localSettings = str_replace('{{ mysql_password }}', Mysql::getMysqlPassword(), $localSettings);
file_put_contents(Platform::sharedDir() . '/settings.local.php', $localSettings);

// Relink if missing.
Expand Down