Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletions src/LagoonCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,35 @@ class LagoonCommands extends DrushCommands implements SiteAliasManagerAwareInter
*
* @var string
*/
private $api;
private $api = 'https://api.lagoon.amazeeio.cloud/graphql';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not entirely against this - just I know we had some focus around not hardcoding this stuff into the integrations work before. Thoughts?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think removing the hardcoded values at this moment would break these drush commands for the majority of users, not just "outside of lagoon environments"? They aren't in .lagoon.yml by default and the env vars LAGOON_OVERRIDE_API/LAGOON_OVERRIDE_SSH are non-standard (not set by Lagoon).


/**
* Lagoon SSH endpoint.
*
* @var string
*/
private $endpoint;
private $endpoint = 'ssh.lagoon.amazeeio.cloud:32222';

/**
* JWT token.
*
* @var string
*/
private $jwttoken;
private $jwtToken;

/**
* Lagoon project name.
*
* @var string
*/
private $projectName;
private $projectName = '';

/**
* Connection timeout.
*
* @var int
*/
private $sshTimeout;
private $sshTimeout = 30;

/**
* Path to a specific SSH key to use for lagoon authentication.
Expand All @@ -64,22 +64,20 @@ class LagoonCommands extends DrushCommands implements SiteAliasManagerAwareInter
* {@inheritdoc}
*/
public function __construct() {
if (getenv('LAGOON')) {
// 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->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;
$this->projectName = getenv('LAGOON_PROJECT') ?: $this->projectName;
$this->sshTimeout = getenv('LAGOON_OVERRIDE_SSH_TIMEOUT') ?: $this->sshTimeout;
$this->sshKey = getenv('LAGOON_SSH_KEY');
}
// Allow `.lagoon.yml` overrides.
$lagoonyml = $this->getLagoonYml();
$this->api = $lagoonyml['api'] ?? $this->api;
$this->endpoint = $lagoonyml['ssh'] ?? $this->endpoint;
$this->projectName = $lagoonyml['project'] ?? $this->projectName;
$this->sshTimeout = $lagoonyml['ssh_port_timeout'] ?? $this->sshTimeout;

// Allow environment variable overrides.
$this->api = getenv('LAGOON_OVERRIDE_API') ?: $this->api;
$this->endpoint = getenv('LAGOON_OVERRIDE_SSH') ?: $this->endpoint;
$this->projectName = getenv('LAGOON_PROJECT') ?: $this->projectName;
$this->sshTimeout = getenv('LAGOON_OVERRIDE_SSH_TIMEOUT') ?: $this->sshTimeout;
$this->sshKey = getenv('LAGOON_SSH_KEY');
$this->jwtToken = getenv('LAGOON_OVERRIDE_JWT_TOKEN');
}

/**
Expand All @@ -96,8 +94,8 @@ public function aliases() {
return;
}

if (empty($this->jwt_token)) {
$this->jwt_token = $this->getJwtToken();
if (empty($this->jwtToken)) {
$this->jwtToken = $this->getJwtToken();
}

$response = $this->getLagoonEnvs();
Expand Down Expand Up @@ -176,9 +174,19 @@ public function runRolloutTasks($stage = 'post') {
* Retrieves the contents of the sites .lagoon.yml file.
*/
public function getLagoonYml() {
$project_root = Drush::bootstrapManager()->getComposerRoot();
$lagoonyml_path = $project_root . "/.lagoon.yml";
return (file_exists($lagoonyml_path)) ? Yaml::parse(file_get_contents($lagoonyml_path)) : [];
try {
$project_root = Drush::bootstrapManager()->getComposerRoot();
$lagoonyml_path = $project_root . "/.lagoon.yml";
return (file_exists($lagoonyml_path)) ? Yaml::parse(file_get_contents($lagoonyml_path)) : [];
}
catch (\Throwable $e) {
if (empty(getenv("LAGOON"))) {
// We might not be in a Lagoon environment, fail silently.
return [];
}

throw $e;
}
}

/**
Expand Down Expand Up @@ -248,7 +256,7 @@ public function getLagoonEnvs() {
'base_uri' => $this->api,
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->jwt_token,
'Authorization' => 'Bearer ' . $this->jwtToken,
],
'body' => json_encode(['query' => $query]),
]);
Expand Down
Loading