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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
036de4d
Listing of Postgres databases
lukasbischof e9aa83a
Info command
lukasbischof 45a8851
Backups capture command
lukasbischof 7fdeecc
Backup download command
lukasbischof e5f4495
Completion
lukasbischof f332c35
tests
lukasbischof e5e713d
fix standard
coorasse 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -32,7 +32,7 @@ def build_logs(build_name, app_ref: nil, tail: false, lines: 5000) | |||||
| if app_ref | ||||||
| args += ["--project", app_ref.project_name, "-a", app_ref.app_name] | ||||||
| end | ||||||
| exec_passthrough("logs", "build", *([build_name].compact), *args) | ||||||
| exec_passthrough("logs", "build", *[build_name].compact, *args) | ||||||
| end | ||||||
|
|
||||||
| def exec_command(app_ref, command) | ||||||
|
|
@@ -64,6 +64,42 @@ def get_app(app_ref) | |||||
| {} | ||||||
| end | ||||||
|
|
||||||
| def get_all_pg_databases | ||||||
| output_dedicated_dbs = capture("get", "postgres", "-A", "-o", "json") | ||||||
| output_shared_dbs = capture("get", "postgresdatabase", "-A", "-o", "json") | ||||||
| if (output_dedicated_dbs.nil? || output_dedicated_dbs.empty?) && | ||||||
|
Member
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.
Suggested change
if it's |
||||||
| (output_shared_dbs.nil? || output_shared_dbs.empty?) | ||||||
| return [] | ||||||
| end | ||||||
|
|
||||||
| [ | ||||||
| *JSON.parse(output_dedicated_dbs), | ||||||
| *JSON.parse(output_shared_dbs) | ||||||
| ] | ||||||
| rescue JSON::ParserError | ||||||
| [] | ||||||
| end | ||||||
|
|
||||||
| def get_pg_database(db_ref) | ||||||
| output = begin | ||||||
| capture("get", "postgres", db_ref.database_name, | ||||||
| "--project", db_ref.project_name, "-o", "json") | ||||||
| rescue Deploio::NctlError | ||||||
| nil | ||||||
| end | ||||||
|
|
||||||
| if output.nil? || output.empty? | ||||||
| output = capture("get", "postgresdatabase", db_ref.database_name, | ||||||
| "--project", db_ref.project_name, "-o", "json") | ||||||
| end | ||||||
|
|
||||||
| return nil if output.nil? || output.empty? | ||||||
|
|
||||||
| JSON.parse(output) | ||||||
| rescue JSON::ParserError | ||||||
| nil | ||||||
| end | ||||||
|
|
||||||
| def get_apps_by_project(project) | ||||||
| output = capture("get", "apps", "--project", project, "-o", "json") | ||||||
| return [] if output.nil? || output.empty? | ||||||
|
|
@@ -287,6 +323,7 @@ def capture(*args) | |||||
| Output.command(cmd.join(" ")) | ||||||
| "" | ||||||
| else | ||||||
| puts "> #{cmd.join(" ")}" if ENV["DEPLOIO_DEBUG"] | ||||||
| stdout, stderr, status = Open3.capture3(*cmd) | ||||||
| unless status.success? | ||||||
| raise Deploio::NctlError, "nctl command failed: #{stderr}" | ||||||
|
|
||||||
Oops, something went wrong.
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