wr0ngway-rubber 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +34 -0
- data/COPYING +339 -0
- data/README +6 -0
- data/TODO +7 -0
- data/VERSION +1 -0
- data/bin/vulcanize +41 -0
- data/generators/vulcanize/USAGE +6 -0
- data/generators/vulcanize/templates/apache/config/rubber/deploy-apache.rb +51 -0
- data/generators/vulcanize/templates/apache/config/rubber/role/apache/deflate.conf +10 -0
- data/generators/vulcanize/templates/apache/config/rubber/role/apache/expires.conf +9 -0
- data/generators/vulcanize/templates/apache/config/rubber/role/apache/headers.conf +6 -0
- data/generators/vulcanize/templates/apache/config/rubber/role/apache/monit-apache.conf +8 -0
- data/generators/vulcanize/templates/apache/config/rubber/role/apache/ports.conf +5 -0
- data/generators/vulcanize/templates/apache/config/rubber/role/apache/setenvif.conf +52 -0
- data/generators/vulcanize/templates/apache/config/rubber/role/web_tools/tools-apache-vhost.conf +62 -0
- data/generators/vulcanize/templates/apache/config/rubber/role/web_tools/tools-apache.auth +7 -0
- data/generators/vulcanize/templates/apache/config/rubber/role/web_tools/tools-index.html +30 -0
- data/generators/vulcanize/templates/apache/config/rubber/rubber-apache.yml +7 -0
- data/generators/vulcanize/templates/apache/templates.yml +1 -0
- data/generators/vulcanize/templates/base/Capfile +14 -0
- data/generators/vulcanize/templates/base/config/deploy.rb +55 -0
- data/generators/vulcanize/templates/base/config/rubber/common/crontab +16 -0
- data/generators/vulcanize/templates/base/config/rubber/common/profile.rc +9 -0
- data/generators/vulcanize/templates/base/config/rubber/deploy-setup.rb +104 -0
- data/generators/vulcanize/templates/base/config/rubber/rubber.yml +241 -0
- data/generators/vulcanize/templates/base/lib/tasks/rubber.rake +15 -0
- data/generators/vulcanize/templates/base/script/cron-rake +18 -0
- data/generators/vulcanize/templates/base/script/cron-runner +18 -0
- data/generators/vulcanize/templates/base/script/cron-sh +67 -0
- data/generators/vulcanize/templates/base/templates.yml +1 -0
- data/generators/vulcanize/templates/complete_mongrel_mysql/config/rubber/role/haproxy/haproxy-mongrel.conf +23 -0
- data/generators/vulcanize/templates/complete_mongrel_mysql/config/rubber/role/nginx/nginx-mongrel.conf +113 -0
- data/generators/vulcanize/templates/complete_mongrel_mysql/config/rubber/rubber-complete.yml +41 -0
- data/generators/vulcanize/templates/complete_mongrel_mysql/templates.yml +6 -0
- data/generators/vulcanize/templates/complete_passenger_mysql/config/rubber/role/haproxy/haproxy-passenger.conf +19 -0
- data/generators/vulcanize/templates/complete_passenger_mysql/config/rubber/rubber-complete.yml +40 -0
- data/generators/vulcanize/templates/complete_passenger_mysql/templates.yml +10 -0
- data/generators/vulcanize/templates/cruise/config/rubber/deploy-cruise.rb +72 -0
- data/generators/vulcanize/templates/cruise/config/rubber/role/cruise/cruise +40 -0
- data/generators/vulcanize/templates/cruise/config/rubber/role/cruise/my.cnf +165 -0
- data/generators/vulcanize/templates/cruise/config/rubber/role/cruise/production.rb +8 -0
- data/generators/vulcanize/templates/cruise/config/rubber/role/cruise/site_config.rb +76 -0
- data/generators/vulcanize/templates/cruise/config/rubber/role/web_tools/cruise-nginx.conf +11 -0
- data/generators/vulcanize/templates/cruise/config/rubber/rubber-cruise.yml +18 -0
- data/generators/vulcanize/templates/cruise/templates.yml +1 -0
- data/generators/vulcanize/templates/haproxy/config/rubber/deploy-haproxy.rb +45 -0
- data/generators/vulcanize/templates/haproxy/config/rubber/role/haproxy/haproxy-base.conf +26 -0
- data/generators/vulcanize/templates/haproxy/config/rubber/role/haproxy/haproxy-default.conf +8 -0
- data/generators/vulcanize/templates/haproxy/config/rubber/role/haproxy/monit-haproxy.conf +9 -0
- data/generators/vulcanize/templates/haproxy/config/rubber/role/haproxy/syslog-haproxy.conf +6 -0
- data/generators/vulcanize/templates/haproxy/config/rubber/role/haproxy/syslogd-default.conf +17 -0
- data/generators/vulcanize/templates/haproxy/config/rubber/role/web_tools/haproxy-nginx.conf +10 -0
- data/generators/vulcanize/templates/haproxy/config/rubber/rubber-haproxy.yml +7 -0
- data/generators/vulcanize/templates/haproxy/templates.yml +1 -0
- data/generators/vulcanize/templates/memcached/config/memcached.yml +28 -0
- data/generators/vulcanize/templates/memcached/config/rubber/common/memcached.yml +14 -0
- data/generators/vulcanize/templates/memcached/config/rubber/role/memcached/memcached.conf +52 -0
- data/generators/vulcanize/templates/memcached/config/rubber/role/memcached/memcached_munin_plugin +249 -0
- data/generators/vulcanize/templates/memcached/config/rubber/rubber-memcached.yml +7 -0
- data/generators/vulcanize/templates/memcached/templates.yml +1 -0
- data/generators/vulcanize/templates/minimal_mysql/templates.yml +7 -0
- data/generators/vulcanize/templates/minimal_nodb/templates.yml +6 -0
- data/generators/vulcanize/templates/mongrel/config/rubber/deploy-mongrel.rb +75 -0
- data/generators/vulcanize/templates/mongrel/config/rubber/role/mongrel/mongrel_cluster.yml +12 -0
- data/generators/vulcanize/templates/mongrel/config/rubber/role/mongrel/monit-mongrel.conf +20 -0
- data/generators/vulcanize/templates/mongrel/config/rubber/rubber-mongrel.yml +9 -0
- data/generators/vulcanize/templates/mongrel/templates.yml +1 -0
- data/generators/vulcanize/templates/monit/config/rubber/common/monit-default.conf +15 -0
- data/generators/vulcanize/templates/monit/config/rubber/common/monit.conf +251 -0
- data/generators/vulcanize/templates/monit/config/rubber/deploy-monit.rb +32 -0
- data/generators/vulcanize/templates/monit/config/rubber/role/web_tools/monit-admin-nginx.conf +10 -0
- data/generators/vulcanize/templates/monit/config/rubber/rubber-monit.yml +6 -0
- data/generators/vulcanize/templates/monit/templates.yml +1 -0
- data/generators/vulcanize/templates/munin/config/rubber/common/monit-munin.conf +8 -0
- data/generators/vulcanize/templates/munin/config/rubber/common/munin-node.conf +48 -0
- data/generators/vulcanize/templates/munin/config/rubber/deploy-munin.rb +46 -0
- data/generators/vulcanize/templates/munin/config/rubber/role/web_tools/munin-nginx.conf +8 -0
- data/generators/vulcanize/templates/munin/config/rubber/role/web_tools/munin-plugins.conf +31 -0
- data/generators/vulcanize/templates/munin/config/rubber/role/web_tools/munin.conf +80 -0
- data/generators/vulcanize/templates/munin/config/rubber/rubber-munin.yml +8 -0
- data/generators/vulcanize/templates/munin/script/munin/example_mysql_query.rb +57 -0
- data/generators/vulcanize/templates/munin/script/munin/example_simple.rb +24 -0
- data/generators/vulcanize/templates/munin/templates.yml +1 -0
- data/generators/vulcanize/templates/mysql/config/rubber/common/database.yml +11 -0
- data/generators/vulcanize/templates/mysql/config/rubber/deploy-mysql.rb +156 -0
- data/generators/vulcanize/templates/mysql/config/rubber/role/db/crontab +14 -0
- data/generators/vulcanize/templates/mysql/config/rubber/role/db/monit-mysql.cnf +10 -0
- data/generators/vulcanize/templates/mysql/config/rubber/role/db/my.cnf +167 -0
- data/generators/vulcanize/templates/mysql/config/rubber/role/mysql_slave/mysql_slave_munin_plugin +51 -0
- data/generators/vulcanize/templates/mysql/config/rubber/rubber-mysql.yml +46 -0
- data/generators/vulcanize/templates/mysql/templates.yml +1 -0
- data/generators/vulcanize/templates/mysql_cluster/config/rubber/common/mysql_cluster_migrations.rb +13 -0
- data/generators/vulcanize/templates/mysql_cluster/config/rubber/deploy-mysql_cluster.rb +173 -0
- data/generators/vulcanize/templates/mysql_cluster/config/rubber/role/mysql_data/my.cnf +15 -0
- data/generators/vulcanize/templates/mysql_cluster/config/rubber/role/mysql_mgm/ndb_mgmd.cnf +39 -0
- data/generators/vulcanize/templates/mysql_cluster/config/rubber/role/mysql_sql/monit-mysql_cluster_sql.cnf +10 -0
- data/generators/vulcanize/templates/mysql_cluster/config/rubber/role/mysql_sql/my.cnf +23 -0
- data/generators/vulcanize/templates/mysql_cluster/config/rubber/rubber-mysql_cluster.yml +32 -0
- data/generators/vulcanize/templates/mysql_cluster/templates.yml +1 -0
- data/generators/vulcanize/templates/mysql_proxy/config/rubber/common/database.yml +16 -0
- data/generators/vulcanize/templates/mysql_proxy/config/rubber/common/monit-mysql_proxy.cnf +10 -0
- data/generators/vulcanize/templates/mysql_proxy/config/rubber/common/mysql-proxy +153 -0
- data/generators/vulcanize/templates/mysql_proxy/config/rubber/common/mysql-proxy.conf +10 -0
- data/generators/vulcanize/templates/mysql_proxy/config/rubber/common/mysql-proxy.lua +5 -0
- data/generators/vulcanize/templates/mysql_proxy/config/rubber/deploy-mysql_proxy.rb +52 -0
- data/generators/vulcanize/templates/mysql_proxy/config/rubber/rubber-mysql_proxy.yml +11 -0
- data/generators/vulcanize/templates/mysql_proxy/templates.yml +1 -0
- data/generators/vulcanize/templates/nginx/config/rubber/deploy-nginx.rb +45 -0
- data/generators/vulcanize/templates/nginx/config/rubber/role/nginx/crontab +9 -0
- data/generators/vulcanize/templates/nginx/config/rubber/role/nginx/monit-nginx.conf +8 -0
- data/generators/vulcanize/templates/nginx/config/rubber/role/nginx/nginx.conf +42 -0
- data/generators/vulcanize/templates/nginx/config/rubber/role/web_tools/nginx-tools.conf +55 -0
- data/generators/vulcanize/templates/nginx/config/rubber/role/web_tools/tools-index.html +30 -0
- data/generators/vulcanize/templates/nginx/config/rubber/role/web_tools/tools-nginx.auth +7 -0
- data/generators/vulcanize/templates/nginx/config/rubber/rubber-nginx.yml +10 -0
- data/generators/vulcanize/templates/nginx/templates.yml +1 -0
- data/generators/vulcanize/templates/passenger/config/rubber/deploy-passenger.rb +37 -0
- data/generators/vulcanize/templates/passenger/config/rubber/role/passenger/munin-passenger-sudoers.conf +6 -0
- data/generators/vulcanize/templates/passenger/config/rubber/role/passenger/munin-passenger.conf +47 -0
- data/generators/vulcanize/templates/passenger/config/rubber/role/passenger/passenger-apache-vhost.conf +46 -0
- data/generators/vulcanize/templates/passenger/config/rubber/role/passenger/passenger.conf +10 -0
- data/generators/vulcanize/templates/passenger/config/rubber/rubber-passenger.yml +12 -0
- data/generators/vulcanize/templates/passenger/templates.yml +1 -0
- data/generators/vulcanize/templates/sphinx/config/rubber/common/sphinx.yml +46 -0
- data/generators/vulcanize/templates/sphinx/config/rubber/deploy-sphinx.rb +112 -0
- data/generators/vulcanize/templates/sphinx/config/rubber/role/sphinx/crontab +11 -0
- data/generators/vulcanize/templates/sphinx/config/rubber/role/sphinx/monit-sphinx.conf +10 -0
- data/generators/vulcanize/templates/sphinx/config/rubber/rubber-sphinx.yml +6 -0
- data/generators/vulcanize/templates/sphinx/templates.yml +1 -0
- data/generators/vulcanize/vulcanize_generator.rb +67 -0
- data/lib/capistrano/hostcmd.rb +12 -0
- data/lib/rubber/capistrano.rb +1 -0
- data/lib/rubber/cloud/aws.rb +305 -0
- data/lib/rubber/cloud/base.rb +16 -0
- data/lib/rubber/cloud.rb +13 -0
- data/lib/rubber/configuration.rb +47 -0
- data/lib/rubber/dns/base.rb +69 -0
- data/lib/rubber/dns/dyndns.rb +63 -0
- data/lib/rubber/dns/nettica.rb +56 -0
- data/lib/rubber/dns/zerigo.rb +121 -0
- data/lib/rubber/dns.rb +13 -0
- data/lib/rubber/environment.rb +161 -0
- data/lib/rubber/generator.rb +197 -0
- data/lib/rubber/instance.rb +165 -0
- data/lib/rubber/recipes/rubber/bundles.rb +28 -0
- data/lib/rubber/recipes/rubber/deploy.rb +90 -0
- data/lib/rubber/recipes/rubber/instances.rb +348 -0
- data/lib/rubber/recipes/rubber/load_balancers.rb +44 -0
- data/lib/rubber/recipes/rubber/security_groups.rb +189 -0
- data/lib/rubber/recipes/rubber/setup.rb +357 -0
- data/lib/rubber/recipes/rubber/static_ips.rb +107 -0
- data/lib/rubber/recipes/rubber/utils.rb +203 -0
- data/lib/rubber/recipes/rubber/volumes.rb +264 -0
- data/lib/rubber/recipes/rubber.rb +89 -0
- data/lib/rubber/tasks/rubber.rb +221 -0
- data/lib/rubber/util.rb +37 -0
- data/lib/rubber.rb +38 -0
- data/test/environment_test.rb +118 -0
- data/test/generator_test.rb +323 -0
- data/test/instance_test.rb +93 -0
- data/test/test_helper.rb +4 -0
- data/test/util_test.rb +16 -0
- metadata +273 -0
@@ -0,0 +1,264 @@
|
|
1
|
+
namespace :rubber do
|
2
|
+
|
3
|
+
desc <<-DESC
|
4
|
+
Sets up persistent volumes in the cloud
|
5
|
+
All volumes defined in rubber.yml will be created if neccessary, and attached/mounted on their associated instances
|
6
|
+
DESC
|
7
|
+
required_task :setup_volumes do
|
8
|
+
rubber_instances.filtered.each do |ic|
|
9
|
+
env = rubber_cfg.environment.bind(ic.role_names, ic.name)
|
10
|
+
created_vols = []
|
11
|
+
vol_specs = env.volumes || []
|
12
|
+
vol_specs.each do |vol_spec|
|
13
|
+
created_vols << setup_volume(ic, vol_spec)
|
14
|
+
end
|
15
|
+
created_vols.compact!
|
16
|
+
|
17
|
+
created_parts = []
|
18
|
+
partition_specs = env.local_volumes || []
|
19
|
+
partition_specs.each do |partition_spec|
|
20
|
+
created_parts << setup_partition(ic, partition_spec)
|
21
|
+
end
|
22
|
+
created_parts.compact!
|
23
|
+
zero_partitions(ic, created_parts)
|
24
|
+
created_vols += created_parts
|
25
|
+
|
26
|
+
created_vols = created_vols.compact.uniq
|
27
|
+
raid_specs = env.raid_volumes || []
|
28
|
+
raid_specs.each do |raid_spec|
|
29
|
+
# we want to format if we created the ec2 volumes, or if we don't have any
|
30
|
+
# ec2 volumes and are just creating raid array from ephemeral stores
|
31
|
+
format = raid_spec['source_devices'].all? {|dev| created_vols.include?(dev)}
|
32
|
+
setup_raid_volume(ic, raid_spec, format)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
desc <<-DESC
|
38
|
+
Shows the configured persistent volumes
|
39
|
+
DESC
|
40
|
+
required_task :describe_volumes do
|
41
|
+
results = []
|
42
|
+
format = "%-20s %-15s %-15s %-20s"
|
43
|
+
results << format % %w[Id Status Attached Instance]
|
44
|
+
|
45
|
+
volumes = cloud.describe_volumes()
|
46
|
+
volumes.each do |volume|
|
47
|
+
results << format % [volume[:id], volume[:status], volume[:attachment_status], volume[:attachment_instance_id]]
|
48
|
+
end
|
49
|
+
|
50
|
+
results.each {|r| logger.info r}
|
51
|
+
end
|
52
|
+
|
53
|
+
desc <<-DESC
|
54
|
+
Shows the configured persistent volumes
|
55
|
+
DESC
|
56
|
+
required_task :destroy_volume do
|
57
|
+
volume_id = get_env('VOLUME_ID', "Volume ID", true)
|
58
|
+
destroy_volume(volume_id)
|
59
|
+
end
|
60
|
+
|
61
|
+
def create_volume(size, zone)
|
62
|
+
volumeId = cloud.create_volume(size.to_s, zone)
|
63
|
+
fatal "Failed to create volume" if volumeId.nil?
|
64
|
+
return volumeId
|
65
|
+
end
|
66
|
+
|
67
|
+
def attach_volume(vol_id, instance_id, device)
|
68
|
+
cloud.attach_volume(vol_id, instance_id, device)
|
69
|
+
end
|
70
|
+
|
71
|
+
def setup_volume(ic, vol_spec)
|
72
|
+
created = nil
|
73
|
+
key = "#{ic.name}_#{vol_spec['device']}"
|
74
|
+
artifacts = rubber_instances.artifacts
|
75
|
+
vol_id = artifacts['volumes'][key]
|
76
|
+
|
77
|
+
# first create the volume if we don't have a global record (artifacts) for it
|
78
|
+
if ! vol_id
|
79
|
+
logger.info "Creating volume for #{ic.full_name}:#{vol_spec['device']}"
|
80
|
+
vol_id = create_volume(vol_spec['size'], vol_spec['zone'])
|
81
|
+
artifacts['volumes'][key] = vol_id
|
82
|
+
rubber_instances.save
|
83
|
+
created = vol_spec['device']
|
84
|
+
end
|
85
|
+
|
86
|
+
# then, attach it if we don't have a record (on instance) of attachment
|
87
|
+
ic.volumes ||= []
|
88
|
+
if ! ic.volumes.include?(vol_id)
|
89
|
+
logger.info "Attaching volume #{vol_id} to #{ic.full_name}:#{vol_spec['device']}"
|
90
|
+
attach_volume(vol_id, ic.instance_id, vol_spec['device'])
|
91
|
+
ic.volumes << vol_id
|
92
|
+
rubber_instances.save
|
93
|
+
|
94
|
+
print "Waiting for volume to attach"
|
95
|
+
while true do
|
96
|
+
print "."
|
97
|
+
sleep 2
|
98
|
+
volume = cloud.describe_volumes(vol_id).first
|
99
|
+
break if volume[:status] == "in-use"
|
100
|
+
end
|
101
|
+
print "\n"
|
102
|
+
|
103
|
+
# we don't mount/format at this time if we are doing a RAID array
|
104
|
+
if vol_spec['mount'] && vol_spec['filesystem']
|
105
|
+
# then format/mount/etc if we don't have an entry in hosts file
|
106
|
+
task :_setup_volume, :hosts => ic.external_ip do
|
107
|
+
rubber.run_script 'setup_volume', <<-ENDSCRIPT
|
108
|
+
if ! grep -q '#{vol_spec['mount']}' /etc/fstab; then
|
109
|
+
if mount | grep -q '#{vol_spec['mount']}'; then
|
110
|
+
umount '#{vol_spec['mount']}'
|
111
|
+
fi
|
112
|
+
mv /etc/fstab /etc/fstab.bak
|
113
|
+
cat /etc/fstab.bak | grep -v '#{vol_spec['mount']}' > /etc/fstab
|
114
|
+
echo '#{vol_spec['device']} #{vol_spec['mount']} #{vol_spec['filesystem']} noatime 0 0 # rubber volume #{vol_id}' >> /etc/fstab
|
115
|
+
|
116
|
+
#{('yes | mkfs -t ' + vol_spec['filesystem'] + ' ' + vol_spec['device']) if created}
|
117
|
+
mkdir -p '#{vol_spec['mount']}'
|
118
|
+
mount '#{vol_spec['mount']}'
|
119
|
+
fi
|
120
|
+
ENDSCRIPT
|
121
|
+
end
|
122
|
+
_setup_volume
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
return created
|
127
|
+
end
|
128
|
+
|
129
|
+
def setup_partition(ic, partition_spec)
|
130
|
+
created = nil
|
131
|
+
part_id = partition_spec['partition_device']
|
132
|
+
|
133
|
+
# Only create the partition if we haven't already done so
|
134
|
+
ic.partitions ||= []
|
135
|
+
if ! ic.partitions.include?(part_id)
|
136
|
+
# then format/mount/etc if we don't have an entry in hosts file
|
137
|
+
task :_setup_partition, :hosts => ic.external_ip do
|
138
|
+
rubber.run_script 'setup_partition', <<-ENDSCRIPT
|
139
|
+
if ! fdisk -l 2>&1 | grep -q '#{partition_spec['partition_device']}'; then
|
140
|
+
if grep -q '#{partition_spec['disk_device']}\\b' /etc/fstab; then
|
141
|
+
umount #{partition_spec['disk_device']}
|
142
|
+
mv /etc/fstab /etc/fstab.bak
|
143
|
+
cat /etc/fstab.bak | grep -v '#{partition_spec['disk_device']}\\b' > /etc/fstab
|
144
|
+
fi
|
145
|
+
|
146
|
+
# partition format is: Start (blank is first available),Size(MB due to -uM),Id(83=linux,82=swap,etc),Bootable
|
147
|
+
echo "#{partition_spec['start']},#{partition_spec['size']},#{partition_spec['type']},#{partition_spec['bootable']}" | sfdisk -L -uM #{partition_spec['disk_device']}
|
148
|
+
fi
|
149
|
+
ENDSCRIPT
|
150
|
+
end
|
151
|
+
_setup_partition
|
152
|
+
|
153
|
+
ic.partitions << part_id
|
154
|
+
rubber_instances.save
|
155
|
+
created = part_id
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
return created
|
160
|
+
end
|
161
|
+
|
162
|
+
def zero_partitions(ic, partitions)
|
163
|
+
env = rubber_cfg.environment.bind(ic.role_names, ic.name)
|
164
|
+
|
165
|
+
# don't zero out the ones that we weren't told to
|
166
|
+
partitions.delete_if do |part|
|
167
|
+
spec = env.local_volumes.find {|s| s['partition_device'] == part}
|
168
|
+
! spec['zero']
|
169
|
+
end
|
170
|
+
|
171
|
+
if partitions.size > 0
|
172
|
+
zero_script = ""
|
173
|
+
partitions.each do |partition|
|
174
|
+
zero_script << "nohup dd if=/dev/zero bs=1M of=#{partition} &> /dev/null &\n"
|
175
|
+
end
|
176
|
+
# then format/mount/etc if we don't have an entry in hosts file
|
177
|
+
task :_zero_partitions, :hosts => ic.external_ip do
|
178
|
+
rubber.run_script 'zero_partitions', <<-ENDSCRIPT
|
179
|
+
# zero out parition for performance (see amazon DevGuide)
|
180
|
+
echo "Zeroing out raid partitions to improve performance, this way take a while"
|
181
|
+
#{zero_script}
|
182
|
+
|
183
|
+
echo "Waiting for partitions to zero out"
|
184
|
+
while true; do
|
185
|
+
if ! ps ax | grep -q "[d]d.*/dev/zero"; then exit; fi
|
186
|
+
echo -n .
|
187
|
+
sleep 1
|
188
|
+
done
|
189
|
+
ENDSCRIPT
|
190
|
+
end
|
191
|
+
_zero_partitions
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
def setup_raid_volume(ic, raid_spec, create=false)
|
196
|
+
if create
|
197
|
+
mdadm_init = "mdadm --create #{raid_spec['device']} --level #{raid_spec['raid_level']} --raid-devices #{raid_spec['source_devices'].size} #{raid_spec['source_devices'].sort.join(' ')}"
|
198
|
+
else
|
199
|
+
mdadm_init = "mdadm --assemble #{raid_spec['device']} #{raid_spec['source_devices'].sort.join(' ')}"
|
200
|
+
end
|
201
|
+
|
202
|
+
task :_setup_raid_volume, :hosts => ic.external_ip do
|
203
|
+
rubber.run_script 'setup_raid_volume', <<-ENDSCRIPT
|
204
|
+
if ! grep -q '#{raid_spec['device']}' /etc/fstab; then
|
205
|
+
if mount | grep -q '#{raid_spec['mount']}'; then
|
206
|
+
umount '#{raid_spec['mount']}'
|
207
|
+
fi
|
208
|
+
mv /etc/fstab /etc/fstab.bak
|
209
|
+
cat /etc/fstab.bak | grep -v '#{raid_spec['mount']}' > /etc/fstab
|
210
|
+
echo '#{raid_spec['device']} #{raid_spec['mount']} #{raid_spec['filesystem']} noatime 0 0 # rubber raid volume' >> /etc/fstab
|
211
|
+
|
212
|
+
# seems to help devices initialize, otherwise mdadm fails because
|
213
|
+
# device not ready even though ec2 says the volume is attached
|
214
|
+
fdisk -l &> /dev/null
|
215
|
+
|
216
|
+
#{mdadm_init}
|
217
|
+
|
218
|
+
# set reconstruction speed
|
219
|
+
echo $((30*1024)) > /proc/sys/dev/raid/speed_limit_min
|
220
|
+
|
221
|
+
echo 'DEVICE /dev/hd*[0-9] /dev/sd*[0-9]' > /etc/mdadm/mdadm.conf
|
222
|
+
mdadm --detail --scan >> /etc/mdadm/mdadm.conf
|
223
|
+
|
224
|
+
mv /etc/rc.local /etc/rc.local.bak
|
225
|
+
echo "mdadm --assemble --scan" > /etc/rc.local
|
226
|
+
chmod +x /etc/rc.local
|
227
|
+
|
228
|
+
#{('yes | mkfs -t ' + raid_spec['filesystem'] + ' ' + raid_spec['device']) if create}
|
229
|
+
mkdir -p '#{raid_spec['mount']}'
|
230
|
+
mount '#{raid_spec['mount']}'
|
231
|
+
fi
|
232
|
+
ENDSCRIPT
|
233
|
+
end
|
234
|
+
_setup_raid_volume
|
235
|
+
end
|
236
|
+
|
237
|
+
def destroy_volume(volume_id)
|
238
|
+
|
239
|
+
logger.info "Detaching volume #{volume_id}"
|
240
|
+
cloud.detach_volume(volume_id) rescue logger.info("Volume was not attached")
|
241
|
+
|
242
|
+
print "Waiting for volume to detach"
|
243
|
+
while true do
|
244
|
+
print "."
|
245
|
+
sleep 2
|
246
|
+
volume = cloud.describe_volumes(volume_id).first
|
247
|
+
status = volume && volume[:attachment_status]
|
248
|
+
break if !status || status == "detached"
|
249
|
+
end
|
250
|
+
print "\n"
|
251
|
+
|
252
|
+
logger.info "Deleting volume #{volume_id}"
|
253
|
+
cloud.destroy_volume(volume_id)
|
254
|
+
|
255
|
+
logger.info "Removing volume #{volume_id} from rubber instances file"
|
256
|
+
artifacts = rubber_instances.artifacts
|
257
|
+
artifacts['volumes'].delete_if {|k,v| v == volume_id}
|
258
|
+
rubber_instances.each do |ic|
|
259
|
+
ic.volumes.delete(volume_id) if ic.volumes
|
260
|
+
end
|
261
|
+
rubber_instances.save
|
262
|
+
end
|
263
|
+
|
264
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# add this plugins lib dir to load path for capistrano
|
2
|
+
$:.unshift "#{File.dirname(__FILE__)}/../lib"
|
3
|
+
require 'rubygems'
|
4
|
+
require "socket"
|
5
|
+
require 'resolv'
|
6
|
+
require 'enumerator'
|
7
|
+
require 'capistrano/hostcmd'
|
8
|
+
require 'pp'
|
9
|
+
require 'rubber'
|
10
|
+
|
11
|
+
namespace :rubber do
|
12
|
+
|
13
|
+
# advise capistrano's task method so that tasks for non-existant roles don't
|
14
|
+
# fail when roles isn't defined due to using a FILTER for load_roles
|
15
|
+
# If you have a task you need to execute even when there are no
|
16
|
+
# roles, you have to use required_task instead of task - see rubber:create
|
17
|
+
# as an example of this role bootstrapping problem.
|
18
|
+
def allow_optional_tasks(ns)
|
19
|
+
class << ns
|
20
|
+
alias :required_task :task
|
21
|
+
def task(name, options={}, &block)
|
22
|
+
required_task(name, options) do
|
23
|
+
# define empty roles for the case when a task has a role that we don't define anywhere
|
24
|
+
[*options[:roles]].each do |r|
|
25
|
+
roles[r] ||= []
|
26
|
+
end
|
27
|
+
|
28
|
+
if find_servers_for_task(current_task).empty?
|
29
|
+
logger.info "No servers for task #{name}, skipping"
|
30
|
+
next
|
31
|
+
end
|
32
|
+
block.call
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
allow_optional_tasks(self)
|
39
|
+
on :load, "rubber:init"
|
40
|
+
|
41
|
+
required_task :init do
|
42
|
+
# Require cap 2.4 since we depend on bugs that have been fixed
|
43
|
+
require 'capistrano/version'
|
44
|
+
if Capistrano::Version::MAJOR < 2 || Capistrano::Version::MINOR < 4
|
45
|
+
fatal "rubber requires capistrano 2.4.0 or greater"
|
46
|
+
end
|
47
|
+
|
48
|
+
set :rubber_cfg, Rubber::Configuration.get_configuration(RUBBER_ENV)
|
49
|
+
set :rubber_env, rubber_cfg.environment.bind()
|
50
|
+
set :rubber_instances, rubber_cfg.instance
|
51
|
+
|
52
|
+
set :cloud, Rubber::Cloud::get_provider(rubber_env.cloud_provider || "aws", rubber_env, self)
|
53
|
+
|
54
|
+
load_roles() unless rubber_env.disable_auto_roles
|
55
|
+
# NOTE: for some reason Capistrano requires you to have both the public and
|
56
|
+
# the private key in the same folder, the public key should have the
|
57
|
+
# extension ".pub".
|
58
|
+
ssh_options[:keys] = rubber_env.cloud_providers[rubber_env.cloud_provider].key_file
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
# Automatically load and define capistrano roles from instance config
|
63
|
+
def load_roles
|
64
|
+
top.roles.clear
|
65
|
+
|
66
|
+
# define empty roles for all known ones so tasks don't fail if a role
|
67
|
+
# doesn't exist due to a filter
|
68
|
+
all_roles = rubber_instances.all_roles
|
69
|
+
all_roles += rubber_cfg.environment.known_roles
|
70
|
+
all_roles.uniq!
|
71
|
+
all_roles.each {|name| top.roles[name.to_sym] = []}
|
72
|
+
|
73
|
+
# define capistrano host => role mapping for all instances
|
74
|
+
rubber_instances.filtered.each do |ic|
|
75
|
+
ic.roles.each do |role|
|
76
|
+
opts = Rubber::Util::symbolize_keys(role.options)
|
77
|
+
msg = "Auto role: #{role.name.to_sym} => #{ic.full_name}"
|
78
|
+
msg << ", #{opts.inspect}" if opts.inspect.size > 0
|
79
|
+
logger.info msg
|
80
|
+
top.role role.name.to_sym, ic.full_name, opts
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
Dir[File.join(File.dirname(__FILE__), 'rubber/*.rb')].each do |rubber_part|
|
88
|
+
load(rubber_part)
|
89
|
+
end
|
@@ -0,0 +1,221 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'date'
|
3
|
+
require 'time'
|
4
|
+
require 'aws/s3'
|
5
|
+
require 'rubber'
|
6
|
+
|
7
|
+
namespace :rubber do
|
8
|
+
|
9
|
+
def rubber_env()
|
10
|
+
Rubber::Configuration.rubber_env
|
11
|
+
end
|
12
|
+
|
13
|
+
def rubber_instances()
|
14
|
+
Rubber::Configuration.rubber_instances
|
15
|
+
end
|
16
|
+
|
17
|
+
def cloud_provider
|
18
|
+
rubber_env.cloud_providers[rubber_env.cloud_provider]
|
19
|
+
end
|
20
|
+
|
21
|
+
def init_s3()
|
22
|
+
AWS::S3::Base.establish_connection!(:access_key_id => cloud_provider.access_key, :secret_access_key => cloud_provider.secret_access_key)
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Generate system config files by transforming the files in the config tree"
|
26
|
+
task :config do
|
27
|
+
cfg = Rubber::Configuration.get_configuration(RUBBER_ENV)
|
28
|
+
instance_alias = cfg.environment.current_host
|
29
|
+
instance = cfg.instance[instance_alias]
|
30
|
+
if instance
|
31
|
+
roles = instance.role_names
|
32
|
+
env = cfg.environment.bind(roles, instance_alias)
|
33
|
+
gen = Rubber::Configuration::Generator.new("#{RUBBER_ROOT}/config/rubber", roles, instance_alias)
|
34
|
+
elsif RUBBER_ENV == 'development'
|
35
|
+
roles = cfg.environment.known_roles
|
36
|
+
role_items = roles.collect do |r|
|
37
|
+
Rubber::Configuration::RoleItem.new(r, r == "db" ? {'primary' => true} : {})
|
38
|
+
end
|
39
|
+
env = cfg.environment.bind(roles, instance_alias)
|
40
|
+
domain = env.domain
|
41
|
+
instance = Rubber::Configuration::InstanceItem.new(instance_alias, domain, role_items, 'dummyid', ['dummygroup'])
|
42
|
+
instance.external_host = instance.full_name
|
43
|
+
instance.external_ip = "127.0.0.1"
|
44
|
+
instance.internal_host = instance.full_name
|
45
|
+
instance.internal_ip = "127.0.0.1"
|
46
|
+
cfg.instance.add(instance)
|
47
|
+
gen = Rubber::Configuration::Generator.new("#{RUBBER_ROOT}/config/rubber", roles, instance_alias)
|
48
|
+
gen.fake_root ="#{RUBBER_ROOT}/tmp/rubber"
|
49
|
+
else
|
50
|
+
puts "Instance not found for host: #{instance_alias}"
|
51
|
+
exit 1
|
52
|
+
end
|
53
|
+
|
54
|
+
if ENV['NO_POST']
|
55
|
+
gen.no_post = true
|
56
|
+
end
|
57
|
+
if ENV['FILE']
|
58
|
+
gen.file_pattern = ENV['FILE']
|
59
|
+
end
|
60
|
+
if ENV['FORCE']
|
61
|
+
gen.force = true
|
62
|
+
end
|
63
|
+
gen.stop_on_error_cmd = env.stop_on_error_cmd
|
64
|
+
gen.run
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
desc <<-DESC
|
69
|
+
Rotate rails app server logfiles. Should be run right after midnight.
|
70
|
+
The following arguments affect behavior:
|
71
|
+
LOG_DIR (required): Directory where log files are located
|
72
|
+
LOG_FILE (*.log): File pattern to match to find logs to rotate
|
73
|
+
LOG_AGE (7): Delete rotated logs older than this many days in the past
|
74
|
+
DESC
|
75
|
+
task :rotate_logs do
|
76
|
+
log_src_dir = get_env('LOG_DIR', true)
|
77
|
+
log_file_glob = get_env('LOG_FILES') || "*.log"
|
78
|
+
log_file_age = (get_env('LOG_AGE') || 7).to_i
|
79
|
+
|
80
|
+
rotated_date = (Date.today - 1).strftime('%Y%m%d')
|
81
|
+
puts "Rotating logfiles located at: #{log_src_dir}/#{log_file_glob}"
|
82
|
+
Dir["#{log_src_dir}/#{log_file_glob}"].each do |logfile|
|
83
|
+
rotated_file = "#{logfile}.#{rotated_date}"
|
84
|
+
if File.exist?(rotated_file)
|
85
|
+
rotated_file += "_#{Time.now.to_i}"
|
86
|
+
end
|
87
|
+
FileUtils.cp(logfile, rotated_file)
|
88
|
+
File.truncate logfile, 0
|
89
|
+
end
|
90
|
+
|
91
|
+
tdate = Date.today - log_file_age
|
92
|
+
threshold = Time.local(tdate.year, tdate.month, tdate.day)
|
93
|
+
puts "Cleaning rotated log files older than #{log_file_age} days"
|
94
|
+
Dir["#{log_src_dir}/#{log_file_glob}.[0-9]*"].each do |logfile|
|
95
|
+
if File.mtime(logfile) < threshold
|
96
|
+
FileUtils.rm_f(logfile)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
desc <<-DESC
|
102
|
+
Backup database to given backup directory
|
103
|
+
The following arguments affect behavior:
|
104
|
+
BACKUP_DIR (required): Directory where db backups will be stored
|
105
|
+
BACKUP_AGE (7): Delete rotated logs older than this many days in the past
|
106
|
+
DBUSER (required) User to connect to the db as
|
107
|
+
DBPASS (optional): Pass to connect to the db with
|
108
|
+
DBHOST (required): Host where the db is
|
109
|
+
DBNAME (required): Database name to backup
|
110
|
+
DESC
|
111
|
+
task :backup_db do
|
112
|
+
dir = get_env('BACKUP_DIR', true)
|
113
|
+
age = (get_env('BACKUP_AGE') || 3).to_i
|
114
|
+
time_stamp = Time.now.strftime("%Y-%m-%d_%H-%M")
|
115
|
+
backup_file = "#{dir}/#{RUBBER_ENV}_dump_#{time_stamp}.sql.gz"
|
116
|
+
FileUtils.mkdir_p(File.dirname(backup_file))
|
117
|
+
|
118
|
+
user = get_env('DBUSER', true)
|
119
|
+
pass = get_env('DBPASS')
|
120
|
+
pass = nil if pass.strip.size == 0
|
121
|
+
host = get_env('DBHOST', true)
|
122
|
+
name = get_env('DBNAME', true)
|
123
|
+
|
124
|
+
raise "No db_backup_cmd defined in rubber.yml, cannot backup!" unless rubber_env.db_backup_cmd
|
125
|
+
db_backup_cmd = rubber_env.db_backup_cmd.gsub(/%([^%]+)%/, '#{\1}')
|
126
|
+
db_backup_cmd = eval('%Q{' + db_backup_cmd + '}')
|
127
|
+
|
128
|
+
puts "Backing up database with command:"
|
129
|
+
sh db_backup_cmd
|
130
|
+
puts "Created backup: #{backup_file}"
|
131
|
+
|
132
|
+
s3_prefix = "db/"
|
133
|
+
backup_bucket = cloud_provider.backup_bucket
|
134
|
+
if backup_bucket
|
135
|
+
init_s3
|
136
|
+
unless AWS::S3::Bucket.list.find { |b| b.name == backup_bucket }
|
137
|
+
AWS::S3::Bucket.create(backup_bucket)
|
138
|
+
end
|
139
|
+
dest = "#{s3_prefix}#{File.basename(backup_file)}"
|
140
|
+
puts "Saving db backup to S3: #{backup_bucket}:#{dest}"
|
141
|
+
AWS::S3::S3Object.store(dest, open(backup_file), backup_bucket)
|
142
|
+
end
|
143
|
+
|
144
|
+
tdate = Date.today - age
|
145
|
+
threshold = Time.local(tdate.year, tdate.month, tdate.day)
|
146
|
+
puts "Cleaning backups older than #{age} days"
|
147
|
+
Dir["#{dir}/*"].each do |file|
|
148
|
+
if File.mtime(file) < threshold
|
149
|
+
puts "Deleting #{file}"
|
150
|
+
FileUtils.rm_f(file)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
if backup_bucket
|
155
|
+
puts "Cleaning S3 backups older than #{age} days from: #{backup_bucket}:#{s3_prefix}"
|
156
|
+
AWS::S3::Bucket.objects(backup_bucket, :prefix => s3_prefix).clone.each do |obj|
|
157
|
+
if Time.parse(obj.about["last-modified"]) < threshold
|
158
|
+
puts "Deleting #{obj.key}"
|
159
|
+
obj.delete
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
desc <<-DESC
|
166
|
+
Restores a database backup from s3.
|
167
|
+
This tries to find the last backup made or the s3 object identified by the
|
168
|
+
key FILENAME
|
169
|
+
The following arguments affect behavior:
|
170
|
+
FILENAME (optional): key of S3 object to use
|
171
|
+
DBUSER (required) User to connect to the db as
|
172
|
+
DBPASS (optional): Pass to connect to the db with
|
173
|
+
DBHOST (required): Host where the db is
|
174
|
+
DBNAME (required): Database name to backup
|
175
|
+
DESC
|
176
|
+
task :restore_db_s3 do
|
177
|
+
file = get_env('FILENAME')
|
178
|
+
user = get_env('DBUSER', true)
|
179
|
+
pass = get_env('DBPASS')
|
180
|
+
pass = nil if pass && pass.strip.size == 0
|
181
|
+
host = get_env('DBHOST', true)
|
182
|
+
name = get_env('DBNAME', true)
|
183
|
+
|
184
|
+
raise "No db_restore_cmd defined in rubber.yml" unless rubber_env.db_restore_cmd
|
185
|
+
db_restore_cmd = rubber_env.db_restore_cmd.gsub(/%([^%]+)%/, '#{\1}')
|
186
|
+
db_restore_cmd = eval('%Q{' + db_restore_cmd + '}')
|
187
|
+
|
188
|
+
# try to fetch a matching file from s3 (if backup_bucket given)
|
189
|
+
backup_bucket = cloud_provider.backup_bucket
|
190
|
+
raise "No backup_bucket defined in rubber.yml" unless backup_bucket
|
191
|
+
if (init_s3 &&
|
192
|
+
AWS::S3::Bucket.list.find { |b| b.name == backup_bucket })
|
193
|
+
s3objects = AWS::S3::Bucket.find(backup_bucket,
|
194
|
+
:prefix => 'db/')
|
195
|
+
if file
|
196
|
+
puts "trying to fetch #{file} from s3"
|
197
|
+
data = s3objects.detect { |o| file == o.key }
|
198
|
+
else
|
199
|
+
puts "trying to fetch last modified s3 backup"
|
200
|
+
data = s3objects.max {|a,b| a.about["last-modified"] <=> b.about["last-modified"] }
|
201
|
+
end
|
202
|
+
end
|
203
|
+
raise "could not access backup file via s3" unless data
|
204
|
+
|
205
|
+
puts "piping restore data to command [#{db_restore_cmd}]"
|
206
|
+
IO.popen(db_restore_cmd, mode='w') do |p|
|
207
|
+
data.value do |segment|
|
208
|
+
p.write segment
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
213
|
+
|
214
|
+
|
215
|
+
def get_env(name, required=false)
|
216
|
+
value = ENV[name]
|
217
|
+
raise("#{name} is required, pass using environment") if required && ! value
|
218
|
+
return value
|
219
|
+
end
|
220
|
+
|
221
|
+
end
|
data/lib/rubber/util.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module Rubber
|
4
|
+
module Util
|
5
|
+
|
6
|
+
def self.symbolize_keys(map)
|
7
|
+
map.inject({}) do |options, (key, value)|
|
8
|
+
options[key.to_sym || key] = value
|
9
|
+
options
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.stringify(val)
|
14
|
+
case val
|
15
|
+
when String
|
16
|
+
val
|
17
|
+
when Hash
|
18
|
+
val.inject({}) {|h, a| h[stringify(a[0])] = stringify(a[1]); h}
|
19
|
+
when Enumerable
|
20
|
+
val.collect {|v| stringify(v)}
|
21
|
+
else
|
22
|
+
val.to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
# Opens the file for writing by root
|
28
|
+
def self.sudo_open(path, perms, &block)
|
29
|
+
open("|sudo tee #{path} > /dev/null", perms, &block)
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.is_rails?
|
33
|
+
File.exist?(File.join(RUBBER_ROOT, 'config', 'environment.rb'))
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
data/lib/rubber.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
module Rubber
|
4
|
+
|
5
|
+
VERSION = File.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).chomp
|
6
|
+
|
7
|
+
def self.initialize(project_root, project_env)
|
8
|
+
return if defined?(RUBBER_ROOT) && defined?(RUBBER_ENV)
|
9
|
+
|
10
|
+
Object.const_set('RUBBER_ENV', project_env)
|
11
|
+
Object.const_set('RUBBER_ROOT', File.expand_path(project_root))
|
12
|
+
|
13
|
+
# pull in basic rails env. rubber only needs RAILS_ROOT and RAILS_ENV.
|
14
|
+
# We actually do NOT want the entire rails environment because it
|
15
|
+
# complicates bootstrap (i.e. can't run config to create db because full
|
16
|
+
# rails env needs db to exist as some plugin accesses model or something)
|
17
|
+
rails_boot_file = File.join(RUBBER_ROOT, 'config', 'boot')
|
18
|
+
require(rails_boot_file) if File.exists? rails_boot_file
|
19
|
+
|
20
|
+
if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER
|
21
|
+
Object.const_set('LOGGER', RAILS_DEFAULT_LOGGER)
|
22
|
+
else
|
23
|
+
Object.const_set('LOGGER', Logger.new($stdout))
|
24
|
+
LOGGER.level = Logger::INFO
|
25
|
+
LOGGER.formatter = lambda {|severity, time, progname, msg| "Rubber[%s]: %s\n" % [severity, msg.to_s.lstrip]}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
require 'rubber/configuration'
|
33
|
+
require 'rubber/environment'
|
34
|
+
require 'rubber/generator'
|
35
|
+
require 'rubber/instance'
|
36
|
+
require 'rubber/util'
|
37
|
+
require 'rubber/cloud'
|
38
|
+
require 'rubber/dns'
|