Project

General

Profile

Download (3.97 KB) Statistics
| Branch: | Tag: | Revision:
55548030 Ohad Levy
module ComputeResourcesVmsHelper

# little helper to help show VM properties
def prop method, title = nil
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
5ceabbf5 Dmitri Dolguikh
#TODO in 4.0 #try will return nil if the method doesn't exist (instead of raising NoMethodError)
# we can drop rescues then.
value.map{|v| (v.try(:name) rescue nil) || (v.try(:to_s) rescue nil) || 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
bfbf7ed8 Lukas Zapletal
_("N/A")
55548030 Ohad Levy
else
f4ab10df Martin Matuska
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?
b760d48d Greg Sutcliffe
options.merge!(
: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
interfaces = compute.interfaces
select = []
select << [_('Physical (Bridge)'), :bridge]
select << [_('Virtual (NAT)'), :network] if networks.any?
select
end

5257110f Martin Matuska
def vsphere_datastores(compute)
compute.datastores.map do |ds|
[
ds.freespace && ds.capacity ?
b917d39a Martin Matuska
"#{ds.name} (#{_('free')}: #{number_to_human_size(ds.freespace)}, #{_('prov')}: #{number_to_human_size(ds.capacity + (ds.uncommitted || 0) - ds.freespace)}, #{_('total')}: #{number_to_human_size(ds.capacity)})" :
5257110f Martin Matuska
ds.name,
ds.name
]
end
end

acfbc458 Marek Hulan
def available_actions(vm, authorizer = nil)
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

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

acfbc458 Marek Hulan
actions << 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))
a6ce3c99 Daniel Lobato
end

acfbc458 Marek Hulan
def default_available_actions(vm, authorizer = nil)
[vm_power_action(vm, authorizer),
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))]
a6ce3c99 Daniel Lobato
end

beed05de Mark O'Shea
def vpc_security_group_hash(security_groups)
vpc_sg_hash = Hash.new
security_groups.each{ |sg|
vpc_id = sg.vpc_id || 'ec2'
( vpc_sg_hash[vpc_id] ||= []) << {:group_name => sg.name, :group_id => sg.group_id}
}
vpc_sg_hash
end

def subnet_vpc_hash(subnets)
subnet_vpc_hash = Hash.new
subnets.each{ |sub| subnet_vpc_hash[sub.subnet_id] = {:vpc_id =>sub.vpc_id, :subnet_name => sub.tag_set["Name"] || sub.subnet_id} }
subnet_vpc_hash
end

def compute_object_vpc_id(form)
form.object.network_interfaces.try(:first).try(:[], "vpcId")
end

def security_groups_for_vpc(security_groups, vpc_id)
68522ec8 Dominic Cleal
security_groups.map{ |sg| [sg.name, sg.group_id] if sg.vpc_id == vpc_id}.compact
beed05de Mark O'Shea
end
c6e02bd3 Joseph Magen
def show_vm_name?
controller_name != 'hosts' && controller_name != 'compute_attributes'
end

55548030 Ohad Levy
end