wakame 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/History.txt +4 -0
  2. data/LICENSE +202 -0
  3. data/Rakefile +11 -12
  4. data/VERSION +1 -1
  5. data/app_generators/wakame/templates/bin/wakame-agent +1 -1
  6. data/app_generators/wakame/templates/bin/wakame-master +1 -1
  7. data/app_generators/wakame/templates/config/cluster.rb +32 -24
  8. data/app_generators/wakame/templates/config/init.d/centos/wakame-agent +40 -0
  9. data/app_generators/wakame/templates/config/init.d/centos/wakame-master +40 -0
  10. data/app_generators/wakame/templates/config/init.d/wakame-master +1 -1
  11. data/lib/ext/eventmachine.rb +5 -5
  12. data/lib/wakame.rb +12 -0
  13. data/lib/wakame/action.rb +10 -11
  14. data/lib/wakame/actions/deploy_application.rb +61 -0
  15. data/lib/wakame/actions/deploy_config.rb +1 -3
  16. data/lib/wakame/actions/freeze_cluster.rb +15 -0
  17. data/lib/wakame/actions/launch_cluster.rb +1 -3
  18. data/lib/wakame/actions/launch_vm.rb +1 -1
  19. data/lib/wakame/actions/migrate_service.rb +4 -3
  20. data/lib/wakame/actions/notify_child_changed.rb +3 -6
  21. data/lib/wakame/actions/notify_parent_changed.rb +4 -7
  22. data/lib/wakame/actions/propagate_resource.rb +1 -3
  23. data/lib/wakame/actions/propagate_service.rb +1 -3
  24. data/lib/wakame/actions/register_agent.rb +43 -0
  25. data/lib/wakame/actions/reload_service.rb +2 -2
  26. data/lib/wakame/actions/shutdown_cluster.rb +4 -6
  27. data/lib/wakame/actions/shutdown_vm.rb +27 -6
  28. data/lib/wakame/actions/start_service.rb +40 -32
  29. data/lib/wakame/actions/stop_service.rb +8 -10
  30. data/lib/wakame/actions/unfreeze_cluster.rb +15 -0
  31. data/lib/wakame/actor.rb +2 -5
  32. data/lib/wakame/actor/deploy.rb +110 -0
  33. data/lib/wakame/actor/monitor.rb +14 -0
  34. data/lib/wakame/actor/s3fs.rb +45 -0
  35. data/lib/wakame/actor/service_monitor.rb +0 -17
  36. data/lib/wakame/actor/system.rb +5 -1
  37. data/lib/wakame/agent.rb +29 -179
  38. data/lib/wakame/agent_manager.rb +11 -0
  39. data/lib/wakame/agent_managers/actor_manager.rb +101 -0
  40. data/lib/wakame/agent_managers/monitor_manager.rb +48 -0
  41. data/lib/wakame/command.rb +4 -7
  42. data/lib/wakame/command/actor.rb +9 -12
  43. data/lib/wakame/command/agent_status.rb +2 -2
  44. data/lib/wakame/command/control_service.rb +66 -0
  45. data/lib/wakame/command/deploy_application.rb +18 -0
  46. data/lib/wakame/command/deploy_config.rb +16 -0
  47. data/lib/wakame/command/launch_cluster.rb +1 -1
  48. data/lib/wakame/command/launch_vm.rb +1 -1
  49. data/lib/wakame/command/propagate_resource.rb +1 -1
  50. data/lib/wakame/command/propagate_service.rb +5 -3
  51. data/lib/wakame/command/reload_service.rb +1 -1
  52. data/lib/wakame/command/shutdown_cluster.rb +1 -1
  53. data/lib/wakame/command/shutdown_vm.rb +37 -11
  54. data/lib/wakame/command/start_service.rb +1 -1
  55. data/lib/wakame/command/status.rb +6 -4
  56. data/lib/wakame/command/stop_service.rb +1 -1
  57. data/lib/wakame/configuration.rb +5 -0
  58. data/lib/wakame/event.rb +85 -33
  59. data/lib/wakame/event_dispatcher.rb +2 -2
  60. data/lib/wakame/initializer.rb +97 -31
  61. data/lib/wakame/master.rb +23 -346
  62. data/lib/wakame/master_manager.rb +11 -0
  63. data/lib/wakame/master_managers/action_manager.rb +321 -0
  64. data/lib/wakame/master_managers/agent_monitor.rb +166 -0
  65. data/lib/wakame/master_managers/cluster_manager.rb +176 -0
  66. data/lib/wakame/master_managers/command_queue.rb +133 -0
  67. data/lib/wakame/models/agent_pool.rb +113 -0
  68. data/lib/wakame/models/application_repository.rb +34 -0
  69. data/lib/wakame/models/object_store.rb +32 -0
  70. data/lib/wakame/models/service_cluster_pool.rb +36 -0
  71. data/lib/wakame/monitor.rb +3 -6
  72. data/lib/wakame/monitor/agent.rb +9 -6
  73. data/lib/wakame/monitor/service.rb +56 -29
  74. data/lib/wakame/runner/administrator_command.rb +210 -24
  75. data/lib/wakame/runner/agent.rb +2 -0
  76. data/lib/wakame/runner/master.rb +2 -1
  77. data/lib/wakame/service.rb +140 -130
  78. data/lib/wakame/status_db.rb +101 -121
  79. data/lib/wakame/util.rb +26 -15
  80. data/tests/setup_master.rb +1 -0
  81. data/tests/test_master.rb +0 -2
  82. data/tests/test_model_agent_pool.rb +21 -0
  83. data/tests/test_service.rb +14 -8
  84. data/tests/test_status_db.rb +2 -0
  85. data/tests/test_util.rb +12 -1
  86. data/wakame_generators/resource/templates/apache_app/apache_app.rb +20 -11
  87. data/wakame_generators/resource/templates/apache_app/conf/vh/aaa.test.conf +1 -1
  88. data/wakame_generators/resource/templates/apache_lb/apache_lb.rb +7 -7
  89. data/wakame_generators/resource/templates/apache_lb/conf/system-lb.conf +6 -4
  90. data/wakame_generators/resource/templates/apache_www/apache_www.rb +15 -13
  91. data/wakame_generators/resource/templates/ec2_elastic_ip/ec2_elastic_ip.rb +17 -17
  92. data/wakame_generators/resource/templates/ec2_elb/ec2_elb.rb +22 -15
  93. data/wakame_generators/resource/templates/mongodb/conf/mongodb.conf +95 -0
  94. data/wakame_generators/resource/templates/mongodb/init.d/mongodb +244 -0
  95. data/wakame_generators/resource/templates/mongodb/mongodb.rb +64 -0
  96. data/wakame_generators/resource/templates/mysql_master/mysql_master.rb +17 -21
  97. data/wakame_generators/resource/templates/nginx/conf/nginx.conf +4 -0
  98. data/wakame_generators/resource/templates/nginx/conf/vh/ec2_elb_common.conf +19 -0
  99. data/wakame_generators/resource/templates/nginx/init.d/nginx +6 -0
  100. data/wakame_generators/resource/templates/nginx/init.d/spawn-fcgi +46 -0
  101. data/wakame_generators/resource/templates/nginx/nginx.rb +15 -10
  102. data/wakame_generators/resource/templates/nginx_passenger/conf/nginx-passenger.conf +39 -0
  103. data/wakame_generators/resource/templates/nginx_passenger/conf/vh/ec2_elb_common.conf +10 -0
  104. data/wakame_generators/resource/templates/nginx_passenger/init.d/nginx-passenger +70 -0
  105. data/wakame_generators/resource/templates/nginx_passenger/nginx_passenger.rb +71 -0
  106. data/wakame_generators/resource/templates/s3fs/s3fs.rb +24 -0
  107. metadata +195 -74
  108. data/lib/wakame/action_manager.rb +0 -303
  109. data/lib/wakame/command/clone_service.rb +0 -12
  110. data/lib/wakame/command_queue.rb +0 -135
  111. data/lib/wakame/vm_manipulator.rb +0 -187
@@ -9,20 +9,15 @@ module Wakame
9
9
 
10
10
 
11
11
  def run
12
+ acquire_lock(@svc.resource.class.to_s)
13
+ @svc.reload
14
+
12
15
  # Skip to act when the service is having below status.
13
- if @svc.status == Service::STATUS_STOPPING || @svc.status == Service::STATUS_OFFLINE
16
+ if @svc.status == Service::STATUS_TERMINATE && @svc.monitor_status == Service::STATUS_OFFLINE
14
17
  Wakame.log.info("Ignore to stop the service as is being or already OFFLINE: #{@svc.resource.class}")
15
18
  return
16
19
  end
17
20
 
18
- acquire_lock { |lst|
19
- lst << @svc.resource.class.to_s
20
- }
21
-
22
- if @svc.resource.require_agent && !@svc.cloud_host.mapped?
23
- raise "Agent is not bound on this service : #{@svc}"
24
- end
25
-
26
21
  StatusDB.barrier {
27
22
  @svc.update_status(Service::STATUS_STOPPING)
28
23
  }
@@ -30,7 +25,10 @@ module Wakame
30
25
  trigger_action(NotifyChildChanged.new(@svc))
31
26
  flush_subactions
32
27
 
33
- @svc.resource.stop(@svc, self)
28
+ @svc.reload
29
+ if @svc.monitor_status == Wakame::Service::STATUS_ONLINE
30
+ @svc.resource.stop(@svc, self)
31
+ end
34
32
 
35
33
  if @do_terminate
36
34
  if @svc.resource.require_agent
@@ -0,0 +1,15 @@
1
+ module Wakame
2
+ module Actions
3
+ class UnfreezeCluster < Action
4
+
5
+ def run
6
+ acquire_lock(cluster.resources.keys.map {|resid| Service::Resource.find(resid).class.to_s })
7
+
8
+ StatusDB.barrier {
9
+ cluster.update_freeze_status(Service::ServiceCluster::STATUS_UNFROZEN)
10
+ }
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -20,13 +20,10 @@ module Wakame
20
20
  @exposed[path]=meth
21
21
  end
22
22
 
23
- def map_path
23
+ def map_path(path=nil)
24
+ @map_path = path if path
24
25
  @map_path ||= Util.snake_case(self.to_s.split('::').last)
25
26
  end
26
-
27
- def map_path=(name)
28
- @map_path=name
29
- end
30
27
  end
31
28
 
32
29
  end
@@ -0,0 +1,110 @@
1
+
2
+ require 'uri'
3
+ require 'fileutils'
4
+
5
+ class Wakame::Actor::Deploy
6
+ include Wakame::Actor
7
+
8
+ # Download the application from repo_uri using arbitrary SCM tool.
9
+ def checkout(ticket, repo_type, repo_uri, deploy_rev, app_root, app_name, options={})
10
+
11
+ case repo_type
12
+ when 's3'
13
+ tmp_dest = checkout_s3(repo_uri, deploy_rev, options)
14
+ when 'curl'
15
+ tmp_dest = checkout_curl(repo_uri, deploy_rev, options)
16
+ else
17
+ raise "Unsupported repository type: #{repo_type}"
18
+ end
19
+
20
+ dest = File.expand_path(File.join(app_name, ticket), app_root)
21
+
22
+ begin
23
+ FileUtils.mkpath(File.dirname(dest))
24
+ Wakame.log.debug("FileUtils.move('#{tmp_dest}', '#{dest}')")
25
+ FileUtils.move(tmp_dest, dest)
26
+
27
+ FileUtils.rm_f(File.join(File.dirname(dest), 'latest'))
28
+ FileUtils.symlink(File.basename(dest), File.join(File.dirname(dest), 'latest'))
29
+ rescue
30
+ FileUtils.rm_r(dest) rescue nil
31
+ end
32
+ end
33
+
34
+ # Swap the symlink "current" in /app_root/app_name to point the location as same as
35
+ # "latest" in the same folder.
36
+ def swap_current_link(app_root, app_name)
37
+ raise "Invalid application root path. Must be an absolute path: #{app_root}" unless app_root =~ /\A\//
38
+
39
+ latest_lnk_path = File.join(app_root, app_name, 'latest')
40
+ cur_lnk_path = File.join(app_root, app_name, 'current')
41
+ raise "'latest' symlink does not exist in #{File.join(app_root, app_name)}" unless File.symlink?(latest_lnk_path)
42
+
43
+
44
+ tgt = File.readlink(latest_lnk_path)
45
+ raise "'latest' symlink may point the target in differnt folder: #{tgt}" if tgt =~ /[\/]/
46
+
47
+ if File.symlink?(cur_lnk_path)
48
+ FileUtils.rm_f(cur_lnk_path)
49
+ end
50
+ FileUtils.symlink(tgt, cur_lnk_path)
51
+ end
52
+
53
+ private
54
+
55
+ def checkout_s3(repo_uri, deploy_rev, options)
56
+ require 'right_aws'
57
+ s3 = RightAws::S3Interface.new(options[:aws_access_key], options[:aws_secret_key])
58
+
59
+ bucket = nil
60
+ begin
61
+ u = URI.parse(repo_uri)
62
+ if u.host =~ /\A(.+)\.s3.amazonaws.com\Z/
63
+ bucket = $1
64
+ key = u.path.sub(/\A\//, '')
65
+ else
66
+ bucket, key = u.path.sub(/\A\//, '').split(/\//, 2)
67
+ end
68
+
69
+ key = key.nil? ? deploy_rev.dup : (key + deploy_rev)
70
+
71
+ rescue URI::InvalidURIError
72
+ # Assume that repo_uri has "bucket/key" syntax.
73
+ bucket, key = repo_uri.sub(/\A\//, '').split(/\//, 2)
74
+ key = key.nil? ? deploy_rev.dup : (key + deploy_rev)
75
+ end
76
+
77
+ tmp_dest = File.expand_path(File.basename(key), '/var/tmp')
78
+ dest = File.join(File.dirname(tmp_dest), 'aaa')
79
+ begin
80
+ Wakame.log.debug("Fetching archive from: s3.get: #{bucket}/#{key}")
81
+ tmp_f = File.new(tmp_dest, "w")
82
+ s3.get(bucket, key) { |buf|
83
+ tmp_f.write(buf)
84
+ }
85
+ tmp_f.close
86
+
87
+ Wakame::Util.exec('/usr/bin/unzip \'%s\' -d \'%s\'' % [tmp_dest, dest])
88
+ return dest
89
+ ensure
90
+ File.unlink(tmp_dest) rescue nil
91
+ end
92
+
93
+ end
94
+
95
+ def checkout_curl(repo_uri, deploy_rev, options)
96
+ src_uri = URI.parse(repo_uri + deploy_rev)
97
+ tmp_dest = File.expand_path(File.basename(src_uri.path), '/var/tmp')
98
+ dest = File.join(File.dirname(tmp_dest), 'aaa')
99
+
100
+ begin
101
+ Wakame::Util.exec('/usr/bin/curl -o \'%s\' \'%s\'' % [tmp_dest, src_uri])
102
+ raise "" unless File.exists?(tmp_dest)
103
+ Wakame::Util.exec('/usr/bin/unzip \'%s\' -d \'%s\'' % [tmp_dest, dest])
104
+ return dest
105
+ ensure
106
+ File.unlink(tmp_dest) rescue nil
107
+ end
108
+ end
109
+
110
+ end
@@ -0,0 +1,14 @@
1
+
2
+ class Wakame::Actor::Monitor
3
+ include Wakame::Actor
4
+
5
+ def reload(monitor_path, config)
6
+ mon = agent.monitor_manager.find_monitor(monitor_path) ||
7
+ raise("#{self.class}: The monitor namespace was not found: #{monitor_path}")
8
+
9
+ EM.barrier {
10
+ mon.reload(config)
11
+ }
12
+ end
13
+
14
+ end
@@ -0,0 +1,45 @@
1
+
2
+ require 'shellwords'
3
+ require 'ext/shellwords' unless Shellwords.respond_to? :shellescape
4
+
5
+ class Wakame::Actor::S3fs
6
+ include Wakame::Actor
7
+
8
+ def mount(bucket, path, opts={})
9
+ Wakame.log.debug("Wakame::Actor::S3fs.mount")
10
+ #return
11
+ raise "#{path} does not exist or not directory." unless File.directory?(path)
12
+ # 's3fs' or '/dev/xxx' ?
13
+ mount_point_dev=`/bin/df "#{path}" | /usr/bin/awk 'NR==2 {print $1}'`.strip
14
+ Wakame.log.debug("#{mount_point_dev}: #{bucket}, /bin/mount | awk '$3==path {print $1}' path=\"#{path}\"")
15
+
16
+ if mount_point_dev != 's3fs'
17
+ Wakame.log.debug("Mounting volume: #{bucket} as #{path} (with options: #{opts})")
18
+ Wakame::Util.exec("/usr/local/sbin/s3fs #{escape_mount_opts(opts)} '#{Shellwords.shellescape(bucket)}' '#{Shellwords.shellescape(path)}'")
19
+ else
20
+ Wakame.log.debug("Mounting s3fs bucket: #{bucket} as #{path} (with options: #{opts})")
21
+ end
22
+ end
23
+
24
+ def umount(path)
25
+ Wakame.log.debug("Wakame::Actor::S3fs.umount")
26
+ #return
27
+ raise "#{path} does not exist or not directory." unless File.directory?(path)
28
+
29
+ mount_point_dev=`/bin/df "#{path}" | awk 'NR==2 {print $1}'`
30
+ Wakame.log.debug("Unmounting volume: #{mount_point_dev} on #{path}")
31
+ Wakame::Util.exec("/bin/umount '#{Shellwords.shellescape(path)}'")
32
+ end
33
+
34
+ private
35
+ def escape_mount_opts(opts)
36
+ return '' if opts.nil?
37
+ return "-o '#{Shellwords.shellescape(opts)}'" if opts.is_a? String
38
+
39
+ out = opts.collect { |k,v|
40
+ v.nil? ? k : "#{k}=#{v}"
41
+ }.join(',')
42
+ "-o #{Shellwords.shellescape(out)}"
43
+ end
44
+
45
+ end
@@ -2,23 +2,6 @@
2
2
  class Wakame::Actor::ServiceMonitor
3
3
  include Wakame::Actor
4
4
 
5
- expose '/service_monitor/register', :register
6
- def register(svc_id, type, *args)
7
- EM.barrier {
8
- svcmon = agent.monitor_registry.find_monitor('/service')
9
- svcmon.register(svc_id, type, *args)
10
- }
11
- self.return_value = check_status(svc_id)
12
- end
13
-
14
- expose '/service_monitor/unregister', :unregister
15
- def unregister(svc_id)
16
- EM.barrier {
17
- svcmon = agent.monitor_registry.find_monitor('/service')
18
- svcmon.unregister(svc_id)
19
- }
20
- end
21
-
22
5
  # Immediate status check for the specified Service ID.
23
6
  def check_status(svc_id)
24
7
  self.return_value = EM.barrier {
@@ -1,7 +1,7 @@
1
1
 
2
-
3
2
  require 'shellwords'
4
3
  require 'ext/shellwords' unless Shellwords.respond_to? :shellescape
4
+ require 'fileutils'
5
5
 
6
6
  class Wakame::Actor::System
7
7
  include Wakame::Actor
@@ -43,6 +43,10 @@ class Wakame::Actor::System
43
43
  Wakame.log.debug("Unmounting volume: #{mount_point_dev} on #{path}")
44
44
  Wakame::Util.exec("/bin/umount '#{Shellwords.shellescape(path)}'")
45
45
  end
46
+
47
+ def touch(path)
48
+ FileUtils.touch(path)
49
+ end
46
50
 
47
51
  private
48
52
  def escape_mount_opts(opts)
@@ -9,35 +9,33 @@ require 'thread'
9
9
  require 'wakame'
10
10
  require 'wakame/amqp_client'
11
11
  require 'wakame/queue_declare'
12
- #require 'wakame/event'
13
- #require 'wakame/vm_manipulator'
14
-
15
12
 
16
13
  module Wakame
17
14
  class Agent
18
15
  include AMQPClient
19
16
  include QueueDeclare
20
17
 
21
- #define_queue 'agent_command.%{agent_id}', 'agent_command', {:key=>'agent_id.%{agent_id}', :auto_delete=>true}
22
18
  define_queue 'agent_actor.%{agent_id}', 'agent_command', {:key=>'agent_id.%{agent_id}', :auto_delete=>true}
23
19
 
24
- attr_reader :actor_registry, :monitor_registry
20
+ attr_reader :managers, :monitor_manager, :actor_manager
25
21
 
26
22
  def agent_id
27
23
  @agent_id
28
24
  end
29
25
 
30
26
  def initialize(opts={})
27
+ @managers = {}
31
28
  determine_agent_id
32
- @actor_registry = ActorRegistry.new
33
- @monitor_registry = MonitorRegistry.new
34
29
  end
35
30
 
36
31
  # post_setup
37
32
  def init
38
- setup_monitors
39
- setup_actors
40
- setup_dispatcher
33
+ @monitor_manager = register_manager(AgentManagers::MonitorManager.new)
34
+ @actor_manager = register_manager(AgentManagers::ActorManager.new)
35
+
36
+ @managers.values.each { |mgr|
37
+ mgr.init
38
+ }
41
39
 
42
40
  if Wakame.config.environment == :EC2
43
41
  attrs = self.class.ec2_fetch_local_attrs
@@ -45,78 +43,31 @@ module Wakame
45
43
  attrs = {}
46
44
  end
47
45
  publish_to('registry', Packets::Register.new(self, Wakame.config.root_path.to_s, attrs).marshal)
48
- Wakame.log.info("Started agent process : WAKAME_ROOT=#{Wakame.config.root_path} WAKAME_ENV=#{Wakame.config.environment}, attrs=#{attrs.inspect}")
46
+ 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}")
49
47
  end
50
48
 
51
- # def send_event_response(event)
52
- # Wakame.log.debug("Sending event to master : #{event.class}")
53
- # publish_to('agent_event', Marshal.dump(Packets::EventResponse.new(self, event)))
54
- # end
55
-
56
49
  def cleanup
57
50
  publish_to('registry', Packets::UnRegister.new(self).marshal)
58
- #@cmd_t.kill
59
51
  end
60
52
 
61
53
  def determine_agent_id
62
54
  if Wakame.config.environment == :EC2
63
55
  @agent_id = self.class.ec2_query_metadata_uri('instance-id')
64
56
  else
65
- @agent_id = '__STAND_ALONE__'
66
- end
67
- end
68
-
69
-
70
- def setup_monitors
71
- load_monitors
72
-
73
- @monitor_registry.register(Monitor::Agent.new, '/agent')
74
- @monitor_registry.register(Monitor::Service.new, '/service')
75
-
76
- @monitor_registry.monitors.each { |path, mon|
77
- mon.agent = self
78
- mon.setup(path)
79
- }
80
- end
81
-
82
- def load_monitors
83
- require 'wakame/monitor/agent'
84
- require 'wakame/monitor/service'
85
- end
86
-
87
- def setup_actors
88
- load_actors
57
+ # for Linux
58
+ @nic = 'eth0'
89
59
 
90
- @actor_registry.register(Actor::ServiceMonitor.new, '/service_monitor')
91
- @actor_registry.register(Actor::Daemon.new, '/daemon')
92
- @actor_registry.register(Actor::System.new, '/system')
93
- @actor_registry.register(Actor::MySQL.new, '/mysql')
94
- @actor_registry.actors.each { |path, actor|
95
- # actor.setup(path)
96
- actor.agent = self
97
- }
98
- end
99
-
100
- def load_actors
101
- require 'wakame/actor/service_monitor'
102
- require 'wakame/actor/daemon'
103
- require 'wakame/actor/system'
104
- require 'wakame/actor/mysql'
105
- end
60
+ cmd = (`/sbin/ifconfig #{@nic}`).split(/\n+/)
61
+ cmd[0] =~ %r/^#{@nic}\s+Link\sencap:Ethernet\s+HWaddr\s(\S+)\s+$/m
62
+ @macaddr = $1
106
63
 
64
+ cmd[1] =~ %r/^\s+inet addr:(\d+\.\d+\.\d+\.\d+).*$/m
65
+ abort("Failed to get ipaddress") if cmd[1].nil?
66
+ @ipaddr = $1
107
67
 
108
- def setup_dispatcher
109
- @dispatcher = Dispatcher.new(self)
110
-
111
- add_subscriber("agent_actor.#{agent_id}") { |data|
112
- begin
113
- request = eval(data)
114
- @dispatcher.handle_request(request)
115
- rescue => e
116
- Wakame.log.error(e)
117
- agent.publish_to('agent_event', Packets::ActorResponse.new(self, request[:token], Actor::STATUS_FAILED).marshal)
118
- end
119
- }
68
+ @agent_id = "#{@ipaddr}-#{@macaddr}"
69
+ @agent_id
70
+ end
120
71
  end
121
72
 
122
73
  def self.ec2_query_metadata_uri(key)
@@ -136,119 +87,18 @@ module Wakame
136
87
  attrs
137
88
  end
138
89
 
139
- end
140
-
141
-
142
- class ActorRegistry
143
- attr_reader :actors
144
- def initialize()
145
- @actors = {}
146
- end
147
-
148
- def register(actor, path=nil)
149
- raise '' unless actor.kind_of?(Wakame::Actor)
150
90
 
151
- if path.nil?
152
- path = '/' + Util.to_const_path(actor.class.to_s)
153
- end
154
-
155
- if @actors.has_key?(path)
156
- Wakame.log.error("#{self.class}: Duplicate registration: #{path}")
157
- raise "Duplicate registration: #{path}"
158
- end
159
-
160
- @actors[path] = actor
91
+ def register_manager(agent_mgr)
92
+ raise ArgumentError unless agent_mgr.kind_of? Wakame::AgentManager
93
+ agent_mgr.agent = self
94
+ raise "The manager module is registered: #{agent_mgr.class.to_s}" if @managers.has_key? agent_mgr.class.to_s
95
+ @managers[agent_mgr.class.to_s] = agent_mgr
96
+ agent_mgr
161
97
  end
162
98
 
163
- def unregister(path)
164
- @actors.delete(path)
165
- end
166
-
167
- def find_actor(path)
168
- @actors[path]
99
+ def unregister_manager(agent_mgr_name)
100
+ @managers.delete(agent_mgr_name.to_s)
169
101
  end
170
102
 
171
103
  end
172
-
173
-
174
- class MonitorRegistry
175
- attr_reader :monitors
176
- def initialize()
177
- @monitors = {}
178
- end
179
-
180
- def register(monitor, path=nil)
181
- raise '' unless monitor.kind_of?(Wakame::Monitor)
182
-
183
- if path.nil?
184
- path = '/' + Util.to_const_path(monitor.class.to_s)
185
- end
186
-
187
- if @monitors.has_key?(path)
188
- Wakame.log.error("#{self.class}: Duplicate registration: #{path}")
189
- raise "Duplicate registration: #{path}"
190
- end
191
-
192
- @monitors[path] = monitor
193
- end
194
-
195
- def unregister(path)
196
- @monitors.delete(path)
197
- end
198
-
199
- def find_monitor(path)
200
- @monitors[path]
201
- end
202
- end
203
-
204
-
205
- class Dispatcher
206
- attr_reader :agent
207
-
208
- def initialize(agent)
209
- @agent = agent
210
- end
211
-
212
- def handle_request(request)
213
- slash = request[:path].rindex('/')
214
- raise "Invalid request path: #{request[:path]}" unless slash
215
-
216
- prefix = request[:path][0, slash]
217
- action = request[:path][slash+1, request[:path].length]
218
-
219
- actor = agent.actor_registry.find_actor(prefix)
220
- unless actor
221
- Wakame.log.error("No refered actor instance: #{prefix}")
222
- raise
223
- end
224
-
225
- EM.defer(proc {
226
- begin
227
- Wakame.log.debug("#{self.class}: Started to run the actor: #{actor.class}, token=#{request[:token]}")
228
- agent.publish_to('agent_event', Packets::ActorResponse.new(agent, request[:token], Actor::STATUS_RUNNING).marshal)
229
- if request[:args].nil?
230
- actor.send(action)
231
- else
232
- actor.send(action, *request[:args])
233
- end
234
- Wakame.log.debug("#{self.class}: Finished to run the actor: #{actor.class}, token=#{request[:token]}")
235
- actor.return_value
236
- rescue => e
237
- Wakame.log.error("#{self.class}: Failed the actor: #{actor.class}, token=#{request[:token]}")
238
- Wakame.log.error(e)
239
- e
240
- end
241
- }, proc { |res|
242
- status = Actor::STATUS_SUCCESS
243
- if res.is_a?(Exception)
244
- status = Actor::STATUS_FAILED
245
- else
246
- opts = {:return_value=>res}
247
- end
248
- agent.publish_to('agent_event', Packets::ActorResponse.new(self.agent, request[:token], status, opts).marshal)
249
- })
250
- end
251
- end
252
-
253
-
254
- end
104
+ end