kompanee-recipes 0.0.10 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of kompanee-recipes might be problematic. Click here for more details.

@@ -0,0 +1,40 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ run_task 'git:install', :as => "manager"
3
+
4
+ namespace :deploy do
5
+ desc <<-DESC
6
+ Tags the deployed Git commit with the timestamp and environment it was deployed to.
7
+
8
+ The tag is auto-pushed to whatever `remote` is set to as well as `origin`.
9
+ Tag push happens in the background so it won't slow down deployment.
10
+ DESC
11
+ task :tag do
12
+ timestamp_string_without_seconds = Time.now.strftime("%Y%m%d%H%M")
13
+ tag_name = "deployed_to_#{rails_env}_#{timestamp_string_without_seconds}"
14
+
15
+ `git tag -a -m "Tagging deploy to #{rails_env} at #{timestamp_string_without_seconds}" #{tag_name} #{branch}`
16
+ `git push #{remote} --tags > /dev/null 2>&1 &`
17
+ `git push origin --tags > /dev/null 2>&1 &`
18
+ end
19
+ end
20
+
21
+ namespace :git do
22
+ desc "[internal] Installs Git on Ubuntu systems using apt-get."
23
+ task :install_on_ubuntu do
24
+ set :dependencies, %w{git-core git-svn}
25
+ os.package_manager.install
26
+ end
27
+
28
+ desc "[internal] Installs Git on the server."
29
+ task :install do
30
+ git.send("install_on_#{os_type}")
31
+
32
+ puts "Git installed. If you're using Github, here's the key you'll need to add:"
33
+ public_key = capture "cat /home/deploy/.ssh/id_rsa.pub"
34
+ puts public_key
35
+
36
+ puts "Go ahead and log into your server and type: ssh git@github.com"
37
+ puts "Follow the on-screen instructions and you should see \"You've successfully authenticated\""
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,243 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ heroku_tasks = [
3
+ "heroku:credentials",
4
+ "heroku:credentials:default",
5
+ "deploy",
6
+ "deploy:default",
7
+ "deploy:initial",
8
+ "deploy:restart",
9
+ "deploy:migrate",
10
+ "deploy:rollback",
11
+ "deploy:rollback:default",
12
+ "deploy:web:enable",
13
+ "deploy:web:disable",
14
+ "website:install",
15
+ "website:remove",
16
+ "db:drop",
17
+ "db:backup",
18
+ "db:reset_and_seed",
19
+ "db:seed",
20
+ "shell",
21
+ "invoke"
22
+ ]
23
+
24
+ on :start, "heroku:credentials", :only => heroku_tasks
25
+ on :start, "heroku:raise_error", :except => heroku_tasks
26
+
27
+ namespace :heroku do
28
+ namespace :domain do
29
+ desc <<-DESC
30
+ Installs a new domain for the application on the Heroku server.
31
+ DESC
32
+ task :install do
33
+ `heroku domains:add #{deploy_name}`
34
+ end
35
+
36
+ desc <<-DESC
37
+ Removes the domain for the application from the Heroku server.
38
+ DESC
39
+ task :remove do
40
+ `heroku domains:remove #{deploy_name}`
41
+ end
42
+
43
+ namespace :addon do
44
+ desc <<-DESC
45
+ Add the Custom Domain Addon to the server.
46
+ DESC
47
+ task :install do
48
+ `heroku addons:add custom_domains:basic`
49
+ end
50
+
51
+ desc <<-DESC
52
+ Removes the Custom Domain Addon from the server.
53
+ DESC
54
+ task :remove do
55
+ `heroku addons:remove custom_domains:basic`
56
+ end
57
+ end
58
+ end
59
+
60
+ namespace :credentials do
61
+ desc <<-DESC
62
+ Selects the correct Heroku credentials for use given the current user.
63
+
64
+ If a credentials file already exists, it is backed up.
65
+ DESC
66
+ task :default do
67
+ if File.exist? heroku_credentials_file
68
+ heroku.credentials.backup
69
+ end
70
+
71
+ if File.exist? "#{heroku_credentials_path}/#{user}_credentials"
72
+ heroku.credentials.switch
73
+ else
74
+ heroku.credentials.create
75
+ end
76
+ end
77
+
78
+ desc <<-DESC
79
+ [internal] Backs up the current credentials file.
80
+ DESC
81
+ task :backup do
82
+ account = File.readlines(heroku_credentials_file)[0].chomp
83
+ File.rename(heroku_credentials_file, "#{heroku_credentials_path}/#{account}_credentials")
84
+ end
85
+
86
+ desc <<-DESC
87
+ [internal] Creates a Heroku credentials file.
88
+ DESC
89
+ task :create do
90
+ `if [ ! -d #{heroku_credentials_path} ]; then mkdir -p #{heroku_credentials_path}; fi`
91
+ `echo #{user} > #{heroku_credentials_file}`
92
+ `echo #{password} >> #{heroku_credentials_file}`
93
+ end
94
+
95
+ desc <<-DESC
96
+ [internal] Switches the credentials file to either the current use or the
97
+ name specified by the `HEROKU_ACCOUNT` environment variable.
98
+ DESC
99
+ task :switch do
100
+ account_to_switch_to = ENV['HEROKU_ACCOUNT'] || user
101
+ File.rename("#{heroku_credentials_path}/#{account_to_switch_to}_credentials", heroku_credentials_file)
102
+ end
103
+ end
104
+
105
+ desc <<-DESC
106
+ [internal] Raises an error if someone tries to run a task other than those
107
+ that are valid for a Heroku deployment.
108
+ DESC
109
+ task :raise_error do
110
+ raise "Deploying the #{rails_env} environment to Heroku. This command is invalid."
111
+ end
112
+ end
113
+
114
+ namespace :deploy do
115
+ desc <<-DESC
116
+ The standard deployment task.
117
+
118
+ It will check out a new release of the code, run any pending migrations and
119
+ restart the application.
120
+ DESC
121
+ task :default do
122
+ `git push heroku #{branch}`
123
+ deploy.migrate
124
+ deploy.restart
125
+ end
126
+
127
+ desc <<-DESC
128
+ Restarts the application.
129
+ DESC
130
+ task :restart do
131
+ `heroku restart`
132
+ end
133
+
134
+ desc <<-DESC
135
+ Runs the migrate rake task.
136
+ DESC
137
+ task :migrate do
138
+ `heroku rake db:migrate`
139
+ end
140
+
141
+ desc <<-DESC
142
+ Rolls back to a previous version and restarts.
143
+ DESC
144
+ namespace :rollback do
145
+ task :default do
146
+ `heroku rollback`
147
+ deploy.restart
148
+ end
149
+ end
150
+
151
+ namespace :web do
152
+ desc "Removes the maintenance page to resume normal site operation."
153
+ task :enable do
154
+ `heroku maintenance:off`
155
+ end
156
+
157
+ desc "Diplays the maintenance page."
158
+ task :disable do
159
+ `heroku maintenance:on`
160
+ end
161
+ end
162
+
163
+ desc "Prepare the server for deployment."
164
+ task :initial do
165
+ website.install
166
+
167
+ heroku.domain.addon.install
168
+ db.backup.addon.install
169
+
170
+ heroku.domain.install
171
+
172
+ `heroku config:add BUNDLE_WITHOUT="development:test"`
173
+ deploy.default
174
+ end
175
+ end
176
+
177
+ namespace :website do
178
+ desc "Installs the application on Heroku"
179
+ task :install do
180
+ `heroku create #{application_underscored}`
181
+ end
182
+
183
+ desc "Completely removes application from Heroku"
184
+ task :remove do
185
+ `heroku destroy --confirm #{application_underscored}`
186
+ end
187
+ end
188
+
189
+ namespace :db do
190
+ desc "Removes the DB from the Server. Also removes the user."
191
+ task :drop do
192
+ `heroku pg:reset`
193
+ end
194
+
195
+ namespace :backup do
196
+ desc "Backup the database"
197
+ task :default do
198
+ `heroku pgbackups:capture`
199
+ end
200
+
201
+ namespace :addon do
202
+ desc <<-DESC
203
+ Add the Postgres Backups Addon to the server.
204
+ DESC
205
+ task :install do
206
+ `heroku addons:add pgbackups:basic`
207
+ end
208
+
209
+ desc <<-DESC
210
+ Removes the Postgres Backups Addon from the server.
211
+ DESC
212
+ task :remove do
213
+ `heroku addons:remove pgbackups:basic`
214
+ end
215
+ end
216
+ end
217
+
218
+ desc "Reset database and seed fresh"
219
+ task :reset_and_seed do
220
+ raise "I'm sorry Dave, but I can't let you do that. I have full control over production." if rails_env == 'production'
221
+ db.backup
222
+ `heroku pg:reset`
223
+ `heroku rake db:seed`
224
+ end
225
+
226
+ desc "Seed database"
227
+ task :seed do
228
+ raise "I'm sorry Dave, but I can't let you do that. I have full control over production." if rails_env == 'production'
229
+ db.backup
230
+ `heroku rake db:seed`
231
+ end
232
+ end
233
+
234
+ desc "Begin an interactive Heroku session."
235
+ task :shell do
236
+ `heroku shell`
237
+ end
238
+
239
+ desc "Invoke a single command on the Heroku server."
240
+ task :invoke do
241
+ `heroku invoke #{ENV['COMMAND']}`
242
+ end
243
+ end
@@ -0,0 +1,128 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ set(:db_root_password) {Capistrano::CLI.password_prompt("Root Password For DB: ")} unless exists?(:db_root_password)
3
+ set(:db_app_password) {Capistrano::CLI.password_prompt("App Password For DB: ")} unless exists?(:db_app_password)
4
+
5
+ run_task "db:create", :as => "manager"
6
+ run_task "db:drop", :as => "manager"
7
+ run_task "db:install", :as => "manager"
8
+
9
+ namespace :db do
10
+ desc <<-DESC
11
+ Installs the MySQL database server on the system.
12
+ DESC
13
+ task :install do
14
+ mysql.send("install_on_#{os_type}")
15
+ end
16
+
17
+ desc <<-DESC
18
+ Creates the MySQL database and user for the application.
19
+
20
+ * Creates a script that is uploaded to the user's home directory.
21
+ * The script is executed as `root` and as such, the user will be prompted
22
+ for `root`'s password.
23
+ * The DB and user name are equivalent to the application_underscored
24
+ variable.
25
+ * The DB user will be granted all privileges on the DB.
26
+ DESC
27
+ task :create do
28
+ create_script = <<-CREATESCRIPT
29
+ create database #{application_underscored} character set utf8;
30
+ create user '#{application_underscored}'@'localhost' identified by '#{db_app_password}';
31
+ grant all on #{application_underscored}.* to #{application_underscored}@localhost;
32
+ CREATESCRIPT
33
+
34
+ put create_script, "#{user_home}/create_#{application_underscored}_db_script"
35
+ run %Q{#{sudo} bash -c "mysql --user=root --password=#{db_root_password} < #{user_home}/create_#{application_underscored}_db_script"}
36
+ run "rm #{user_home}/create_#{application_underscored}_db_script"
37
+ end
38
+
39
+ desc <<-DESC
40
+ Drops the MySQL database and user for the application.
41
+
42
+ * Creates a script that is uploaded to the user's home directory.
43
+ * The script is executed as `root` and as such, the user will be prompted
44
+ for `root`'s password.
45
+ DESC
46
+ task :drop do
47
+ drop_script = <<-DROPSCRIPT
48
+ drop user #{application_underscored}@localhost;
49
+ drop database #{application_underscored};
50
+ DROPSCRIPT
51
+
52
+ put drop_script, "#{user_home}/drop_#{application_underscored}_db_script"
53
+ run %Q{#{sudo} bash -c "mysql --user=root --password=#{db_root_password} < #{user_home}/drop_#{application_underscored}_db_script"}
54
+ run "rm #{user_home}/drop_#{application_underscored}_db_script"
55
+ end
56
+
57
+ desc <<-DESC
58
+ Calls the rake task `db:backup` on the server for the given environment.
59
+
60
+ * The backup file is placed in a directory called `db_backups` under the `shared`
61
+ directory by default.
62
+ * The filenames are formatted with the timestamp of the backup.
63
+ * After export, each file is zipped up using a bzip2 compression format.
64
+ DESC
65
+ task :backup do
66
+ run "if [ ! -d #{shared_path}/db_backups ]; then mkdir #{shared_path}/db_backups; fi"
67
+ run "cd #{current_path} && rake BACKUP_DIRECTORY=#{shared_path}/db_backups RAILS_ENV=#{rails_env} db:backup"
68
+ end
69
+
70
+ desc <<-DESC
71
+ Calls the rake task `db:reset_and_seed` on the server for the given environment.
72
+
73
+ Typically, this task will drop the DB, recreate the DB, load the development
74
+ schema and then populate the DB by calling the `db:seed` task.
75
+
76
+ Warning: This task cannot be called in production mode. If you truely wish
77
+ to run this in production, you'll need to log into the server and
78
+ run the rake task manually.
79
+ DESC
80
+ task :reset_and_seed do
81
+ raise "I'm sorry Dave, but I can't let you do that. I have full control over production." if rails_env == 'production'
82
+ db.backup
83
+ run "cd #{current_path} && rake RAILS_ENV=#{rails_env} db:reset_and_seed"
84
+ end
85
+
86
+ desc <<-DESC
87
+ Calls the rake task `db:seed` on the server for the given environment.
88
+
89
+ Typically, this task will populate the DB with valid data which is necessary
90
+ for the initial production deployment. An example may be that the `STATES`
91
+ table gets populated with all the information about the States.
92
+
93
+ Warning: This task cannot be called in production mode. If you truely wish
94
+ to run this in production, you'll need to log into the server and
95
+ run the rake task manually.
96
+ DESC
97
+ task :seed do
98
+ raise "I'm sorry Dave, but I can't let you do that. I have full control over production." if rails_env == 'production'
99
+ db.backup
100
+ run "cd #{current_path} && rake RAILS_ENV=#{rails_env} db:seed"
101
+ end
102
+ end
103
+
104
+ namespace :mysql do
105
+ desc <<-DESC
106
+ [internal] Install MySQL on an Ubuntu server.
107
+
108
+ * Will use `apt-get` to install MySQL.
109
+ * Will additionally create a debconf answers file so that the installation
110
+ can be automated.
111
+ * The Capistrano user will be prompted for the desired `root` password for
112
+ the DB.
113
+ DESC
114
+ task :install_on_ubuntu do
115
+ mysql_install_answers = <<-INSTALLANSWERS
116
+ mysql-server mysql-server/root_password select #{db_root_password}
117
+ mysql-server mysql-server/root_password_again select #{db_root_password}
118
+ INSTALLANSWERS
119
+
120
+ put mysql_install_answers, "#{user_home}/mysql_install_answers"
121
+ run "#{sudo} debconf-set-selections #{user_home}/mysql_install_answers"
122
+ run "rm #{user_home}/mysql_install_answers"
123
+
124
+ set :dependencies, %w{mysql-server-5.1 libmysql-ruby}
125
+ os.package_manager.install
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,124 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ run_task "passenger:install:web_server_module", :as => "manager"
3
+ run_task "passenger:enable", :as => "manager"
4
+ run_task "passenger:disable", :as => "manager"
5
+
6
+ namespace :deploy do
7
+ desc <<-DESC
8
+ Starts/Restarts the application.
9
+
10
+ Passenger knows when you'd like to reset by looking at a file in the `tmp`
11
+ directory called `restart.txt`. If the Last Access time for `restart.txt`
12
+ changes, Passenger restarts the app.
13
+ DESC
14
+ task :start, :except => { :no_release => true } do
15
+ run "touch #{File.join(current_path,'tmp','restart.txt')}"
16
+ end
17
+ desc <<-DESC
18
+ There is no way to stop the application via Passenger.
19
+
20
+ This task does nothing.
21
+ DESC
22
+ task :stop do ; end
23
+ desc <<-DESC
24
+ Starts/Restarts the application.
25
+
26
+ Passenger knows when you'd like to reset by looking at a file in the `tmp`
27
+ directory called `restart.txt`. If the Last Access time for `restart.txt`
28
+ changes, Passenger restarts the app.
29
+ DESC
30
+ task :restart, :except => { :no_release => true } do
31
+ run "touch #{File.join(current_path,'tmp','restart.txt')}"
32
+ end
33
+ end
34
+
35
+ namespace :passenger do
36
+ namespace :install do
37
+ desc <<-DESC
38
+ Installs the Passenger gem
39
+
40
+ Passenger will be installed via RVM in the `@global` gemset
41
+ for the default Ruby version.
42
+ DESC
43
+ task :gems do
44
+ run "rvm use #{default_ruby_version}@global && gem install passenger --version #{passenger_version} --no-ri --no-rdoc"
45
+ end
46
+
47
+ desc <<-DESC
48
+ Installs the Passenger module
49
+ DESC
50
+ task :web_server_module do
51
+ passenger.install.send("#{web_server_name}_module")
52
+ end
53
+
54
+ desc <<-DESC
55
+ [internal] Installs the Passenger module
56
+
57
+ The Passenger Apache module will be installed via RVM from the `@global` gemset for the default Ruby version.
58
+ DESC
59
+ task :apache_module do
60
+ run "rvm use #{default_ruby_version}@global && rvmsudo passenger-install-apache2-module -a"
61
+
62
+ apache_passenger_load = <<-PASSLOAD
63
+ LoadModule passenger_module #{user_home}/.rvm/gems/#{default_ruby_version}@global/gems/passenger-#{passenger_version}/ext/apache2/mod_passenger.so
64
+ PASSLOAD
65
+
66
+ apache_passenger_conf = <<-PASSCONF
67
+ <IfModule mod_passenger.c>
68
+ PassengerRoot #{user_home}/.rvm/gems/#{default_ruby_version}@global/gems/passenger-#{passenger_version}
69
+ PassengerRuby #{user_home}/.rvm/bin/#{default_ruby_version}
70
+
71
+ <Directory "#{deploy_dir}">
72
+ Order allow,deny
73
+ Allow from all
74
+ </Directory>
75
+ </IfModule>
76
+ PASSCONF
77
+
78
+ put apache_passenger_load, "#{user_home}/passenger.load"
79
+ put apache_passenger_conf, "#{user_home}/passenger.conf"
80
+ run "#{sudo} mv #{user_home}/passenger.load /etc/apache2/mods-available/"
81
+ run "#{sudo} mv #{user_home}/passenger.conf /etc/apache2/mods-available/"
82
+ passenger.enable
83
+ end
84
+
85
+ desc <<-DESC
86
+ Installs the Passenger gem and Apache module.
87
+ DESC
88
+ task :default do
89
+ passenger.install.gems
90
+ passenger.install.web_server_module
91
+ end
92
+ end
93
+
94
+ desc <<-DESC
95
+ Enables the Passenger module.
96
+ DESC
97
+ task :enable do
98
+ passenger.send("enable_on_#{web_server_name}")
99
+ web_server.restart
100
+ end
101
+
102
+ desc <<-DESC
103
+ Disables the Passenger module.
104
+ DESC
105
+ task :disable do
106
+ passenger.send("disable_on_#{web_server_name}")
107
+ web_server.restart
108
+ end
109
+
110
+ desc <<-DESC
111
+ [internal] Enables the Passenger module on Apache.
112
+ DESC
113
+ task :enable_on_apache do
114
+ run "#{sudo} a2enmod passenger"
115
+ end
116
+
117
+ desc <<-DESC
118
+ [internal] Disables the Passenger module on Apache.
119
+ DESC
120
+ task :disable_on_apache do
121
+ run "#{sudo} a2dismod passenger"
122
+ end
123
+ end
124
+ end