Class: Wakame::Service::CloudHost

Inherits:
Wakame::StatusDB::Model show all
Defined in:
lib/wakame/service.rb

Constant Summary

Constants included from AttributeHelper

AttributeHelper::CLASS_TYPE_KEY, AttributeHelper::CONVERT_CLASSES, AttributeHelper::PRIMITIVE_CLASSES

Instance Method Summary collapse

Methods inherited from Wakame::StatusDB::Model

#delete, #dirty?, #id, inherited, #new_record?, #on_after_delete, #on_after_load, #on_before_delete, #on_before_load, #reload, #save

Methods included from AttributeHelper

#dump_attrs, #retrieve_attr_attribute

Instance Method Details

#agentObject



180
181
182
183
# File 'lib/wakame/service.rb', line 180

def agent
  raise "#{self.class}: Agent is not mapped yet to this cloud host \"#{self.id}\"." unless mapped?
  Agent.find(self.agent_id) || raise("#{self.class}: Could not find the mapped agent for #{self.class}.id=\"#{self.id}\"")
end

#agent_ipObject



235
236
237
# File 'lib/wakame/service.rb', line 235

def agent_ip
  self.agent.agent_ip
end

#assigned_resourcesObject



253
254
255
256
257
# File 'lib/wakame/service.rb', line 253

def assigned_resources()
  assigned_services.map { |svc_id|
    ServiceInstance.find(svc_id).resource
  }.uniq
end

#assigned_servicesObject



248
249
250
251
# File 'lib/wakame/service.rb', line 248

def assigned_services()
  cluster = ServiceCluster.find(self.cluster_id)
  cluster.services.keys.find_all { |svc_id| ServiceInstance.find(svc_id).cloud_host_id == self.id }
end

#has_resource_type?(key) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
242
243
244
245
246
# File 'lib/wakame/service.rb', line 239

def has_resource_type?(key)
  res_id = key.is_a?(ServiceInstance) ? key.resource.id : Resource.id(key)

  assigned_services.any? { |k|
    svc = ServiceInstance.find(k)
    svc.resource.id == res_id
  }
end

#live_monitors(live_stats = [Service::STATUS_ENTERING, Service::STATUS_STARTING, Service::STATUS_RUNNING]) ⇒ Object

Combine all the monitorring data from associated Resources. Example of return value: { ‘/service’ =>

  'svcid1' => {:type=>:pidfile, :path=>'/var/run/pid1.pid',
  'svcid2' => :cmdline=>'ps -ef | grep key'
}

}



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/wakame/service.rb', line 198

def live_monitors(live_stats=[Service::STATUS_ENTERING, Service::STATUS_STARTING, Service::STATUS_RUNNING])
  mon = {}
  c = ServiceCluster.find(cluster_id)
  c.services.keys.each { |svc_id|
    svc = ServiceInstance.find(svc_id)
    next unless svc.cloud_host_id == self.id
    next unless live_stats.member?(svc.status)

    svc.resource.monitors.each { |path, data|

      data.dup.each { |k, v|
        if v.is_a? String
          data[k] = instance_eval('"' + v.gsub(/%\{/, '#{') + '"')
        end
      }

      mon[path] ||= {}
      mon[path][svc_id] = data
    }
  }
  mon
end

#map_agent(agent) ⇒ Object

Raises:

  • (ArgumentError)


154
155
156
157
158
159
160
161
162
163
164
# File 'lib/wakame/service.rb', line 154

def map_agent(agent)
  raise ArgumentError unless agent.is_a?(Agent)
  raise "Ensure to call unmap_agent() prior to mapping new agent: #{self.agent_id}" if self.mapped?

  self.agent_id = agent.id
  agent.cloud_host_id = self.id

  self.save
  agent.save
  ED.fire_event(Event::AgentMappedCloudHost.new(self.id, agent.id))
end

#mapped?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/wakame/service.rb', line 150

def mapped?
  !self.agent_id.nil?
end

#reported_servicesObject



227
228
229
# File 'lib/wakame/service.rb', line 227

def reported_services
  self.agent.reported_services
end

#root_pathObject



231
232
233
# File 'lib/wakame/service.rb', line 231

def root_path
  self.agent.root_path
end

#statusObject

Delegate methods for Agent class



223
224
225
# File 'lib/wakame/service.rb', line 223

def status
  self.agent.status
end

#unmap_agentObject



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/wakame/service.rb', line 166

def unmap_agent()
  if mapped?
    agent = Agent.find(self.agent_id)
    if agent && self.agent_id == agent.id
      agent.cloud_host_id = nil
      agent.save
    end

    self.agent_id = nil
    self.save
    ED.fire_event(Event::AgentUnmappedCloudHost.new(self.id, agent.id))
  end
end

#validate_on_saveObject



259
260
261
# File 'lib/wakame/service.rb', line 259

def validate_on_save
  raise "#{self.class}.cluster_id property can't be nil." if self.cluster_id.nil?
end

#vm_specObject



185
186
187
188
189
# File 'lib/wakame/service.rb', line 185

def vm_spec
  spec = VmSpec.current
  spec.table = self.vm_attr
  spec
end