Class: Wakame::Agent

Inherits:
Object
  • Object
show all
Includes:
AMQPClient, QueueDeclare
Defined in:
lib/wakame/agent.rb

Instance Attribute Summary collapse

Attributes included from AMQPClient

#amqp_client, #mq, #queue_subscribers

Class Method Summary collapse

Instance Method Summary collapse

Methods included from QueueDeclare

included

Methods included from AMQPClient

#add_subscriber, #amq, #amqp_server_uri, #close, #connect, #connected?, #define_queue, included, #publish_to

Constructor Details

#initialize(opts = {}) ⇒ Agent

Returns a new instance of Agent.



26
27
28
29
# File 'lib/wakame/agent.rb', line 26

def initialize(opts={})
  @managers = {}
  determine_agent_id
end

Instance Attribute Details

#actor_managerObject (readonly)

Returns the value of attribute actor_manager.



20
21
22
# File 'lib/wakame/agent.rb', line 20

def actor_manager
  @actor_manager
end

#managersObject (readonly)

Returns the value of attribute managers.



20
21
22
# File 'lib/wakame/agent.rb', line 20

def managers
  @managers
end

#monitor_managerObject (readonly)

Returns the value of attribute monitor_manager.



20
21
22
# File 'lib/wakame/agent.rb', line 20

def monitor_manager
  @monitor_manager
end

Class Method Details

.ec2_fetch_local_attrsObject



80
81
82
83
84
85
86
87
88
# File 'lib/wakame/agent.rb', line 80

def self.ec2_fetch_local_attrs
  attrs = {}
  %w[instance-id instance-type local-ipv4 local-hostname public-hostname public-ipv4 ami-id].each { |key|
    rkey = key.tr('-', '_')
    attrs[rkey.to_sym]=(key)
  }
  attrs[:availability_zone] = ('placement/availability-zone')
  attrs
end

.ec2_query_metadata_uri(key) ⇒ Object



73
74
75
76
77
78
# File 'lib/wakame/agent.rb', line 73

def self.(key)
  require 'open-uri'
  open("http://169.254.169.254/2008-02-01/meta-data/#{key}") { |f|
    return f.readline
  }
end

Instance Method Details

#agent_idObject



22
23
24
# File 'lib/wakame/agent.rb', line 22

def agent_id
  @agent_id
end

#cleanupObject



49
50
51
# File 'lib/wakame/agent.rb', line 49

def cleanup
  publish_to('registry', Packets::UnRegister.new(self).marshal)
end

#determine_agent_idObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/wakame/agent.rb', line 53

def determine_agent_id
  if Wakame.config.environment == :EC2
    @agent_id = self.class.('instance-id')
  else
    # for Linux
    @nic = 'eth0'

    cmd = (`/sbin/ifconfig #{@nic}`).split(/\n+/)
    cmd[0] =~ %r/^#{@nic}\s+Link\sencap:Ethernet\s+HWaddr\s(\S+)\s+$/m
    @macaddr = $1

    cmd[1] =~ %r/^\s+inet addr:(\d+\.\d+\.\d+\.\d+).*$/m
    abort("Failed to get ipaddress") if cmd[1].nil?
    @ipaddr = $1

    @agent_id = "#{@ipaddr}-#{@macaddr}"
    @agent_id
  end
end

#initObject

post_setup



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/wakame/agent.rb', line 32

def init
  @monitor_manager = register_manager(AgentManagers::MonitorManager.new)
  @actor_manager = register_manager(AgentManagers::ActorManager.new)

  @managers.values.each { |mgr|
    mgr.init
  }

  if Wakame.config.environment == :EC2
    attrs = self.class.ec2_fetch_local_attrs
  else
    attrs = {}
  end
  publish_to('registry', Packets::Register.new(self, Wakame.config.root_path.to_s, attrs).marshal)
  Wakame.log.info("Started agent process : AMQP Server=#{amqp_server_uri.to_s} WAKAME_ROOT=#{Wakame.config.root_path} WAKAME_ENV=#{Wakame.config.environment}, attrs=#{attrs.inspect}")
end

#register_manager(agent_mgr) ⇒ Object

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
# File 'lib/wakame/agent.rb', line 91

def register_manager(agent_mgr)
  raise ArgumentError unless agent_mgr.kind_of? Wakame::AgentManager
  agent_mgr.agent = self
  raise "The manager module is registered: #{agent_mgr.class.to_s}" if @managers.has_key? agent_mgr.class.to_s
  @managers[agent_mgr.class.to_s] = agent_mgr
  agent_mgr
end

#unregister_manager(agent_mgr_name) ⇒ Object



99
100
101
# File 'lib/wakame/agent.rb', line 99

def unregister_manager(agent_mgr_name)
  @managers.delete(agent_mgr_name.to_s)
end