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
75 changes: 72 additions & 3 deletions src/LagoonCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,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;
Expand Down Expand Up @@ -106,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) {
Expand All @@ -117,6 +116,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,
Copy link
Member

Choose a reason for hiding this comment

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

we need to handle existing deploytargets here that have no specific ssh host set - otherwise they will return null as the host?

I'm just not sure where to read it from - we could fall back to the lagoon aliases (or a hard coded default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tobybellwood - I think that should do it.

"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),
"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 +299,11 @@ public function getLagoonEnvs() {
standbyAlias,
environments {
name,
openshiftProjectName
kubernetesNamespaceName,
kubernetes {
sshHost,
sshPort
}
}
}
}', $this->projectName);
Expand Down