#!/usr/bin/env ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2021, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

ONE_LOCATION = ENV['ONE_LOCATION']

if !ONE_LOCATION
    RUBY_LIB_LOCATION = '/usr/lib/one/ruby'
    GEMS_LOCATION     = '/usr/share/one/gems'
else
    RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby'
    GEMS_LOCATION     = ONE_LOCATION + '/share/gems'
end

# %%RUBYGEMS_SETUP_BEGIN%%
if File.directory?(GEMS_LOCATION)
    real_gems_path = File.realpath(GEMS_LOCATION)
    if !defined?(Gem) || Gem.path != [real_gems_path]
        $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }

        # Suppress warnings from Rubygems
        # https://github.com/OpenNebula/one/issues/5379
        begin
            verb = $VERBOSE
            $VERBOSE = nil
            require 'rubygems'
            Gem.use_paths(real_gems_path)
        ensure
            $VERBOSE = verb
        end
    end
end
# %%RUBYGEMS_SETUP_END%%

$LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << RUBY_LIB_LOCATION + '/cli'

require 'command_parser'
require 'one_helper/onevm_helper'
require 'one_helper/onedatastore_helper'
require 'opennebula/virtual_machine_ext'

#rbt: add require 'securerandom'
require 'securerandom'

CommandParser::CmdParser.new(ARGV) do
    usage '`onevm` <command> [<args>] [<options>]'
    version OpenNebulaHelper::ONE_VERSION

    helper = OneVMHelper.new

    # rbt: scheduling only allowed for backups BREST-3771
    brest_mode = ENV["ONE_BREST_MODE"]
    if brest_mode == "1"
        schedule_opts = []
    else
        schedule_opts = [
            OneVMHelper::SCHEDULE,
            OneVMHelper::WEEKLY,
            OneVMHelper::MONTHLY,
            OneVMHelper::YEARLY,
            OneVMHelper::HOURLY,
            OneVMHelper::END_TIME
        ]
    end

    before_proc do
        #rbt: add generate unique key, user and serviceuser
        unless ENV['USER'].nil?
            options[:session]={"cmd_session_id"=>SecureRandom.uuid, "service_user_vm"=>false, "cmd_username"=>ENV['USER']}
        end

        helper.set_client(options)
    end

    TYPE = {
        :name => 'type',
        :short => '-t type',
        :large => '--type type',
        :format => String,
        :description => 'Type of the new Image'
    }

    TARGET = {
        :name => 'target',
        :short => '-t target',
        :large => '--target target',
        :format => String,
        :description => 'Device where the image will be attached'
    }

    PREFIX = {
        :name => 'prefix',
        :large => '--prefix prefix',
        :format => String,
        :description => 'Overrides the DEV_PREFIX of the image'
    }

    CACHE = {
        :name => 'cache',
        :large => '--cache cache_mode',
        :format => String,
        :description => 'Hypervisor cache mode: default, none, writethrough,'\
                        ' writeback, directsync or unsafe. (Only KVM driver)'
    }

    DISCARD = {
        :name => 'discard',
        :large => '--discard discard_mode',
        :format => String,
        :description => 'Hypervisor discard mode: ignore or unmap.'\
                        ' (Only KVM driver)'
    }

    ENFORCE = {
        :name => 'enforce',
        :short => '-e',
        :large => '--enforce',
        :description => 'Enforce that the host capacity is not exceeded'
    }

    SUCCESS = {
        :name => 'success',
        :large => '--success',
        :description => 'Recover a VM by succeeding the pending action'
    }

    FAILURE = {
        :name => 'failure',
        :large => '--failure',
        :description => 'Recover a VM by failing the pending action'
    }

    RETRY = {
        :name => 'retry',
        :large => '--retry',
        :description => 'Recover a VM by retrying the last failed action'
    }

    DELETE = {
        :name => 'delete',
        :large => '--delete',
        :description => 'No recover action possible, delete the VM'
    }

    RECREATE = {
        :name => 'recreate',
        :large => '--recreate',
        :description => 'No recover action possible, delete and recreate the VM'
    }

    DELETE_DB = {
        :name => 'deletedb',
        :large => '--delete-db',
        :description => 'No recover action possible, delete the VM from the '\
                        'DB. It does not trigger any action on the hypervisor'
    }

    INTERACTIVE = {
        :name => 'interactive',
        :large => '--interactive',
        :description => 'Enables interactive recovery. Only works alongside '\
                        ' the --retry option.'
    }

    SNAP = {
        :name => 'snapshot',
        :short => '-s snapshot',
        :large => '--snapshot snapshot',
        :format => String,
        :description => 'ID of the Snapshot to save.'
    }

    PERSISTENT = {
        :name => 'persistent',
        :large => '--persistent',
        :description => 'Make the new images persistent'
    }

    USE = {
        :name => 'use',
        :large => '--use',
        :description => 'lock use actions'
    }

    MANAGE = {
        :name => 'manage',
        :large => '--manage',
        :description => 'lock manage actions'
    }

    ADMIN = {
        :name => 'admin',
        :large => '--admin',
        :description => 'lock admin actions'
    }

    ALL = {
        :name => 'all',
        :large => '--all',
        :description => 'lock all actions'
    }

    LOGGER = {
        :name   => 'logger',
        :large  => '--logger logger',
        :format => String,
        :description => 'Set logger to STDOUT or FILE'
    }

    KEEP = {
        :name  => 'keep',
        :large => '--keep-backup',
        :description => 'Keep previous backup when creating a new one'
    }

    ONESHOT = {
        :name  => 'oneshot',
        :large => '--oneshot',
        :description => 'Take an snapshot of the VM without saving backup info'
    }

    MARKET = {
        :name   => 'market',
        :large  => '--market market_id',
        :format => Integer,
        :description => 'Market to save oneshot'
    }

    NIC_ID = {
        :name   => 'nic_id',
        :large  => '--nic-id nic_id',
        :format => String,
        :description => 'NIC to use when SSH'
    }

    CMD = {
        :name   => 'cmd',
        :large  => '--cmd cmd',
        :format => String,
        :description => 'CMD to run when SSH'
    }

    SSH_OPTS = {
        :name   => 'ssh_opts',
        :large  => '--ssh-options options',
        :format => String,
        :description => 'SSH options to use'
    }

    ########################################################################
    # Global Options
    ########################################################################
    set :option, CommandParser::OPTIONS + OpenNebulaHelper::CLIENT_OPTIONS

    ########################################################################
    # Formatters for arguments
    ########################################################################
    set :format, :hostid, OpenNebulaHelper.rname_to_id_desc('HOST') do |arg|
        OpenNebulaHelper.rname_to_id(arg, 'HOST')
    end

    set :format, :groupid, OpenNebulaHelper.rname_to_id_desc('GROUP') do |arg|
        OpenNebulaHelper.rname_to_id(arg, 'GROUP')
    end

    set :format, :userid, OpenNebulaHelper.rname_to_id_desc('USER') do |arg|
        OpenNebulaHelper.rname_to_id(arg, 'USER')
    end

    set :format,
        :datastoreid,
        OpenNebulaHelper.rname_to_id_desc('DATASTORE') do |arg|
        OpenNebulaHelper.rname_to_id(arg, 'DATASTORE')
    end

    set :format, :vmid, OneVMHelper.to_id_desc do |arg|
        tmp = helper.to_id(arg)
        @current_vm = tmp[1]
        tmp
    end

    set :format, :vmid_list, OneVMHelper.list_to_id_desc do |arg|
        helper.list_to_id(arg)
    end

    set :format, :filterflag, OneVMHelper.filterflag_to_i_desc do |arg|
        helper.filterflag_to_i(arg)
    end

    set :format, :diskid, 'Integer' do |arg|
        format_int(arg)
    end

    set :format, :size, 'Disk size in MiB' do |arg|
        OpenNebulaHelper.size_in_mb(arg)
    end

    format :snapshot_id, 'Snapshot identifier' do |arg|
        helper.retrieve_snapshot_id(@current_vm, arg)
    end

    format :disk_snapshot_id, 'Disk_snapshot identifier' do |arg|
        helper.retrieve_disk_snapshot_id(@current_vm, arg)
    end

    ########################################################################
    # Commands
    ########################################################################

    create_desc = <<-EOT.unindent
        Creates a new VM from the given description instead of using a
        previously defined template (see 'onetemplate create' and
        'onetemplate instantiate').

        Examples:

          - using a template description file:

            onevm create vm_description.tmpl

          - new VM named "arch vm" with a disk and a nic

            onevm create --name "arch vm" --memory 128 --cpu 1 --disk arch \\
                         --network private_lan

          - a vm with two disks

            onevm create --name "test vm" --memory 128 --cpu 1 --disk arch,data

    EOT

    command :create, create_desc, [:file, nil], :options =>
            [OneVMHelper::MULTIPLE, OneVMHelper::HOLD] +
            OpenNebulaHelper::TEMPLATE_OPTIONS_VM do
        number    = options[:multiple] || 1
        exit_code = nil

        if args[0] && OpenNebulaHelper.create_template_options_used?(options)
            STDERR.puts 'You can not use both template file and template'\
                        ' creation options.'
            exit(-1)
        end

        begin
            if args[0]
                template = File.read(args[0])
                template.force_encoding('utf-8')
            else
                res = OpenNebulaHelper.create_template(options)

                if res.first != 0
                    STDERR.puts res.last
                    exit(-1)
                end

                template = res.last
            end
        rescue StandardError
            STDERR.puts 'Error reading template.'
            next -1
        end

        if options[:dry]
            puts template
            exit 0
        end

        on_hold = !options[:hold].nil?

        number.times do
            exit_code = helper.create_resource(options) do |vm|
                vm.allocate(template, on_hold)
            end

            break if exit_code == -1
        end

        exit_code
    end

    #rbt: vgpu
    attach_vgpu_desc = <<-EOT.unindent
        Attaches a VGPU to a VM.
        You can specify the VGPU device with --vgpu_pool, --vgpu_profile and --vgpu_vfs.
        States: POWEROFF
    EOT

    command :'attach-vgpu', attach_vgpu_desc, :vmid,
            :options => [
                OneVMHelper::FILE,
                OneVMHelper::VGPU_POOL,
                OneVMHelper::VGPU_PROFILE,
                OneVMHelper::VGPU_VFS
            ] do


        vgpu_attrs = [:vgpu_pool, :vgpu_profile, :vgpu_vfs].any? do |o|
            !options[o].nil?
        end

        if options[:file]
            template = File.read(options[:file])
        elsif vgpu_attrs
            pool = options[:vgpu_pool]
            profile = options[:vgpu_profile]
            vfs = options[:vgpu_vfs]

            vgpu = []
            vgpu << "POOL    = \"#{pool}\"" if pool
            vgpu << "PROFILE = \"#{profile}\"" if profile
            vgpu << "VFS     = \"#{vfs}\"" if vfs

            template = "VGPU = [ #{vgpu.join(',')} ]"
        else
            STDERR.puts 'Provide a VGPU description file with --file or the relevant vgpu options:'
            exit(-1)
        end

        helper.perform_action(args[0], options, 'Attaching VGPU device') do |vm|
            vm.vgpu_attach(template)
        end
    end

    detach_vgpu_desc = <<-EOT.unindent
        Detaches a VGPU device from a VM
        States: POWEROFF
    EOT

    command :'detach-vgpu', detach_vgpu_desc, :vmid, :vgpu_pool do
        pool = args[1]

        helper.perform_action(args[0], options, 'Detaching VGPU') do |vm|
            vm.vgpu_detach(pool)
        end
    end

    update_desc = <<-EOT.unindent
        Update the user template contents. If a path is not provided the
        editor will be launched to modify the current content.
    EOT

    command :update, update_desc, :vmid, [:file, nil],
            :options => OpenNebulaHelper::APPEND do
        helper.perform_action(args[0], options, 'modified') do |obj|
            if options[:append]
                str = OpenNebulaHelper
                      .append_template(args[0], obj, args[1], 'USER_TEMPLATE')
            else
                str = OpenNebulaHelper
                      .update_template(args[0], obj, args[1], 'USER_TEMPLATE')
            end

            helper.set_client(options)
            obj = helper.retrieve_resource(obj.id)

            obj.update(str, options[:append])
        end
    end

    hold_desc = <<-EOT.unindent
        Sets the given VM on hold. A VM on hold is not scheduled until it is
        released. It can be, however, deployed manually; see 'onevm deploy'

        States: PENDING
    EOT

    command :hold, hold_desc, [:range, :vmid_list],
            :options => [*schedule_opts] do
        if !options[:schedule].nil?
            helper.schedule_actions(args[0], options, @comm_name)
        else
            helper.perform_actions(args[0], options, 'put on hold') do |vm|
                vm.hold
            end
        end
    end

    release_desc = <<-EOT.unindent
        Releases a VM on hold. See 'onevm hold'

        States: HOLD
    EOT

    command :release, release_desc, [:range, :vmid_list],
            :options => [*schedule_opts] do
        if !options[:schedule].nil?
            helper.schedule_actions(args[0], options, @comm_name)
        else
            helper.perform_actions(args[0], options, 'released') do |vm|
                vm.release
            end
        end
    end

    disk_saveas_desc = <<-EOT.unindent
        Saves the specified VM disk as a new Image. The Image is
        created immediately, and the contents of the VM disk will be saved to
        it.

        States: ANY
    EOT

    command :"disk-saveas", disk_saveas_desc, :vmid, :diskid, :img_name,
            :options => [TYPE, SNAP] do
        disk_id     = args[1].to_i
        image_name  = args[2]
        image_type  = options[:type] || ''
        snapshot_id = options[:snapshot]

        if snapshot_id.nil? || snapshot_id.empty?
            snapshot_id = -1

            verbose = "disk #{disk_id} prepared to be saved in " \
                      "the image #{image_name}"
        else
            snapshot_id = snapshot_id.to_i

            verbose = "disk #{disk_id} snapshot #{snapshot_id} prepared to " \
                      "be saved in the image #{image_name}"
        end

        helper.perform_action(args[0], options, verbose) do |vm|
            res = vm.disk_saveas(disk_id, image_name, image_type, snapshot_id)

            if !OpenNebula.is_error?(res)
                puts "Image ID: #{res}"
            end

            res
        end
    end

    terminate_desc = <<-EOT.unindent
        Terminates the given VM. The VM life cycle will end.

        With --hard it unplugs the VM.

        States: valid if no operation is being performed on the VM
    EOT

    command :terminate, terminate_desc, [:range, :vmid_list],
            :options => [*schedule_opts,
                         OneVMHelper::HARD] do
        command_name = 'terminate'
        command_name << '-hard' if options[:hard]

        if !options[:schedule].nil?
            helper.schedule_actions(args[0], options, command_name)
        else
            helper.perform_actions(args[0], options, 'terminated') do |vm|
                helper.get_vm_service_status(vm, options) #rbt: receive service_user_vm
                vm.terminate(options[:hard] == true)
            end
        end
    end

    undeploy_desc = <<-EOT.unindent
        Shuts down the given VM. The VM is saved in the system Datastore.

        With --hard it unplugs the VM.

        States: RUNNING
    EOT

    command :undeploy, undeploy_desc, [:range, :vmid_list],
            :options => [*schedule_opts,
                         OneVMHelper::HARD] do
        command_name = 'undeploy'
        command_name << '-hard' if options[:hard]

        if !options[:schedule].nil?
            helper.schedule_actions(args[0], options, command_name)
        else
            helper.perform_actions(args[0], options, 'shutting down') do |vm|
                helper.get_vm_service_status(vm, options) #rbt: receive service_user_vm
                vm.undeploy(options[:hard] == true)
            end
        end
    end

    poweroff_desc = <<-EOT.unindent
        Powers off the given VM. The VM will remain in the poweroff state, and
        can be powered on with the 'onevm resume' command.

        States: RUNNING
    EOT

    command :poweroff, poweroff_desc, [:range, :vmid_list],
            :options => [*schedule_opts,
                         OneVMHelper::HARD] do
        command_name = 'poweroff'
        command_name << '-hard' if options[:hard]

        if !options[:schedule].nil?
            helper.schedule_actions(args[0], options, @comm_name)
        else
            helper.perform_actions(args[0], options, 'shutting down') do |vm|
                helper.get_vm_service_status(vm, options) #rbt: receive service_user_vm
                vm.poweroff(options[:hard] == true)
            end
        end
    end

    reboot_desc = <<-EOT.unindent
        Reboots the given VM, this is equivalent to execute the reboot command
        from the VM console.

        The VM will be ungracefully rebooted if --hard is used.

        States: RUNNING
    EOT

    command :reboot, reboot_desc, [:range, :vmid_list],
            :options => [*schedule_opts,
                         OneVMHelper::HARD] do
        command_name = 'reboot'
        command_name << '-hard' if options[:hard]

        if !options[:schedule].nil?
            helper.schedule_actions(args[0], options, command_name)
        else
            helper.perform_actions(args[0], options, 'rebooting') do |vm|
                helper.get_vm_service_status(vm, options) #rbt: receive service_user_vm
                vm.reboot(options[:hard])
            end
        end
    end

    deploy_desc = <<-EOT.unindent
        Deploys the given VM in the specified Host. This command forces the
        deployment, in a standard installation the Scheduler is in charge
        of this decision

        States: PENDING, HOLD, STOPPED, UNDEPLOYED
    EOT

    command :deploy,
            deploy_desc,
            [:range, :vmid_list],
            :hostid,
            [:datastoreid, nil],
            :options => [ENFORCE, OneVMHelper::FILE] do
        host_id = args[1]
        verbose = "deploying in host #{host_id}"

        options[:enforce].nil? ? enforce = false : enforce = options[:enforce]
        args[2].nil? ? ds_id = -1 : ds_id = args[2]

        extra_template = nil

        if options[:file]
            extra_template = File.read(options[:file])
        end

        helper.perform_actions(args[0], options, verbose) do |vm|
            helper.get_vm_service_status(vm, options) #rbt: receive service_user_vm
            vm.deploy(host_id, enforce, ds_id, extra_template)
        end
    end

    migrate_desc = <<-EOT.unindent
        Migrates the given running VM to another Host. If used with --live
        parameter the miration is done without downtime.

        States: RUNNING
    EOT

    command :migrate, migrate_desc, [:range, :vmid_list], :hostid,
            [:datastoreid, nil], :options => [ENFORCE,
                                              OneVMHelper::LIVE,
                                              OneVMHelper::POFF,
                                              OneVMHelper::POFFHARD] do
        host_id = args[1]
        verbose = "migrating to #{host_id}"

        args[2].nil? ? ds_id = -1 : ds_id = args[2]

        helper.perform_actions(args[0], options, verbose) do |vm|
            helper.get_vm_service_status(vm, options) #rbt: receive service_user_vm
            vm.migrate(host_id,
                       options[:live] == true,
                       options[:enforce] == true,
                       ds_id,
                       helper.get_migration_type(options))
        end
    end

    stop_desc = <<-EOT.unindent
        Stops a running VM. The VM state is saved and transferred back to the
        front-end along with the disk files

        States: RUNNING
    EOT

    command :stop, stop_desc, [:range, :vmid_list],
            :options => [*schedule_opts] do
        if !options[:schedule].nil?
            helper.schedule_actions(args[0], options, @comm_name)
        else
            helper.perform_actions(args[0], options, 'stopping') do |vm|
                helper.get_vm_service_status(vm, options) #rbt: receive service_user_vm
                vm.stop
            end
        end
    end

    suspend_desc = <<-EOT.unindent
        Saves a running VM. It is the same as 'onevm stop', but the files
        are left in the remote machine to later restart the VM there
        (i.e. the resources are not freed and there is no need to
        re-schedule the VM).

        States: RUNNING
    EOT

    command :suspend, suspend_desc, [:range, :vmid_list],
            :options => [*schedule_opts] do
        if !options[:schedule].nil?
            helper.schedule_actions(args[0], options, @comm_name)
        else
            helper.perform_actions(args[0], options, 'suspending') do |vm|
                helper.get_vm_service_status(vm, options) #rbt: receive service_user_vm
                vm.suspend
            end
        end
    end

    resume_desc = <<-EOT.unindent
        Resumes the execution of the a saved VM

        States: STOPPED, SUSPENDED, UNDEPLOYED, POWEROFF, UNKNOWN
    EOT

    command :resume, resume_desc, [:range, :vmid_list],
            :options => [*schedule_opts] do
        if !options[:schedule].nil?
            helper.schedule_actions(args[0], options, @comm_name)
        else
            helper.perform_actions(args[0], options, 'resuming') do |vm|
                helper.get_vm_service_status(vm, options) #rbt: receive service_user_vm
                vm.resume
            end
        end
    end

    recover_desc = <<-EOT.unindent
        Recovers a stuck VM that is waiting for a driver operation. The recovery
        may be done by failing, succeeding or retrying the current operation.
        YOU NEED TO MANUALLY CHECK THE VM STATUS ON THE HOST, to decide if the
        operation was successful or not, or if it can be retried.

        Example: A VM is stuck in "migrate" because of a hardware failure. You
        need to check if the VM is running in the new host or not to recover
        the vm with --success or --failure, respectively.

        States for success/failure recovers: Any ACTIVE state.
        States for a retry recover: Any *FAILURE state
        States for delete: Any
        States for delete-recreate: Any but STOP/UNDEPLOYED
        States for delete-db: Any
    EOT

    command :recover, recover_desc, [:range, :vmid_list],
            :options => [SUCCESS, FAILURE, RETRY, INTERACTIVE,
                         DELETE, RECREATE, DELETE_DB] do
        if !options[:success].nil?
            result = 1
        elsif !options[:failure].nil?
            result = 0
        elsif !options[:retry].nil?
            result = 2
        elsif !options[:delete].nil?
            result = 3
        elsif !options[:recreate].nil?
            result = 4
        elsif !options[:deletedb].nil?
            result = 5
        else
            error_message = <<-EOT.unindent
            Need to specify the result of the pending action.
            \t--success recover the VM by succeeding the missing action.
            \t--failure recover the VM by failing the missing action.
            \t--retry recover the VM by retrying the last failed action.
            \t--delete no recover possible, delete the VM.
            \t--recreate no recover possible, delete and recreate the VM.
            \t--delete-db no recover possible, delete the VM from the DB. No action performed on the hypervisor
            EOT

            STDERR.puts error_message
            exit(-1)
        end

        helper.perform_actions(args[0], options, 'recovering') do |vm|
            if options[:retry] && options[:interactive]
                helper.recover_retry_interactive(vm)
            else
                vm.recover(result)
            end
        end
    end

    disk_attach_desc = <<-EOT.unindent
        Attaches a disk to a running VM. When using --file add only one
        DISK instance.

        States: RUNNING, POWEROFF
    EOT

    command :"disk-attach", disk_attach_desc, :vmid,
            :options => [OneVMHelper::FILE, OneVMHelper::IMAGE,
                         TARGET, CACHE, DISCARD, PREFIX] do
        if options[:file].nil? && options[:image].nil?
            STDERR.puts 'Provide a template file or an image:'
            STDERR.puts '\t--file  <file>'
            STDERR.puts '\t--image <image>'
            exit(-1)
        end

        if options[:file]
            template = File.read(options[:file])
        else
            image_id = options[:image]
            target   = options[:target]
            prefix   = options[:prefix]

            if target
                template =
                    "DISK = [ IMAGE_ID = #{image_id}, TARGET = #{target}"
            else
                template = "DISK = [ IMAGE_ID = #{image_id}"
                template << ", DEV_PREFIX = #{prefix}" if prefix
            end

            if options[:cache]
                template << ", CACHE = \"#{options[:cache]}\""
            end

            if options[:discard]
                template << ", DISCARD = \"#{options[:discard]}\""
            end

            template << ' ]'
        end

        helper.perform_action(args[0], options, 'Attach disk') do |vm|
            vm.disk_attach(template)
        end
    end

    disk_detach_desc = <<-EOT.unindent
        Detaches a disk from a running VM

        States: RUNNING, POWEROFF
    EOT

    command :"disk-detach", disk_detach_desc, :vmid, :diskid do
        diskid = args[1].to_i

        helper.perform_action(args[0], options, 'Detach disk') do |vm|
            vm.disk_detach(diskid)
        end
    end

    nic_attach_desc = <<-EOT.unindent
        Attaches a NIC to a running VM. When using --file add only one
        NIC instance.

        To attach a nic alias, use --file or --alias option.

        States: RUNNING, POWEROFF
    EOT

    command :"nic-attach", nic_attach_desc, :vmid,
            :options => [OneVMHelper::FILE,
                         OneVMHelper::NETWORK,
                         OneVMHelper::IP,
                         OneVMHelper::ALIAS,
                         OneVMHelper::NIC_NAME] do
        if options[:file].nil? && options[:network].nil?
            STDERR.puts 'Provide a template file or a network:'
            STDERR.puts "\t--file    <file>"
            STDERR.puts "\t--network <network>"
            exit(-1)
        end

        if options[:file]
            template = File.read(options[:file])
        else
            network_id = options[:network]
            ip = options[:ip]
            nic_alias = options[:alias]
            nic_name = options[:nic_name]

            if ip
                if !nic_alias && !nic_name
                    template = "NIC = [ NETWORK_ID = #{network_id}, \
                                IP = #{ip} ]"
                elsif !nic_alias && nic_name
                    template = "NIC = [ NETWORK_ID = #{network_id},
                                        IP = #{ip},
                                        NAME = #{nic_name} ]"
                else
                    template = "NIC_ALIAS = \
                                [ NETWORK_ID = #{network_id},\
                                  IP = #{ip},\
                                  PARENT = #{nic_alias} ]"
                end
            else
                if !nic_alias && !nic_name
                    template = "NIC = [ NETWORK_ID = #{network_id} ]"
                elsif !nic_alias && nic_name
                    template = "NIC = [ NETWORK_ID = #{network_id},
                                        NAME = #{nic_name} ]"
                else
                    template = "NIC_ALIAS = \
                                [ NETWORK_ID = #{network_id},\
                                  PARENT = #{nic_alias} ]"
                end
            end
        end

        helper.perform_action(args[0], options, 'Attach NIC') do |vm|
            vm.nic_attach(template)
        end
    end

    nic_detach_desc = <<-EOT.unindent
        Detaches a NIC from a running VM

        States: RUNNING, POWEROFF
    EOT

    command :"nic-detach", nic_detach_desc, :vmid, :nicid do
        nicid = args[1].to_i

        helper.perform_action(args[0], options, 'Detach NIC') do |vm|
            vm.nic_detach(nicid)
        end
    end

    nic_mirror_desc = <<-EOT.unindent
        Sets port mirroring from a source NIC to a destination NIC.

        <vmid> and <nicid> define a unique NIC Mirroring source,
        while <dest_vmid> and <dest_nicid> define a unique
        NIC Mirroring destination:

        [Source VM]-[Source NIC] => [Dest. VM]-[Dest. NIC]

        Mirroring type (Local/Remote) is determined based on a
        source/destination VM location. If VMs are co-hosted
        the SPAN session will be used, otherwise ERSPAN session
        will be established over GRE tunnel.

        NIC Mirroring is currently supported for Open vSwitch mode only.

        States: RUNNING, POWEROFF
    EOT

    command :"nic-mirror", nic_mirror_desc, :vmid, :nicid, :dest_vmid, :dest_nicid do
        vmid = args[0].to_i
        nicid = args[1].to_i
        dest_vmid = args[2].to_i
        dest_nicid = args[3].to_i

        helper.perform_action(args[0], options, 'Set port mirroring') do |vm|
            rc = vm.info

            if OpenNebula.is_error?(rc)
                STDERR.puts rc.message
                exit(-1)
            end

            if vmid == dest_vmid
                STDERR.puts 'Source VM ID and destination VM ID must be different'
                exit(-1)
            end

            nic = vm.retrieve_xmlelements(
                "//TEMPLATE/NIC[NIC_ID=\"#{nicid}\"]"
            )[0]

            if nic.nil?
                STDERR.puts "No source NIC found with NIC_ID=#{nicid}"
                exit(-1)
            end

            vm.nic_mirror(nicid, dest_vmid, dest_nicid)
        end
    end

    nic_unmirror_desc = <<-EOT.unindent
        Removes mirroring configuration for a NIC

        States: RUNNING, POWEROFF
    EOT

    command :"nic-unmirror", nic_unmirror_desc, :vmid, :nicid, :mirroringid do
        nicid = args[1].to_i
        mirroringid  = args[2].to_i

        helper.perform_action(args[0], options, 'Remove port mirroring') do |vm|
            rc = vm.info

            if OpenNebula.is_error?(rc)
                STDERR.puts rc.message
                exit(-1)
            end

            nic = vm.retrieve_xmlelements(
                "//TEMPLATE/NIC[NIC_ID=\"#{nicid}\"]"
            )[0]

            if nic.nil?
                STDERR.puts "No source NIC found with NIC_ID=#{nicid}"
                exit(-1)
            end

            vm.nic_unmirror(nicid, mirroringid)
        end
    end

    chgrp_desc = <<-EOT.unindent
        Changes the VM group
    EOT

    command :chgrp, chgrp_desc, [:range, :vmid_list], :groupid do
        helper.perform_actions(args[0], options, 'Group changed') do |vm|
            vm.chown(-1, args[1].to_i)
        end
    end

    chown_desc = <<-EOT.unindent
        Changes the VM owner and group
    EOT

    command :chown, chown_desc, [:range, :vmid_list], :userid,
            [:groupid, nil] do
        args[2].nil? ? gid = -1 : gid = args[2].to_i
        helper.perform_actions(args[0], options, 'Owner/Group changed') do |vm|
            vm.chown(args[1].to_i, gid)
        end
    end

    chmod_desc = <<-EOT.unindent
        Changes the VM permissions
    EOT

    command :chmod, chmod_desc, [:range, :vmid_list], :octet do
        helper.perform_actions(args[0], options, 'Permissions changed') do |vm|
            vm.chmod_octet(args[1])
        end
    end

    resched_desc = <<-EOT.unindent
        Sets the rescheduling flag for the VM.

        States: RUNNING, POWEROFF
    EOT

    command :resched, resched_desc, [:range, :vmid_list] do
        helper.perform_actions(args[0], options, 'Setting resched flag') do |vm|
            vm.resched
        end
    end

    unresched_desc = <<-EOT.unindent
        Clears the rescheduling flag for the VM.

        States: RUNNING, POWEROFF
    EOT

    command :unresched, unresched_desc, [:range, :vmid_list] do
        helper.perform_actions(args[0],
                               options,
                               'Clearing resched flag') do |vm|
            vm.unresched
        end
    end

    rename_desc = <<-EOT.unindent
        Renames the VM
    EOT

    command :rename, rename_desc, :vmid, :name do
        helper.perform_action(args[0], options, 'renamed') do |o|
            o.rename(args[1])
        end
    end

    # TODO: Write a more complete description:
    # what is a snapshot (system snapshot)
    # how to revert to a snapshot
    snapshot_create_desc = <<-EOT.unindent
        Creates a new VM snapshot
    EOT

    command :"snapshot-create", snapshot_create_desc, [:range, :vmid_list],
            [:name, nil], :options => [*schedule_opts] do
        if !options[:schedule].nil?
            # add name as an argument
            options[:args] = args[1]

            helper.schedule_actions(args[0], options, @comm_name)
        else
            helper.perform_actions(args[0], options, 'snapshot created') do |o|
                o.snapshot_create(args[1])
            end
        end
    end

    # TODO: Write a more complete description:
    snapshot_revert_desc = <<-EOT.unindent
        Reverts a VM to a saved snapshot
    EOT

    command :"snapshot-revert", snapshot_revert_desc, :vmid, :snapshot_id,
            :options => [*schedule_opts] do
        if !options[:schedule].nil?
            # add snap ID as an argument
            options[:args] = args[1]

            helper.schedule_actions([args[0]], options, @comm_name)
        else
            helper.perform_action(args[0], options, 'snapshot reverted') do |o|
                o.snapshot_revert(args[1].to_i)
            end
        end
    end

    # TODO: Write a more complete description:
    snapshot_delete_desc = <<-EOT.unindent
        Delets a snapshot of a VM
    EOT

    command :"snapshot-delete", snapshot_delete_desc, :vmid, :snapshot_id,
            :options => [*schedule_opts] do
        if !options[:schedule].nil?
            # add snap ID as an argument
            options[:args] = args[1]

            helper.schedule_actions([args[0]], options, @comm_name)
        else
            helper.perform_action(args[0], options, 'snapshot deleted') do |o|
                o.snapshot_delete(args[1])
            end
        end
    end

    disk_snapshot_create_desc = <<-EOT.unindent
        Takes a new snapshot of the given disk. This operation needs support
        from the Datastore drivers: QCOW2 or Ceph.

        States: RUNNING, POWEROFF, SUSPENDED
    EOT

    command :"disk-snapshot-create", disk_snapshot_create_desc,
            :vmid, :diskid, :name,
            :options => [*schedule_opts] do
        if !options[:schedule].nil?
            # add disk ID and name as arguments
            options[:args] = "#{args[1]},#{args[2]}"

            helper.schedule_actions([args[0]], options, @comm_name)
        else
            helper.perform_action(args[0], options,
                                  'disk snapshot created') do |o|
                o.disk_snapshot_create(args[1].to_i, args[2])
            end
        end
    end

    disk_snapshot_revert_desc = <<-EOT.unindent
        Reverts disk state to a previously taken snapshot.

        States: POWEROFF, SUSPENDED
    EOT

    command :"disk-snapshot-revert", disk_snapshot_revert_desc,
            :vmid, :diskid, :disk_snapshot_id,
            :options => [*schedule_opts] do
        if !options[:schedule].nil?
            # add disk ID and snap ID as arguments
            options[:args] = "#{args[1]},#{args[2]}"

            helper.schedule_actions([args[0]], options, @comm_name)
        else
            helper.perform_action(args[0], options,
                                  'disk snapshot reverted') do |o|
                o.disk_snapshot_revert(args[1].to_i, args[2].to_i)
            end
        end
    end

    disk_snapshot_delete_desc = <<-EOT.unindent
        Deletes a disk snapshot.

        States: RUNNING, POWEROFF, SUSPENDED
    EOT

    command :"disk-snapshot-delete", disk_snapshot_delete_desc,
            :vmid, :diskid, :disk_snapshot_id,
            :options => [*schedule_opts] do
        if !options[:schedule].nil?
            # add disk ID and snap ID as arguments
            options[:args] = "#{args[1]},#{args[2]}"

            helper.schedule_actions([args[0]], options, @comm_name)
        else
            helper.perform_action(args[0], options,
                                  'disk snapshot deleted') do |o|
                o.disk_snapshot_delete(args[1].to_i, args[2].to_i)
            end
        end
    end

    disk_snapshot_rename_desc = <<-EOT.unindent
        Renames a disk snapshot.
    EOT

    command :"disk-snapshot-rename", disk_snapshot_rename_desc,
            :vmid, :diskid, :disk_snapshot_id, :new_snapshot_name do
        helper.perform_action(args[0], options, 'disk snapshot rename') do |o|
            is_num = true if Integer(args[2]) rescue false

            if !is_num
                OpenNebula::Error.new('The disk snapshot ID must be an integer')
            else
                o.disk_snapshot_rename(args[1].to_i, args[2].to_i, args[3].to_s)
            end
        end
    end

    disk_resize_desc = <<-EOT.unindent
        Resizes a VM disk. The new size should be larger than the old one.
        The valid units are:
            - T: TiB
            - G: GiB
            - M: MiB
            By default is MiB.

        States: RUNNING, POWEROFF
    EOT

    command :"disk-resize", disk_resize_desc,
            :vmid, :diskid, :size do
        helper.perform_action(args[0], options, 'disk resized') do |o|
            o.info
            size = o["/VM/TEMPLATE/DISK[DISK_ID='#{args[1]}']/SIZE"].to_i

            new_size = args[2]

            if size < new_size
                o.disk_resize(args[1].to_i, args[2])
            else
                OpenNebula::Error.new("New size '#{new_size}' must be larger " \
                                      "than current size '#{size}'")
            end
        end
    end

    list_desc = <<-EOT.unindent
        Lists VMs in the pool
    EOT

    command :list, list_desc, [:filterflag, nil],
            :options => CLIHelper::OPTIONS + OpenNebulaHelper::OPTIONS +
                        [OpenNebulaHelper::DESCRIBE] + [OneVMHelper::SEARCH] do
        if !options[:search]
            helper.list_pool(options, false, args[0])
        else
            table = helper.format_pool(options)
            pool = OpenNebula::VirtualMachinePool.new(OneVMHelper.get_client)

            rc = pool.info_search(
                :extended => options[:extended], :query => options[:search]
            )

            if !rc.nil?
                puts rc.message
                exit(-1)
            end

            if options[:xml]
                puts pool.to_xml
            else
                table.show(pool.to_hash, options)
            end

            0
        end
    end

    show_desc = <<-EOT.unindent
        Shows information for the given VM
    EOT

    command :show, show_desc, :vmid,
            :options => [OpenNebulaHelper::FORMAT,
                         OpenNebulaHelper::DECRYPT,
                         OneVMHelper::ALL_TEMPLATE,
                         OneVMHelper::EXPORT_TEMPLATE,          #rbt
                         OneVMHelper::DISKLIST_TEMPLATE,        #rbt
                         OneVMHelper::DISKLISTCSV_TEMPLATE,     #rbt
                         OneVMHelper::NAME_TEMPLATE,            #rbt
                         OneVMHelper::BOOTLIST_TEMPLATE] do     #rbt
        helper.show_resource(args[0], options)
    end

    #rbt: add backup command desc
    backup_desc = <<-EOT.unindent
    Backup VM to datastore
EOT

#rbt: add backup command
command :backup, backup_desc, :vmid,
        :options=>[OneVMHelper::DATASTORE_ID_BACKUP, OneVMHelper::NAME_BACKUP, OneVMHelper::EXCLUDE_BACKUP] do
    # rbt: BREST-1264
    rc = helper.save_backup(args[0],options)
    if OpenNebula.is_error? rc
        STDERR.puts "#{rc.message}"
        exit -1
    end
    exit 0
end

#rbt: add restore command desc
restore_desc = <<-EOT.unindent
    Restore BACKUP file to VM
EOT

#rbt: add restore command
command :restore, restore_desc, :backup_id,
        :options=>[OneVMHelper::DATASTORE_ID_RESTORE, OneVMHelper::NAME_RESTORE] do
    # rbt: BREST-1264
    rc = helper.restore_backup(args[0],options)
    if OpenNebula.is_error? rc
        STDERR.puts "#{rc.message}"
        exit -1
    end
    exit 0
end

#rbt: add securityvm command desc
securityvm_desc = <<-EOT.unindent
    Set DAC security for VM
EOT

#rbt: add backup command
command :security, securityvm_desc, :vmid,
        :options=>[OneVMHelper::SECURITYVM_ACTION_ADD, OneVMHelper::SECURITYVM_ACTION_DEL, OneVMHelper::SECURITYVM_LEVEL_С, OneVMHelper::SECURITYVM_LEVEL_U, OneVMHelper::SECURITYVM_ENTITY_USER, OneVMHelper::SECURITYVM_ENTITY_GROUP] do
    helper.save_security(args[0],options)
end

#rbt: add setboot command desc
setboot_desc = <<-EOT.unindent
    Set boot order to VM
EOT

#rbt: add setboot command
command :setboot, setboot_desc, :vmid, :boot do
    begin
        bootorder=args[1] if args[1]
    rescue Exception => e
        STDERR.puts "Error reading boot order."
        exit -1
    end

    unless bootorder.empty?
        helper.perform_action(args[0], options, "Updating VM configuration boot order") do |vm|
            vm.info
            if !vm.has_elements?("/VM/TEMPLATE/OS")
                vm.add_element('/VM/TEMPLATE', 'OS' => nil)
            end
            if vm.has_elements?("/VM/TEMPLATE/OS/BOOT")
                vm.delete_element("/VM/TEMPLATE/OS/BOOT")
            end
            vm.add_element('/VM/TEMPLATE/OS', 'BOOT' => bootorder)
            template = vm.template_like_str('TEMPLATE', true, 'OS | FEATURES | INPUT | GRAPHICS | RAW | CONTEXT')
            vm.updateconf(template)
        end
    end
end

    top_desc = <<-EOT.unindent
        Lists Images continuously
    EOT

    command :top, top_desc, [:filterflag, nil],
            :options => CLIHelper::OPTIONS + OpenNebulaHelper::OPTIONS do
        helper.list_pool(options, true, args[0])
    end

    resize_desc = <<-EOT.unindent
        Resizes the capacity of a Virtual Machine (offline, the VM cannot be
        RUNNING)

    EOT

    command :resize, resize_desc, :vmid,
            :options => OpenNebulaHelper::CAPACITY_OPTIONS_VM +
                        [ENFORCE, OneVMHelper::FILE] do
        if options[:file]
            template = File.read(options[:file])
        else
            template = ''

            if !options[:cpu].nil?
                template << "CPU = #{options[:cpu]}\n"
            end

            if !options[:vcpu].nil?
                template << "VCPU = #{options[:vcpu]}\n"
            end

            if !options[:memory].nil?
                template << "MEMORY = #{options[:memory]}\n"
            end
        end

        enforce = options[:enforce] || false

        helper.perform_action(args[0], options, 'Resizing VM') do |vm|
            vm.resize(template, enforce)
        end
    end

    save_desc = <<-EOT.unindent
        Clones the VM's source Template, replacing the disks with live snapshots
        of the current disks. The VM capacity and NICs are also preserved

        States: POWEROFF
    EOT

    command :save, save_desc, :vmid, :name, :options => [PERSISTENT] do
        helper.perform_action(args[0], options, 'Saving VM') do |vm|
            vm.extend(VirtualMachineExt)

            res = vm.save_as_template(args[1],
                                      '',
                                      :persistent => options[:persistent])

            if !OpenNebula.is_error?(res)
                puts "Template ID: #{res}"
            end

            res
        end
    end

    updateconf_desc = <<-EOT.unindent
        Updates the configuration of a VM. Valid states are: running, pending,
        failure, poweroff, undeploy, hold or cloning.
        In running state only changes in CONTEXT take effect immediately,
        other values may need a VM restart.

        This command accepts a template file or opens an editor, the full list of
        configuration attributes are:

        OS        = ["ARCH", "MACHINE", "KERNEL", "INITRD", "BOOTLOADER", "BOOT", "UUID"]
        FEATURES  = ["ACPI", "PAE", "APIC", "LOCALTIME", "HYPERV", "GUEST_AGENT", "IOTHREADS"]
        INPUT     = ["TYPE", "BUS"]
        GRAPHICS  = ["TYPE", "LISTEN", "PASSWD", "KEYMAP" ]
        RAW       = ["DATA", "DATA_VMX", "TYPE"]
        CPU_MODEL = ["MODEL"]
        CONTEXT (any value, **variable substitution will be made**)
    EOT

    command :updateconf, updateconf_desc, :vmid, [:file, nil] do
        template = ''

        begin
            template = File.read(args[1]) if args[1]
        rescue StandardError => e
            STDERR.puts "Error reading template: #{e.message}."
            exit(-1)
        end

        helper.perform_action(args[0], options,
                              'Updating VM configuration') do |vm|
            if template.empty?
                rc = vm.info

                if OpenNebula.is_error?(rc)
                    STDERR.puts "ERROR: #{rc.message}"
                    exit(-1)
                end

                template = vm.template_like_str('TEMPLATE', true,
                                                'OS | FEATURES | INPUT | '\
                                                'GRAPHICS | RAW | CONTEXT | '\
                                                'CPU_MODEL')
                template = OpenNebulaHelper.editor_input(template)
            end

            vm.updateconf(template)
        end
    end

    lock_desc = <<-EOT.unindent
        Locks a VM with differents levels for lock any actions with this VM, show and
        monitoring never will be locked.
        Valid states are: All.
    EOT

    command :lock, lock_desc, [:range, :vmid_list],
            :options => [USE, MANAGE, ADMIN, ALL] do
        helper.perform_actions(args[0], options, 'VM locked') do |vm|
            if !options[:use].nil?
                level = 1
            elsif !options[:manage].nil?
                level = 2
            elsif !options[:admin].nil?
                level = 3
            elsif !options[:all].nil?
                level = 4
            else
                level = 1
            end
            vm.lock(level)
        end
    end

    unlock_desc = <<-EOT.unindent
        Unlocks a VM for unlock any actions with this VM.
        Valid states are: All.
    EOT

    command :unlock, unlock_desc, [:range, :vmid_list] do
        helper.perform_actions(args[0], options, 'VM unlocked') do |vm|
            vm.unlock
        end
    end

    if brest_mode != "1" # rbt: charters is useless then backup is only allowed operation BREST-3771
        ########################### Charters Management ############################

        create_chart_desc = <<-EOT.unindent
            Adds a charter to the VM, these are some consecutive scheduled actions

            You can configure the actions in /etc/one/cli/onevm.yaml
        EOT

        command :'create-chart', create_chart_desc, :vmid do
            charters = helper.get_charters

            if charters.nil?
                STDERR.puts 'No charters found on configuration file'
                exit(-1)
            end

            acc_t = 0
            acc_w = 0

            charters.each do |action, time|
                sched = "+#{time[:time].to_i + acc_t}"

                helper.schedule_actions([args[0]].flatten,
                                        { :schedule => sched },
                                        action.to_s,
                                        time[:warning][:time] + acc_w)

                acc_t += time[:time].to_i
                acc_w += time[:warning][:time].to_i
            end

            0
        end

        delete_chart_desc = <<-EOT.unindent
            Deletes a charter from the VM
        EOT

        command :'delete-chart', delete_chart_desc, :vmid, :sched_id do
            helper.perform_action(args[0], {}, 'Charter deleted') do |vm|
                rc = vm.info

                if OpenNebula.is_error?(rc)
                    STDERR.puts "Error #{rc.message}"
                    exit(-1)
                end

                xpath = "USER_TEMPLATE/SCHED_ACTION[ID=#{args[1]}]"

                unless vm.retrieve_elements(xpath)
                    STDERR.puts "Sched action #{args[1]} not found"
                    exit(-1)
                end

                vm.delete_element(xpath)
                rc = vm.update(vm.user_template_str)

                if OpenNebula.is_error?(rc)
                    STDERR.puts "Error deleting: #{rc.message}"
                    exit(-1)
                end
            end

            0
        end

        update_chart_desc = <<-EOT.unindent
            Updates a charter from a VM
        EOT

        command :'update-chart',
                update_chart_desc,
                :vmid,
                :sched_id,
                [:file, nil] do
            helper.update_schedule_action(args[0], args[1], args[2])

            0
        end
    end

    # rbt: del backup_mp and restore_mp commands
    # backup_vm_desc = <<-EOT.unindent
    #     Creates a VM backup and stores it in the marketplace
    # EOT

    # command :backup_mp,
    #         backup_vm_desc,
    #         :vmid,
    #         :options => [LOGGER, KEEP, ONESHOT, MARKET] do
    #     require 'logger'

    #     if options.key?(:oneshot) && !options.key?(:market)
    #         STDERR.puts 'ERROR: no market given'
    #         exit(-1)
    #     end

    #     helper.perform_action(args[0], options, 'Backup_mp') do |vm|
    #         vm.extend(OpenNebula::VirtualMachineExt)

    #         # Read user options
    #         if options[:verbose]
    #             log_to = STDOUT
    #         elsif !options[:logger].nil?
    #             log_to = options[:logger]
    #         end

    #         keep = options.key?(:keep)

    #         if log_to
    #             logger = Logger.new(log_to)
    #             format = '%Y-%m-%d %H:%M:%S'

    #             logger.formatter = proc do |severity, datetime, _p, msg|
    #                 "#{datetime.strftime(format)} " \
    #                 "#{severity.ljust(5)} : #{msg}\n"
    #             end
    #         end

    #         binfo          = {}
    #         binfo[:market] = options[:market]

    #         if options.key?(:oneshot)
    #             binfo[:name] = "VM #{vm.id} BACKUP_MP - " \
    #                            "#{Time.now.strftime('%Y%m%d_%k%M')}"
    #             binfo[:freq]   = 1
    #             binfo[:last]   = Time.now.to_i - 100
    #         end

    #         begin
    #             rc = vm.backup(keep, logger, binfo)

    #             if OpenNebula.is_error?(rc)
    #                 STDERR.puts rc.message
    #                 exit(-1)
    #             else
    #                 0
    #             end
    #         rescue StandardError => e
    #             STDERR.puts e
    #             exit(-1)
    #         end
    #     end
    # end

    # restore_vm_desc = <<-EOT.unindent
    #     Restores a VM from a previous backup
    # EOT

    # command :restore_mp,
    #         restore_vm_desc,
    #         :vmid,
    #         :options => [OneDatastoreHelper::DATASTORE, LOGGER] do
    #     require 'logger'

    #     unless options[:datastore]
    #         STDERR.puts 'ERROR: no datastore given'
    #         exit(-1)
    #     end

    #     helper.perform_action(args[0], options, 'Restore_mp') do |vm|
    #         vm.extend(OpenNebula::VirtualMachineExt)

    #         # If logger is specified use it, if not use STDOUT
    #         options[:logger].nil? ? log_to = STDOUT : log_to = options[:logger]
    #         logger = Logger.new(log_to)
    #         format = '%Y-%m-%d %H:%M:%S'

    #         logger.formatter = proc do |severity, datetime, _p, msg|
    #             "#{datetime.strftime(format)} #{severity.ljust(5)} : #{msg}\n"
    #         end

    #         begin
    #             rc = vm.restore(options[:datastore], logger)

    #             if OpenNebula.is_error?(rc)
    #                 STDERR.puts rc.message
    #                 exit(-1)
    #             else
    #                 puts "ID: #{rc}"
    #                 0
    #             end
    #         rescue StandardError => e
    #             STDERR.puts e
    #             exit(-1)
    #         end
    #     end
    # end

    ssh_desc = <<-EOT.unindent
        SSH into VM

        Options example:

        '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'
    EOT

    command :ssh,
            ssh_desc,
            :vmid,
            [:login, nil],
            :options => [NIC_ID, CMD, SSH_OPTS] do
        helper.perform_action(args[0], options, 'SSH') do |vm|
            rc = vm.info

            if OpenNebula.is_error?(rc)
                STDERR.puts rc.message
                exit(-1)
            end

            # Get user to login
            args[1].nil? ? login = 'root' : login = args[1]

            # Get CMD to run
            options[:cmd].nil? ? cmd = '' : cmd = options[:cmd]

            # Ensure the command is properly quoted
            cmd = "\"#{cmd}\"" unless cmd.empty?

            # Get NIC to connect
            if options[:nic_id]
                nic = vm.retrieve_xmlelements(
                    "//TEMPLATE/NIC[NIC_ID=\"#{options[:nic_id]}\"]"
                )[0]
            else
                nic = vm.retrieve_xmlelements('//TEMPLATE/NIC[SSH="YES"]')[0]
            end

            nic = vm.retrieve_xmlelements('//TEMPLATE/NIC[1]')[0] if nic.nil?

            if nic.nil?
                STDERR.puts 'No NIC found'
                exit(-1)
            end

            # If there is node port
            if nic['EXTERNAL_PORT_RANGE']
                ip   = vm.to_hash['VM']['HISTORY_RECORDS']['HISTORY']
                ip   = [ip].flatten[-1]['HOSTNAME']
                port = Integer(nic['EXTERNAL_PORT_RANGE'].split(':')[0]) + 21
            else
                ip   = nic['IP']
                port = 22
            end

            options[:ssh_opts].nil? ? opts = '' : opts = options[:ssh_opts]

            system("ssh #{opts} #{login}@#{ip} -p #{port} #{cmd}")
        end

        $?.exitstatus
    end

    port_desc = <<-EOT.unindent
        Get port forwarding from a NIC, e.g:

        1.2.3.4@4000 -> 1, means that to connect to VM port 1, you need to
        connect to IP 1.2.3.4 in port 4000
    EOT

    command :'port-forward',
            port_desc,
            :vmid,
            [:port, nil],
            :options => NIC_ID do
        helper.perform_action(args[0], options, 'Port Forward') do |vm|
            rc = vm.info

            if OpenNebula.is_error?(rc)
                STDERR.puts rc.message
                exit(-1)
            end

            if options[:nic_id]
                nic = vm.retrieve_xmlelements(
                    "//TEMPLATE/NIC[NIC_ID=\"#{options[:nic_id]}\"]"
                )[0]
            else
                nic = vm.retrieve_xmlelements('//TEMPLATE/NIC[SSH="YES"]')[0]
            end

            nic = vm.retrieve_xmlelements('//TEMPLATE/NIC[1]')[0] if nic.nil?

            if nic.nil?
                STDERR.puts 'No NIC found'
                exit(-1)
            end

            if nic['EXTERNAL_PORT_RANGE'].nil?
                STDERR.puts 'No PORT_RANGE found'
                exit(-1)
            end

            ip = vm.to_hash['VM']['HISTORY_RECORDS']['HISTORY'][-1]['HOSTNAME']

            e_range   = nic['EXTERNAL_PORT_RANGE'].split(':')
            e_start_p = Integer(e_range[0])
            e_end_p   = Integer(e_range[1])

            i_range   = nic['INTERNAL_PORT_RANGE'].split('-')
            i_start_p = Integer(i_range[0])
            i_end_p   = Integer(i_range[1].split('/')[0])

            if args[1].nil?
                [*e_start_p..e_end_p].zip([*i_start_p..i_end_p]) do |p1, p2|
                    puts "#{ip}@#{p1} -> #{p2}"
                end
            else
                puts "#{ip}@#{e_start_p + Integer(args[1]) - 1} -> #{args[1]}"
            end
        end

    end

    changeds_desc = <<-EOT.unindent
        Changes the system datastore from LVM_LVM, LVM_THIN or OCFS to BREST_LVM type
    EOT

    command :"changeds", changeds_desc, :vmid, :sys_ds_id, :img_ds_id do
        sys_id = args[1].to_i
        img_id = args[2].to_i

        helper.perform_action(args[0], options, 'Change DS') do |vm|
            res = vm.changeds(sys_id, img_id)
        end
    end

    # Deprecated commands

    deprecated_command(:shutdown, 'terminate')
    deprecated_command(:delete, 'recover')
end
