From 611fe5f3634471b0cf988dd27dc86c71d7271a0c Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Tue, 28 Nov 2023 09:19:44 -0600 Subject: [PATCH] F OpenNebula/one#6059: Added QoL features for backup management (#2837) using the CLI --- share/shell/bash_completion | 9 ++++++--- src/cli/one_helper.rb | 10 ++++++++++ src/cli/one_helper/oneimage_helper.rb | 15 ++++++++++++--- src/cli/one_helper/onevm_helper.rb | 15 +++++++++++++++ src/cli/oneimage | 11 +++++++++++ src/cli/onevm | 23 +++++++++++++++++++++-- 6 files changed, 75 insertions(+), 8 deletions(-) diff --git a/share/shell/bash_completion b/share/shell/bash_completion index 7342e76ad0..9cd1356bf7 100755 --- a/share/shell/bash_completion +++ b/share/shell/bash_completion @@ -445,7 +445,7 @@ _oneimage() { fi opts="create clone delete persistent nonpersistent update enable chtype disable chgrp chown chmod rename snapshot-delete snapshot-revert - snapshot-flatten list show top lock unlock orphans dockerfile" + snapshot-flatten list show top lock unlock orphans dockerfile restore" cmd=oneimage if [ "$COMP_CWORD" == 1 ]; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) @@ -462,6 +462,9 @@ _oneimage() { disable) _complete $cmd $COL_NAME "stat=rdy" ;; + restore) + _complete $cmd $COL_NAME "type=BK" + ;; create) COMPREPLY=( $(compgen -A file -- "${cur}") ) return 0 @@ -740,7 +743,7 @@ _onevm() { rename snapshot-create snapshot-revert snapshot-delete disk-snapshot-create disk-snapshot-revert disk-snapshot-delete disk-snapshot-rename disk-resize list show top resize save updateconf lock unlock create-chart - delete-chart update-chart backup restore ssh port-forward" + delete-chart update-chart backup restore ssh port-forward backupmode" cmd=onevm if [ "$COMP_CWORD" == 1 ]; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) @@ -752,7 +755,7 @@ _onevm() { unresched|rename|snapshot-create|snapshot-revert|snapshot-delete| \ disk-snapshot-create|disk-snapshot-delete|disk-snapshot-revert| \ disk-snapshot-rename|disk-resize|save|updateconf|lock|unlock|create-chart| \ - delete-chart|update-chart|backup|restore|ssh|port-forward) + delete-chart|update-chart|backup|restore|ssh|port-forward|backupmode) _complete $cmd ;; undeploy|poweroff|reboot|shutdown|suspend|stop|migrate) diff --git a/src/cli/one_helper.rb b/src/cli/one_helper.rb index 7f9303ccdd..c36ebf1e64 100644 --- a/src/cli/one_helper.rb +++ b/src/cli/one_helper.rb @@ -593,6 +593,8 @@ Bash symbols must be escaped on STDIN passing' OPTIONS = FORMAT, EXTENDED, NUMERIC, KILOBYTES + BACKUP_MODES = %w[FULL INCREMENT] + class OneHelper attr_accessor :client @@ -717,6 +719,14 @@ Bash symbols must be escaped on STDIN passing' end end + def backup_mode_valid?(sus_backup_mode) + BACKUP_MODES.each do |backup_mode| + return true if backup_mode.casecmp?(sus_backup_mode) + end + + false + end + #----------------------------------------------------------------------- # List pool functions #----------------------------------------------------------------------- diff --git a/src/cli/one_helper/oneimage_helper.rb b/src/cli/one_helper/oneimage_helper.rb index 2ecfc82a4b..e37f2b33c6 100644 --- a/src/cli/one_helper/oneimage_helper.rb +++ b/src/cli/one_helper/oneimage_helper.rb @@ -173,6 +173,15 @@ class OneImageHelper < OpenNebulaHelper::OneHelper :format => String } + FILTERS = [ + { + :name => 'backup', + :large => '--backup', + :description => 'Show only backup type images', + :format => String + } + ] + def self.rname 'IMAGE' end @@ -263,13 +272,13 @@ class OneImageHelper < OpenNebulaHelper::OneHelper tmpl_pool.info pool.each do |img| + next if img['TYPE'].to_i == 6 # skip backup images + attrs = { :id => img['ID'], :name => img['NAME'], :uname => img['UNAME'] } - orphans << img['ID'] if check_orphan(tmpl_pool, - xpath, - 'IMAGE', attrs) + orphans << img['ID'] if check_orphan(tmpl_pool, xpath, 'IMAGE', attrs) end orphans diff --git a/src/cli/one_helper/onevm_helper.rb b/src/cli/one_helper/onevm_helper.rb index 88bfcfe190..4e6132bd66 100644 --- a/src/cli/one_helper/onevm_helper.rb +++ b/src/cli/one_helper/onevm_helper.rb @@ -639,6 +639,21 @@ class OneVMHelper < OpenNebulaHelper::OneHelper # rubocop:enable Style/SpecialGlobalVars end + def get_backup_mode(vm_id) + vm = retrieve_resource(vm_id) + + if vm.has_elements?('/VM/BACKUPS/BACKUP_CONFIG') + vm.template_like_str('BACKUPS/BACKUP_CONFIG/MODE') + else + nil + end + end + + def set_backup_mode(vm_ref, backup_mode) + vm = retrieve_resource(vm_ref) + vm.updateconf("BACKUP_CONFIG = [\"MODE\"=\"#{backup_mode}\"]") + end + private def factory(id = nil) diff --git a/src/cli/oneimage b/src/cli/oneimage index bbe7c43db0..759c17e96a 100755 --- a/src/cli/oneimage +++ b/src/cli/oneimage @@ -130,6 +130,7 @@ CommandParser::CmdParser.new(ARGV) do list_options += OpenNebulaHelper::FORMAT list_options << OpenNebulaHelper::NUMERIC list_options << OpenNebulaHelper::DESCRIBE + list_options << OneImageHelper::FILTERS CREATE_OPTIONS = [OneDatastoreHelper::DATASTORE, OneImageHelper::IMAGE, @@ -487,6 +488,16 @@ CommandParser::CmdParser.new(ARGV) do EOT command :list, list_desc, [:filterflag, nil], :options => list_options do + if options.key?(:backup) + filter = 'TYPE=BK' + + if options[:filter] + options[:filter] << filter + else + options[:filter] = [filter] + end + options.delete(:backup) + end helper.list_pool(options, false, args[0]) end diff --git a/src/cli/onevm b/src/cli/onevm index 3710e49cf2..21f4f2e2c5 100755 --- a/src/cli/onevm +++ b/src/cli/onevm @@ -1325,6 +1325,26 @@ CommandParser::CmdParser.new(ARGV) do end end + backupmode_set_desc = <<-EOT.unindent + Updates the backup mode of a VM. Can be #{OpenNebulaHelper::BACKUP_MODES.join('|')} + EOT + + command :backupmode, backupmode_set_desc, :vmid, :mode do + vm_ref = args[0] + backup_mode = args[1] + + if !helper.backup_mode_valid?(backup_mode) + err_msg = "Invalid backup mode: #{backup_mode}\n" + err_msg << "Valid backup modes are: #{OpenNebulaHelper::BACKUP_MODES.join(',')}" + + STDERR.puts err_msg + exit(-1) + end + + # TODO: Ensure other BACKUP_CONFIG is not overwritten + helper.set_backup_mode(vm_ref, backup_mode) + end + updateconf_desc = <<-EOT.unindent Updates the configuration of a VM. Valid states are: running, pending, failure, poweroff, undeploy, hold or cloning. @@ -1362,8 +1382,7 @@ CommandParser::CmdParser.new(ARGV) do exit(-1) end - helper.perform_action(args[0], options, - 'Updating VM configuration') do |vm| + helper.perform_action(args[0], options, 'Updating VM configuration') do |vm| if template.empty? rc = vm.info