wakame 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +20 -0
- data/README.rdoc +63 -0
- data/Rakefile +86 -0
- data/VERSION +1 -0
- data/app_generators/wakame/templates/README +0 -0
- data/app_generators/wakame/templates/Rakefile +18 -0
- data/app_generators/wakame/templates/bin/wakame-agent +9 -0
- data/app_generators/wakame/templates/bin/wakame-master +9 -0
- data/app_generators/wakame/templates/bin/wakameadm +9 -0
- data/app_generators/wakame/templates/cluster/resources/apache_app/apache_app.rb +54 -0
- data/app_generators/wakame/templates/cluster/resources/apache_app/conf/apache2.conf +46 -0
- data/app_generators/wakame/templates/cluster/resources/apache_app/conf/envvars-app +7 -0
- data/app_generators/wakame/templates/cluster/resources/apache_app/conf/sites-app.conf +23 -0
- data/app_generators/wakame/templates/cluster/resources/apache_app/conf/system-app.conf +67 -0
- data/app_generators/wakame/templates/cluster/resources/apache_app/init.d/apache2-app +192 -0
- data/app_generators/wakame/templates/cluster/resources/apache_lb/apache_lb.rb +56 -0
- data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/apache2.conf +46 -0
- data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/envvars-lb +6 -0
- data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/sites-lb.conf +54 -0
- data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/system-lb.conf +75 -0
- data/app_generators/wakame/templates/cluster/resources/apache_lb/init.d/apache2-lb +192 -0
- data/app_generators/wakame/templates/cluster/resources/apache_www/apache_www.rb +50 -0
- data/app_generators/wakame/templates/cluster/resources/apache_www/conf/apache2.conf +47 -0
- data/app_generators/wakame/templates/cluster/resources/apache_www/conf/envvars-www +7 -0
- data/app_generators/wakame/templates/cluster/resources/apache_www/conf/sites-www.conf +23 -0
- data/app_generators/wakame/templates/cluster/resources/apache_www/conf/system-www.conf +63 -0
- data/app_generators/wakame/templates/cluster/resources/apache_www/init.d/apache2-www +192 -0
- data/app_generators/wakame/templates/cluster/resources/ec2_elastic_ip/ec2_elastic_ip.rb +39 -0
- data/app_generators/wakame/templates/cluster/resources/mysql_master/conf/my.cnf +154 -0
- data/app_generators/wakame/templates/cluster/resources/mysql_master/init.d/mysql +185 -0
- data/app_generators/wakame/templates/cluster/resources/mysql_master/mysql_master.rb +174 -0
- data/app_generators/wakame/templates/config/boot.rb +85 -0
- data/app_generators/wakame/templates/config/cluster.rb +64 -0
- data/app_generators/wakame/templates/config/environments/common.rb +0 -0
- data/app_generators/wakame/templates/config/environments/ec2.rb +3 -0
- data/app_generators/wakame/templates/config/environments/stand_alone.rb +0 -0
- data/app_generators/wakame/templates/config/init.d/wakame-agent +72 -0
- data/app_generators/wakame/templates/config/init.d/wakame-master +73 -0
- data/app_generators/wakame/wakame_generator.rb +124 -0
- data/bin/wakame +18 -0
- data/contrib/imagesetup.sh +77 -0
- data/lib/ext/eventmachine.rb +86 -0
- data/lib/ext/shellwords.rb +172 -0
- data/lib/ext/uri.rb +15 -0
- data/lib/wakame/action.rb +156 -0
- data/lib/wakame/actions/destroy_instances.rb +39 -0
- data/lib/wakame/actions/launch_cluster.rb +31 -0
- data/lib/wakame/actions/migrate_service.rb +65 -0
- data/lib/wakame/actions/propagate_instances.rb +95 -0
- data/lib/wakame/actions/reload_service.rb +21 -0
- data/lib/wakame/actions/scaleout_when_high_load.rb +44 -0
- data/lib/wakame/actions/shutdown_cluster.rb +22 -0
- data/lib/wakame/actions/shutdown_vm.rb +19 -0
- data/lib/wakame/actions/start_service.rb +64 -0
- data/lib/wakame/actions/stop_service.rb +49 -0
- data/lib/wakame/actions/util.rb +71 -0
- data/lib/wakame/actor/daemon.rb +37 -0
- data/lib/wakame/actor/service_monitor.rb +21 -0
- data/lib/wakame/actor/system.rb +46 -0
- data/lib/wakame/actor.rb +33 -0
- data/lib/wakame/agent.rb +226 -0
- data/lib/wakame/amqp_client.rb +219 -0
- data/lib/wakame/command/action_status.rb +62 -0
- data/lib/wakame/command/actor.rb +23 -0
- data/lib/wakame/command/clone_service.rb +12 -0
- data/lib/wakame/command/launch_cluster.rb +15 -0
- data/lib/wakame/command/migrate_service.rb +21 -0
- data/lib/wakame/command/propagate_service.rb +24 -0
- data/lib/wakame/command/shutdown_cluster.rb +15 -0
- data/lib/wakame/command/status.rb +81 -0
- data/lib/wakame/command.rb +31 -0
- data/lib/wakame/command_queue.rb +44 -0
- data/lib/wakame/configuration.rb +93 -0
- data/lib/wakame/daemonize.rb +96 -0
- data/lib/wakame/event.rb +232 -0
- data/lib/wakame/event_dispatcher.rb +154 -0
- data/lib/wakame/graph.rb +79 -0
- data/lib/wakame/initializer.rb +162 -0
- data/lib/wakame/instance_counter.rb +78 -0
- data/lib/wakame/logger.rb +12 -0
- data/lib/wakame/manager/commands.rb +134 -0
- data/lib/wakame/master.rb +369 -0
- data/lib/wakame/monitor/agent.rb +50 -0
- data/lib/wakame/monitor/service.rb +183 -0
- data/lib/wakame/monitor.rb +69 -0
- data/lib/wakame/packets.rb +160 -0
- data/lib/wakame/queue_declare.rb +14 -0
- data/lib/wakame/rule.rb +116 -0
- data/lib/wakame/rule_engine.rb +202 -0
- data/lib/wakame/runner/administrator_command.rb +112 -0
- data/lib/wakame/runner/agent.rb +81 -0
- data/lib/wakame/runner/master.rb +93 -0
- data/lib/wakame/scheduler.rb +251 -0
- data/lib/wakame/service.rb +914 -0
- data/lib/wakame/template.rb +189 -0
- data/lib/wakame/trigger.rb +66 -0
- data/lib/wakame/triggers/instance_count_update.rb +45 -0
- data/lib/wakame/triggers/load_history.rb +107 -0
- data/lib/wakame/triggers/maintain_ssh_known_hosts.rb +43 -0
- data/lib/wakame/triggers/process_command.rb +34 -0
- data/lib/wakame/triggers/shutdown_unused_vm.rb +16 -0
- data/lib/wakame/util.rb +569 -0
- data/lib/wakame/vm_manipulator.rb +186 -0
- data/lib/wakame.rb +59 -0
- data/tasks/ec2.rake +127 -0
- data/tests/cluster.json +3 -0
- data/tests/conf/a +1 -0
- data/tests/conf/b +1 -0
- data/tests/conf/c +1 -0
- data/tests/setup_agent.rb +39 -0
- data/tests/setup_master.rb +28 -0
- data/tests/test_actor.rb +54 -0
- data/tests/test_agent.rb +218 -0
- data/tests/test_amqp_client.rb +94 -0
- data/tests/test_graph.rb +36 -0
- data/tests/test_master.rb +167 -0
- data/tests/test_monitor.rb +47 -0
- data/tests/test_rule_engine.rb +127 -0
- data/tests/test_scheduler.rb +123 -0
- data/tests/test_service.rb +60 -0
- data/tests/test_template.rb +67 -0
- data/tests/test_uri_amqp.rb +19 -0
- data/tests/test_util.rb +71 -0
- data/wakame_generators/resource/resource_generator.rb +54 -0
- data/wakame_generators/resource/templates/apache_app/apache_app.rb +60 -0
- data/wakame_generators/resource/templates/apache_app/conf/apache2.conf +46 -0
- data/wakame_generators/resource/templates/apache_app/conf/envvars-app +7 -0
- data/wakame_generators/resource/templates/apache_app/conf/sites-app.conf +23 -0
- data/wakame_generators/resource/templates/apache_app/conf/system-app.conf +67 -0
- data/wakame_generators/resource/templates/apache_app/init.d/apache2-app +192 -0
- data/wakame_generators/resource/templates/apache_lb/apache_lb.rb +67 -0
- data/wakame_generators/resource/templates/apache_lb/conf/apache2.conf +46 -0
- data/wakame_generators/resource/templates/apache_lb/conf/envvars-lb +6 -0
- data/wakame_generators/resource/templates/apache_lb/conf/sites-lb.conf +54 -0
- data/wakame_generators/resource/templates/apache_lb/conf/system-lb.conf +75 -0
- data/wakame_generators/resource/templates/apache_lb/init.d/apache2-lb +192 -0
- data/wakame_generators/resource/templates/apache_www/apache_www.rb +56 -0
- data/wakame_generators/resource/templates/apache_www/conf/apache2.conf +47 -0
- data/wakame_generators/resource/templates/apache_www/conf/envvars-www +7 -0
- data/wakame_generators/resource/templates/apache_www/conf/sites-www.conf +23 -0
- data/wakame_generators/resource/templates/apache_www/conf/system-www.conf +63 -0
- data/wakame_generators/resource/templates/apache_www/init.d/apache2-www +192 -0
- data/wakame_generators/resource/templates/ec2_elastic_ip/ec2_elastic_ip.rb +39 -0
- data/wakame_generators/resource/templates/mysql_master/conf/my.cnf +154 -0
- data/wakame_generators/resource/templates/mysql_master/init.d/mysql +185 -0
- data/wakame_generators/resource/templates/mysql_master/mysql_master.rb +119 -0
- metadata +289 -0
@@ -0,0 +1,186 @@
|
|
1
|
+
|
2
|
+
require 'wakame'
|
3
|
+
|
4
|
+
module Wakame
|
5
|
+
module VmManipulator
|
6
|
+
def self.create
|
7
|
+
Wakame.new_([self.to_s, Wakame.config.vm_environment ].join('::'))
|
8
|
+
end
|
9
|
+
|
10
|
+
class Base
|
11
|
+
def start_instance(attr)
|
12
|
+
end
|
13
|
+
|
14
|
+
def stop_instance(instance_id)
|
15
|
+
end
|
16
|
+
|
17
|
+
# expected_status : :online, :offline, :failed
|
18
|
+
def check_status(instance_id, expected_status=:online)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Expected common keys/attributes when returned.
|
22
|
+
# local_ipv4 : Internal IP address which is assigned to the VM instance.
|
23
|
+
# local_hostname : Internal hostname assigned to the VM instance.
|
24
|
+
def fetch_local_attrs
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class StandAlone < Base
|
30
|
+
INSTANCE_ID='__standalone__'
|
31
|
+
|
32
|
+
def start_instance(attr)
|
33
|
+
# Nothing to be done
|
34
|
+
{:instance_id =>INSTANCE_ID}
|
35
|
+
end
|
36
|
+
|
37
|
+
def stop_instance(instance_id)
|
38
|
+
# Nothing to be done
|
39
|
+
end
|
40
|
+
|
41
|
+
def check_status(instance_id, expected_status=:online)
|
42
|
+
# Always running
|
43
|
+
expected_status == :online
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.fetch_local_attrs
|
47
|
+
attrs = {:instance_id=>INSTANCE_ID, :local_ipv4=>'127.0.0.1', :local_hostname=>'localhost', :ami_id=>'localimage'}
|
48
|
+
attrs
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class EC2 < Base
|
53
|
+
|
54
|
+
require 'EC2'
|
55
|
+
|
56
|
+
def initialize()
|
57
|
+
@ec2 = ::EC2::Base.new(:access_key_id => Wakame.config.aws_access_key, :secret_access_key => Wakame.config.aws_secret_key )
|
58
|
+
end
|
59
|
+
|
60
|
+
def start_instance(image_id, attr={})
|
61
|
+
res = @ec2.run_instances(:image_id => image_id,
|
62
|
+
:availability_zone => attr[:availability_zone],
|
63
|
+
:group_id => attr[:security_groups],
|
64
|
+
:instance_type => attr[:instance_type],
|
65
|
+
:user_data => attr[:user_data]
|
66
|
+
)
|
67
|
+
{:instance_id => res.instancesSet.item[0].instanceId}
|
68
|
+
end
|
69
|
+
|
70
|
+
def stop_instance(instance_id)
|
71
|
+
@ec2.terminate_instances(:instance_id=>instance_id)
|
72
|
+
end
|
73
|
+
|
74
|
+
def check_status(instance_id, expected_status=:online)
|
75
|
+
res = @ec2.describe_instances(:instance_id => instance_id)
|
76
|
+
|
77
|
+
status = res.reservationSet.item[0].instancesSet.item[0].instanceState
|
78
|
+
# status is returned in a hash structure. i.e. {'name'=>'running', 'code'=>'16' }
|
79
|
+
Wakame.log.debug("VM (#{instance_id}) status: #{status['name']}")
|
80
|
+
return case status['name']
|
81
|
+
when "running"
|
82
|
+
expected_status == :online
|
83
|
+
when "terminated"
|
84
|
+
expected_status == :offline
|
85
|
+
when "rebooting"
|
86
|
+
expected_status == :offline
|
87
|
+
when "starting"
|
88
|
+
expected_status == :offline
|
89
|
+
when "pending"
|
90
|
+
expected_status == :offline
|
91
|
+
else
|
92
|
+
raise "Unknown status from AWS: #{status['name']}"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def associate_address(instance_id, ip_addr)
|
97
|
+
res = @ec2.associate_address(:instance_id=>instance_id, :public_ip=>ip_addr)
|
98
|
+
# {"requestId"=>"000ac66b-4a9c-43be-8176-b1b96ed6d4b7", "return"=>"true", "xmlns"=>"http://ec2.amazonaws.com/doc/2008-12-01/"}
|
99
|
+
res['return'] == 'true'
|
100
|
+
end
|
101
|
+
def disassociate_address(ip_addr)
|
102
|
+
res = @ec2.disassociate_address(:public_ip=>ip_addr)
|
103
|
+
# {"requestId"=>"2f38c8bb-4b1a-4df3-9f30-fa17317c89c4", "return"=>"true", "xmlns"=>"http://ec2.amazonaws.com/doc/2008-12-01/"}
|
104
|
+
res['return'] == 'true'
|
105
|
+
end
|
106
|
+
|
107
|
+
# volume
|
108
|
+
def describe_volume(vol_id)
|
109
|
+
res = @ec2.describe_volumes(:volume_id=>vol_id)
|
110
|
+
if res['volumeSet']['item'][0]['attachmentSet']
|
111
|
+
res['volumeSet']['item'][0]['attachmentSet']['item'][0]
|
112
|
+
else
|
113
|
+
res['volumeSet']['item'][0]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
# >> @ec2.attach_volume(:instance_id => 'i-1fa1cd76', :volume_id => "vol-1f927176", :device=>'/dev/sde')
|
117
|
+
# => {"attachTime"=>"2009-04-17T05:46:18.000Z", "status"=>"attaching", "device"=>"/dev/sde", "requestId"=>"0fd3797b-b4f9-476b-8cb2-3e7401c6fae2", "instanceId"=>"i-1fa1cd76", "volumeId"=>"vol-1f927176", "xmlns"=>"http://ec2.amazonaws.com/doc/2008-12-01/"}
|
118
|
+
def attach_volume(instance_id, vol_id, vol_dev)
|
119
|
+
res = @ec2.attach_volume(:instance_id=>instance_id, :volume_id=>vol_id, :device=>vol_dev)
|
120
|
+
end
|
121
|
+
def detach_volume(vol_id)
|
122
|
+
res = @ec2.detach_volume(:volume_id=>vol_id)
|
123
|
+
end
|
124
|
+
|
125
|
+
# volume
|
126
|
+
# >> @ec2.describe_volumes(:volume_id => "vol-c58360ac")
|
127
|
+
# => {"volumeSet"=>{"item"=>[{"status"=>"available", "size"=>"1", "snapshotId"=>nil, "availabilityZone"=>"us-east-1a", "attachmentSet"=>nil, "createTime"=>"2009-04-16T09:56:01.000Z", "volumeId"=>"vol-c58360ac"}]}, "requestId"=>"0e6d0923-eba8-425a-b939-87c7fe8e835e", "xmlns"=>"http://ec2.amazonaws.com/doc/2008-12-01/"}
|
128
|
+
def describe_volume(vol_id)
|
129
|
+
res = @ec2.describe_volumes(:volume_id=>vol_id)
|
130
|
+
res['volumeSet']['item'][0]
|
131
|
+
end
|
132
|
+
def create_volume(availability_zone, size)
|
133
|
+
res = @ec2.create_volume(:availability_zone=>availability_zone, :size=>size)
|
134
|
+
end
|
135
|
+
# >> @ec2.create_volume(:availability_zone=>"us-east-1b", :snapshot_id=>"snap-27c1324e")
|
136
|
+
# => {"status"=>"creating", "size"=>"1", "snapshotId"=>"snap-27c1324e", "requestId"=>"f3a0ddbf-9eb8-4594-b43e-8486459a0168", "availabilityZone"=>"us-east-1b", "createTime"=>"2009-04-17T05:44:58.000Z", "volumeId"=>"vol-1f927176", "xmlns"=>"http://ec2.amazonaws.com/doc/2008-12-01/"}
|
137
|
+
def create_volume_from_snapshot(availability_zone, snapshot_id)
|
138
|
+
res = @ec2.create_volume(:availability_zone=>availability_zone, :snapshot_id=>snapshot_id)
|
139
|
+
end
|
140
|
+
def delete_volume(vol_id)
|
141
|
+
res = @ec2.delete_volume(:volume_id=>vol_id)
|
142
|
+
end
|
143
|
+
|
144
|
+
# snapshot
|
145
|
+
def describe_snapshot(snapshot_id)
|
146
|
+
res = @ec2.describe_snapshots(:snapshot_id=>snapshot_id)
|
147
|
+
res['snapshotSet']['item'][0]
|
148
|
+
end
|
149
|
+
|
150
|
+
# >> @ec2.create_snapshot(:volume_id => 'vol-c58360ac')
|
151
|
+
# => {"status"=>"pending", "snapshotId"=>"snap-18c13271", "requestId"=>"9d1d586a-44b7-4edd-b94a-aaccb54e888d", "progress"=>nil, "startTime"=>"2009-04-16T10:13:37.000Z", "volumeId"=>"vol-c58360ac", "xmlns"=>"http://ec2.amazonaws.com/doc/2008-12-01/"}
|
152
|
+
def create_snapshot(vol_id)
|
153
|
+
res = @ec2.create_snapshot(:volume_id=>vol_id)
|
154
|
+
end
|
155
|
+
def delete_snapshot(snapshot_id)
|
156
|
+
res = @ec2.delete_snapshot(:snapshot_id=>snapshot_id)
|
157
|
+
end
|
158
|
+
|
159
|
+
module MetadataService
|
160
|
+
def query_metadata_uri(key)
|
161
|
+
require 'open-uri'
|
162
|
+
open("http://169.254.169.254/2008-02-01/meta-data/#{key}") { |f|
|
163
|
+
return f.readline
|
164
|
+
}
|
165
|
+
end
|
166
|
+
module_function :query_metadata_uri
|
167
|
+
public :query_metadata_uri
|
168
|
+
|
169
|
+
def fetch_local_attrs
|
170
|
+
attrs = {}
|
171
|
+
%w[instance-id instance-type local-ipv4 local-hostname public-hostname public-ipv4 ami-id].each { |key|
|
172
|
+
rkey = key.tr('-', '_')
|
173
|
+
attrs[rkey.to_sym]=query_metadata_uri(key)
|
174
|
+
}
|
175
|
+
attrs
|
176
|
+
end
|
177
|
+
module_function :fetch_local_attrs
|
178
|
+
public :fetch_local_attrs
|
179
|
+
|
180
|
+
end
|
181
|
+
|
182
|
+
include MetadataService
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
186
|
+
end
|
data/lib/wakame.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
# For debugging
|
5
|
+
Thread.abort_on_exception = true
|
6
|
+
|
7
|
+
module Wakame
|
8
|
+
require 'jeweler/version_helper'
|
9
|
+
VERSION=Jeweler::VersionHelper.new(File.expand_path('../', File.dirname(__FILE__))).to_s
|
10
|
+
|
11
|
+
autoload :Agent, 'wakame/agent'
|
12
|
+
autoload :Master, 'wakame/master'
|
13
|
+
autoload :Daemonize, 'wakame/daemonize'
|
14
|
+
autoload :Util, 'wakame/util'
|
15
|
+
autoload :Event, 'wakame/event'
|
16
|
+
autoload :Service, 'wakame/service'
|
17
|
+
autoload :Rule, 'wakame/rule'
|
18
|
+
autoload :Graph, 'wakame/graph'
|
19
|
+
autoload :Monitor, 'wakame/monitor'
|
20
|
+
autoload :Actor, 'wakame/actor'
|
21
|
+
autoload :Configuration, 'wakame/configuration'
|
22
|
+
autoload :Logger, 'wakame/logger'
|
23
|
+
autoload :Packets, 'wakame/packets'
|
24
|
+
autoload :AMQPClient, 'wakame/amqp_client'
|
25
|
+
autoload :EventDispatcher, 'wakame/event_dispatcher'
|
26
|
+
autoload :Scheduler, 'wakame/scheduler'
|
27
|
+
autoload :Command, 'wakame/command'
|
28
|
+
autoload :CommandQueue, 'wakame/command_queue'
|
29
|
+
autoload :Template, 'wakame/template'
|
30
|
+
autoload :Trigger, 'wakame/trigger'
|
31
|
+
autoload :Action, 'wakame/action'
|
32
|
+
autoload :RuleEngine, 'wakame/rule_engine'
|
33
|
+
#autoload :Initializer, 'wakame/initializer' # Do not autoload this class since the constant is used for the flag in bootstrap.
|
34
|
+
|
35
|
+
def gen_id(str=nil)
|
36
|
+
Util.gen_id(str)
|
37
|
+
end
|
38
|
+
|
39
|
+
def log
|
40
|
+
Logger.log
|
41
|
+
end
|
42
|
+
|
43
|
+
module_function :gen_id, :log
|
44
|
+
|
45
|
+
class << self
|
46
|
+
def config
|
47
|
+
Initializer.instance.configuration
|
48
|
+
end
|
49
|
+
|
50
|
+
def environment
|
51
|
+
config.environment
|
52
|
+
end
|
53
|
+
|
54
|
+
def new_(class_or_str)
|
55
|
+
Util.new_(class_or_str)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/tasks/ec2.rake
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
#require 'wakame'
|
4
|
+
require 'timeout'
|
5
|
+
|
6
|
+
#AWS_ACCESS_KEY=ENV['AMAZON_ACCESS_KEY_ID'] || Wakame.config.aws_access_key
|
7
|
+
#AWS_SECRET_KEY=ENV['AMAZON_SECRET_ACCESS_KEY'] || Wakame.config.aws_secret_key
|
8
|
+
AWS_ACCESS_KEY=ENV['AMAZON_ACCESS_KEY_ID']
|
9
|
+
AWS_SECRET_KEY=ENV['AMAZON_SECRET_ACCESS_KEY']
|
10
|
+
|
11
|
+
def create_ec2
|
12
|
+
require 'EC2'
|
13
|
+
ec2 = EC2::Base.new(:access_key_id =>AWS_ACCESS_KEY, :secret_access_key =>AWS_SECRET_KEY)
|
14
|
+
end
|
15
|
+
|
16
|
+
def request_metadata_url(key)
|
17
|
+
require 'open-uri'
|
18
|
+
open("http://169.254.169.254/2008-09-01/meta-data/#{key}") { |f|
|
19
|
+
return f.readline
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
namespace :ec2 do
|
24
|
+
desc "Automate the EC2 image bundle procedure.(ec2-bundle-vol + ec2-upload-bundle + ec2-register)"
|
25
|
+
task :bundle, :manifest_path do |t, args|
|
26
|
+
raise 'This task requires root privilege.' unless Process.uid == 0
|
27
|
+
raise 'Required key files counld not be detected: /mnt/cert.pem or /mnt/pk.pem' unless File.exist?('/mnt/cert.pem') && File.exist?('/mnt/pk.pem')
|
28
|
+
|
29
|
+
#sh("/etc/init.d/rabbitmq-server stop") rescue puts $!
|
30
|
+
|
31
|
+
bundle_tmpdir='/mnt/wakame-bundle'
|
32
|
+
# If the arg was not set, it tries to overwrite the running image.
|
33
|
+
manifest_path= args.manifest_path || request_metadata_url('ami-manifest-path')
|
34
|
+
|
35
|
+
#manifest_path.sub!(/.manifest.xml\Z/, '')
|
36
|
+
if manifest_path =~ %r{\A([^/]+)/(.+)\.manifest\.xml\Z}
|
37
|
+
#s3bucket = manifest_path[0, manifest_path.index('/') - 1]
|
38
|
+
#manifest_prefix = manifest_path[manifest_path.index('/')]
|
39
|
+
s3bucket = $1
|
40
|
+
manifest_basename = File.basename($2)
|
41
|
+
manifest_path = "#{s3bucket}/#{manifest_basename}.manifest.xml"
|
42
|
+
#puts "#{manifest_path}"
|
43
|
+
else
|
44
|
+
fail "Given manifest path is not valid: #{manifest_path}"
|
45
|
+
end
|
46
|
+
|
47
|
+
puts "Manifest Path: #{manifest_path}"
|
48
|
+
|
49
|
+
ec2 = create_ec2()
|
50
|
+
|
51
|
+
ami_id = request_metadata_url('ami-id')
|
52
|
+
|
53
|
+
instance_id = request_metadata_url('instance-id')
|
54
|
+
res = ec2.describe_instances(:instance_id=>instance_id)
|
55
|
+
account_no = res['reservationSet']['item'][0]['ownerId']
|
56
|
+
|
57
|
+
res = ec2.describe_images(:image_id=>ami_id)
|
58
|
+
arch = res['imagesSet']['item'][0]['architecture']
|
59
|
+
|
60
|
+
begin
|
61
|
+
FileUtils.rm_rf(bundle_tmpdir) if File.exist?(bundle_tmpdir)
|
62
|
+
FileUtils.mkpath(bundle_tmpdir)
|
63
|
+
|
64
|
+
sh("ec2-bundle-vol --batch -d '#{bundle_tmpdir}' -p '#{manifest_basename}' -c /mnt/cert.pem -k /mnt/pk.pem -u '#{account_no}' -r '#{arch}'")
|
65
|
+
sh("ec2-upload-bundle -d '#{bundle_tmpdir}' -b '#{s3bucket}' -m '#{File.join(bundle_tmpdir, manifest_basename + '.manifest.xml')}' -a '#{AWS_ACCESS_KEY}' -s '#{AWS_SECRET_KEY}'")
|
66
|
+
res = ec2.register_image(:image_location=>manifest_path)
|
67
|
+
puts "New AMI ID for #{manifest_path}: #{res['imageId']}"
|
68
|
+
ensure
|
69
|
+
FileUtils.rm_rf(bundle_tmpdir) if File.exist?(bundle_tmpdir)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
desc "Initiate the mysql master volume using Amazon EBS"
|
75
|
+
task :mysqlsetupvol, :size do |t, args|
|
76
|
+
raise 'This task requires root privilege.' unless Process.uid == 0
|
77
|
+
|
78
|
+
ATTACH_DEV='/dev/sdw'
|
79
|
+
TMP_MNT='/mnt/mysql-tmp'
|
80
|
+
vol_size = (args.size || '1').to_s
|
81
|
+
zone = request_metadata_url('placement/availability-zone')
|
82
|
+
instance_id = request_metadata_url('instance-id')
|
83
|
+
|
84
|
+
ec2 = create_ec2
|
85
|
+
res = ec2.create_volume(:size=>vol_size, :availability_zone=>zone)
|
86
|
+
vol_id = res['volumeId']
|
87
|
+
|
88
|
+
timeout(10) {
|
89
|
+
begin
|
90
|
+
res = ec2.describe_volumes(:volume_id=>vol_id)
|
91
|
+
next if res['volumeSet']['item'][0]['status'] == 'available'
|
92
|
+
sleep 0.5
|
93
|
+
retry
|
94
|
+
end
|
95
|
+
}
|
96
|
+
|
97
|
+
begin
|
98
|
+
res = ec2.attach_volume(:instance_id=>instance_id, :volume_id=>vol_id, :device=>ATTACH_DEV)
|
99
|
+
|
100
|
+
timeout(10) {
|
101
|
+
begin
|
102
|
+
res = ec2.describe_volumes(:volume_id=>vol_id)
|
103
|
+
next if res['volumeSet']['item'][0]['status'] == 'in-use' && File.blockdev?(ATTACH_DEV)
|
104
|
+
sleep 0.5
|
105
|
+
retry
|
106
|
+
end
|
107
|
+
}
|
108
|
+
|
109
|
+
sh("echo 'y' | mkfs.ext3 -q #{ATTACH_DEV}")
|
110
|
+
|
111
|
+
begin
|
112
|
+
FileUtils.mkpath(TMP_MNT) unless File.exist?(TMP_MNT)
|
113
|
+
sh("mount #{ATTACH_DEV} #{TMP_MNT}")
|
114
|
+
sleep 1.0
|
115
|
+
sh("/usr/bin/mysql_install_db --datadir=#{TMP_MNT}")
|
116
|
+
ensure
|
117
|
+
sh("umount #{TMP_MNT}")
|
118
|
+
sleep 1.0
|
119
|
+
end
|
120
|
+
|
121
|
+
ensure
|
122
|
+
ec2.detach_volume(:volume_id=>vol_id, :instance_id=>instance_id) rescue puts $!
|
123
|
+
end
|
124
|
+
|
125
|
+
puts "Initialized EBS Volume: #{vol_id} "
|
126
|
+
end
|
127
|
+
end
|
data/tests/cluster.json
ADDED
data/tests/conf/a
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
aaaaaa
|
data/tests/conf/b
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
bbbbbbb
|
data/tests/conf/c
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ccccccc
|
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
3
|
+
|
4
|
+
WAKAME_ROOT="#{File.dirname(__FILE__)}/.."
|
5
|
+
WAKAME_FRAMEWORK_ROOT="#{File.dirname(__FILE__)}/.."
|
6
|
+
WAKAME_ENV=:StandAlone
|
7
|
+
|
8
|
+
require 'rubygems' rescue nil
|
9
|
+
|
10
|
+
require 'test/unit'
|
11
|
+
|
12
|
+
require 'wakame'
|
13
|
+
require 'wakame/initializer'
|
14
|
+
|
15
|
+
#require "#{WAKAME_ROOT}/config/boot"
|
16
|
+
#Wakame::Bootstrap.boot_agent!
|
17
|
+
|
18
|
+
Wakame::Initializer.run(:process_agent)
|
19
|
+
|
20
|
+
|
21
|
+
class DummyAgent
|
22
|
+
attr_reader :publish_count
|
23
|
+
|
24
|
+
attr_accessor :actor_registry, :monitor_registry
|
25
|
+
|
26
|
+
def initialize(&blk)
|
27
|
+
blk.call(self) if blk
|
28
|
+
@publish_count = 0
|
29
|
+
end
|
30
|
+
|
31
|
+
def agent_id
|
32
|
+
'test_id'
|
33
|
+
end
|
34
|
+
|
35
|
+
def publish_to(*args)
|
36
|
+
@publish_count += 1
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
3
|
+
|
4
|
+
WAKAME_ROOT="#{File.dirname(__FILE__)}/.."
|
5
|
+
WAKAME_FRAMEWORK_ROOT="#{File.dirname(__FILE__)}/.."
|
6
|
+
WAKAME_ENV=:StandAlone
|
7
|
+
|
8
|
+
require 'rubygems' rescue nil
|
9
|
+
|
10
|
+
require 'test/unit'
|
11
|
+
|
12
|
+
require 'wakame'
|
13
|
+
require 'wakame/initializer'
|
14
|
+
|
15
|
+
require 'ext/eventmachine'
|
16
|
+
|
17
|
+
#require "#{WAKAME_ROOT}/config/boot"
|
18
|
+
#Wakame::Bootstrap.boot_agent!
|
19
|
+
|
20
|
+
#Wakame::Initializer.run(:process_master)
|
21
|
+
Wakame::Initializer.run(:setup_load_paths)
|
22
|
+
Wakame::Initializer.run(:setup_logger)
|
23
|
+
|
24
|
+
|
25
|
+
class MockCluster < Wakame::Service::ServiceCluster
|
26
|
+
end
|
27
|
+
|
28
|
+
Wakame.config.cluster_class = 'MockCluster'
|
data/tests/test_actor.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
|
2
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
3
|
+
$:.unshift(File.dirname(__FILE__))
|
4
|
+
|
5
|
+
require 'setup_agent.rb'
|
6
|
+
|
7
|
+
require 'rubygems'
|
8
|
+
|
9
|
+
require 'test/unit'
|
10
|
+
|
11
|
+
require 'wakame'
|
12
|
+
require 'wakame/packets'
|
13
|
+
require 'wakame/agent'
|
14
|
+
require 'wakame/actor'
|
15
|
+
require 'wakame/actor/service_monitor'
|
16
|
+
|
17
|
+
class TestActor < Test::Unit::TestCase
|
18
|
+
include Wakame
|
19
|
+
|
20
|
+
|
21
|
+
def test_actor_registry
|
22
|
+
reg = ActorRegistry.new
|
23
|
+
reg.register(Actor::ServiceMonitor.new)
|
24
|
+
assert(reg.actors.keys.include?('/wakame/actor/service_monitor'))
|
25
|
+
reg.unregister('/wakame/actor/service_monitor')
|
26
|
+
assert(reg.actors.size == 0)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_service_monitor
|
30
|
+
require 'wakame/monitor/service'
|
31
|
+
|
32
|
+
EM.run {
|
33
|
+
agent = DummyAgent.new { |me|
|
34
|
+
me.actor_registry = ActorRegistry.new
|
35
|
+
me.monitor_registry = MonitorRegistry.new
|
36
|
+
mon = Wakame::Monitor::Service.new
|
37
|
+
mon.agent = me
|
38
|
+
me.monitor_registry.register(mon, '/service')
|
39
|
+
}
|
40
|
+
|
41
|
+
d = Dispatcher.new(agent)
|
42
|
+
svcmon = Actor::ServiceMonitor.new
|
43
|
+
svcmon.agent = agent
|
44
|
+
|
45
|
+
agent.actor_registry.register(svcmon, '/service_monitor')
|
46
|
+
d.handle_request({:path=>'/service_monitor/register', :args=>['aaaa', 'ls -l /tmp']})
|
47
|
+
EM.next_tick {
|
48
|
+
assert(agent.monitor_registry.find_monitor('/service').checkers.keys.include?('aaaa'))
|
49
|
+
EM.stop
|
50
|
+
}
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|