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

Anonymous FTP upload #65

Merged
merged 3 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
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
71 changes: 56 additions & 15 deletions lib/manageiq/appliance_console/database_admin.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
require 'manageiq/appliance_console/errors'
require 'uri'

module ManageIQ
module ApplianceConsole
class DatabaseAdmin < HighLine
include ManageIQ::ApplianceConsole::Prompts

LOCAL_FILE = "Local file".freeze
NFS_FILE = "Network File System (NFS)".freeze
SMB_FILE = "Samba (SMB)".freeze
S3_FILE = "Amazon S3 (S3)".freeze
FTP_FILE = "File Transfer Protocol (FTP)".freeze
FILE_OPTIONS = [LOCAL_FILE, NFS_FILE, SMB_FILE, S3_FILE, FTP_FILE, CANCEL].freeze
LOCAL_FILE = "local".freeze
NFS_FILE = "nfs".freeze
SMB_FILE = "smb".freeze
S3_FILE = "s3".freeze
FTP_FILE = "ftp".freeze

DB_RESTORE_FILE = "/tmp/evm_db.backup".freeze
DB_DEFAULT_DUMP_FILE = "/tmp/evm_db.dump".freeze
Expand Down Expand Up @@ -55,13 +55,14 @@ def activate
end

def ask_file_location
case @backup_type = ask_with_menu(*file_menu_args)
when LOCAL_FILE then ask_local_file_options
when NFS_FILE then ask_nfs_file_options
when SMB_FILE then ask_smb_file_options
when S3_FILE then ask_s3_file_options
when FTP_FILE then ask_ftp_file_options
when CANCEL then raise MiqSignalError
@backup_type = ask_with_menu(*file_menu_args) do |menu|
menu.choice(CANCEL) { |_| raise MiqSignalError }
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this is something we should just add to the end of every menu? Unlikely that it should be added to this PR, but this made me think of it.

Copy link
Member Author

Choose a reason for hiding this comment

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

@kbrock When I was typing this I was thinking of modifying ask_with_menu to add that as an option

Copy link
Member

Choose a reason for hiding this comment

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

Maybe just make an issue for this? I agree, that would definitely make this cleaner. Maybe also have it as an option to opt of ( something like ask_with_menu(*file_menu_args, :cancel_option => false) maybe?).

...which reminds me... I really need to add an issue for the "UI stuff" I have wanted to refactor in this...

end
if URI(backup_type).scheme
ask_custom_file_options(backup_type)
else
# calling methods like ask_ftp_file_options and ask_s3_file_options
send("ask_#{backup_type}_file_options")
end
end

Expand Down Expand Up @@ -138,6 +139,20 @@ def ask_ftp_file_options
@task_params = ["--", params]
end

def ask_custom_file_options(server_uri)
@filename = just_ask(*filename_prompt_args) unless action == :restore
sample_case = server_uri.split("/").last
hostname = URI(server_uri).host
uri_filename = ask_custom_prompt(hostname, 'filename', "Target filename (e.g.: #{sample_case})")
@uri = server_uri.gsub(sample_case, uri_filename)

params = { :uri => uri }
params[:remote_file_name] = filename if filename

@task = "evm:db:#{action}:remote"
@task_params = ["--", params]
end

def ask_to_delete_backup_after_restore
if action == :restore && backup_type == LOCAL_FILE
say("The local database restore file is located at: '#{uri}'.\n")
Expand Down Expand Up @@ -181,14 +196,27 @@ def confirm_and_execute

def allowed_to_execute?
return true unless action == :restore

say("\nNote: A database restore cannot be undone. The restore will use the file: #{uri}.\n")
agree("Are you sure you would like to restore the database? (Y/N): ")
end

def file_options
@file_options ||= I18n.t("database_admin.menu_order").each_with_object({}) do |file_option, h|
# special anonymous ftp sites are defined by uri
uri = URI(file_option)
if uri.scheme
h["#{uri.scheme} to #{uri.host}"] = file_option
else
h[I18n.t("database_admin.#{file_option}")] = file_option
end
end
end

def file_menu_args
[
action == :restore ? "Restore Database File" : "#{action.capitalize} Output File Name",
FILE_OPTIONS,
file_options,
LOCAL_FILE,
nil
]
Expand All @@ -200,6 +228,15 @@ def setting_header

private

def ask_custom_prompt(type, prompt_name, default_prompt)
# type (domain name) has a period in it, so we need to look it up by [] instead of the traditional i18n method
prompts = I18n.t("database_admin.prompts", :default => nil).try(:[], type.to_sym)
prompt_text = prompts && prompts["#{prompt_name}_text".to_sym] || default_prompt
prompt_regex = prompts && prompts["#{prompt_name}_validator".to_sym]
validator = prompt_regex ? ->(x) { x.to_s =~ /#{prompt_regex}/ } : ->(x) { x.to_s.present? }
just_ask(prompt_text, nil, validator)
end

def should_exclude_tables?
ask_yn?("Would you like to exclude tables in the dump") do |q|
q.readline = true
Expand Down Expand Up @@ -232,10 +269,14 @@ def remote_file_prompt_args_for(remote_type)
else
"location to save the remote #{action} file to"
end
prompt += "\nExample: #{SAMPLE_URLS[remote_type]}"
prompt += "\nExample: #{sample_url(remote_type)}"
[prompt, remote_type]
end

def sample_url(scheme)
I18n.t("database_admin.sample_url.#{scheme}")
end

def processing_message
msg = if action == :restore
"\nRestoring the database..."
Expand Down
11 changes: 0 additions & 11 deletions lib/manageiq/appliance_console/prompts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ module Prompts
NONE_REGEXP = /^('?NONE'?)?$/i.freeze
HOSTNAME_REGEXP = /^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/

SAMPLE_URLS = {
'nfs' => 'nfs://host.mydomain.com/exported/my_exported_folder/db.backup',
'smb' => 'smb://host.mydomain.com/my_share/daily_backup/db.backup',
's3' => 's3://mybucket/my_subdirectory/daily_backup/db.backup',
'ftp' => 'ftp://host.mydomain.com/path/to/daily_backup/db.backup'
}

def sample_url(scheme)
SAMPLE_URLS[scheme]
end

def ask_for_uri(prompt, expected_scheme, opts = {})
require 'uri'
just_ask(prompt, nil, nil, 'a valid URI') do |q|
Expand Down
17 changes: 17 additions & 0 deletions locales/appliance/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@ en:
shutdown: Shut Down Appliance
summary: Summary Information
quit: Quit
database_admin:
menu_order:
- local
- nfs
- smb
- s3
- ftp
local: Local file
nfs: Network File System (NFS)
smb: Samba (SMB)
s3: Amazon S3 (S3)
ftp: File Transfer Protocol (FTP)
sample_url:
nfs: nfs://host.mydomain.com/exported/my_exported_folder/db.backup
smb: smb://host.mydomain.com/my_share/daily_backup/db.backup
s3: s3://mybucket/my_subdirectory/daily_backup/db.backup
ftp: ftp://host.mydomain.com/path/to/daily_backup/db.backup
Loading