Slide 1

Slide 1 text

Chef Infrastructure as code

Slide 2

Slide 2 text

Background • SaaS application • Some clients had outgrown standard needs • They want private instances • They want run older but stable releases

Slide 3

Slide 3 text

Problems • Configuration management • Monitoring • Updates • Deployment

Slide 4

Slide 4 text

Chef • Systems integration framework • Client-server architecture • Idempotence • Imperative approach • Ruby everywhere • Configuration as code

Slide 5

Slide 5 text

Basic terminology Node chef-server Run List (Roles) Attributes chef-client Node Run List (Roles) Attributes chef-client

Slide 6

Slide 6 text

Basic terminology • Client • Node • Role • Resource • Recipe • Cookbook • Attribute

Slide 7

Slide 7 text

Basic components • chef-client • ohai • chef-server • chef-webui • knife

Slide 8

Slide 8 text

Node lifecycle • Bootstrap • Configure • Control

Slide 9

Slide 9 text

Bootstrap $ knife bootstrap 178.79.166.70 -x root -P password -r 'role[base]' -d ubuntu10.04 -N new-node

Slide 10

Slide 10 text

chef-client run • Authenticate node • Synchronize cookbooks • Compile • Converge

Slide 11

Slide 11 text

Chef repository chef-repo |-certificates |-config |-cookbooks |-data_bags |-roles |-script |---Rakefile https://github.com/opscode/chef-repo

Slide 12

Slide 12 text

Cookbook cookbook |-attributes |-definitions |-files |-libraries |-recipes |-templates |---metadata.rb

Slide 13

Slide 13 text

Recipe package "sendmail" do action :install end

Slide 14

Slide 14 text

Recipe %w{releases shared/config shared/log}.each do |dir| directory "/var/www/apps/application/#{dir}" do owner "deploy" group "deploy" mode 0755 recursive true end end

Slide 15

Slide 15 text

script "do_something_scary" do interpreter "bash" user "root" cwd "/tmp" code <<-EOS wget http://www.example.com/tarball.tar.gz tar -zxf tarball.tar.gz EOS not_if { File.exists? "/tmp/lock" } end Recipe

Slide 16

Slide 16 text

Templates # passenger.conf.erb # Auto-generated. Local modifications will be overwritten. passenger_root <%= node[:rvm_passenger][:root_path] %>; passenger_ruby <%= node[:rvm_passenger][:ruby_wrapper] %>; # passenger_nginx.rb template "/etc/conf.d/passenger.conf" do source "passenger_nginx.conf.erb" owner "root" group "root" mode "0644" notifies :restart, resources(:service => "nginx") end

Slide 17

Slide 17 text

Resources Cookbook File Cron Deploy Directory Env Erlang Call Execute File Git Group HTTP Request Ifconfig Link Log Mdadm Mount Ohai Package PowerShell Script User Remote Directory Remote File Route Ruby Block SCM Script Service Subversion Template

Slide 18

Slide 18 text

Providers package apt rpm macports Resource Providers Interface Implementation

Slide 19

Slide 19 text

Our setup chef server staging CI getsocio.com production shard2.g.com base slave shard3.g.com base slave VPN auxillary-server.com

Slide 20

Slide 20 text

Deployment deploy_revision "/var/www/apps/application" do repo "[email protected]:iafonov/ha.git" environment "RAILS_ENV" => "production" branch "release7" action :deploy restart_command "touch tmp/restart.txt" end

Slide 21

Slide 21 text

Deployment $ rake deploy $ rake deploy:production $ rake rollback $ rake rollback:production

Slide 22

Slide 22 text

Deployment Hint #1 # /etc/ssh_config ForwardAgent yes (UNIX is your friend)

Slide 23

Slide 23 text

Deployment Hint #2 file "/etc/sudoers.d/deploy_chef" do owner "root" group "root" mode 0440 content <<-EOS Defaults env_keep = "SSH_AUTH_SOCK" deploy ALL= NOPASSWD: /usr/bin/chef-client EOS end (UNIX is your friend)

Slide 24

Slide 24 text

API class Deployer def initialize(query_str) @nodes = Chef::Search::Query.new.search(:node, query_str) @ssh = SshWrapper.new.configure_session(@nodes) end def deploy set_action_and_update_nodes('deploy') end def rollback set_action_and_update_nodes('rollback') end private def set_action_and_update_nodes(action) @nodes.each {|node| node.set['groupinator']["deploy_action"] = action} @ssh.ssh_command("sudo chef-client") end end Deployer.new("name:staging").deploy

Slide 25

Slide 25 text

Testing • Vagrant - http://vagrantup.com/ • Linode/Amazon

Slide 26

Slide 26 text

Dark sides • Complexity • Lack of dry-run mode • Complexity • Lack of documentation

Slide 27

Slide 27 text

Links • http://wiki.opscode.com/display/chef/Home • https://github.com/opscode/chef • http://tickets.opscode.com/browse/CHEF

Slide 28

Slide 28 text

http://iafonov.github.com/ @iafonov