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

Chef or how to make computers do the work for us

Marcin Kulik
November 09, 2011

Chef or how to make computers do the work for us

My KRUG (Kraków Ruby Users Group) presentation about automating boring tasks with Opscode's Chef.

Marcin Kulik

November 09, 2011
Tweet

More Decks by Marcin Kulik

Other Decks in Programming

Transcript

  1. C H E F or how to make computers do

    the work for us Marcin Kulik, Lunar Logic Polska KRUG 2011/11/08
  2. DSL

  3. "Chef is an open source systems integration framework built to

    bring the benefits of configuration management to your entire infrastructure." "You write source code to describe how you want each part of your infrastructure to be built, then apply those descriptions to your servers." "The result is a fully automated infrastructure: when a new server comes on line, the only thing you have to do is tell Chef what role it should play in your architecture."
  4. Cookbook structure |-- config | |-- node.json | `-- solo.rb

    |-- cookbooks | |-- book1 | | |-- attributes | | |-- files | | |-- metadata.rb | | |-- recipes | | | |-- default.rb | | | `-- source.rb | | `-- templates | |-- book2 | | |-- attributes | | | `-- default.rb | | |-- files | | |-- recipes | | | `-- default.rb | | `-- templates
  5. | | `-- templates | | `-- default | |

    `-- authorized_keys.erb | |-- book3 | | |-- attributes | | |-- files | | | `-- default | | | `-- secret-key | | |-- recipes | | | `-- default.rb | | `-- templates |-- config | |-- node.json | `-- solo.rb |-- cookbooks | |-- book1 | | |-- attributes | | |-- files | | |-- metadata.rb | | |-- recipes | | | |-- default.rb | | | `-- libs.rb | | `-- templates Installation
  6. Lunar Station is a set of Chef cookbooks and a

    bash script (???) for bootstrapping developers machines at Lunar Logic Polska.
  7. detects platform (Ubuntu, Fedora, OSX) installs compilers and other RVM

    dependencies installs RVM & ruby 1.9 & chef gem downloads latest Lunar Station cookbooks runs chef-solo
  8. $ curl -skL http://bit.ly/lunar-station | bash Initializing Lunar Workstation... >>

    Fedora Linux detected. >> Checking for RVM... >> Fetching latest version of Lunar Station cookbooks... >> Starting chef-solo run... [Mon, 07 Nov 2011 22:19:54 +0100] INFO: *** Chef 0.10.4 *** [Mon, 07 Nov 2011 22:19:54 +0100] INFO: Setting the run_list to ...
  9. # cookbooks/repos/recipes/default.rb case node[:platform] when 'fedora' path = "/tmp/rpmfusion-free-release-stable.noarch.rpm" bash

    "download rpmfusion free package" do code "wget http://download1.rpmfusion.org/.../" + "rpmfusion-free-release-stable.noarch.rpm -O #{path}" not_if { File.exist?(path) } end package "rpmfusion-free-release-stable" do source path options "--nogpgcheck" end when 'ubuntu' ... end
  10. end # cookbooks/repos/recipes/default.rb case node[:platform] when 'fedora' ... when 'ubuntu'

    bash "enable multiverse repo" do code "head -n 1 /etc/apt/sources.list | " + "sed 's/main universe/multiverse/' " + ">> /etc/apt/sources.list" not_if "egrep '^deb.+multiverse' /etc/apt/sources.list" end end
  11. # cookbooks/vim/recipes/default.rb case node[:platform] when "ubuntu" package "vim" package "vim-gnome"

    when "fedora" package "vim-enhanced" package "vim-X11" when 'mac_os_x' package "macvim" end
  12. # cookbooks/skype/recipes/default.rb case node[:platform] when 'ubuntu' include_recipe 'init::ubuntu' # for

    partner repo package 'skype' when 'mac_os_x' dmg_package "Skype" do source "http://www.skype.com/go/getskype-macosx.dmg" action :install end when 'fedora' ... end
  13. Each server we configure has its corresponding node configuration file

    in nodes/ directory of kitchen project that specifies run_list and few other settings
  14. # nodes/deneb.json { "run_list": [ "recipe[ssh_access]" ], "ssh_access": [ "marcin.kulik",

    "anna.lesniak", ...], "opened_ports": { "tcp": [80, 443, 22, 8080], "udp": [] }, ...
  15. # See the list of configured servers: $ cap -T

    # Make the changes happen on the server: $ cap configure:deneb
  16. set :user, 'chef' NODE_LIST = Dir["nodes/*.json"].map do |nodefile| File.basename(nodefile, '.json')

    end NODE_LIST.each do |node| role node.to_sym, node end NODE_CONFIG = <<-EOS file_cache_path '/tmp/chef-solo' cookbook_path '/tmp/chef-solo/cookbooks' role_path '/tmp/chef-solo/roles' EOS ...
  17. ... namespace :configure do NODE_LIST.each do |node| desc "Configure #{node}"

    task node.to_sym, :roles => node.to_sym do run "if [ ! -e /tmp/chef-solo ]; then mkdir /tmp/chef-sol upload("cookbooks", "/tmp/chef-solo/", :via => :scp, :rec upload("roles", "/tmp/chef-solo/", :via => :scp, :recursi upload("nodes/#{node}.json", "/tmp/chef-solo/node.json", put(NODE_CONFIG, "/tmp/chef-solo/solo.rb") run "rvmsudo chef-solo " + "-c /tmp/chef-solo/solo.rb " + "-j /tmp/chef-solo/node.json" end end end
  18. ├── Capfile ├── config ├── cookbooks ├── nodes ├── README.md

    ├── roles └── ssh_keys ├── anna.lesniak ├── artur.bilski ├── ... └── marcin.kulik
  19. # cookbooks/access/recipes/default.rb username = 'dev' ssh_keys = node[:ssh_access].map do |f|

    File.read("/tmp/chef-solo/ssh_keys/#{f}") end template "/home/#{username}/.ssh/authorized_keys" do source "authorized_keys.erb" owner username group 'users' mode "0600" variables :ssh_keys => ssh_keys end
  20. Learn step by step EC2 + Chef + Knife +

    Opscode... = Fuuuuuuuuuuuuuuuuuuuuu
  21. Q?