lib/dcmgr/node_modules/hva_collector.rb in wakame-vdc-agents-10.12.0 vs lib/dcmgr/node_modules/hva_collector.rb in wakame-vdc-agents-11.06.0

- old
+ new

@@ -4,10 +4,11 @@ module Dcmgr module NodeModules class HvaCollector < Isono::NodeModules::Base include Isono::NodeModules + include Dcmgr::Logger initialize_hook do rpc = RpcChannel.new(node) app = Isono::Rack::ObjectMethod.new(myinstance) rpc.register_endpoint('hva-collector', Isono::Rack.build do @@ -21,20 +22,18 @@ terminate_hook do end def get_instance(instance_id) - Models::Instance.lock! inst = Models::Instance[instance_id] raise "UnknownInstanceID" if inst.nil? ret = inst.to_hash ret end def update_instance(instance_id, data) - Models::Instance.lock! inst = Models::Instance[instance_id] raise "UnknownInstanceID" if inst.nil? if data[:state] == :terminated inst.terminated_at = data[:terminated_at] # Instance#destroy do not really delete row. @@ -46,67 +45,87 @@ # do not respond model object. nil end def get_netfilter_groups_of_instance(instance_id) - Models::Instance.lock! inst = Models::Instance[instance_id] raise "UnknownInstanceID" if inst.nil? inst.netfilter_groups.map { |g| g.to_hash } end - def get_group_instance_ipv4s(instance_id) - Models::Instance.lock! + #Returns an array containing the ip addresses of all instances in the same security group. + # _set_ determines which ip addresses are returned. There are 3 possible values + # :inside is the default value. This returns all inside ip addresses + # :outside returns all the outside addresses for instances that are natted. + # :all returns all ip addresses regardless of whether they're natted or not + def get_group_instance_ipv4s(instance_id,set = :inside) inst = Models::Instance[instance_id] raise "UnknownInstanceID" if inst.nil? + raise "Unknown ip set." unless [:inside,:all,:outside].member?(set) - ipv4s = inst.netfilter_groups.map { |netfilter_group| - next if netfilter_group.nil? - netfilter_group.instance_netfilter_groups.map { |instance_netfilter_group| - next if instance_netfilter_group.nil? - instance_netfilter_group.instance_dataset.lives.all.map { |instance| - next if instance.nil? - instance.ips.map { |ip| - next if ip.nil? - ip.ipv4 + inst.netfilter_groups.compact.map { |netfilter_group| + netfilter_group.instances_dataset.lives.all.compact.map { |instance| + instance.ips.compact.map { |ip| + case set + when :all + ip + when :inside + ip.map {|i| unless i.is_natted? then i.ipv4 else nil end}.compact + when :outside + ip.map {|i| if i.is_natted? then i.ipv4 else nil end}.compact + end } } - } - }.flatten.uniq.compact - ipv4s + }.flatten.uniq.compact end # def get_instances_of_account_netfilter_group(account_id, netfilter_group_id) def get_instances_of_account_netfilter_group(account_id, netfilter_group_name) - Models::NetfilterGroup.lock! ng_map = Models::NetfilterGroup.find(:account_id => account_id, :name => netfilter_group_name) raise "UnknownNetfilterGroupID" if ng_map.nil? - inst_maps = ng_map.instance_netfilter_groups.map { |instance_netfilter_group| - instance_netfilter_group.instance_dataset.lives.all.map { |inst| inst.to_hash } - }.flatten.uniq.compact + inst_maps = ng_map.instances_dataset.lives.all.map { |inst| inst.to_hash }.flatten.uniq.compact inst_maps end def get_network(network_id) - Models::Network.lock! network = Models::Network[network_id] raise "UnknownNetworkID" if network.nil? network.to_hash end + #Returns the current iplease for nic with uuid _nic_uuid_ + def get_iplease_for_nic(nic_uuid) + nic = Models::Taggable.find(nic_uuid) + Models::IpLease.find(:instance_nic_id => nic[:id])[:ipv4] + end + + def get_nat_leases(nic_uuid) + #TODO: get this to work with non canonical uuid + nic = Models::Taggable.find(nic_uuid) + + leases = Models::IpLease.filter({:instance_nic_id => nic[:id]} & ~{:network_id => nic[:network_id]}) + leases.map {|l| l[:ipv4]} + end + + def is_natted_ip?(ip) + lease = Models::IpLease.find(:ipv4 => ip) + + return false if lease.nil? + + #lease.instance_nic.network_id != lease.network_id + lease.is_natted? + end + def get_networks - Models::Network.lock! networks = Models::Network.all networks.map { |network| network.to_hash } end def get_dhcp_conf(network_name) - Models::Network.lock! - build_network_segment = proc { |network| gwaddr = network.ipaddress h = { :uuid => network.canonical_uuid, :ipv4_first => gwaddr.network.first.to_s, @@ -119,21 +138,21 @@ :metadata_server => network.metadata_server, :mac2addr => [], :addr2host=> [], } - network.ip_lease_dataset.filter(:type=>Models::IpLease::TYPE_AUTO).each { |ip| + network.ip_lease_dataset.filter(:alloc_type=>Models::IpLease::TYPE_AUTO).each { |ip| # ignore IPs unbound to vnic. next if ip.instance_nic.nil? || ip.instance_nic.instance.nil? h[:mac2addr] << { :mac_addr => ip.instance_nic.pretty_mac_addr, :ipaddr => ip.ipv4 } h[:addr2host] << { :hostname => ip.instance_nic.instance.fqdn_hostname, - :ipaddr => ip.ipv4 + :ipaddr => network.nat_network.nil? ? ip.ipv4 : ip.nat_outside_lease.ipv4 } } h } @@ -155,26 +174,27 @@ } h end def get_instances_of_netfilter_group(netfilter_group_id) - Models::NetfilterGroup.lock! g = Models::NetfilterGroup[netfilter_group_id] raise "UnknownNetfilterGroupID" if g.nil? - inst_maps = g.instance_netfilter_groups.map { |instance_netfilter_group| - instance_netfilter_group.instance_dataset.lives.all.map { |inst| inst.to_hash } - }.flatten.uniq.compact - inst_maps + g.instances.map {|i| i.to_hash }.flatten.uniq.compact end def get_alive_instances(node_id) - Models::HostPool.lock! hp = Models::HostPool.find(:node_id => node_id) - raise "UnknownNodeID", node_id if hp.nil? + if hp.nil? + logger.error("The node ID is not bound to HostPool yet: #{node_id}") + return [] + end hps = Models::HostPool.where(:account_id => hp.account_id).all inst_on_hp = hps.map { |hp| inst_on_hp = hp.instances_dataset.lives.all.map { |inst| - inst.to_hash + inst_map = inst.to_hash + # Does the hva have instance? + next unless inst_map[:host_pool][:node_id] == node_id + inst_map } }.flatten.uniq.compact inst_on_hp end