Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Application deployment with Chef

Igor Afonov
November 22, 2012

Application deployment with Chef

Agile tour Riga 2012

Igor Afonov

November 22, 2012
Tweet

More Decks by Igor Afonov

Other Decks in Programming

Transcript

  1. "Keep track of all the stuff you do to take

    a system from 'bare metal' to 'doing its job'." - Adam Jacob
  2. ๏ Remember everything ๏ Keep notes in notepad ๏ Maintain

    wiki ๏ Bash scripts ๏ Automation frameworks
  3. Cookbook File Cron Deploy Directory Env Execute* File Git Group

    HTTP Request Ifconfig Link Log Mount Ohai Package PowerShell Script User Remote Directory Remote File Route Ruby Block SCM Script Service Subversion Template Bundled resources
  4. service "redis" do action :start end Method call Parameter1 Parameter2

    # lib/chef/platform.rb - Chef::Platform :macosx => { :default => { ... :package => Chef::Provider::Package::Macports, :service => Chef::Provider::Service::Macosx, ... } } # lib/chef/provider/service/macosx.rb class Chef::Provider::Service::Macosx < Chef::Provider::Service::Simple def start if @current_resource.running Chef::Log.debug("#{@new_resource} already running, not starting") else shell_out!("launchctl load -w '#{@plist}'") end end end
  5. package "postfix" do action :install end %w{main master}.each do |config|

    template "/etc/postfix/#{config}.cf" do source "#{config}.cf.erb" owner "root" group "root" mode 0644 end end
  6. ๏ Cookbooks should be data-driven ๏ Do not hardcode anything

    ๏ Control behavior via: ๏ Attributes ๏ Databags ๏ Search
  7. name "mail-server" description "Installs and configures postfix MTA" default_attributes :postfix

    => { "mydomain" => "domain.com", "myorigin" => "domain.com" } run_list "recipe[postfix]"
  8. chef-server push pull VCS chef-client node chef-client node chef-client node

    server chef repository developer The big picture
  9. ๏ Resource is a unit of work ๏ Provider take

    real action ๏ Recipe is collection of resources ๏ Cookbook is reusable set of recipes ๏ Code is pushed to chef-server ๏ Node pulls code from chef-server and runs it Recap
  10. ๏ Get the code from VCS ๏ Put the code

    to the right place ๏ Do some rituals Three easy* steps * Not true
  11. Get the code & put it to the right place

    application 'copycopter' do repository '[email protected]:iafonov/copycopter-server.git' revision 'master' path '/var/www/apps/copycopter' end
  12. Pick the SCM strategy application 'copycopter' do repository '[email protected]:iafonov/copycopter-server.git' revision

    'master' path '/var/www/apps/copycopter' strategy :deploy_revision # :deploy_timestamped end
  13. Set the code owner application 'copycopter' do repository '[email protected]:iafonov/copycopter-server.git' revision

    'master' path '/var/www/apps/copycopter' strategy :deploy_revision owner 'deploy' group 'deploy' end
  14. Install library dependencies application 'copycopter' do repository '[email protected]:iafonov/copycopter-server.git' revision 'master'

    path '/var/www/apps/copycopter' strategy :deploy_revision owner 'deploy' group 'deploy' packages ['libxml2-dev', 'libxslt-dev'] end
  15. Run migrations application 'copycopter' do repository '[email protected]:iafonov/copycopter-server.git' revision 'master' path

    '/var/www/apps/copycopter' strategy :deploy_revision owner 'deploy' group 'deploy' packages ['libxml2-dev', 'libxslt-dev'] migrate true end
  16. Restart service application 'copycopter' do repository '[email protected]:iafonov/copycopter-server.git' revision 'master' path

    '/var/www/apps/copycopter' strategy :deploy_revision owner 'deploy' group 'deploy' packages ['libxml2-dev', 'libxslt-dev'] migrate true restart_command 'service copycopter restart' end
  17. Rails application 'copycopter' do ... rails do gems ['bundler'] database

    do username node['mysql']['username'] password node['mysql']['password'] database 'copycopter_production' end end end
  18. nginx application 'copycopter' do ... rails do ... end unicorn

    do ... end nginx_load_balancer do static_files '/public' => 'public' application_server_role 'copycopter_app_server' end end
  19. Notify monitoring system application 'copycopter' do ... before_deploy do execute

    'run_backups' do command 'rsync ....' end end after_restart do StatsD.gauge("#{node.name}.deploy") end end
  20. ๏ Chef allows declaratively describe deploy process ๏ A lot

    of technology stacks are supported via sub-resources ๏ You can precisely control each step of process Recap
  21. FAQ