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
71 changes: 69 additions & 2 deletions src/LagoonCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -70,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 @@ -116,6 +116,69 @@ 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
*
* @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;
}

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

$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 @@ -213,7 +276,11 @@ public function getLagoonEnvs() {
standbyAlias,
environments {
name,
openshiftProjectName
openshiftProjectName,
openshift {
tobybellwood marked this conversation as resolved.
Show resolved Hide resolved
sshHost,
sshPort
}
}
}
}', $this->projectName);
Expand Down