Skip to content
This repository was archived by the owner on Jul 24, 2020. It is now read-only.

Commit

Permalink
Finalize Capistrano setup for ITS deployment
Browse files Browse the repository at this point in the history
Resolves #1074
- finalize deploy script setup
- finalize custom tasks for deployment
- move custom tasks to lib/capistrano/tasks directory
- update dotenv to 2.0.2; remove dotenv-deployment (deprecated)
- other tweaks / fixes
  • Loading branch information
orenyk committed Sep 16, 2015
1 parent ea431b5 commit 6602cc7
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 66 deletions.
8 changes: 4 additions & 4 deletions Capfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ require 'capistrano/rails'
require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
# require 'capistrano/bundler'
# require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'

# Includes whenever tasks
require "whenever/capistrano"
# require "whenever/capistrano"

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
14 changes: 6 additions & 8 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ gem 'rake', '~> 10.4.2'
gem 'rdoc', '~> 4.2.0'

# simulate environment variables
group :development, :test do
gem 'dotenv-rails', '~> 1.0.2'
end
gem 'dotenv-rails', '~> 2.0.2'

# authentication / authorization
gem 'devise', '~> 3.4.1'
Expand Down Expand Up @@ -66,10 +64,11 @@ group :development, :test do
gem 'spring-commands-rspec', '~> 1.0.4'
gem 'fuubar', '~> 2.0.0'
gem 'guard-livereload', '~> 2.4.0'
gem 'capistrano', '~> 3.3.5'
gem 'capistrano-bundler', '~> 1.1.4'
gem 'capistrano-rails', '~> 1.1.2'
gem 'capistrano-rvm', '~> 0.1.2'
gem 'capistrano', '~> 3.3.5', require: false
gem 'capistrano-bundler', '~> 1.1.4', require: false
gem 'capistrano-rails', '~> 1.1.2', require: false
gem 'capistrano-rvm', '~> 0.1.2', require: false
gem 'highline', '~> 1.7.2', require: false
gem 'awesome_print', '~> 1.6.1'
gem 'codeclimate-test-reporter', '~> 0.4.6'
gem 'database_cleaner', '~> 1.4.0'
Expand Down Expand Up @@ -98,7 +97,6 @@ end
group :production do
gem 'therubyracer', '~> 0.12.1', require: 'v8'
gem 'party_foul', '~> 1.5.5'
gem 'dotenv-deployment', '~> 0.2.0'
# for Heroku
gem 'pg', '~> 0.18.1'
gem 'unicorn', '~> 4.8.3'
Expand Down
14 changes: 7 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,10 @@ GEM
rubycas-client (>= 2.2.1)
diff-lcs (1.2.5)
docile (1.1.5)
dotenv (1.0.2)
dotenv-deployment (0.2.0)
dotenv (~> 1.0)
dotenv-rails (1.0.2)
dotenv (= 1.0.2)
dotenv (2.0.2)
dotenv-rails (2.0.2)
dotenv (= 2.0.2)
railties (~> 4.0)
draper (1.4.0)
actionpack (>= 3.0)
activemodel (>= 3.0)
Expand Down Expand Up @@ -159,6 +158,7 @@ GEM
rspec (>= 2.99.0, < 4.0)
haml (4.0.6)
tilt
highline (1.7.2)
hike (1.2.3)
hitimes (1.2.2)
http_parser.rb (0.6.0)
Expand Down Expand Up @@ -430,15 +430,15 @@ DEPENDENCIES
database_cleaner (~> 1.4.0)
devise (~> 3.4.1)
devise_cas_authenticatable (~> 1.3.7)
dotenv-deployment (~> 0.2.0)
dotenv-rails (~> 1.0.2)
dotenv-rails (~> 2.0.2)
draper (~> 1.4.0)
factory_girl_rails (~> 4.5.0)
ffaker (~> 1.32.1)
font-awesome-rails (~> 4.3.0)
fuubar (~> 2.0.0)
guard-livereload (~> 2.4.0)
guard-rspec (~> 4.5.0)
highline (~> 1.7.2)
inline_svg
jquery-datatables-rails (~> 3.1.1)
jquery-rails (~> 3.1.2)
Expand Down
66 changes: 20 additions & 46 deletions config/deploy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# config valid only for Capistrano 3.1
lock '3.2.1'
lock '3.3.5'

set :application, 'Reservations'
set :repo_url, 'https://github.com/YaleSTC/reservations.git'
Expand All @@ -9,17 +9,19 @@

# Default deploy_to directory is /var/www/my_app
set :deploy_to, "#{ENV['DEPLOY_DIR']}"
set :param_file, "#{ENV['PARAM_FILE']}"

# Set Rails environment
# set, :rails_env, 'production'

# Set RVM version
set :rvm_ruby_version, '2.1.2'
# Set rvm stuff
set :rvm_ruby_version, File.read('.ruby-version').strip

# include whenever recipes
# Include whenever recipes
set :whenever_environment, ->{ fetch(:environment) }
set :whenever_command, 'bundle exec whenever'
set :whenever_environment, defer { stage }
set :whenever_variables, { "rails_root=#{fetch :release_path}&environment=#{fetch :whenever_environment}" }
set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:environment)}" }
set :whenever_variables, ->{ "rails_root=#{fetch :release_path}&environment=#{fetch :whenever_environment}" }

# Default value for :scm is :git
# set :scm, :git
Expand All @@ -45,55 +47,27 @@
# Default value for keep_releases is 5
# set :keep_releases, 5

# configuration tasks
namespace :init do
namespace :config do

desc 'Create .env'
task :env
execute "cp #{release_path}/.env.example #{release_path}/.env"
end

desc 'Create database.yml'
task :db
execute "cp #{release_path}/config/database.yml.example.production #{release_path}/config/database.yml"
end

desc 'Create party_foul initializer'
task :party_foul
execute "cp #{release_path}/config/initializers/party_foul.rb.example #{release_path}/config/initializers/party_foul.rb"
end
end
end

namespace :deploy do

desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
execute :touch, release_path.join('tmp/restart.txt')
end
end

after :updated, 'config:env'
after :updated, 'config:db'
# figure out how to make this optional
after :updated, 'config:party_foul'
# clear crontab if in staging environment
if :stage == 'staging'
before :restart, 'whenever:clear_crontab'
end

after :publishing, :restart

after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
# see http://tutor.lugolabs.com/articles/14-schedule-rails-tasks-with-whenever-and-capistrano
desc "Update crontab with whenever"
task :update_cron do
on roles(:app) do
within current_path do
execute :bundle, :exec, "whenever --update-crontab #{fetch(:application)}"
end
end
end

before :updated, 'config:env'
before :updated, 'config:db'
before :updated, 'config:secrets'
before :updated, 'config:party_foul'
after :publishing, :restart
end
5 changes: 5 additions & 0 deletions config/deploy/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@
# auth_methods: %w(publickey password)
# # password: 'please use keys'
# }

# set crontab with whenever
namespace :deploy do
after :finishing, :update_cron
end
6 changes: 5 additions & 1 deletion config/initializers/authentication.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Check for authentication method and copy data over if necessary (ENV variable
# to skip if necessary)
unless ENV['SKIP_AUTH_INIT'] || !User.table_exists? # skip if no users table

# Also skip if no users table or username field (old schema)
unless ENV['SKIP_AUTH_INIT'] || !User.table_exists? ||
!User.column_names.include?('username')

user = User.first

# if we want to use CAS authentication and the username parameter doesn't
Expand Down
45 changes: 45 additions & 0 deletions lib/capistrano/tasks/deployment_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'capistrano'

# configuration tasks
namespace :config do
desc 'Create .env'
task :env do
on roles(:app) do
# copy Jenkins/parameter file to .env if it exists
if test "[ -e #{fetch(:param_file)} ]"
execute "cp -rf #{fetch(:param_file)} #{release_path}/.env"
else
fail Capistrano::Error, 'You must specify a valid parameter file.'
end
end
end

desc 'Create database.yml'
task :db do
on roles(:app) do
execute "cp #{release_path}/config/database.yml.example.production "\
"#{release_path}/config/database.yml"
end
end

desc 'Copy secrets.yml'
task :secrets do
on roles(:app) do
execute "cp #{release_path}/config/secrets.yml.example "\
"#{release_path}/config/secrets.yml"
end
end

desc 'Create party_foul initializer'
task :party_foul do
on roles(:app) do
# check for Party Foul parameter in .env file
env_lines = File.foreach("#{release_path}/.env")
if env_lines.grep(/PARTY_FOUL_TOKEN/).length > 0
# copy initializer
execute "cp -rf #{release_path}/config/initializers/party_foul.rb"\
".example #{release_path}/config/initializers/party_foul.rb"
end
end
end
end

0 comments on commit 6602cc7

Please sign in to comment.