ChefDK on Windows – Environmental Variables

The recent release of the Chef Development Kit for Windows has been great for my work flow. If you do not have your own Ruby installed on the system, you probably want to use the Ruby bundled with ChefDK. For beginners, you can operate inside this environment by prepending ‘chef exec’ to any Ruby commands you may want to run. Eventually, you might want to install your own Ruby Gems. This can be a problem, if you do not have certain environmental variables set.

Here, I include a PowerShell script which sets or removes the environmental variables required to use ChefDK’s Ruby as your local environment’s Ruby. I make use of the local user’s %PATH% variable, which will always be appended to the system’s %PATH%. We also create a User environmental variable called CHEFDK_RUBY, which will be appended to the local user’s %PATH%. A few other Ruby-related variables are set based on recommendations set forth in the ChefDK on Windows Survival Guide.

# Filename: Set-ChefDK_Enviro_Vars.ps1
# Brian Dwyer - Intelligent Digital Services - 11/5/14

# ***USAGE***
# To Setup the ChefDK Ruby Variables
# ./Set-ChefDK_Enviro_Vars.ps1 set

# To Remove the ChefDK Ruby Variables
# ./Set-ChefDK_Enviro_Vars.ps1 unset

# System-Wide Environmental Variables
$System_Vars='HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'

# User-Specific Environmental Variables
$User_Vars='HKCU:\Environment'

# Make Sure we don't expand/evaluate environmental variables on the return
$DoNotExpand=[Microsoft.WIN32.RegistryValueOptions]::DoNotExpandEnvironmentNames

# Check if ChefDK is installed
If (!$env:Path.Contains('chefdk\bin'))
  {
    echo ""
    echo "/======| Error: ChefDK does not seem to be installed. |=====\"
    echo ""
    pause
    exit
  }

# Get ChefDK Installation Directory
$ChefDK_DIR = $env:Path.Split(';') -like "*chefdk\bin" | Out-String -Stream | Split-Path -Parent

# Determine ChefDK Ruby Version
$ChefDK_RubyVer = Get-ChildItem -Name $ChefDK_DIR\embedded\lib\ruby\gems

# Setup
If ( $args[0] -eq 'set' )
  {
  echo "/======| Setting up Registry Keys... |=====\"
  Set-ItemProperty $User_Vars -Name 'CHEFDK_RUBY' -Value "$env:USERPROFILE\.chefdk\gem\ruby\$ChefDK_RubyVer\bin;$ChefDK_DIR\embedded\bin"
  Set-ItemProperty $User_Vars -Name 'GEM_ROOT' -Value "$ChefDK_DIR\embedded\lib\ruby\gems\$ChefDK_RubyVer"
  Set-ItemProperty $User_Vars -Name 'GEM_HOME' -Value "$env:USERPROFILE\.chefdk\gem\ruby\$ChefDK_RubyVer"
  Set-ItemProperty $User_Vars -Name 'GEM_PATH' -Value "$env:USERPROFILE\.chefdk\gem\ruby\$ChefDK_RubyVer;$ChefDK_DIR\lib\ruby\gems\$ChefDK_RubyVer"
  If (!(Get-Item $User_Vars).GetValue('PATH','Default',$DoNotExpand).Contains('CHEFDK_RUBY'))
   {
   If (((Get-ItemProperty $User_Vars).PATH) -eq $null)
    {
    Set-ItemProperty $User_Vars -Name 'PATH' -Value '%CHEFDK_RUBY%'
    }
   Else
    {
    $NewVal=(Get-Item $User_Vars).GetValue('PATH','Default',$DoNotExpand) + ';%CHEFDK_RUBY%'
    Set-ItemProperty $User_Vars -Name 'PATH' -Value $NewVal
    }
   }
  pause
  exit
  }
Elseif ( $args[0] -eq 'unset' )
  {
  echo "/======| Removing Registry Keys... |=====\"
  Set-ItemProperty $User_Vars -Name 'PATH' -Value (Get-Item $User_Vars).GetValue('PATH','Default',$DoNotExpand).replace(';%CHEFDK_RUBY%', '')
  Set-ItemProperty $User_Vars -Name 'PATH' -Value (Get-Item $User_Vars).GetValue('PATH','Default',$DoNotExpand).replace('%CHEFDK_RUBY%', '')
  Remove-ItemProperty $User_Vars -Name 'CHEFDK_RUBY'
  Remove-ItemProperty $User_Vars -Name 'GEM_ROOT'
  Remove-ItemProperty $User_Vars -Name 'GEM_HOME'
  Remove-ItemProperty $User_Vars -Name 'GEM_PATH'
  pause
  exit
  }
 Else
  {
  echo '------------------------------------------------'
  echo "|*|  - Set ChefDK Environmental Variables -  |*|"
  echo '------------------------------------------------'
  echo "|   Use 'set' or 'unset' to control script     |"
  echo '------------------------------------------------'
  pause
  exit
  }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s