Showing posts with label chef. Show all posts
Showing posts with label chef. Show all posts

Friday, 8 July 2016

Habitat.sh

I am a big Chef fanboy. It's not something I hide very well or attempt to hide. Chef was a huge part of my role within a release engineering team for a few hours and I think that using it has made lots of positive impacts to the organisation I work for as we strive towards continuous delivery.

You can imagine my giddy excitement then when the current Release Engineering team managed to snag a few Chef employees to come into our office to talk about their new product habitat. Also I'm really struggling to get out to events this year and that's probably one of the career resolutions I've struggled with the most.

Nathen Hervey and Thom May gave us a an overview of the solution and it was great. They have a fantastic interactive demo you can walk through in your browser on their site as well so go and take a look at that.

I'll try to give a brief thought on why Habitat is so exciting. Chef has a node orientated view of the world. That's where the rubber hits the road and things like runlists and recipes get applied and attributes get levelled out and that's brilliant for a lot of applications that people run or for infrastructure servers but it does muddy the waters a little when it comes to distributed services and the clusters that offer that service. That's where Habitat comes in with it's gossip protocol to spread config updates and the supervisor to manage the processes you are getting considerable value add over Chef.

There was of course ways to try and bend Chef to work with these services. We had for example many clusters of couchbase servers that fulfill different behaviours within the same environment and so we used the Role concept in Chef to manage that. That was because for the most part the install was the same but there was some config (other cluster members) that was what differentiated one cluster from another. Using roles allowed us to dynamically discover other servers that existed in the cluster but sadly didn't alllow for us to update all of them at once like Habitat is built for.

The one question I have is how this is going to interact with Chef and other stores in terms of forming an open source toolchain that will allow for you to manage and deploy modern distributed services. Most of the demo we saw involved manually pushing messages in using the gossip protocol (which is used in Ubers RingPop) and so I'm guessing you could probably have Chef Providers or similar which would allow you to do that. I would guess you want to have your config that you've just pushed to your production cluster stored somewhere... Also the attribute precedence setting and levelling in Chef is GOLD so you would really want to be able to hook back into that.

I am feeling pretty excited about the opportunities that habitat.sh can bring to us so much so I've started learning Rust so I can attempt to contribute something to the product.

Thursday, 13 December 2012

How to install a ruby gem in chef

Hello

This seems to stump a few people. You are wanting to leverage some super cool gem to make your recipes even more all powerful right? The thing is you can't get the bloody thing to install

Fear not



gem_package "nokogiri" do
  action :nothing
end.run_action(:install)

gem 'nokogiri'
require 'nokogiri'

This is all the code that you need to install and require a gem in your recipe.


Friday, 13 July 2012

Unit Testing Chef

I am a little confused about Unit Testing.

Yeah I know, I shouldn't say it but sometimes a man just needs let his feelings be known.

I'm new to the whole DevOps sphere and release engineering etc. I am a quasi experienced developer who has had a little bit of experience with unit testing and test driven development. I don't want to blow my own trumpet here guys but I did in fact buy a copy of "THE ART OF UNIT TESTING" and whats more one day I will read it*
So when working on my current project, which is my first foray into DevOps and Chef I was quite enthralled by the concept of testing all this good stuff.

My little head buzzing with yet more buzzwords a heady mix of infastructure as code coupled with test driven development. Things are going be great lads we're going to finally be able to TEST ALL OF THE THINGS.

So I cracked open a copy of Test-Driven Infastructure with Chef and I looked at minitest-handler and I looked at Chef Spec and I thought yeah there's some lovely things going on here. There's some great state checking/functional testing going on with these tools and I like it but something was nagging away at me. What about unit testing?

Now whenever I did my brief toe in the water searches on this I was getting more confused. Whenever someone asked for a good unit testing framework they were being pointed towards these tools almost exclusively. Odder still nobody was really correcting them or saying no that's not quite what I'm after. So that begs the questions right, have I gone mad? What is Unit Testing with Chef?

Well let's start with what it's not, or at least what I think it's not. I don't believe simply checking the state of the box after your runlist has executed cuts it. That's a functional test and you are treating your chef code as a blackbox right? That's great that after running 50 recipes I know that 7 services have been created but do I really know that my code underlying is executing correctly?

So it seems very quickly that we've gone from TEST ALL OF THE THINGS to TEST WHAT ALL OF THE THINGS HAVE DONE WHEN THEY ARE FINISHED AND HOPE THEY AREN'T USING THE SAME RESOURCES or put another TEST ALL OF THE THINGS BY ASSERTING THE POSITIVE PATH
To me I feel like if I have crafted a provider or a library, then I should be looking to unit test those in the same way that the code I'm deploying has been tested. Is that too much? If I have a library with 20 methods I should have tests for each method executing as much of the code path as I think is appropriate.

So what I did was start looking at what happens during a node converge when a the cookbooks/methods/providers get loaded for execution and I tried to do the same from within a minitest file. Below is my attempt at testing the registry_helper library within the windows cookbook as I had noticed some funk with w2008r2 servers

require 'chef' 
require "minitest/autorun" 
require 'win32/registry' 
require 'ruby-wmi'


class RegistryTest < MiniTest::Unit::TestCase 

def setup 
   Kernel.load(File.expand_path(File.dirname(__FILE__))<< "\\..\\..\\..\\..\\libraries\\registry_helper.rb") 
end 

def test_GetHiveNameLM
   hivename = Registry.get_hive_name("HKLM\\SOFTWARE\\Wow6432Node\\Clients\\Mail")       
   assert_equal "HKEY_LOCAL_MACHINE", hivename 
end 

So in the setup we're loading the library and then we can access methods defined within it and check the responses coming back. Similar functionality could be exercised in a test.rb within the cookbook but I think we would lose the ability to have asserts and metrics displayed. Some more complicated functions such as managing node objects or resources when testing providers can be exercised as well. Example shown below

node = Chef::Node.new 
run_context = Chef::RunContext.new(node, Chef::CookbookCollection.new) 
resource = Chef::Resource::ResourceName.new("UnitTest",run_context) 
resource.version("version") 
provider = Chef::Provider::ResourceName.new(resource, run_context) 
provider.uuid = "123" 
result = provider.dosomestuff() 

Another potential advantage over having this as a test.rb is that we could enforce unit test runs before allowing these cookbooks to be uploaded meaning that broken or ineffective cookbooks never end up in the deployment chain. This is similar to function used on most CI systems with the code which we are deploying.

It's not perfect and it's not pretty but it's allowing me to drive specific methods from my libraries and provider.

Thoughts? Comments?

*I jest I did read it and it was a very good read. Much recommended