Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate alias file #15

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
87 changes: 83 additions & 4 deletions src/LagoonCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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;

Comment on lines +21 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'd ideally make a function here to handle uselagoon/build-deploy-tool#193 and fallback defaults to use in a chained null coalesce as per the api. We can probably assume that the last chance fallback doesn't need to be host and port configurable though?

/**
* Lagoon API endpoint.
*
Expand Down Expand Up @@ -67,11 +77,10 @@ public function __construct() {
// Get default config.
$lagoonyml = $this->getLagoonYml();
$this->api = $lagoonyml['api'] ?? 'https://api.lagoon.amazeeio.cloud/graphql';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to accommodate uselagoon/build-deploy-tool#193

Suggested change
$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'] ?? '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;

// Allow environment variable overrides.
$this->api = getenv('LAGOON_OVERRIDE_API') ?: $this->api;
$this->endpoint = getenv('LAGOON_OVERRIDE_SSH') ?: $this->endpoint;
Expand Down Expand Up @@ -106,7 +115,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) {
Expand All @@ -117,6 +126,72 @@ public function aliases() {
}
}

/**
* Get and print remote aliases from lagoon API site aliases file.
*
* @param string $file
* Optional, output the alias file to a particular file.
*
* @command lagoon:generate-aliases
*
* @aliases lg
*/
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');
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;
}

foreach ($response->data->project->environments as $env) {
$details = [
"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 ?: self::DEFAULT_SSH_PORT),
"tty" => "false",
],
];

$alias[$env->name] = $details;
}

$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);
}
}

/**
* Generate a JWT token for the lagoon API.
*
Expand Down Expand Up @@ -234,7 +309,11 @@ public function getLagoonEnvs() {
standbyAlias,
environments {
name,
openshiftProjectName
kubernetesNamespaceName,
kubernetes {
sshHost,
sshPort
}
}
}
}', $this->projectName);
Expand Down