Converging Your Own Home Listing with Chef

Document created by Soujanya Naganuri on Jun 1, 2017
Version 1Show Document
  • View in full screen mode

I will take you through using Chef and Homebrew to control your private home listing in Mac OS X. i've also protected a neat cookbook so that you can assist you to use Homebrew as your native packaging system in Chef.

 

TL;DR

Chef is awesome. you may take hold of the cookbook to use Homebrew because the native packaging gadget for Chef DevOps on GitHub at chef-homebrew. handling your configuration isn’t only for servers. in case you spend any time at a command line, you owe it to yourself to use tmux (or display). The formatting of the code samples in this post is a work in progres

 

Getting started with Chef

 

Mac OS X, and the current breed of open source developer equipment (drastically Homebrew and RVM) introduce an thrilling opportunity when it comes to coping with one’s domestic directory. these mechanisms allow you to set up arbitrary packages, owned and run by means of your very own user.

 

 

So, you’ve got a package deal manager which installs software program and permits you to begin/stop offerings. You’ve got a gaggle of configuration files, each for the ones offerings, and dotfiles in your home directory. What’s the best manner to manipulate those? With a few sort of configuration management tool, of path! There’s masses to pick out from (which includes lcfg, cfengine and Puppet) but the one I’ve been running with on and rancid for the past couple of years is Chef.

 

How do you get started out? well, in my opinion, I’d simply pick out one piece of configuration to control. allow’s say, for instance, that you need to control your tmux configuration. permit’s create a cookbook for that. start with a repository:

 

mkdir personal-chef
cd personal-chef
touch README
git init
git add .
git commit -m "First post."

 

I really like to use bundler to control gem dependencies. There’s handiest one dependency proper now – chef – however we'd as well start off sensibly. Create a Gemfile in the root of your repository and supply it the subsequent content material:

 

source :rubygems
gem 'chef'

 

save that and run:

 

bundle

 

so one can installation chef & its dependencies. you would possibly want to noodle around with a .rvmrc to get an isolated gemset for the assignment too (I do), but that’s very an awful lot a private taste factor (that I’ll cover in any other post).

Now permit’s create a without a doubt basic configuration for chef-solo to permit it to position things in the right region, and to discover its roles & cookbooks. Create a config folder in your new repository and upload the subsequent to config/solo.rb:

root_path = File.expand_path(File.join(File.dirname(__FILE__), '..'))
cookbook_path   File.join(root_path, 'cookbooks')
role_path   File.join(root_path, 'roles')
# Move all the state stuff from /var/chef. I wish there was a single config
# variable for this!
state_root_path = File.expand_path('~/.chef/state')
file_cache_path  "#{state_root_path}/cache"
checksum_path   "#{state_root_path}/checksums"
sandbox_path   "#{state_root_path}/sandbox"
file_backup_path "#{state_root_path}/backup"
cache_options[:path] = file_cache_path

I haven’t stuck all of the paths pointing to /var/chef however it has been sufficient for chef to prevent whining at me up to now. ;-)

Now let’s create ourselves a few facts that tells chef what we need to do with ourselves. as the configuration grows larger, I’m likely going to need to introduce roles, however allow’s keep it simple for now. Create config/mathie.json (alter to flavor!) and fill it in with the following:

{
  "run_list": [ "recipe[tmux]" ]
}

quite simple for now. The final little bit of context we’re going to want earlier than we get to the interesting bits is a short script to run chef. here’s one I organized in advance, called converge.sh:

#!/bin/bash
chef-solo -c config/solo.rb -j config/mathie.json $*

(That should possibly be package exec chef-solo however it failed the time I attempted it, and i haven’t but tracked down why.) Make it executable:

chmod +x ./converge.sh

upload that lot for your git repository and commit if you haven’t been doing so already (little and often!). The final thing we need to do before we will affirm the machinery is running is create a skeleton for the cookbook itself:

mkdir -p cookbooks/tmux/recipes
touch cookbooks/tmux/recipes/default.rb

All being properly, the next issue we are able to do is take a look at that it’s all stressed up efficiently:

./converge.sh

which have to spit out ~6 traces from chef announcing that it’s beginning, finishing and tidying up. If that doesn’t work so nicely, strive:

./converge.sh -l debug

(that's why we stuck the $* on the end of the chef-solo invocation in there!) and see if you could see what’s going incorrect. If you can’t, drop me a remark and that i’ll see if i will help!

 

Converging on tmux

 

So, we’ve got the infrastructure in region. subsequent component we want to determine out is installing applications. Chef has a few neat separation between what it refers to as ‘sources’ and 'providers’ so that, on your recipes, you may say:

 

package 'tmux'

and it will do the proper issue, irrespective of what platform you’re on. lamentably, Chef doesn’t ship with the capacity to control applications with Homebrew. That’s why I come in. I’ve prepare a cookbook so that you can installation homebrew if it doesn’t already exist, maintain it updated and, most importantly, hook into Chef to apply Homebrew as the native package manager. allow’s pull that extremely good cookbook in the usage of git’s subtree merge strategy (which, considering the fact that we’re DevOps Ninjas, in this example, is quite tons the manner forward):

git remote add -f homebrew git://github.com/mathie/chef-homebrew.git
git merge -s ours --no-commit homebrew/master
git read-tree --prefix cookbooks/homebrew -u homebrew/master
git commit -m "Pull in mathie's awesome Homebrew cookbook."

 

(If I’ve just misplaced you, or you’re not the usage of Git, head to https://github.com/mathie/chef-homebrew and stick the contents of that repository in cookbooks/homebrew.)

Now we need to be precise to go along with getting tmux established through Homebrew. Edit cookbooks/tmux/recipes/default.rb in order that it consists of:

include_recipe 'homebrew'
package 'tmux'

shop and commit (early and often!). Now run ./converge.sh. It have to bumble around for a piece, installing Homebrew in case you haven’t got it already, and putting in tmux the use of Homebrew. Run it once more, and it shouldn’t do a whole lot. Win.

 

Converging a tmux configuration

 

closing thing we’re going to do right here, just to illustrate managing configuration too, is installing a controlled ~/.tmux.conf document. upload the subsequent to cookbooks/tmux/recipes/default.rb:

template "#{ENV['HOME']}/.tmux.conf" do
  source "tmux.conf.erb"
end

and create cookbooks/tmux/templates/default/tmux.conf along with your favored tmux configuration. permit’s say, for instance, which you’re on Mac OS X and you need to know the modern-day battery stage for your reputation bar:

set -g status-right "#[fg=green]#(pmset -g ps |awk 'BEGIN { FS=\"\t\" } /InternalBattery/ { print $2 }')"

(kinda beneficial, huh?) Now run:

  ./converge.sh

and your tmux configuration will magically be hooked up into ~/.tmux.conf. Neat, huh?

 

end

 

Chef isn’t just for managing your massively scaled production infrastructure. you could use it to manipulate plenty of different matters too, together with your private home listing. The win right here is magnified with each computer you figure with (home laptop, work computer and a laptop even as you’re out and approximately?).

 

So, have you ever tried using Chef to manipulate your house listing? How did you discover it? correct stuff? awful stuff? What other strategies have you ever tried?

 

replace similarly to publishing the Cookbook on Github as chef-homebrew I’ve also uploaded it to the Opscode Cookbooks. Get it here: homebrew.

1 person found this helpful

Attachments

    Outcomes