/chef/lib/chef/provider/env/windows.rb

http://github.com/opscode/chef · Ruby · 75 lines · 49 code · 8 blank · 18 comment · 2 complexity · 5653e605230af824803ebb4a02c7fffd MD5 · raw file

  1. #
  2. # Author:: Doug MacEachern (<dougm@vmware.com>)
  3. # Copyright:: Copyright (c) 2010 VMware, Inc.
  4. # License:: Apache License, Version 2.0
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. if RUBY_PLATFORM =~ /mswin|mingw32|windows/
  19. require 'ruby-wmi'
  20. require 'Win32API'
  21. end
  22. class Chef
  23. class Provider
  24. class Env
  25. class Windows < Chef::Provider::Env
  26. def create_env
  27. obj = env_obj(@new_resource.key_name)
  28. unless obj
  29. obj = WIN32OLE.connect("winmgmts://").get("Win32_Environment").spawninstance_
  30. obj.name = @new_resource.key_name
  31. obj.username = "<System>"
  32. end
  33. obj.variablevalue = @new_resource.value
  34. obj.put_
  35. broadcast_env_change
  36. end
  37. def delete_env
  38. obj = env_obj(@new_resource.key_name)
  39. if obj
  40. obj.delete_
  41. broadcast_env_change
  42. end
  43. end
  44. def env_value(key_name)
  45. obj = env_obj(key_name)
  46. return obj ? obj.variablevalue : nil
  47. end
  48. def env_obj(key_name)
  49. WMI::Win32_Environment.find(:first,
  50. :conditions => { :name => key_name })
  51. end
  52. #see: http://msdn.microsoft.com/en-us/library/ms682653%28VS.85%29.aspx
  53. HWND_BROADCAST = 0xffff
  54. WM_SETTINGCHANGE = 0x001A
  55. SMTO_BLOCK = 0x0001
  56. SMTO_ABORTIFHUNG = 0x0002
  57. SMTO_NOTIMEOUTIFNOTHUNG = 0x0008
  58. def broadcast_env_change
  59. result = 0
  60. flags = SMTO_BLOCK | SMTO_ABORTIFHUNG | SMTO_NOTIMEOUTIFNOTHUNG
  61. @send_message ||= Win32API.new('user32', 'SendMessageTimeout', 'LLLPLLP', 'L')
  62. @send_message.call(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 'Environment', flags, 5000, result)
  63. end
  64. end
  65. end
  66. end
  67. end