Class: Dcmgr::Models::Instance

Inherits:
AccountResource show all
Defined in:
lib/dcmgr/models/instance.rb

Overview

Model class which represents Virtual Machine or Isolated Instace running on HostPool.

Constant Summary

Constants inherited from BaseNew

BaseNew::LOCK_TABLES_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AccountResource

#account

Methods inherited from BaseNew

Proxy, dataset, install_data, install_data_hooks, unlock!

Class Method Details

.lock!Object



245
246
247
248
249
250
251
252
253
# File 'lib/dcmgr/models/instance.rb', line 245

def self.lock!
  super()
  Image.lock!
  InstanceSpec.lock!
  InstanceNic.lock!
  Volume.lock!
  VolumeSnapshot.lock!
  IpLease.lock!
end

Instance Method Details

#add_nic(vifname = nil, vendor_id = nil) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/dcmgr/models/instance.rb', line 172

def add_nic(vifname=nil, vendor_id=nil)
  vifname ||= "vif-#{self[:uuid]}"
  # TODO: get default vendor ID based on the hypervisor.
  vendor_id ||= '00:ff:f1'
  nic = InstanceNic.new({:vif=>vifname,
                          :mac_addr=>vendor_id
                        })
  nic.instance = self
  nic.save
end

#archObject

Returns the architecture type of the image



156
157
158
# File 'lib/dcmgr/models/instance.rb', line 156

def arch
  self.image.arch
end

#before_validationObject



60
61
62
63
64
65
# File 'lib/dcmgr/models/instance.rb', line 60

def before_validation
  super

  self[:user_data] = '' if self.user_data.nil?
  self[:hostname] = self.uuid if self.hostname.nil?
end

#configObject



168
169
170
# File 'lib/dcmgr/models/instance.rb', line 168

def config
  self.instance_spec.config
end

#cpu_coresObject



160
161
162
# File 'lib/dcmgr/models/instance.rb', line 160

def cpu_cores
  self.instance_spec.cpu_cores
end

#fqdn_hostnameObject



212
213
214
# File 'lib/dcmgr/models/instance.rb', line 212

def fqdn_hostname
  sprintf("%s.%s.%s", self.hostname, self..uuid, self.host_pool.network.domain_name)
end

#hypervisorObject

Returns the hypervisor type for the instance.



151
152
153
# File 'lib/dcmgr/models/instance.rb', line 151

def hypervisor
  self.host_pool.hypervisor
end

#ipsObject



201
202
203
# File 'lib/dcmgr/models/instance.rb', line 201

def ips
  self.instance_nic.map { |nic| nic.ip }
end

#join_netfilter_group(netfilter_group_uuids) ⇒ Object

Join this instance to the list of netfilter group using group’s uuid.

Parameters:

  • netfilter_group_uuids (String, Array)


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

def join_netfilter_group(netfilter_group_uuids)
  netfilter_group_uuids = [netfilter_group_uuids] if netfilter_group_uuids.is_a?(String)
  joined_group_uuids = self.netfilter_groups.map { |netfilter_group|
    netfilter_group.canonical_uuid
  }
  target_group_uuids = netfilter_group_uuids.uniq - joined_group_uuids.uniq
  target_group_uuids.uniq!

  target_group_uuids.map { |target_group_uuid|
    if ng = NetfilterGroup[target_group_uuid]
      InstanceNetfilterGroup.create(:instance_id => self.id,
                                    :netfilter_group_id => ng.id)
    end
  }
end

#join_nfgroup_by_name(account_id, nfgroup_names) ⇒ Object

Join this instance to the list of netfilter group using group name.

Parameters:

  • account_id (String)

    uuid of current account.

  • nfgroup_names (String, Array)


233
234
235
236
237
238
239
240
241
242
243
# File 'lib/dcmgr/models/instance.rb', line 233

def join_nfgroup_by_name(, nfgroup_names)
  nfgroup_names = [nfgroup_names] if nfgroup_names.is_a?(String)

  uuids = nfgroup_names.map { |n|
    ng = NetfilterGroup.for_update.filter(:account_id=>,
                                          :name=>n).first
    ng.nil? ? nil : ng.canonical_uuid
  }
  # clean up nils
  join_netfilter_group(uuids.compact.uniq)
end

#memory_sizeObject



164
165
166
# File 'lib/dcmgr/models/instance.rb', line 164

def memory_size
  self.instance_spec.memory_size
end

#netfilter_group_instancesObject



205
206
207
208
209
210
# File 'lib/dcmgr/models/instance.rb', line 205

def netfilter_group_instances
  instances = self.netfilter_groups.map { |g| g.instances }

  instances.flatten!.uniq! if instances.size > 0
  instances
end

#networksArray[Models::Network]

Retrieve all networks belong to this instance

Returns:



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/dcmgr/models/instance.rb', line 218

def networks
  instance_nic.select { |nic|
    !nic.ip.nil?
  }.map { |nic|
    nic.ip.network
  }.group_by { |net|
    net.name
  }.values.map { |i|
    i.first
  }
end

#to_api_documentObject

returns hash data for API response on GET instances/

{ :id=>

:cpu_cores
:memory_size
:image_id
:network => {'global1'=>{:ipaddr=>'111.111.111.111'}}
:volume => {'uuid'=>{:guest_device_name=>,}}
:ssh_key_pair => 'xxxxx',
:netfilter_group => ['rule1', 'rule2']
:created_at
:state
:status

}



103
104
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
# File 'lib/dcmgr/models/instance.rb', line 103

def to_api_document
  h = {
    :id => canonical_uuid,
    :cpu_cores   => instance_spec.cpu_cores,
    :memory_size => instance_spec.memory_size,
    :image_id    => image.canonical_uuid,
    :created_at  => self.created_at,
    :state => self.state,
    :status => self.status,
    :ssh_key_pair => nil,
    :network => [],
    :volume => [],
    :netfilter_group => [],
  }
  if self.ssh_key_pair
    h[:ssh_key_pair] = self.ssh_key_pair.name
  end

  if instance_nic
    instance_nic.each { |n|
      if n.ip
        h[:network] << {
          :network_name => n.ip.network.name,
          :ipaddr => n.ip.ipv4
        }
      end
    }
  end
  
  if self.volume
    self.volume.each { |v|
      h[:volume] << {
        :vol_id => v.canonical_uuid,
        :guest_device_name=>v.guest_device_name,
        :state=>v.state,
      }
    }
  end

  if self.netfilter_groups
    self.netfilter_groups.each { |n|
      h[:netfilter_group] << n.name
    }
  end
  h
end

#to_hashObject

dump column data as hash with details of associated models. this is for internal use.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dcmgr/models/instance.rb', line 69

def to_hash
  h = super
  h = h.merge({:user_data => user_data.to_s, # Sequel::BLOB -> String
                :runtime_config => self.runtime_config, # yaml -> hash
                :image=>image.to_hash,
                :host_pool=>host_pool.to_hash,
                :instance_nics=>instance_nic.map {|n| n.to_hash },
                :instance_spec=>instance_spec.to_hash,
              })
  h[:volume]={}
  if self.volume
    self.volume.each { |v|
      h[:volume][v.canonical_uuid] = v.to_hash_document
    }
  end
  h
end

#validateObject



54
55
56
57
58
# File 'lib/dcmgr/models/instance.rb', line 54

def validate
  super

  # TODO: hostname column validation
end