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

- old
+ new

@@ -32,11 +32,19 @@ end def update_instance(instance_id, data) Models::Instance.lock! inst = Models::Instance[instance_id] - inst.set_fields(data, [:state]).save + raise "UnknownInstanceID" if inst.nil? + if data[:state] == :terminated + inst.terminated_at = data[:terminated_at] + # Instance#destroy do not really delete row. + # just for chain react destroy hooks in the associated models. + inst.destroy + else + inst.set(data).save + end # do not respond model object. nil end def get_netfilter_groups_of_instance(instance_id) @@ -50,18 +58,35 @@ def get_group_instance_ipv4s(instance_id) Models::Instance.lock! inst = Models::Instance[instance_id] raise "UnknownInstanceID" if inst.nil? - insts = inst.netfilter_group_instances - ips = insts.map { |instance| - next if instance.nil? - instance.ips.map { |ip| ip.ipv4 unless ip.nil? } - } + 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 + } + } + } + }.flatten.uniq.compact + ipv4s + end - ips.flatten! if ips.size > 0 - ips.compact +# 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 end def get_network(network_id) Models::Network.lock! network = Models::Network[network_id] @@ -77,56 +102,81 @@ } end def get_dhcp_conf(network_name) Models::Network.lock! - network = Models::Network.find(:name=>network_name) - raise "Unknown network name: #{network_name}" if network.nil? - prefix = IPAddress::Prefix32.new(network.prefix) - h = { - :ipv4_gw=>network.ipv4_gw, - :netmask => prefix.to_ip, - :prefix => network.prefix, - :dns_server=> network.dns_server, - :domain_name => network.domain_name, - :mac2addr => [], - :addr2host=> [], - } - - network.ip_lease.each { |ip| - # ignore IPs unbound to vnic. - next if ip.instance_nic.nil? || ip.instance_nic.instance.nil? + + build_network_segment = proc { |network| + gwaddr = network.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, + :prefix => network.prefix, + :dns_server=> network.dns_server, + :domain_name => network.domain_name, + :metadata_server => network.metadata_server, + :mac2addr => [], + :addr2host=> [], + } - h[:mac2addr] << { - :mac_addr => ip.instance_nic.pretty_mac_addr, - :ipaddr => ip.ipv4 + network.ip_lease_dataset.filter(: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 + } } - h[:addr2host] << { - :hostname => ip.instance_nic.instance.fqdn_hostname, - :ipaddr => ip.ipv4 - } + + h } + network_set = nil + Tags::NetworkPool + Models::Network + case network = Models::Taggable.find(network_name) + when Models::Network + network_set = [network] + 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) Models::NetfilterGroup.lock! g = Models::NetfilterGroup[netfilter_group_id] raise "UnknownNetfilterGroupID" if g.nil? - inst_maps = g.instances.map { |instance| instance.to_hash unless instance.nil? } - inst_maps.compact + 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 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? - - inst_on_hp = hp.instances_dataset.lives.all.map { |inst| - inst.to_hash - } - inst_on_hp.uniq! if inst_on_hp.size > 0 + 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 + } + }.flatten.uniq.compact inst_on_hp end end end