wakame 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. data/History.txt +20 -0
  2. data/README.rdoc +63 -0
  3. data/Rakefile +86 -0
  4. data/VERSION +1 -0
  5. data/app_generators/wakame/templates/README +0 -0
  6. data/app_generators/wakame/templates/Rakefile +18 -0
  7. data/app_generators/wakame/templates/bin/wakame-agent +9 -0
  8. data/app_generators/wakame/templates/bin/wakame-master +9 -0
  9. data/app_generators/wakame/templates/bin/wakameadm +9 -0
  10. data/app_generators/wakame/templates/cluster/resources/apache_app/apache_app.rb +54 -0
  11. data/app_generators/wakame/templates/cluster/resources/apache_app/conf/apache2.conf +46 -0
  12. data/app_generators/wakame/templates/cluster/resources/apache_app/conf/envvars-app +7 -0
  13. data/app_generators/wakame/templates/cluster/resources/apache_app/conf/sites-app.conf +23 -0
  14. data/app_generators/wakame/templates/cluster/resources/apache_app/conf/system-app.conf +67 -0
  15. data/app_generators/wakame/templates/cluster/resources/apache_app/init.d/apache2-app +192 -0
  16. data/app_generators/wakame/templates/cluster/resources/apache_lb/apache_lb.rb +56 -0
  17. data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/apache2.conf +46 -0
  18. data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/envvars-lb +6 -0
  19. data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/sites-lb.conf +54 -0
  20. data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/system-lb.conf +75 -0
  21. data/app_generators/wakame/templates/cluster/resources/apache_lb/init.d/apache2-lb +192 -0
  22. data/app_generators/wakame/templates/cluster/resources/apache_www/apache_www.rb +50 -0
  23. data/app_generators/wakame/templates/cluster/resources/apache_www/conf/apache2.conf +47 -0
  24. data/app_generators/wakame/templates/cluster/resources/apache_www/conf/envvars-www +7 -0
  25. data/app_generators/wakame/templates/cluster/resources/apache_www/conf/sites-www.conf +23 -0
  26. data/app_generators/wakame/templates/cluster/resources/apache_www/conf/system-www.conf +63 -0
  27. data/app_generators/wakame/templates/cluster/resources/apache_www/init.d/apache2-www +192 -0
  28. data/app_generators/wakame/templates/cluster/resources/ec2_elastic_ip/ec2_elastic_ip.rb +39 -0
  29. data/app_generators/wakame/templates/cluster/resources/mysql_master/conf/my.cnf +154 -0
  30. data/app_generators/wakame/templates/cluster/resources/mysql_master/init.d/mysql +185 -0
  31. data/app_generators/wakame/templates/cluster/resources/mysql_master/mysql_master.rb +174 -0
  32. data/app_generators/wakame/templates/config/boot.rb +85 -0
  33. data/app_generators/wakame/templates/config/cluster.rb +64 -0
  34. data/app_generators/wakame/templates/config/environments/common.rb +0 -0
  35. data/app_generators/wakame/templates/config/environments/ec2.rb +3 -0
  36. data/app_generators/wakame/templates/config/environments/stand_alone.rb +0 -0
  37. data/app_generators/wakame/templates/config/init.d/wakame-agent +72 -0
  38. data/app_generators/wakame/templates/config/init.d/wakame-master +73 -0
  39. data/app_generators/wakame/wakame_generator.rb +124 -0
  40. data/bin/wakame +18 -0
  41. data/contrib/imagesetup.sh +77 -0
  42. data/lib/ext/eventmachine.rb +86 -0
  43. data/lib/ext/shellwords.rb +172 -0
  44. data/lib/ext/uri.rb +15 -0
  45. data/lib/wakame/action.rb +156 -0
  46. data/lib/wakame/actions/destroy_instances.rb +39 -0
  47. data/lib/wakame/actions/launch_cluster.rb +31 -0
  48. data/lib/wakame/actions/migrate_service.rb +65 -0
  49. data/lib/wakame/actions/propagate_instances.rb +95 -0
  50. data/lib/wakame/actions/reload_service.rb +21 -0
  51. data/lib/wakame/actions/scaleout_when_high_load.rb +44 -0
  52. data/lib/wakame/actions/shutdown_cluster.rb +22 -0
  53. data/lib/wakame/actions/shutdown_vm.rb +19 -0
  54. data/lib/wakame/actions/start_service.rb +64 -0
  55. data/lib/wakame/actions/stop_service.rb +49 -0
  56. data/lib/wakame/actions/util.rb +71 -0
  57. data/lib/wakame/actor/daemon.rb +37 -0
  58. data/lib/wakame/actor/service_monitor.rb +21 -0
  59. data/lib/wakame/actor/system.rb +46 -0
  60. data/lib/wakame/actor.rb +33 -0
  61. data/lib/wakame/agent.rb +226 -0
  62. data/lib/wakame/amqp_client.rb +219 -0
  63. data/lib/wakame/command/action_status.rb +62 -0
  64. data/lib/wakame/command/actor.rb +23 -0
  65. data/lib/wakame/command/clone_service.rb +12 -0
  66. data/lib/wakame/command/launch_cluster.rb +15 -0
  67. data/lib/wakame/command/migrate_service.rb +21 -0
  68. data/lib/wakame/command/propagate_service.rb +24 -0
  69. data/lib/wakame/command/shutdown_cluster.rb +15 -0
  70. data/lib/wakame/command/status.rb +81 -0
  71. data/lib/wakame/command.rb +31 -0
  72. data/lib/wakame/command_queue.rb +44 -0
  73. data/lib/wakame/configuration.rb +93 -0
  74. data/lib/wakame/daemonize.rb +96 -0
  75. data/lib/wakame/event.rb +232 -0
  76. data/lib/wakame/event_dispatcher.rb +154 -0
  77. data/lib/wakame/graph.rb +79 -0
  78. data/lib/wakame/initializer.rb +162 -0
  79. data/lib/wakame/instance_counter.rb +78 -0
  80. data/lib/wakame/logger.rb +12 -0
  81. data/lib/wakame/manager/commands.rb +134 -0
  82. data/lib/wakame/master.rb +369 -0
  83. data/lib/wakame/monitor/agent.rb +50 -0
  84. data/lib/wakame/monitor/service.rb +183 -0
  85. data/lib/wakame/monitor.rb +69 -0
  86. data/lib/wakame/packets.rb +160 -0
  87. data/lib/wakame/queue_declare.rb +14 -0
  88. data/lib/wakame/rule.rb +116 -0
  89. data/lib/wakame/rule_engine.rb +202 -0
  90. data/lib/wakame/runner/administrator_command.rb +112 -0
  91. data/lib/wakame/runner/agent.rb +81 -0
  92. data/lib/wakame/runner/master.rb +93 -0
  93. data/lib/wakame/scheduler.rb +251 -0
  94. data/lib/wakame/service.rb +914 -0
  95. data/lib/wakame/template.rb +189 -0
  96. data/lib/wakame/trigger.rb +66 -0
  97. data/lib/wakame/triggers/instance_count_update.rb +45 -0
  98. data/lib/wakame/triggers/load_history.rb +107 -0
  99. data/lib/wakame/triggers/maintain_ssh_known_hosts.rb +43 -0
  100. data/lib/wakame/triggers/process_command.rb +34 -0
  101. data/lib/wakame/triggers/shutdown_unused_vm.rb +16 -0
  102. data/lib/wakame/util.rb +569 -0
  103. data/lib/wakame/vm_manipulator.rb +186 -0
  104. data/lib/wakame.rb +59 -0
  105. data/tasks/ec2.rake +127 -0
  106. data/tests/cluster.json +3 -0
  107. data/tests/conf/a +1 -0
  108. data/tests/conf/b +1 -0
  109. data/tests/conf/c +1 -0
  110. data/tests/setup_agent.rb +39 -0
  111. data/tests/setup_master.rb +28 -0
  112. data/tests/test_actor.rb +54 -0
  113. data/tests/test_agent.rb +218 -0
  114. data/tests/test_amqp_client.rb +94 -0
  115. data/tests/test_graph.rb +36 -0
  116. data/tests/test_master.rb +167 -0
  117. data/tests/test_monitor.rb +47 -0
  118. data/tests/test_rule_engine.rb +127 -0
  119. data/tests/test_scheduler.rb +123 -0
  120. data/tests/test_service.rb +60 -0
  121. data/tests/test_template.rb +67 -0
  122. data/tests/test_uri_amqp.rb +19 -0
  123. data/tests/test_util.rb +71 -0
  124. data/wakame_generators/resource/resource_generator.rb +54 -0
  125. data/wakame_generators/resource/templates/apache_app/apache_app.rb +60 -0
  126. data/wakame_generators/resource/templates/apache_app/conf/apache2.conf +46 -0
  127. data/wakame_generators/resource/templates/apache_app/conf/envvars-app +7 -0
  128. data/wakame_generators/resource/templates/apache_app/conf/sites-app.conf +23 -0
  129. data/wakame_generators/resource/templates/apache_app/conf/system-app.conf +67 -0
  130. data/wakame_generators/resource/templates/apache_app/init.d/apache2-app +192 -0
  131. data/wakame_generators/resource/templates/apache_lb/apache_lb.rb +67 -0
  132. data/wakame_generators/resource/templates/apache_lb/conf/apache2.conf +46 -0
  133. data/wakame_generators/resource/templates/apache_lb/conf/envvars-lb +6 -0
  134. data/wakame_generators/resource/templates/apache_lb/conf/sites-lb.conf +54 -0
  135. data/wakame_generators/resource/templates/apache_lb/conf/system-lb.conf +75 -0
  136. data/wakame_generators/resource/templates/apache_lb/init.d/apache2-lb +192 -0
  137. data/wakame_generators/resource/templates/apache_www/apache_www.rb +56 -0
  138. data/wakame_generators/resource/templates/apache_www/conf/apache2.conf +47 -0
  139. data/wakame_generators/resource/templates/apache_www/conf/envvars-www +7 -0
  140. data/wakame_generators/resource/templates/apache_www/conf/sites-www.conf +23 -0
  141. data/wakame_generators/resource/templates/apache_www/conf/system-www.conf +63 -0
  142. data/wakame_generators/resource/templates/apache_www/init.d/apache2-www +192 -0
  143. data/wakame_generators/resource/templates/ec2_elastic_ip/ec2_elastic_ip.rb +39 -0
  144. data/wakame_generators/resource/templates/mysql_master/conf/my.cnf +154 -0
  145. data/wakame_generators/resource/templates/mysql_master/init.d/mysql +185 -0
  146. data/wakame_generators/resource/templates/mysql_master/mysql_master.rb +119 -0
  147. metadata +289 -0
@@ -0,0 +1,189 @@
1
+
2
+ require 'fileutils'
3
+ require 'erb'
4
+
5
+ module Wakame
6
+ class Template
7
+ attr_accessor :service_instance
8
+ attr_reader :tmp_basedir
9
+
10
+ def initialize(service_instance)
11
+ @service_instance = service_instance
12
+ @tmp_basedir = File.expand_path(Util.gen_id, File.join(Wakame.config.root_path, 'tmp', 'config') )
13
+ FileUtils.mkdir_p @tmp_basedir
14
+ end
15
+
16
+ def basedir
17
+ @service_instance.resource.basedir
18
+ end
19
+
20
+ def render_config
21
+ service_instance.property.render_config(self)
22
+ end
23
+
24
+ def cleanup
25
+ FileUtils.rm_r( @tmp_basedir, :force=>true)
26
+ end
27
+
28
+ def render(args)
29
+ args = [args] if args.is_a? String
30
+
31
+ args.each { |path|
32
+ update(path) { |buf|
33
+ ERB.new(buf, nil, '-').result(service_instance.export_binding)
34
+ }
35
+ }
36
+ end
37
+
38
+ def cp(args)
39
+ args = [args] if args.is_a? String
40
+ args.each { |fname|
41
+
42
+ destpath = File.expand_path(fname, @tmp_basedir)
43
+ FileUtils.mkpath(File.dirname(destpath)) unless File.directory?(File.dirname(destpath))
44
+
45
+ FileUtils.cp_r(File.join(basedir, fname),
46
+ destpath,
47
+ {:preserve=>true}
48
+ )
49
+ }
50
+ end
51
+
52
+ def chmod(fname, mode)
53
+ File.chmod(mode, File.join(@tmp_basedir, fname))
54
+ end
55
+
56
+ def load(path)
57
+ path = path.sub(/^\//, '')
58
+
59
+ return File.readlines(File.expand_path(path, basedir), "r").join('')
60
+ end
61
+
62
+ def save(path, buf)
63
+ path = path.sub(/^\//, '')
64
+
65
+ destpath = File.expand_path(path, @tmp_basedir)
66
+ FileUtils.mkpath(File.dirname(destpath)) unless File.directory?(File.dirname(destpath))
67
+
68
+ File.open(destpath, "w", 0644) { |f|
69
+ f.write(buf)
70
+ }
71
+ end
72
+
73
+ def update(path, &blk)
74
+ buf = load(path)
75
+ buf = yield buf if block_given?
76
+ save(path, buf)
77
+ end
78
+
79
+ end
80
+
81
+ module Template2
82
+ class Base
83
+ attr_reader :tmp_output_basedir
84
+
85
+ def initialize()
86
+ end
87
+
88
+
89
+ def pre_render
90
+ @tmp_output_basedir = File.expand_path(Util.gen_id, Wakame.config.config_tmp_root)
91
+ FileUtils.mkdir_p @tmp_output_basedir
92
+ end
93
+
94
+ def render(service_instance)
95
+ end
96
+
97
+ def post_render
98
+
99
+ end
100
+
101
+ # Returns source path which to be synced to the agent host
102
+ def sync_src
103
+ end
104
+
105
+
106
+ def load(path)
107
+ path = path.sub(/^\//, '')
108
+
109
+ return File.readlines(File.expand_path(path, Wakame.config.config_template_root), "r").join('')
110
+ end
111
+
112
+ def save(path, buf)
113
+ path = path.sub(/^\//, '')
114
+
115
+ destpath = File.expand_path(path, @tmp_output_basedir)
116
+ FileUtils.mkpath(File.dirname(destpath)) unless File.directory?(File.dirname(destpath))
117
+
118
+ File.open(destpath, "w", 0644) {|f|
119
+ f.write(buf)
120
+ }
121
+ end
122
+
123
+ def update(path, &blk)
124
+ buf = load(path)
125
+ buf = yield buf if block_given?
126
+ save(path, buf)
127
+ end
128
+
129
+
130
+ end
131
+
132
+ class MySQLTemplate < Base
133
+ def render(service_instance)
134
+ require 'erb'
135
+
136
+ FileUtils.mkpath File.expand_path('mysql', tmp_output_basedir)
137
+
138
+ ["mysql/my.cnf"].each { |path|
139
+ update(path) { |buf|
140
+ ERB.new(buf, nil, '-').result service_instance.export_binding
141
+ }
142
+ }
143
+ end
144
+
145
+
146
+ def sync_src
147
+ File.join(@tmp_output_basedir, 'mysql')
148
+ end
149
+ end
150
+
151
+ class ApacheTemplate < Base
152
+ attr_accessor :suffix
153
+
154
+ def initialize(suffix)
155
+ super()
156
+ @suffix = suffix
157
+ end
158
+
159
+ def render(service_instance)
160
+ #p service_instance.service_cluster.virtual_hosts
161
+ require 'erb'
162
+
163
+ FileUtils.mkpath File.expand_path('apache2', tmp_output_basedir)
164
+
165
+ ["envvars-#{@suffix}", "apache2.conf"].each { |fname|
166
+ FileUtils.cp_r(File.join(Wakame.config.config_template_root, "apache2/#{fname}"),
167
+ File.join(tmp_output_basedir, "apache2/#{fname}"),
168
+ {:preserve=>true})
169
+ }
170
+
171
+ ["apache2/system-#{@suffix}.conf", "apache2/sites-#{@suffix}.conf"].each { |path|
172
+ update(path) { |buf|
173
+ ERB.new(buf, nil, '-').result service_instance.export_binding
174
+ }
175
+ }
176
+ end
177
+
178
+
179
+ def sync_src
180
+ File.join(@tmp_output_basedir, 'apache2')
181
+ end
182
+
183
+ end
184
+
185
+
186
+ end
187
+
188
+
189
+ end
@@ -0,0 +1,66 @@
1
+ module Wakame
2
+ class Trigger
3
+ include FilterChain
4
+ include AttributeHelper
5
+
6
+ def_attribute :enabled, true
7
+
8
+ attr_reader :rule_engine
9
+ def command_queue
10
+ @rule_engine.command_queue
11
+ end
12
+
13
+ def service_cluster
14
+ @rule_engine.service_cluster
15
+ end
16
+
17
+ def master
18
+ @rule_engine.master
19
+ end
20
+
21
+ def agent_monitor
22
+ @rule_engine.agent_monitor
23
+ end
24
+
25
+ def bind_engine(rule_engine)
26
+ @rule_engine = rule_engine
27
+ end
28
+
29
+ def trigger_action(action)
30
+ found = rule_engine.active_jobs.find { |id, job|
31
+ job[:src_rule].class == self.class
32
+ }
33
+
34
+ if found
35
+ Wakame.log.warn("#{self.class}: Exisiting Job \"#{found[:job_id]}\" was kicked from this rule and it's still running. Skipping...")
36
+ raise CancelActionError
37
+ end
38
+
39
+ rule_engine.create_job_context(self, action)
40
+ action.bind_triggered_rule(self)
41
+
42
+ rule_engine.run_action(action)
43
+ action.job_id
44
+ end
45
+
46
+ def register_hooks
47
+ end
48
+
49
+ def cleanup
50
+ end
51
+
52
+ protected
53
+ def event_subscribe(event_class, &blk)
54
+ EventDispatcher.subscribe(event_class) { |event|
55
+ begin
56
+ run_filter(self)
57
+ blk.call(event) if self.enabled
58
+ rescue => e
59
+ Wakame.log.error(e)
60
+ end
61
+ }
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,45 @@
1
+ module Wakame
2
+ module Triggers
3
+ class InstanceCountUpdate < Trigger
4
+ append_filter { |rule|
5
+ rule.service_cluster.status == Service::ServiceCluster::STATUS_ONLINE
6
+ }
7
+
8
+ def register_hooks
9
+ @instance_counters = {}
10
+ ms = Scheduler::PerHourSequence.new
11
+ #ms[0]=1
12
+ #ms[30]=5
13
+ ms[0]=1
14
+ ms["0:2:00"]=4
15
+ ms["0:5:00"]=1
16
+ ms["0:9:00"]=4
17
+ ms["0:13:00"]=1
18
+ ms["0:17:00"]=4
19
+ ms["0:21:00"]=1
20
+ ms["0:25:00"]=1
21
+ ms["0:29:00"]=1
22
+ ms["0:33:00"]=4
23
+ #ms["0:37:00"]=1
24
+ #ms["0:41:00"]=4
25
+ ms["0:45:00"]=1
26
+ ms["0:49:00"]=4
27
+ ms["0:53:00"]=1
28
+ ms["0:57:00"]=4
29
+ # @instance_counters[Apache_APP.to_s] = TimedCounter.new(Scheduler::LoopSequence.new(ms), self)
30
+
31
+ event_subscribe(Event::InstanceCountChanged) { |event|
32
+ next if service_cluster.status == Service::ServiceCluster::STATUS_OFFLINE
33
+
34
+ if event.increased?
35
+ Wakame.log.debug("#{self.class}: trigger PropagateInstancesAction.new(#{event.resource.class})")
36
+ trigger_action(Actions::PropagateInstancesAction.new(event.resource))
37
+ elsif event.decreased?
38
+ Wakame.log.debug("#{self.class}: trigger DestroyInstancesAction.new(#{event.resource.class})")
39
+ trigger_action(Actions::DestroyInstancesAction.new(event.resource))
40
+ end
41
+ }
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,107 @@
1
+
2
+ module Wakame
3
+ module Triggers
4
+ class LoadHistoryMonitor < Trigger
5
+ class AgentLoadHighEvent < Wakame::Event::Base
6
+ attr_reader :agent, :load_avg
7
+ def initialize(agent, load_avg)
8
+ super()
9
+ @agent = agent
10
+ @load_avg = load_avg
11
+ end
12
+ end
13
+ class AgentLoadNormalEvent < Wakame::Event::Base
14
+ attr_reader :agent, :load_avg
15
+ def initialize(agent, load_avg)
16
+ super()
17
+ @agent = agent
18
+ @load_avg = load_avg
19
+ end
20
+ end
21
+ class ServiceLoadHighEvent < Wakame::Event::Base
22
+ attr_reader :service_property, :load_avg
23
+ def initialize(svc_prop, load_avg)
24
+ super()
25
+ @service_property = svc_prop
26
+ @load_avg = load_avg
27
+ end
28
+ end
29
+ class ServiceLoadNormalEvent < Wakame::Event::Base
30
+ attr_reader :service_property, :load_avg
31
+ def initialize(svc_prop, load_avg)
32
+ super()
33
+ @service_property = svc_prop
34
+ @load_avg = load_avg
35
+ end
36
+ end
37
+
38
+ def initialize
39
+ @agent_data = {}
40
+ @service_data = {}
41
+ @high_threashold = 1.2
42
+ @history_period = 3
43
+ end
44
+
45
+ def register_hooks
46
+ event_subscribe(Event::AgentMonitored) { |event|
47
+ @agent_data[event.agent.agent_id]={:load_history=>[], :last_event=>:normal}
48
+ service_cluster.properties.each { |klass, prop|
49
+ @service_data[klass] ||= {:load_history=>[], :last_event=>:normal}
50
+ }
51
+ }
52
+ event_subscribe(Event::AgentUnMonitored) { |event|
53
+ @agent_data.delete(event.agent.agent_id)
54
+ }
55
+
56
+ event_subscribe(Event::AgentPong) { |event|
57
+ calc_load(event.agent)
58
+ }
59
+ end
60
+
61
+ private
62
+ def calc_load(agent)
63
+ data = @agent_data[agent.agent_id] || next
64
+ data[:load_history] << agent.attr[:uptime]
65
+ Wakame.log.debug("Load History for agent \"#{agent.agent_id}\": " + data[:load_history].inspect )
66
+ detect_threadshold(data, proc{
67
+ ED.fire_event(AgentLoadHighEvent.new(agent, data[:load_history][-1]))
68
+ }, proc{
69
+ ED.fire_event(AgentLoadNormalEvent.new(agent, data[:load_history][-1]))
70
+ })
71
+
72
+ # service_cluster.services.each { |id, svc|
73
+ # next unless agent.services.keys.include? id
74
+ # data = @service_data[svc.property.class.to_s] || next
75
+
76
+ # data[:load_history] << agent.attr[:uptime]
77
+ # Wakame.log.debug("Load History for service \"#{svc.property.class}\": " + data[:load_history].inspect )
78
+ # detect_threadshold(data, proc{
79
+ # ED.fire_event(ServiceLoadHighEvent.new(svc.property, data[:load_history][-1]))
80
+ # }, proc{
81
+ # ED.fire_event(ServiceLoadNormalEvent.new(svc.property, data[:load_history][-1]))
82
+ # })
83
+ # }
84
+
85
+ end
86
+
87
+ def detect_threadshold(data, when_high, when_low)
88
+ hist = data[:load_history]
89
+ if hist.size >= @history_period
90
+
91
+ all_higher = hist.all? { |h| h > @high_threashold }
92
+
93
+ if data[:last_event] == :normal && all_higher
94
+ when_high.call
95
+ data[:last_event] = :high
96
+ end
97
+ if data[:last_event] == :high && !all_higher
98
+ when_low.call
99
+ data[:last_event] = :normal
100
+ end
101
+ end
102
+ hist.shift while hist.size > @history_period
103
+ end
104
+
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,43 @@
1
+ module Wakame
2
+ module Triggers
3
+ class MaintainSshKnownHosts < Trigger
4
+ class UpdateKnownHosts < Action
5
+ require 'fileutils'
6
+
7
+ def run
8
+ host_keys = []
9
+ ['/etc/ssh/ssh_host_rsa_key.pub', '/etc/ssh/ssh_host_dsa_key.pub'].each { |k|
10
+ next unless File.file? k
11
+ host_keys << File.readlines(k).join('').chomp.sub(/ host$/, '')
12
+ }
13
+ return if host_keys.empty?
14
+
15
+ basedir = File.dirname(Wakame.config.ssh_known_hosts)
16
+ FileUtils.mkpath(basedir) unless File.exist? basedir
17
+
18
+ tmpfile = File.expand_path(File.basename(Wakame.config.ssh_known_hosts) + '.tmp', basedir)
19
+ File.open(tmpfile, 'w') { |f|
20
+ agent_monitor.registered_agents.each { |k, agent|
21
+ host_keys.each { |k|
22
+ f << "#{Wakame::Util.ssh_known_hosts_hash(agent.agent_ip)} #{k}\n"
23
+ }
24
+ }
25
+ }
26
+
27
+ FileUtils.move(tmpfile, Wakame.config.ssh_known_hosts, {:force=>true})
28
+ end
29
+ end
30
+
31
+
32
+ def register_hooks
33
+ event_subscribe(Event::AgentMonitored) { |event|
34
+ trigger_action(UpdateKnownHosts.new)
35
+ }
36
+
37
+ event_subscribe(Event::AgentUnMonitored) { |event|
38
+ trigger_action(UpdateKnownHosts.new)
39
+ }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,34 @@
1
+
2
+ module Wakame
3
+ module Triggers
4
+ class ProcessCommand < Trigger
5
+ def register_hooks
6
+ @@command_thread ||= Thread.new {
7
+ while cmd = self.command_queue.deq_cmd
8
+ res = nil
9
+ begin
10
+ EM.barrier {
11
+ Wakame.log.debug("#{self.class}: Being processed the command: #{cmd.class}")
12
+ cmd.run(self)
13
+ res = cmd
14
+ }
15
+ rescue => e
16
+ Wakame.log.error(e)
17
+ res = e
18
+ ensure
19
+ self.command_queue.enq_result(res)
20
+ end
21
+ end
22
+ }
23
+
24
+ #event_subscribe(Event::CommandReceived) { |event|
25
+ # event.command.run(self)
26
+ #}
27
+ end
28
+
29
+ def cleanup
30
+ @@command_thread.kill if @@command_thread.alive?
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+ module Wakame
2
+ module Triggers
3
+ class ShutdownUnusedVM < Trigger
4
+ def register_hooks
5
+ event_subscribe(Event::AgentPong) { |event|
6
+ if event.agent.services.empty? &&
7
+ Time.now - event.agent.last_service_assigned_at > Wakame.config.unused_vm_live_period &&
8
+ event.agent.agent_id != master.attr[:instance_id]
9
+ Wakame.log.info("Shutting the unused VM down: #{event.agent.agent.id}")
10
+ trigger_action(ShutdownVM.new(event.agent))
11
+ end
12
+ }
13
+ end
14
+ end
15
+ end
16
+ end