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

Becoming Fearlessly Definite & Resourceful

Becoming Fearlessly Definite & Resourceful

Doing everything in chef cookbooks and definitions is easy and powerful. But I'll show you when it's time to set aside them and bring out the heavyweight guns: full fledged resources. I'll show you how we've taken our (surprisingly complicated) application deployment and database management definitions and converted them to heavyweight resources. Some things that are better: input validation, error handling, debugging information, testing, readability (really!) and ease of future improvements. Most importantly, I'll show that sometimes heavyweight resources are easier than LWRPs.

More Decks by Joseph Anthony Pasquale Holsten

Other Decks in Technology

Transcript

  1. include_recipe 'mysql'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'!

    ! group 'adm'! ! user 'awesome' do! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "/srv/awesome/shared/#{dir}" do! owner 'awesome'! group 'adm'! mode '0750'! end! end! ! template '/srv/awesome/shared/config/database.yml' do! source 'database.yml.erb'! owner 'awesome'! group 'adm'! mode '0640'! variables(! database: {! host: 'awesome-db.awsm.net',! adapter: 'mysql2',! database: 'awesome',! username: 'awesome',! password: 's3kr3t'! },! rails_env: node.chef_environment! )! end! ! deploy '/srv/awesome' do! repo '[email protected]/awsm/awesome'! user 'awesome'! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user 'awesome'! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user 'awesome'! environment new_resource.environment! end! end! end! ! web_app 'awesome' do! docroot '/srv/awesome/current/public'! template 'awesome.conf.erb'! server_name "awesome.#{node['domain']}"! server_aliases [ 'awesome', node['hostname'] ]! rails_env node.chef_environment! end
  2. include_recipe 'mysql'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'!

    ! group 'adm'! ! user 'awesome' do! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "/srv/awesome/shared/#{dir}" do! owner 'awesome'! group 'adm'! mode '0750'! end! end! ! template '/srv/awesome/shared/config/database.yml' do! source 'database.yml.erb'! owner 'awesome'! group 'adm'! mode '0640'! variables(! database: {! host: 'awesome-db.awsm.net',! adapter: 'mysql2',! database: 'awesome',! username: 'awesome',! password: 's3kr3t'! },! rails_env: node.chef_environment! )! end! ! deploy '/srv/awesome' do! repo '[email protected]/awsm/awesome'! user 'awesome'! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user 'awesome'! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user 'awesome'! environment new_resource.environment! end! end! end! ! web_app 'awesome' do! docroot '/srv/awesome/current/public'! template 'awesome.conf.erb'! server_name "awesome.#{node['domain']}"! server_aliases [ 'awesome', node['hostname'] ]! rails_env node.chef_environment! end include_recipe 'mysql'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user 'spiffy' do! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "/srv/spiffy/shared/#{dir}" do! owner 'spiffy'! group 'adm'! mode '0750'! end! end! ! template '/srv/spiffy/shared/config/database.yml' do! source 'database.yml.erb'! owner 'spiffy'! group 'adm'! mode '0640'! variables(! database: {! host: 'spiffy-db.awsm.net',! adapter: 'mysql2',! database: 'spiffy',! username: 'spiffy',! password: 's3kr3t'! },! rails_env: node.chef_environment! )! end! ! deploy '/srv/spiffy' do! repo '[email protected]/awsm/spiffy'! user 'spiffy'! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user 'spiffy'! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user 'spiffy'! environment new_resource.environment! end! end! end! ! web_app 'spiffy' do! docroot '/srv/spiffy/current/public'! template 'spiffy.conf.erb'! server_name "spiffy.#{node['domain']}"! server_aliases [ 'spiffy', node['hostname'] ]! rails_env node.chef_environment! end
  3. +-- 2 lines: include_recipe 'mysql'----------------------------------------------------------------------------------------------------- ------------------! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'!

    include_recipe 'ruby'! ! group 'adm'! ! user 'awesome' do ! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "/srv/awesome/shared/#{dir}" do ! owner 'awesome' ! group 'adm'! mode '0750'! end! end! ! template '/srv/awesome/shared/config/database.yml' do ! source 'database.yml.erb'! owner 'awesome' ! group 'adm'! mode '0640'! variables(! database: {! host: 'awesome-db.awsm.net', ! adapter: 'mysql2',! database: 'awesome', ! username: 'awesome', ! password: 's3kr3t'! },! rails_env: node.chef_environment! )! end! ! deploy '/srv/awesome' do ! repo '[email protected]/awsm/awesome' ! user 'awesome' ! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user 'awesome' ! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user 'awesome' ! environment new_resource.environment! end! end! end! ! web_app 'awesome' do ! docroot '/srv/awesome/current/public' ! template 'awesome.conf.erb' ! server_name "awesome.#{node['domain']}" ! server_aliases [ 'awesome', node['hostname'] ] ! rails_env node.chef_environment! end +-- 2 lines: include_recipe 'mysql'----------------------------------------------------------------------------------------------------- ------------------! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user 'spiffy' do group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "/srv/spiffy/shared/#{dir}" do owner 'spiffy' group 'adm'! mode '0750'! end! end! ! template '/srv/spiffy/shared/config/database.yml' do source 'database.yml.erb'! owner 'spiffy' group 'adm'! mode '0640'! variables(! database: {! host: 'spiffy-db.awsm.net', adapter: 'mysql2',! database: 'spiffy', username: 'spiffy', password: 's3kr3t'! },! rails_env: node.chef_environment! )! end! ! deploy '/srv/spiffy' do repo '[email protected]/awsm/spiffy' user 'spiffy' group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user 'spiffy' environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user 'spiffy' environment new_resource.environment! end! end! end! ! web_app 'spiffy' do docroot '/srv/spiffy/current/public' template 'spiffy.conf.erb' server_name "spiffy.#{node['domain']}" server_aliases [ 'spiffy', node['hostname'] ] rails_env node.chef_environment! end
  4. include_recipe 'mysql'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'!

    ! group 'adm'! ! user 'awesome' do! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "/srv/awesome/shared/#{dir}" do! owner 'awesome'! group 'adm'! mode '0750'! end! end! ! template '/srv/awesome/shared/config/database.yml' do! source 'database.yml.erb'! owner 'awesome'! group 'adm'! mode '0640'! variables(! database: {! host: 'awesome-db.awsm.net',! adapter: 'mysql2',! database: 'awesome',! username: 'awesome',! password: 's3kr3t'! },! rails_env: node.chef_environment! )! end! ! deploy '/srv/awesome' do! repo '[email protected]/awsm/awesome'! user 'awesome'! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user 'awesome'! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user 'awesome'! environment new_resource.environment! end! end! end! ! web_app 'awesome' do! docroot '/srv/awesome/current/public'! template 'awesome.conf.erb'! server_name "awesome.#{node['domain']}"! server_aliases [ 'awesome', node['hostname'] ]! rails_env node.chef_environment! end include_recipe 'mysql'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user 'spiffy' do! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "/srv/spiffy/shared/#{dir}" do! owner 'spiffy'! group 'adm'! mode '0750'! end! end! ! template '/srv/spiffy/shared/config/database.yml' do! source 'database.yml.erb'! owner 'spiffy'! group 'adm'! mode '0640'! variables(! database: {! host: 'spiffy-db.awsm.net',! adapter: 'mysql2',! database: 'spiffy',! username: 'spiffy',! password: 's3kr3t'! },! rails_env: node.chef_environment! )! end! ! deploy '/srv/spiffy' do! repo '[email protected]/awsm/spiffy'! user 'spiffy'! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user 'spiffy'! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user 'spiffy'! environment new_resource.environment! end! end! end! ! web_app 'spiffy' do! docroot '/srv/spiffy/current/public'! template 'spiffy.conf.erb'! server_name "spiffy.#{node['domain']}"! server_aliases [ 'spiffy', node['hostname'] ]! rails_env node.chef_environment! end
  5. params = {} ! params[:user] = 'spiffy' ! params[:deploy_to] =

    '/srv/spiffy' ! params[:database] = { ! host: 'spiffy-db.awsm.net', ! adapter: 'mysql2', ! database: 'spiffy', ! username: 'spiffy', ! password: 's3kr3t' ! } ! params[:repo] = '[email protected]/awsm/spiffy' ! params[:app_name] = 'spiffy' ! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user params[:user] do ! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "#{params[:deploy_to]}/shared/#{dir}" do ! owner params[:user] ! group 'adm'! mode '0750'! end! end! ! template "#{params[:deploy_to]}/shared/config/database.yml" do ! source 'database.yml.erb'! owner params[:user] ! group 'adm'! mode '0640'! variables(! database: params[:database], ! rails_env: node.chef_environment! )! end! ! deploy params[:deploy_to] do ! repo params[:repo] ! user params[:user] ! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user params[:user] ! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user params[:user] ! environment new_resource.environment! end! end! end! ! web_app params[:app_name] do ! docroot "#{params[:deploy_to]}/current/public" ! template 'vhost.conf.erb' ! server_name "#{params[:app_name]}.#{node['domain']}" ! server_aliases [ params[:app_name], node['hostname'] ] ! rails_env node.chef_environment! end
  6. params = {} ! params[:user] = 'spiffy' ! params[:deploy_to] =

    '/srv/spiffy' ! params[:database] = { ! host: 'spiffy-db.awsm.net', ! adapter: 'mysql2', ! database: 'spiffy', ! username: 'spiffy', ! password: 's3kr3t' ! } ! params[:repo] = '[email protected]/simply_spiffy/spiffy' ! params[:app_name] = 'spiffy' ! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user params[:user] do ! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "#{params[:deploy_to]}/shared/#{dir}" do ! owner params[:user] ! group 'adm'! mode '0750'! end! end! ! template "#{params[:deploy_to]}/shared/config/database.yml" do ! source 'database.yml.erb'! owner params[:user] ! group 'adm'! mode '0640'! variables(! database: params[:database], ! rails_env: node.chef_environment! )! end! ! deploy params[:deploy_to] do ! repo params[:repo] ! user params[:user] ! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user params[:user] ! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user params[:user] ! environment new_resource.environment! end! end! end! ! web_app params[:app_name] do ! docroot "#{params[:deploy_to]}/current/public" ! template 'vhost.conf.erb' ! server_name "#{params[:app_name]}.#{node['domain']}" ! server_aliases [ params[:app_name], node['hostname'] ] ! rails_env node.chef_environment! end params = {}! params[:user] = 'spiffy'! params[:deploy_to] = '/srv/spiffy'! params[:database] = {! host: 'spiffy-db.awsm.net',! adapter: 'mysql2',! database: 'spiffy',! username: 'spiffy',! password: 's3kr3t'! }! params[:repo] = '[email protected]/awsm/spiffy'! params[:app_name] = 'spiffy'
  7. params = {} ! params[:user] = 'spiffy' ! params[:deploy_to] =

    '/srv/spiffy' ! params[:database] = { ! host: 'spiffy-db.awsm.net', ! adapter: 'mysql2', ! database: 'spiffy', ! username: 'spiffy', ! password: 's3kr3t' ! } ! params[:repo] = '[email protected]/simply_spiffy/spiffy' ! params[:app_name] = 'spiffy' ! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user params[:user] do ! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "#{params[:deploy_to]}/shared/#{dir}" do ! owner params[:user] ! group 'adm'! mode '0750'! end! end! ! template "#{params[:deploy_to]}/shared/config/database.yml" do ! source 'database.yml.erb'! owner params[:user] ! group 'adm'! mode '0640'! variables(! database: params[:database], ! rails_env: node.chef_environment! )! end! ! deploy params[:deploy_to] do ! repo params[:repo] ! user params[:user] ! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user params[:user] ! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user params[:user] ! environment new_resource.environment! end! end! end! ! web_app params[:app_name] do ! docroot "#{params[:deploy_to]}/current/public" ! template 'vhost.conf.erb' ! server_name "#{params[:app_name]}.#{node['domain']}" ! server_aliases [ params[:app_name], node['hostname'] ] ! rails_env node.chef_environment! end define :ruby_app_deploy,! user: nil,! deploy_to: nil,! database: nil,! repo: nil do! ! include_recipe 'sqlite'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! # ...! ! web_app params[:name] do! docroot "#{params[:deploy_to]}/current/public"! template 'vhost.conf.erb'! server_name "#{params[:name]}.#{node['domain']}"! server_aliases [ params[:name], node['hostname'] ]! rails_env node.chef_environment! end! end
  8. params = {}! params[:user] = 'spiffy'! params[:deploy_to] = '/srv/spiffy'! params[:database]

    = {! host: 'spiffy-db.awsm.net',! adapter: 'mysql2',! database: 'spiffy',! username: 'spiffy',! password: 's3kr3t'! }! params[:repo] = '[email protected]/awsm/spiffy'! params[:app_name] = 'spiffy'! ! ruby_app_deploy params[:app_name] do! repo params[:repo]! database params[:database]! user params[:user]! deploy_to params[:deploy_to]! end params = {}! params[:user] = 'spiffy'! params[:deploy_to] = '/srv/spiffy'! params[:database] = {! host: 'spiffy-db.awsm.net',! adapter: 'mysql2',! database: 'spiffy',! username: 'spiffy',! password: 's3kr3t'! }! params[:repo] = '[email protected]/simply_spiffy/spiffy'! params[:app_name] = 'spiffy'! ! include_recipe 'sqlite'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user params[:user] do! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "#{params[:deploy_to]}/shared/#{dir}" do! owner params[:user]! group 'adm'! mode '0750'! end! end! ! template "#{params[:deploy_to]}/shared/config/database.yml" do! source 'database.yml.erb'! owner params[:user]! group 'adm'! mode '0640'! variables(! database: params[:database],! rails_env: node.chef_environment! )! end! ! deploy params[:deploy_to] do! repo params[:repo]! user params[:user]! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user params[:user]! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user params[:user]! environment new_resource.environment! end! end! end! ! web_app params[:app_name] do! docroot "#{params[:deploy_to]}/current/public"! template 'vhost.conf.erb'! server_name "#{params[:app_name]}.#{node['domain']}"! server_aliases [ params[:app_name], node['hostname'] ]! rails_env node.chef_environment! end! !
  9. ruby_app_deploy 'spiffy' do! repo '[email protected]/awsm/spiffy'! database host: 'spiffy-db.awsm.net',! adapter: 'mysql2',!

    database: 'spiffy',! username: 'spiffy',! password: 's3kr3t'! user 'spiffy'! deploy_to '/srv/spiffy'! end
  10. ruby_app_deploy 'spiffy' do! repo '[email protected]/awsm/spiffy'! database host: 'spiffy-db.awsm.net',! adapter: 'mysql2',!

    database: 'spiffy',! username: 'spiffy',! password: 's3kr3t'! user 'spiffy'! deploy_to '/srv/spiffy'! end include_recipe 'mysql'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user 'spiffy' do! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "/srv/spiffy/shared/#{dir}" do! owner 'spiffy'! group 'adm'! mode '0750'! end! end! ! template '/srv/spiffy/shared/config/database.yml' do! source 'database.yml.erb'! owner 'spiffy'! group 'adm'! mode '0640'! variables(! database: {! host: 'spiffy-db.awsm.net',! adapter: 'mysql2',! database: 'spiffy',! username: 'spiffy',! password: 's3kr3t'! },! rails_env: node.chef_environment! )! end! ! deploy '/srv/spiffy' do! repo '[email protected]/awsm/spiffy'! user 'spiffy'! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user 'spiffy'! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user 'spiffy'! environment new_resource.environment! end! end! end! ! web_app 'spiffy' do! docroot '/srv/spiffy/current/public'! template 'spiffy.conf.erb'! server_name "spiffy.#{node['domain']}"! server_aliases [ 'spiffy', node['hostname'] ]! rails_env node.chef_environment! end
  11. ruby_app_deploy 'spiffy' do! repo '[email protected]/awsm/spiffy'! database host: 'spiffy-db.awsm.net',! adapter: 'mysql2',!

    database: 'spiffy',! username: 'spiffy',! password: 's3kr3t'! user 'spiffy'! deploy_to '/srv/spiffy'! end
  12. template '/srv/fancy/etc/features.json' do! group 'wheel'! owner 'fancy'! mode '0664'! variables

    all_my_features! # notifies :reload, 'ruby_app_deploy[fancy]'! end! ! ruby_app_deploy 'fancy' do! repo '[email protected]/awsm/fancy'! database host: 'fancy-db.awsm.net',! adapter: 'mysql2',! database: 'fancy',! username: 'fancy',! password: 's3kr3t'! user 'fancy'! deploy_to '/srv/fancy'! end
  13. re•source n. an asset that can be drawn on in

    order to function effectively
  14. # provider! ! action :create do! file "/tmp/#{new_resource.name}" do! owner

    new_resource.name! group new_resource.name! mode '0664'! action :create! end! end # resource! ! actions :create! ! attribute :name, :kind_of => String
  15. define :ruby_app_deploy,! user: nil,! deploy_to: nil,! database: nil,! repo: nil

    do! ! require 'ostruct' ! ! new_resource = OpenStruct.new ! new_resource.name = params[:name] ! new_resource.user = params[:user] ! new_resource.deploy_to = params[:deploy_to] ! new_resource.repo = params[:repo] ! new_resource.database = params[:database] ! ! include_recipe 'sqlite'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user new_resource.user do ! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "#{new_resource.deploy_to}/shared/#{dir}" do ! owner new_resource.user ! group 'adm'! mode '0750'! end! end! ! template "#{new_resource.deploy_to}/shared/config/database.yml" do ! source 'database.yml.erb'! owner new_resource.user ! group 'adm'! mode '0640'! variables(! database: new_resource.database, ! rails_env: node.chef_environment! )! end! ! deploy new_resource.deploy_to do ! repo new_resource.repo ! user new_resource.user ! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user new_resource.user ! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user new_resource.user ! environment new_resource.environment! end! end! end! ! web_app new_resource.name do ! docroot "#{new_resource.deploy_to}/current/public" ! template 'vhost.conf.erb'! server_name "#{new_resource.name}.#{node['domain']}" ! server_aliases [ new_resource.name, node['hostname'] ] ! rails_env node.chef_environment! end! end! require 'ostruct'! ! new_resource = OpenStruct.new! new_resource.name = params[:name]! new_resource.user = params[:user]! new_resource.deploy_to = params[:deploy_to]! new_resource.repo = params[:repo]! new_resource.database = params[:database]
  16. # ruby_app_deploy resource! ! action :create! ! attribute :name, :kind_of

    => String! attribute :user, :kind_of => String! attribute :deploy_to, :kind_of => String! attribute :repo, :kind_of => String! attribute :database, :kind_of => Hash # ruby_app_deploy definition! ! require 'ostruct'! new_resource = OpenStruct.new! ! new_resource.name = params[:name]! new_resource.user = params[:user]! new_resource.deploy_to = params[:deploy_to]! new_resource.repo = params[:repo]! new_resource.database = params[:database]
  17. define :ruby_app_deploy,! user: nil,! deploy_to: nil,! database: nil,! repo: nil

    do! ! require 'ostruct' ! ! new_resource = OpenStruct.new ! new_resource.name = params[:name] ! new_resource.user = params[:user] ! new_resource.deploy_to = params[:deploy_to] ! new_resource.repo = params[:repo] ! new_resource.database = params[:database] ! ! include_recipe 'sqlite'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user new_resource.user do ! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "#{new_resource.deploy_to}/shared/#{dir}" do ! owner new_resource.user ! group 'adm'! mode '0750'! end! end! ! template "#{new_resource.deploy_to}/shared/config/database.yml" do ! source 'database.yml.erb'! owner new_resource.user ! group 'adm'! mode '0640'! variables(! database: new_resource.database, ! rails_env: node.chef_environment! )! end! ! deploy new_resource.deploy_to do ! repo new_resource.repo ! user new_resource.user ! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user new_resource.user ! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user new_resource.user ! environment new_resource.environment! end! end! end! ! web_app new_resource.name do ! docroot "#{new_resource.deploy_to}/current/public" ! template 'vhost.conf.erb'! server_name "#{new_resource.name}.#{node['domain']}" ! server_aliases [ new_resource.name, node['hostname'] ] ! rails_env node.chef_environment! end! end! # ruby_app_deploy provider! ! action :create do! include_recipe 'sqlite'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user new_resource.user do! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "#{new_resource.deploy_to}/shared/#{dir}" do! owner new_resource.user! group 'adm'! mode '0750'! end! end! ! template "#{new_resource.deploy_to}/shared/config/database.yml" do! source 'database.yml.erb'! owner new_resource.user! group 'adm'! mode '0640'! variables(! database: new_resource.database,! rails_env: node.chef_environment! )! end! ! deploy new_resource.deploy_to do! repo new_resource.repo! user new_resource.user! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user new_resource.user! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user new_resource.user! environment new_resource.environment! end! end! end! ! web_app new_resource.name do! docroot "#{new_resource.deploy_to}/current/public"! template 'vhost.conf.erb'! server_name "#{new_resource.name}.#{node['domain']}"! server_aliases [ new_resource.name, node['hostname'] ]! rails_env node.chef_environment! end! end
  18. # ruby_app_deploy resource! ! action :create! ! attribute :name, :kind_of

    => String! attribute :user, :kind_of => String! attribute :deploy_to, :kind_of => String! attribute :repo, :kind_of => String! attribute :database, :kind_of => Hash! # ruby_app_deploy provider! ! action :create do! include_recipe 'sqlite'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user new_resource.user do! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "#{new_resource.deploy_to}/shared/#{dir}" do! owner new_resource.user! group 'adm'! mode '0750'! end! end! ! template "#{new_resource.deploy_to}/shared/config/database.yml" do! source 'database.yml.erb'! owner new_resource.user! group 'adm'! mode '0640'! variables(! database: new_resource.database,! rails_env: node.chef_environment! )! end! ! deploy new_resource.deploy_to do! repo new_resource.repo! user new_resource.user! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user new_resource.user! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user new_resource.user! environment new_resource.environment! end! end! end! ! web_app new_resource.name do! docroot "#{new_resource.deploy_to}/current/public"! template 'vhost.conf.erb'! server_name "#{new_resource.name}.#{node['domain']}"! server_aliases [ new_resource.name, node['hostname'] ]! rails_env node.chef_environment! end! end
  19. class Chef! class Resource::Minimal < Resource! def initialize(name, run_context =

    nil)! super! @app_name = name! @action.push(:create)! @allowed_actions.push(:create)! end! ! def app_name(arg=nil)! set_or_return(! :app_name,! arg,! :kind_of => [ String ]! )! end! end! ! class Provider::Minimal < Provider! def load_current_resource! end! ! def action_create! file "/tmp/#{new_resource.app_name}" do! owner new_resource.app_name! group new_resource.app_name! mode '0664'! action :create! end! end! end! end
  20. class Chef! class Resource::RubyAppDeploy < Resource! def initialize(name, run_context =

    nil)! super! @app_name = name! @action.push(:create)! @allowed_actions.push(:create,:reload)! end! ! def user(arg=nil)! set_or_return(! :user,! arg,! :kind_of => [ String ]! )! end! def deploy_to(arg=nil)! set_or_return(! :deploy_to,! arg,! :kind_of => [ String ]! )! end! def repo(arg=nil)! set_or_return(! :repo,! arg,! :kind_of => [ String ]! )! end! def database(arg=nil)! set_or_return(! :database,! arg,! :kind_of => [ String ]! )! end! end! end
  21. class Chef! class Provider::RubyAppDeploy < Provider! def load_current_resource! @current_resource =

    Chef::Resource::RubyAppDeploy.new(@new_resource.name)! @current_resource! end! ! def action_create! include_recipe 'sqlite'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user new_resource.user do! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "#{new_resource.deploy_to}/shared/#{dir}" do! owner new_resource.user! group 'adm'! mode '0750'! end! end! ! template "#{new_resource.deploy_to}/shared/config/database.yml" do! source 'database.yml.erb'! owner new_resource.user! group 'adm'! mode '0640'! variables(! database: new_resource.database,! rails_env: node.chef_environment! )! end! ! deploy new_resource.deploy_to do! repo new_resource.repo! user new_resource.user! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user new_resource.user! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user new_resource.user! environment new_resource.environment! end! end! end! ! web_app new_resource.name do! docroot "#{new_resource.deploy_to}/current/public"! template 'vhost.conf.erb'! server_name "#{new_resource.name}.#{node['domain']}"! server_aliases [ new_resource.name, node['hostname'] ]! rails_env node.chef_environment! end! end! end! end
  22. class Chef! class Provider::RubyAppDeploy < Provider! def load_current_resource! @current_resource =

    Chef::Resource::RubyAppDeploy.new(@new_resource.name)! @current_resource! end! ! def action_create! include_recipe 'sqlite'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user new_resource.user do! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "#{new_resource.deploy_to}/shared/#{dir}" do! owner new_resource.user! group 'adm'! mode '0750'! end! end! ! template "#{new_resource.deploy_to}/shared/config/database.yml" do! source 'database.yml.erb'! owner new_resource.user! group 'adm'! mode '0640'! variables(! database: new_resource.database,! rails_env: node.chef_environment! )! end! ! deploy new_resource.deploy_to do! repo new_resource.repo! user new_resource.user! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user new_resource.user! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user new_resource.user! environment new_resource.environment! end! end! end! ! web_app new_resource.name do! docroot "#{new_resource.deploy_to}/current/public"! template 'vhost.conf.erb'! server_name "#{new_resource.name}.#{node['domain']}"! server_aliases [ new_resource.name, node['hostname'] ]! rails_env node.chef_environment! end! end! end! end class Chef! class Resource::RubyAppDeploy < Resource! def initialize(name, run_context = nil)! super! @app_name = name! @action.push(:create)! @allowed_actions.push(:create,:reload)! end! ! def user(arg=nil)! set_or_return(! :user,! arg,! :kind_of => [ String ]! )! end! def deploy_to(arg=nil)! set_or_return(! :deploy_to,! arg,! :kind_of => [ String ]! )! end! def repo(arg=nil)! set_or_return(! :repo,! arg,! :kind_of => [ String ]! )! end! def database(arg=nil)! set_or_return(! :database,! arg,! :kind_of => [ String ]! )! end! end! end
  23. class Chef! class Provider::RubyAppDeploy < Provider! def load_current_resource! @current_resource =

    Chef::Resource::RubyAppDeploy.new(@new_resource.name)! @current_resource! end! ! def action_create! include_recipe 'sqlite'! include_recipe 'apache2'! include_recipe 'apache2::mod_rewrite'! include_recipe 'passenger_apache2::mod_rails'! include_recipe 'ruby'! ! group 'adm'! ! user new_resource.user do! group 'adm'! end! ! gem_package 'bundler'! ! %w{log vendor_bundle config}.each do |dir|! directory "#{new_resource.deploy_to}/shared/#{dir}" do! owner new_resource.user! group 'adm'! mode '0750'! end! end! ! template "#{new_resource.deploy_to}/shared/config/database.yml" do! source 'database.yml.erb'! owner new_resource.user! group 'adm'! mode '0640'! variables(! database: new_resource.database,! rails_env: node.chef_environment! )! end! ! deploy new_resource.deploy_to do! repo new_resource.repo! user new_resource.user! group 'adm'! migrate true! migration_command 'bundle exec rake db:migrate'! environment 'RAILS_ENV' => node.chef_environment! create_dirs_before_symlink %w{tmp public config vendor}! symlinks 'log' => 'log',! 'vendor_bundle' => 'vendor/bundle'! restart_command 'touch tmp/restart.txt'! ! before_migrate do! execute 'bundle install --deployment' do! cwd release_path! user new_resource.user! environment new_resource.environment! end! end! before_symlink do! execute 'bundle exec rake assets:precompile' do! cwd release_path! user new_resource.user! environment new_resource.environment! end! end! end! ! web_app new_resource.name do! docroot "#{new_resource.deploy_to}/current/public"! template 'vhost.conf.erb'! server_name "#{new_resource.name}.#{node['domain']}"! server_aliases [ new_resource.name, node['hostname'] ]! rails_env node.chef_environment! end! end! end! end class Chef! class Resource::RubyAppDeploy < Resource! def initialize(name, run_context = nil)! super! @app_name = name! @action.push(:create)! @allowed_actions.push(:create,:reload)! end! ! def user(arg=nil)! set_or_return(! :user,! arg,! :kind_of => [ String ]! )! end! def deploy_to(arg=nil)! set_or_return(! :deploy_to,! arg,! :kind_of => [ String ]! )! end! def repo(arg=nil)! set_or_return(! :repo,! arg,! :kind_of => [ String ]! )! end! def database(arg=nil)! set_or_return(! :database,! arg,! :kind_of => [ String ]! )! end! end! end