Created
July 15, 2012 20:41
-
-
Save jverdeyen/3118544 to your computer and use it in GitHub Desktop.
Webistrano : Symfony2 Recipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
require "fileutils" | |
require "zlib" | |
#STDOUT.sync | |
$error = false | |
$pretty_errors_defined = false | |
# Be less verbose by default | |
#logger.level = Logger::IMPORTANT | |
# Symfony environment on local | |
set :symfony_env_local, "dev" | |
# Symfony environment | |
set :symfony_env_prod, "prod" | |
# PHP binary to execute | |
set :php_bin, "php" | |
# Symfony application path | |
set :app_path, "app" | |
# Symfony web path | |
set :web_path, "web" | |
# Symfony console bin | |
set :symfony_console, app_path + "/console" | |
# Symfony log path | |
set :log_path, app_path + "/logs" | |
# Symfony cache path | |
set :cache_path, app_path + "/cache" | |
# Symfony bin vendors | |
set :symfony_vendors, "bin/vendors" | |
# Symfony build_bootstrap script | |
set :build_bootstrap, "bin/build_bootstrap" | |
# Whether to use composer to install vendors. | |
# If set to false, it will use the bin/vendors script | |
set :use_composer, true | |
# Path to composer binary | |
# If set to false, Capifony will download/install composer | |
set :composer_bin, false | |
# Options to pass to composer when installing/updating | |
set :composer_options, "--no-scripts --verbose" | |
# Whether to update vendors using the configured dependency manager (composer or bin/vendors) | |
set :update_vendors, false | |
# run bin/vendors script in mode (upgrade, install (faster if shared /vendor folder) or reinstall) | |
set :vendors_mode, "install" | |
# copy venders from previous deploy (if available) | |
set :copy_vendors, true | |
# Whether to run cache warmup | |
set :cache_warmup, true | |
# Use AsseticBundle | |
set :dump_assetic_assets, true | |
# Assets install | |
set :assets_install, true | |
# Whether to update `assets_version` in `parameters.yml` | |
set :update_assets_version, true | |
# Files that need to remain the same between deploys | |
set :shared_files, ["app/config/parameters.yml"] | |
# Dirs that need to remain the same between deploys (shared dirs) | |
#set :shared_children, [log_path, web_path + "/uploads"] | |
set :shared_children, [log_path, "vendor"] | |
# Asset folders (that need to be timestamped) | |
set :asset_children, [web_path + "/css", web_path + "/images", web_path + "/js"] | |
# Dirs that need to be writable by the HTTP Server (i.e. cache, log dirs) | |
set :writable_dirs, [log_path, cache_path] | |
# Name used by the Web Server (i.e. www-data for Apache) | |
set :webserver_user, "www-data" | |
# Method used to set permissions (:chmod, :acl, or :chown) | |
set :permission_method, :acl | |
# Model manager: (doctrine, propel) | |
set :model_manager, "doctrine" | |
# Symfony2 version | |
set(:symfony_version) { guess_symfony_version } | |
# If set to false, it will never ask for confirmations (migrations task for instance) | |
# Use it carefully, really! | |
set :interactive_mode, false | |
namespace :deploy do | |
desc <<-DESC | |
Blank task exists as a hook into which to install your own environment \ | |
specific behaviour. | |
DESC | |
task :start, :roles => :app do | |
# Empty Task to overload with your platform specifics | |
end | |
desc <<-DESC | |
Blank task exists as a hook into which to install your own environment \ | |
specific behaviour. | |
DESC | |
task :stop, :roles => :app do | |
# Empty Task to overload with your platform specifics | |
end | |
desc "Overwrite the restart task because symfony doesn't need it." | |
task :restart do ; end | |
desc <<-DESC | |
Prepares one or more servers for deployment. Before you can use any \ | |
of the Capistrano deployment tasks with your project, you will need to \ | |
make sure all of your servers have been prepared with `cap deploy:setup'. When \ | |
you add a new server to your cluster, you can easily run the setup task \ | |
on just that server by specifying the HOSTS environment variable: | |
$ cap HOSTS=new.server.com deploy:setup | |
It is safe to run this task on servers that have already been set up; it \ | |
will not destroy any deployed revisions or data. | |
DESC | |
task :setup, :except => { :no_release => true } do | |
dirs = [deploy_to, releases_path, shared_path] | |
run "mkdir -p #{dirs.join(' ')}" | |
run "chmod g+w #{dirs.join(' ')}" if fetch(:group_writable, true) | |
end | |
desc <<-DESC | |
Sets permissions for writable_dirs folders as described in the Symfony documentation | |
(http://symfony.com/doc/master/book/installation.html#configuration-and-setup) | |
DESC | |
task :set_permissions, :roles => :app, :except => { :no_release => true } do | |
if writable_dirs && permission_method | |
dirs = [] | |
writable_dirs.each do |link| | |
if shared_children && shared_children.include?(link) | |
absolute_link = shared_path + "/" + link | |
else | |
absolute_link = latest_release + "/" + link | |
end | |
dirs << absolute_link | |
end | |
methods = { | |
:chmod => [ | |
"chmod +a \"#{user} allow delete,write,append,file_inherit,directory_inherit\" %s", | |
"chmod +a \"#{webserver_user} allow delete,write,append,file_inherit,directory_inherit\" %s" | |
], | |
:acl => [ | |
"setfacl -R -m u:#{user}:rwx -m u:#{webserver_user}:rwx %s", | |
"setfacl -dR -m u:#{user}:rwx -m u:#{webserver_user}:rwx %s" | |
], | |
:chown => ["chown #{webserver_user} %s"] | |
} | |
if methods[permission_method] | |
logger.info "--> Setting permissions" | |
if fetch(:use_sudo, false) | |
methods[permission_method].each do |cmd| | |
sudo sprintf(cmd, dirs.join(' ')) | |
end | |
elsif permission_method == :chown | |
puts " You can't use chown method without sudoing" | |
else | |
dirs.each do |dir| | |
is_owner = (capture "`echo stat #{dir} -c %U`").chomp == user | |
if is_owner && permission_method != :chown | |
methods[permission_method].each do |cmd| | |
try_sudo sprintf(cmd, dir) | |
end | |
else | |
puts " #{dir} is not owned by #{user} or you are using 'chown' method without ':use_sudo'" | |
end | |
end | |
end | |
puts_ok | |
else | |
puts " Permission method '#{permission_method}' does not exist." | |
end | |
end | |
end | |
desc "Symlinks static directories and static files that need to remain between deployments" | |
task :share_childs, :except => { :no_release => true } do | |
if shared_children | |
logger.info "--> Creating symlinks for shared directories" | |
shared_children.each do |link| | |
run "mkdir -p #{shared_path}/#{link}" | |
run "if [ -d #{release_path}/#{link} ] ; then rm -rf #{release_path}/#{link}; fi" | |
run "ln -nfs #{shared_path}/#{link} #{release_path}/#{link}" | |
end | |
puts_ok | |
end | |
if shared_files | |
logger.info "--> Creating symlinks for shared files" | |
shared_files.each do |link| | |
link_dir = File.dirname("#{shared_path}/#{link}") | |
run "mkdir -p #{link_dir}" | |
run "touch #{shared_path}/#{link}" | |
run "ln -nfs #{shared_path}/#{link} #{release_path}/#{link}" | |
end | |
puts_ok | |
end | |
end | |
desc "Updates latest release source path" | |
task :finalize_update, :except => { :no_release => true } do | |
run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true) | |
logger.info "--> Creating cache directory" | |
run "if [ -d #{latest_release}/#{cache_path} ] ; then rm -rf #{latest_release}/#{cache_path}; fi" | |
run "mkdir -p #{latest_release}/#{cache_path} && chmod -R 0777 #{latest_release}/#{cache_path}" | |
run "chmod -R g+w #{latest_release}/#{cache_path}" | |
set_permissions | |
puts_ok | |
share_childs | |
if fetch(:normalize_asset_timestamps, true) | |
stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S") | |
asset_paths = asset_children.map { |p| "#{latest_release}/#{p}" }.join(" ") | |
if asset_paths.chomp.empty? | |
logger.info " No asset paths found, skipped".yellow | |
else | |
logger.info "--> Normalizing asset timestamps" | |
run "find #{asset_paths} -exec touch -t #{stamp} {} ';' >/dev/null 2>&1 || true", :env => { "TZ" => "UTC" } | |
puts_ok | |
end | |
end | |
end | |
desc <<-DESC | |
Deploys and starts a `cold' application. This is useful if you have not \ | |
deployed your application before. | |
DESC | |
task :cold do | |
update | |
start | |
end | |
desc "Deploys the application and runs the test suite" | |
task :testall, :except => { :no_release => true } do | |
update_code | |
create_symlink | |
run "cd #{latest_release} && phpunit -c #{app_path} src" | |
end | |
desc "Runs the Symfony2 migrations" | |
task :migrate do | |
if model_manager == "doctrine" | |
symfony.doctrine.migrations.migrate | |
else | |
if model_manager == "propel" | |
logger.info " Propel doesn't have built-in migration for now".yellow | |
end | |
end | |
end | |
namespace :web do | |
desc <<-DESC | |
Present a maintenance page to visitors. Disables your application's web \ | |
interface by writing a "#{maintenance_basename}.html" file to each web server. The \ | |
servers must be configured to detect the presence of this file, and if \ | |
it is present, always display it instead of performing the request. | |
By default, the maintenance page will just say the site is down for \ | |
"maintenance", and will be back "shortly", but you can customize the \ | |
page by specifying the REASON and UNTIL environment variables: | |
$ cap deploy:web:disable \\ | |
REASON="hardware upgrade" \\ | |
UNTIL="12pm Central Time" | |
You can use a different template for the maintenance page by setting the \ | |
:maintenance_template_path variable in your deploy.rb file. The template file \ | |
should either be a plaintext or an erb file. | |
Further customization will require that you write your own task. | |
DESC | |
task :disable, :roles => :web, :except => { :no_release => true } do | |
require 'erb' | |
on_rollback { run "rm #{latest_release}/#{web_path}/#{maintenance_basename}.html" } | |
warn <<-EOHTACCESS | |
# Please add something like this to your site's Apache htaccess to redirect users to the maintenance page. | |
# More Info: http://www.shiftcommathree.com/articles/make-your-rails-maintenance-page-respond-with-a-503 | |
ErrorDocument 503 /#{maintenance_basename}.html | |
RewriteEngine On | |
RewriteCond %{REQUEST_URI} !\.(css|gif|jpg|png)$ | |
RewriteCond %{DOCUMENT_ROOT}/#{maintenance_basename}.html -f | |
RewriteCond %{SCRIPT_FILENAME} !#{maintenance_basename}.html | |
RewriteRule ^.*$ - [redirect=503,last] | |
# Or if you are using Nginx add this to your server config: | |
if (-f $document_root/maintenance.html) { | |
return 503; | |
} | |
error_page 503 @maintenance; | |
location @maintenance { | |
rewrite ^(.*)$ /maintenance.html last; | |
break; | |
} | |
EOHTACCESS | |
reason = ENV['REASON'] | |
deadline = ENV['UNTIL'] | |
template = File.read(maintenance_template_path) | |
result = ERB.new(template).result(binding) | |
put result, "#{latest_release}/#{web_path}/#{maintenance_basename}.html", :mode => 0644 | |
end | |
desc <<-DESC | |
Makes the application web-accessible again. Removes the \ | |
"#{maintenance_basename}.html" page generated by deploy:web:disable, which (if your \ | |
web servers are configured correctly) will make your application \ | |
web-accessible again. | |
DESC | |
task :enable, :roles => :web, :except => { :no_release => true } do | |
run "rm #{latest_release}/#{web_path}/#{maintenance_basename}.html" | |
end | |
end | |
end | |
namespace :database do | |
namespace :dump do | |
desc "Dumps remote database" | |
task :remote do | |
filename = "#{application}.remote_dump.#{Time.now.to_i}.sql.gz" | |
file = "/tmp/#{filename}" | |
sqlfile = "#{application}_dump.sql" | |
config = "" | |
run "cat #{current_path}/app/config/parameters.yml" do |ch, st, data| | |
config = load_database_config data, symfony_env_prod | |
end | |
case config['database_driver'] | |
when "pdo_mysql", "mysql" | |
run "mysqldump -u#{config['database_user']} --password='#{config['database_password']}' #{config['database_name']} | gzip -c > #{file}" do |ch, stream, data| | |
logger.info data | |
end | |
when "pdo_pgsql", "pgsql" | |
run "pg_dump -U #{config['database_user']} #{config['database_name']} --clean | gzip -c > #{file}" do |ch, stream, data| | |
logger.info data | |
end | |
end | |
FileUtils.mkdir_p("backups") | |
get file, "backups/#{filename}" | |
begin | |
FileUtils.ln_sf(filename, "backups/#{application}.remote_dump.latest.sql.gz") | |
rescue NotImplementedError # hack for windows which doesnt support symlinks | |
FileUtils.cp_r("backups/#{filename}", "backups/#{application}.remote_dump.latest.sql.gz") | |
end | |
run "rm #{file}" | |
end | |
desc "Dumps local database" | |
task :local do | |
filename = "#{application}.local_dump.#{Time.now.to_i}.sql.gz" | |
tmpfile = "backups/#{application}_dump_tmp.sql" | |
file = "backups/#{filename}" | |
config = load_database_config IO.read('app/config/parameters.yml'), symfony_env_local | |
sqlfile = "#{application}_dump.sql" | |
FileUtils::mkdir_p("backups") | |
case config['database_driver'] | |
when "pdo_mysql", "mysql" | |
`mysqldump -u#{config['database_user']} --password=\"#{config['database_password']}\" #{config['database_name']} > #{tmpfile}` | |
when "pdo_pgsql", "pgsql" | |
`pg_dump -U #{config['database_user']} #{config['database_name']} --clean > #{tmpfile}` | |
end | |
File.open(tmpfile, "r+") do |f| | |
gz = Zlib::GzipWriter.open(file) | |
while (line = f.gets) | |
gz << line | |
end | |
gz.flush | |
gz.close | |
end | |
begin | |
FileUtils.ln_sf(filename, "backups/#{application}.local_dump.latest.sql.gz") | |
rescue NotImplementedError # hack for windows which doesnt support symlinks | |
FileUtils.cp_r("backups/#{filename}", "backups/#{application}.local_dump.latest.sql.gz") | |
end | |
FileUtils.rm(tmpfile) | |
end | |
end | |
namespace :move_db do | |
desc "Dumps remote database, downloads it to local, and populates here" | |
task :to_local do | |
filename = "#{application}.remote_dump.latest.sql.gz" | |
config = load_database_config IO.read('app/config/parameters.yml'), symfony_env_local | |
sqlfile = "#{application}_dump.sql" | |
database.dump.remote | |
f = File.new("backups/#{sqlfile}", "a+") | |
gz = Zlib::GzipReader.new(File.open("backups/#{filename}", "r")) | |
f << gz.read | |
f.close | |
case config['database_driver'] | |
when "pdo_mysql", "mysql" | |
`mysql -u#{config['database_user']} --password=\"#{config['database_password']}\" #{config['database_name']} < backups/#{sqlfile}` | |
when "pdo_pgsql", "pgsql" | |
`psql -U #{config['database_user']} #{config['database_name']} < backups/#{sqlfile}` | |
end | |
FileUtils.rm("backups/#{sqlfile}") | |
end | |
desc "Dumps local database, loads it to remote, and populates there" | |
task :to_remote do | |
filename = "#{application}.local_dump.latest.sql.gz" | |
file = "backups/#{filename}" | |
sqlfile = "#{application}_dump.sql" | |
config = "" | |
database.dump.local | |
upload(file, "/tmp/#{filename}", :via => :scp) | |
run "gunzip -c /tmp/#{filename} > /tmp/#{sqlfile}" | |
run "cat #{shared_path}/config/databases.yml" do |ch, st, data| | |
config = load_database_config data, symfony_env_prod | |
end | |
case config['database_driver'] | |
when "pdo_mysql", "mysql" | |
run "mysql -u#{config['database_user']} --password='#{config['database_password']}' #{config['database_name']} < /tmp/#{sqlfile}" do |ch, stream, data| | |
logger.info data | |
end | |
when "pdo_pgsql", "pgsql" | |
run "psql -U #{config['database_user']} #{config['database_name']} < /tmp/#{sqlfile}" do |ch, stream, data| | |
logger.info data | |
end | |
end | |
run "rm /tmp/#{filename}" | |
run "rm /tmp/#{sqlfile}" | |
end | |
end | |
end | |
namespace :symfony do | |
namespace :doctrine do | |
namespace :cache do | |
desc "Clears all metadata cache for a entity manager" | |
task :clear_metadata do | |
logger.info "--> Clearing Doctrine metadata cache" | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:cache:clear-metadata --env=#{symfony_env_prod}" | |
puts_ok | |
end | |
desc "Clears all query cache for a entity manager" | |
task :clear_query do | |
logger.info "--> Clearing Doctrine query cache" | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:cache:clear-query --env=#{symfony_env_prod}" | |
puts_ok | |
end | |
desc "Clears result cache for a entity manager" | |
task :clear_result do | |
logger.info "--> Clearing Doctrine result cache" | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:cache:clear-result --env=#{symfony_env_prod}" | |
puts_ok | |
end | |
end | |
namespace :database do | |
desc "Creates the configured databases" | |
task :create do | |
logger.info "--> Creating databases" | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:database:create --env=#{symfony_env_prod}" | |
puts_ok | |
end | |
desc "Drops the configured databases" | |
task :drop do | |
logger.info "--> Dropping databases" | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:database:drop --env=#{symfony_env_prod}" | |
puts_ok | |
end | |
end | |
namespace :schema do | |
desc "Processes the schema and either create it directly on EntityManager Storage Connection or generate the SQL output" | |
task :create do | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:schema:create --env=#{symfony_env_prod}" | |
end | |
desc "Drops the complete database schema of EntityManager Storage Connection or generate the corresponding SQL output" | |
task :drop do | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:schema:drop --env=#{symfony_env_prod}" | |
end | |
end | |
namespace :migrations do | |
desc "Executes a migration to a specified version or the latest available version" | |
task :migrate do | |
currentVersion = nil | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:migrations:status --env=#{symfony_env_prod}" do |ch, stream, out| | |
if stream == :out and out =~ /Current Version:[^$]+\(([\w]+)\)/ | |
currentVersion = Regexp.last_match(1) | |
end | |
if stream == :out and out =~ /Current Version:\s*0\s*$/ | |
currentVersion = 0 | |
end | |
end | |
if currentVersion == nil | |
#raise "Could not find current database migration version #{currentVersion}" | |
end | |
logger.info "Current database version: #{currentVersion}" | |
on_rollback { | |
if !interactive_mode || Capistrano::CLI.ui.agree("Do you really want to migrate #{symfony_env_prod}'s database back to version #{currentVersion}? (y/N)") | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:migrations:migrate #{currentVersion} --env=#{symfony_env_prod} --no-interaction" | |
end | |
} | |
if !interactive_mode || Capistrano::CLI.ui.agree("Do you really want to migrate #{symfony_env_prod}'s database? (y/N)") | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:migrations:migrate --env=#{symfony_env_prod} --no-interaction" | |
end | |
end | |
desc "Views the status of a set of migrations" | |
task :status do | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:migrations:status --env=#{symfony_env_prod}" | |
end | |
end | |
namespace :mongodb do | |
namespace :schema do | |
desc "Allows you to create databases, collections and indexes for your documents" | |
task :create do | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:mongodb:schema:create --env=#{symfony_env_prod}" | |
end | |
desc "Allows you to drop databases, collections and indexes for your documents" | |
task :drop do | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:mongodb:schema:drop --env=#{symfony_env_prod}" | |
end | |
end | |
end | |
end | |
namespace :init do | |
desc "Mounts ACL tables in the database" | |
task :acl do | |
logger.info "--> Mounting Doctrine ACL tables" | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} init:acl --env=#{symfony_env_prod}" | |
puts_ok | |
end | |
end | |
namespace :propel do | |
namespace :database do | |
desc "Creates the configured databases" | |
task :create do | |
logger.info "--> Creating databases" | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} propel:database:create --env=#{symfony_env_prod}" | |
puts_ok | |
end | |
desc "Drops the configured databases" | |
task :drop do | |
logger.info "--> Dropping databases" | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} propel:database:drop --env=#{symfony_env_prod}" | |
puts_ok | |
end | |
end | |
namespace :build do | |
desc "Builds the Model classes" | |
task :model do | |
command = "propel:model:build" | |
if /2\.0\.[0-9]+.*/ =~ symfony_version | |
command = "propel:build-model" | |
end | |
logger.info "--> Generating Propel classes" | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} #{command} --env=#{symfony_env_prod}" | |
puts_ok | |
end | |
desc "Builds SQL statements" | |
task :sql do | |
command = "propel:sql:build" | |
if /2\.0\.[0-9]+.*/ =~ symfony_version | |
command = "propel:build-sql" | |
end | |
logger.info "--> Generating Propel SQL" | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} #{command} --env=#{symfony_env_prod}" | |
puts_ok | |
end | |
desc "Inserts SQL statements" | |
task :sql_load do | |
command = "propel:sql:insert" | |
if /2\.0\.[0-9]+.*/ =~ symfony_version | |
command = "propel:insert-sql" | |
end | |
logger.info "--> Inserting Propel SQL" | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} #{command} --force --env=#{symfony_env_prod}" | |
puts_ok | |
end | |
desc "Builds the Model classes, SQL statements and insert SQL" | |
task :all_and_load do | |
logger.info "--> Setting up Propel (classes, SQL)" | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} propel:build --insert-sql --env=#{symfony_env_prod}" | |
puts_ok | |
end | |
desc "Generates ACLs models" | |
task :acl do | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} propel:acl:init --env=#{symfony_env_prod}" | |
end | |
desc "Inserts propel ACL tables" | |
task :acl_load do | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} propel:acl:init --env=#{symfony_env_prod} --force" | |
end | |
end | |
end | |
task :default do | |
prompt_with_default(:task_arguments, "cache:clear") | |
stream "cd #{latest_release} && #{php_bin} #{symfony_console} #{task_arguments} --env=#{symfony_env_prod}" | |
end | |
namespace :assets do | |
desc "Updates assets version (in config.yml)" | |
task :update_version, :except => { :no_release => true } do | |
run "sed -i 's/\\(assets_version: \\)\\(.*\\)$/\\1 #{real_revision}/g' #{latest_release}/app/config/parameters.yml" | |
end | |
desc "Installs bundle's assets" | |
task :install, :except => { :no_release => true } do | |
logger.info "--> Installing bundle's assets" | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} assets:install #{web_path} --env=#{symfony_env_prod}" | |
puts_ok | |
end | |
end | |
namespace :assetic do | |
desc "Dumps all assets to the filesystem" | |
task :dump, :except => { :no_release => true } do | |
logger.info "--> Dumping all assets to the filesystem" | |
run "cd #{latest_release} && #{php_bin} #{symfony_console} assetic:dump --env=#{symfony_env_prod} --no-debug" | |
puts_ok | |
end | |
end | |
namespace :vendors do | |
desc "Runs the bin/vendors script to install the vendors (fast if already installed)" | |
task :install, :roles => :app, :except => { :no_release => true } do | |
pretty_print "--> Installing vendors" | |
run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_vendors} install'" | |
puts_ok | |
end | |
desc "Runs the bin/vendors script to reinstall the vendors" | |
task :reinstall, :roles => :app, :except => { :no_release => true } do | |
pretty_print "--> Reinstalling vendors" | |
run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_vendors} install --reinstall'" | |
puts_ok | |
end | |
desc "Runs the bin/vendors script to upgrade the vendors" | |
task :upgrade, :roles => :app, :except => { :no_release => true } do | |
pretty_print "--> Upgrading vendors" | |
run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_vendors} update'" | |
puts_ok | |
end | |
end | |
namespace :bootstrap do | |
desc "Runs the bin/build_bootstrap script" | |
task :build, :roles => :app, :except => { :no_release => true } do | |
pretty_print "--> Building bootstrap file" | |
if !remote_file_exists?("#{latest_release}/#{build_bootstrap}") && true == use_composer then | |
set :build_bootstrap, "vendor/sensio/distribution-bundle/Sensio/Bundle/DistributionBundle/Resources/bin/build_bootstrap.php" | |
run "#{try_sudo} sh -c 'cd #{latest_release} && test -f #{build_bootstrap} && #{php_bin} #{build_bootstrap} #{app_path} || echo '#{build_bootstrap} not found, skipped''" | |
else | |
run "#{try_sudo} sh -c 'cd #{latest_release} && test -f #{build_bootstrap} && #{php_bin} #{build_bootstrap} || echo '#{build_bootstrap} not found, skipped''" | |
end | |
puts_ok | |
end | |
end | |
namespace :composer do | |
desc "Gets composer and installs it" | |
task :get, :roles => :app, :except => { :no_release => true } do | |
pretty_print "--> Downloading Composer" | |
if !remote_file_exists?("#{latest_release}/composer.phar") | |
run "#{try_sudo} sh -c 'cd #{latest_release} && curl -s http://getcomposer.org/installer | #{php_bin}'" | |
else | |
run "#{try_sudo} cd #{latest_release} && #{php_bin} composer.phar self-update" | |
end | |
puts_ok | |
end | |
desc "Updates composer" | |
task :self_update, :roles => :app, :except => { :no_release => true } do | |
pretty_print "--> Updating Composer" | |
try_sudo "#{composer_bin} self-update" | |
puts_ok | |
end | |
desc "Runs composer to install vendors from composer.lock file" | |
task :install, :roles => :app, :except => { :no_release => true } do | |
if composer_bin | |
symfony.composer.self_update | |
else | |
symfony.composer.get | |
composer_bin = "#{php_bin} composer.phar" | |
end | |
pretty_print "--> Installing Composer dependencies" | |
run "#{try_sudo} sh -c 'cd #{latest_release} && #{composer_bin} install #{composer_options}'" | |
puts_ok | |
end | |
desc "Runs composer to update vendors, and composer.lock file" | |
task :update, :roles => :app, :except => { :no_release => true } do | |
if composer_bin | |
symfony.composer.self_update | |
else | |
symfony.composer.get | |
composer_bin = "#{php_bin} composer.phar" | |
end | |
pretty_print "--> Updating Composer dependencies" | |
run "#{try_sudo} sh -c 'cd #{latest_release} && #{composer_bin} update #{composer_options}'" | |
puts_ok | |
end | |
desc "Dumps an optimized autoloader" | |
task :dump_autoload, :roles => :app, :except => { :no_release => true } do | |
if composer_bin | |
symfony.composer.self_update | |
else | |
symfony.composer.get | |
composer_bin = "#{php_bin} composer.phar" | |
end | |
pretty_print "--> Dumping an optimized autoloader" | |
run "#{try_sudo} sh -c 'cd #{latest_release} && #{composer_bin} dump-autoload --optimize'" | |
puts_ok | |
end | |
end | |
end | |
def load_database_config(data, env) | |
parameters = YAML::load(data) | |
parameters['parameters'] | |
end | |
def guess_symfony_version | |
capture("cd #{latest_release} && #{php_bin} #{symfony_console} --version |cut -d \" \" -f 3") | |
end | |
def remote_file_exists?(full_path) | |
'true' == capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip | |
end | |
def puts_ok | |
if logger.level == Logger::IMPORTANT && !$error | |
logger.info 'ok' | |
end | |
$error = false | |
end | |
def pretty_errors | |
if !$pretty_errors_defined | |
$pretty_errors_defined = true | |
class << $stderr | |
@@firstLine = true | |
alias _write write | |
def write(s) | |
if @@firstLine | |
s = 'ok' << "\n" << s | |
@@firstLine = false | |
end | |
_write(s) | |
$error = true | |
end | |
end | |
end | |
end | |
after "deploy:finalize_update" do | |
if use_composer | |
if update_vendors | |
symfony.composer.update | |
else | |
symfony.composer.install | |
end | |
else | |
if update_vendors | |
vendors_mode.chomp # To remove trailing whiteline | |
case vendors_mode | |
when "upgrade" then symfony.vendors.upgrade | |
when "install" then symfony.vendors.install | |
when "reinstall" then symfony.vendors.reinstall | |
end | |
end | |
end | |
symfony.bootstrap.build | |
if model_manager == "propel" | |
symfony.propel.build.model | |
end | |
if assets_install | |
symfony.assets.install # 2. Publish bundle assets | |
end | |
if update_assets_version | |
symfony.assets.update_version # 4. Update `assets_version` | |
symfony.cache.clear | |
end | |
if cache_warmup | |
symfony.cache.warmup # 3. Warmup clean cache | |
end | |
if dump_assetic_assets | |
symfony.assetic.dump # 5. Dump assetic assets | |
end | |
end | |
def prompt_with_default(var, default, &block) | |
set(var) do | |
Capistrano::CLI.ui.ask("#{var} [#{default}] : ", &block) | |
end | |
set var, default if eval("#{var.to_s}.empty?") | |
end | |
before "deploy:update_code" do | |
msg = "--> Updating code base with #{deploy_via} strategy" | |
if logger.level == Logger::IMPORTANT | |
pretty_errors | |
logger.info msg | |
else | |
logger.info msg | |
end | |
end | |
after "deploy:create_symlink" do | |
logger.info "--> Successfully deployed!" | |
end | |
before 'symfony:composer:install', 'composer:copy_vendors' | |
before 'symfony:composer:update', 'composer:copy_vendors' | |
namespace :composer do | |
task :copy_vendors, :except => { :no_release => true } do | |
if update_vendors | |
logger.info "--> Copy vendor file from previous release" | |
run "vendorDir=#{current_path}/vendor; if [ -d $vendorDir ] || [ -h $vendorDir ]; then cp -a $vendorDir #{latest_release}/vendor; fi;" | |
puts_ok | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment