Skip to content
Browse files

* DRY'ed all code of main script

* Added proper definition of often used tasks (`cap symfony:cc`, `cap symfony:doctrine:build_all_and_load`, `cap symfony:project:permissions` etc.)
* Removed `system`, `pids` and other Rails-specific folders from shared path
* :create_dirs now touches `#{shared_path}/databases.yml`, so you can create it later
* Added database configure task with user input (`cap symfony:configure:database`)
* Added command to run custom symfony tasks (`cap symfony:run_task`)
* :finalize_update now publishes plugin assets & clears non-production controllers
* Bunch of symfony & doctrine tasks out of the box
  • Loading branch information...
1 parent 8e42141 commit 6e30b73950b5e7c4df13d4fb20de4d2b3ce0ed8b @everzet committed Jun 3, 2010
Showing with 153 additions and 63 deletions.
  1. +13 −7 README.md
  2. +125 −40 config/capifony.rb
  3. +15 −16 config/deploy.rb
View
20 README.md
@@ -36,21 +36,22 @@ This command will create the following folder structure on your server:
|-- 20100509150741
|-- 20100509145325
|-- shared
- |-- cached-copy
|-- log
- |-- pids
- |-- system
+ |-- web
+ |-- uploads
-The folders in the releases directory will be the actual deployed code, timestamped. The pids folder in the shared directory is only used for Rails applications, so you can ignore it completely. Capistrano symlinks your log directory from your app to the log directory in the shared folder so that it doesn’t get erased when you deploy a new version of your code.
-
-This is an important step! Before you deploy the application for the first time, it’s really a good idea to scp your databases.yml file up to your server so you can set the proper credentials for your production server and keep it out of your repository. Make sure you put the databases.yml file into the shared/system directory, because we already have a task that will symlink it when each new code version is deployed.
+The folders in the releases directory will be the actual deployed code, timestamped. Capistrano symlinks your log & web/uploads directories from your app to the directories in the shared folder so that it doesn’t get erased when you deploy a new version of your code.
Now, to deploy your application for the first time, you can run:
cap deploy:cold
This will deploy your application, create the db, models, forms, filters, and run all of your migrations.
+To configure database on production environment, run:
+
+ cap symfony:configure:database
+
Now, whenever you need to deploy a new version of your code, you can just run:
cap deploy
@@ -61,10 +62,14 @@ If you need to deploy and run your migrations you can call:
We’ve also added a custom task to run your test suite on the production server. You can invoke this by calling:
- cap deploy:testall
+ cap deploy:tests:all
This will deploy the application, rebuild the test database, then run all of the tests.
+Also, you have command to run your custom symfony tasks:
+
+ cap symfony:run_task
+
If you want to see all of the Capistrano tasks available, you can run:
cap -T
@@ -74,4 +79,5 @@ We’ve been using this setup for a little while now, and it’s saved us a ton
Contributors
============
+* everzet (owner): [http://github.com/everzet](http://github.com/everzet)
* Travis Roberts (creator of improved version): [http://blog.centresource.com/author/troberts/](http://blog.centresource.com/author/troberts/)
View
165 config/capifony.rb
@@ -1,46 +1,53 @@
-set :app_symlinks, %w{uploads} # dirs that need to remain the same between deploys
+# Dirs that need to remain the same between deploys (shared dirs)
+set :shared_children, %w(log web/uploads)
+
+def prompt_with_default(var, default)
+ set(var) do
+ Capistrano::CLI.ui.ask "#{var} [#{default}] : "
+ end
+ set var, default if eval("#{var.to_s}.empty?")
+end
-# =============================================================================
-# OVERWRITE CAPISTRANO TASKS
-# =============================================================================
namespace :deploy do
desc "Overwrite the start task to set the permissions on the project."
task :start do
- run "php #{release_path}/symfony project:permissions"
- run "php #{release_path}/symfony doctrine:build --all --and-load --no-confirmation"
+ symfony.project.permissions
+ doctrine.build_all_and_load
end
-
+
desc "Overwrite the restart task because symfony doesn't need it."
task :restart do ; end
-
+
desc "Overwrite the stop task because symfony doesn't need it."
task :stop do ; end
-
- desc "Customize migrate task to work with symfony."
+
+ desc "Customize migrate task because symfony doesn't need it."
task :migrate do
- run "php #{latest_release}/symfony doctrine:migrate --env=prod"
+ doctrine.migrate
end
-
+
desc "Symlink static directories that need to remain between deployments."
task :create_dirs do
- if app_symlinks
- app_symlinks.each do |link|
- run "mkdir -p #{shared_path}/system/#{link}"
- run "ln -nfs #{shared_path}/system/#{link} #{release_path}/web/#{link}"
+ if shared_children
+ shared_children.each do |link|
+ run "mkdir -p #{shared_path}/#{link}"
+ run "ln -nfs #{shared_path}/#{link} #{release_path}/#{link}"
end
end
+
+ run "touch #{shared_path}/databases.yml"
end
-
+
desc "Customize the finalize_update task to work with symfony."
task :finalize_update, :except => { :no_release => true } do
run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
+ run "mkdir -p #{latest_release}/cache"
- run <<-CMD
- rm -rf #{latest_release}/log #{latest_release}/cache &&
- mkdir -p #{shared_path}/cache &&
- ln -s #{shared_path}/log #{latest_release}/log &&
- ln -s #{shared_path}/cache #{latest_release}/cache
- CMD
+ # Symlink directories
+ create_dirs
+
+ # Rotate log file
+ symfony.log.rotate
if fetch(:normalize_asset_timestamps, true)
stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
@@ -54,38 +61,116 @@
update
start
end
-
- desc "Task to run all the tests for the application."
- task :symfony_test do
- run "php #{latest_release}/symfony test:all"
- end
- desc "Task used in conjunction with the symfony_test task to rebuild the database."
- task :rebuild do
- run "php #{latest_release}/symfony doctrine:build --all --and-load --env=test --no-confirmation"
- end
-
desc "Deploy the application and run the test suite."
task :testall do
update_code
symlink
- rebuild
- symfony_test
+ doctrine.build_all_and_load_test
+ symfony.tests.all
end
end
namespace :symlink do
desc "Symlink the database"
task :db do
- run "ln -nfs #{shared_path}/databases.yml #{release_path}/config/databases.yml"
+ run "ln -nfs #{shared_path}/databases.yml #{latest_release}/config/databases.yml"
end
end
namespace :symfony do
- desc "Task to clear the cache on deploy."
- task :clear_cache do
- run "php #{release_path}/symfony cache:clear"
+ desc "Clears the cache"
+ task :cc do
+ run "php #{latest_release}/symfony cache:clear"
+ end
+
+ desc "Runs custom symfony task"
+ task :run_task do
+ prompt_with_default(:task_arguments, "cache:clear")
+
+ run "php #{latest_release}/symfony #{task_arguments}"
+ end
+
+ namespace :configure do
+ desc "Configure database DSN"
+ task :database do
+ prompt_with_default(:dsn, "mysql:host=localhost;dbname=example_dev")
+ prompt_with_default(:user, "root")
+ prompt_with_default(:pass, "")
+ dbclass = "sfDoctrineDatabase"
+
+ run "php #{latest_release}/symfony configure:database --class=#{dbclass} '#{dsn}' '#{user}' '#{pass}'"
+ end
+ end
+
+ namespace :project do
+ desc "Fixes symfony directory permissions"
+ task :permissions do
+ run "php #{latest_release}/symfony project:permissions"
+ end
+
+ desc "Optimizes a project for better performance"
+ task :optimize do
+ run "php #{latest_release}/symfony project:optimize"
+ end
+
+ desc "Clears all non production environment controllers"
+ task :clear_controllers do
+ run "php #{latest_release}/symfony project:clear-controllers"
+ end
+ end
+
+ namespace :plugin do
+ desc "Publishes web assets for all plugins"
+ task :publish_assets do
+ run "php #{latest_release}/symfony plugin:publish-assets"
+ end
+ end
+
+ namespace :log do
+ desc "Clears log files"
+ task :clear do
+ run "php #{latest_release}/symfony log:clear"
+ end
+
+ desc "Rotates an application's log files"
+ task :rotate do
+ run "php #{latest_release}/symfony log:rotate"
+ end
+ end
+
+ namespace :tests do
+ desc "Task to run all the tests for the application."
+ task :all do
+ run "php #{latest_release}/symfony test:all"
+ end
+ end
+end
+
+namespace :doctrine do
+ desc "Migrates database to current version"
+ task :migrate do
+ run "php #{latest_release}/symfony doctrine:migrate --env=prod"
+ end
+
+ desc "Generate code & database based on your schema"
+ task :build_all do
+ run "php #{latest_release}/symfony doctrine:build --all --no-confirmation --env=prod"
+ end
+
+ desc "Generate code & database based on your schema & load fixtures"
+ task :build_all_and_load do
+ run "php #{latest_release}/symfony doctrine:build --all --and-load --no-confirmation --env=prod"
+ end
+
+ desc "Generate code & database based on your schema & load fixtures for test environment"
+ task :build_all_and_load_test do
+ run "php #{latest_release}/symfony doctrine:build --all --and-load --no-confirmation --env=test"
end
end
-after "deploy:finalize_update", "symlink:db", "deploy:create_dirs", "symfony:clear_cache"
+after "deploy:finalize_update", # After finalizing update:
+ "symlink:db", # 1. Symlink database
+ "symfony:cc", # 2. Clear cache
+ "symfony:plugin:publish_assets", # 3. Publish plugin assets
+ "symfony:project:permissions" # 4. Fix project permissions
View
31 config/deploy.rb
@@ -2,34 +2,33 @@
# REQUIRED VARIABLES
# =============================================================================
set :application, "my_app"
-set :domain, "mydomain.com"
+set :domain, "#{application}.com"
set :deploy_to, "/var/www/#{domain}"
-set :user, "username"
# =============================================================================
# SCM OPTIONS
# =============================================================================
-set :scm, :git # or :subversion
-set :scm_user, "username" # optional
-set :scm_password, "password" # optional
-set :repository, "http://svn.myrepo.com/#{application}/trunk/"
-
-# =============================================================================
-# SSH OPTIONS
-# =============================================================================
-set :user, "username"
-set :use_sudo, false # optional
+set :scm, :git # or :subversion
+set :repository, "#{domain}:/reps/#{application}.git"
+#set :scm_user, "username" # optional
+#set :scm_password, "password" # optional
# =============================================================================
# ROLES
# =============================================================================
# Modify these values to execute tasks on a different server.
-role :web, domain
-role :app, domain
-role :db, domain, :primary => true
+role :web, domain
+role :app, domain
+role :db, domain, :primary => true
# =============================================================================
# CAPISTRANO OPTIONS
# =============================================================================
set :keep_releases, 3
-set :deploy_via, :remote_cache
+set :deploy_via, :remote_cache
+
+# =============================================================================
+# SSH OPTIONS (if hasn't got ssh-key for server)
+# =============================================================================
+#set :user, "username"
+#set :use_sudo, false # optional

0 comments on commit 6e30b73

Please sign in to comment.
Something went wrong with that request. Please try again.