-
Notifications
You must be signed in to change notification settings - Fork 4
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
bomoko
wants to merge
14
commits into
master
Choose a base branch
from
feature/generate_alias_file
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9543cb7
Adds generate alias file command
bomoko 0a16652
Adds some debugging and file output option
bomoko 1a12df8
Fixing phpcs errors
bomoko 0cce42b
fixes phpcs errors and changes references in graphql
bomoko e92abec
Fixing phpcs issues
bomoko b7f413d
phpcs errors
bomoko b5ffe77
Merge branch 'main' into feature/generate_alias_file
tobybellwood 277c4b9
use kubernetesNamespaceName for la returns too
tobybellwood fb9bfd0
Adds fallbacks for lagoon alias file generation
bomoko 1e310ed
Merge branch 'feature/generate_alias_file' of github.com:amazeeio/dru…
bomoko fa5cf9c
Makes tty a bool
bomoko 47a1dfc
Adds env for config api host
bomoko b5ce1f2
Fixes correct operator
bomoko 4997d43
Uses fully qualified name for env name
bomoko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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; | ||||||
|
||||||
/** | ||||||
* Lagoon API endpoint. | ||||||
* | ||||||
|
@@ -67,11 +77,10 @@ public function __construct() { | |||||
// Get default config. | ||||||
$lagoonyml = $this->getLagoonYml(); | ||||||
$this->api = $lagoonyml['api'] ?? 'https://api.lagoon.amazeeio.cloud/graphql'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to accommodate uselagoon/build-deploy-tool#193
Suggested change
|
||||||
$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; | ||||||
|
@@ -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) { | ||||||
|
@@ -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. | ||||||
* | ||||||
|
@@ -234,7 +309,11 @@ public function getLagoonEnvs() { | |||||
standbyAlias, | ||||||
environments { | ||||||
name, | ||||||
openshiftProjectName | ||||||
kubernetesNamespaceName, | ||||||
kubernetes { | ||||||
sshHost, | ||||||
sshPort | ||||||
} | ||||||
} | ||||||
} | ||||||
}', $this->projectName); | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?