forked from cb341/deploio-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/pg command #1
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
Open
lukasbischof
wants to merge
6
commits into
main
Choose a base branch
from
feature/pg-command
base: main
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6b54891
Listing of Postgres databases
lukasbischof bc825d3
Info command
lukasbischof 66b0bb6
Backups capture command
lukasbischof dcb2610
Backup download command
lukasbischof c1f8244
Completion
lukasbischof 770623b
tests
lukasbischof 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| module Deploio | ||
| module Commands | ||
| class PostgreSQL < Thor | ||
| include SharedOptions | ||
|
|
||
| namespace "pg" | ||
|
|
||
| class_option :json, type: :boolean, default: false, desc: "Output as JSON" | ||
|
|
||
| default_task :list | ||
|
|
||
| desc "list", "List all PostgreSQL databases" | ||
| def list | ||
| setup_options | ||
| raw_dbs = @nctl.get_all_pg_databases | ||
|
|
||
| if options[:json] | ||
| puts JSON.pretty_generate(raw_dbs) | ||
| return | ||
| end | ||
|
|
||
| if raw_dbs.empty? | ||
| Output.warning("No PostgreSQL databases found") unless merged_options[:dry_run] | ||
| return | ||
| end | ||
|
|
||
| resolver = PgDatabaseResolver.new(nctl_client: @nctl) | ||
|
|
||
| rows = raw_dbs.map do |pg| | ||
| kind = pg["kind"] || "" | ||
| metadata = pg["metadata"] || {} | ||
| spec = pg["spec"] || {} | ||
| for_provider = spec["forProvider"] || {} | ||
| version = for_provider["version"] | ||
| namespace = metadata["namespace"] || "" | ||
| name = metadata["name"] || "" | ||
|
|
||
| [ | ||
| resolver.short_name_for(namespace, name), | ||
| project_from_namespace(namespace, resolver.current_org), | ||
| presence(kind, default: "-"), | ||
| presence(version, default: "?") | ||
| ] | ||
| end | ||
|
|
||
| Output.table(rows, headers: ["NAME", "PROJECT", "KIND", "VERSION"]) | ||
| end | ||
|
|
||
| desc "info NAME", "Show PostgreSQL database details" | ||
| def info(name) | ||
| setup_options | ||
| resolver = PgDatabaseResolver.new(nctl_client: @nctl) | ||
| db_ref = resolver.resolve(database_name: name) | ||
| data = @nctl.get_pg_database(db_ref) | ||
|
|
||
| if options[:json] | ||
| puts JSON.pretty_generate(data) | ||
| return | ||
| end | ||
|
|
||
| kind = data["kind"] | ||
| metadata = data["metadata"] || {} | ||
| spec = data["spec"] || {} | ||
| for_provider = spec["forProvider"] || {} | ||
| status = data["status"] || {} | ||
| at_provider = status["atProvider"] || {} | ||
|
|
||
| Output.header("PostgreSQL Database: #{db_ref.full_name}") | ||
| puts | ||
|
|
||
| Output.header("General") | ||
| Output.table([ | ||
| ["Name", presence(metadata["name"])], | ||
| ["Project", presence(metadata["namespace"])], | ||
| ["Kind", presence(kind, default: "-")], | ||
| ["Version", presence(for_provider["version"], default: "?")], | ||
| ["FQDN", presence(at_provider["fqdn"])], | ||
| ["Size", presence(at_provider["size"], default: "-")] | ||
| ]) | ||
|
|
||
| puts | ||
|
|
||
| Output.header("Status") | ||
| conditions = status["conditions"] || [] | ||
| ready_condition = conditions.find { |c| c["type"] == "Ready" } | ||
| synced_condition = conditions.find { |c| c["type"] == "Synced" } | ||
|
|
||
| Output.table([ | ||
| ["Ready", presence(ready_condition&.dig("status"))], | ||
| ["Synced", presence(synced_condition&.dig("status"))] | ||
| ]) | ||
|
|
||
| if for_provider["allowedCIDRs"].is_a?(Array) | ||
| puts | ||
|
|
||
| Output.header("Access") | ||
| Output.table([ | ||
| ["Allowed CIDRs", for_provider["allowedCIDRs"].join(", ")] | ||
| ]) | ||
|
|
||
| puts | ||
|
|
||
| Output.header("SSH Keys") | ||
| for_provider["sshKeys"].each do |key| | ||
| puts "- #{key}" | ||
| end | ||
| end | ||
| rescue Deploio::Error => e | ||
| Output.error(e.message) | ||
| exit 1 | ||
| end | ||
|
|
||
| desc "backups COMMAND", "Manage PostgreSQL database backups" | ||
| subcommand "backups", Commands::PostgreSQLBackups | ||
|
|
||
| private | ||
|
|
||
| def presence(value, default: "-") | ||
| (value.nil? || value.to_s.empty?) ? default : value | ||
| end | ||
|
|
||
| def project_from_namespace(namespace, current_org) | ||
| if current_org && namespace.start_with?("#{current_org}-") | ||
| namespace.delete_prefix("#{current_org}-") | ||
| else | ||
| namespace | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| module Deploio | ||
| module Commands | ||
| class PostgreSQLBackups < Thor | ||
| include SharedOptions | ||
|
|
||
| namespace "pg:backups" | ||
|
|
||
| desc "capture NAME", "Capture a new backup for the specified PostgreSQL database" | ||
| def capture(name) | ||
| setup_options | ||
| resolver = PgDatabaseResolver.new(nctl_client: @nctl) | ||
| db_ref = resolver.resolve(database_name: name) | ||
| data = @nctl.get_pg_database(db_ref) | ||
| kind = data["kind"] || "" | ||
|
|
||
| unless kind == "Postgres" || @nctl.dry_run | ||
| Output.error("Backups can only be captured for PostgreSQL databases. (shared dbs are not supported)") | ||
| exit 1 | ||
| end | ||
|
|
||
| fqdn = data.dig("status", "atProvider", "fqdn") | ||
| if fqdn.nil? || fqdn.empty? | ||
| Output.error("Database FQDN not found; cannot capture backup.") | ||
| exit 1 | ||
| end | ||
|
|
||
| cmd = ["ssh", "dbadmin@#{fqdn}", "sudo nine-postgresql-backup"] | ||
| Output.command(cmd.join(" ")) | ||
| system(*cmd) unless @nctl.dry_run | ||
| end | ||
|
|
||
| desc "download NAME [--output destination_path]", "Download the latest backup for the specified PostgreSQL database instance" | ||
| method_option :output, type: :string, desc: "Output file path (defaults to current directory with auto-generated name)" | ||
| method_option :db_name, type: :string, desc: "If there are multiple DBs, specify which one to download the backup for", default: nil | ||
| def download(name) | ||
| destination = options[:output] || "./#{name}-latest-backup.zst" | ||
|
|
||
| setup_options | ||
| resolver = PgDatabaseResolver.new(nctl_client: @nctl) | ||
| db_ref = resolver.resolve(database_name: name) | ||
| data = @nctl.get_pg_database(db_ref) | ||
| kind = data["kind"] || "" | ||
|
|
||
| unless kind == "Postgres" || @nctl.dry_run | ||
| Output.error("Backups can only be downloaded for PostgreSQL databases. (shared dbs are not supported)") | ||
| exit 1 | ||
| end | ||
|
|
||
| databases = data.dig("status", "atProvider", "databases")&.keys || [] | ||
| databases.reject! { |db| db.strip.empty? } | ||
| if databases.empty? | ||
| Output.error("No databases found in PostgreSQL instance; cannot download backup.") | ||
| exit 1 | ||
| elsif databases.size > 1 && options[:db_name].nil? | ||
| Output.error("Multiple databases found in PostgreSQL instance") | ||
| Output.error("Databases: #{databases.join(", ")}") | ||
| Output.error("Please specify the database name using the --db_name option.") | ||
| exit 1 | ||
| end | ||
|
|
||
| db_name = options[:db_name] || databases.first | ||
|
|
||
| fqdn = data.dig("status", "atProvider", "fqdn") | ||
| if fqdn.nil? || fqdn.empty? | ||
| Output.error("Database FQDN not found; cannot download backup.") | ||
| exit 1 | ||
| end | ||
|
|
||
| cmd = ["rsync", "-avz", "dbadmin@#{fqdn}:~/backup/postgresql/latest/customer/#{db_name}/#{db_name}.zst", destination] | ||
| Output.command(cmd.join(" ")) | ||
| system(*cmd) unless @nctl.dry_run | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "did_you_mean" | ||
|
|
||
| module Deploio | ||
| class PgDatabaseRef | ||
| attr_reader :project_name, :database_name | ||
|
|
||
| # The input is given in the format "<project>-<database>" | ||
| def initialize(input, available_databases: {}) | ||
| @input = input.to_s | ||
| parse_from_available_databases(available_databases) | ||
| end | ||
|
|
||
| def full_name | ||
| "#{project_name}-#{database_name}" | ||
| end | ||
|
|
||
| def to_s | ||
| full_name | ||
| end | ||
|
|
||
| def ==(other) | ||
| return false unless other.is_a?(PgDatabaseRef) | ||
|
|
||
| project_name == other.project_name && database_name == other.database_name | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def parse_from_available_databases(available_databases) | ||
| if available_databases.key?(@input) | ||
| match = available_databases[@input] | ||
| @project_name = match[:project_name] | ||
| @database_name = match[:database_name] | ||
| return | ||
| end | ||
|
|
||
| # If available_databases provided but no match, raise error with suggestions | ||
| raise_not_found_error(@input, available_databases.keys) unless available_databases.empty? | ||
|
|
||
| raise_not_found_error(@input, []) | ||
| end | ||
|
|
||
| def raise_not_found_error(input, available_database_names) | ||
| message = "Database not found: '#{input}'" | ||
|
|
||
| suggestions = suggest_similar(input, available_database_names) | ||
| unless suggestions.empty? | ||
| message += "\n\nDid you mean?" | ||
| suggestions.each { |s| message += "\n #{s}" } | ||
| end | ||
|
|
||
| message += "\n\nRun 'deploio pg' to see available Postgres databases." | ||
|
|
||
| raise Deploio::PgDatabaseNotFoundError, message | ||
| end | ||
|
|
||
| def suggest_similar(input, dictionary) | ||
| return [] if dictionary.empty? | ||
|
|
||
| spell_checker = DidYouMean::SpellChecker.new(dictionary: dictionary) | ||
| spell_checker.correct(input) | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
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.
Not supported yet afaik