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

Wildfly Java Application Server – Chef Cookbook

A while back, I wrote a cookbook to deploy the Wildfly Java Application Server. Wildfly is the successor of JBoss AS. This Chef cookbook handles the entire deployment process, and also contains LWRP’s to set system attributes, datasources, and deploy code. If you are looking to automate the installation and configuration of Wildfly, I am confident that this Chef cookbook will be a big help. Check it out on my GitHub. Feel free to contribute as well!

Hello world!

I have created this site to share my ideas, encounters, and solutions to technology problems.  I have a lot of information and guides which I’ve created and stored in my Microsoft OneNote notebook throughout the years. Some of it is only pertinent to me, but as I encounter challenges while performing various tasks, I document my solutions. I have found that this documentation can save a lot of time when repeating the task again.

In the field of Information Technology, no one can memorize everything; possessing high-quality, readily available information is crucial to success. I hope that some of my content will prove valuable to others.