From 9543cb7a7a3d5c7db50d0818806eac618aed1dd7 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Fri, 21 Jul 2023 12:46:24 +1200 Subject: [PATCH 01/12] Adds generate alias file command --- src/LagoonCommands.php | 52 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 187f6e7..f594ef1 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -70,7 +70,6 @@ public function __construct() { $this->jwt_token = getenv('LAGOON_OVERRIDE_JWT_TOKEN'); $this->projectName = $lagoonyml['project'] ?? ''; $this->ssh_port_timeout = $lagoonyml['ssh_port_timeout'] ?? 30; - // Allow environment variable overrides. $this->api = getenv('LAGOON_OVERRIDE_API') ?: $this->api; $this->endpoint = getenv('LAGOON_OVERRIDE_SSH') ?: $this->endpoint; @@ -116,6 +115,51 @@ public function aliases() { } } + /** + * Get all remote aliases from lagoon API and generate a drush compatible site.aliases file + * + * @command lagoon:generate-aliases + * + * @aliases lg + */ + public function generateAliases() { + // Project still not defined, throw a warning. + if ($this->projectName === FALSE) { + $this->logger()->warning('ERROR: Could not discover project name, you should define it inside your .lagoon.yml file'); + return; + } + + if (empty($this->jwt_token)) { + $this->jwt_token = $this->getJwtToken(); + } + + $response = $this->getLagoonEnvs(); + // Check if the query returned any data for the requested project. + if (empty($response->data->project->environments)) { + $this->logger()->warning("API request didn't return any environments for the given project '$this->projectName'."); + return; + } + + $aliases = []; + + foreach ($response->data->project->environments as $env) { + $details = [ + "host" => $env->openshift->sshHost, + "user" => $env->openshiftProjectName, + "paths" => ["files" => "/app/web/sites/default/files"], + "ssh" => [ + "options" => sprintf('-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=FATAL -p %s', $env->openshift->sshPort), + "tty" => "false", + ], + ]; + + $alias[$env->name] = $details; + } + + print(Yaml::dump($alias, 2)); + } + + /** * Generate a JWT token for the lagoon API. * @@ -213,7 +257,11 @@ public function getLagoonEnvs() { standbyAlias, environments { name, - openshiftProjectName + openshiftProjectName, + openshift { + sshHost, + sshPort + } } } }', $this->projectName); From 0a166529adb62c1e2eaa2becaeeb75e0a2063daf Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Mon, 24 Jul 2023 11:26:35 +1200 Subject: [PATCH 02/12] Adds some debugging and file output option --- src/LagoonCommands.php | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index f594ef1..60ad56e 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -7,6 +7,7 @@ use Drush\Drush; use Drush\SiteAlias\SiteAliasManagerAwareInterface; use GuzzleHttp\Client; +use mysql_xdevapi\Exception; use Symfony\Component\Process\Process; use Symfony\Component\Yaml\Yaml; @@ -116,13 +117,14 @@ public function aliases() { } /** - * Get all remote aliases from lagoon API and generate a drush compatible site.aliases file + * Get all remote aliases from lagoon API and generate a drush compatible site aliases file + * * @param $file Optional argument to output the alias file to a particular file * * @command lagoon:generate-aliases * * @aliases lg */ - public function generateAliases() { + public function generateAliases($file = null) { // Project still not defined, throw a warning. if ($this->projectName === FALSE) { $this->logger()->warning('ERROR: Could not discover project name, you should define it inside your .lagoon.yml file'); @@ -156,7 +158,24 @@ public function generateAliases() { $alias[$env->name] = $details; } - print(Yaml::dump($alias, 2)); + $aliasContents = ""; + + try { + $aliasContents = Yaml::dump($alias, 2); + } catch (\Exception $exception) { + $this->logger->warning("Unable to dump alias yaml: " . $exception->getMessage()); + } + + if (!is_null($file)) { + if(file_put_contents($file, $aliasContents) === FALSE) { + $this->logger->warning("Unable to write aliases to " . $file); + } else { + $this->logger->warning("Successfully wrote aliases to " . $file); + } + } + else { + $this->io()->writeln($aliasContents); + } } From 1a12df8e607debe379114620453dac8ae4186155 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Mon, 24 Jul 2023 11:47:04 +1200 Subject: [PATCH 03/12] Fixing phpcs errors --- src/LagoonCommands.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 60ad56e..1d0a873 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -117,17 +117,20 @@ public function aliases() { } /** - * Get all remote aliases from lagoon API and generate a drush compatible site aliases file - * * @param $file Optional argument to output the alias file to a particular file + * Get all remote aliases from lagoon API and generate a drush compatible + * site aliases file + * * @param $file Optional argument to output the alias file to a particular + * file * * @command lagoon:generate-aliases * * @aliases lg */ - public function generateAliases($file = null) { + public function generateAliases($file = NULL) { // Project still not defined, throw a warning. if ($this->projectName === FALSE) { - $this->logger()->warning('ERROR: Could not discover project name, you should define it inside your .lagoon.yml file'); + $this->logger() + ->warning('ERROR: Could not discover project name, you should define it inside your .lagoon.yml file'); return; } @@ -138,12 +141,11 @@ public function generateAliases($file = null) { $response = $this->getLagoonEnvs(); // Check if the query returned any data for the requested project. if (empty($response->data->project->environments)) { - $this->logger()->warning("API request didn't return any environments for the given project '$this->projectName'."); + $this->logger() + ->warning("API request didn't return any environments for the given project '$this->projectName'."); return; } - $aliases = []; - foreach ($response->data->project->environments as $env) { $details = [ "host" => $env->openshift->sshHost, @@ -162,14 +164,16 @@ public function generateAliases($file = null) { try { $aliasContents = Yaml::dump($alias, 2); - } catch (\Exception $exception) { + } + catch (\Exception $exception) { $this->logger->warning("Unable to dump alias yaml: " . $exception->getMessage()); } if (!is_null($file)) { - if(file_put_contents($file, $aliasContents) === FALSE) { + if (file_put_contents($file, $aliasContents) === FALSE) { $this->logger->warning("Unable to write aliases to " . $file); - } else { + } + else { $this->logger->warning("Successfully wrote aliases to " . $file); } } @@ -178,7 +182,6 @@ public function generateAliases($file = null) { } } - /** * Generate a JWT token for the lagoon API. * From 0cce42bc7994c50a055f900e632ebb990f0ceea5 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Mon, 24 Jul 2023 12:00:47 +1200 Subject: [PATCH 04/12] fixes phpcs errors and changes references in graphql --- src/LagoonCommands.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 1d0a873..2acd9b3 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -119,8 +119,7 @@ public function aliases() { /** * Get all remote aliases from lagoon API and generate a drush compatible * site aliases file - * * @param $file Optional argument to output the alias file to a particular - * file + * * @param $file Optional output the alias file to a particular file. * * @command lagoon:generate-aliases * @@ -148,11 +147,11 @@ public function generateAliases($file = NULL) { foreach ($response->data->project->environments as $env) { $details = [ - "host" => $env->openshift->sshHost, - "user" => $env->openshiftProjectName, + "host" => $env->kubernetes->sshHost, + "user" => $env->kubernetesProjectName, "paths" => ["files" => "/app/web/sites/default/files"], "ssh" => [ - "options" => sprintf('-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=FATAL -p %s', $env->openshift->sshPort), + "options" => sprintf('-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=FATAL -p %s', $env->kubernetes->sshPort), "tty" => "false", ], ]; @@ -279,8 +278,8 @@ public function getLagoonEnvs() { standbyAlias, environments { name, - openshiftProjectName, - openshift { + kubernetesNamespaceName, + kubernetes { sshHost, sshPort } From e92abece8982199ece90e1f92e23a27c553819c5 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Mon, 24 Jul 2023 12:27:02 +1200 Subject: [PATCH 05/12] Fixing phpcs issues --- src/LagoonCommands.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 2acd9b3..498c05e 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -118,8 +118,9 @@ public function aliases() { /** * Get all remote aliases from lagoon API and generate a drush compatible - * site aliases file - * * @param $file Optional output the alias file to a particular file. + * site aliases file. + * + * @param $file Optional output the alias file to a particular file. * * @command lagoon:generate-aliases * @@ -148,7 +149,7 @@ public function generateAliases($file = NULL) { foreach ($response->data->project->environments as $env) { $details = [ "host" => $env->kubernetes->sshHost, - "user" => $env->kubernetesProjectName, + "user" => $env->kubernetesNamespaceName, "paths" => ["files" => "/app/web/sites/default/files"], "ssh" => [ "options" => sprintf('-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=FATAL -p %s', $env->kubernetes->sshPort), From b7f413dc4f29f5f06a48b1ba69e07b3fb03b75d8 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Mon, 24 Jul 2023 12:40:22 +1200 Subject: [PATCH 06/12] phpcs errors --- src/LagoonCommands.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 498c05e..cf5d353 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -7,7 +7,6 @@ use Drush\Drush; use Drush\SiteAlias\SiteAliasManagerAwareInterface; use GuzzleHttp\Client; -use mysql_xdevapi\Exception; use Symfony\Component\Process\Process; use Symfony\Component\Yaml\Yaml; @@ -117,10 +116,10 @@ public function aliases() { } /** - * Get all remote aliases from lagoon API and generate a drush compatible - * site aliases file. + * Get and print remote aliases from lagoon API site aliases file. * - * @param $file Optional output the alias file to a particular file. + * @param string $file + * Optional, output the alias file to a particular file. * * @command lagoon:generate-aliases * From 277c4b9842cc1fea95e577d1dc454fae7c851b5b Mon Sep 17 00:00:00 2001 From: Toby Bellwood Date: Mon, 14 Aug 2023 11:40:57 +1000 Subject: [PATCH 07/12] use kubernetesNamespaceName for la returns too --- src/LagoonCommands.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 4f91649..b8eea5d 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -105,7 +105,7 @@ public function aliases() { } foreach ($response->data->project->environments as $env) { - $alias = '@lagoon.' . $env->openshiftProjectName; + $alias = '@lagoon.' . $env->kubernetesNamespaceName; // Add production flag. if ($env->name === $response->data->project->productionEnvironment) { From fb9bfd021fb71c6c38148903f4cd0908a96b2be5 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Wed, 16 Aug 2023 10:28:54 +1200 Subject: [PATCH 08/12] Adds fallbacks for lagoon alias file generation --- src/LagoonCommands.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index cf5d353..8e8c1c8 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -17,6 +17,16 @@ class LagoonCommands extends DrushCommands implements SiteAliasManagerAwareInter use SiteAliasManagerAwareTrait; + /** + * Default ssh host, used for fallback + */ + const DEFAULT_SSH_HOST = 'ssh.lagoon.amazeeio.cloud'; + + /** + * Default ssh port, used for fallback + */ + const DEFAULT_SSH_PORT = 32222; + /** * Lagoon API endpoint. * @@ -66,7 +76,7 @@ public function __construct() { // Get default config. $lagoonyml = $this->getLagoonYml(); $this->api = $lagoonyml['api'] ?? 'https://api.lagoon.amazeeio.cloud/graphql'; - $this->endpoint = $lagoonyml['ssh'] ?? 'ssh.lagoon.amazeeio.cloud:32222'; + $this->endpoint = $lagoonyml['ssh'] ?? sprintf("%s:%s", self::DEFAULT_SSH_HOST, self::DEFAULT_SSH_PORT); $this->jwt_token = getenv('LAGOON_OVERRIDE_JWT_TOKEN'); $this->projectName = $lagoonyml['project'] ?? ''; $this->ssh_port_timeout = $lagoonyml['ssh_port_timeout'] ?? 30; @@ -147,11 +157,11 @@ public function generateAliases($file = NULL) { foreach ($response->data->project->environments as $env) { $details = [ - "host" => $env->kubernetes->sshHost, + "host" => $env->kubernetes->sshHost ?: self::DEFAULT_SSH_HOST, "user" => $env->kubernetesNamespaceName, "paths" => ["files" => "/app/web/sites/default/files"], "ssh" => [ - "options" => sprintf('-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=FATAL -p %s', $env->kubernetes->sshPort), + "options" => sprintf('-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=FATAL -p %s', $env->kubernetes->sshPort ?: self::DEFAULT_SSH_PORT), "tty" => "false", ], ]; From fa5cf9c2f00aa11d92dfdccbb15fef0a83369995 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Fri, 8 Sep 2023 07:18:29 +1200 Subject: [PATCH 09/12] Makes tty a bool --- src/LagoonCommands.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index d72537c..83ced7a 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -163,7 +163,7 @@ public function generateAliases($file = NULL) { "paths" => ["files" => "/app/web/sites/default/files"], "ssh" => [ "options" => sprintf('-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=FATAL -p %s', $env->kubernetes->sshPort ?: self::DEFAULT_SSH_PORT), - "tty" => "false", + "tty" => false, ], ]; @@ -284,9 +284,9 @@ public function getJwtToken() { } $ssh->setTimeout($this->sshTimeout); - + try { - $ssh->mustRun(); + $ssh->mustRun(); } catch (ProcessFailedException $exception) { $this->logger->debug($ssh->getMessage()); } From 47a1dfce95bb96b9490ded8a139c14cc4f2af6a9 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Fri, 8 Sep 2023 07:56:53 +1200 Subject: [PATCH 10/12] Adds env for config api host --- src/LagoonCommands.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 83ced7a..1f50d94 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -76,7 +76,7 @@ class LagoonCommands extends DrushCommands implements SiteAliasManagerAwareInter public function __construct() { // Get default config. $lagoonyml = $this->getLagoonYml(); - $this->api = $lagoonyml['api'] ?? 'https://api.lagoon.amazeeio.cloud/graphql'; + $this->api = getenv('LAGOON_CONFIG_API_HOST') ?? $lagoonyml['api'] ?? 'https://api.lagoon.amazeeio.cloud/graphql'; $this->endpoint = $lagoonyml['ssh'] ?? sprintf("%s:%s", self::DEFAULT_SSH_HOST, self::DEFAULT_SSH_PORT); $this->jwt_token = getenv('LAGOON_OVERRIDE_JWT_TOKEN'); $this->projectName = $lagoonyml['project'] ?? ''; From b5ce1f2760cddd6ee60011f60e04228268ad93e7 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Fri, 8 Sep 2023 09:44:13 +1200 Subject: [PATCH 11/12] Fixes correct operator --- src/LagoonCommands.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 1f50d94..6542b26 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -76,7 +76,7 @@ class LagoonCommands extends DrushCommands implements SiteAliasManagerAwareInter public function __construct() { // Get default config. $lagoonyml = $this->getLagoonYml(); - $this->api = getenv('LAGOON_CONFIG_API_HOST') ?? $lagoonyml['api'] ?? 'https://api.lagoon.amazeeio.cloud/graphql'; + $this->api = getenv('LAGOON_CONFIG_API_HOST') ?: $lagoonyml['api'] ?? 'https://api.lagoon.amazeeio.cloud/graphql'; $this->endpoint = $lagoonyml['ssh'] ?? sprintf("%s:%s", self::DEFAULT_SSH_HOST, self::DEFAULT_SSH_PORT); $this->jwt_token = getenv('LAGOON_OVERRIDE_JWT_TOKEN'); $this->projectName = $lagoonyml['project'] ?? ''; From 4997d436c00192c55a349065ed7a1f3706fff618 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Fri, 8 Sep 2023 13:49:38 +1200 Subject: [PATCH 12/12] Uses fully qualified name for env name --- src/LagoonCommands.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LagoonCommands.php b/src/LagoonCommands.php index 6542b26..597850d 100644 --- a/src/LagoonCommands.php +++ b/src/LagoonCommands.php @@ -167,7 +167,7 @@ public function generateAliases($file = NULL) { ], ]; - $alias[$env->name] = $details; + $alias[$env->kubernetesNamespaceName] = $details; } $aliasContents = "";