hennk-ec2onrails 0.9.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +0 -0
- data/COPYING +339 -0
- data/Manifest +148 -0
- data/README.textile +214 -0
- data/Rakefile +36 -0
- data/TODO +5 -0
- data/ec2onrails.gemspec +44 -0
- data/lib/ec2onrails.rb +20 -0
- data/lib/ec2onrails/capistrano_utils.rb +33 -0
- data/lib/ec2onrails/recipes.rb +460 -0
- data/lib/ec2onrails/version.rb +32 -0
- data/server/build-ec2onrails.sh +44 -0
- data/server/files/etc/aliases +6 -0
- data/server/files/etc/aliases.db +0 -0
- data/server/files/etc/apache2/apache2.conf +295 -0
- data/server/files/etc/apache2/conf.d/app.proxy_cluster.conf +7 -0
- data/server/files/etc/apache2/conf.d/app.proxy_frontend.conf +10 -0
- data/server/files/etc/apache2/mods-available/proxy.conf +18 -0
- data/server/files/etc/apache2/sites-available/app.common +58 -0
- data/server/files/etc/apache2/sites-available/app.custom +0 -0
- data/server/files/etc/apache2/sites-available/default +14 -0
- data/server/files/etc/apache2/sites-available/default-ssl +18 -0
- data/server/files/etc/cron.d/backup_app_db_to_s3 +6 -0
- data/server/files/etc/cron.daily/app +5 -0
- data/server/files/etc/cron.daily/logrotate_post +27 -0
- data/server/files/etc/cron.hourly/app +5 -0
- data/server/files/etc/cron.monthly/app +5 -0
- data/server/files/etc/cron.weekly/app +5 -0
- data/server/files/etc/denyhosts.conf +628 -0
- data/server/files/etc/dpkg/dpkg.cfg +13 -0
- data/server/files/etc/ec2onrails/balancer_members +6 -0
- data/server/files/etc/ec2onrails/roles.yml +5 -0
- data/server/files/etc/environment +2 -0
- data/server/files/etc/event.d/monit +13 -0
- data/server/files/etc/init.d/ec2-every-startup +29 -0
- data/server/files/etc/init.d/ec2-first-startup +36 -0
- data/server/files/etc/init.d/mongrel +91 -0
- data/server/files/etc/init.d/set_roles +3 -0
- data/server/files/etc/logrotate.d/apache2 +13 -0
- data/server/files/etc/logrotate.d/mysql-server +23 -0
- data/server/files/etc/logrotate.d/rails +7 -0
- data/server/files/etc/memcached.conf +47 -0
- data/server/files/etc/mongrel_cluster/app.yml +9 -0
- data/server/files/etc/monit/app.monitrc +71 -0
- data/server/files/etc/monit/db_primary.monitrc +10 -0
- data/server/files/etc/monit/memcache.monitrc +8 -0
- data/server/files/etc/monit/monitrc +13 -0
- data/server/files/etc/monit/system.monitrc +15 -0
- data/server/files/etc/monit/web.monitrc +10 -0
- data/server/files/etc/motd.tail +13 -0
- data/server/files/etc/mysql/my.cnf +149 -0
- data/server/files/etc/postfix/main.cf +4 -0
- data/server/files/etc/rc0.d/K10mongrel +1 -0
- data/server/files/etc/rc1.d/K10mongrel +1 -0
- data/server/files/etc/rc2.d/S90mongrel +1 -0
- data/server/files/etc/rc3.d/S90mongrel +1 -0
- data/server/files/etc/rc4.d/S90mongrel +1 -0
- data/server/files/etc/rc5.d/S90mongrel +1 -0
- data/server/files/etc/rc6.d/K10mongrel +1 -0
- data/server/files/etc/rcS.d/S91ec2-first-startup +1 -0
- data/server/files/etc/rcS.d/S92ec2-every-startup +1 -0
- data/server/files/etc/rcS.d/S99set_roles +1 -0
- data/server/files/etc/ssh/sshd_config +94 -0
- data/server/files/etc/sudoers +24 -0
- data/server/files/etc/syslog.conf +69 -0
- data/server/files/usr/local/ec2onrails/COPYING +339 -0
- data/server/files/usr/local/ec2onrails/bin/archive_file.rb +44 -0
- data/server/files/usr/local/ec2onrails/bin/backup_app_db.rb +83 -0
- data/server/files/usr/local/ec2onrails/bin/backup_files.rb +51 -0
- data/server/files/usr/local/ec2onrails/bin/mongrel_start +8 -0
- data/server/files/usr/local/ec2onrails/bin/mongrel_stop +8 -0
- data/server/files/usr/local/ec2onrails/bin/rails_env +35 -0
- data/server/files/usr/local/ec2onrails/bin/rebundle.sh +70 -0
- data/server/files/usr/local/ec2onrails/bin/restore_app_db.rb +58 -0
- data/server/files/usr/local/ec2onrails/bin/restore_files.rb +59 -0
- data/server/files/usr/local/ec2onrails/bin/set_rails_env +40 -0
- data/server/files/usr/local/ec2onrails/bin/set_roles.rb +156 -0
- data/server/files/usr/local/ec2onrails/config +30 -0
- data/server/files/usr/local/ec2onrails/lib/mysql_helper.rb +101 -0
- data/server/files/usr/local/ec2onrails/lib/s3_helper.rb +126 -0
- data/server/files/usr/local/ec2onrails/lib/utils.rb +16 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/every-startup/get-hostname.sh +25 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/create-dirs.sh +39 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/generate-default-web-cert-and-key.sh +49 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/misc.sh +30 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/prepare-mysql-data-dir.sh +24 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/setup_credentials.sh +33 -0
- data/server/rakefile.rb +246 -0
- data/setup.rb +1585 -0
- data/test/autobench.conf +60 -0
- data/test/spec/lib/s3_helper_spec.rb +134 -0
- data/test/spec/lib/s3_old.yml +3 -0
- data/test/spec/test_files/test1 +0 -0
- data/test/spec/test_files/test2 +0 -0
- data/test/test_app/Capfile +3 -0
- data/test/test_app/README +182 -0
- data/test/test_app/Rakefile +10 -0
- data/test/test_app/app/controllers/application.rb +7 -0
- data/test/test_app/app/controllers/db_fast_controller.rb +6 -0
- data/test/test_app/app/controllers/fast_controller.rb +5 -0
- data/test/test_app/app/controllers/slow_controller.rb +6 -0
- data/test/test_app/app/controllers/very_slow_controller.rb +6 -0
- data/test/test_app/app/helpers/application_helper.rb +3 -0
- data/test/test_app/app/helpers/db_fast_helper.rb +2 -0
- data/test/test_app/app/helpers/fast_helper.rb +2 -0
- data/test/test_app/app/helpers/slow_helper.rb +2 -0
- data/test/test_app/app/helpers/very_slow_helper.rb +2 -0
- data/test/test_app/config/boot.rb +109 -0
- data/test/test_app/config/database.yml +36 -0
- data/test/test_app/config/deploy.rb +21 -0
- data/test/test_app/config/environment.rb +60 -0
- data/test/test_app/config/environments/development.rb +21 -0
- data/test/test_app/config/environments/production.rb +18 -0
- data/test/test_app/config/environments/test.rb +19 -0
- data/test/test_app/config/routes.rb +27 -0
- data/test/test_app/db/schema.rb +7 -0
- data/test/test_app/doc/README_FOR_APP +2 -0
- data/test/test_app/public/404.html +30 -0
- data/test/test_app/public/500.html +30 -0
- data/test/test_app/public/dispatch.cgi +10 -0
- data/test/test_app/public/dispatch.fcgi +24 -0
- data/test/test_app/public/dispatch.rb +10 -0
- data/test/test_app/public/favicon.ico +0 -0
- data/test/test_app/public/images/rails.png +0 -0
- data/test/test_app/public/javascripts/application.js +2 -0
- data/test/test_app/public/javascripts/controls.js +963 -0
- data/test/test_app/public/javascripts/dragdrop.js +972 -0
- data/test/test_app/public/javascripts/effects.js +1120 -0
- data/test/test_app/public/javascripts/prototype.js +4225 -0
- data/test/test_app/public/robots.txt +1 -0
- data/test/test_app/script/about +3 -0
- data/test/test_app/script/breakpointer +3 -0
- data/test/test_app/script/console +3 -0
- data/test/test_app/script/destroy +3 -0
- data/test/test_app/script/generate +3 -0
- data/test/test_app/script/performance/benchmarker +3 -0
- data/test/test_app/script/performance/profiler +3 -0
- data/test/test_app/script/performance/request +3 -0
- data/test/test_app/script/plugin +3 -0
- data/test/test_app/script/process/inspector +3 -0
- data/test/test_app/script/process/reaper +3 -0
- data/test/test_app/script/process/spawner +3 -0
- data/test/test_app/script/runner +3 -0
- data/test/test_app/script/server +3 -0
- data/test/test_app/test/functional/db_fast_controller_test.rb +18 -0
- data/test/test_app/test/functional/fast_controller_test.rb +18 -0
- data/test/test_app/test/functional/slow_controller_test.rb +18 -0
- data/test/test_app/test/functional/very_slow_controller_test.rb +18 -0
- data/test/test_app/test/test_helper.rb +28 -0
- metadata +259 -0
@@ -0,0 +1,156 @@
|
|
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
|
+
|
22
|
+
require 'net/http'
|
23
|
+
require 'pp'
|
24
|
+
require 'socket'
|
25
|
+
require 'yaml'
|
26
|
+
|
27
|
+
def start(role, service, prog_name = service)
|
28
|
+
# ensure script is executable
|
29
|
+
run "chmod a+x /etc/init.d/#{service}"
|
30
|
+
# start service if not running:
|
31
|
+
unless (system("pidof -x #{prog_name}"))
|
32
|
+
run "sh /etc/init.d/#{service} start && sleep 30" # give the service 30 seconds to start before attempting to monitor it
|
33
|
+
end
|
34
|
+
run "monit -g #{role} monitor all"
|
35
|
+
end
|
36
|
+
|
37
|
+
def stop(role, service, prog_name = service)
|
38
|
+
run "monit -g #{role} unmonitor all"
|
39
|
+
if (system("pidof -x #{prog_name}"))
|
40
|
+
result = run("sh /etc/init.d/#{service} stop")
|
41
|
+
end
|
42
|
+
# make start script non-executable in case of reboot
|
43
|
+
run("chmod a-x /etc/init.d/#{service}")
|
44
|
+
end
|
45
|
+
|
46
|
+
def run(cmd)
|
47
|
+
result = system(cmd)
|
48
|
+
puts("*****ERROR: #{cmd} returned #{$?}") unless result
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_address_metadata(type)
|
52
|
+
address = Net::HTTP.get('169.254.169.254', "/2007-08-29/meta-data/#{type}").strip
|
53
|
+
raise "couldn't get instance data: #{type}" unless address =~ /\A\d+\.\d+\.\d+\.\d+\Z/
|
54
|
+
puts "#{type}: #{address}"
|
55
|
+
return address
|
56
|
+
end
|
57
|
+
|
58
|
+
def resolve(hostname)
|
59
|
+
address = IPSocket.getaddress(hostname).strip
|
60
|
+
if address == @local_address || address == @public_address
|
61
|
+
"127.0.0.1"
|
62
|
+
else
|
63
|
+
address
|
64
|
+
end
|
65
|
+
rescue Exception => e
|
66
|
+
puts "couldn't resolve hostname '#{hostname}'"
|
67
|
+
raise e
|
68
|
+
end
|
69
|
+
|
70
|
+
def resolve_all_addresses(original)
|
71
|
+
resolved = {}
|
72
|
+
original.each do |rolename, hostnames|
|
73
|
+
resolved[rolename] = hostnames.map{|hostname| resolve(hostname)} if hostnames
|
74
|
+
end
|
75
|
+
resolved
|
76
|
+
end
|
77
|
+
|
78
|
+
def in_role(role)
|
79
|
+
return false unless @roles[role]
|
80
|
+
return @roles[role].include?("127.0.0.1")
|
81
|
+
end
|
82
|
+
|
83
|
+
ROLES_FILE = "/etc/ec2onrails/roles.yml"
|
84
|
+
|
85
|
+
@local_address = get_address_metadata "local-ipv4"
|
86
|
+
@public_address = get_address_metadata "public-ipv4"
|
87
|
+
|
88
|
+
@roles = resolve_all_addresses(YAML::load_file(ROLES_FILE))
|
89
|
+
puts "Roles: "
|
90
|
+
pp @roles
|
91
|
+
|
92
|
+
|
93
|
+
#######################################
|
94
|
+
# TODO move these role definitions each into it's own file so adding a new
|
95
|
+
# role is just dropping a ruby file into a directory
|
96
|
+
|
97
|
+
# memcache role:
|
98
|
+
if in_role(:memcache)
|
99
|
+
puts "Starting memcache role..."
|
100
|
+
# increase memory size, etc if no other roles exist?
|
101
|
+
start(:memcache, "memcached")
|
102
|
+
else
|
103
|
+
puts "Stopping memcache role..."
|
104
|
+
stop(:memcache, "memcached")
|
105
|
+
end
|
106
|
+
|
107
|
+
# db primary role:
|
108
|
+
if in_role(:db_primary)
|
109
|
+
puts "Starting db_primary role..."
|
110
|
+
# increase caches, etc if no other roles exist?
|
111
|
+
start(:db_primary, "mysql", "mysqld")
|
112
|
+
else
|
113
|
+
puts "Stopping db_primary role..."
|
114
|
+
stop(:db_primary, "mysql", "mysqld")
|
115
|
+
end
|
116
|
+
|
117
|
+
# web role:
|
118
|
+
if in_role(:web)
|
119
|
+
puts "Starting web role..."
|
120
|
+
balancer_members = File.open("/etc/ec2onrails/balancer_members", "w") do |f|
|
121
|
+
@roles[:app].each do |address|
|
122
|
+
(8000..8005).each do |port|
|
123
|
+
f << "BalancerMember http://#{address}:#{port}\n"
|
124
|
+
end
|
125
|
+
f << "\n"
|
126
|
+
end
|
127
|
+
end
|
128
|
+
start(:web, "apache2")
|
129
|
+
# Force apache to reload config files in case it was already running and app hosts changed.
|
130
|
+
run "/etc/init.d/apache2 reload"
|
131
|
+
else
|
132
|
+
puts "Stopping web role..."
|
133
|
+
stop(:web, "apache2")
|
134
|
+
end
|
135
|
+
|
136
|
+
# app role:
|
137
|
+
if in_role(:app)
|
138
|
+
puts "Starting app role..."
|
139
|
+
|
140
|
+
if @roles[:db_primary]
|
141
|
+
# edit /etc/hosts, need db_primary address & db_slave address
|
142
|
+
db_primary_addr = @roles[:db_primary][0]
|
143
|
+
puts "db_primary has ip address: #{db_primary_addr}"
|
144
|
+
|
145
|
+
# TODO just use ruby here...
|
146
|
+
run("cp /etc/hosts.original /etc/hosts")
|
147
|
+
run("echo '\n#{db_primary_addr}\tdb_primary\n' >> /etc/hosts")
|
148
|
+
end
|
149
|
+
|
150
|
+
start(:app, "mongrel", "mongrel_rails")
|
151
|
+
else
|
152
|
+
puts "Stopping app role..."
|
153
|
+
stop(:app, "mongrel", "mongrel_rails")
|
154
|
+
end
|
155
|
+
|
156
|
+
#######################################
|
@@ -0,0 +1,30 @@
|
|
1
|
+
############################################################
|
2
|
+
# This script is sourced by .bashrc and by /usr/local/ec2onrails/rebundle.sh
|
3
|
+
# You shouldn't need to change anything in here
|
4
|
+
|
5
|
+
if [ -r /mnt/aws-config/config ] ; then
|
6
|
+
. /mnt/aws-config/config
|
7
|
+
export EC2_PRIVATE_KEY="/mnt/aws-config/$KEY_FILE_NAME"
|
8
|
+
export EC2_CERT="/mnt/aws-config/$CERT_FILE_NAME"
|
9
|
+
|
10
|
+
fi
|
11
|
+
|
12
|
+
export EC2_HOME=/usr/local/ec2-api-tools
|
13
|
+
export PATH="$PATH:$EC2_HOME/bin"
|
14
|
+
|
15
|
+
export JAVA_HOME="/usr/lib/jvm/java-6-sun"
|
16
|
+
|
17
|
+
# Set the bash prompt to include the rails environment name
|
18
|
+
case "$TERM" in
|
19
|
+
xterm-color)
|
20
|
+
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]-\[\033[01;35m\]`/usr/local/ec2onrails/bin/rails_env`\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
|
21
|
+
;;
|
22
|
+
*)
|
23
|
+
PS1='${debian_chroot:+($debian_chroot)}\u@\h-`/usr/local/ec2onrails/bin/rails_env`:\w\$ '
|
24
|
+
;;
|
25
|
+
esac
|
26
|
+
|
27
|
+
# Set some useful aliases
|
28
|
+
alias app="pushd /mnt/app/current && sudo su app && popd"
|
29
|
+
alias log="tail -f /mnt/app/current/log/`/usr/local/ec2onrails/bin/rails_env`.log"
|
30
|
+
alias logs="tail -f /mnt/app/current/log/*.log /mnt/log/apache2/*.log /mnt/log/syslog"
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# This file is part of EC2 on Rails.
|
2
|
+
# http://rubyforge.org/projects/ec2onrails/
|
3
|
+
#
|
4
|
+
# Copyright 2007 Paul Dowman, http://pauldowman.com/
|
5
|
+
#
|
6
|
+
# EC2 on Rails is free software; you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU General Public License as published by
|
8
|
+
# the Free Software Foundation; either version 2 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# EC2 on Rails is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU General Public License
|
17
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
|
19
|
+
require 'rubygems'
|
20
|
+
require 'aws/s3'
|
21
|
+
require 'yaml'
|
22
|
+
require 'erb'
|
23
|
+
require 'fileutils'
|
24
|
+
require "#{File.dirname(__FILE__)}/utils"
|
25
|
+
|
26
|
+
module Ec2onrails
|
27
|
+
class MysqlHelper
|
28
|
+
|
29
|
+
DEFAULT_CONFIG_FILE = "/mnt/app/current/config/database.yml"
|
30
|
+
|
31
|
+
attr_accessor :database
|
32
|
+
attr_accessor :user
|
33
|
+
attr_accessor :password
|
34
|
+
|
35
|
+
def initialize(config_file = DEFAULT_CONFIG_FILE, rails_env = Utils.rails_env)
|
36
|
+
@rails_env = rails_env
|
37
|
+
load_db_config(config_file)
|
38
|
+
end
|
39
|
+
|
40
|
+
def load_db_config(config_file)
|
41
|
+
db_config = YAML::load(ERB.new(File.read(config_file)).result)
|
42
|
+
if db_config && db_config[@rails_env].nil?
|
43
|
+
puts "the rails environment '#{@rails_env}' was not found in this db onfig file: #{config_file}"
|
44
|
+
end
|
45
|
+
db_config = db_config[@rails_env]
|
46
|
+
@database = db_config['database']
|
47
|
+
@user = db_config['username']
|
48
|
+
@password = db_config['password']
|
49
|
+
end
|
50
|
+
|
51
|
+
def execute_sql(sql)
|
52
|
+
raise "@user not set" unless @user
|
53
|
+
raise "sql not given" unless sql
|
54
|
+
cmd = %{mysql -u #{@user} -e "#{sql}"}
|
55
|
+
cmd += " -p'#{@password}' " unless @password.nil?
|
56
|
+
Utils.run cmd
|
57
|
+
end
|
58
|
+
|
59
|
+
def execute
|
60
|
+
require "mysql"
|
61
|
+
|
62
|
+
begin
|
63
|
+
# connect to the MySQL server
|
64
|
+
dbh = Mysql.real_connect("localhost", "#{@user}", "#{@password}", "#{@database}")
|
65
|
+
yield dbh
|
66
|
+
rescue Mysql::Error => e
|
67
|
+
puts "Error code: #{e.errno}"
|
68
|
+
puts "Error message: #{e.error}"
|
69
|
+
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
|
70
|
+
ensure
|
71
|
+
# disconnect from server
|
72
|
+
dbh.close if dbh
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
def dump(out_file, reset_logs)
|
79
|
+
cmd = "mysqldump --quick --single-transaction --create-options -u#{@user} "
|
80
|
+
if reset_logs
|
81
|
+
cmd += " --flush-logs --master-data=2 --delete-master-logs "
|
82
|
+
end
|
83
|
+
cmd += " -p'#{@password}' " unless @password.nil?
|
84
|
+
cmd += " #{@database} | gzip > #{out_file}"
|
85
|
+
Utils.run cmd
|
86
|
+
end
|
87
|
+
|
88
|
+
def load_from_dump(in_file)
|
89
|
+
cmd = "gunzip -c #{in_file} | mysql -u#{@user} "
|
90
|
+
cmd += " -p'#{@password}' " unless @password.nil?
|
91
|
+
cmd += " #{@database}"
|
92
|
+
Utils.run cmd
|
93
|
+
end
|
94
|
+
|
95
|
+
def execute_binary_log(log_file)
|
96
|
+
cmd = "mysqlbinlog --database=#{@database} #{log_file} | mysql -u#{@user} "
|
97
|
+
cmd += " -p'#{@password}' " unless @password.nil?
|
98
|
+
Utils.run cmd
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# This file is part of EC2 on Rails.
|
2
|
+
# http://rubyforge.org/projects/ec2onrails/
|
3
|
+
#
|
4
|
+
# Copyright 2007 Paul Dowman, http://pauldowman.com/
|
5
|
+
#
|
6
|
+
# EC2 on Rails is free software; you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU General Public License as published by
|
8
|
+
# the Free Software Foundation; either version 2 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# EC2 on Rails is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU General Public License
|
17
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
|
19
|
+
require 'rubygems'
|
20
|
+
require 'aws/s3'
|
21
|
+
require 'yaml'
|
22
|
+
require 'erb'
|
23
|
+
require 'fileutils'
|
24
|
+
require "#{File.dirname(__FILE__)}/utils"
|
25
|
+
|
26
|
+
module Ec2onrails
|
27
|
+
class S3Helper
|
28
|
+
|
29
|
+
DEFAULT_CONFIG_FILE = "/mnt/app/current/config/s3.yml"
|
30
|
+
|
31
|
+
# make attributes available for specs
|
32
|
+
attr_accessor :bucket
|
33
|
+
attr_accessor :dir
|
34
|
+
attr_accessor :config_file
|
35
|
+
attr_accessor :rails_env
|
36
|
+
attr_accessor :aws_access_key
|
37
|
+
attr_accessor :aws_secret_access_key
|
38
|
+
attr_accessor :bucket
|
39
|
+
|
40
|
+
def initialize(bucket, dir, config_file = DEFAULT_CONFIG_FILE, rails_env = Utils.rails_env)
|
41
|
+
@dir = dir
|
42
|
+
@config_file = config_file
|
43
|
+
@rails_env = rails_env
|
44
|
+
load_s3_config
|
45
|
+
@bucket = bucket || "#{@bucket_base_name}-#{Ec2onrails::Utils.hostname}"
|
46
|
+
AWS::S3::Base.establish_connection!(:access_key_id => @aws_access_key, :secret_access_key => @aws_secret_access_key, :use_ssl => true)
|
47
|
+
end
|
48
|
+
|
49
|
+
def load_s3_config
|
50
|
+
if File.exists?(@config_file)
|
51
|
+
s3_config = YAML::load(ERB.new(File.read(@config_file)).result)
|
52
|
+
|
53
|
+
# try to load the section for the current RAILS_ENV
|
54
|
+
section = s3_config[@rails_env]
|
55
|
+
if section.nil?
|
56
|
+
# fall back to keys at the root of the tree
|
57
|
+
section = s3_config
|
58
|
+
end
|
59
|
+
|
60
|
+
@aws_access_key = section['aws_access_key']
|
61
|
+
@aws_secret_access_key = section['aws_secret_access_key']
|
62
|
+
@bucket_base_name = section['bucket_base_name']
|
63
|
+
else
|
64
|
+
if !File.exists?('/mnt/aws-config/config')
|
65
|
+
raise "Can't find either #{@config_file} or /mnt/aws-config/config"
|
66
|
+
end
|
67
|
+
@aws_access_key = get_bash_config('AWS_ACCESS_KEY_ID')
|
68
|
+
@aws_secret_access_key = get_bash_config('AWS_SECRET_ACCESS_KEY')
|
69
|
+
@bucket_base_name = get_bash_config('BUCKET_BASE_NAME')
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def create_bucket
|
74
|
+
retries = 0
|
75
|
+
begin
|
76
|
+
AWS::S3::Bucket.find(@bucket)
|
77
|
+
rescue AWS::S3::NoSuchBucket
|
78
|
+
AWS::S3::Bucket.create(@bucket)
|
79
|
+
sleep 1 # If we try to use the bucket too quickly sometimes it's not found
|
80
|
+
retry if (retries += 1) < 15
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def store_file(file)
|
85
|
+
create_bucket
|
86
|
+
AWS::S3::S3Object.store(s3_key(file), open(file), @bucket)
|
87
|
+
end
|
88
|
+
|
89
|
+
def retrieve_file(file)
|
90
|
+
key = s3_key(file)
|
91
|
+
AWS::S3::S3Object.find(key, @bucket)
|
92
|
+
open(file, 'w') do |f|
|
93
|
+
AWS::S3::S3Object.stream(key, @bucket) do |chunk|
|
94
|
+
f.write chunk
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def list_keys(filename_prefix)
|
100
|
+
prefix = @dir ? "#{@dir}/#{filename_prefix}" : filename_prefix
|
101
|
+
AWS::S3::Bucket.objects(@bucket, :prefix => prefix).collect{|obj| obj.key}
|
102
|
+
end
|
103
|
+
|
104
|
+
def retrieve_files(filename_prefix, local_dir)
|
105
|
+
list_keys(filename_prefix).each do |k|
|
106
|
+
file = "#{local_dir}/#{File.basename(k)}"
|
107
|
+
retrieve_file(file)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def delete_files(filename_prefix)
|
112
|
+
list_keys(filename_prefix).each do |k|
|
113
|
+
AWS::S3::S3Object.delete(k, @bucket)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def s3_key(file)
|
118
|
+
@dir ? "#{@dir}/#{File.basename(file)}" : File.basename(file)
|
119
|
+
end
|
120
|
+
|
121
|
+
# load an env value from the shared config file
|
122
|
+
def get_bash_config(name)
|
123
|
+
`bash -c 'source /mnt/aws-config/config; echo $#{name}'`.strip
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Ec2onrails
|
2
|
+
module Utils
|
3
|
+
def self.run(command)
|
4
|
+
result = system command
|
5
|
+
raise("error: #{$?}") unless result
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.rails_env
|
9
|
+
`/usr/local/ec2onrails/bin/rails_env`.strip
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.hostname
|
13
|
+
`hostname -s`.strip
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,25 @@
|
|
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
|
+
# Set the hostname to this instance's public hostname
|
22
|
+
|
23
|
+
hostname `curl http://169.254.169.254/latest/meta-data/public-hostname`
|
24
|
+
sudo monit reload
|
25
|
+
|