Friday, August 15, 2014

Challenges setting up a Chef Pagefile Cookbook

I figured this would be simple...I did it is Altiris without an issue. I have done it with reg files easily. How hard could it be? Well it was tricky. I wanted to leverage all the cool information that I got from ohai. So I started out like this using a Windows Server 2012R2 VM to grab the ohai results for the various settings.

Abandoned
default.rb

case node['kernel']['cs_info']['total_physical_memory']
when "2147012608"
  include_recipe "pagefile::2gb"
when "4294496256"
  include_recipe "pagefile::4gb"
when "6441979904"
  include_recipe "pagefile::6gb"
when "6437851136"
  include_recipe "pagefile::6gb"
when "8585334784"
  include_recipe "pagefile::8gb"
when "17171070976"
  include_recipe "pagefile::16gb"
end

Then I had corresponding jobs for - 2gb, 4gb, 6gb, 8gb, & 16gb

2gb.rb
windows_pagefile 'c:\pagefile.sys' do
      initial_size (3072)
      maximum_size (3072)
      system_managed false
      automatic_managed false
      action :set
      not_if {reboot_pending?}
    end

Everything was going great. That was until I decided to set a VM that was on Windows Server 2008 R2. The recipe never ran.. So I proceeded to run ohai >c:\w2k8r28gb.txt. Sure enough, even though both VMs had the same 8GB installed, the total_physical_memory was different. So I proceeded to create additional when statements calling the same include_recipe. Ok so everything was running great on my VMs on CloudStack/XenServer.

I decided to test on vSphere and vCloud, guess what, all different...I couldn't believe it..

So I had to come up a completely different way. I tried making variables with ranges to no avail.

I ended up abandoning using ohai and the case in favor of some POSH and if, elsif, else.

Thanks to my buddy, Ron Oberjohann (ron@advdat.com) for help with the POSH. I was use 'Get-WMIObject -class Win32_PhysicalMemory', but wasn't doing the math.

I am seeing if I can still get the ohai portion to work by trying to round that info...time to revisit my Ruby course from last year :)

But for now here is what works:

def memory
  powershell_script "$ram" do
    code <<-eoh p="">    $computer = Get-WMIObject -class "Win32_ComputerSystem"
    $ram = [math]::Round($computer.TotalPhysicalMemory/1024/1024/1024/0)
    EOH
  end
end

if memory == 2
    windows_pagefile 'c:\pagefile.sys' do
      initial_size (3072)
      maximum_size (3072)
      system_managed false
      automatic_managed false
      action :set
      not_if {reboot_pending?}
    end
elsif memory == 4
    windows_pagefile 'c:\pagefile.sys' do
      initial_size (6144)
      maximum_size (6144)
      system_managed false
      automatic_managed false
      action :set
      not_if {reboot_pending?}
    end
elsif memory == 6
    windows_pagefile 'c:\pagefile.sys' do
      initial_size (9216)
      maximum_size (9216)
      system_managed false
      automatic_managed false
      action :set
      not_if {reboot_pending?}
    end
elsif memory == 8
    windows_pagefile 'C:\pagefile.sys' do
      initial_size  (8193)
      maximum_size  (8193)
      system_managed false
      automatic_managed false
      action :set
      not_if {reboot_pending?}
    end
else memory = 16
    windows_pagefile 'c:\pagefile.sys' do
      initial_size (16385)
      maximum_size (16385)
      system_managed false
      automatic_managed false
      action :set
      not_if {reboot_pending?}
    end
  Chef::Log.error("Can't determine memory and set pagefile")
end

The code is on my github. I thought I'd share my findings.

No comments:

Post a Comment