Capistrano - A remote server automation tool

Capistrano

This is a work in progress. May contain wrong / obsolete information.

Git setup for Capistrano :

+----+       +----------+     +------------+
|GIT +--<->--+ DELIVERY +-->--+ WEB SERVER |
+----+       +----------+     +------------+

The delivery host gets content from GIT, then sends it to the web server.

On delivery host, get SSH keys so that you can do :
	- ssh git@myGitHost -i ~/.ssh/id_rsa.myGitHost
	- ssh <web server>

git@myGitHost:/path/to/projectName

cd [project directory]
git clone git@myGitHost:/path/to/projectName

	Cloning into 'projectName'...
	remote: Counting objects: 3279, done.
	remote: Compressing objects: 100% (1529/1529), done.
	remote: Total 3279 (delta 1598), reused 2609 (delta 1235)
	Receiving objects: 100% (3279/3279), 571.76 KiB, done.
	Resolving deltas: 100% (1598/1598), done.
	warning: remote HEAD refers to nonexistent ref, unable to checkout.

git ls-remote git@myGitHost:/path/to/projectName
	5124fb1090e00733738d3c876f0339befb56d6e6	refs/heads/analytics
	38f08f318fe2cac0d473ce992e4ebd906d5f0631	refs/heads/develop

cd projectName;git branch -a
	remotes/origin/analytics
	remotes/origin/develop

git checkout remotes/origin/develop
	source : https://drupal.org/node/2053869

Install Capistrano :

aptitude install ruby
install RubyGems (see below)
gem install capifony
	==> install directory : /var/lib/gems/1.9.1/gems/capifony-2.3.0
cd ~/mySymfonyProject/

make local repo :
	repo='myRepoName'; git clone git@myGitHost:/path/to/$repo; git branch -a; cd $repo; git checkout remotes/origin/develop

capifony .

edit ~/tmp/capitest/app/config/deploy.rb

cap deploy:setup (run just once !)

cap deploy

Hooks :

https://github.com/capistrano/capistrano/wiki/Capistrano-Tasks
http://makandracards.com/makandra/1176-which-capistrano-hooks-to-use-for-events-to-happen-on-both-cap-deploy-and-cap-deploy-migrations

VARIABLES IN CONFIG FILES :

http://stackoverflow.com/questions/4902635/prompt-for-username-password-on-login-with-capistrano#adzerk1807650935

Run commands from tasks :

system("echo -n 'MY NAME IS : ';hostname")
Executed locally (where Capistrano is installed). Echoes the shell command.
run("echo -n 'MY NAME IS : ';hostname")
Executed remotely : on the server identified by the server variable.
run_locally("echo -n 'MY NAME IS : ';hostname")
Executed locally. No echo of the command.
hostname = capture("hostname")
Executed remotely : on the server identified by the server variable.

MULTISTAGE VARIABLES :

https://github.com/capistrano/capistrano/wiki/2.x-Multistage-Extension#caveats
http://stackoverflow.com/questions/4137493/capistrano-multistage-variables
https://groups.google.com/forum/#!topic/capistrano/B_HQsovUhQQ

# in config/deploy/production.rb
set :rails_env, 'production'

# in config/deploy.rb
set(:deploy_to) { "/home/#{user}/web/#{rails_env}" }

rails_env and 'production' :
	https://github.com/sshingler/capistrano-resque/pull/15

Capistrano internal variables (source) :

LOG LEVELS :
	logger.level = Logger::MAX_LEVEL

	# IMPORTANT	= 0
	# INFO		= 1
	# DEBUG		= 2
	# TRACE		= 3
	# MAX_LEVEL	= 3
http://jondavidjohn.com/blog/2012/04/cleaning-up-capistrano-deployment-output
http://jondavidjohn.com/blog/2013/06/capistrano-output-cleanup-with-steps

CAPISTRANO RECIPES :

/var/lib/gems/1.9.1/gems/capistrano-2.14.2/lib/capistrano/recipes

deploy.rb

	deploy							deploy the app (after a successful "cold" deploy 1st)
		update						copy files and update the symlink
			update_code				copy files (code and assets) to the remote server(s). cf "deploy_via" and "strategy"
				strategy.deploy
				finalize_update
			create_symlink
		restart

Links :

https://github.com/capistrano/capistrano/wiki/2.x-Significant-Configuration-Variables
	https://github.com/peritor/webistrano/wiki/Configuration-Parameters
	http://rdoc.info/github/capistrano/capistrano/master/Capistrano/Deploy/Strategy/Copy
	http://brighternet.com/articles/deploying-static-sites-with-capistrano/
	http://brighternet.com/assets/articles/deploy.txt

CAP DEPLOY :

cap deploy:setup		: create directories on server
	cap deploy:check		: check your local environment and your server and locate likely problems
	cap deploy:cold		: copy code, build database, make symlinks
	cap deploy				: copy code, make symlinks
	cap deploy:cleanup	: delete old releases on the server and keep only "keep_releases" releases (?)

cap -T	: list available Capistrano tasks
cap -vT	: same as above with increased verbosity

cap deploy:setup && cap deploy:cold && cap deploy

Hooks :
	https://github.com/capistrano/capistrano/wiki/Capistrano-Tasks

CREATE A NEW RECIPE :
	http://guides.beanstalkapp.com/deployments/deploy-with-capistrano.html#recipe-validate

uptodate:git

capistrano-uptodate is capistrano extension that automatically check your local repository with remote repository and if repository is not up-to-date:

	display corresponding message.
	ask to confirm recipe execution or abort recipe execution.

https://github.com/railsware/capistrano-uptodate