Skip to content

Instantly share code, notes, and snippets.

View coateds's full-sized avatar

David Coate coateds

View GitHub Profile
@coateds
coateds / ChefForWindowsTutorial7.md
Last active February 5, 2018 21:57
Tutorial 7: Starting to work with a Chef Server

Tutorial 7: Starting to work with a Chef Server

At this point you should have one or more cookbooks each with one or more recipes. We have been using test kitchen to develop and test these. That is kitchen's purpose. In a real Chef deployment, these cookbooks should be hosted on a Chef server. So the next step would be to copy these cookbooks to a Chef server. Of course, that would imply that we have a Chef server!

One option is to setup a Linux server, download and install Chef server on it and you can be off to the races. This is a long and laborious process! The alternative is Hosted Chef. This is a free service from Chef. You can spin up a Chef server in Chef's cloud rather quickly and is a perfectly adequate resource for learning.

Hosted Chef

@coateds
coateds / ChefForWindowsTutorial6.md
Last active February 1, 2018 16:43
Tutorial 6: Custom Resources and Libraries

Tutorial 6: Custom Resources and Libraries

In the last tutorial, I demonstrated how to build a Ruby function, (def ... end block) and suggested these could be moved to a library. A library is a collection of arbitrary Ruby code. In practice, this seems to be Ruby functions in an exported name space.

As I work through modularizing and reusing resources and code, I realize that custom resources are the place to start. That will likely mean a complete reorganization of this tutorial some time in the future. In the mean time, I will add the contents at the end of the README.md for a project that made heavy use of custom resources, chocoupgrades.

Libraries

Use existing libraries

A great deal of Chef is implemented as Ruby. In addition to resources, there are ruby functions built into Chef that are available for use. In the following example I will use functions in file_edit.rb. In my installation, this file is found: `C:\opscode\chefdk\embedded\lib\ruby\gems\2.4.0\gems\chef-13.4.19-universal-mingw32\l

@coateds
coateds / ChefForWindowsTutorial5a.md
Last active January 18, 2018 18:09
Tutorial 5a: Advanced ChefSpec

Tutorial 5a: Advanced ChefSpec

When going beyond the testing of simple resources, ChefSpec gets complicated quick. I have already shown how certain types of guards can "blow up" the entire testing, making it useless.

Here is a good place to start in going farther down the ChefSpec rabbit hole. How far down that hole you want to go is up to you! https://chefspec.github.io/chefspec/

A start on a simple example:

  • Attribute
node.default['default']['var'] = 'Set in Attr Default'
@coateds
coateds / ChefForWindowsTutorial5.md
Last active May 27, 2019 20:40
Tutorial 5: Integrating with PowerShell

Tutorial 5: Integrating with PowerShell

As a windows guy, I am excited anytime I see that something is compatible with PowerShell in any way. So far, I have had a lot of success calling PowerShell snippets from Chef/Ruby. Obviously, this will not be a tutorial on scripting. It will be necessary to learn some (Chef specific) Ruby. So far, I have been able to keep that at a minimum. Here is one of the better tutorial pages I have found so far: https://learn.chef.io/modules/ruby-behind-chef#/

To this point, I have identified 4 ways of working with Powershell

  1. The powershell_script resource code block
  2. As a guard within a resource
  3. As an integration test
  4. To return a value to Ruby and extended Ruby for further processing

There are sufficient examples of items 1 - 3 in tutorial 4 to get started. As mentioned before, the code in items 2 and 3 can be exactly the same code. As long as it returns true/false.

@coateds
coateds / ChefForWindowsTutorial4.md
Created January 16, 2018 19:09
Tutorial 4: Windows resources and tests

Tutorial 4: Windows resources and tests

In this section there likely will not be any large new concepts. Rather it will be a collection of recipes and code I have collected into one location.

In an earlier tutorial, I suggested that I would not provied step-by-step instruction on resources because they are very well documented. In addition, in many cases it is easier to learn from examples than by explanation. Here, I will provide a lot of examples!

However, I do think there is value in reviewing the use of PowerShell both in Recipes and in Testing. It turns out that you can use PowerShell in both recipes and InSpec integration tests. I am putting most of these explanations in as comments inline to the files themselves, then they are copied and posted below. Some highlights include:

  • Command outputs often return with a "\r\n" which is a carriage return/line feed. The .chop method eliminates those characters.
  • The to_s method converts something to a string. I am still learning where this is actually
@coateds
coateds / ChefForWindowsTutorial3.md
Created January 16, 2018 19:03
Tutorial 3: Increasing the complexity - Lint, Unit and Integration Testing

Tutorial 3: Increasing the complexity - Lint, Unit and Integration Testing

To this point, we have written some simple recipes for Windows Chef Test Kitchen VMs. I am going to shift gears and build up some more complete examples on Linux. I have two reasons for this

  1. I want to keep working on my Linux skills. This is a great way to build Linux machines to play with.
  2. Chef resources and testing tools are more complete for Linux. Therefore the Linux examples will be simpler.

Chef comes with a series of testing and integrity tools. These help with discovering syntactical errors and some logic issues before you even run a converge, as well as confirm the results after the converge.

Linting tools

At this point I have not done much with these in practice. The two tools are Rubocop (now cookstyle) for Ruby code and Foodcritic for the ChefDSL code.

@coateds
coateds / ChefForWindowsTutorial2.md
Last active January 30, 2018 23:09
Library Cookbooks that do Stuff

Tutorial 2: Library Cookbooks that do Stuff

One thing I do not want to do here is reinvent the wheel. The use of resources are well documented here: https://docs.chef.io/resource.html. The concept of idempotence seems to be well explained out there. I encourage you to look that up. It is core to how Chef is supposed to work. Please do not interpret my brevity here to indicate these are not important subjects. You should spend a great deal of time studying resources and how to make them idempotent. I just find the documentation and explanations to be complete and plentiful. I have very little to add.

At this point, I am going to talk about the different types of cookbooks. In one sense, a cookbook is a cookbook is a cookbook. They are all created exactly the same way. However, they fulfill different roles. This is my word. Chef does not make some of the distinctions that I do.

The cookbooks I have presented so far are for building test kitchen instances. Another type of cookbook contain code and resources

@coateds
coateds / ChefForWindowsTutorial1.md
Last active January 16, 2018 19:11
Tutorial 1: ChefDK workstation and first Test Kitchen Cookbooks

Tutorial 1: ChefDK workstation and first Test Kitchen Cookbooks

As stated in my introduction, this tutorial is not for beginners! In some cases, what I will present are options and suggestions. Some of the technologies I will present are going to be optional and may require extra study. For instance, I will be using Chocolatey throughout many of the tutorials in this series. If this is new and of interest to you, I recommend extra study. I happen to think Chocolatey works very well and is worth the time.

Because some of the supporting technologies may be unfamiliar, I cannot give an estimate of the time it will take to work through this tutorial. It could take several hours or several days.

The Chef workstation

In an enterprise Chef installation/topology, most of the development work is done on a Chef workstation. This gets integrated with a Hypervisor/Cloud to build VM "clients" for which cookbooks with recipes get applied for configuration. We will start this tutorial by installing and configuring a

@coateds
coateds / ChefForWindowsIntro.md
Last active January 16, 2018 19:17
Introduction

Purpose and Audience

After several false starts, I finally settled on Azure as the best option for hosting my Chef development (test kitchen) virtual machines. I am not going to suggest this is the best option for everyone, nor is it a perfect solution for me. When working in a pure Windows environment, particularly with an Active Directory domain, I do like the HyperV driver as well, but at the time of this writing (late 2017) I find that it is problematic when building Linux test kitchen instances and depending on the version of the host, it will not successfully remove an instance without a reboot. I admit I am rather Microsoft centric. I have fared well professionally in that space. If you are like me, you may find Chef challenging to learn. Many of the best examples and tutorials out there are written for Linux. However, Chef and Microsoft seem to be teaming up so it may prove fruitful to try!

I am attempting to write the tutorial that I would want. I will try to guide the process to the simplest poss

LOCAL COOKBOOK DEVELOPMENT BADGE TOPICS

The Local Cookbook Development badge is awarded when someone proves that they understand the process of developing cookbooks locally. Candidates must show:

  • An understanding of authoring cookbooks and setting up the local environment.
  • An understanding of the Chef DK tools.
  • An understanding of Test Kitchen configuration.
  • An understanding of the available testing frameworks.
  • An understanding of troubleshooting cookbooks.
  • An understanding of search and databags. Here is a detailed breakdown of each area.