Class: Dcmgr::NodeModules::HvaCollector

Inherits:
Isono::NodeModules::Base
  • Object
show all
Includes:
Isono::NodeModules
Defined in:
lib/dcmgr/node_modules/hva_collector.rb

Instance Method Summary collapse

Instance Method Details

#get_alive_instances(node_id) ⇒ Object

Raises:

  • ("UnknownNodeID")


169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 169

def get_alive_instances(node_id)
  Models::HostPool.lock!
  hp = Models::HostPool.find(:node_id => node_id)
  raise "UnknownNodeID", node_id if hp.nil?
  hps = Models::HostPool.where(:account_id => hp.).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

#get_dhcp_conf(network_name) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 105

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,
      :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.,
      :mac2addr => [],
      :addr2host=> [],
    }
    
    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
  }

  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

#get_group_instance_ipv4s(instance_id) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 58

def get_group_instance_ipv4s(instance_id)
  Models::Instance.lock!
  inst = Models::Instance[instance_id]
  raise "UnknownInstanceID" if inst.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

#get_instance(instance_id) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 25

def get_instance(instance_id)
  Models::Instance.lock!
  inst = Models::Instance[instance_id]
  raise "UnknownInstanceID" if inst.nil?

  ret = inst.to_hash
  ret
end

#get_instances_of_account_netfilter_group(account_id, netfilter_group_name) ⇒ Object

def get_instances_of_account_netfilter_group(account_id, netfilter_group_id)



80
81
82
83
84
85
86
87
88
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 80

def (, netfilter_group_name)
  Models::NetfilterGroup.lock!
  ng_map = Models::NetfilterGroup.find(: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

#get_instances_of_netfilter_group(netfilter_group_id) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 159

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
end

#get_netfilter_groups_of_instance(instance_id) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 50

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

#get_network(network_id) ⇒ Object



90
91
92
93
94
95
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 90

def get_network(network_id)
  Models::Network.lock!
  network = Models::Network[network_id]
  raise "UnknownNetworkID" if network.nil?
  network.to_hash
end

#get_networksObject



97
98
99
100
101
102
103
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 97

def get_networks
  Models::Network.lock!
  networks = Models::Network.all
  networks.map { |network|
    network.to_hash
  }
end

#update_instance(instance_id, data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 34

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.
    # 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