gigpark-ec2onrails 0.9.10.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +190 -0
- data/COPYING +339 -0
- data/Manifest +160 -0
- data/README.textile +233 -0
- data/Rakefile +31 -0
- data/TODO +104 -0
- data/ec2onrails.gemspec +46 -0
- data/echoe_config.rb +27 -0
- data/examples/Capfile +3 -0
- data/examples/deploy.rb +103 -0
- data/examples/s3.yml +9 -0
- data/lib/ec2onrails.rb +20 -0
- data/lib/ec2onrails/capistrano_utils.rb +47 -0
- data/lib/ec2onrails/recipes.rb +146 -0
- data/lib/ec2onrails/recipes/db.rb +366 -0
- data/lib/ec2onrails/recipes/deploy.rb +26 -0
- data/lib/ec2onrails/recipes/server.rb +517 -0
- data/lib/ec2onrails/version.rb +29 -0
- data/server/build +74 -0
- data/server/files/etc/README +7 -0
- data/server/files/etc/aliases +5 -0
- data/server/files/etc/cron.d/ec2onrails +16 -0
- data/server/files/etc/cron.daily/app +31 -0
- data/server/files/etc/cron.daily/logrotate_post +25 -0
- data/server/files/etc/cron.hourly/app +31 -0
- data/server/files/etc/cron.monthly/app +31 -0
- data/server/files/etc/cron.weekly/app +31 -0
- data/server/files/etc/default/varnish +33 -0
- data/server/files/etc/default/varnishncsa +11 -0
- data/server/files/etc/denyhosts.conf +628 -0
- data/server/files/etc/dpkg/dpkg.cfg +13 -0
- data/server/files/etc/ec2onrails/rails_env +1 -0
- data/server/files/etc/ec2onrails/roles.yml +5 -0
- data/server/files/etc/environment +2 -0
- data/server/files/etc/event.d/god +8 -0
- data/server/files/etc/god/db_primary.god +16 -0
- data/server/files/etc/god/dkim_filter.god +17 -0
- data/server/files/etc/god/master.conf +40 -0
- data/server/files/etc/god/memcache.god +15 -0
- data/server/files/etc/god/notifications.god +11 -0
- data/server/files/etc/god/proxy.god +40 -0
- data/server/files/etc/god/system.god +37 -0
- data/server/files/etc/god/web.god +23 -0
- data/server/files/etc/init.d/ec2-every-startup +29 -0
- data/server/files/etc/init.d/ec2-first-startup +37 -0
- data/server/files/etc/init.d/nginx +78 -0
- data/server/files/etc/logrotate.d/nginx +10 -0
- data/server/files/etc/logrotate.d/rails +8 -0
- data/server/files/etc/logrotate.d/varnish +14 -0
- data/server/files/etc/memcached.conf +47 -0
- data/server/files/etc/motd.tail +13 -0
- data/server/files/etc/mysql/my.cnf +152 -0
- data/server/files/etc/nginx/conf.d/custom.conf +1 -0
- data/server/files/etc/nginx/nginx.conf.erb +152 -0
- data/server/files/etc/postfix/main.cf +4 -0
- data/server/files/etc/ssh/sshd_config +96 -0
- data/server/files/etc/sudoers +31 -0
- data/server/files/etc/syslog.conf +63 -0
- data/server/files/etc/varnish/default.vcl.erb +140 -0
- data/server/files/usr/local/ec2onrails/COPYING +339 -0
- data/server/files/usr/local/ec2onrails/bin/archive_file +44 -0
- data/server/files/usr/local/ec2onrails/bin/backup_app_db +160 -0
- data/server/files/usr/local/ec2onrails/bin/backup_dir +89 -0
- data/server/files/usr/local/ec2onrails/bin/ec2_meta_data +80 -0
- data/server/files/usr/local/ec2onrails/bin/exec_runner +76 -0
- data/server/files/usr/local/ec2onrails/bin/in_role +33 -0
- data/server/files/usr/local/ec2onrails/bin/init_services +43 -0
- data/server/files/usr/local/ec2onrails/bin/install_system_files +56 -0
- data/server/files/usr/local/ec2onrails/bin/optimize_mysql +346 -0
- data/server/files/usr/local/ec2onrails/bin/public-hostname +24 -0
- data/server/files/usr/local/ec2onrails/bin/rails_env +33 -0
- data/server/files/usr/local/ec2onrails/bin/rebundle +62 -0
- data/server/files/usr/local/ec2onrails/bin/restore_app_db +58 -0
- data/server/files/usr/local/ec2onrails/bin/set_rails_env +36 -0
- data/server/files/usr/local/ec2onrails/bin/set_roles +36 -0
- data/server/files/usr/local/ec2onrails/bin/uninstall_system_files +34 -0
- data/server/files/usr/local/ec2onrails/config +30 -0
- data/server/files/usr/local/ec2onrails/lib/aws_helper.rb +75 -0
- data/server/files/usr/local/ec2onrails/lib/god_helper.rb +84 -0
- data/server/files/usr/local/ec2onrails/lib/mysql_helper.rb +100 -0
- data/server/files/usr/local/ec2onrails/lib/roles_helper.rb +156 -0
- data/server/files/usr/local/ec2onrails/lib/s3_helper.rb +116 -0
- data/server/files/usr/local/ec2onrails/lib/system_files_helper.rb +110 -0
- data/server/files/usr/local/ec2onrails/lib/system_files_manifest.rb +107 -0
- data/server/files/usr/local/ec2onrails/lib/utils.rb +26 -0
- data/server/files/usr/local/ec2onrails/lib/vendor/ini.rb +268 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/every-startup/README +5 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/every-startup/create-mysqld-pid-dir +26 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/README +5 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/create-dirs +45 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/generate-default-web-cert-and-key +49 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/get-hostname +27 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/misc +27 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/prepare-mysql-data-dir +24 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/setup-credentials +32 -0
- data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/setup-file-permissions +30 -0
- data/server/rakefile-wrapper +26 -0
- data/server/rakefile.rb +267 -0
- data/test/autobench.conf +60 -0
- data/test/spec/lib/s3_helper_spec.rb +154 -0
- data/test/spec/lib/s3_old.yml +3 -0
- data/test/spec/lib/system_files_manifest_spec.rb +122 -0
- data/test/spec/test_files/system_files1/_manifest +13 -0
- data/test/spec/test_files/system_files1/test1 +0 -0
- data/test/spec/test_files/system_files1/test2 +0 -0
- data/test/spec/test_files/system_files1/testfolder/test3 +0 -0
- data/test/spec/test_files/system_files2/_manifest +2 -0
- data/test/spec/test_files/system_files2/test1 +0 -0
- data/test/spec/test_files/system_files2/test2 +0 -0
- data/test/spec/test_files/system_files2/testfolder/test3 +0 -0
- data/test/spec/test_files/test2 +0 -0
- data/test/test_app/Capfile +3 -0
- data/test/test_app/README +243 -0
- data/test/test_app/Rakefile +10 -0
- data/test/test_app/app/controllers/application_controller.rb +10 -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/config/boot.rb +110 -0
- data/test/test_app/config/database.yml +24 -0
- data/test/test_app/config/deploy.rb +22 -0
- data/test/test_app/config/ec2onrails/config.rb +5 -0
- data/test/test_app/config/environment.rb +41 -0
- data/test/test_app/config/environments/development.rb +17 -0
- data/test/test_app/config/environments/production.rb +28 -0
- data/test/test_app/config/environments/test.rb +28 -0
- data/test/test_app/config/initializers/backtrace_silencers.rb +7 -0
- data/test/test_app/config/initializers/inflections.rb +10 -0
- data/test/test_app/config/initializers/mime_types.rb +5 -0
- data/test/test_app/config/initializers/new_rails_defaults.rb +19 -0
- data/test/test_app/config/initializers/session_store.rb +15 -0
- data/test/test_app/config/locales/en.yml +5 -0
- data/test/test_app/config/routes.rb +43 -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/422.html +30 -0
- data/test/test_app/public/500.html +30 -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/index.html +275 -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 +973 -0
- data/test/test_app/public/javascripts/effects.js +1128 -0
- data/test/test_app/public/javascripts/prototype.js +4320 -0
- data/test/test_app/public/robots.txt +5 -0
- data/test/test_app/script/about +4 -0
- data/test/test_app/script/console +3 -0
- data/test/test_app/script/dbconsole +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/plugin +3 -0
- data/test/test_app/script/runner +3 -0
- data/test/test_app/script/server +3 -0
- data/test/test_app/test/performance/browsing_test.rb +9 -0
- data/test/test_app/test/test_helper.rb +38 -0
- metadata +266 -0
@@ -0,0 +1,100 @@
|
|
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 'yaml'
|
21
|
+
require 'erb'
|
22
|
+
require 'fileutils'
|
23
|
+
require "#{File.dirname(__FILE__)}/utils"
|
24
|
+
|
25
|
+
module Ec2onrails
|
26
|
+
class MysqlHelper
|
27
|
+
|
28
|
+
DEFAULT_CONFIG_FILE = "/mnt/app/current/config/database.yml"
|
29
|
+
|
30
|
+
attr_accessor :database
|
31
|
+
attr_accessor :user
|
32
|
+
attr_accessor :password
|
33
|
+
|
34
|
+
def initialize(config_file = DEFAULT_CONFIG_FILE, rails_env = Utils.rails_env)
|
35
|
+
@rails_env = rails_env
|
36
|
+
load_db_config(config_file)
|
37
|
+
end
|
38
|
+
|
39
|
+
def load_db_config(config_file)
|
40
|
+
db_config = YAML::load(ERB.new(File.read(config_file)).result)
|
41
|
+
if db_config && db_config[@rails_env].nil?
|
42
|
+
puts "the rails environment '#{@rails_env}' was not found in this db config file: #{config_file}"
|
43
|
+
end
|
44
|
+
db_config = db_config[@rails_env]
|
45
|
+
@database = db_config['database']
|
46
|
+
@user = db_config['username']
|
47
|
+
@password = db_config['password']
|
48
|
+
end
|
49
|
+
|
50
|
+
def execute_sql(sql)
|
51
|
+
raise "@user not set" unless @user
|
52
|
+
raise "sql not given" unless sql
|
53
|
+
cmd = %{mysql -u #{@user} -e "#{sql}"}
|
54
|
+
cmd += " -p'#{@password}' " unless @password.nil?
|
55
|
+
Utils.run cmd
|
56
|
+
end
|
57
|
+
|
58
|
+
def execute
|
59
|
+
require "mysql"
|
60
|
+
|
61
|
+
begin
|
62
|
+
# connect to the MySQL server
|
63
|
+
dbh = Mysql.real_connect("localhost", "#{@user}", "#{@password}", "#{@database}")
|
64
|
+
yield dbh
|
65
|
+
rescue Mysql::Error => e
|
66
|
+
puts "Error code: #{e.errno}"
|
67
|
+
puts "Error message: #{e.error}"
|
68
|
+
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
|
69
|
+
ensure
|
70
|
+
# disconnect from server
|
71
|
+
dbh.close if dbh
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
def dump(out_file, reset_logs)
|
78
|
+
cmd = "mysqldump --quick --single-transaction --create-options -u#{@user} "
|
79
|
+
if reset_logs
|
80
|
+
cmd += " --flush-logs --master-data=2 --delete-master-logs "
|
81
|
+
end
|
82
|
+
cmd += " -p'#{@password}' " unless @password.nil?
|
83
|
+
cmd += " #{@database} | gzip > #{out_file}"
|
84
|
+
Utils.run cmd
|
85
|
+
end
|
86
|
+
|
87
|
+
def load_from_dump(in_file)
|
88
|
+
cmd = "gunzip -c #{in_file} | mysql -u#{@user} "
|
89
|
+
cmd += " -p'#{@password}' " unless @password.nil?
|
90
|
+
cmd += " #{@database}"
|
91
|
+
Utils.run cmd
|
92
|
+
end
|
93
|
+
|
94
|
+
def execute_binary_log(log_file)
|
95
|
+
cmd = "mysqlbinlog --database=#{@database} #{log_file} | mysql -u#{@user} "
|
96
|
+
cmd += " -p'#{@password}' " unless @password.nil?
|
97
|
+
Utils.run cmd
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,156 @@
|
|
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 'erb'
|
20
|
+
require 'fileutils'
|
21
|
+
require 'net/http'
|
22
|
+
require 'pp'
|
23
|
+
require 'resolv'
|
24
|
+
require 'socket'
|
25
|
+
require 'yaml'
|
26
|
+
require "#{File.dirname(__FILE__)}/utils"
|
27
|
+
|
28
|
+
module Ec2onrails
|
29
|
+
module RolesHelper
|
30
|
+
|
31
|
+
ROLES_FILE = "/etc/ec2onrails/roles.yml"
|
32
|
+
|
33
|
+
def local_address
|
34
|
+
@local_address ||= get_metadata "local-ipv4"
|
35
|
+
end
|
36
|
+
|
37
|
+
def public_address
|
38
|
+
@public_address ||= get_metadata "public-ipv4"
|
39
|
+
end
|
40
|
+
|
41
|
+
def roles
|
42
|
+
@roles ||= resolve_all_addresses(YAML::load_file(ROLES_FILE))
|
43
|
+
end
|
44
|
+
|
45
|
+
def start(role)
|
46
|
+
puts "STARTING #{role} role"
|
47
|
+
sudo "god start #{role}"
|
48
|
+
end
|
49
|
+
|
50
|
+
def stop(role)
|
51
|
+
puts "STOPING #{role} role"
|
52
|
+
sudo "god stop #{role}"
|
53
|
+
end
|
54
|
+
|
55
|
+
def run(cmd)
|
56
|
+
result = system(cmd)
|
57
|
+
puts("*****ERROR: #{cmd} returned #{$?}") unless result
|
58
|
+
end
|
59
|
+
|
60
|
+
def sudo(cmd)
|
61
|
+
run("sudo #{cmd}")
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_metadata(type)
|
65
|
+
data = Net::HTTP.get('169.254.169.254', "/latest/meta-data/#{type}").strip
|
66
|
+
|
67
|
+
raise "couldn't get instance data: #{type}" if data.nil? || data.strip.length == 0
|
68
|
+
# puts "#{type}: #{address}"
|
69
|
+
return data
|
70
|
+
end
|
71
|
+
|
72
|
+
def resolve(hostname)
|
73
|
+
# hostname might be an alias, so get the .amazonaws.com hostname
|
74
|
+
canonical_name = Resolv.getname(IPSocket.getaddress(hostname))
|
75
|
+
|
76
|
+
# EC2's internal DNS resolves the external hostnames (*.amazonaws.com) into internal IP addresses
|
77
|
+
address = IPSocket.getaddress(canonical_name).strip
|
78
|
+
|
79
|
+
if address == local_address || address == public_address
|
80
|
+
"127.0.0.1"
|
81
|
+
else
|
82
|
+
address
|
83
|
+
end
|
84
|
+
rescue Exception => e
|
85
|
+
puts "couldn't resolve hostname '#{hostname}'"
|
86
|
+
raise e
|
87
|
+
end
|
88
|
+
|
89
|
+
def resolve_all_addresses(original)
|
90
|
+
resolved = {}
|
91
|
+
original.each do |rolename, hostnames|
|
92
|
+
resolved[rolename] = hostnames.map{|hostname| resolve(hostname)} if hostnames
|
93
|
+
end
|
94
|
+
resolved
|
95
|
+
end
|
96
|
+
|
97
|
+
def in_role?(role)
|
98
|
+
return false unless roles[role]
|
99
|
+
return roles[role].include?("127.0.0.1")
|
100
|
+
end
|
101
|
+
#to provide deprecated usage
|
102
|
+
alias :in_role :in_role?
|
103
|
+
|
104
|
+
# Re-write the roles file with the hostnames resolved
|
105
|
+
def resolve_hostnames_in_roles_file
|
106
|
+
File.open(ROLES_FILE, 'w') {|f| YAML.dump(roles, f)}
|
107
|
+
end
|
108
|
+
|
109
|
+
# write a hostname alias for each host. The hostnames will be "rolename-n" where n
|
110
|
+
# is an integer
|
111
|
+
def set_hostnames
|
112
|
+
hosts_file = "/etc/hosts"
|
113
|
+
FileUtils.cp "#{hosts_file}.original", hosts_file
|
114
|
+
File.open(hosts_file, 'a') do |f|
|
115
|
+
f << "\n"
|
116
|
+
f << "# The following is automatically added by the EC2 on Rail set_roles script:\n"
|
117
|
+
roles.each do |rolename, addresses|
|
118
|
+
addresses.each_with_index do |address, i|
|
119
|
+
f << "#{address} #{rolename.to_s.gsub(/_/, "-")}-#{i+1}\n"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# Eventually we'll remove this:
|
125
|
+
if roles[:db_primary]
|
126
|
+
db_primary_addr = roles[:db_primary][0]
|
127
|
+
File.open(hosts_file, 'a') do |f|
|
128
|
+
f << "# DEPRECATED: this is here for backwards compatibility, eventually it will be removed:\n"
|
129
|
+
f << "#{db_primary_addr} db_primary\n"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# Process any ERB template under /etc
|
135
|
+
# The output from "filename.erb" will be saved as "filename"
|
136
|
+
def process_config_file_templates
|
137
|
+
# Set any variables that will be needed inside the templates
|
138
|
+
# We're processing ALL templates, even ones that won't be used in the current role.
|
139
|
+
rails_env = Ec2onrails::Utils.rails_env
|
140
|
+
config = Ec2onrails::Utils.load_config
|
141
|
+
roles = self.roles
|
142
|
+
|
143
|
+
Dir["/etc/**/*.erb"].each do |template|
|
144
|
+
puts "Processing config file template: #{template}..."
|
145
|
+
STDOUT.flush
|
146
|
+
# Use '<>' for ERB formatting options, it's safer than '%' but still allows lines that are ruby-only to be
|
147
|
+
# omitted from the output.
|
148
|
+
# For all options see: http://stdlib.rubyonrails.org/libdoc/erb/rdoc/classes/ERB.html#M000684
|
149
|
+
contents = ERB.new(IO.read(template), nil, "<>").result(binding)
|
150
|
+
output_file = template.sub(/\.erb$/, '')
|
151
|
+
File.open(output_file, 'w'){|f| f << contents}
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
end
|
@@ -0,0 +1,116 @@
|
|
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 'right_aws'
|
21
|
+
require 'yaml'
|
22
|
+
require 'erb'
|
23
|
+
require 'fileutils'
|
24
|
+
require "#{File.dirname(__FILE__)}/utils"
|
25
|
+
require "#{File.dirname(__FILE__)}/aws_helper"
|
26
|
+
|
27
|
+
|
28
|
+
# Hack to get rid of the "warning: peer certificate won't be verified in this SSL session" message
|
29
|
+
# See http://www.5dollarwhitebox.org/drupal/node/64
|
30
|
+
class Net::HTTP
|
31
|
+
alias_method :old_initialize, :initialize
|
32
|
+
def initialize(*args)
|
33
|
+
old_initialize(*args)
|
34
|
+
@ssl_context = OpenSSL::SSL::SSLContext.new
|
35
|
+
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
module Ec2onrails
|
41
|
+
class S3Helper
|
42
|
+
SCRATCH_SPACE = '/mnt/tmp'
|
43
|
+
|
44
|
+
# make attributes available for specs
|
45
|
+
attr_accessor :dir
|
46
|
+
attr_accessor :config_file
|
47
|
+
attr_accessor :rails_env
|
48
|
+
attr_accessor :aws_access_key
|
49
|
+
attr_accessor :aws_secret_access_key
|
50
|
+
attr_accessor :bucket_name
|
51
|
+
|
52
|
+
def initialize(bucket_name, dir, config_file = Ec2onrails::AwsHelper.default_config_file, rails_env = Utils.rails_env)
|
53
|
+
@dir = dir
|
54
|
+
@config_file = config_file
|
55
|
+
@rails_env = rails_env
|
56
|
+
@awsHelper = Ec2onrails::AwsHelper.new(config_file, rails_env)
|
57
|
+
@aws_access_key = @awsHelper.aws_access_key
|
58
|
+
@aws_secret_access_key = @awsHelper.aws_secret_access_key
|
59
|
+
@bucket_base_name = @awsHelper.bucket_base_name
|
60
|
+
@bucket_name = bucket_name || "#{@bucket_base_name}-#{Ec2onrails::Utils.hostname}"
|
61
|
+
logger = Logger.new(STDOUT)
|
62
|
+
logger.level = Logger::ERROR
|
63
|
+
s3 = RightAws::S3.new(@aws_access_key, @aws_secret_access_key, :logger => logger)
|
64
|
+
@bucket = s3.bucket(@bucket_name, true)
|
65
|
+
end
|
66
|
+
|
67
|
+
def store_file(filename)
|
68
|
+
@bucket.put(s3_key(filename), File.read(filename))
|
69
|
+
end
|
70
|
+
|
71
|
+
def store_dir(dir, options={})
|
72
|
+
FileUtils.mkdir_p SCRATCH_SPACE
|
73
|
+
compress = options[:compress]
|
74
|
+
exclude = options[:exclude]
|
75
|
+
|
76
|
+
#should be of the format:
|
77
|
+
# mnt-app-shared_ec2-75-101-250-19__20090217-183411.tgz
|
78
|
+
archive_nm = "#{Ec2onrails::Utils.hostname}__#{Time.new.strftime('%Y%m%d-%H%M%S')}"
|
79
|
+
archive_nm += compress ? ".tgz" : '.tar'
|
80
|
+
cmd = "cd #{SCRATCH_SPACE} && tar -cph"
|
81
|
+
cmd += 'z' if compress
|
82
|
+
cmd += "f #{archive_nm} -C / #{dir[1..-1]} "
|
83
|
+
cmd += " --exclude=#{exclude} " if exclude
|
84
|
+
system(cmd)
|
85
|
+
file = "#{SCRATCH_SPACE}/#{archive_nm}"
|
86
|
+
@bucket.put(s3_key(archive_nm), File.read(file))
|
87
|
+
ensure
|
88
|
+
system "nice -n 15 rm -f #{file}"
|
89
|
+
end
|
90
|
+
|
91
|
+
def retrieve_file(file)
|
92
|
+
key = s3_key(file)
|
93
|
+
open(file, 'w') { |f| f.write @bucket.get(key) }
|
94
|
+
end
|
95
|
+
|
96
|
+
def keys(filename_prefix)
|
97
|
+
prefix = @dir ? "#{@dir}/#{filename_prefix}" : filename_prefix
|
98
|
+
@bucket.keys('prefix' => prefix).collect{|key| key}
|
99
|
+
end
|
100
|
+
|
101
|
+
def retrieve_files(filename_prefix, local_dir)
|
102
|
+
keys(filename_prefix).each do |k|
|
103
|
+
file = "#{local_dir}/#{File.basename(k.to_s)}"
|
104
|
+
retrieve_file(file)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def delete_files(filename_prefix)
|
109
|
+
keys(filename_prefix).each { |k| k.delete }
|
110
|
+
end
|
111
|
+
|
112
|
+
def s3_key(file)
|
113
|
+
@dir ? "#{@dir}/#{File.basename(file)}" : File.basename(file)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,110 @@
|
|
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 'fileutils'
|
20
|
+
require "#{File.dirname(__FILE__)}/system_files_manifest"
|
21
|
+
require "#{File.dirname(__FILE__)}/utils"
|
22
|
+
|
23
|
+
module Ec2onrails
|
24
|
+
class SystemFilesHelper
|
25
|
+
|
26
|
+
BACKUP_FILE_EXT = ".ec2onrails_backup"
|
27
|
+
INSTALLED_MANIFEST_FILE = "/etc/ec2onrails/system_files/#{SystemFilesManifest::MANIFEST_FILE_NAME}"
|
28
|
+
|
29
|
+
def install_system_files(from_dir)
|
30
|
+
uninstall_system_files
|
31
|
+
|
32
|
+
unless File.directory?(from_dir)
|
33
|
+
puts "no system files to install, #{from_dir} doesn't exist or is not a directory."
|
34
|
+
return
|
35
|
+
end
|
36
|
+
|
37
|
+
puts "installing system files from #{from_dir}..."
|
38
|
+
src_manifest = File.join from_dir, SystemFilesManifest::MANIFEST_FILE_NAME
|
39
|
+
|
40
|
+
@manifest = nil
|
41
|
+
if File.exists? src_manifest
|
42
|
+
@manifest = Ec2onrails::SystemFilesManifest.new(from_dir)
|
43
|
+
FileUtils.cp src_manifest, INSTALLED_MANIFEST_FILE
|
44
|
+
end
|
45
|
+
|
46
|
+
FileUtils.cd from_dir do
|
47
|
+
Dir.glob("**/*").each do |f|
|
48
|
+
unless File.directory?(f) || File.basename(f) == SystemFilesManifest::MANIFEST_FILE_NAME
|
49
|
+
dest = File.join("/", f)
|
50
|
+
backup(dest)
|
51
|
+
make_dirs(dest)
|
52
|
+
install_file(f, dest, @manifest)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def uninstall_system_files
|
59
|
+
unless File.exist? INSTALLED_MANIFEST_FILE
|
60
|
+
puts "not uninstalling system files, #{INSTALLED_MANIFEST_FILE} doesn't exist."
|
61
|
+
return
|
62
|
+
end
|
63
|
+
|
64
|
+
puts "uninstalling system files..."
|
65
|
+
@manifest = Ec2onrails::SystemFilesManifest.new(INSTALLED_MANIFEST_FILE)
|
66
|
+
@manifest.filenames.each do |f|
|
67
|
+
file = File.join("/", f)
|
68
|
+
unless File.directory?(file)
|
69
|
+
FileUtils.rm file
|
70
|
+
restore_backup_of file
|
71
|
+
end
|
72
|
+
end
|
73
|
+
FileUtils.rm INSTALLED_MANIFEST_FILE
|
74
|
+
end
|
75
|
+
|
76
|
+
def backup(f)
|
77
|
+
if File.exist?(f)
|
78
|
+
puts "backing up file #{f}..."
|
79
|
+
backup_file = f + BACKUP_FILE_EXT
|
80
|
+
FileUtils.mv f, backup_file
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def restore_backup_of(f)
|
85
|
+
backup_file = f + BACKUP_FILE_EXT
|
86
|
+
if File.exist?(backup_file)
|
87
|
+
puts "restoring backup of file #{f}..."
|
88
|
+
FileUtils.mv backup_file, f
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def make_dirs(f)
|
93
|
+
dir = File.dirname(f)
|
94
|
+
unless dir == "/"
|
95
|
+
puts "making dirs #{dir}..."
|
96
|
+
FileUtils.mkdir_p File.dirname(f)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def install_file(f, dest, manifest)
|
101
|
+
puts "installing file #{f} into #{dest}..."
|
102
|
+
FileUtils.cp f, dest
|
103
|
+
if manifest
|
104
|
+
Utils.run "chown #{manifest[f][:owner]} #{dest}" if manifest[f][:owner]
|
105
|
+
Utils.run "chmod #{manifest[f][:mode]} #{dest}" if manifest[f][:mode]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
end
|