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

- old
+ new

@@ -44,15 +44,15 @@ end # do not respond model object. nil end - def get_netfilter_groups_of_instance(instance_id) + def get_security_groups_of_instance(instance_id) inst = Models::Instance[instance_id] raise "UnknownInstanceID" if inst.nil? - inst.netfilter_groups.map { |g| g.to_hash } + inst.security_groups.map { |g| g.to_hash } end #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 @@ -61,12 +61,14 @@ 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) - inst.netfilter_groups.compact.map { |netfilter_group| - netfilter_group.instances_dataset.lives.all.compact.map { |instance| + inst.security_groups.compact.map { |security_group| + # do not include 'stopped' or 'scheduling' instances as they + # are not allocated IP address. + security_group.instances_dataset.runnings.all.compact.map { |instance| instance.ips.compact.map { |ip| case set when :all ip when :inside @@ -77,27 +79,36 @@ } } }.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) - ng_map = Models::NetfilterGroup.find(:account_id => account_id, :name => netfilter_group_name) - raise "UnknownNetfilterGroupID" if ng_map.nil? - inst_maps = ng_map.instances_dataset.lives.all.map { |inst| inst.to_hash }.flatten.uniq.compact - inst_maps - end - def get_network(network_id) network = Models::Network[network_id] raise "UnknownNetworkID" if network.nil? network.to_hash end + def get_nic(nic_uuid) + nic = Models::Taggable.find(nic_uuid) + raise "UnknownNIC" if nic.nil? + nic.to_hash + end + + def get_instance_of_nic(nic_uuid) + nic = Models::Taggable.find(nic_uuid) + raise "UnknownNIC" if nic.nil? + + instance = nic.instance + raise "UnknownInstance" if instance.nil? + + instance.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) + raise "UnknownNIC" if nic.nil? 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 @@ -123,17 +134,17 @@ } end def get_dhcp_conf(network_name) build_network_segment = proc { |network| - gwaddr = network.ipaddress + nwaddr = network.ipv4_ipaddress h = { :uuid => network.canonical_uuid, - :ipv4_first => gwaddr.network.first.to_s, - :ipv4_last => gwaddr.network.last.to_s, - :ipv4_gw=>network.ipv4_gw, - :netmask => gwaddr.network.prefix.to_ip, + :ipv4_first => nwaddr.first.to_s, + :ipv4_last => nwaddr.last.to_s, + :ipv4_gw=> network.ipv4_gw, + :netmask => nwaddr.prefix.to_ip, :prefix => network.prefix, :dns_server=> network.dns_server, :domain_name => network.domain_name, :metadata_server => network.metadata_server, :mac2addr => [], @@ -147,12 +158,12 @@ h[:mac2addr] << { :mac_addr => ip.instance_nic.pretty_mac_addr, :ipaddr => ip.ipv4 } h[:addr2host] << { - :hostname => ip.instance_nic.instance.fqdn_hostname, - :ipaddr => network.nat_network.nil? ? ip.ipv4 : ip.nat_outside_lease.ipv4 + :hostname => ip.instance_nic.fqdn_hostname, + :ipaddr => ip.instance_nic.nat_network_id.nil? ? ip.ipv4 : ip.nat_outside_lease.ipv4 } } h } @@ -166,38 +177,68 @@ when Tags::NetworkPool network_set = network.mapped_uuids.map {|m| Models::Network[m.uuid] } else raise "Unknown network name: #{network_name}" end + h = {} network_set.each {|n| h[n.canonical_uuid] = build_network_segment.call(n) } h end - def get_instances_of_netfilter_group(netfilter_group_id) - g = Models::NetfilterGroup[netfilter_group_id] - raise "UnknownNetfilterGroupID" if g.nil? - g.instances.map {|i| i.to_hash }.flatten.uniq.compact + def get_instances_of_security_group(security_group_uuid) + sg_map = Models::SecurityGroup[security_group_uuid] + raise "Unknown security group ID: #{security_group_uuid}" if sg_map.nil? + # do not include 'stopped' or 'scheduling' instances as they + # are not allocated IP address. + inst_maps = sg_map.instances_dataset.runnings.all.map { |inst| inst.to_hash }.flatten.uniq.compact + inst_maps end def get_alive_instances(node_id) - hp = Models::HostPool.find(:node_id => node_id) + hp = Models::HostNode.find(:node_id => node_id) if hp.nil? - logger.error("The node ID is not bound to HostPool yet: #{node_id}") + logger.error("The node ID is not bound to HostNode yet: #{node_id}") return [] end - hps = Models::HostPool.where(:account_id => hp.account_id).all + hps = Models::HostNode.where(:account_id => hp.account_id).all inst_on_hp = hps.map { |hp| - inst_on_hp = hp.instances_dataset.lives.all.map { |inst| + inst_on_hp = hp.instances_dataset.runnings.all.map { |inst| inst_map = inst.to_hash # Does the hva have instance? - next unless inst_map[:host_pool][:node_id] == node_id + next unless inst_map[:host_node][:node_id] == node_id inst_map } }.flatten.uniq.compact inst_on_hp + end + + # Method designed to get all data needed for the netfilter service in a single request + def get_netfilter_data(node_id) + inst_maps = get_alive_instances(node_id) + + #Determine which security groups are in use + secg_uuids = inst_maps.map { |inst_map| + inst_map[:security_groups] + }.flatten.uniq + + secg_maps = Models::SecurityGroup.map { |secg| + secg.to_hash if secg_uuids.member? secg.canonical_uuid + }.compact + + # Trim out the data netfilter doesn't need and return a clean hash + { + :instances => inst_maps.map { |inst_map| + { + :uuid => inst_map[:uuid], + :security_groups => inst_map[:security_groups], + :vif => inst_map[:vif].map { |vif| vif.merge({:security_groups => inst_map[:security_groups]}) }# <-- Just putting the security groups in vif for the vnet isolator, gonna properly do this in the database asap + } + }, + :security_groups => secg_maps + } end end end end