Class: Dcmgr::NodeModules::HvaCollector

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

Instance Method Summary collapse

Methods included from Logger

create, default_logdev, included

Instance Method Details

#get_alive_instances(node_id) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 184

def get_alive_instances(node_id)
  hp = Models::HostPool.find(:node_id => node_id)
  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.).all
  inst_on_hp = hps.map { |hp|
    inst_on_hp = hp.instances_dataset.lives.all.map { |inst|
      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

#get_dhcp_conf(network_name) ⇒ Object



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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 126

def get_dhcp_conf(network_name)
  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(: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   => network.nat_network.nil? ? ip.ipv4 : ip.nat_outside_lease.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, set = :inside) ⇒ Object

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



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

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|
        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
end

#get_instance(instance_id) ⇒ Object



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

def get_instance(instance_id)
  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)



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

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

#get_instances_of_netfilter_group(netfilter_group_id) ⇒ Object



178
179
180
181
182
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 178

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
end

#get_iplease_for_nic(nic_uuid) ⇒ Object

Returns the current iplease for nic with uuid nic_uuid



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

def get_iplease_for_nic(nic_uuid)
  nic = Models::Taggable.find(nic_uuid)
  Models::IpLease.find(:instance_nic_id => nic[:id])[:ipv4]
end

#get_nat_leases(nic_uuid) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 102

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

#get_netfilter_groups_of_instance(instance_id) ⇒ Object



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

def get_netfilter_groups_of_instance(instance_id)
  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
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 90

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

#get_networksObject



119
120
121
122
123
124
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 119

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

#is_natted_ip?(ip) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
# File 'lib/dcmgr/node_modules/hva_collector.rb', line 110

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

#update_instance(instance_id, data) ⇒ Object



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

def update_instance(instance_id, data)
  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