I am in the process of writing up some blog posts about working with PowerShell Desired State Configuration (DSC) and OpsCode Chef from a Windows Workstation / Windows Server perspective.
This first article will cover the steps required to setup a development environment for someone that is doing work with PowerShell Desired State Configuration (DSC) or OpsCode Chef.
We will be covering the following:
- Installing git and poshgit – source control for your code and PowerShell integration for git
- Installing VirtualBox – will be used with Vagrant to allow testing of DSC scripts and Chef recipes. I am using VirtualBox instead of Hyper-V as there are a ton of pre-build images for Linux and Windows available to download on Atlas. Not having to make these yourself is a huge time saver.
- Installing Vagrant – simple virtual machine creation. If you haven’t used Vagrant before I recommend checking out this video.
- Installing ChefDK – the Chef Development Kit, includes knife, berkshelf, test-kitchen and Foodcritic
- Installing Atom – awesome text editor that includes linters which will check your code for issues as you type
Once everything is installed, we will be customizing and setting up the following:
- Customizing the PowerShell Profile to make it easier to work with Git and set environment paths
- Customizing Atom with some plugins including a ruby linter to validate the ruby we write in the chef recipes
- Setting up ssh keys for use with Git including configuring ssh agent, allowing us to push changes to git or work with private repositories
- Downloading some vagrant plugins and adding some vagrant boxes
- Configuring the knife.rb for working with Chef and creating a Berkshelf config.json file
Let’s get started!
Set Execution Policy and Install Applications
- Open an Administrative PowerShell Prompt
Customize Your PowerShell Profile
Now that you have installed PoshGit, it is a good idea to take a look at your PowerShell Profile.
You can make any sort of customization you like, but here are two suggestions for things to do in your profile:
- Refresh the user and machine path environment variables. You will need to do this to use tools from the command line especially Atom, Chef and Vagrant.
- Perform a Set-Location on the directory you use the most – for me this is my ProjectsGit directory where I keep all of my git repositories.
My profile contains some useful functions for setting and reloading environment variables which I recommend you use in your own profile.
Path Environment Variable
With many of the tools used for developing with Chef on Windows, they will require a correctly configured Path environment variable.
If one of your tools is not working or you cannot run it from the command line, there is a good chance something is wrong with your Path variable.
For example, to use Ruby from the command line after you install the ChefDK, you need to add ruby to the path variable.
If you are using the functions from my PowerShell Profile from above, it is very easy to add Path environment variables:
(Optional) Move PowerShell Profile to a Synced Drive
If you move around to different machines, it is a good idea to move your your PowerShell Profile into a synced directory like Dropbox or OneDrive. From there you can create a symlink from the profile .ps1 in the synced path to the $PROFILE path.
- Move your PowerShell profile from the $PROFILE location to the synced folder
- Open an Administrative PowerShell Prompt
- Run the following commands:
Atom is a great text editor which I like to use for everything but working on PowerShell scripts. It is my editor of choice for modifying Chef recipes.
We can make it more powerful with some additional packages.
- Open PowerShell
- Run the following commands to install some handy Atom packages
Setup Git SSH Keys
You should be using source control for your Chef recipes and PowerShell scripts. Warren Frame has an excellent blog on the topic specific to PowerShell here.
To work with git repositories, it is best to use ssh keys. On Windows, the ssh keys live under your user directory in a .ssh folder, for example C:\Users\YourName\.ssh
Setting up ssh keys on Windows for GitHub and BitBucket can be a bit of a pain, but the below will guide you through the process
Download Vagrant Boxes
Vagrant is an excellent way to test your DSC scripts or Chef recipes.
I like to use 2 boxes for my Windows 2012 R2 and Ubuntu testing with Chef/DSC. There are also several plugins that make using Vagrant even nicer.
We will install some plugins and pre-load the Vagrant boxes:
Configure Chef and Berkshelf
Next step is to get your chef user.pem file sorted out. Chef has a how-to guide for this here.
Once you have your .pem file, we will setup the knife.rb and the berkshelf config.json
Here is an example knife.rb that I use.
Here is an example berkshef .config file that I use.
(Optional) Customize the PowerShell ISE Theme
The default theme for the PowerShell ISE is boring, lets spice it up with a theme. There is a great repository with PowerShell ISE themes located at https://github.com/marzme/PowerShell_ISE_Themes
To import the themes into the PowerShell ISE, go to Tools | Options | Manage Themes | Import.
Again, I would drop the theme into a synced folder so you can use it on all your machines.
With that, you should have your Windows machine setup PowerShell DSC and Chef development. Did I miss anything? Send me a tweet @matthodge and let me know!