From 79f36807d50841bf607388b266c033c2ff16390d Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 8 Mar 2017 16:57:34 +0000 Subject: [PATCH 1/4] Get the MySQL from .my.cnf due to drush bug --- resources/stacks/drupal7/settings.local.php | 4 +- resources/stacks/drupal8/settings.local.php | 4 +- src/Docker/ComposeContainers.php | 41 ++++++++++++++++++++- src/Stacks/Drupal.php | 4 +- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/resources/stacks/drupal7/settings.local.php b/resources/stacks/drupal7/settings.local.php index 82d1f4d..283fbd8 100644 --- a/resources/stacks/drupal7/settings.local.php +++ b/resources/stacks/drupal7/settings.local.php @@ -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' => '', ); diff --git a/resources/stacks/drupal8/settings.local.php b/resources/stacks/drupal8/settings.local.php index ccb00eb..c1c2575 100644 --- a/resources/stacks/drupal8/settings.local.php +++ b/resources/stacks/drupal8/settings.local.php @@ -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', diff --git a/src/Docker/ComposeContainers.php b/src/Docker/ComposeContainers.php index f33a4c1..85a6030 100644 --- a/src/Docker/ComposeContainers.php +++ b/src/Docker/ComposeContainers.php @@ -91,14 +91,51 @@ public function addDatabase() ], 'environment' => [ 'MYSQL_DATABASE' => 'data', - 'MYSQL_USER' => 'mysql', - 'MYSQL_PASSWORD' => 'mysql', + 'MYSQL_USER' => static::getMysqlUser(), + 'MYSQL_PASSWORD' => static::getMysqlPassword(), 'MYSQL_ALLOW_EMPTY_PASSWORD' => 'yes', 'MYSQL_ROOT_PASSWORD' => 'root,' ], ]; } + 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'] : 'mysql'; + } + + 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['password'] : 'mysql'; + } + + /** + * @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'); + } + /** * */ diff --git a/src/Stacks/Drupal.php b/src/Stacks/Drupal.php index a88bff7..bc6f3b1 100644 --- a/src/Stacks/Drupal.php +++ b/src/Stacks/Drupal.php @@ -3,6 +3,7 @@ namespace mglaman\PlatformDocker\Stacks; +use mglaman\PlatformDocker\Docker\ComposeContainers; use mglaman\Toolstack\Toolstack; use mglaman\Toolstack\Stacks\Drupal as DrupalStackHelper; use mglaman\PlatformDocker\Platform; @@ -92,7 +93,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 }}', ComposeContainers::getMysqlUser(), $localSettings); + $localSettings = str_replace('{{ mysql_password }}', ComposeContainers::getMysqlPassword(), $localSettings); file_put_contents(Platform::sharedDir() . '/settings.local.php', $localSettings); // Relink if missing. From c107e08da62f6ce64b93dbb989bd0686bd16c552 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 8 Mar 2017 17:14:03 +0000 Subject: [PATCH 2/4] Move mysql stuff to dedicated class --- src/Docker/ComposeContainers.php | 44 ++----------------- src/Mysql/Mysql.php | 74 ++++++++++++++++++++++++++++++++ src/Stacks/Drupal.php | 7 ++- 3 files changed, 81 insertions(+), 44 deletions(-) create mode 100644 src/Mysql/Mysql.php diff --git a/src/Docker/ComposeContainers.php b/src/Docker/ComposeContainers.php index 85a6030..91cc1d2 100644 --- a/src/Docker/ComposeContainers.php +++ b/src/Docker/ComposeContainers.php @@ -4,6 +4,7 @@ use mglaman\PlatformDocker\Config; +use mglaman\PlatformDocker\Mysql\Mysql; use mglaman\PlatformDocker\Platform; use Symfony\Component\Yaml\Yaml; @@ -91,51 +92,14 @@ public function addDatabase() ], 'environment' => [ 'MYSQL_DATABASE' => 'data', - 'MYSQL_USER' => static::getMysqlUser(), - 'MYSQL_PASSWORD' => static::getMysqlPassword(), + 'MYSQL_USER' => Mysql::getMysqlUser(), + 'MYSQL_PASSWORD' => Mysql::getMysqlPassword(), 'MYSQL_ALLOW_EMPTY_PASSWORD' => 'yes', - 'MYSQL_ROOT_PASSWORD' => 'root,' + 'MYSQL_ROOT_PASSWORD' => Mysql::getMysqlRootPassword(), ], ]; } - 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'] : 'mysql'; - } - - 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['password'] : 'mysql'; - } - - /** - * @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'); - } - /** * */ diff --git a/src/Mysql/Mysql.php b/src/Mysql/Mysql.php new file mode 100644 index 0000000..e9ceb1d --- /dev/null +++ b/src/Mysql/Mysql.php @@ -0,0 +1,74 @@ +containerName, $localSettings); $localSettings = str_replace('{{ redis_container_name }}', $this->redisContainerName, $localSettings); $localSettings = str_replace('{{ project_domain }}', $this->projectName . '.' . $this->projectTld, $localSettings); - $localSettings = str_replace('{{ mysql_user }}', ComposeContainers::getMysqlUser(), $localSettings); - $localSettings = str_replace('{{ mysql_password }}', ComposeContainers::getMysqlPassword(), $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. From 71d14ad779483cb78129d3535d8caf7edbfa17ac Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 8 Mar 2017 20:49:47 +0000 Subject: [PATCH 3/4] Make the new mysql user fix work --- src/Docker/ComposeContainers.php | 8 ++++++-- src/Mysql/Mysql.php | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Docker/ComposeContainers.php b/src/Docker/ComposeContainers.php index 91cc1d2..79514dd 100644 --- a/src/Docker/ComposeContainers.php +++ b/src/Docker/ComposeContainers.php @@ -92,12 +92,16 @@ public function addDatabase() ], 'environment' => [ 'MYSQL_DATABASE' => 'data', - 'MYSQL_USER' => Mysql::getMysqlUser(), - 'MYSQL_PASSWORD' => Mysql::getMysqlPassword(), 'MYSQL_ALLOW_EMPTY_PASSWORD' => 'yes', '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(); + } } /** diff --git a/src/Mysql/Mysql.php b/src/Mysql/Mysql.php index e9ceb1d..b11b648 100644 --- a/src/Mysql/Mysql.php +++ b/src/Mysql/Mysql.php @@ -4,6 +4,11 @@ /** * 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 { From 678b711a9f70959b16f4f6d469f0c2e469651432 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Thu, 9 Mar 2017 09:29:13 +0000 Subject: [PATCH 4/4] Harder mysql user usage --- src/Mysql/Mysql.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mysql/Mysql.php b/src/Mysql/Mysql.php index b11b648..058882b 100644 --- a/src/Mysql/Mysql.php +++ b/src/Mysql/Mysql.php @@ -24,7 +24,7 @@ public static function getMysqlUser() { if (file_exists($my_cnf_file)) { $my_cnf = parse_ini_file($my_cnf_file); } - return isset($my_cnf['user']) ? $my_cnf['user'] : 'mysql'; + return isset($my_cnf['user']) && $my_cnf['user'] !== 'root' ? $my_cnf['user'] : 'mysql'; } /** @@ -38,7 +38,7 @@ public static function getMysqlPassword() { if (file_exists($my_cnf_file)) { $my_cnf = parse_ini_file($my_cnf_file); } - return isset($my_cnf['password']) ? $my_cnf['password'] : 'mysql'; + return isset($my_cnf['password'], $my_cnf['user']) && $my_cnf['user'] !== 'root' ? $my_cnf['password'] : 'mysql'; } /**