wakame 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. data/History.txt +18 -0
  2. data/README.rdoc +2 -4
  3. data/Rakefile +4 -1
  4. data/VERSION +1 -1
  5. data/app_generators/wakame/templates/config/cluster.rb +36 -0
  6. data/app_generators/wakame/wakame_generator.rb +1 -0
  7. data/lib/wakame/action.rb +19 -34
  8. data/lib/wakame/actions/launch_cluster.rb +6 -3
  9. data/lib/wakame/actions/launch_vm.rb +57 -0
  10. data/lib/wakame/actions/migrate_service.rb +6 -25
  11. data/lib/wakame/actions/propagate_instances.rb +16 -41
  12. data/lib/wakame/actions/reload_service.rb +2 -2
  13. data/lib/wakame/actions/shutdown_cluster.rb +4 -0
  14. data/lib/wakame/actions/start_service.rb +30 -9
  15. data/lib/wakame/actions/stop_service.rb +1 -4
  16. data/lib/wakame/actions/util.rb +0 -24
  17. data/lib/wakame/actor/mysql.rb +106 -0
  18. data/lib/wakame/actor/service_monitor.rb +10 -0
  19. data/lib/wakame/actor/system.rb +19 -9
  20. data/lib/wakame/actor.rb +1 -1
  21. data/lib/wakame/agent.rb +23 -17
  22. data/lib/wakame/command/action_status.rb +7 -38
  23. data/lib/wakame/command/agent_status.rb +18 -0
  24. data/lib/wakame/command/launch_cluster.rb +0 -4
  25. data/lib/wakame/command/launch_vm.rb +11 -0
  26. data/lib/wakame/command/migrate_service.rb +12 -9
  27. data/lib/wakame/command/propagate_service.rb +15 -7
  28. data/lib/wakame/command/reload_service.rb +21 -0
  29. data/lib/wakame/command/shutdown_cluster.rb +0 -5
  30. data/lib/wakame/command/shutdown_vm.rb +20 -0
  31. data/lib/wakame/command/status.rb +6 -66
  32. data/lib/wakame/command/stop_service.rb +31 -0
  33. data/lib/wakame/command.rb +3 -0
  34. data/lib/wakame/command_queue.rb +76 -5
  35. data/lib/wakame/configuration.rb +6 -1
  36. data/lib/wakame/event.rb +6 -5
  37. data/lib/wakame/event_dispatcher.rb +5 -3
  38. data/lib/wakame/initializer.rb +1 -1
  39. data/lib/wakame/master.rb +14 -10
  40. data/lib/wakame/monitor/agent.rb +1 -6
  41. data/lib/wakame/monitor/service.rb +13 -1
  42. data/lib/wakame/packets.rb +51 -13
  43. data/lib/wakame/rule_engine.rb +7 -18
  44. data/lib/wakame/runner/administrator_command.rb +517 -46
  45. data/lib/wakame/service.rb +104 -173
  46. data/lib/wakame/trigger.rb +1 -10
  47. data/lib/wakame/triggers/process_command.rb +9 -2
  48. data/lib/wakame/util.rb +21 -21
  49. data/lib/wakame/vm_manipulator.rb +1 -0
  50. data/tests/test_service.rb +30 -6
  51. data/wakame_generators/resource/templates/ec2_elb/ec2_elb.rb +80 -0
  52. data/wakame_generators/resource/templates/mysql_master/conf/my.cnf +3 -1
  53. data/wakame_generators/resource/templates/mysql_master/init.d/mysql +3 -3
  54. data/wakame_generators/resource/templates/mysql_master/mysql_master.rb +11 -12
  55. data/{app_generators/wakame/templates/cluster/resources/mysql_master → wakame_generators/resource/templates/mysql_slave}/conf/my.cnf +19 -9
  56. data/{app_generators/wakame/templates/cluster/resources/mysql_master/init.d/mysql → wakame_generators/resource/templates/mysql_slave/init.d/mysql-slave} +4 -4
  57. data/wakame_generators/resource/templates/mysql_slave/mysql_slave.rb +123 -0
  58. data/wakame_generators/resource/templates/nginx/conf/nginx.conf +166 -0
  59. data/wakame_generators/resource/templates/nginx/init.d/nginx +70 -0
  60. data/{app_generators/wakame/templates/cluster/resources/apache_lb/apache_lb.rb → wakame_generators/resource/templates/nginx/nginx.rb} +28 -17
  61. metadata +46 -27
  62. data/app_generators/wakame/templates/cluster/resources/apache_app/apache_app.rb +0 -54
  63. data/app_generators/wakame/templates/cluster/resources/apache_app/conf/apache2.conf +0 -46
  64. data/app_generators/wakame/templates/cluster/resources/apache_app/conf/envvars-app +0 -7
  65. data/app_generators/wakame/templates/cluster/resources/apache_app/conf/sites-app.conf +0 -23
  66. data/app_generators/wakame/templates/cluster/resources/apache_app/conf/system-app.conf +0 -67
  67. data/app_generators/wakame/templates/cluster/resources/apache_app/init.d/apache2-app +0 -192
  68. data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/apache2.conf +0 -46
  69. data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/envvars-lb +0 -6
  70. data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/sites-lb.conf +0 -54
  71. data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/system-lb.conf +0 -75
  72. data/app_generators/wakame/templates/cluster/resources/apache_lb/init.d/apache2-lb +0 -192
  73. data/app_generators/wakame/templates/cluster/resources/apache_www/apache_www.rb +0 -50
  74. data/app_generators/wakame/templates/cluster/resources/apache_www/conf/apache2.conf +0 -47
  75. data/app_generators/wakame/templates/cluster/resources/apache_www/conf/envvars-www +0 -7
  76. data/app_generators/wakame/templates/cluster/resources/apache_www/conf/sites-www.conf +0 -23
  77. data/app_generators/wakame/templates/cluster/resources/apache_www/conf/system-www.conf +0 -63
  78. data/app_generators/wakame/templates/cluster/resources/apache_www/init.d/apache2-www +0 -192
  79. data/app_generators/wakame/templates/cluster/resources/ec2_elastic_ip/ec2_elastic_ip.rb +0 -39
  80. data/app_generators/wakame/templates/cluster/resources/mysql_master/mysql_master.rb +0 -174
  81. data/tests/conf/a +0 -1
  82. data/tests/conf/b +0 -1
  83. data/tests/conf/c +0 -1
@@ -8,6 +8,7 @@ class Wakame::Actor::ServiceMonitor
8
8
  svcmon = agent.monitor_registry.find_monitor('/service')
9
9
  svcmon.register(svc_id, type, *args)
10
10
  }
11
+ self.return_value = check_status(svc_id)
11
12
  end
12
13
 
13
14
  expose '/service_monitor/unregister', :unregister
@@ -18,4 +19,13 @@ class Wakame::Actor::ServiceMonitor
18
19
  }
19
20
  end
20
21
 
22
+ # Immediate status check for the specified Service ID.
23
+ def check_status(svc_id)
24
+ self.return_value = EM.barrier {
25
+ svcmon = agent.monitor_registry.find_monitor('/service')
26
+ svcmon.check_status(svc_id)
27
+ }
28
+ self.return_value
29
+ end
30
+
21
31
  end
@@ -5,22 +5,32 @@ require 'ext/shellwords' unless Shellwords.respond_to? :shellescape
5
5
 
6
6
  class Wakame::Actor::System
7
7
  include Wakame::Actor
8
-
8
+
9
+ def sync(count = 1)
10
+ count.to_i.times do |i|
11
+ Wakame.log.debug("Wakame::Actor::System.sync #{i + 1}/#{count}")
12
+ Wakame::Util.exec("/bin/sync")
13
+ end
14
+ end
15
+
9
16
  def mount(dev, path, opts={})
10
- raise "#{path} does not exist or not directory." unless File.directory?(path)
17
+ Wakame.log.debug("Wakame::Actor::System.mount")
18
+ cond = ConditionalWait.new { |c|
19
+ c.poll {
20
+ self.sync
21
+ File.blockdev?(dev)
22
+ }
23
+ }
24
+ cond.wait
25
+
26
+ raise "#{dev} does not exist or not block device." unless File.blockdev?(dev)
27
+ raise "#{path} does not exist or not directory." unless File.directory?(path)
11
28
 
12
29
  mount_point_dev=`/bin/df "#{path}" | /usr/bin/awk 'NR==2 {print $1}'`.strip
13
-
14
- #mount_point_dev=`/bin/mount | /usr/bin/awk '$3==path {print $1}' path="#{path}"`.strip
15
30
  Wakame.log.debug("#{mount_point_dev}: #{dev}, /bin/mount | awk '$3==path {print $1}' path=\"#{path}\"")
16
31
  if mount_point_dev != dev
17
32
  Wakame.log.debug("Mounting volume: #{dev} as #{path} (with options: #{opts})")
18
33
  Wakame::Util.exec("/bin/mount -o #{mount_opts(opts)} '#{Shellwords.shellescape(dev)}' '#{Shellwords.shellescape(path)}'")
19
- # sync
20
- 3.times do |i|
21
- system("/bin/sync")
22
- sleep 1.0
23
- end
24
34
  else
25
35
  Wakame.log.debug("Mounting EBS volume: #{dev} as #{path} (with options: #{opts})")
26
36
  end
data/lib/wakame/actor.rb CHANGED
@@ -10,7 +10,7 @@ module Wakame
10
10
  def self.included(klass)
11
11
  klass.extend ClassMethods
12
12
  klass.class_eval {
13
- attr_accessor :agent
13
+ attr_accessor :agent, :return_value
14
14
  }
15
15
  end
16
16
 
data/lib/wakame/agent.rb CHANGED
@@ -38,8 +38,9 @@ module Wakame
38
38
  setup_actors
39
39
  setup_dispatcher
40
40
 
41
- publish_to('registry', Packets::Register.new(self, Wakame.config.root_path.to_s).marshal)
42
- Wakame.log.info("Started agent process : WAKAME_ROOT=#{Wakame.config.root_path} WAKAME_ENV=#{Wakame.config.environment}")
41
+ attrs = Wakame::VmManipulator.create.fetch_local_attrs
42
+ publish_to('registry', Packets::Register.new(self, Wakame.config.root_path.to_s, attrs).marshal)
43
+ Wakame.log.info("Started agent process : WAKAME_ROOT=#{Wakame.config.root_path} WAKAME_ENV=#{Wakame.config.environment}, attrs=#{attrs.inspect}")
43
44
  end
44
45
 
45
46
  # def send_event_response(event)
@@ -84,6 +85,7 @@ module Wakame
84
85
  @actor_registry.register(Actor::ServiceMonitor.new, '/service_monitor')
85
86
  @actor_registry.register(Actor::Daemon.new, '/daemon')
86
87
  @actor_registry.register(Actor::System.new, '/system')
88
+ @actor_registry.register(Actor::MySQL.new, '/mysql')
87
89
  @actor_registry.actors.each { |path, actor|
88
90
  # actor.setup(path)
89
91
  actor.agent = self
@@ -94,6 +96,7 @@ module Wakame
94
96
  require 'wakame/actor/service_monitor'
95
97
  require 'wakame/actor/daemon'
96
98
  require 'wakame/actor/system'
99
+ require 'wakame/actor/mysql'
97
100
  end
98
101
 
99
102
 
@@ -198,26 +201,29 @@ module Wakame
198
201
  end
199
202
 
200
203
  EM.defer(proc {
201
- return begin
202
- Wakame.log.debug("#{self.class}: Started to run the actor: #{actor.class}, token=#{request[:token]}")
203
- agent.publish_to('agent_event', Packets::ActorResponse.new(agent, request[:token], Actor::STATUS_RUNNING).marshal)
204
- if request[:args].nil?
205
- actor.send(action)
206
- else
207
- actor.send(action, *request[:args])
208
- end
209
- Wakame.log.debug("#{self.class}: Finished to run the actor: #{actor.class}, token=#{request[:token]}")
210
- rescue => e
211
- Wakame.log.error("#{self.class}: Failed the actor: #{actor.class}, token=#{request[:token]}")
212
- Wakame.log.error(e)
213
- e
214
- end
204
+ begin
205
+ Wakame.log.debug("#{self.class}: Started to run the actor: #{actor.class}, token=#{request[:token]}")
206
+ agent.publish_to('agent_event', Packets::ActorResponse.new(agent, request[:token], Actor::STATUS_RUNNING).marshal)
207
+ if request[:args].nil?
208
+ actor.send(action)
209
+ else
210
+ actor.send(action, *request[:args])
211
+ end
212
+ Wakame.log.debug("#{self.class}: Finished to run the actor: #{actor.class}, token=#{request[:token]}")
213
+ actor.return_value
214
+ rescue => e
215
+ Wakame.log.error("#{self.class}: Failed the actor: #{actor.class}, token=#{request[:token]}")
216
+ Wakame.log.error(e)
217
+ e
218
+ end
215
219
  }, proc { |res|
216
220
  status = Actor::STATUS_SUCCESS
217
221
  if res.is_a?(Exception)
218
222
  status = Actor::STATUS_FAILED
223
+ else
224
+ opts = {:return_value=>res}
219
225
  end
220
- agent.publish_to('agent_event', Packets::ActorResponse.new(self.agent, request[:token], status).marshal)
226
+ agent.publish_to('agent_event', Packets::ActorResponse.new(self.agent, request[:token], status, opts).marshal)
221
227
  })
222
228
  end
223
229
  end
@@ -4,21 +4,6 @@ require 'erb'
4
4
  class Wakame::Command::ActionStatus
5
5
  include Wakame::Command
6
6
 
7
- ACTION_STATUS_TMPL=<<__E__
8
- Running Actions : <%= @status.size %> action(s)
9
- <%- if @status.size > 0 -%>
10
- <%- @status.each { |id, j| -%>
11
- JOB <%= id %> :
12
- start : <%= j[:created_at] %>
13
- <%= tree_subactions(j[:root_action]) %>
14
- <%- } -%>
15
- <%- end -%>
16
- __E__
17
-
18
-
19
- def parse(args)
20
- end
21
-
22
7
  def run(rule)
23
8
  walk_subactions = proc { |a, level|
24
9
  res = a.dump_attrs
@@ -29,34 +14,18 @@ __E__
29
14
  end
30
15
  res
31
16
  }
32
-
17
+
33
18
  EM.barrier {
34
19
  result = {}
35
- rule.master.service_cluster.rule_engine.active_jobs.each { |id, v|
36
- result[id]={:actions=>[], :created_at=>v[:created_at], :src_rule=>v[:src_rule].class.to_s}
37
-
38
- result[id][:root_action] = walk_subactions.call(v[:root_action], 0)
39
- }
40
-
41
- @status = result
42
- }
43
- end
44
20
 
45
- def print_result
46
- puts ERB.new(ACTION_STATUS_TMPL, nil, '-').result(binding)
47
- end
21
+ rule.rule_engine.active_jobs.each { |id, v|
48
22
 
23
+ result[id]={:created_at=>v[:created_at], :src_rule=>v[:src_rule].class.to_s}
24
+ result[id][:root_action] = walk_subactions.call(v[:root_action], 0)
49
25
 
50
- private
51
- def tree_subactions(root, level=0)
52
- str= (" " * level) + "#{root[:type]} (#{root[:status]})"
53
- unless root[:subactions].nil?
54
- root[:subactions].each { |a|
55
- str << "\n "
56
- str << tree_subactions(a, level + 1)
57
26
  }
58
- end
59
- str
27
+ @status = result
28
+ @status
29
+ }
60
30
  end
61
-
62
31
  end
@@ -0,0 +1,18 @@
1
+
2
+ class Wakame::Command::AgentStatus
3
+ include Wakame::Command
4
+
5
+ command_name='agent_status'
6
+
7
+ def run(rule)
8
+ EM.barrier {
9
+ registered_agents = rule.agent_monitor.registered_agents[@options["agent_id"]]
10
+ service_cluster = rule.service_cluster
11
+ res ={
12
+ :agent_status =>registered_agents.dump_status,
13
+ :service_cluster =>service_cluster.dump_status
14
+ }
15
+ res
16
+ }
17
+ end
18
+ end
@@ -5,11 +5,7 @@ class Wakame::Command::LaunchCluster
5
5
 
6
6
  command_name='launch_cluster'
7
7
 
8
- def parse(args)
9
- end
10
-
11
8
  def run(rule)
12
9
  rule.trigger_action(Wakame::Actions::LaunchCluster.new)
13
10
  end
14
-
15
11
  end
@@ -0,0 +1,11 @@
1
+
2
+ class Wakame::Command::LaunchVm
3
+ include Wakame::Command
4
+
5
+ command_name='launch_vm'
6
+
7
+ def run(rule)
8
+ inst_id_key = "new_inst_id_" + Wakame::Util.gen_id
9
+ rule.trigger_action(Wakame::Actions::LaunchVM.new(inst_id_key))
10
+ end
11
+ end
@@ -2,20 +2,23 @@
2
2
  class Wakame::Command::MigrateService
3
3
  include Wakame::Command
4
4
 
5
- #command_name='launch_cluster'
6
-
7
- def parse(args)
8
- @svc_id = args.shift
9
- end
10
-
11
5
  def run(rule)
12
6
  svc = nil
13
- svc = rule.service_cluster.instances[@svc_id]
7
+ svc = rule.service_cluster.instances[@options["service_id"]]
14
8
  if svc.nil?
15
- raise "Unknown Service ID: #{@svc_id}"
9
+ raise "Unknown Service ID: #{@options["service_id"]}"
10
+ end
11
+
12
+ # Optional destination agent
13
+ agent = nil
14
+ if @options["agent_id"]
15
+ agent = rule.agent_monitor.agent(@options["agent_id"])
16
+ if agent.nil?
17
+ raise "Unknown Agent ID: #{@options["agent_id"]}"
18
+ end
16
19
  end
17
20
 
18
- rule.trigger_action(Wakame::Actions::MigrateService.new(svc))
21
+ rule.trigger_action(Wakame::Actions::MigrateService.new(svc, agent))
19
22
  end
20
23
 
21
24
  end
@@ -5,20 +5,28 @@ class Wakame::Command::PropagateService
5
5
  #command_name='launch_cluster'
6
6
 
7
7
  def parse(args)
8
- @resname = args.shift
9
- @num = args.shift unless args.empty?
8
+ @resource
10
9
  end
11
10
 
12
11
  def run(rule)
13
12
  prop = nil
14
- prop = rule.service_cluster.properties[@resname.to_s]
13
+ prop = rule.service_cluster.properties[@options["service"]]
15
14
  if prop.nil?
16
- raise "UnknownProperty: #{@resname}"
15
+ raise "UnknownProperty: #{@options["service"]}"
17
16
  end
18
17
 
19
- @num ||= 1
18
+ @num = nil
19
+ @num = @options["num"] || 1
20
20
 
21
- rule.trigger_action(Wakame::Actions::PropagateInstances.new(prop, @num))
22
- end
21
+ unless /^(\d){1,32}$/ =~ @num.to_s
22
+ raise "The number is not appropriate: #{@num}"
23
+ end
23
24
 
25
+ instance = rule.master.service_cluster.dump_status[:properties][@options["service"]]
26
+ if @num.to_i > (instance[:max_instances].to_i - instance[:instances].count.to_i)
27
+ raise "The number is not appropriate: #{@num}"
28
+ end
29
+
30
+ rule.trigger_action(Wakame::Actions::PropagateInstances.new(prop, @num.to_i))
31
+ end
24
32
  end
@@ -0,0 +1,21 @@
1
+
2
+ class Wakame::Command::ReloadService
3
+ include Wakame::Command
4
+
5
+ command_name='reload_service'
6
+
7
+ def run(rule)
8
+ if !@options["service_name"].nil?
9
+ levels = rule.service_cluster.dg.levels
10
+ levels.reverse.each {|lv|
11
+ lv.each { |svc_prop|
12
+ if svc_prop.class.to_s == @options["service_name"].to_s
13
+ rule.service_cluster.each_instance(svc_prop.class) { |svc_inst|
14
+ rule.trigger_action(Wakame::Actions::ReloadService.new(svc_inst))
15
+ }
16
+ end
17
+ }
18
+ }
19
+ end
20
+ end
21
+ end
@@ -1,15 +1,10 @@
1
1
 
2
-
3
2
  class Wakame::Command::ShutdownCluster
4
3
  include Wakame::Command
5
4
 
6
5
  command_name='shutdown_cluster'
7
6
 
8
- def parse(args)
9
- end
10
-
11
7
  def run(rule)
12
8
  rule.trigger_action(Wakame::Actions::ShutdownCluster.new)
13
9
  end
14
-
15
10
  end
@@ -0,0 +1,20 @@
1
+
2
+ class Wakame::Command::ShutdownVm
3
+ include Wakame::Command
4
+
5
+ command_name='shutdown_vm'
6
+
7
+ def run(rule)
8
+ registered_agents = rule.agent_monitor.registered_agents[@options["agent_id"]]
9
+ if !registered_agents.services.nil?
10
+ if !@options["force"].nil?
11
+ registered_agents.services.each{|id, svc_inst|
12
+ rule.trigger_action(Wakame::Actions::StopService.new(svc_inst))
13
+ }
14
+ else
15
+ raise "Service instances Launched"
16
+ end
17
+ end
18
+ rule.trigger_action(Wakame::Actions::ShutdownVM.new(registered_agents))
19
+ end
20
+ end
@@ -4,78 +4,18 @@ require 'erb'
4
4
  class Wakame::Command::Status
5
5
  include Wakame::Command
6
6
 
7
- STATUS_TMPL =<<__E__
8
- Cluster : <%= @service_cluster[:name].to_s %> (<%= @service_cluster[:status].to_s %>)
9
- <%- @service_cluster[:properties].each { |prop, v| -%>
10
- <%= v[:type].to_s %> : <current=<%= v[:instance_count] %> min=<%= v[:min_instances] %>, max=<%= v[:max_instances] %>>
11
- <%- v[:instances].each { |id|
12
- svc_inst = @service_cluster[:instances][id]
13
- -%>
14
- <%= svc_inst[:instance_id] %> (<%= trans_svc_status(svc_inst[:status]) %>)
15
- <%- } -%>
16
- <%- } -%>
17
- <%- if @service_cluster[:instances].size > 0 -%>
18
-
19
- Instances :
20
- <%- @service_cluster[:instances].each { |k, v| -%>
21
- <%= v[:instance_id] %> : <%= v[:property] %> (<%= trans_svc_status(v[:status]) %>)
22
- <%- if v[:agent_id ] -%>
23
- On VM instance: <%= v[:agent_id ]%>
24
- <%- end -%>
25
- <%- } -%>
26
- <%- end -%>
27
- <%- if @agent_monitor[:registered].size > 0 -%>
28
-
29
- Agents :
30
- <%- @agent_monitor[:registered].each { |a| -%>
31
- <%= a[:agent_id] %> : <%= a[:attr][:local_ipv4] %>, <%= a[:attr][:public_ipv4] %> load=<%= a[:attr][:uptime] %>, <%= (Time.now - a[:last_ping_at]).to_i %> sec(s) <%= a[:root_path] %>(<%= a[:status] %>)
32
- <%- if !a[:services].nil? && a[:services].size > 0 -%>
33
- Services (<%= a[:services].size %>): <%= a[:services].collect{|id| @service_cluster[:instances][id][:property] }.join(', ') %>
34
- <%- end -%>
35
- <%- } -%>
36
- <%- end -%>
37
- __E__
38
-
39
- SVC_STATUS_MSG={
40
- Wakame::Service::STATUS_OFFLINE=>'Offline',
41
- Wakame::Service::STATUS_ONLINE=>'ONLINE',
42
- Wakame::Service::STATUS_UNKNOWN=>'Unknown',
43
- Wakame::Service::STATUS_FAIL=>'Fail',
44
- Wakame::Service::STATUS_STARTING=>'Starting...',
45
- Wakame::Service::STATUS_STOPPING=>'Stopping...',
46
- Wakame::Service::STATUS_RELOADING=>'Reloading...',
47
- Wakame::Service::STATUS_MIGRATING=>'Migrating...',
48
- }
49
-
50
- def parse(args)
51
- end
52
-
53
7
  def run(rule)
54
- EM.barrier {
8
+ EM.barrier {
55
9
  master = rule.master
56
10
 
57
- sc = master.service_cluster
58
- #result = {
59
- # :rule_engine => {
60
- # :rules => sc.rule_engine.rules
61
- # },
62
- # :service_cluster => sc.dump_status,
63
- # :agent_monitor => master.agent_monitor.dump_status
64
- #}
65
-
66
11
  @service_cluster = master.service_cluster.dump_status
67
12
  @agent_monitor = master.agent_monitor.dump_status
13
+ res = {
14
+ :service_cluster => @service_cluster,
15
+ :agent_monitor => @agent_monitor
16
+ }
17
+ res
68
18
  }
69
- end
70
-
71
- def print_result
72
- puts ERB.new(STATUS_TMPL, nil, '-').result(binding)
73
- end
74
-
75
19
 
76
- private
77
- def trans_svc_status(stat)
78
- SVC_STATUS_MSG[stat]
79
20
  end
80
-
81
21
  end
@@ -0,0 +1,31 @@
1
+ class Wakame::Command::StopService
2
+ include Wakame::Command
3
+
4
+ command_name='stop_service'
5
+
6
+ def run(rule)
7
+
8
+ if !@options["service_id"].nil?
9
+ svc_inst = rule.service_cluster.instances
10
+ rule.trigger_action(Wakame::Actions::StopService.new(svc_inst[@options["service_id"]]))
11
+ end
12
+ if !@options["service_name"].nil?
13
+ levels = rule.service_cluster.dg.levels
14
+ levels.reverse.each {|lv|
15
+ lv.each { |svc_prop|
16
+ if svc_prop.class.to_s == @options["service_name"].to_s
17
+ rule.service_cluster.each_instance(svc_prop.class) { |svc_inst|
18
+ rule.trigger_action(Wakame::Actions::StopService.new(svc_inst))
19
+ }
20
+ end
21
+ }
22
+ }
23
+ end
24
+ if !@options["agent_id"].nil?
25
+ registered_agents = rule.agent_monitor.registered_agents[@options["agent_id"]]
26
+ registered_agents.services.each{|id, svc_inst|
27
+ rule.trigger_action(Wakame::Actions::StopService.new(svc_inst))
28
+ }
29
+ end
30
+ end
31
+ end
@@ -17,6 +17,9 @@ module Wakame
17
17
  }
18
18
  end
19
19
 
20
+ def options=(path)
21
+ @options = path
22
+ end
20
23
 
21
24
  def parse(args)
22
25
  end
@@ -1,6 +1,10 @@
1
1
 
2
- require 'drb/drb'
2
+ require 'uri'
3
+ require 'thin'
3
4
  require 'thread'
5
+ require 'json'
6
+ #require 'openssl'
7
+ #require 'base64'
4
8
 
5
9
  module Wakame
6
10
  class CommandQueue
@@ -11,13 +15,15 @@ module Wakame
11
15
  @queue = Queue.new
12
16
  @result_queue = Queue.new
13
17
 
14
- DRb.start_service(Wakame.config.drb_command_server_uri, self)
15
- #@drb_server = DRb.start_drbserver(Wakame.config.drb_command_server_uri, self)
18
+ cmdsv_uri = URI.parse(Wakame.config.http_command_server_uri)
19
+
20
+ @thin_server = Thin::Server.new(cmdsv_uri.host, cmdsv_uri.port, Adapter.new(self))
21
+ @thin_server.threaded = true
22
+ @thin_server.start
16
23
  end
17
24
 
18
25
  def shutdown
19
- DRb.stop_service()
20
- #@drb_server.stop_service()
26
+ @thin_server.stop
21
27
  end
22
28
 
23
29
  def deq_cmd()
@@ -30,9 +36,13 @@ module Wakame
30
36
 
31
37
  def send_cmd(cmd)
32
38
  begin
39
+
33
40
  #cmd = Marshal.load(cmd)
41
+
34
42
  @queue.enq(cmd)
43
+
35
44
  ED.fire_event(Event::CommandReceived.new(cmd))
45
+
36
46
  return @result_queue.deq()
37
47
  rescue => e
38
48
  Wakame.log.error("#{self.class}:")
@@ -41,4 +51,65 @@ module Wakame
41
51
  end
42
52
 
43
53
  end
54
+ class Adapter
55
+
56
+ def initialize(command_queue)
57
+ @command_queue = command_queue
58
+ end
59
+
60
+ def call(env)
61
+ Wakame.log.debug EM.reactor_thread?
62
+ req = Rack::Request.new(env)
63
+ begin
64
+ unless req.get?().to_s == "true"
65
+ raise "No Support Response"
66
+ end
67
+ query = req.query_string()
68
+ params = req.params()
69
+ if Wakame.config.enable_authentication == "true"
70
+ auth = authentication(params, query)
71
+ end
72
+ cname = params["action"].split("_")
73
+ begin
74
+ cmd = eval("Command::#{(cname.collect{|c| c.capitalize}).join}").new
75
+ cmd.options = params
76
+ command = @command_queue.send_cmd(cmd)
77
+
78
+ if command.is_a?(Exception)
79
+ status = 500
80
+ body = json_encode(status, command.message)
81
+ else
82
+ status = 200
83
+ body = json_encode(status, "OK", command)
84
+ end
85
+ rescue => e
86
+ status = 404
87
+ body = json_encode(status, e)
88
+ end
89
+ rescue => e
90
+ status = 403
91
+ body = json_encode(status, e)
92
+ end
93
+ [ status, {'Content-Type' => 'text/javascript+json'}, body]
94
+ end
95
+
96
+ def authentication(path, query)
97
+ key = Wakame.config.private_key
98
+ req = query.split(/\&signature\=/)
99
+ hash = OpenSSL::HMAC::digest(OpenSSL::Digest::SHA256.new, key, req[0])
100
+ hmac = Base64.encode64(hash).gsub(/\+/, "").gsub(/\n/, "").to_s
101
+ if hmac != path["signature"]
102
+ raise "Authentication failed"
103
+ end
104
+ end
105
+
106
+ def json_encode(status, message, data=nil)
107
+ if status == 200 && data.is_a?(Hash)
108
+ body = [{:status=>status, :message=>message}, {:data=>data}].to_json
109
+ else
110
+ body = [{:status=>status, :message=>message}].to_json
111
+ end
112
+ body
113
+ end
114
+ end
44
115
  end
@@ -13,7 +13,7 @@ module Wakame
13
13
  :cluster_class => 'WebCluster',
14
14
  :load_paths => [],
15
15
  :ssh_private_key => nil,
16
- :drb_command_server_uri => 'druby://localhost:12345',
16
+ :http_command_server_uri => 'http://localhost:3000',
17
17
  :amqp_server_uri => nil,
18
18
  :unused_vm_live_period => 60 * 10,
19
19
  :eventmachine_use_epoll => true
@@ -64,6 +64,11 @@ module Wakame
64
64
 
65
65
  paths.map{|dir| File.join(framework_root_path, dir) }.select{|path| File.directory?(path) }
66
66
  end
67
+
68
+ def project_paths
69
+ %w(lib).map{|dir| File.join(root_path, dir) }.select{|path| File.directory?(path)}
70
+ end
71
+
67
72
  #
68
73
  class DefaultSet
69
74
  def process(config)
data/lib/wakame/event.rb CHANGED
@@ -149,7 +149,7 @@ module Wakame
149
149
  end
150
150
 
151
151
  def log_message
152
- "#{@action.class}"
152
+ "#{@action.class}, job_id=#{@action.job_id}"
153
153
  end
154
154
 
155
155
  end
@@ -170,11 +170,11 @@ module Wakame
170
170
  attr_reader :job_id
171
171
  def initialize(job_id)
172
172
  super()
173
- @action = job_id
173
+ @job_id = job_id
174
174
  end
175
175
 
176
176
  def log_message
177
- "#{@action.class}"
177
+ "#{@job_id}"
178
178
  end
179
179
  end
180
180
  class JobStart < JobEvent
@@ -220,11 +220,12 @@ module Wakame
220
220
  end
221
221
 
222
222
  class ActorComplete < Base
223
- attr_reader :agent_id, :token, :status
224
- def initialize(agent_id, token, status)
223
+ attr_reader :agent_id, :token, :status, :return_value
224
+ def initialize(agent_id, token, status, return_value)
225
225
  @agent_id = agent_id
226
226
  @token = token
227
227
  @status = status
228
+ @return_value = return_value
228
229
  end
229
230
  end
230
231