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,79 @@
1
+
2
+ module Wakame
3
+
4
+ class Graph
5
+
6
+ def initialize
7
+ @edges={}
8
+ end
9
+
10
+ def add_edge(v, c)
11
+ return if v == c
12
+ add_vertex(v)
13
+ add_vertex(c)
14
+ @edges[v][c]=1
15
+ end
16
+
17
+ def remove_edge(v, c)
18
+ @edges[v].delete(c)
19
+ end
20
+
21
+ def has_edge?(v, c)
22
+ @edges.has_key?(v) && @edges[v].has_key?(c)
23
+ end
24
+
25
+ def add_vertex(v)
26
+ @edges[v] ||= {}
27
+ end
28
+
29
+ def remove_vertex(v)
30
+ @edges.keys.each { |n|
31
+ n.delete(v)
32
+ }
33
+ @edges.delete(v)
34
+ end
35
+
36
+ def parents(v)
37
+ plist=[]
38
+ @edges.each { |n, c|
39
+ plist << n if c.has_key?(v)
40
+ }
41
+ plist
42
+ end
43
+
44
+ def children(v)
45
+ @edges[v].keys
46
+ end
47
+
48
+ def inspect
49
+ str="#{self}:"
50
+ str << @edges.keys.sort.collect { |v|
51
+ "#{v}=>#{@edges[v].keys.inspect}"
52
+ }.join(', ')
53
+ str
54
+ end
55
+
56
+
57
+ def level_layout(root)
58
+ @vtx_ylevels = {root=>0}
59
+
60
+ descend(root, 0)
61
+
62
+ ycoord=Array.new(@vtx_ylevels.values.max)
63
+ @vtx_ylevels.each { |v, lv|
64
+ (ycoord[lv] ||= []) << v
65
+ }
66
+
67
+ ycoord
68
+ end
69
+
70
+ private
71
+ def descend(v, ylevel=0)
72
+ ylevel += 1
73
+ @edges[v].keys.each { |c|
74
+ @vtx_ylevels[c]=ylevel if @vtx_ylevels[c].nil? || @vtx_ylevels[c] < ylevel
75
+ descend(c, ylevel)
76
+ }
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,162 @@
1
+
2
+ WAKAME_ENV = (ENV['WAKAME_ENV'] || 'StandAlone').dup.to_sym unless defined?(WAKAME_ENV)
3
+
4
+ module Wakame
5
+ class Initializer
6
+
7
+ class << self
8
+ def run(command, configuration=Configuration.new)
9
+ @instance ||= new(configuration)
10
+ @instance.send(command)
11
+ end
12
+
13
+ def instance
14
+ @instance
15
+ end
16
+ end
17
+
18
+ attr_reader :configuration
19
+
20
+ def initialize(configuration)
21
+ @configuration = configuration
22
+ end
23
+
24
+ def process
25
+ setup_load_paths
26
+ setup_logger
27
+ load_environment
28
+ end
29
+
30
+ def process_master
31
+ process
32
+ load_cluster
33
+ load_resources
34
+ load_core_commands
35
+ load_core_actions
36
+ load_core_triggers
37
+ end
38
+
39
+ def process_agent
40
+ process
41
+ load_actors
42
+ end
43
+
44
+ def process_cli
45
+ process
46
+ load_core_commands
47
+ end
48
+
49
+ def setup_load_paths
50
+ load_paths = configuration.load_paths + configuration.framework_paths
51
+ load_paths.reverse_each { |dir| $LOAD_PATH.unshift(dir) if File.directory?(dir) }
52
+ $LOAD_PATH.uniq!
53
+
54
+ require 'wakame'
55
+ end
56
+
57
+ def setup_logger
58
+ require 'log4r'
59
+ Logger.log = begin
60
+ #log = Logger.new((Wakame.root||Dir.pwd) / "log.log")
61
+ out = ::Log4r::StdoutOutputter.new('stdout',
62
+ :formatter => Log4r::PatternFormatter.new(
63
+ :pattern => "%d %C [%l]: %M",
64
+ :date_format => "%Y/%m/%d %H:%M:%S"
65
+ )
66
+ )
67
+ log = ::Log4r::Logger.new(File.basename($0.to_s))
68
+ log.add(out)
69
+ log
70
+ end
71
+ end
72
+
73
+
74
+ def setup_system_actors
75
+ end
76
+
77
+ def load_system_monitors
78
+ end
79
+
80
+ def load_core_commands
81
+ # %w( cluster/commands ).each { |load_path|
82
+ # load_path = File.expand_path(load_path, configuration.root_path)
83
+ # matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
84
+ # Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
85
+ # require file.sub(matcher, '\1')
86
+ # end
87
+ # }
88
+
89
+ $LOAD_PATH.each{ |d|
90
+ Dir.glob("#{d}/wakame/command/**/*.rb").each{ |f|
91
+ f =~ %r{(wakame/command/.+)\.rb\Z}
92
+ require "#{$1}"
93
+ }
94
+ }
95
+
96
+ #%w(launch_cluster shutdown_cluster status action_status actor).each { |f|
97
+ # require "wakame/command/#{f}"
98
+ #}
99
+ end
100
+
101
+
102
+ def load_environment
103
+ config = configuration
104
+ constants = self.class.constants
105
+
106
+ [:common, config.environment].each { |key|
107
+ eval(IO.read(config.environment_path(key)), binding, config.environment_path(key))
108
+ }
109
+
110
+ (self.class.constants - constants).each do |const|
111
+ Object.const_set(const, self.class.const_get(const))
112
+ end
113
+ end
114
+
115
+ def load_resources
116
+ load_path = File.expand_path('cluster/resources', configuration.root_path)
117
+ Dir.glob("#{load_path}/*/*.rb").sort.each do |file|
118
+ if file =~ %r{\A#{Regexp.escape(load_path)}/([^/]+)/([^/]+)\.rb\Z} && $1 == $2
119
+ Wakame.log.debug("Loading resource definition: #{file}")
120
+ load file
121
+ end
122
+ #require file.sub(matcher, '\1')
123
+ end
124
+
125
+ end
126
+
127
+
128
+ def load_cluster
129
+ load File.expand_path('config/cluster.rb', configuration.root_path)
130
+ end
131
+
132
+ def load_actors
133
+ load_path = File.expand_path('cluster/actors', configuration.root_path)
134
+ Dir.glob("#{load_path}/*.rb").sort.each do |file|
135
+ if file =~ %r{\A#{Regexp.escape(load_path)}/([^/]+)\.rb\Z}
136
+ Wakame.log.debug("Loading Actor: #{file}")
137
+ load file
138
+ end
139
+ end
140
+ end
141
+
142
+ def load_core_triggers
143
+ $LOAD_PATH.each{ |d|
144
+ Dir.glob("#{d}/wakame/triggers/**/*.rb").each{ |f|
145
+ f =~ %r{(wakame/triggers/.+)\.rb\Z}
146
+ require "#{$1}"
147
+ }
148
+ }
149
+ end
150
+
151
+ def load_core_actions
152
+ $LOAD_PATH.each{ |d|
153
+ Dir.glob("#{d}/wakame/actions/**/*.rb").each{ |f|
154
+ f =~ %r{(wakame/actions/.+)\.rb\Z}
155
+ require "#{$1}"
156
+ }
157
+ }
158
+ end
159
+
160
+
161
+ end
162
+ end
@@ -0,0 +1,78 @@
1
+ module Wakame
2
+ class InstanceCounter
3
+ class OutOfLimitRangeError < StandardError; end
4
+
5
+ include AttributeHelper
6
+
7
+ def bind_resource(resource)
8
+ @resource = resource
9
+ end
10
+
11
+ def resource
12
+ @resource
13
+ end
14
+
15
+ def instance_count
16
+ raise NotImplementedError
17
+ end
18
+
19
+ protected
20
+ def check_hard_limit(count=self.instance_count)
21
+ Range.new(@resource.min_instances, @resource.max_instances, true).include?(count)
22
+ end
23
+ end
24
+
25
+
26
+ class ConstantCounter < InstanceCounter
27
+ def initialize(resource)
28
+ @instance_count = 1
29
+ bind_resource(resource)
30
+ end
31
+
32
+ def instance_count
33
+ @instance_count
34
+ end
35
+
36
+ def instance_count=(count)
37
+ raise OutOfLimitRangeError unless check_hard_limit(count)
38
+ if @instance_count != count
39
+ prev = @instance_count
40
+ @instance_count = count
41
+ ED.fire_event(Event::InstanceCountChanged.new(@resource, prev, count))
42
+ end
43
+ end
44
+ end
45
+
46
+ class TimedCounter < InstanceCounter
47
+ def initialize(seq, resource)
48
+ @sequence = seq
49
+ bind_resource(resource)
50
+ @timer = Scheduler::SequenceTimer.new(seq)
51
+ @timer.add_observer(self)
52
+ @instance_count = 1
53
+ end
54
+
55
+ def instance_count
56
+ @instance_count
57
+ end
58
+
59
+ def update(*args)
60
+ new_count = args[0]
61
+ if @instance_count != count
62
+ prev = @instance_count
63
+ @instance_count = count
64
+ ED.fire_event(Event::InstanceCountChanged.new(@resource, prev, count))
65
+ end
66
+ #if self.min > new_count || self.max < new_count
67
+ #if self.min != new_count || self.max != new_count
68
+ # prev_min = self.min
69
+ # prev_max = self.max
70
+
71
+ # self.max = self.min = new_count
72
+ # ED.fire_event(Event::InstanceCountChanged.new(@resource, prev_min, prev_max, self.min, self.max))
73
+ #end
74
+
75
+ end
76
+ end
77
+
78
+ end
@@ -0,0 +1,12 @@
1
+
2
+ module Wakame
3
+ module Logger
4
+ def self.log
5
+ @log
6
+ end
7
+
8
+ def self.log=(log)
9
+ @log = log
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,134 @@
1
+
2
+ module Wakame
3
+ module Manager
4
+ module Commands
5
+
6
+ class Nop
7
+ end
8
+
9
+ class ClusterLaunch
10
+ end
11
+
12
+ class ClusterShutdown
13
+ end
14
+
15
+ class DeployConfig
16
+ attr_reader :property
17
+ def initialize(prop=nil)
18
+ @property = prop
19
+ end
20
+ end
21
+
22
+ class PropagateService
23
+ attr_reader :property
24
+ def initialize(prop)
25
+ @property = prop
26
+ end
27
+ end
28
+
29
+ class MigrateService
30
+ attr_reader :service_instance, :agent
31
+ def initialize(svc_inst, agent=nil)
32
+ @service_instance = svc_inst
33
+ @agent = agent
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ class CommandDelegator
40
+
41
+ attr_reader :command_queue
42
+ def initialize(command_queue)
43
+ @command_queue = command_queue
44
+ end
45
+
46
+ def nop
47
+ @command_queue.send_cmd(Commands::Nop.new)
48
+ end
49
+
50
+ def launch_cluster
51
+ @command_queue.send_cmd(Commands::ClusterLaunch.new)
52
+ end
53
+
54
+ def shutdown_cluster
55
+ @command_queue.send_cmd(Commands::ClusterShutdown.new)
56
+ end
57
+ def propagate_service(prop_name)
58
+ prop = nil
59
+ prop = master.service_cluster.properties[prop_name.to_s]
60
+ if prop.nil?
61
+ raise "UnknownProperty: #{prop_name}"
62
+ end
63
+
64
+ @command_queue.send_cmd(Commands::PropagateService.new(prop))
65
+ end
66
+ def migrate_service(svc_inst_id, agent_id=nil)
67
+ svc = master.service_cluster.instances[svc_inst_id]
68
+ if svc.nil?
69
+ raise "Unknown Service Instance: #{svc_inst_id}"
70
+ end
71
+ agent = nil
72
+ if agent_id
73
+ agent = master.agent_monitor.agents.has_key?(agent_id)
74
+ if agent.nil?
75
+ raise "Unknown Agent: #{agent_id}"
76
+ end
77
+ end
78
+
79
+ @command_queue.send_cmd(Commands::MigrateService.new(svc, agent))
80
+ end
81
+ def deploy_config(prop_name=nil)
82
+ prop = nil
83
+ @command_queue.send_cmd(Commands::DeployConfig.new)
84
+ end
85
+
86
+ def status
87
+ EM.barrier {
88
+ master = Master.instance
89
+
90
+ sc = master.service_cluster
91
+ result = {
92
+ :rule_engine => {
93
+ :rules => sc.rule_engine.rules
94
+ },
95
+ :service_cluster => sc.dump_status,
96
+ :agent_monitor => master.agent_monitor.dump_status
97
+ }
98
+ result
99
+ }
100
+ end
101
+
102
+ def action_status
103
+ walk_subactions = proc { |a, level|
104
+ res = a.dump_attrs
105
+ unless a.subactions.empty?
106
+ res[:subactions] = a.subactions.collect { |s|
107
+ walk_subactions.call(s, level + 1)
108
+ }
109
+ end
110
+ res
111
+ }
112
+
113
+ EM.barrier {
114
+ result = {}
115
+ Master.instance.service_cluster.rule_engine.active_jobs.each { |id, v|
116
+ result[id]={:actions=>[], :created_at=>v[:created_at], :src_rule=>v[:src_rule].class.to_s}
117
+
118
+ result[id][:root_action] = walk_subactions.call(v[:root_action], 0)
119
+ }
120
+
121
+ result
122
+ }
123
+ end
124
+
125
+
126
+ private
127
+ def master
128
+ command_queue.master
129
+ end
130
+
131
+ end
132
+
133
+ end
134
+ end