Deploying CakePHP applications using Capistrano and Chef. Covering both server - MySQL/nginx/php - and application deployment, as well as existing tools and usage examples.
Rants at http://josediazgonzalez.com • Harasses as @savant • Codes as josegonzalez on github • EMPLOYED at @seatgeek I’m also hot like sriracha sauce ➘ me ➘ not me
Ruby • Developers who don’t have time to waste with sysops • Sysadmins who are looking to automate more of their workflow Also anyone who couldn’t get into Mark’s talk
Convergence implies we may never be within compliance • Community maintained packages? # Every policy must have a bundlesequence body common control { bundlesequence => { "test" }; inputs => { "cfengine_stdlib.cf" }; } bundle agent test { files: # This is a throw-‐away comment, below is a full-‐bodied promise "/tmp/testfile" # promiser comment => "This is for keeps...", # Live comment create => "true", # Constraint 1 perms => m("612"); # Constraint 2, rw-‐-‐-‐x-‐ w-‐ } I love it when an automation tool makes a promise
wheel • Officially-sanctioned Cookbooks • Community-supported Cookbooks • All the cool rails developers are using it • Awesome logo • Screams when things are broken • Extensive Documentation And the obvious Chef/CakePHP pun
or more cookbooks • Cookbooks are like plugins (nginx, apache, memcache) that can depend upon/require one another • Cookbooks have recipes, libraries, templates, definitions, files • Recipes are sets of instructions I vote we rename “plugins” to “recipes”
group "deploy" end ➘ ➘ resource name of resource options WHAT’S IN A RESOURCE CALL? http://wiki.opscode.com/display/chef/Resources ➘ special node var ➘
params[:name] do source "html.erb" owner "deploy" group "deploy" mode 0644 variables(params[:variables]) end nginx_site params[:hostname] do action :enable end end # Usage nginx_up "#{node[:nginx][:dir]}/sites-‐available/#{hostname}.#{base}" do hostname "#{info[:hostname]}.#{info[:base]}" variables(info[:variables]) end “Fake” resources, for more info see http://wiki.opscode.com/display/chef/Providers Resources on the Cheap ➘ Just chain resources ➘ Fire at will
directory "#{node[:server][:production][:dir]}/#{hostname}/#{base}" do owner "deploy" group "deploy" mode "0755" recursive true end git "#{node[:server][:production][:dir]}/#{hostname}/#{base}/public" do repository info[:repository] user "deploy" group "deploy" end nginx_up "#{node[:nginx][:dir]}/sites-‐available/#{hostname}.#{base}" do hostname "#{hostname}.#{base}" variables(info[:variables]) end end end ALL TOGETHER NOW Simplified version of something in production use
DNA file • dna.json reference both recipes and configurations • Modifying DNA should reconfigure server • DNA can be versioned with the app You too can be a Geneticist
everywhere • Share, collaborate, grow optimized server cookbooks • Define servers in terms of DNA files: lb.json, db.json, web.json, etc. • Automate server deployment: • server new 127.0.0.1 lb.json • CakePHP http://bit.ly/cakechef Contributions welcome, documentation being fleshed out
- has deployment strategies • Separates related tasks into namespaces (deploy, migration, etc.) • Easy to extend and manipulate • Docs are a bit hazy at the moment • Geared towards SCM users http://capistranorb.com
"set your repository location here" set :scm, :subversion # Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none` role :web, “your web-‐server here” # Your HTTP server, Apache/etc role :app, “your app-‐server here” # This may be the same as your `Web` server role :db, “your primary db-‐server here”, :primary => true # This is where Rails migrations will run role :db, “your slave db-‐server here” ➘ ➘ ➘ ➘ <3 Perforce CakePHP is cool too Cause we’re all on failover servers, right? milliondollarapp.com deploy.rb
$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 :staging 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
desc "Clears assets" task :clear do run "cd #{app_dir} && ../cake/console/cake -‐app #{app_dir} asset_compress clear" end desc "Builds all assets" task :build do run "cd #{app_dir} && ../cake/console/cake -‐app #{app_dir} asset_compress build" end desc "Builds ini assets" task :build_ini do run "cd #{app_dir} && ../cake/console/cake -‐app #{app_dir} asset_compress build_ini" end desc "Rebuilds assets" task :rebuild do run "cd #{app_dir} && ../cake/console/cake -‐app #{app_dir} asset_compress clear" run "cd #{app_dir} && ../cake/console/cake -‐app #{app_dir} asset_compress build" end end
use -app flag • Takes some initial setup • Behaves funky with multiple concurrent deploys • Potentially slow with large deploys • Will murder your datacenter if deploying to hundreds of servers