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

ChefDK or how I fell in love with embedded Ruby.

ChefDK or how I fell in love with embedded Ruby.

My talk at DevOpsDays Austin 2015

JJ Asghar

May 04, 2015
Tweet

More Decks by JJ Asghar

Other Decks in Technology

Transcript

  1. ChefDK or how I fell in love with embedded Ruby.

    Jonathan “JJ” Asghar May 2015
  2. Who Am I? • Senior Partner Engineer • [email protected]

    @jjasghar • Loves Beer, Hugs, and Obscure Television
  3. tl;dr of Chef • An Open Source IT Automation Platform

    • Supporting • Windows • Linux • Mac • AIX • Multiple Networking stacks • Multiple Storage stacks
  4. tl;dr of Chef Chef turns infrastructure into code. With Chef,

    you can automate how you build, deploy, and manage your infrastructure. Your infrastructure becomes as versionable, testable, and repeatable as application code. stolen from https://www.chef.io/chef/
  5. tl;dr of Chef Even more simply said: You can TDD

    your infrastructure and have confidence in your infrastructure changes.
  6. Back to the dark times… source 'https://rubygems.org' gem 'berkshelf', '>

    3' gem 'chef', '>= 11.8' gem 'rake', '>= 10.2' gem 'rubocop', '= 0.23' gem 'foodcritic', '< 4.0' gem 'chefspec', '>= 3.4' gem 'serverspec', '>= 1.6' gem 'test-kitchen' gem 'kitchen-vagrant' gem 'guard', '>= 2.6' gem 'guard-rubocop', '>= 1.1' gem 'guard-foodcritic', '>= 1.0.2' gem 'fog' Thanks cz :)
  7. The dark times… So with the Gemfile you’ll have to

    start learning rvm/ rbenv or the like…
  8. Then something like this happens…. Fetching gem metadata from https://

    rubygems.org/........ Fetching gem metadata from https://rubygems.org/.. Resolving dependencies... Bundler could not find compatible versions for gem "json": In Gemfile: chef (= 11.4.4) depends on json (<= 1.7.7, >= 1.4.4) berkshelf (= 2.0.4) depends on json (1.8.0)
  9. So what would you do? •Break open you Gemfile •Cry

    a little •Try to figure out what was conflicting •bundle install •Cry a little more •GOTO 10
  10. So what is the ChefDK? ChefDK is everything you need

    to build that “workstation” all bundled up in one package. It has the generally accepted packages and plugins that the majority of users want. 24
  11. Workstation? •A workstation is a computer that is configured to

    run knife, to synchronize with the chef-repo, and interact with a single Chef server. The workstation is the location from which most users will do most of their work. stolen from http://docs.chef.io/workstation.html 25
  12. And to build a workstation you used to need 26

    source 'https://rubygems.org' gem 'berkshelf', '> 3' gem 'chef', '>= 11.8' gem 'rake', '>= 10.2' gem 'rubocop', '= 0.23' gem 'foodcritic', '< 4.0' gem 'chefspec', '>= 3.4' gem 'serverspec', '>= 1.6' gem 'test-kitchen' gem 'kitchen-vagrant' gem 'guard', '>= 2.6' gem 'guard-rubocop', '>= 1.1' gem 'guard-foodcritic', '>= 1.0.2' gem 'fog'
  13. ChefDK is built on embedded ruby Chef provides a package

    that installs ruby in a non- critical path for you along with multiple gems. 27
  14. How the ChefDK is built Also known as what was

    needed to create to get to the “light times”
  15. How ChefDK is built Chef created two projects to deal

    with the lack of tools: appbundler omnibus 36
  16. appbundler • https://github.com/chef/appbundler • Appbundler reads a Gemfile.lock and generates

    code with gem "some-dep", "= VERSION" statements to lock the app's dependencies to the versions selected by bundler. 37
  17. appbundler • It “packages” up a ruby application via a

    Gem.lock file • It deals with the dependancies and other rubyish stuff • It removes the need for bundle exec 38
  18. omnibus • Have you tried to upstream a “fast moving”

    piece of software to distros? • Omnibus was created to allow for packaging of quickly moving software 40
  19. omnibus • Multiple companies have leveraged this to build all

    in one builds for their stacks. • Moves extremely fast • Extremely vibrant community • Just google “github omnibus” • Leverages test-kitchen to build packages for other distros and systems 41
  20. omnibus • Multiple companies have leveraged this to build all

    in one builds for their stacks. • Dell with Enstratius or now known as Dell Cloud Manager • Heavy Water Ops with Sensu • Gitlab uses it for Gitlab installation 42
  21. The chef binaries • ChefDK comes with a bunch of

    wrapper commands with chef as the way to call them. • Here’s just a few of them. 45
  22. chef gem • The best way to inject a specific

    gem into the embedded ruby. •chef gem install knife-c2 •chef gem install knife-openstack 46
  23. chef generate • Builds a de-facto cookbook with some of

    the “best” practices for creating your cookbook: 48
  24. chef generate 49 15:49:14 ~ > chef generate cookbook fake_cookbook

    Compiling Cookbooks... Recipe: code_generator::cookbook * directory[/Users/jasghar/fake_cookbook] action create - create new directory /Users/jasghar/fake_cookbook * template[/Users/jasghar/fake_cookbook/metadata.rb] action create_if_missing - create new file /Users/jasghar/fake_cookbook/metadata.rb - update content in file /Users/jasghar/fake_cookbook/metadata.rb from none to ba0ae2 (diff output suppressed by config) * template[/Users/jasghar/fake_cookbook/README.md] action create_if_missing - create new file /Users/jasghar/fake_cookbook/README.md - update content in file /Users/jasghar/fake_cookbook/README.md from none to 6b3b9c (diff output suppressed by config) * cookbook_file[/Users/jasghar/fake_cookbook/chefignore] action create - create new file /Users/jasghar/fake_cookbook/chefignore - update content in file /Users/jasghar/fake_cookbook/chefignore from none to 9727b1 (diff output suppressed by config) * cookbook_file[/Users/jasghar/fake_cookbook/Berksfile] action create_if_missing - create new file /Users/jasghar/fake_cookbook/Berksfile - update content in file /Users/jasghar/fake_cookbook/Berksfile from none to 4a0cca (diff output suppressed by config) * cookbook_file[/Users/jasghar/fake_cookbook/spec/spec_helper.rb] action create_if_missing - create new file /Users/jasghar/fake_cookbook/spec/spec_helper.rb - update content in file /Users/jasghar/fake_cookbook/spec/spec_helper.rb from none to 945e09 (diff output suppressed by config) * template[/Users/jasghar/fake_cookbook/spec/unit/recipes/default_spec.rb] action create_if_missing - create new file /Users/jasghar/fake_cookbook/spec/unit/recipes/default_spec.rb - update content in file /Users/jasghar/fake_cookbook/spec/unit/recipes/default_spec.rb from none to 09c3c9 (diff output suppressed by config) * directory[/Users/jasghar/fake_cookbook/recipes] action create - create new directory /Users/jasghar/fake_cookbook/recipes * template[/Users/jasghar/fake_cookbook/recipes/default.rb] action create_if_missing - create new file /Users/jasghar/fake_cookbook/recipes/default.rb - update content in file /Users/jasghar/fake_cookbook/recipes/default.rb from none to cd7656 (diff output suppressed by config) * execute[initialize-git] action run - execute git init . * cookbook_file[/Users/jasghar/fake_cookbook/.gitignore] action create - create new file /Users/jasghar/fake_cookbook/.gitignore - update content in file /Users/jasghar/fake_cookbook/.gitignore from none to dd37b2 (diff output suppressed by config)
  25. test-kitchen • Created by Fletcher Nichol • Created because he

    got annoyed with so many VagrantFiles • Super-nice Canadian 51
  26. test-kitchen • http://kitchen.ci • The quickly becoming the standard for

    integration testing machines • There are integrations for • Chef • Ansible • Puppet • Salt 52
  27. test-kitchen • A simple yaml configuration • It can leverage

    multiple backends here’s a few of them: • Vagrant (default) • OpenStack • Amazon EC2 • Docker • DigitalOcean • Rackspace 53
  28. test-kitchen 54 --- driver: name: vagrant provisioner: name: chef_zero platforms:

    - name: ubuntu-14.04 - name: centos-6.6 suites: - name: default run_list: - recipe[blah::default] attributes:
  29. test-kitchen • Runs a small application called “bussers” after the

    converge • serverspec • bats • bash • Do yourself a favor and go to: https://github.com/test-kitchen • If y'all want a demo of it running find me after this talk, I truly love this software. 55
  30. foodcritic • Ran by volunteers but under Andrew Crump’s github

    account • Created for a way to help standardize code • http://www.foodcritic.io/ • You can write your own for your own coding style 57
  31. foodcritic • Some examples: • FC030: Cookbook contains debugger breakpoints

    • FC008: Generated cookbook metadata needs updating • FC048: Prefer Mixlib::ShellOut 58
  32. foodcritic • tl;dr • Rubyists: Think of Rubocop for Chef

    • Pythonists: Think of pylint for Chef 59
  33. chefspec Unit tests are important, you know you don’t remember

    why you wrote that code that way 6 months down the line.
  34. chefspec • Created by Seth Vargo • Unit testing your

    recipes • The main site you need: • https://github.com/ sethvargo/chefspec/tree/ master/examples 61
  35. chefspec I tell people that it’s good to write chefspec

    when you hit your 1.0.0 release of your cookbook 62
  36. chefspec Typical TDD with Chef is hard, you don’t know

    what you’re doing till you do it. Start small. 63
  37. chefspec 64 # in your install.rb package 'explicit_action' do action

    :install end # in your install_spec.rb describe 'package::install' do let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) } it 'installs a package with an explicit action' do expect(chef_run).to install_package('explicit_action') end end
  38. chefspec It runs a complete chef-client run in memory and

    checks against the compiled code to confirm it’s doing what it expects. It only takes seconds to run! 65
  39. curl -L https://chef.sh | sudo bash -s -- -P chefdk

    The easiest way to install chef-dk I’m trying to get chef-dk.sh too
  40. Any Questions? Don’t hesitate to contact me: [email protected] or j^2

    on freenode. Or track me down, I really do love talking about this stuff.