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,93 @@
1
+ require 'uri'
2
+ require 'ext/uri'
3
+ require 'optparse'
4
+
5
+ require 'amqp'
6
+
7
+ module Wakame
8
+ module Runner
9
+ class Master
10
+ include Wakame::Daemonize
11
+
12
+ def initialize(argv)
13
+ @argv = argv
14
+
15
+ @options = {
16
+ :amqp_server => URI.parse('amqp://guest@localhost/'),
17
+ :log_file => '/var/log/wakame-master.log',
18
+ :pid_file => '/var/run/wakame/wakame-master.pid',
19
+ :daemonize => true
20
+ }
21
+
22
+ parser.parse! @argv
23
+ end
24
+
25
+
26
+ def parser
27
+ @parser ||= OptionParser.new do |opts|
28
+ opts.banner = "Usage: master [options]"
29
+
30
+ opts.separator ""
31
+ opts.separator "Master options:"
32
+ opts.on( "-p", "--pid PIDFILE", "pid file path" ) {|str| @options[:pid_file] = str }
33
+ opts.on( "-u", "--uid UID", "user id for the running process" ) {|str| @options[:uid] = str }
34
+ opts.on( "-s", "--server AMQP_URI", "amqp server" ) {|str|
35
+ begin
36
+ @options[:amqp_server] = URI.parse(str)
37
+ rescue URI::InvalidURIError => e
38
+ fail "#{e}"
39
+ end
40
+ }
41
+ opts.on("-X", "", "daemonize flag" ) { @options[:daemonize] = false }
42
+
43
+ end
44
+
45
+
46
+ end
47
+
48
+
49
+ def run
50
+ %w(QUIT INT TERM).each { |i|
51
+ Signal.trap(i) { Wakame::Master.stop{ remove_pidfile } }
52
+ }
53
+
54
+ unless @options[:amqp_server].nil?
55
+ uri = @options[:amqp_server]
56
+ default = ::AMQP.settings
57
+ opts = {:host => uri.host,
58
+ :port => uri.port || default[:port],
59
+ :vhost => uri.vhost || default[:vhost],
60
+ :user=>uri.user || default[:user],
61
+ :pass=>uri.password ||default[:pass]
62
+ }
63
+ else
64
+ opts = nil
65
+ end
66
+
67
+ if @options[:daemonize]
68
+ daemonize(@options[:log_file])
69
+ end
70
+
71
+ change_privilege(@options[:uid]) if @options[:uid]
72
+
73
+
74
+ EM.epoll if Wakame.config.eventmachine_use_epoll
75
+ EM.run {
76
+ Wakame::Master.start(opts)
77
+
78
+ EM.add_periodic_timer(5) {
79
+ next
80
+ buf = ''
81
+ buf << "<--- RUNNING THREADS --->\n"
82
+ ThreadGroup::Default.list.each { |i|
83
+ buf << "#{i.inspect} #{i[:name].to_s}\n"
84
+ }
85
+ buf << ">--- RUNNING THREADS ---<\n"
86
+ puts buf
87
+ }
88
+ }
89
+ end
90
+
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,251 @@
1
+
2
+ require 'wakame/util'
3
+
4
+ module Wakame
5
+ class Scheduler
6
+ def initialize
7
+ @channels = {}
8
+ end
9
+
10
+ def add_sequence(key, seq)
11
+ @channels[key]=seq
12
+ end
13
+
14
+
15
+ def next_event(time)
16
+ n = @channels.collect { |k, seq|
17
+ seq.next_event(time)
18
+ }
19
+ n.compact!
20
+ n = n.sort {|a,b| a[0] <=> b[0] }
21
+ n.first
22
+ end
23
+
24
+
25
+ class Sequence
26
+ def next_event(time)
27
+ end
28
+
29
+ def value_at(time)
30
+ end
31
+ end
32
+
33
+
34
+ class LoopSequence < Sequence
35
+ def initialize(timed_seq)
36
+ raise ArgumentError unless timed_seq.is_a?(TimedSequence)
37
+ @timed_sequence = timed_seq
38
+ end
39
+
40
+
41
+ def next_event(time)
42
+ @timed_sequence.start_at ||= time
43
+ unless @timed_sequence.range_check?(time)
44
+ @timed_sequence.start_at = time
45
+ end
46
+
47
+ event = @timed_sequence.next_event(time)
48
+ if event.nil?
49
+ puts "overwrap : #{time}"
50
+ # Here comes means that the current offset time is larger than the last offset time.
51
+ # So the value at next first offset time will become the return value.
52
+ f = @timed_sequence.first_event
53
+ #l = @timed_sequence.last_event
54
+
55
+ offset = ((@timed_sequence.start_at + @timed_sequence.duration) - time) + f[0]
56
+
57
+ event = [offset, f[1]]
58
+ end
59
+ event
60
+ end
61
+
62
+ def value_at(time)
63
+ @timed_sequence.start_at ||= time
64
+ unless @timed_sequence.range_check?(time)
65
+ @timed_sequence.start_at = time
66
+ end
67
+ #next_event(time)
68
+ @timed_sequence.value_at(time)
69
+ end
70
+
71
+ end
72
+
73
+
74
+ class TimedSequence
75
+ attr_accessor :start_at
76
+
77
+ def initialize(*args)
78
+ @event_list = SortedHash.new
79
+
80
+ if args.size == 1
81
+ case args[0]
82
+ when SortedHash
83
+ @event_list = args[0]
84
+ when Array
85
+ args[0].each { |a|
86
+ @event_list[a[0]] = a[1]
87
+ }
88
+ end
89
+ elsif args.size > 1 && args.all? { |a| a.is_a?(Array) && a.size == 2 }
90
+ ary.each { |a|
91
+ @event_list[a[0]] = a[1]
92
+ }
93
+ end
94
+
95
+ @event_list[0]=1 if @event_list.empty?
96
+ end
97
+
98
+ def set(offset_time, value)
99
+ if value.nil?
100
+ @event_list.delete(offset_time)
101
+ else
102
+ @event_list[offset_time] = value
103
+ end
104
+ end
105
+
106
+ def []=(offset_time, value)
107
+ set(offset_time, value)
108
+ end
109
+
110
+ def next_event(time)
111
+ if @start_at > time
112
+ event = first_event
113
+ elsif @start_at + duration <= time
114
+ event = nil
115
+ else
116
+ event = @event_list.find { |k, v|
117
+ start_at + k > time
118
+ }
119
+ end
120
+
121
+ return nil if event.nil?
122
+
123
+ # Set the offset time (in sec) from the given absolute time
124
+ event[0] = (@start_at + event[0].to_f) - time
125
+ event
126
+ end
127
+
128
+ def first_event
129
+ [@event_list.first_key, @event_list.first]
130
+ end
131
+
132
+ def last_event
133
+ [@event_list.last_key, @event_list.last]
134
+ end
135
+
136
+ def value_at(time)
137
+ return nil unless range_check?(time)
138
+ return @event_list.last if (start_at + @event_list.last_key) <= time
139
+
140
+ pos=0
141
+ @event_list.find { |k, v|
142
+ #puts "#{start_at} + #{k} > #{time}"
143
+ next true if start_at + k >= time
144
+ pos += 1
145
+ false
146
+ }
147
+ #puts "pos=#{pos}"
148
+ @event_list[@event_list.keys[((pos-1) < 0 ? 0 : (pos-1))]]
149
+ end
150
+
151
+ def duration
152
+ @event_list.last_key
153
+ end
154
+
155
+ def range_check?(time)
156
+ raise "start_at is not set (=nil)" if start_at.nil?
157
+ res = Range.new(start_at, start_at + duration.to_f).include?(time)
158
+ #Wakame.log.debug("#{self.class}.range_check?(#{start_at}, #{start_at + duration.to_f}).include?(#{time})=#{res}")
159
+ res
160
+ end
161
+
162
+ end
163
+
164
+
165
+ class UnitTimeSequence < TimedSequence
166
+ # Snap to the begging of the period
167
+ def start_at=(time)
168
+ @start_at = Time.at(time.tv_sec - (time.tv_sec % duration))
169
+ end
170
+
171
+ require 'time'
172
+
173
+ def set(offset_time, value)
174
+ offset_time = case offset_time
175
+ when String
176
+ t=Time.parse(offset_time)
177
+ t.tv_sec % duration
178
+ else
179
+ offset_time
180
+ end
181
+ super(offset_time, value) if offset_time < duration
182
+ self
183
+ end
184
+
185
+ def duration
186
+ raise NotImplementedError
187
+ end
188
+ end
189
+
190
+ class PerMinuteSequence < UnitTimeSequence
191
+ MINUTE_IN_SEC=60
192
+
193
+ def duration
194
+ MINUTE_IN_SEC
195
+ end
196
+ end
197
+
198
+
199
+ class PerHourSequence < UnitTimeSequence
200
+ HOUR_IN_SEC=60 * 60
201
+
202
+ def duration
203
+ HOUR_IN_SEC
204
+ end
205
+ end
206
+
207
+ class PerDaySequence < UnitTimeSequence
208
+ DAY_IN_SEC=60 * 60 * 24
209
+
210
+ def duration
211
+ DAY_IN_SEC
212
+ end
213
+ end
214
+
215
+ class PerWeekSequence < UnitTimeSequence
216
+ WEEK_IN_SEC=60 * 60 * 24 * 7
217
+ def duration
218
+ WEEK_IN_SEC
219
+ end
220
+ end
221
+
222
+ require 'observer'
223
+
224
+ class SequenceTimer < EM::PeriodicTimer
225
+ include Observable
226
+
227
+ def initialize(seq)
228
+ @sequence = seq
229
+ super(0) {
230
+ tnow = Time.now
231
+ v = @sequence.value_at(tnow)
232
+ self.changed
233
+ self.notify_observers(v)
234
+
235
+ ev = @sequence.next_event(tnow)
236
+ if ev.nil?
237
+ # Terminate this time when it runs out the event to be processed
238
+ Wakame.log.debug("#{self.class}: Quit the timer.")
239
+ cancel
240
+ else
241
+ @interval = ev[0]
242
+ Wakame.log.debug("#{tnow + ev[0]} - #{tnow}(offset sec=#{ev[0]}) : #{ev[1]}")
243
+ end
244
+ }
245
+ end
246
+
247
+
248
+ end
249
+
250
+ end
251
+ end