Class: Wakame::Service::Agent

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

Overview

Data model for agent Status life cycle: STATUS_INIT -> [STATUS_ONLINE|STATUS_OFFLINE|STATUS_TIMEOUT] -> STATUS_END

Constant Summary collapse

STATUS_END =
-1
STATUS_INIT =
-2
STATUS_OFFLINE =
0
STATUS_ONLINE =
1
STATUS_UNKNOWN =
2
STATUS_TIMEOUT =
3
STATUS_RUNNING =
4
STATUS_REGISTERRING =
5
STATUS_TERMINATING =
6

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?, 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

#actor_request(path, *args) ⇒ Object



102
103
104
# File 'lib/wakame/service.rb', line 102

def actor_request(path, *args)
  Master.instance.actor_request(self.id, path, *args)
end

#agent_ipObject



63
64
65
# File 'lib/wakame/service.rb', line 63

def agent_ip
  vm_attr[:private_dns_name]
end

#cloud_hostObject



71
72
73
# File 'lib/wakame/service.rb', line 71

def cloud_host
  self.cloud_host_id.nil? ? nil : CloudHost.find(self.cloud_host_id)
end

#has_resource_type?(key) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
# File 'lib/wakame/service.rb', line 113

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

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

#idObject



59
60
61
# File 'lib/wakame/service.rb', line 59

def id
  @id || raise("Agent.id is unset: #{self}")
end

#id=(agent_id) ⇒ Object



55
56
57
# File 'lib/wakame/service.rb', line 55

def id=(agent_id)
  @id = agent_id
end

#last_ping_at_timeObject

Tentative…



76
77
78
79
# File 'lib/wakame/service.rb', line 76

def last_ping_at_time
  require 'time'
  Time.parse(last_ping_at)
end

#mapped?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/wakame/service.rb', line 51

def mapped?
  !self.cloud_host_id.nil?
end

#renew_reported_services(svc_id_list) ⇒ Object



106
107
108
109
110
111
# File 'lib/wakame/service.rb', line 106

def renew_reported_services(svc_id_list)
  reported_services.clear
  svc_id_list.each { |svc_id, data|
    reported_services[svc_id] = data
  }
end

#terminateObject



122
123
124
125
126
127
128
# File 'lib/wakame/service.rb', line 122

def terminate
  if mapped?
    raise "#{self.class}[#{self.id}]: CloudHost is assigned live services." if cloud_host.assigned_services.all? {|svc_id| ServiceInstance.find(svc_id).status == Service::STATUS_TERMINATE }
    CloudHost.delete(cloud_host_id)
  end
  self.delete
end

#update_monitor_status(new_status) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/wakame/service.rb', line 88

def update_monitor_status(new_status)
  if @monitor_status != new_status
    @monitor_status = new_status
    self.save

    ED.fire_event(Event::AgentStatusChanged.new(self))
    # Send status specific event
    case @status
    when STATUS_TIMEOUT
      ED.fire_event(Event::AgentTimedOut.new(self))
    end
  end
end

#update_status(new_status) ⇒ Object



81
82
83
84
85
86
# File 'lib/wakame/service.rb', line 81

def update_status(new_status)
  if @status != new_status
    @status = new_status
    self.save
  end
end

#update_vm_attrObject



130
131
132
133
134
135
136
137
138
# File 'lib/wakame/service.rb', line 130

def update_vm_attr
  if Wakame.config.environment == :EC2
    require 'right_aws'
    ec2 = RightAws::Ec2.new(Wakame.config.aws_access_key, Wakame.config.aws_secret_key)
    dat = ec2.describe_instances([self.id])
    @vm_attr = dat[0]
  end
  self.save
end

#vm_idObject



67
68
69
# File 'lib/wakame/service.rb', line 67

def vm_id
  vm_attr[:aws_instance_id]
end