foreman/app/helpers/compute_resources_vms_helper.rb @ 9d43fc71
55548030 | Ohad Levy | module ComputeResourcesVmsHelper
|
|
5aac54d7 | Dmitri Dolguikh | def vm_power_actions(host, vm)
|
|
88ed4900 | Amos Benari | button_group(
|
|
if vm
|
|||
afe02d30 | Daniel Lobato | html_opts = vm.ready? ? {:confirm => _('Are you sure?'), :class => "btn btn-danger"} : {:class => "btn btn-success"}
|
|
link_to_if_authorized _("Power%s") % state(vm.ready?), hash_for_power_host_path(:power_action => vm.ready? ? :stop : :start).merge(:auth_object => host, :permission => 'power_hosts'),
|
|||
html_opts.merge(:method => :put)
|
|||
88ed4900 | Amos Benari | else
|
|
afe02d30 | Daniel Lobato | link_to(_("Unknown Power State"), '#', :disabled => true, :class => "btn btn-warning")
|
|
88ed4900 | Amos Benari | end
|
|
)
|
|||
end
|
|||
5aac54d7 | Dmitri Dolguikh | def vm_console(host, vm)
|
|
b03dcd1b | Michael Moll | if vm&.ready?
|
|
5aac54d7 | Dmitri Dolguikh | link_to_if_authorized(_("Console"), hash_for_console_host_path().merge(:auth_object => host, :permission => 'console_hosts'),
|
|
88ed4900 | Amos Benari | { :class => "btn btn-info" })
|
|
else
|
|||
9d43fc71 | Michael Moll | link_to(_("Console"), '#', {:disabled => true, :class => "btn btn-info"})
|
|
88ed4900 | Amos Benari | end
|
|
end
|
|||
55548030 | Ohad Levy | # little helper to help show VM properties
|
|
5f029ed6 | Daniel Lobato | def prop(method, title = nil)
|
|
55548030 | Ohad Levy | content_tag :tr do
|
|
acfbc458 | Marek Hulan | result = content_tag(:td) do
|
|
55548030 | Ohad Levy | title || method.to_s.humanize
|
|
end
|
|||
acfbc458 | Marek Hulan | result += content_tag(:td) do
|
|
55548030 | Ohad Levy | value = @vm.send(method) rescue nil
|
|
case value
|
|||
when Array
|
|||
d468c1bc | kgaikwad | value.map{|v| v.try(:name) || v.try(:to_s) || v}.to_sentence
|
|
132a991c | Ohad Levy | when Fog::Time, Time
|
|
bfbf7ed8 | Lukas Zapletal | _("%s ago") % time_ago_in_words(value)
|
|
132a991c | Ohad Levy | when nil
|
|
afe02d30 | Daniel Lobato | _("N/A")
|
|
55548030 | Ohad Levy | else
|
|
da9865b8 | Michael Moll | (method == :memory) ? number_to_human_size(value) : value.to_s
|
|
55548030 | Ohad Levy | end
|
|
end
|
|||
result
|
|||
end
|
|||
end
|
|||
8ffd9aee | Ohad Levy | def supports_spice_xpi?
|
|
user_agent = request.env['HTTP_USER_AGENT']
|
|||
user_agent =~ /linux/i && user_agent =~ /firefox/i
|
|||
end
|
|||
def spice_data_attributes(console)
|
|||
options = {
|
|||
:port => console[:proxy_port],
|
|||
:password => console[:password]
|
|||
}
|
|||
options.merge!(
|
|||
:address => console[:address],
|
|||
:secure_port => console[:secure_port],
|
|||
36a6345d | Jason Montleon | :subject => console[:subject],
|
|
bfbf7ed8 | Lukas Zapletal | :title => _("%s - Press Shift-F12 to release the cursor.") % console[:name]
|
|
8ffd9aee | Ohad Levy | ) if supports_spice_xpi?
|
|
cabe2c2e | Ohad Levy | options[:ca_cert] = URI.escape(console[:ca_cert]) if console[:ca_cert].present?
|
|
8ffd9aee | Ohad Levy | options
|
|
end
|
|||
c5c84034 | Ohad Levy | def libvirt_networks(compute)
|
|
networks = compute.networks
|
|||
select = []
|
|||
select << [_('Physical (Bridge)'), :bridge]
|
|||
select << [_('Virtual (NAT)'), :network] if networks.any?
|
|||
select
|
|||
end
|
|||
d4af7cae | Ivan Necas | def vsphere_networks(compute_resource)
|
|
networks = compute_resource.networks
|
|||
networks.map do |net|
|
|||
net_id = net.id
|
|||
net_name = net.name
|
|||
net_name += " (#{net.virtualswitch})" if net.virtualswitch
|
|||
[net_id, net_name]
|
|||
end
|
|||
end
|
|||
c1c9649b | Daniel Lobato | def datastore_stats(datastore)
|
|
return datastore.name unless datastore.freespace && datastore.capacity
|
|||
"#{datastore.name} (#{_('free')}: #{number_to_human_size(datastore.freespace)}, #{_('prov')}: #{number_to_human_size(datastore.capacity + (datastore.uncommitted || 0) - datastore.freespace)}, #{_('total')}: #{number_to_human_size(datastore.capacity)})"
|
|||
5257110f | Martin Matuska | end
|
|
80dc430e | Ohad Levy | def vsphere_datastores(compute)
|
|
d611c53b | Michael Moll | compute.datastores.each_with_object({}) do |datastore, hsh|
|
|
80dc430e | Ohad Levy | hsh[datastore.name] = datastore_stats(datastore)
|
|
end
|
|||
end
|
|||
d46cc693 | Timo Goebel | def vsphere_storage_pods(compute)
|
|
d611c53b | Michael Moll | compute.storage_pods.each_with_object({}) do |pod, hsh|
|
|
80dc430e | Ohad Levy | hsh[pod.name] = storage_pod_stats(pod)
|
|
end
|
|||
d46cc693 | Timo Goebel | end
|
|
def storage_pod_stats(pod)
|
|||
80dc430e | Ohad Levy | "#{pod.name} (#{_('free')}: #{number_to_human_size(pod.freespace.to_i)}, #{_('prov')}: #{number_to_human_size(pod.capacity.to_i - pod.freespace.to_i)}, #{_('total')}: #{number_to_human_size(pod.capacity.to_i)})"
|
|
d46cc693 | Timo Goebel | end
|
|
acfbc458 | Marek Hulan | def available_actions(vm, authorizer = nil)
|
|
49c332e5 | Tom Caspy | return default_available_actions(vm, authorizer) unless defined? Fog::Compute::OpenStack::Server
|
|
a6ce3c99 | Daniel Lobato | case vm
|
|
when Fog::Compute::OpenStack::Server
|
|||
acfbc458 | Marek Hulan | openstack_available_actions(vm, authorizer)
|
|
a6ce3c99 | Daniel Lobato | else
|
|
acfbc458 | Marek Hulan | default_available_actions(vm, authorizer)
|
|
a6ce3c99 | Daniel Lobato | end
|
|
end
|
|||
dea3cf64 | Timo Goebel | def common_available_actions(vm, authorizer = nil)
|
|
actions = []
|
|||
actions << vm_delete_action(vm, authorizer)
|
|||
actions << vm_console_action(vm)
|
|||
host_action = vm_host_action(vm)
|
|||
if host_action
|
|||
actions << host_action
|
|||
else
|
|||
actions << vm_import_action(vm, :class => 'btn btn-default')
|
|||
actions << vm_associate_action(vm)
|
|||
end
|
|||
actions
|
|||
end
|
|||
acfbc458 | Marek Hulan | def openstack_available_actions(vm, authorizer = nil)
|
|
a6ce3c99 | Daniel Lobato | actions = []
|
|
if vm.state == 'ACTIVE'
|
|||
acfbc458 | Marek Hulan | actions << vm_power_action(vm, authorizer)
|
|
actions << vm_pause_action(vm, authorizer)
|
|||
a6ce3c99 | Daniel Lobato | elsif vm.state == 'PAUSED'
|
|
acfbc458 | Marek Hulan | actions << vm_pause_action(vm, authorizer)
|
|
a6ce3c99 | Daniel Lobato | else
|
|
acfbc458 | Marek Hulan | actions << vm_power_action(vm, authorizer)
|
|
a6ce3c99 | Daniel Lobato | end
|
|
dea3cf64 | Timo Goebel | (actions + common_available_actions(vm, authorizer)).compact
|
|
a6ce3c99 | Daniel Lobato | end
|
|
acfbc458 | Marek Hulan | def default_available_actions(vm, authorizer = nil)
|
|
dea3cf64 | Timo Goebel | actions = []
|
|
actions << vm_power_action(vm, authorizer)
|
|||
(actions + common_available_actions(vm, authorizer)).compact
|
|||
a6ce3c99 | Daniel Lobato | end
|
|
beed05de | Mark O'Shea | def vpc_security_group_hash(security_groups)
|
|
6ace062d | Daniel Lobato | vpc_sg_hash = {}
|
|
7deaced2 | Daniel Lobato | security_groups.each do |sg|
|
|
beed05de | Mark O'Shea | vpc_id = sg.vpc_id || 'ec2'
|
|
f4459c11 | David Davis | (vpc_sg_hash[vpc_id] ||= []) << {:group_name => sg.name, :group_id => sg.group_id}
|
|
7deaced2 | Daniel Lobato | end
|
|
beed05de | Mark O'Shea | vpc_sg_hash
|
|
end
|
|||
def subnet_vpc_hash(subnets)
|
|||
6ace062d | Daniel Lobato | subnet_vpc_hash = {}
|
|
9d43fc71 | Michael Moll | subnets.each{ |sub| subnet_vpc_hash[sub.subnet_id] = {:vpc_id => sub.vpc_id, :subnet_name => sub.tag_set["Name"] || sub.subnet_id} }
|
|
beed05de | Mark O'Shea | subnet_vpc_hash
|
|
end
|
|||
891c47ba | Swapnil Abnave | def security_groups_selectable(compute_resource, form)
|
|
all_security_groups = compute_resource.security_groups.all
|
|||
subnet_vpc_hash = subnet_vpc_hash(compute_resource.subnets)
|
|||
vpc_sg_hash = vpc_security_group_hash(all_security_groups)
|
|||
selected_subnet = form.object.subnet_id
|
|||
vpc_id = selected_subnet.present? && subnet_vpc_hash[selected_subnet][:vpc_id]
|
|||
groups = security_groups_for_vpc(all_security_groups, vpc_id).presence ||
|
|||
security_group_not_selected(subnet_vpc_hash, vpc_sg_hash, vpc_id)
|
|||
[groups, vpc_sg_hash, subnet_vpc_hash]
|
|||
beed05de | Mark O'Shea | end
|
|
def security_groups_for_vpc(security_groups, vpc_id)
|
|||
891c47ba | Swapnil Abnave | security_groups.map{ |sg| [sg.name, sg.group_id] if sg.vpc_id == vpc_id }.compact
|
|
end
|
|||
def security_group_not_selected(subnet_vpc_hash, vpc_sg_hash, vpc_id)
|
|||
return [] if vpc_id.blank?
|
|||
vpc_sg_hash[vpc_id].map do |vpc_sg|
|
|||
["#{vpc_sg[:group_name]} - #{selected_subnet}", vpc_sg[:group_id]]
|
|||
end
|
|||
beed05de | Mark O'Shea | end
|
|
c6e02bd3 | Joseph Magen | ||
def show_vm_name?
|
|||
controller_name != 'hosts' && controller_name != 'compute_attributes'
|
|||
end
|
|||
637da2f2 | Timo Goebel | def vsphere_resource_pools(form, compute_resource, disabled = false)
|
|
af80ee8b | Shlomi Zadok | resource_pools = compute_resource.available_resource_pools(:cluster_id => form.object.cluster) rescue []
|
|
637da2f2 | Timo Goebel | selectable_f form, :resource_pool, resource_pools, { }, :class => "col-md-2", :label => _('Resource pool'), :disabled => disabled
|
|
af80ee8b | Shlomi Zadok | end
|
|
fbd765f1 | Tomer Brisker | ||
def vms_table
|
|||
data = if @compute_resource.supports_vms_pagination?
|
|||
{ :table => 'server', :source => compute_resource_vms_path }
|
|||
else
|
|||
{ :table => 'inline' }
|
|||
end
|
|||
77e12c85 | Tomer Brisker | content_tag :table, :class => table_css_classes, :width => '100%', :data => data do
|
|
fbd765f1 | Tomer Brisker | yield
|
|
end
|
|||
end
|
|||
# Really counting vms is as expansive as loading them all, especially when
|
|||
# a filter is in place. So we create a fake count to get table pagination to work.
|
|||
def ovirt_fake_vms_count
|
|||
77e12c85 | Tomer Brisker | params['start'].to_i + 1 + [@vms.length, params['length'].to_i].min
|
|
fbd765f1 | Tomer Brisker | end
|
|
def ovirt_vms_data
|
|||
data = @vms.map do |vm|
|
|||
[
|
|||
link_to_if_authorized(html_escape(vm.name), hash_for_compute_resource_vm_path(:compute_resource_id => @compute_resource, :id => vm.id).merge(:auth_object => @compute_resource, :auth_action => 'view', :authorizer => authorizer)),
|
|||
vm.cores,
|
|||
number_to_human_size(vm.memory),
|
|||
"<span #{vm_power_class(vm.ready?)}>#{vm_state(vm)}</span>",
|
|||
action_buttons(vm_power_action(vm, authorizer),
|
|||
637da2f2 | Timo Goebel | vm_import_action(vm),
|
|
fbd765f1 | Tomer Brisker | display_delete_if_authorized(hash_for_compute_resource_vm_path(:compute_resource_id => @compute_resource, :id => vm.id).merge(:auth_object => @compute_resource, :authorizer => authorizer)))
|
|
]
|
|||
end
|
|||
JSON.fast_generate(data).html_safe
|
|||
end
|
|||
9c779942 | Tomer Brisker | ||
def vm_delete_action(vm, authorizer = nil)
|
|||
display_delete_if_authorized(hash_for_compute_resource_vm_path(:compute_resource_id => @compute_resource, :id => vm.identity).merge(:auth_object => @compute_resource, :authorizer => authorizer), :class => 'btn btn-danger')
|
|||
end
|
|||
637da2f2 | Timo Goebel | ||
80dc430e | Ohad Levy | def vsphere_scsi_controllers(compute)
|
|
scsi_controllers = {}
|
|||
compute.scsi_controller_types.each { |type| scsi_controllers[type[:key]] = type[:title] }
|
|||
scsi_controllers
|
|||
end
|
|||
637da2f2 | Timo Goebel | def new_vm?(host)
|
|
return true unless host.present?
|
|||
e2f7f37e | Ori Rabin | compute_object = host.compute_object
|
|
return true unless compute_object.present?
|
|||
!compute_object.persisted?
|
|||
637da2f2 | Timo Goebel | end
|
|
dea3cf64 | Timo Goebel | def vm_host_action(vm)
|
|
host = Host.for_vm(@compute_resource, vm).first
|
|||
return unless host
|
|||
display_link_if_authorized(_("Host"), hash_for_host_path(:id => host), :class => 'btn btn-default')
|
|||
end
|
|||
def vm_import_action(vm, html_options = {})
|
|||
637da2f2 | Timo Goebel | return unless Host.for_vm(@compute_resource, vm).empty?
|
|
dea3cf64 | Timo Goebel | display_link_if_authorized(
|
|
_("Import"),
|
|||
hash_for_import_compute_resource_vm_path(
|
|||
:compute_resource_id => @compute_resource,
|
|||
:id => vm.identity),
|
|||
html_options
|
|||
)
|
|||
end
|
|||
def vm_associate_action(vm)
|
|||
display_link_if_authorized(
|
|||
_("Associate VM"),
|
|||
hash_for_associate_compute_resource_vm_path(
|
|||
:compute_resource_id => @compute_resource,
|
|||
:id => vm.identity
|
|||
).merge(
|
|||
:auth_object => @compute_resource,
|
|||
:permission => 'edit_compute_resources'),
|
|||
:title => _("Associate VM to a Foreman host"),
|
|||
:method => :put,
|
|||
9d43fc71 | Michael Moll | :class => "btn btn-default"
|
|
dea3cf64 | Timo Goebel | )
|
|
end
|
|||
def vm_console_action(vm)
|
|||
return unless vm.ready?
|
|||
link_to_if_authorized(
|
|||
_("Console"),
|
|||
hash_for_console_compute_resource_vm_path.merge(
|
|||
:auth_object => @compute_resource
|
|||
),
|
|||
{
|
|||
:class => "btn btn-info"
|
|||
}
|
|||
)
|
|||
637da2f2 | Timo Goebel | end
|
|
55548030 | Ohad Levy | end
|