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
@@ -34,6 +34,18 @@ module Wakame
34
34
  autoload :RuleEngine, 'wakame/rule_engine'
35
35
  autoload :StatusDB, 'wakame/status_db'
36
36
  autoload :ActionManager, 'wakame/action_manager'
37
+ autoload :AgentManager, 'wakame/agent_manager'
38
+ module AgentManagers
39
+ autoload :MonitorManager, 'wakame/agent_managers/monitor_manager'
40
+ autoload :ActorManager, 'wakame/agent_managers/actor_manager'
41
+ end
42
+ autoload :MasterManager, 'wakame/master_manager'
43
+ module MasterManagers
44
+ autoload :ActionManager, 'wakame/master_managers/action_manager'
45
+ autoload :CommandQueue, 'wakame/master_managers/command_queue'
46
+ autoload :AgentMonitor, 'wakame/master_managers/agent_monitor'
47
+ autoload :ClusterManager, 'wakame/master_managers/cluster_manager'
48
+ end
37
49
  #autoload :Initializer, 'wakame/initializer' # Do not autoload this class since the constant is used for the flag in bootstrap.
38
50
 
39
51
  def gen_id(str=nil)
@@ -20,10 +20,12 @@ module Wakame
20
20
 
21
21
  # Tentative utility method for
22
22
  def service_cluster
23
- cluster_id = master.cluster_manager.clusters.first
24
- raise "There is no cluster loaded" if cluster_id.nil?
23
+ StatusDB.barrier {
24
+ cluster_id = master.cluster_manager.clusters.first
25
+ raise "There is no cluster loaded" if cluster_id.nil?
25
26
 
26
- Service::ServiceCluster.find(cluster_id)
27
+ Service::ServiceCluster.find(cluster_id)
28
+ }
27
29
  end
28
30
  alias :cluster :service_cluster
29
31
 
@@ -124,14 +126,11 @@ module Wakame
124
126
  action_manager.active_jobs[self.job_id][:notes]
125
127
  end
126
128
 
127
- # Set the lock flags to resources
128
- def acquire_lock(&blk)
129
- StatusDB.barrier {
130
- reslist = []
131
- blk.call(reslist)
132
- reslist.flatten!
133
- reslist.each {|r| action_manager.lock_queue.set(r.to_s, self.job_id) }
134
- }
129
+ # Set the lock flags to arbitrary object names.
130
+ # acquire_lock('Lock Target 1', 'Lock Target 2')
131
+ # acquire_lock(%w(aaaa bbbb cccc))
132
+ def acquire_lock(*args)
133
+ args.flatten.each {|r| action_manager.lock_queue.set(r.to_s, self.job_id) }
135
134
 
136
135
  action_manager.lock_queue.wait(self.job_id)
137
136
  end
@@ -0,0 +1,61 @@
1
+ module Wakame
2
+ module Actions
3
+ class DeployApplication < Action
4
+ def initialize(app_name)
5
+ @app_name = app_name
6
+ end
7
+
8
+ def run
9
+ Wakame.log.debug("#{self.class}: run() Begin:")
10
+ #raise "CloudHost is not mapped Agent: CloudHost.id=#{@svc.cloud_host.id}" unless @svc.cloud_host.mapped?
11
+
12
+ repo_data = Models::ApplicationRepository.find(:app_name=>@app_name) || raise
13
+ deploy_opts={}
14
+ if repo_data[:repo_type] == 's3'
15
+ deploy_opts[:aws_access_key] ||= Wakame.config.aws_access_key
16
+ deploy_opts[:aws_secret_key] ||= Wakame.config.aws_secret_key
17
+ end
18
+ deploy_ticket = Wakame::Util.gen_id
19
+
20
+
21
+ resclass_names = []
22
+ svc_lst = []
23
+ cluster.each_instance { |svc|
24
+ next unless svc.resource.tags.member?(repo_data.resource_tag.to_sym)
25
+ resclass_names << svc.resource.class.to_s
26
+ svc_lst << svc
27
+ }
28
+
29
+ if svc_lst.empty?
30
+ return
31
+ end
32
+
33
+ resclass_names.uniq!
34
+ acquire_lock(resclass_names)
35
+
36
+ # Checkout apps from each repository
37
+ svc_lst.each { |svc|
38
+ trigger_action {|action|
39
+ action.actor_request(svc.cloud_host.agent_id, '/deploy/checkout',
40
+ deploy_ticket,
41
+ repo_data[:repo_type],
42
+ repo_data[:repo_uri],
43
+ repo_data[:revision],
44
+ svc.resource.application_root_path,
45
+ repo_data[:app_name],
46
+ deploy_opts
47
+ ).request.wait
48
+ }
49
+ }
50
+ flush_subactions
51
+
52
+ # Reload Appserver
53
+ svc_lst.each { |svc|
54
+ actor_request(svc.cloud_host.agent_id, '/deploy/swap_current_link', svc.resource.application_root_path, repo_data[:app_name]).request.wait
55
+ svc.resource.on_reload_application(svc, self, repo_data)
56
+ }
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -9,9 +9,7 @@ module Wakame
9
9
  Wakame.log.debug("#{self.class}: run() Begin: #{@svc.resource.class}")
10
10
  raise "CloudHost is not mapped Agent: CloudHost.id=#{@svc.cloud_host.id}" unless @svc.cloud_host.mapped?
11
11
 
12
- acquire_lock { |lst|
13
- lst << @svc.resource.class.to_s
14
- }
12
+ acquire_lock(@svc.resource.class.to_s)
15
13
 
16
14
  begin
17
15
  tmpl = Wakame::Template.new(@svc)
@@ -0,0 +1,15 @@
1
+ module Wakame
2
+ module Actions
3
+ class FreezeCluster < 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
@@ -12,9 +12,7 @@ module Wakame
12
12
 
13
13
  levels = service_cluster.dg.levels
14
14
  Wakame.log.debug("#{self.class}: Scheduled resource launch order: " + levels.collect {|lv| '['+ lv.collect{|prop| "#{prop.class}" }.join(', ') + ']' }.join(', '))
15
- acquire_lock { |list|
16
- levels.each {|lv| list << lv.collect{|res| res.class } }
17
- }
15
+ acquire_lock(levels.map {|lv| lv.map{|res| res.class.to_s } })
18
16
 
19
17
  levels.each { |lv|
20
18
  Wakame.log.info("#{self.class}: Launching resources: #{lv.collect{|prop| "#{prop.class}" }.join(', ')}")
@@ -34,7 +34,7 @@ __END__
34
34
 
35
35
  ConditionalWait.wait { | cond |
36
36
  cond.wait_event(Event::AgentMonitored) { |event|
37
- event.agent.vm_attr[:instance_id] == inst_id
37
+ event.agent.vm_attr[:aws_instance_id] == inst_id
38
38
  }
39
39
 
40
40
  cond.poll(5, 360) {
@@ -9,9 +9,8 @@ module Wakame
9
9
  end
10
10
 
11
11
  def run
12
- acquire_lock { |list|
13
- list << @svc.resource.class
14
- }
12
+ acquire_lock(@svc.resource.class.to_s)
13
+ @svc.reload
15
14
 
16
15
  if @svc.status == Service::STATUS_MIGRATING
17
16
  Wakame.log.info("Ignore to migrate the service as is already MIGRATING: #{@svc.resource.class}")
@@ -25,8 +24,10 @@ module Wakame
25
24
  clone_service()
26
25
  flush_subactions
27
26
  trigger_action(StopService.new(@svc))
27
+ @svc.reload
28
28
  else
29
29
  trigger_action(StopService.new(@svc))
30
+ @svc.reload
30
31
  flush_subactions
31
32
  clone_service()
32
33
  end
@@ -6,19 +6,16 @@ module Wakame
6
6
  end
7
7
 
8
8
  def run
9
- parents = []
10
- StatusDB.barrier {
9
+ parents = StatusDB.barrier {
11
10
  parents = @child_svc.parent_instances
12
11
  }
13
12
 
14
- acquire_lock { |lst|
15
- lst << parents.map{|c| c.resource.class.to_s }.uniq
16
- }
13
+ acquire_lock(parents.map{|c| c.resource.class.to_s }.uniq)
17
14
 
18
15
  Wakame.log.debug("#{self.class}: Parent nodes for #{@child_svc.resource.class}: " + parents.map{|c| c.resource.class }.uniq.inspect )
19
16
 
20
17
  parents.each { |svc|
21
- if svc.status != Service::STATUS_ONLINE
18
+ if svc.monitor_status != Service::STATUS_ONLINE
22
19
  next
23
20
  end
24
21
 
@@ -6,19 +6,16 @@ module Wakame
6
6
  end
7
7
 
8
8
  def run
9
- children = []
10
- StatusDB.barrier {
11
- children = @parent_svc.child_instances
9
+ children = StatusDB.barrier {
10
+ @parent_svc.child_instances
12
11
  }
13
12
 
14
- acquire_lock { |lst|
15
- lst << children.map{|c| c.resource.class.to_s }.uniq
16
- }
13
+ acquire_lock(children.map{|c| c.resource.class.to_s }.uniq)
17
14
 
18
15
  Wakame.log.debug("#{self.class}: Child nodes for #{@parent_svc.resource.class}: " + children.map{|c| c.resource.class }.uniq.inspect )
19
16
 
20
17
  children.each { |svc|
21
- if svc.status != Service::STATUS_ONLINE
18
+ if svc.monitor_status != Service::STATUS_ONLINE
22
19
  next
23
20
  end
24
21
 
@@ -9,9 +9,7 @@ module Wakame
9
9
  end
10
10
 
11
11
  def run
12
- acquire_lock { |ary|
13
- ary << @resource.class.to_s
14
- }
12
+ acquire_lock(@resource.class.to_s)
15
13
 
16
14
  newsvc=nil
17
15
  StatusDB.barrier {
@@ -10,9 +10,7 @@ module Wakame
10
10
 
11
11
 
12
12
  def run
13
- acquire_lock { |ary|
14
- ary << @svc.resource.class.to_s
15
- }
13
+ acquire_lock(@svc.resource.class.to_s)
16
14
 
17
15
  newsvc = nil
18
16
  StatusDB.barrier {
@@ -0,0 +1,43 @@
1
+ module Wakame
2
+ module Actions
3
+ class RegisterAgent < Action
4
+ def initialize(agent)
5
+ @agent = agent
6
+ end
7
+
8
+ def run
9
+ Wakame.log.debug("#{self.class}: run() begin: #{@agent.id}")
10
+
11
+ acquire_lock("Agent:#{@agent.id}")
12
+
13
+ StatusDB.barrier {
14
+ @agent.update_vm_attr
15
+ }
16
+
17
+ # Send monitoring conf
18
+ if @agent.cloud_host_id
19
+ Wakame.log.debug("#{self.class}: #{@agent.id} is to be set the monitor conf: #{@agent.cloud_host.live_monitors.inspect}")
20
+ StatusDB.barrier {
21
+ @agent.cloud_host.live_monitors.each { |path, data|
22
+ master.actor_request(@agent.id, '/monitor/reload', path, data).request.wait
23
+ }
24
+ }
25
+ end
26
+
27
+ StatusDB.barrier {
28
+ @agent.update_status(Service::Agent::STATUS_RUNNING)
29
+ @agent.update_monitor_status(Service::Agent::STATUS_ONLINE)
30
+ Models::AgentPool.instance.register(@agent)
31
+ }
32
+ Wakame.log.debug("#{self.class}: run() end: #{@agent.id}")
33
+ end
34
+
35
+ def on_fail
36
+ StatusDB.barrier {
37
+ @agent.update_status(Service::Agent::STATUS_FAIL)
38
+ }
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -7,7 +7,7 @@ module Wakame
7
7
 
8
8
  def run
9
9
  # Skip to act when the service is having below status.
10
- if @svc.status != Service::STATUS_ONLINE
10
+ if @svc.monitor_status != Service::STATUS_ONLINE
11
11
  raise "Canceled as the service is being or already ONLINE: #{@svc.resource.class}"
12
12
  end
13
13
 
@@ -18,7 +18,7 @@ module Wakame
18
18
  @svc.resource.reload(@svc, self)
19
19
 
20
20
  StatusDB.barrier {
21
- @svc.update_status(Service::STATUS_ONLINE)
21
+ @svc.update_status(Service::STATUS_RUNNING)
22
22
  }
23
23
  end
24
24
 
@@ -4,9 +4,7 @@ module Wakame
4
4
  def run
5
5
  levels = cluster.dg.levels
6
6
  Wakame.log.debug("#{self.class}: Resource shutdown order: " + levels.collect {|lv| '['+ lv.collect{|prop| "#{prop.class}" }.join(', ') + ']' }.join(', '))
7
- acquire_lock { |list|
8
- levels.each {|lv| list << lv.collect{|res| res.class } }
9
- }
7
+ acquire_lock(levels.map {|lv| lv.map{|res| res.class.to_s } })
10
8
 
11
9
  levels.reverse.each { |lv|
12
10
  lv.each { |svc_prop|
@@ -16,9 +14,9 @@ module Wakame
16
14
  }
17
15
  flush_subactions
18
16
  }
19
-
20
- agent_monitor.agent_pool.group_active.keys.each { |agent_id|
21
- trigger_action(ShutdownVM.new(Service::Agent.find(agent_id)))
17
+ cluster.cloud_hosts.keys.each { |cloud_host_id|
18
+ cloud_host = Service::CloudHost.find(cloud_host_id)
19
+ trigger_action(ShutdownVM.new(cloud_host.agent))
22
20
  }
23
21
  end
24
22
  end
@@ -7,11 +7,32 @@ module Wakame
7
7
  end
8
8
 
9
9
  def run
10
- #if @agent.id == master.master_local_agent_id
11
- # Wakame.log.info("Skip to shutdown VM as the master is running on this node: #{@agent.agent_id}")
12
- # return
13
- #end
14
-
10
+ if @agent.mapped?
11
+ cloud_host = @agent.cloud_host
12
+ raise "The VM has running service(s)." if cloud_host.assigned_services.any? {|svc_id|
13
+ Wakame::Service::ServiceInstance.find(svc_id).monitor_status == Wakame::Service::STATUS_ONLINE
14
+ }
15
+ end
16
+
17
+ require 'uri'
18
+ require 'socket'
19
+ Master.instance.cluster_manager.clusters.each { |cluster_id|
20
+ cluster = Service::ServiceCluster.find(cluster_id)
21
+ next if cluster.advertised_amqp_servers.nil?
22
+ amqp_uri = URI.parse(cluster.advertised_amqp_servers)
23
+ amqp_svr_ip = IPSocket.getaddress(amqp_uri.host)
24
+
25
+ [@agent.vm_attr[:dns_name], @agent.vm_attr[:private_dns_name]].each { |hostname|
26
+ if IPSocket.getaddress(hostname) == amqp_svr_ip
27
+ Wakame.log.info("Skip to shutdown the VM as the master is running on this node: #{@agent.id}")
28
+ return
29
+ end
30
+ }
31
+ }
32
+
33
+ StatusDB.barrier {
34
+ @agent.update_status(Service::Agent::STATUS_TERMINATING)
35
+ }
15
36
  shutdown_ec2_instance
16
37
 
17
38
  end
@@ -21,7 +42,7 @@ module Wakame
21
42
  require 'right_aws'
22
43
  ec2 = RightAws::Ec2.new(Wakame.config.aws_access_key, Wakame.config.aws_secret_key, {:cache=>false})
23
44
 
24
- res = ec2.terminate_instances([@agent.vm_attr[:instance_id]])
45
+ res = ec2.terminate_instances([@agent.vm_attr[:aws_instance_id]])
25
46
  end
26
47
  end
27
48
  end
@@ -2,88 +2,96 @@
2
2
  module Wakame
3
3
  module Actions
4
4
  class StartService < Action
5
- def initialize(service_instance)
6
- @service_instance = service_instance
5
+ def initialize(svc)
6
+ @svc = svc
7
7
  end
8
8
 
9
9
  def run
10
+ acquire_lock(@svc.resource.class.to_s)
11
+ @svc.reload
12
+
10
13
  # Skip to act when the service is having below status.
11
- if @service_instance.status == Service::STATUS_STARTING || @service_instance.status == Service::STATUS_ONLINE
12
- Wakame.log.info("Ignore to start the service as is being or already Online: #{@service_instance.resource.class}")
14
+ if @svc.status == Service::STATUS_RUNNING && @svc.monitor_status == Service::STATUS_ONLINE
15
+ Wakame.log.info("Ignore to start the service as is being or already Online: #{@svc.resource.class}")
13
16
  return
14
17
  end
15
18
 
16
- acquire_lock { |lst|
17
- lst << @service_instance.resource.class.to_s
18
- }
19
-
20
- if @service_instance.resource.require_agent
21
- raise "The service is not bound cloud host object: #{@service_instance.id}" if @service_instance.cloud_host_id.nil?
19
+ if @svc.resource.require_agent
20
+ raise "The service is not bound cloud host object: #{@svc.id}" if @svc.cloud_host_id.nil?
22
21
 
23
- unless @service_instance.cloud_host.mapped?
24
- acquire_lock { |lst|
25
- lst << Service::AgentPool.class.to_s
26
- }
22
+ unless @svc.cloud_host.mapped?
23
+ acquire_lock(Models::AgentPool.to_s)
27
24
 
28
25
  # Try to arrange agent from existing agent pool.
29
26
  StatusDB.barrier {
30
- next if Service::AgentPool.instance.group_active.empty?
27
+ next if Models::AgentPool.group_active.empty?
31
28
  agent2host = cluster.agents.invert
32
29
 
33
- Service::AgentPool.instance.group_active.keys.each { |agent_id|
30
+ Models::AgentPool.group_active.each { |agent_id|
34
31
  agent = Service::Agent.find(agent_id)
35
- if !agent.has_resource_type?(@service_instance.resource) &&
32
+ if !agent.has_resource_type?(@svc.resource) &&
36
33
  agent2host[agent_id].nil? && # This agent is not mapped to any cloud hosts.
37
- @service_instance.cloud_host.vm_spec.satisfy?(agent.vm_attr)
34
+ @svc.cloud_host.vm_spec.satisfy?(agent.vm_attr)
38
35
 
39
- @service_instance.cloud_host.map_agent(agent)
36
+ @svc.cloud_host.map_agent(agent)
40
37
  break
41
38
  end
42
39
  }
43
40
  }
44
41
 
45
42
  # Start new VM when the target agent is still nil.
46
- unless @service_instance.cloud_host.mapped?
43
+ unless @svc.cloud_host.mapped?
47
44
  inst_id_key = "new_inst_id_" + Wakame::Util.gen_id
48
- trigger_action(LaunchVM.new(inst_id_key, @service_instance.cloud_host.vm_spec))
45
+ trigger_action(LaunchVM.new(inst_id_key, @svc.cloud_host.vm_spec))
49
46
  flush_subactions
50
47
 
51
48
  StatusDB.barrier {
52
49
  agent = Service::Agent.find(notes[inst_id_key])
53
50
  raise "Cound not find the specified VM instance \"#{notes[inst_id_key]}\"" if agent.nil?
54
- @service_instance.cloud_host.map_agent(agent)
51
+ @svc.cloud_host.map_agent(agent)
55
52
  }
56
53
  end
57
54
 
58
- raise "Could not find the agent to be assigned to : #{@service_instance.resource.class}" unless @service_instance.cloud_host.mapped?
59
-
60
- @service_instance.resource.on_enter_agent(@service_instance, self)
55
+ raise "Could not find the agent to be assigned to : #{@svc.resource.class}" unless @svc.cloud_host.mapped?
61
56
  end
62
57
 
63
- raise "The assigned agent \"#{@service_instance.cloud_host.agent_id}\" for the service instance #{@service_instance.id} is not online." unless @service_instance.cloud_host.status == Service::Agent::STATUS_ONLINE
58
+ raise "The assigned agent \"#{@svc.cloud_host.agent_id}\" for the service instance #{@svc.id} is not online." unless @svc.cloud_host.agent.monitor_status == Service::Agent::STATUS_ONLINE
59
+
60
+ StatusDB.barrier {
61
+ @svc.update_status(Service::STATUS_ENTERING)
62
+ }
64
63
 
64
+ @svc.resource.on_enter_agent(@svc, self)
65
65
  end
66
66
 
67
67
 
68
68
  StatusDB.barrier {
69
- @service_instance.update_status(Service::STATUS_STARTING)
69
+ @svc.update_status(Service::STATUS_STARTING)
70
70
  }
71
71
 
72
- if @service_instance.resource.require_agent
73
- trigger_action(DeployConfig.new(@service_instance))
72
+ if @svc.resource.require_agent
73
+ trigger_action(DeployConfig.new(@svc))
74
74
  flush_subactions
75
75
  end
76
76
 
77
- @service_instance.resource.start(@service_instance, self)
77
+ @svc.reload
78
+ Wakame.log.debug("#{@svc.resource.class}: svc.monitor_status == Wakame::Service::STATUS_ONLINE => #{@svc.monitor_status == Wakame::Service::STATUS_ONLINE}")
79
+ if @svc.monitor_status != Wakame::Service::STATUS_ONLINE
80
+ @svc.resource.start(@svc, self)
81
+ end
82
+
83
+ StatusDB.barrier {
84
+ @svc.update_status(Service::STATUS_RUNNING)
85
+ }
78
86
 
79
- trigger_action(NotifyParentChanged.new(@service_instance))
87
+ trigger_action(NotifyParentChanged.new(@svc))
80
88
  flush_subactions
81
89
 
82
90
  end
83
91
 
84
92
  def on_failed
85
93
  StatusDB.barrier {
86
- @service_instance.update_status(Service::STATUS_FAIL)
94
+ @svc.update_status(Service::STATUS_FAIL)
87
95
  }
88
96
  end
89
97
  end