74
C H E F or how to make computers do the work for us Marcin Kulik, Lunar Logic Polska KRUG 2011/11/08

Chef or how to make computers do the work for us

  • Upload
    sickill

  • View
    4.140

  • Download
    3

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Chef or how to make computers do the work for us

C H E For how to make computers do the work for us

Marcin Kulik, Lunar Logic Polska

KRUG 2011/11/08

Page 2: Chef or how to make computers do the work for us

Everyday we're dealing with mechanical, repetitive tasks... wecan automate.

Page 3: Chef or how to make computers do the work for us

What is Chef?

Page 4: Chef or how to make computers do the work for us

Automation tool

written in Ruby

Page 5: Chef or how to make computers do the work for us

DSL

Page 6: Chef or how to make computers do the work for us

Created by Opscode

Page 7: Chef or how to make computers do the work for us

"Chef is an open source systems integration framework builtto bring the benefits of configuration management to your

entire infrastructure."

"You write source code to describe how you want each part ofyour infrastructure to be built, then apply those descriptions

to your servers."

"The result is a fully automated infrastructure: when a newserver comes on line, the only thing you have to do is tell Chef

what role it should play in your architecture."

Page 8: Chef or how to make computers do the work for us

Why do you need it?

Page 9: Chef or how to make computers do the work for us

Economics + Efficiency + Scalability

Page 10: Chef or how to make computers do the work for us

Terms

Page 11: Chef or how to make computers do the work for us

Noderemote server, local machine...

Page 12: Chef or how to make computers do the work for us

Roleweb server, database server, ruby dev workstation...

Page 13: Chef or how to make computers do the work for us

Cookbookmysql, ssh-access, dotfiles...

Page 14: Chef or how to make computers do the work for us

Recipeinstall mysql-server, create database, add user...

Page 15: Chef or how to make computers do the work for us

Resourcefile, dir, user, package, service, gem, virtual host...

Page 16: Chef or how to make computers do the work for us

Run listlist of recipes to run in order

Page 17: Chef or how to make computers do the work for us

{ "run_list": [ "recipe[mysql]", "recipe[git]", "recipe[ruby19]" ]}

Page 18: Chef or how to make computers do the work for us

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

Page 19: Chef or how to make computers do the work for us

| | `-- 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

Page 20: Chef or how to make computers do the work for us

$ gem install chef

Page 21: Chef or how to make computers do the work for us

Modes of operation

Page 22: Chef or how to make computers do the work for us

Cookbooks stored

in central repository(free cookbooks hosting by Opscode:

https://manage.opscode.com/)

Page 23: Chef or how to make computers do the work for us

$ sudo chef-client

Page 24: Chef or how to make computers do the work for us

Cookbooks stored

on the node

Page 25: Chef or how to make computers do the work for us

$ sudo chef-solo -c /path/to/cfg.rb -j /path/to/node-data.json

Page 26: Chef or how to make computers do the work for us

Use cases

Page 27: Chef or how to make computers do the work for us

Configure new machine

(in the cloud with Knife)Amazon EC2, Engine Yard, Linode, BrightBox...

Page 28: Chef or how to make computers do the work for us

Manage config of existing

company serversClient demo apps (directory, vhost, god config), developers' ssh

keys...

Page 29: Chef or how to make computers do the work for us

Bootstrap workstation!rvm + ruby 1.9, git, mysql, vim/emacs...

Page 30: Chef or how to make computers do the work for us

Enough with theory!

Page 31: Chef or how to make computers do the work for us

Lunar Stationhttps://github.com/LunarLogicPolska/lunar-station

Page 32: Chef or how to make computers do the work for us

Lunar Station is a set of Chef cookbooks and a bash script (???)for bootstrapping developers machines at Lunar Logic Polska.

Page 33: Chef or how to make computers do the work for us

You need ruby to run Chef

Page 34: Chef or how to make computers do the work for us

(We assume) you use RVMNo need for system ruby for ruby devs nowadays

Page 35: Chef or how to make computers do the work for us

bootstrap.sh

Page 36: Chef or how to make computers do the work for us

detects platform (Ubuntu, Fedora, OSX)

installs compilers and other RVMdependencies

installs RVM & ruby 1.9 & chef gem

downloads latest Lunar Stationcookbooks

runs chef-solo

Page 37: Chef or how to make computers do the work for us

$ curl -skL http://bit.ly/lunar-station | bashInitializing 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 ...

Page 38: Chef or how to make computers do the work for us

Nodes

Page 39: Chef or how to make computers do the work for us

# linux-rubydev.json

{ "run_list": [ "role[rubydev]" ]}

Page 40: Chef or how to make computers do the work for us

# osx-rubydev.json

{ "run_list": [ "role[osx]", "role[rubydev]" ]}

Page 41: Chef or how to make computers do the work for us

Roles

Page 42: Chef or how to make computers do the work for us

# base.rb

run_list 'recipe[repos]', 'recipe[curl]', 'recipe[wget]', 'recipe[git]', 'recipe[libxml2]', 'recipe[ack]', 'recipe[vim]', 'recipe[ctags]', 'recipe[skype]', 'recipe[firefox]' , 'recipe[google-chrome]'

Page 43: Chef or how to make computers do the work for us

# rubydev.rb

run_list 'role[base]', 'recipe[mysql]'

Page 44: Chef or how to make computers do the work for us

# osx.rb

run_list "recipe[homebrew]"

Page 45: Chef or how to make computers do the work for us

Cookbooks

Page 46: Chef or how to make computers do the work for us

repos cookbook

Page 47: Chef or how to make computers do the work for us

# 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

Page 48: Chef or how to make computers do the work for us

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" endend

Page 49: Chef or how to make computers do the work for us

vim cookbook

Page 50: Chef or how to make computers do the work for us

# 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

Page 51: Chef or how to make computers do the work for us

skype cookbook

Page 52: Chef or how to make computers do the work for us

# 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

Page 53: Chef or how to make computers do the work for us

Lunar Kitchen

Page 54: Chef or how to make computers do the work for us

Source of LLP servers configuration data and a set of Chefcookbooks

Page 55: Chef or how to make computers do the work for us

chef-solo invoked onremote machines

no chef server

Page 56: Chef or how to make computers do the work for us

Each server we configure has its corresponding nodeconfiguration file in nodes/ directory of kitchen project that

specifies run_list and few other settings

Page 57: Chef or how to make computers do the work for us

# nodes/deneb.json

{ "run_list": [ "recipe[ssh_access]" ],

"ssh_access": [ "marcin.kulik", "anna.lesniak", ...],

"opened_ports": { "tcp": [80, 443, 22, 8080], "udp": [] }, ...

Page 58: Chef or how to make computers do the work for us

How do we run chef-soloon remote machine?

Page 59: Chef or how to make computers do the work for us

Capistrano!

Page 60: Chef or how to make computers do the work for us

# See the list of configured servers:

$ cap -T

# Make the changes happen on the server:

$ cap configure:deneb

Page 61: Chef or how to make computers do the work for us

How does Capfile look like?

Page 62: Chef or how to make computers do the work for us

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, nodeend

NODE_CONFIG = <<-EOS file_cache_path '/tmp/chef-solo' cookbook_path '/tmp/chef-solo/cookbooks' role_path '/tmp/chef-solo/roles'EOS...

Page 63: Chef or how to make computers do the work for us

...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-solo; fi" upload("cookbooks", "/tmp/chef-solo/", :via => :scp, :recursive => true) upload("roles", "/tmp/chef-solo/", :via => :scp, :recursive => true) upload("nodes/#{node}.json", "/tmp/chef-solo/node.json", :via => :scp) 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 endend

Page 64: Chef or how to make computers do the work for us

SSH access

Page 65: Chef or how to make computers do the work for us

├── Capfile├── config├── cookbooks├── nodes├── README.md├── roles└── ssh_keys ├── anna.lesniak ├── artur.bilski ├── ... └── marcin.kulik

Page 66: Chef or how to make computers do the work for us

# 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_keysend

Page 67: Chef or how to make computers do the work for us

# cookbooks/access/templates/authorized_keys.erb

# Generated by Chef, do not edit!

<%= @ssh_keys.join("\n") %>

Page 68: Chef or how to make computers do the work for us

Tips

Page 69: Chef or how to make computers do the work for us

Learn step by stepEC2 + Chef + Knife + Opscode... = Fuuuuuuuuuuuuuuuuuuuuu

Page 70: Chef or how to make computers do the work for us

Start with chef-solo

Page 71: Chef or how to make computers do the work for us

Run on local machineEasy to troubleshoot problems

Page 72: Chef or how to make computers do the work for us

Use Vagranthttp://vagrantup.com/

Great for testing cookbooks - doesn't pollute your system

Page 73: Chef or how to make computers do the work for us

Q?

Page 74: Chef or how to make computers do the work for us

[email protected] | @sickill | https://github.com/sickill