ec2onrails 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,329 @@
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
+ include FileUtils
21
+
22
+ require 'ec2onrails/version'
23
+ require 'ec2onrails/capistrano_utils'
24
+ include Ec2onrails::CapistranoUtils
25
+
26
+ Capistrano::Configuration.instance.load do
27
+
28
+ unless ec2onrails_config
29
+ raise "ec2onrails_config variable not set. (It should be a hash.)"
30
+ end
31
+
32
+ cfg = ec2onrails_config
33
+
34
+ set :image_id, Ec2onrails::VERSION::AMI_ID
35
+ set :deploy_to, "/mnt/app"
36
+ set :use_sudo, false
37
+ set :user, "app"
38
+
39
+ make_admin_role_for(:web, :web_admin)
40
+ make_admin_role_for(:app, :app_admin)
41
+ make_admin_role_for(:db, :db_admin)
42
+
43
+ roles[:web_admin].to_s
44
+ roles[:app_admin].to_s
45
+ roles[:db_admin].to_s
46
+
47
+ # override default start/stop/restart tasks
48
+ namespace :deploy do
49
+ desc <<-DESC
50
+ Overrides the default Capistrano deploy:start, directly calls \
51
+ /usr/local/ec2onrails/bin/mongrel_cluster_ctl_wrapper
52
+ DESC
53
+ task :start, :except => { :no_release => true } do
54
+ run "/usr/local/ec2onrails/bin/mongrel_cluster_ctl_wrapper start"
55
+ end
56
+
57
+ desc <<-DESC
58
+ Overrides the default Capistrano deploy:stop, directly calls \
59
+ /usr/local/ec2onrails/bin/mongrel_cluster_ctl_wrapper
60
+ DESC
61
+ task :stop, :except => { :no_release => true } do
62
+ run "/usr/local/ec2onrails/bin/mongrel_cluster_ctl_wrapper stop"
63
+ end
64
+
65
+ desc <<-DESC
66
+ Overrides the default Capistrano deploy:restart, directly calls \
67
+ /usr/local/ec2onrails/bin/mongrel_cluster_ctl_wrapper
68
+ DESC
69
+ task :restart, :except => { :no_release => true } do
70
+ run "/usr/local/ec2onrails/bin/mongrel_cluster_ctl_wrapper restart"
71
+ end
72
+ end
73
+
74
+ namespace :ec2onrails do
75
+
76
+ desc <<-DESC
77
+ Start a new server instance and prepare it for a cold deploy.
78
+ DESC
79
+ task :setup, :roles => [:web, :db, :app] do
80
+ ec2.start_instance
81
+ server.set_timezone
82
+ server.upgrade_and_install_all
83
+ server.deploy_files
84
+ server.restart_services
85
+ deploy.setup
86
+ db.create
87
+ end
88
+
89
+ desc <<-DESC
90
+ Deploy and restore database from S3
91
+ DESC
92
+ task :restore_db_and_deploy, :roles => [:web, :db, :app] do
93
+ db.recreate
94
+ deploy.update_code
95
+ deploy.symlink
96
+ # don't need to migrate because we're restoring the db
97
+ db.restore
98
+ deploy.restart
99
+ end
100
+
101
+ namespace :ec2 do
102
+ desc <<-DESC
103
+ Start an instance, using the AMI of the correct version to match this gem.
104
+ DESC
105
+ task :start_instance, :roles => [:web, :db, :app] do
106
+ # TODO
107
+ # ec2 = EC2::Base.new(:access_key_id => access_key_id, :secret_access_key => secret_access_key)
108
+ # ec2.run_instances(:image_id => image_id, :key_name => key_name, :group_id => group_id)
109
+ # wait until image is booted
110
+ end
111
+
112
+ desc <<-DESC
113
+ Set default firewall rules.
114
+ DESC
115
+ task :configure_firewall do
116
+ # TODO
117
+ end
118
+ end
119
+
120
+ namespace :db do
121
+ desc <<-DESC
122
+ Load configuration info for the production database from \
123
+ config/database.yml.
124
+ DESC
125
+ task :load_config, :roles => :db do
126
+ db_config = YAML::load(ERB.new(File.read("config/database.yml")).result)['production']
127
+ cfg[:production_db_name] = db_config['database']
128
+ cfg[:production_db_user] = db_config['username']
129
+ cfg[:production_db_password] = db_config['password']
130
+
131
+ [cfg[:production_db_name], cfg[:production_db_user], cfg[:production_db_password]].each do |s|
132
+ if s.match(/['"]/)
133
+ raise "ERROR: database config string '#{s}' contains quotes."
134
+ end
135
+ end
136
+ end
137
+
138
+ desc <<-DESC
139
+ Create the MySQL production database. Assumes there is no MySQL root \
140
+ password. To create a MySQL root password create a task that's run \
141
+ after this task using an after hook.
142
+ DESC
143
+ task :create, :roles => :db do
144
+ on_rollback { drop }
145
+ load_config
146
+ run "echo 'create database #{cfg[:production_db_name]};' | mysql -u root"
147
+ run "echo \"grant all on #{cfg[:production_db_name]}.* to '#{cfg[:production_db_user]}'@'localhost' identified by '#{cfg[:production_db_password]}';\" | mysql -u root"
148
+ end
149
+
150
+ desc <<-DESC
151
+ Drop the MySQL production database. Assumes there is no MySQL root \
152
+ password. If there is a MySQL root password, create a task that removes \
153
+ it and run that task before this one using a before hook.
154
+ DESC
155
+ task :drop, :roles => :db do
156
+ load_config
157
+ run "echo 'drop database if exists #{cfg[:production_db_name]};' | mysql -u root"
158
+ end
159
+
160
+ desc <<-DESC
161
+ db:drop and db:create.
162
+ DESC
163
+ task :recreate, :roles => :db do
164
+ drop
165
+ create
166
+ end
167
+
168
+ desc <<-DESC
169
+ Set a root password for MySQL, using the variable mysql_root_password \
170
+ if it is set. If this is done db:drop won't work.
171
+ DESC
172
+ task :set_root_password, :roles => :db do
173
+ if cfg[:mysql_root_password]
174
+ run "echo 'set password for root@localhost=password('#{cfg[:mysql_root_password]}');' | mysql -u root"
175
+ end
176
+ end
177
+
178
+ desc <<-DESC
179
+ Dump the MySQL database to the S3 bucket specified by \
180
+ ec2onrails_config[:archive_to_bucket]. The filename will be \
181
+ "app-<timestamp>.sql.gz".
182
+ DESC
183
+ task :archive, :roles => [:db] do
184
+ run "/usr/local/ec2onrails/bin/backup_app_db.rb #{cfg[:archive_to_bucket]} app-#{Time.new.strftime('%y-%m-%d--%H-%M-%S')}.sql.gz"
185
+ end
186
+
187
+ desc <<-DESC
188
+ Restore the MySQL database from the S3 bucket specified by \
189
+ ec2onrails_config[:restore_from_bucket]. The archive filename is \
190
+ expected to be the default, "app.sql.gz".
191
+ DESC
192
+ task :restore, :roles => [:db] do
193
+ run "/usr/local/ec2onrails/bin/restore_app_db.rb #{cfg[:restore_from_bucket]}"
194
+ end
195
+ end
196
+
197
+ namespace :server do
198
+ desc <<-DESC
199
+ Upgrade to the newest versions of all Ubuntu packages.
200
+ DESC
201
+ task :upgrade_packages, :roles => [:web_admin, :db_admin, :app_admin] do
202
+ sudo "aptitude -q update"
203
+ run "export DEBIAN_FRONTEND=noninteractive; sudo aptitude -q -y dist-upgrade"
204
+ end
205
+
206
+ desc <<-DESC
207
+ Upgrade to the newest versions of all rubygems.
208
+ DESC
209
+ task :upgrade_gems, :roles => [:web_admin, :db_admin, :app_admin] do
210
+ sudo "gem update -y"
211
+ end
212
+
213
+ desc <<-DESC
214
+ Install extra Ubuntu packages. Set ec2onrails_config[:packages], it \
215
+ should be an array of strings.
216
+ NOTE: the package installation will be non-interactive, if the packages \
217
+ require configuration either log in as 'admin' and run \
218
+ 'dpkg-reconfigure packagename' or replace the package's config files \
219
+ using the 'ec2onrails:deploy_config_files' task.
220
+ DESC
221
+ task :install_packages, :roles => [:web_admin, :db_admin, :app_admin] do
222
+ if cfg[:packages] && cfg[:packages].any?
223
+ run "export DEBIAN_FRONTEND=noninteractive; sudo aptitude -q -y install #{cfg[:packages].join(' ')}"
224
+ end
225
+ end
226
+
227
+ desc <<-DESC
228
+ Install extra rubygems. Set ec2onrails_config[:rubygems], it should \
229
+ be with an array of strings.
230
+ DESC
231
+ task :install_gems, :roles => [:web_admin, :db_admin, :app_admin] do
232
+ if cfg[:rubygems] && cfg[:rubygems].any?
233
+ sudo "gem install #{cfg[:rubygems].join(' ')} -y" do |ch, str, data|
234
+ ch[:data] ||= ""
235
+ ch[:data] << data
236
+ if data =~ />\s*$/
237
+ puts "The gem command is asking for a number:"
238
+ choice = STDIN.gets
239
+ ch.send_data(choice)
240
+ else
241
+ puts data
242
+ end
243
+ end
244
+ end
245
+ end
246
+
247
+ desc <<-DESC
248
+ A convenience task to upgrade existing packages and gems and install \
249
+ specified new ones.
250
+ DESC
251
+ task :upgrade_and_install_all, :roles => [:web_admin, :db_admin, :app_admin] do
252
+ upgrade_packages
253
+ upgrade_gems
254
+ install_packages
255
+ install_gems
256
+ end
257
+
258
+ desc <<-DESC
259
+ Set the timezone using the value of the variable named timezone. \
260
+ Valid options for timezone can be determined by the contents of \
261
+ /usr/share/zoneinfo, which can be seen here: \
262
+ http://packages.ubuntu.com/cgi-bin/search_contents.pl?searchmode=filelist&word=tzdata&version=feisty&arch=all&page=1&number=all \
263
+ Remove 'usr/share/zoneinfo/' from the filename, and use the last \
264
+ directory and file as the value. For example 'Africa/Abidjan' or \
265
+ 'posix/GMT' or 'Canada/Eastern'.
266
+ DESC
267
+ task :set_timezone, :roles => [:web_admin, :db_admin, :app_admin] do
268
+ if cfg[:timezone]
269
+ sudo "bash -c 'echo #{cfg[:timezone]} > /etc/timezone'"
270
+ sudo "cp /usr/share/zoneinfo/#{cfg[:timezone]} /etc/localtime"
271
+ end
272
+ end
273
+
274
+ desc <<-DESC
275
+ Deploy a set of config files to the server, the files will be owned by \
276
+ root. This doesn't delete any files from the server.
277
+ DESC
278
+ task :deploy_files, :roles => [:web_admin, :db_admin, :app_admin] do
279
+ if cfg[:server_config_files_root]
280
+ begin
281
+ # TODO use Zlib to support Windows
282
+ file = '/tmp/config_files.tgz'
283
+ run_local "tar zcf #{file} -C '#{cfg[:server_config_files_root]}' ."
284
+ put File.read(file), file
285
+ sudo "tar zxvf #{file} -o -C /"
286
+ ensure
287
+ rm_rf file
288
+ sudo "rm -f #{file}"
289
+ end
290
+ end
291
+ end
292
+
293
+ desc <<-DESC
294
+ DESC
295
+ task :restart_services, :roles => [:web_admin, :db_admin, :app_admin] do
296
+ if cfg[:services_to_restart] && cfg[:services_to_restart].any?
297
+ cfg[:services_to_restart].each do |service|
298
+ sudo "/etc/init.d/#{service} restart"
299
+ end
300
+ end
301
+ end
302
+
303
+ desc <<-DESC
304
+ DESC
305
+ task :enable_mail_server, :roles => [:web_admin, :db_admin, :app_admin] do
306
+ # TODO
307
+ end
308
+
309
+ desc <<-DESC
310
+ DESC
311
+ task :add_user, :roles => [:web_admin, :db_admin, :app_admin] do
312
+ # TODO
313
+ end
314
+
315
+ desc <<-DESC
316
+ DESC
317
+ task :run_script, :roles => [:web_admin, :db_admin, :app_admin] do
318
+ # TODO
319
+ end
320
+
321
+ desc <<-DESC
322
+ DESC
323
+ task :archive_logs, :roles => [:web_admin, :db_admin, :app_admin] do
324
+ # TODO
325
+ end
326
+ end
327
+
328
+ end
329
+ end
@@ -0,0 +1,28 @@
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
+ module Ec2onrails #:nodoc:
20
+ module VERSION #:nodoc:
21
+ MAJOR = 0
22
+ MINOR = 9
23
+ TINY = 3
24
+ STRING = [MAJOR, MINOR, TINY].join('.')
25
+
26
+ AMI_ID = 'ami-a3f91cca'
27
+ end
28
+ end
data/lib/ec2onrails.rb ADDED
@@ -0,0 +1,20 @@
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
+
20
+ $:.unshift File.dirname(__FILE__)
data/log/debug.log ADDED
File without changes
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.join(File.dirname(__FILE__), '..')
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.join(File.dirname(__FILE__), '..')
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
data/script/txt2html ADDED
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ begin
5
+ require 'newgem'
6
+ rescue LoadError
7
+ puts "\n\nGenerating the website requires the newgem RubyGem"
8
+ puts "Install: gem install newgem\n\n"
9
+ exit(1)
10
+ end
11
+ require 'redcloth'
12
+ require 'syntax/convertors/html'
13
+ require 'erb'
14
+ require File.dirname(__FILE__) + '/../lib/ec2onrails/version.rb'
15
+
16
+ version = Ec2onrails::VERSION::STRING
17
+ download = 'http://rubyforge.org/projects/ec2onrails'
18
+
19
+ class Fixnum
20
+ def ordinal
21
+ # teens
22
+ return 'th' if (10..19).include?(self % 100)
23
+ # others
24
+ case self % 10
25
+ when 1: return 'st'
26
+ when 2: return 'nd'
27
+ when 3: return 'rd'
28
+ else return 'th'
29
+ end
30
+ end
31
+ end
32
+
33
+ class Time
34
+ def pretty
35
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
36
+ end
37
+ end
38
+
39
+ def convert_syntax(syntax, source)
40
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
41
+ end
42
+
43
+ if ARGV.length >= 1
44
+ src, template = ARGV
45
+ template ||= File.join(File.dirname(__FILE__), '/../website/template.rhtml')
46
+
47
+ else
48
+ puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
49
+ exit!
50
+ end
51
+
52
+ template = ERB.new(File.open(template).read)
53
+
54
+ title = nil
55
+ body = nil
56
+ File.open(src) do |fsrc|
57
+ title_text = fsrc.readline
58
+ body_text = fsrc.read
59
+ syntax_items = []
60
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
61
+ ident = syntax_items.length
62
+ element, syntax, source = $1, $2, $3
63
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
64
+ "syntax-temp-#{ident}"
65
+ }
66
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
67
+ body = RedCloth.new(body_text).to_html
68
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
69
+ end
70
+ stat = File.stat(src)
71
+ created = stat.ctime
72
+ modified = stat.mtime
73
+
74
+ $stdout << template.result(binding)