skippy-ec2onrails 0.9.10 → 0.9.11
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +21 -0
- data/Manifest +7 -1
- data/README.textile +10 -13
- data/Rakefile +4 -3
- data/TODO +10 -8
- data/ec2onrails.gemspec +17 -15
- data/examples/deploy.rb +7 -1
- data/lib/ec2onrails/recipes.rb +11 -714
- data/lib/ec2onrails/recipes/db.rb +377 -0
- data/lib/ec2onrails/recipes/deploy.rb +30 -0
- data/lib/ec2onrails/recipes/server.rb +489 -0
- data/lib/ec2onrails/version.rb +1 -1
- data/server/files/etc/apache2/sites-available/app.common +6 -1
- data/server/files/etc/cron.d/{backup_app_db_to_s3 → ec2onrails} +8 -0
- data/server/files/etc/cron.daily/app +17 -2
- data/server/files/etc/cron.hourly/app +16 -2
- data/server/files/etc/cron.monthly/app +16 -2
- data/server/files/etc/cron.weekly/app +16 -2
- data/server/files/etc/ec2onrails/README +1 -1
- data/server/files/etc/god/app.god +7 -2
- data/server/files/etc/god/dkim_filter.god +20 -0
- data/server/files/etc/god/system.god +1 -1
- data/server/files/etc/god/web.god +6 -4
- data/server/files/etc/mysql/my.cnf +3 -0
- data/server/files/etc/nginx/nginx.conf +11 -2
- data/server/files/etc/rcS.d/S91ec2-first-startup +36 -1
- data/server/files/etc/rcS.d/S92ec2-every-startup +29 -1
- data/server/files/etc/rcS.d/S99set_roles +3 -1
- data/server/files/etc/sudoers +26 -1
- data/server/files/usr/bin/god +0 -0
- data/server/files/usr/local/ec2onrails/bin/backup_app_db.rb +3 -2
- data/server/files/usr/local/ec2onrails/bin/backup_dir.rb +89 -0
- data/server/files/usr/local/ec2onrails/bin/exec_runner +9 -6
- data/server/files/usr/local/ec2onrails/bin/init_services.rb +7 -0
- data/server/files/usr/local/ec2onrails/bin/rails_env +1 -2
- data/server/files/usr/local/ec2onrails/bin/setup_web_proxy.rb +32 -28
- data/server/files/usr/local/ec2onrails/bin/update_hostname +40 -0
- data/server/files/usr/local/ec2onrails/lib/mysql_helper.rb +1 -1
- data/server/files/usr/local/ec2onrails/lib/s3_helper.rb +22 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/every-startup/get-hostname.sh +1 -3
- data/server/rakefile.rb +12 -5
- data/test/test_app/config/deploy.rb +1 -1
- metadata +16 -12
data/lib/ec2onrails/version.rb
CHANGED
@@ -43,7 +43,7 @@ RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
|
|
43
43
|
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
|
44
44
|
|
45
45
|
# Deflate
|
46
|
-
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css
|
46
|
+
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript application/x-javascript text/css
|
47
47
|
BrowserMatch ^Mozilla/4 gzip-only-text/html
|
48
48
|
BrowserMatch ^Mozilla/4.0[678] no-gzip
|
49
49
|
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
|
@@ -54,3 +54,8 @@ BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
|
|
54
54
|
#DeflateFilterNote Ratio ratio_info
|
55
55
|
#LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
|
56
56
|
#CustomLog logs/myapp_deflate_log deflate
|
57
|
+
|
58
|
+
# Fix rails "IP spoofing attack?!" error
|
59
|
+
# see http://iprog.com/posting/2008/08/rails_500_error_ip_spoofing_attack
|
60
|
+
# see http://rails.lighthouseapp.com/projects/8994/tickets/322-don-t-return-500-if-client-ip-and-x-forwarded-for-agree
|
61
|
+
RequestHeader unset Client-IP
|
@@ -14,3 +14,11 @@
|
|
14
14
|
# with EBS:
|
15
15
|
# Full snapshot every 2 hours
|
16
16
|
11 */2 * * * root test -f /etc/mysql/conf.d/mysql-ec2-ebs.cnf && /usr/local/ec2onrails/bin/backup_app_db.rb
|
17
|
+
|
18
|
+
|
19
|
+
# Check if the hostname has been updated every 15 minutes.
|
20
|
+
# This can happen if the user associated or disassociated an Elastic IP
|
21
|
+
12,27,42,57 * * * * root /usr/local/ec2onrails/bin/update_hostname
|
22
|
+
|
23
|
+
|
24
|
+
|
@@ -4,6 +4,21 @@
|
|
4
4
|
# /usr/local/ec2onrails/bin/exec_runner
|
5
5
|
# to run a script under a specific role
|
6
6
|
# see the file for details
|
7
|
-
|
7
|
+
|
8
8
|
cd /mnt/app/current
|
9
|
-
|
9
|
+
|
10
|
+
if test -e /mnt/app/current/script/cron/daily; then
|
11
|
+
if test -f /mnt/app/current/script/cron/daily; then
|
12
|
+
sudo -u app /usr/local/ec2onrails/bin/rails_env script/cron/daily;
|
13
|
+
else
|
14
|
+
sudo -u app /usr/local/ec2onrails/bin/rails_env script/cron/daily/*;
|
15
|
+
fi
|
16
|
+
exit 0;
|
17
|
+
fi
|
18
|
+
|
19
|
+
#DEPRECATED: just for old usage....
|
20
|
+
if test -e /mnt/app/current/script/daily
|
21
|
+
then
|
22
|
+
sudo -u app /usr/local/ec2onrails/bin/rails_env script/daily
|
23
|
+
exit 0;
|
24
|
+
fi
|
@@ -5,6 +5,20 @@
|
|
5
5
|
# to run a script under a specific role
|
6
6
|
# see the file for details
|
7
7
|
|
8
|
-
test -e /mnt/app/current/script/hourly || exit 0
|
9
8
|
cd /mnt/app/current
|
10
|
-
|
9
|
+
|
10
|
+
if test -e /mnt/app/current/script/cron/hourly; then
|
11
|
+
if test -f /mnt/app/current/script/cron/hourly; then
|
12
|
+
sudo -u app /usr/local/ec2onrails/bin/rails_env script/cron/hourly;
|
13
|
+
else
|
14
|
+
sudo -u app /usr/local/ec2onrails/bin/rails_env script/cron/hourly/*;
|
15
|
+
fi
|
16
|
+
exit 0;
|
17
|
+
fi
|
18
|
+
|
19
|
+
#DEPRECATED: just for old usage....
|
20
|
+
if test -e /mnt/app/current/script/hourly
|
21
|
+
then
|
22
|
+
sudo -u app /usr/local/ec2onrails/bin/rails_env script/hourly
|
23
|
+
exit 0;
|
24
|
+
fi
|
@@ -5,6 +5,20 @@
|
|
5
5
|
# to run a script under a specific role
|
6
6
|
# see the file for details
|
7
7
|
|
8
|
-
test -e /mnt/app/current/script/monthly || exit 0
|
9
8
|
cd /mnt/app/current
|
10
|
-
|
9
|
+
|
10
|
+
if test -e /mnt/app/current/script/cron/monthly; then
|
11
|
+
if test -f /mnt/app/current/script/cron/monthly; then
|
12
|
+
sudo -u app /usr/local/ec2onrails/bin/rails_env script/cron/monthly;
|
13
|
+
else
|
14
|
+
sudo -u app /usr/local/ec2onrails/bin/rails_env script/cron/monthly/*;
|
15
|
+
fi
|
16
|
+
exit 0;
|
17
|
+
fi
|
18
|
+
|
19
|
+
#DEPRECATED: just for old usage....
|
20
|
+
if test -e /mnt/app/current/script/monthly
|
21
|
+
then
|
22
|
+
sudo -u app /usr/local/ec2onrails/bin/rails_env script/monthly
|
23
|
+
exit 0;
|
24
|
+
fi
|
@@ -5,6 +5,20 @@
|
|
5
5
|
# to run a script under a specific role
|
6
6
|
# see the file for details
|
7
7
|
|
8
|
-
test -e /mnt/app/current/script/weekly || exit 0
|
9
8
|
cd /mnt/app/current
|
10
|
-
|
9
|
+
|
10
|
+
if test -e /mnt/app/current/script/cron/weekly; then
|
11
|
+
if test -f /mnt/app/current/script/cron/weekly; then
|
12
|
+
sudo -u app /usr/local/ec2onrails/bin/rails_env script/cron/weekly;
|
13
|
+
else
|
14
|
+
sudo -u app /usr/local/ec2onrails/bin/rails_env script/cron/weekly/*;
|
15
|
+
fi
|
16
|
+
exit 0;
|
17
|
+
fi
|
18
|
+
|
19
|
+
#DEPRECATED: just for old usage....
|
20
|
+
if test -e /mnt/app/current/script/weekly
|
21
|
+
then
|
22
|
+
sudo -u app /usr/local/ec2onrails/bin/rails_env script/weekly
|
23
|
+
exit 0;
|
24
|
+
fi
|
@@ -1,5 +1,5 @@
|
|
1
1
|
You can place a *.erb file in this directory and the set_roles script will
|
2
|
-
format them. This is helpful if you want to customize your
|
2
|
+
format them. This is helpful if you want to customize your balancer_members
|
3
3
|
or nginx_upstream_members files. For example, if you want to define multiple
|
4
4
|
instances for nginx to hook into, you can put something like this in
|
5
5
|
nginx_upstream_members.erb:
|
@@ -1,6 +1,8 @@
|
|
1
1
|
# rolling restart idea plagiarized directly from:
|
2
2
|
# http://blog.pragmatic-it.de/articles/2008/07/09/poor-mans-rolling-restart-for-thin-god
|
3
|
-
|
3
|
+
#NOTE: this doesn't do what you think it does...
|
4
|
+
# requests are queued up at nginx and requests start to time out
|
5
|
+
restart_time = 2.seconds #how long to restart the entire cluster
|
4
6
|
rolling_delay = (restart_time / @configs.web_num_instances.to_f).ceil
|
5
7
|
@configs.web_port_range.each_with_index do |port, i|
|
6
8
|
God.watch do |w|
|
@@ -20,13 +22,16 @@ rolling_delay = (restart_time / @configs.web_num_instances.to_f).ceil
|
|
20
22
|
default_configurations(w)
|
21
23
|
create_pid_dir(w)
|
22
24
|
restart_if_resource_hog(w, :memory_usage => 170.megabytes) do |restart|
|
25
|
+
#NOTE: this will hit every instance, meaning every minute you have a hit for every port you have a mongrel on.
|
26
|
+
# adding the port number to the call just to help with making this obvious in the logs
|
23
27
|
restart.condition(:http_response_code) do |c|
|
24
28
|
c.code_is_not = %w(200 304)
|
25
29
|
c.host = '127.0.0.1'
|
26
|
-
c.path =
|
30
|
+
c.path = "/?port=#{port}"
|
27
31
|
c.port = port
|
28
32
|
c.timeout = 10.seconds
|
29
33
|
c.times = 2
|
34
|
+
c.interval = 1.minute
|
30
35
|
end
|
31
36
|
end
|
32
37
|
|
@@ -0,0 +1,20 @@
|
|
1
|
+
if File.exists?('/etc/init.d/dkim-filter')
|
2
|
+
#we have it installed, so lets register it with God
|
3
|
+
God.watch do |w|
|
4
|
+
w.name = 'dkim_filter'
|
5
|
+
w.group = 'app'
|
6
|
+
w.autostart = false
|
7
|
+
|
8
|
+
w.start = "/etc/init.d/dkim-filter start"
|
9
|
+
w.stop = "/etc/init.d/dkim-filter stop"
|
10
|
+
w.restart = "/etc/init.d/dkim-filter restart"
|
11
|
+
|
12
|
+
w.pid_file = "/var/run/dkim-filter/dkim-filter.pid"
|
13
|
+
|
14
|
+
default_configurations(w)
|
15
|
+
create_pid_dir(w)
|
16
|
+
restart_if_resource_hog(w, :memory_usage => 20.megabytes, :cpu_usage => 10.percent)
|
17
|
+
monitor_lifecycle(w)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -2,21 +2,23 @@ nginx_enabled = system("which nginx 2>&1 > /dev/null")
|
|
2
2
|
|
3
3
|
God.watch do |w|
|
4
4
|
applog(w, :info, "web: using #{nginx_enabled ? 'nginx' : 'apache2'}")
|
5
|
-
|
5
|
+
server_status_path = '/'
|
6
6
|
if nginx_enabled
|
7
7
|
w.name = "nginx"
|
8
8
|
w.start = "/etc/init.d/nginx start"
|
9
9
|
w.stop = "/etc/init.d/nginx stop"
|
10
10
|
w.restart = "/etc/init.d/nginx restart"
|
11
11
|
w.pid_file = "/var/run/nginx.pid"
|
12
|
+
server_status_path = '/nginx_status'
|
12
13
|
else
|
13
14
|
w.name = "apache"
|
14
15
|
w.start = "/etc/init.d/apache2 start"
|
15
16
|
w.stop = "/etc/init.d/apache2 stop"
|
16
17
|
w.restart = "/etc/init.d/apache2 restart"
|
17
18
|
w.pid_file = "/var/run/apache2.pid"
|
19
|
+
server_status_path = '/server-status'
|
18
20
|
end
|
19
|
-
w.grace =
|
21
|
+
w.grace = 30.seconds
|
20
22
|
w.group = 'web'
|
21
23
|
w.autostart = false
|
22
24
|
|
@@ -25,9 +27,9 @@ God.watch do |w|
|
|
25
27
|
restart.condition(:http_response_code) do |c|
|
26
28
|
c.host = '127.0.0.1'
|
27
29
|
c.port = 80
|
28
|
-
c.path =
|
30
|
+
c.path = server_status_path
|
29
31
|
c.code_is_not = 200
|
30
|
-
c.timeout =
|
32
|
+
c.timeout = 5.seconds
|
31
33
|
c.times = [3, 5] # 3 out of 5 intervals
|
32
34
|
end
|
33
35
|
end
|
@@ -42,6 +42,8 @@ tmpdir = /mnt/mysql_data/tmp
|
|
42
42
|
language = /usr/share/mysql/english
|
43
43
|
skip-external-locking
|
44
44
|
default-storage-engine = InnoDB
|
45
|
+
character-set-server = utf8
|
46
|
+
collation-server = utf8_general_ci
|
45
47
|
|
46
48
|
#
|
47
49
|
# Instead of skip-networking the default is now to listen only on
|
@@ -126,6 +128,7 @@ quote-names
|
|
126
128
|
max_allowed_packet = 16M
|
127
129
|
|
128
130
|
[mysql]
|
131
|
+
default-character-set = utf8
|
129
132
|
#no-auto-rehash # faster start of mysql but no tab completition
|
130
133
|
|
131
134
|
[isamchk]
|
@@ -27,7 +27,7 @@ http {
|
|
27
27
|
log_format main '$remote_addr [$time_local] '
|
28
28
|
'"$scheme $host $request" $status $body_bytes_sent "$http_referer" '
|
29
29
|
'"$http_user_agent" "$http_x_forwarded_for" '
|
30
|
-
'($request_time'
|
30
|
+
'($request_time)';
|
31
31
|
|
32
32
|
# main access log
|
33
33
|
access_log /mnt/log/nginx/access.log main;
|
@@ -51,6 +51,7 @@ http {
|
|
51
51
|
text/html \
|
52
52
|
text/css \
|
53
53
|
application/x-javascript \
|
54
|
+
application/json \
|
54
55
|
text/xml \
|
55
56
|
application/xml \
|
56
57
|
application/xml+rss \
|
@@ -103,7 +104,15 @@ http {
|
|
103
104
|
location = /500.html {
|
104
105
|
root /mnt/app/current/public;
|
105
106
|
}
|
106
|
-
|
107
|
+
|
108
|
+
#hide hidden files and folders
|
109
|
+
location ~ /\..+ {
|
110
|
+
deny all;
|
111
|
+
}
|
112
|
+
|
113
|
+
#do not show the nginx version number in the server header
|
114
|
+
server_tokens off;
|
115
|
+
|
107
116
|
# this allows people to use images and css in their maintenance.html file
|
108
117
|
if ($request_filename ~* \.(css|jpg|gif|png)$) {
|
109
118
|
break;
|
@@ -1 +1,36 @@
|
|
1
|
-
|
1
|
+
#!/bin/sh
|
2
|
+
|
3
|
+
# This file is part of EC2 on Rails.
|
4
|
+
# http://rubyforge.org/projects/ec2onrails/
|
5
|
+
#
|
6
|
+
# Copyright 2007 Paul Dowman, http://pauldowman.com/
|
7
|
+
#
|
8
|
+
# EC2 on Rails is free software; you can redistribute it and/or modify
|
9
|
+
# it under the terms of the GNU General Public License as published by
|
10
|
+
# the Free Software Foundation; either version 2 of the License, or
|
11
|
+
# (at your option) any later version.
|
12
|
+
#
|
13
|
+
# EC2 on Rails is distributed in the hope that it will be useful,
|
14
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
+
# GNU General Public License for more details.
|
17
|
+
#
|
18
|
+
# You should have received a copy of the GNU General Public License
|
19
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
20
|
+
|
21
|
+
|
22
|
+
# This script runs the ec2 startup scripts
|
23
|
+
|
24
|
+
FILE=/ec2onrails-first-boot
|
25
|
+
|
26
|
+
if [ -e $FILE ] ; then
|
27
|
+
|
28
|
+
# this is an ugly hack to make sure DHCP initialization is finished:
|
29
|
+
sleep 15
|
30
|
+
|
31
|
+
for SCRIPT in `ls -I README /usr/local/ec2onrails/startup-scripts/first-startup` ; do
|
32
|
+
"/usr/local/ec2onrails/startup-scripts/first-startup/$SCRIPT"
|
33
|
+
done
|
34
|
+
|
35
|
+
rm $FILE
|
36
|
+
fi
|
@@ -1 +1,29 @@
|
|
1
|
-
|
1
|
+
#!/bin/sh
|
2
|
+
|
3
|
+
# This file is part of EC2 on Rails.
|
4
|
+
# http://rubyforge.org/projects/ec2onrails/
|
5
|
+
#
|
6
|
+
# Copyright 2007 Paul Dowman, http://pauldowman.com/
|
7
|
+
#
|
8
|
+
# EC2 on Rails is free software; you can redistribute it and/or modify
|
9
|
+
# it under the terms of the GNU General Public License as published by
|
10
|
+
# the Free Software Foundation; either version 2 of the License, or
|
11
|
+
# (at your option) any later version.
|
12
|
+
#
|
13
|
+
# EC2 on Rails is distributed in the hope that it will be useful,
|
14
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
+
# GNU General Public License for more details.
|
17
|
+
#
|
18
|
+
# You should have received a copy of the GNU General Public License
|
19
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
20
|
+
|
21
|
+
|
22
|
+
# This script runs the ec2 startup scripts
|
23
|
+
|
24
|
+
# this is an ugly hack to make sure DHCP initialization is finished:
|
25
|
+
sleep 15
|
26
|
+
|
27
|
+
for SCRIPT in `ls -I README /usr/local/ec2onrails/startup-scripts/every-startup` ; do
|
28
|
+
"/usr/local/ec2onrails/startup-scripts/every-startup/$SCRIPT"
|
29
|
+
done
|
data/server/files/etc/sudoers
CHANGED
@@ -1 +1,26 @@
|
|
1
|
-
sudoers
|
1
|
+
# /etc/sudoers
|
2
|
+
#
|
3
|
+
# This file MUST be edited with the 'visudo' command as root.
|
4
|
+
#
|
5
|
+
# See the man page for details on how to write a sudoers file.
|
6
|
+
# Host alias specification
|
7
|
+
|
8
|
+
# User alias specification
|
9
|
+
|
10
|
+
# Cmnd alias specification
|
11
|
+
|
12
|
+
# Defaults
|
13
|
+
|
14
|
+
Defaults !lecture,tty_tickets,!fqdn
|
15
|
+
|
16
|
+
# User privilege specification
|
17
|
+
root ALL=(ALL) ALL
|
18
|
+
|
19
|
+
# The 'app' user can run sudo without a password
|
20
|
+
# This is a security hole. Use sudoers.restricted when running in regular mode
|
21
|
+
app ALL=(ALL) NOPASSWD: ALL
|
22
|
+
|
23
|
+
|
24
|
+
# If you add named administrator accounts, add them to the group 'sudoers'
|
25
|
+
# to give them sudo access
|
26
|
+
%sudoers ALL=(ALL) ALL
|
data/server/files/usr/bin/god
CHANGED
File without changes
|
@@ -44,12 +44,13 @@ module CommandLineArgs extend OptiFlagSet
|
|
44
44
|
optional_flag "dir"
|
45
45
|
optional_switch_flag "incremental"
|
46
46
|
optional_switch_flag "reset"
|
47
|
+
optional_switch_flag "no_ebs"
|
47
48
|
and_process!
|
48
49
|
end
|
49
50
|
@mysql = Ec2onrails::MysqlHelper.new
|
50
51
|
|
51
52
|
|
52
|
-
if File.exists?("/etc/mysql/conf.d/mysql-ec2-ebs.cnf")
|
53
|
+
if File.exists?("/etc/mysql/conf.d/mysql-ec2-ebs.cnf") && !ARGV.flags.no_ebs
|
53
54
|
# we have ebs enabled....
|
54
55
|
|
55
56
|
@aws = Ec2onrails::AwsHelper.new
|
@@ -57,7 +58,7 @@ if File.exists?("/etc/mysql/conf.d/mysql-ec2-ebs.cnf")
|
|
57
58
|
ec2 = EC2::Base.new( :access_key_id => @aws.aws_access_key, :secret_access_key => @aws.aws_secret_access_key )
|
58
59
|
|
59
60
|
#lets make sure we have space: AMAZON puts a 500 limit on the number of snapshots
|
60
|
-
snaps = ec2.describe_snapshots['
|
61
|
+
snaps = ec2.describe_snapshots['snapshotSet']['item'] rescue nil
|
61
62
|
if snaps && snaps.size > 450
|
62
63
|
# TODO:
|
63
64
|
# can we make this a bit smarter? With a limit of 500, that is difficult.
|
@@ -0,0 +1,89 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
# This file is part of EC2 on Rails.
|
4
|
+
# http://rubyforge.org/projects/ec2onrails/
|
5
|
+
#
|
6
|
+
# Copyright 2007 Paul Dowman, http://pauldowman.com/
|
7
|
+
#
|
8
|
+
# EC2 on Rails is free software; you can redistribute it and/or modify
|
9
|
+
# it under the terms of the GNU General Public License as published by
|
10
|
+
# the Free Software Foundation; either version 2 of the License, or
|
11
|
+
# (at your option) any later version.
|
12
|
+
#
|
13
|
+
# EC2 on Rails is distributed in the hope that it will be useful,
|
14
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
+
# GNU General Public License for more details.
|
17
|
+
#
|
18
|
+
# You should have received a copy of the GNU General Public License
|
19
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
20
|
+
|
21
|
+
require "rubygems"
|
22
|
+
require "optiflag"
|
23
|
+
require "fileutils"
|
24
|
+
require 'EC2'
|
25
|
+
require "#{File.dirname(__FILE__)}/../lib/mysql_helper"
|
26
|
+
require "#{File.dirname(__FILE__)}/../lib/s3_helper"
|
27
|
+
require "#{File.dirname(__FILE__)}/../lib/aws_helper"
|
28
|
+
require "#{File.dirname(__FILE__)}/../lib/roles_helper"
|
29
|
+
|
30
|
+
require "#{File.dirname(__FILE__)}/../lib/utils"
|
31
|
+
|
32
|
+
# Only run if this instance is the db_pimrary
|
33
|
+
# The original code would run on any instance that had /etc/init.d/mysql
|
34
|
+
# Which was pretty much all instances no matter what role
|
35
|
+
include Ec2onrails::RolesHelper
|
36
|
+
|
37
|
+
|
38
|
+
module CommandLineArgs extend OptiFlagSet
|
39
|
+
curr_env = Ec2onrails::Utils.rails_env
|
40
|
+
default_bucket = "#{curr_env}_backup"
|
41
|
+
|
42
|
+
flag "dir" do
|
43
|
+
description "the directory that will be tarred and compressed and put on S3 with the name DIR_#{Ec2onrails::Utils.hostname}_TIMESTAMP.tgz"
|
44
|
+
end
|
45
|
+
|
46
|
+
optional_flag "role" do
|
47
|
+
description "The role of this server, as defined by capistrano. ex. 'db', or 'app' If not used, will be applied to all roles"
|
48
|
+
end
|
49
|
+
|
50
|
+
optional_flag "only_env" do
|
51
|
+
description "Only apply the script if it is running within this environment"
|
52
|
+
end
|
53
|
+
|
54
|
+
optional_flag "bucket" do
|
55
|
+
description "The s3 bucket you would like to save this backup to. Will default to #{default_bucket}"
|
56
|
+
end
|
57
|
+
|
58
|
+
optional_switch_flag "v" do
|
59
|
+
description "let you know if the script stopped because it was running in either a different role or environment than the one specified"
|
60
|
+
end
|
61
|
+
|
62
|
+
and_process!
|
63
|
+
end
|
64
|
+
curr_env = Ec2onrails::Utils.rails_env
|
65
|
+
default_bucket = "#{curr_env}_backup"
|
66
|
+
|
67
|
+
verbose = ARGV.flags.v
|
68
|
+
dir = ARGV.flags.dir
|
69
|
+
bucket = ARGV.flags.bucket || default_bucket
|
70
|
+
curr_env = Ec2onrails::Utils.rails_env
|
71
|
+
default_bucket = "#{curr_env}_backup"
|
72
|
+
|
73
|
+
if ARGV.flags.role && !in_role?(ARGV.flags.role.sub(/^:/, '').to_sym)
|
74
|
+
puts "This script is not being run because the server is not running under the #{role} role" if verbose
|
75
|
+
exit
|
76
|
+
end
|
77
|
+
|
78
|
+
if ARGV.flags.only_env && ARGV.flags.only_env.strip.downcase != curr_env.strip.downcase
|
79
|
+
puts "This script is not being run because the server is not running under the #{curr_env} environment" if verbose
|
80
|
+
exit
|
81
|
+
end
|
82
|
+
|
83
|
+
if !dir || File.exists?(dir)
|
84
|
+
puts "The directory '#{dir}' does not exist. Please enter a valid, full path to a directory you would like backed up" if verbose
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
@s3 = Ec2onrails::S3Helper.new(bucket, dir)
|
89
|
+
@s3.store_dir(dir, :compress => true)
|