I think it is safe to say that if you are a developer, you probably hate programming in Windows. There are hundreds of organisations worldwide with their legacy applications running on Windows, and they are still struggling to automate their infrastructure due to the misconception that DevOps is just for the open source world—and that’s paired with a lack of proper package management solutions for Windows. Personally, after a brief period of using Chef on Windows, and discovering how convenient they have made it to implement config management for Windows platform, I thought it would be worth sharing my experience.
What is Chef?
Chef is an automation platform founded in 2008, and it gives you automation solutions for configuration management, installing packages, etc. It transforms infrastructure to code—that is, it automates how infrastructure is configured, deployed and managed across your network irrespective of the size of your environment.
Chef started its support for Microsoft Windows from 2011 when it released the knife-windows plugin. To get started, you can install the ChefDK install package from here. This makes it easier than ever to get Chef and Ruby installed on Windows.
You can check to see if the Chef Client Service is operational by running the following commands:
c:\WINDOWS\system32>chef-service-manager -a install Service “chef-client” has successfully been installed. C:\WINDOWS\system32>chef-service-manager -a start One moment...Start pending One moment...Start Pending Service “ chef-client” is now ‘running’. C:\WINDOWS\system32>chef-service-manager State of chef-client service is: running C:\WINDOWS\system32>chef Usage: chef -h/--help chef -v/--version Chef command [arguments...] [options...] Available Commands: exec Runs the command in context of the embedded ruby env Prints environment variables used by ChefDK gem Runs the ‘gem’ command in context of the embedded ruby generate Generate a new app, cookbook, or component shell-init Initialize your shell to use ChefDK as your primary ruby install Install cookbooks from a Policyfile and generate a locked cookbook set update Updates a Policyfile.lock.json with latest run_list and cookbooks push Push a local policy lock to a policy group on the server push-archive Push a policy archive to a policy group on the server show-policy show policyfile objects on your chef server diff generate an itemized diff of two policyfile lock documents provision provision VMs and clusters via cookbook export Export a policy lock as a chef zero code repo clean-policy-revisions Delete unused policy revisions on the server clean-policy-cookbooks Delete unused policyfile cookbooks on the server delete-policy-group Delete a policy group on the server delete-policy Delete all revisions of a policy on the server undelete undo a delete command verify Test the embedded ChefDK application
Cooking on Windows
Developing cookbooks on Windows is as easy as on any other platform. Let’s start with a simple example of creating a text file on the desktop. Navigate to the cookbook folder in your chef-repo, and generate a new cookbook called ‘testcookbook’.
C:\User\Dimple\chef-repo\cookbooks.chef generate cookbook testcookbook Generating cookbook testcookbook - Ensuring correct cookbook file content - Committing cookbook file to git - Ensuring delivery configuration - Ensuring correct delivery build cookbook content - Adding delivery configuration to feature branch - Adding build cookbook to feature branch - Merging delivery content feature branch to master Your cookbook is ready. Type ‘cd testcookbook’ to enter it. There are several commands you can run to get started locally developing and testing cookbooks. Type ‘ delivery local ..help see a full list/ Why not start by writing a test? Tests for the default recipe are stored at: test/smoke/defualt/_test.rb If you‘d prefer to dive right in, the default recipe can be found at: recipes/default.rbThe above commands create a template of a cookbook. Now, to write the first recipe, open the default.rb file in the Recipes folder.
# # Cookbook:: testcookbook # Recipe:: default # # Copyright:: 2017, The Authors, All Rights Reserved. File ‘ c:\user\Dimple\Desktop\test.txt’ do Content ‘This is a test file” action :create end
The above recipe contains a simple ‘file’ resource which creates a text file called test and puts in the content “This is a test file” on the system desktop. If you are familiar with writing Chef for Linux, you will see how they have kept the ‘file’ resource the same for both platforms. (This is one of the advantages I observed.) To run the above recipe, execute the chef-client command in the local mode as:
Chef-client --local-mode –runlist “recipe[testcookbook]” C:\Users\Dimple\chef-repo\cookbooks>chef-client --localmode --runlist “recipe[testcookbook]” [2017-01-09T13:58:12+00:00] WARN: No config file found or specified on command line , using command line options Starting chef client, version 12.17.44 Resolving cookbooks for run list : [“testcookbook”] Synchronizing cookbooks testcookbook (0.1.0) Installing cookbook Gems: Compiling cookbooks... Converging 1 resources Recipe: testcookbook::default file[C:\Users\Dimple|Desktop|test.txt] action create Create new file C:\User\Dimple|Desktop\test.txt Update content in file C:\Users\Dimple\Desktop\test.txt from none to eZdofe --- C:\Users\Dimple|desktop\test.txt 2017-01-09 13:58:16.000000000 =0000 @@ -1 =1,2 @@ = this is a test file Running Handlers : Running Handlers Complete Chef Client Finished, 1/1 resource updated in 03 seconds
Next, let’s create a more complex cookbook to do the following:
Install IIS (Web-Server) if it is not already installed.
– Install the IIS Management Console (Web-Mgmt-Console) if it is not already installed.
– Start and enable the IIS Service (W3SVC)
– Set the Default.htm webroot page to whatever is configured in our template HTML file, index.html.erb
Create a IIS Cookbook
chef generate cookbook IIS
Add the following to a new recipe:
# Cookbook: :: iis # Recipe : :: 01_install_iis # # Copyright: :: 2017, The Authors, ALL Rights Reserved powershell_script ‘Install IIS’ do code ‘ windowsFeature Web-server’ guard_interpreter :powershell_script not_if “(Get-WindowsFeature -Name Web-Server).Installed” End Powershell_script ‘Install IIS Management Console’ do Code ‘Add-WindowsFeature Web-Mgmt-Console’ Guard_interpreter :powershell_script Not_if “(Get-WindowsFeature-Name Web-MGMT-Console).Installed” end Powershell_script ‘Install ASP.NET’ do code ‘Add-WindowsFeature web-ASP-Net45’ guard_interpreter :powershell_script not_if “(Get-WindowsFeature -Name Web-Server).Installed” end Powershell_script ‘Install IIS Static Content’ do code ‘Add-WindowsFeature Web-Static-Content’ guard_interpreter :powershell_script Not_if “(Get-WindowsFeature -Name Web-Static-Content).Installed” end service ‘w3svc’ do Action [:start, :enable] end directory “C:/inetpub/wwwroot” do Recursive true end template “C:/inetpub/wwwroot/index.html” do Source ‘index.html.erb’ end
The Windows Cookbook
The Windows cookbook is a community cookbook available in the Chef supermarket that helps all Chef users create Windows-based cookbooks easily. For example, the resource ‘windows_feature’ allows you to install any software that can support or improve the functionality of the server in an idempotent way without the user worrying about the underlying powershell code.
windows_feature 'NET-Framework' do action :install end
if I were to rewrite the above-mentioned IIS cookbook using the Windows cookbook resources, it would look as below:
# Cookbook:: iis # Recipe:: 01_install_iis # # Copyright:: 2017, The Authors, All Rights Reserved. windows_feature ‘Web-Server’ do action :install end windows_feature ‘Web-Mgmt-Console’ do action :install end windows_feature ‘web-ASP-Net45’ do action :install end windows_feature ‘web-static-content’ do action :install end
Windows-specific Chef Resources
· Automatic architecture handling
And that’s it! Hopefully this will get you started with Chef on Windows, get your workstation ready, and let you run your first Windows cookbook successfully. To further build on what you have currently, I would highly recommend going through the Windows cookbook in much more detail and taking the time to understand its full potential. As much of a pain as it is to program in Windows, Chef has definitely done their part in making it as easy as possible.