public
Created — forked from josegonzalez/deploy.rb

Capistrano Deploy Script for CakePHP applications using GIT (Update the $config hash))

  • Download Gist
deploy.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
$config = {
"application" => "DOMAIN.TLD",
"repository" => "git@GITHOST:USERNAME/REPOSITORYNAME.git",
"remoteusername" => "REMOTEUSERNAME",
"cake_folder" => "/PATH/TO/CAKE",
"cake_version" => "cakephp1.3",
"plugin_dir" => "plugins",
"servers" => {
"prod" => {
"server" => "APPLICATION.TLD",
"application" => "APPLICATION.TLD",
"deploy_to" => "/PATH/TO/APP/PROD"
},
"dev" => {
"server" => "DEV.APPLICATION.TLD",
"application" => "DEV.APPLICATION.TLD",
"deploy_to" => "/PATH/TO/APP/DEV"
}
}
}
 
# The application name. Pretty arbitrary, doesn't affect anything I think
set :application, $config["application"]
# Where is the repository held? Depends on your application
set :repository, $config["repository"]
# Deploy as this username
set :user, $config["remoteusername"]
# Do NOT use sudo by default. Helps with file permissions. You can still
# manually sudo by prepending #{sudo} to run commands
set :use_sudo, false
 
# SCM Settings
# Use git to deploy. You can also set this to 'svn'
set :scm, :git
# Only deploy the master branch
set :branch, "master"
# Keep Git quiet
set :scm_verbose, false
 
## Deploy Settings
# Deploy via a remote repository cache. In git's case, it
# does a "git fetch" on the remote cache before moving it into place
set :deploy_via, :remote_cache
# Overriding my 'current' directory to public, as that's how I roll
set :current_dir, "public"
 
## Deploy Specific settings
# The folder holding all of my CakePHP core stuff,
# like plugins and the individual cores
set :cake_folder, $config["cake_folder"]
# Folder name of the specific cakephp version I want to use.
# This is a raw checkout straight from github
# Assumes you have this folder in the :cake_folder directory
set :cake_version, $config["cake_version"]
# The plugin directory (relative to :cake_folder) to be deployed
set :plugin_dir, $config["plugin_dir"]
 
## SSH Options
# Deploy as this username
set :ssh_options, :username => $config["remoteusername"]
# SSH Agent forwarding, sends my personal keys for usage by git when deploying.
set :ssh_options, :forward_agent => true
 
## Available Environments
task :prod do
server $config["servers"]["prod"]["server"], :web, :god
set :application, $config["servers"]["prod"]["application"]
set :deploy_to, $config["servers"]["prod"]["deploy_to"]
set :branch, :master
end
 
task :dev do
role :web, $config["servers"]["dev"]["server"]
set :application, $config["servers"]["dev"]["application"]
set :deploy_to, $config["servers"]["dev"]["deploy_to"]
set :branch, ENV['branch'] if ENV.has_key?('branch') && ENV['branch'] =~ /[\w_-]+/i
end
 
## Deployment tasks
namespace :deploy do
task :start do
end
 
task :stop do
end
 
desc "Override the original :restart"
task :restart, :roles => :app do
# after "deploy:restart", "misc:clear_cache"
end
 
desc "Override the original :migrate"
task :migrate do
end
 
desc <<-DESC
Symlinks shared configuration and directories into the latest release
Also clear persistent and model cache and sessions and symlink for usability.
DESC
task :finalize_update do
before "deploy:symlink", "link:core", "link:plugins", "link:config", "link:tmp", "misc:submodule"
end
 
desc <<-DESC
Copies over the latest release. Necessary unless we place the cake core inside releases
For larger repositories, something different should be tried instead
DESC
task :symlink do
run "rm -rf #{deploy_to}/#{current_dir} && cp -rf #{latest_release} #{deploy_to}/#{current_dir}"
end
 
end
 
## Link tasks
namespace :link do
desc <<-DESC
Link the CakePHP Core
You may need to change this to a 'cp -rf' instead of 'ln -s' depending upon your shell requirements
DESC
task :core do
run "rm -rf #{deploy_to}/cake && cp -rf #{cake_folder}/#{cake_version}/cake #{deploy_to}/cake"
end
 
desc "Link the CakePHP Plugins for this repository"
task :plugins do
run "rm -rf #{deploy_to}/plugins && cp -rf #{cake_folder}/#{plugin_dir} #{deploy_to}/plugins"
end
 
desc <<-DESC
Link the configuration files
May fail if you are not using the asset_compress plugin
DESC
task :config do
run [
"rm -rf #{current_release}/config/core.php",
"ln -s #{shared_path}/config/core.php #{current_release}/config/core.php",
 
"rm -rf #{current_release}/config/database.php",
"ln -s #{shared_path}/config/database.php #{current_release}/config/database.php",
 
"rm -rf #{current_release}/config/bootstrap.php",
"ln -s #{shared_path}/config/bootstrap.php #{current_release}/config/bootstrap.php",
 
"rm -rf #{current_release}/webroot/cache_css",
"ln -s #{shared_path}/webroot/cache_css #{current_release}/webroot/cache_css",
 
"rm -rf #{current_release}/webroot/cache_js",
"ln -s #{shared_path}/webroot/cache_js #{current_release}/webroot/cache_js",
 
"rm -rf #{current_release}/webroot/uploads",
"ln -s #{shared_path}/webroot/uploads #{current_release}/webroot/uploads",
 
"rm -rf #{current_release}/webroot/files",
"ln -s #{shared_path}/webroot/files #{current_release}/webroot/files",
].join(' && ')
end
 
desc "Link the temporary directory"
task :tmp do
run "rm -rf #{current_release}/tmp && ln -s #{shared_path}/tmp #{current_release}/tmp"
end
 
end
 
## Miscellaneous tasks
namespace :misc do
desc "Blow up all the cache files CakePHP uses, ensuring a clean restart."
task :clear_cache do
# Remove absolutely everything from TMP
run "rm -rf #{shared_path}/tmp/*"
 
# Create TMP folders
run [
"rm -rf #{shared_path}/tmp/*",
"rm -rf #{shared_path}/webroot/cache_css/*",
"rm -rf #{shared_path}/webroot/cache_js/*",
 
"mkdir -p #{shared_path}/tmp/cache/models",
"mkdir -p #{shared_path}/tmp/cache/persistent",
"mkdir -p #{shared_path}/tmp/cache/views",
"mkdir -p #{shared_path}/tmp/sessions",
"mkdir -p #{shared_path}/tmp/logs",
"mkdir -p #{shared_path}/tmp/tests",
 
"chmod -R 777 #{shared_path}/tmp",
].join(' && ')
end
 
desc "Startup a new deployment"
task :startup do
# symlink the cake core folder to where we need it
after "misc:startup", "link:core", "link:plugins"
 
run [
# Setup shared folders
"mkdir -p #{shared_path}/tmp/cache/models",
"mkdir -p #{shared_path}/tmp/cache/persistent",
"mkdir -p #{shared_path}/tmp/cache/views",
"mkdir -p #{shared_path}/tmp/sessions",
"mkdir -p #{shared_path}/tmp/logs",
"mkdir -p #{shared_path}/tmp/tests",
"mkdir -p #{shared_path}/webroot/files",
"mkdir -p #{shared_path}/webroot/uploads",
"mkdir -p #{shared_path}/webroot/cache_css",
"mkdir -p #{shared_path}/webroot/cache_js",
 
# Make the TMP and Uploads folder writeable
"chmod -R 777 #{shared_path}/tmp",
"chmod -R 644 #{shared_path}/webroot/cache_css #{shared_path}/webroot/cache_js",
"chmod -R 755 #{shared_path}/tmp #{shared_path}/webroot/uploads #{shared_path}/webroot/files"
].join(' && ')
end
 
desc "Initialize the submodules and update them"
task :submodule do
run "cd #{current_release} && git submodule init && git submodule update"
end
 
desc "Tail the log files"
task :tail do
run "tail -f #{deploy_to}/logs/*.log"
end
end
 
## Tasks involving migrations
namespace :migrate do
desc "Run CakeDC Migrations"
task :all do
run "cd #{deploy_to}/#{current_dir} && ../cake/console/cake -app #{deploy_to}/#{current_dir} migration run all"
end
 
desc "Run CakeDC Migrations"
task :status do
run "cd #{deploy_to}/#{current_dir} && ../cake/console/cake -app #{deploy_to}/#{current_dir} migration status"
end
end
 
## Tasks involving God+CakeDJJob
namespace :god do
task :stop, :roles => :god do
run "#{sudo} service god stop"
end
 
task :start, :roles => :god do
run "#{sudo} service god start"
end
 
task :status, :roles => :god do
run "#{sudo} service god status"
end
 
task :restart, :roles => :god do
run [
"#{sudo} service god stop",
"#{sudo} rm /etc/god/conf.d/workers.god",
"#{sudo} rm /etc/god/conf.d/cakephp_god.rb",
"#{sudo} ln -s #{current_release}/config/workers.god /etc/god/conf.d/workers.god",
"#{sudo} ln -s #{current_release}/config/cakephp_god.rb /etc/god/conf.d/cakephp_god.rb",
"#{sudo} service god start"
].join(' && ')
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.