Skip to content

Instantly share code, notes, and snippets.

/gist:2d821924f03e56de839e Secret
Created Jul 30, 2014

Embed
What would you like to do?
# config valid only for Capistrano 3.1
lock '3.2.1'
set :application, 'the-site'
set :repo_url, 'git@bitbucket.org:thesite/the-site.git'
# Default branch is :master
ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call
set :symfony_env, :prod
set :tag_release, false
set :deploy_to, '/var/www/the-site'
set :tmp_dir, '/var/www/the-site/tmp'
# Default value for :scm is :git
# set :scm, :git
# Default value for :format is :pretty
# set :format, :pretty
# Default value for :log_level is :debug
# set :log_level, :debug
# Default value for :pty is false
# set :pty, true
# Default value for :linked_files is []
set :linked_files, %w{the-site/app/config/parameters.yml}
# Default value for linked_dirs is []
set :linked_dirs, %w{the-site/app/logs}
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
# Default value for keep_releases is 5
# set :keep_releases, 5
namespace :deploy do
desc 'Build application dependencies, etc'
task :build do
invoke 'deploy:build:vendors'
invoke 'deploy:build:bootstrap'
invoke 'deploy:build:cache'
invoke 'deploy:build:permissions'
# invoke 'deploy:build:assets'
end
desc 'Run pending Doctrine migrations'
task :migrate do
on primary :web do
within "#{release_path}/the-site" do
execute :php, 'app/console', 'doctrine:migrations:migrate',
'--no-interaction', "--env=#{fetch(:symfony_env)}"
end
end
end
desc 'Tag the release'
task :tag do
if fetch(:tag_release)
on primary :web do
within repo_path do
# tag and push upstream
tag = env.timestamp.strftime("%Y%m%d%H%M%S")
name = `git config user.name`.chomp
email = `git config user.email`.chomp
execute :git,
'-c', "user.name='#{name}'",
'-c', "user.email='#{email}'",
:tag, tag, fetch(:branch),
'-m', "'#{name} deployed #{fetch(:branch)} to #{fetch(:stage)}'"
execute :git, :push, repo_url, tag
end
end
end
end
namespace :build do
task :vendors do
on roles :web do
within "#{release_path}/the-site" do
timestamps = capture(:ls, '-xr', releases_path).split
if timestamps[1] then
execute :cp, '-R', "../../#{timestamps[1]}/the-site/vendor/", 'vendor/;', :true
end
execute :composer, :install, '--no-scripts', '--no-dev'
end
end
end
task :bootstrap do
on roles :web do
within "#{release_path}/the-site" do
execute :php, release_path.join(
'the-site', 'vendor', 'sensio', 'distribution-bundle', 'Sensio', 'Bundle',
'DistributionBundle', 'Resources', 'bin', 'build_bootstrap.php'
)
end
end
end
task :cache do
on roles :web do
within "#{release_path}/the-site" do
execute :php, 'app/console', 'cache:warmup',
"--env=#{fetch(:symfony_env)}"
end
end
end
task :permissions do
on roles :web do
within "#{release_path}/the-site" do
execute :chmod, 'g+w', 'app/cache'
end
end
end
task :assets do
invoke 'deploy:build:assets:symlink'
invoke 'deploy:build:assets:assetic'
end
namespace :assets do
task :symlink do
on roles :web do
within "#{release_path}/the-site" do
execute :php, 'app/console', 'assets:install', 'web',
'--symlink', '--relative', "--env=#{fetch(:symfony_env)}"
end
end
end
task :assetic do
on roles :web do
within "#{release_path}/the-site" do
execute :php, 'app/console', 'assetic:dump', 'web',
"--env=#{fetch(:symfony_env)}"
end
end
end
end
end
after :updated, 'deploy:build'
# after :updated, 'deploy:migrate'
after :finished, 'deploy:tag'
after :finished, 'deploy:cleanup'
end
def random_string(length)
(0...length).map { (65 + rand(26)).chr }.join
end
def extract_yaml_cmd(file, key)
%(perl -ne 'print $1 if /#{key}:\\s*(\\S+)\\s*\$/' #{file})
end
def db_config_path
File.join(fetch(:deploy_to), 'shared/the-site/app/config/parameters.yml')
end
set :dbs, %w[iredge ee ircameras]
namespace :db do
namespace :pull do
task :download_db, :db, :path do |t, args|
db = args[:db]
path = args[:path]
on primary :source do |server|
fname = "#{db}-#{random_string 6}.sql.gz"
remote_path = "/tmp/#{fname}"
local_path = remote_path
info "Looking for credentials inside #{db_config_path}"
user = capture(extract_yaml_cmd(db_config_path, 'database_user'), verbosity: :trace)
pass = capture(extract_yaml_cmd(db_config_path, 'database_password'), verbosity: :trace)
info "Dumping the satabase into #{remote_path}"
execute "mysqldump -u #{user} --password='#{pass}' #{db} | gzip > #{remote_path}", verbosity: :trace
download! remote_path, local_path
info "Unzipping to #{path}"
system "gzip -d -c #{local_path} > #{path}"
end
on primary :db do |server|
if fetch(:stage) == :local
info "Using local (TODO)"
info "$ mysql -u deploy --password=xxx -D #{db}"
else
remote_path = "/tmp/#{db}-#{random_string 6}.sql"
upload! path, remote_path
user = capture(extract_yaml_cmd(db_config_path, 'database_user'), verbosity: :trace)
pass = capture(extract_yaml_cmd(db_config_path, 'database_password'), verbosity: :trace)
info "Restoring database(#{db}) from #{remote_path}"
execute "mysql -u #{user} --password='#{pass}' -D #{db} < #{remote_path}", verbosity: :trace
end
end
end
(fetch :dbs).each do |t|
desc "pull the #{t} database"
task :"#{t}" do
Rake::Task["db:pull:download_db"].invoke t, "/tmp/#{t}.sql"
Rake::Task["db:pull:download_db"].reenable
end
end
desc "Pull all databases"
task :all do
(fetch :dbs).each do |t|
Rake::Task["db:pull:#{t}"].execute
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.