Skip to content

Commit

Permalink
Queue operations after check_policy_prevent
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Nov 21, 2019
1 parent 3fa0b49 commit 0beb804
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 47 deletions.
20 changes: 20 additions & 0 deletions app/models/vm_or_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class VmOrTemplate < ApplicationRecord
virtual_delegate :ram_size, :to => "hardware.memory_mb", :allow_nil => true, :default => 0, :type => :integer

delegate :connect_lans, :disconnect_lans, :to => :hardware, :allow_nil => true
delegate :queue_name_for_ems_operations, :to => :ext_management_system, :allow_nil => true

after_save :save_genealogy_information

Expand Down Expand Up @@ -336,6 +337,15 @@ def run_command_via_parent(verb, options = {})
ext_management_system.send(verb, self, options)
end

def run_command_via_task(task_options, queue_options)
MiqTask.generic_action_with_callback(task_options, command_queue_options(queue_options))
end

def run_command_via_queue(method_name, queue_options)
queue_options[:method_name] = method_name
MiqQueue.put(command_queue_options(queue_options))
end

# keep the same method signature as others in retirement mixin
def self.make_retire_request(*src_ids, requester, initiated_by: 'user')
vms = where(:id => src_ids)
Expand Down Expand Up @@ -1844,6 +1854,16 @@ def create_notification(type, options)
)
end

def command_queue_options(queue_options)
{
:class_name => self.class.name,
:instance_id => id,
:role => "ems_operations",
:queue_name => queue_name_for_ems_operations,
:zone => my_zone,
}.merge(queue_options)
end

# this is verbose, helper for generating arel
def self.arel_coalesce(values)
Arel::Nodes::NamedFunction.new('COALESCE', values)
Expand Down
23 changes: 11 additions & 12 deletions app/models/vm_or_template/operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,28 @@ def raw_unregister
raise NotImplementedError, _("must be implemented in a subclass")
end

def unregister_queue
run_command_via_queue("raw_unregister")
end

def unregister
raise _("VM has no Provider, unable to unregister VM") unless ext_management_system

check_policy_prevent(:request_vm_unregister, :raw_unregister)
check_policy_prevent(:request_vm_unregister, :unregister_queue)
end

def raw_destroy
raise NotImplementedError, _("must be implemented in a subclass")
end

def destroy_queue
run_command_via_queue("raw_destroy")
end

def vm_destroy
raise _("VM has no Provider, unable to destroy VM") unless ext_management_system

check_policy_prevent(:request_vm_destroy, :raw_destroy)
check_policy_prevent(:request_vm_destroy, :destroy_queue)
end

def raw_rename(new_name)
Expand All @@ -75,16 +83,7 @@ def rename_queue(userid, new_name)
:userid => userid
}

queue_opts = {
:class_name => self.class.name,
:method_name => 'rename',
:instance_id => id,
:role => 'ems_operations',
:zone => my_zone,
:args => [new_name]
}

MiqTask.generic_action_with_callback(task_opts, queue_opts)
run_command_via_queue(task_opts, :method_name => "rename", :args => [new_name])
end

def raw_set_custom_field(_attribute, _value)
Expand Down
36 changes: 30 additions & 6 deletions app/models/vm_or_template/operations/power.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,63 @@ def raw_start
raise NotImplementedError, _("must be implemented in a subclass")
end

def queue_start
run_command_via_queue("raw_start")
end

def start
check_policy_prevent(:request_vm_start, :raw_start)
check_policy_prevent(:request_vm_start, :queue_start)
end

def raw_stop
raise NotImplementedError, _("must be implemented in a subclass")
end

def stop_queue
run_command_via_queue("raw_stop")
end

def stop
check_policy_prevent(:request_vm_poweroff, :raw_stop)
check_policy_prevent(:request_vm_poweroff, :stop_queue)
end

# Suspend saves the state of the VM to disk and shuts it down
def raw_suspend
raise NotImplementedError, _("must be implemented in a subclass")
end

def suspend_queue
run_command_via_queue("raw_suspend")
end

def suspend
check_policy_prevent(:request_vm_suspend, :raw_suspend)
check_policy_prevent(:request_vm_suspend, :suspend_queue)
end

# All associated data and resources are kept but anything still in memory is not retained.
def raw_shelve
raise NotImplementedError, _("must be implemented in a subclass")
end

def shelve_queue
run_command_via_queue("raw_shelve")
end

def shelve
check_policy_prevent(:request_vm_shelve, :raw_shelve)
check_policy_prevent(:request_vm_shelve, :shelve_queue)
end

# Has to be in shelved state first. Data and resource associations are deleted.
def raw_shelve_offload
raise NotImplementedError, _("must be implemented in a subclass")
end

def shelve_offload_queue
run_command_via_queue("raw_shelve_offload")
end

def shelve_offload
check_policy_prevent(:request_vm_shelve_offload, :raw_shelve_offload)
check_policy_prevent(:request_vm_shelve_offload, :shelve_offload_queue)
end

# Pause keeps the VM in memory but does not give it CPU cycles.
Expand All @@ -48,7 +68,11 @@ def raw_pause
raise NotImplementedError, _("must be implemented in a subclass")
end

def pause_queue
run_command_via_queue("raw_pause")
end

def pause
check_policy_prevent(:request_vm_pause, :raw_pause)
check_policy_prevent(:request_vm_pause, :pause_queue)
end
end
37 changes: 8 additions & 29 deletions app/models/vm_or_template/operations/snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ def raw_create_snapshot(name, desc = nil, memory)
raise NotImplementedError, _("must be implemented in a subclass")
end

def create_snapshot_queue(name, desc = nil, memory)
run_command_via_queue("raw_create_snapshot", :args => [name, desc, memory])
end

def create_snapshot(name, desc = nil, memory = false)
check_policy_prevent(:request_vm_create_snapshot, :raw_create_snapshot, name, desc, memory)
check_policy_prevent(:request_vm_create_snapshot, :create_snapshot_queue, name, desc, memory)
end

def raw_remove_snapshot(snapshot_id)
Expand All @@ -74,27 +78,11 @@ def remove_evm_snapshot(snapshot_id)
end

def remove_snapshot_queue(snapshot_id, task_id = nil)
MiqQueue.put_unless_exists(
:class_name => self.class.name,
:instance_id => id,
:method_name => 'remove_snapshot',
:args => [snapshot_id],
:role => "ems_operations",
:zone => my_zone,
:task_id => task_id
)
run_command_via_queue("remove_snapshot", :args => [snapshot_id], :task_id => task_id)
end

def remove_evm_snapshot_queue(snapshot_id, task_id = nil)
MiqQueue.put_unless_exists(
:class_name => self.class.name,
:instance_id => id,
:method_name => 'remove_evm_snapshot',
:args => [snapshot_id],
:role => "ems_operations",
:zone => my_zone,
:task_id => task_id
)
run_command_via_queue("remove_evm_snapshot", :args => [snapshot_id], :task_id => task_id)
end

def raw_remove_snapshot_by_description(description, refresh = false)
Expand Down Expand Up @@ -134,16 +122,7 @@ def remove_all_snapshots_queue(userid)
:userid => userid
}

queue_opts = {
:class_name => self.class.name,
:method_name => 'remove_all_snapshots',
:instance_id => id,
:role => 'ems_operations',
:zone => ext_management_system.my_zone,
:args => []
}

MiqTask.generic_action_with_callback(task_opts, queue_opts)
run_command_via_task(task_opts, :method_name => "remove_all_snapshots")
end

def raw_revert_to_snapshot(snapshot_id)
Expand Down

0 comments on commit 0beb804

Please sign in to comment.