Class: Chef::Provider::WindowsEnv

Inherits:
Chef::Provider show all
Includes:
Mixin::WindowsEnvHelper
Defined in:
lib/chef/provider/windows_env.rb

Constant Summary

Constants included from Mixin::WindowsEnvHelper

Mixin::WindowsEnvHelper::HWND_BROADCAST, Mixin::WindowsEnvHelper::SMTO_ABORTIFHUNG, Mixin::WindowsEnvHelper::SMTO_BLOCK, Mixin::WindowsEnvHelper::SMTO_NOTIMEOUTIFNOTHUNG, Mixin::WindowsEnvHelper::WM_SETTINGCHANGE

Constants included from ReservedNames::Win32::API::System

ReservedNames::Win32::API::System::PRODUCT_BUSINESS, ReservedNames::Win32::API::System::PRODUCT_BUSINESS_N, ReservedNames::Win32::API::System::PRODUCT_CLUSTER_SERVER, ReservedNames::Win32::API::System::PRODUCT_DATACENTER_SERVER, ReservedNames::Win32::API::System::PRODUCT_DATACENTER_SERVER_CORE, ReservedNames::Win32::API::System::PRODUCT_DATACENTER_SERVER_CORE_V, ReservedNames::Win32::API::System::PRODUCT_DATACENTER_SERVER_V, ReservedNames::Win32::API::System::PRODUCT_ENTERPRISE, ReservedNames::Win32::API::System::PRODUCT_ENTERPRISE_E, ReservedNames::Win32::API::System::PRODUCT_ENTERPRISE_N, ReservedNames::Win32::API::System::PRODUCT_ENTERPRISE_SERVER, ReservedNames::Win32::API::System::PRODUCT_ENTERPRISE_SERVER_CORE, ReservedNames::Win32::API::System::PRODUCT_ENTERPRISE_SERVER_CORE_V, ReservedNames::Win32::API::System::PRODUCT_ENTERPRISE_SERVER_IA64, ReservedNames::Win32::API::System::PRODUCT_ENTERPRISE_SERVER_V, ReservedNames::Win32::API::System::PRODUCT_HOME_BASIC, ReservedNames::Win32::API::System::PRODUCT_HOME_BASIC_E, ReservedNames::Win32::API::System::PRODUCT_HOME_BASIC_N, ReservedNames::Win32::API::System::PRODUCT_HOME_PREMIUM, ReservedNames::Win32::API::System::PRODUCT_HOME_PREMIUM_E, ReservedNames::Win32::API::System::PRODUCT_HOME_PREMIUM_N, ReservedNames::Win32::API::System::PRODUCT_HOME_PREMIUM_SERVER, ReservedNames::Win32::API::System::PRODUCT_HOME_SERVER, ReservedNames::Win32::API::System::PRODUCT_HYPERV, ReservedNames::Win32::API::System::PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT, ReservedNames::Win32::API::System::PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING, ReservedNames::Win32::API::System::PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY, ReservedNames::Win32::API::System::PRODUCT_PROFESSIONAL, ReservedNames::Win32::API::System::PRODUCT_PROFESSIONAL_E, ReservedNames::Win32::API::System::PRODUCT_PROFESSIONAL_N, ReservedNames::Win32::API::System::PRODUCT_SB_SOLUTION_SERVER, ReservedNames::Win32::API::System::PRODUCT_SERVER_FOR_SMALLBUSINESS, ReservedNames::Win32::API::System::PRODUCT_SERVER_FOR_SMALLBUSINESS_V, ReservedNames::Win32::API::System::PRODUCT_SERVER_FOUNDATION, ReservedNames::Win32::API::System::PRODUCT_SMALLBUSINESS_SERVER, ReservedNames::Win32::API::System::PRODUCT_SOLUTION_EMBEDDEDSERVER, ReservedNames::Win32::API::System::PRODUCT_STANDARD_SERVER, ReservedNames::Win32::API::System::PRODUCT_STANDARD_SERVER_CORE, ReservedNames::Win32::API::System::PRODUCT_STANDARD_SERVER_CORE_V, ReservedNames::Win32::API::System::PRODUCT_STANDARD_SERVER_V, ReservedNames::Win32::API::System::PRODUCT_STARTER, ReservedNames::Win32::API::System::PRODUCT_STARTER_E, ReservedNames::Win32::API::System::PRODUCT_STARTER_N, ReservedNames::Win32::API::System::PRODUCT_STORAGE_ENTERPRISE_SERVER, ReservedNames::Win32::API::System::PRODUCT_STORAGE_EXPRESS_SERVER, ReservedNames::Win32::API::System::PRODUCT_STORAGE_STANDARD_SERVER, ReservedNames::Win32::API::System::PRODUCT_STORAGE_WORKGROUP_SERVER, ReservedNames::Win32::API::System::PRODUCT_ULTIMATE, ReservedNames::Win32::API::System::PRODUCT_ULTIMATE_E, ReservedNames::Win32::API::System::PRODUCT_ULTIMATE_N, ReservedNames::Win32::API::System::PRODUCT_UNDEFINED, ReservedNames::Win32::API::System::PRODUCT_WEB_SERVER, ReservedNames::Win32::API::System::PRODUCT_WEB_SERVER_CORE, ReservedNames::Win32::API::System::SM_SERVERR2, ReservedNames::Win32::API::System::VER_NT_DOMAIN_CONTROLLER, ReservedNames::Win32::API::System::VER_NT_SERVER, ReservedNames::Win32::API::System::VER_NT_WORKSTATION, ReservedNames::Win32::API::System::VER_SUITE_BACKOFFICE, ReservedNames::Win32::API::System::VER_SUITE_BLADE, ReservedNames::Win32::API::System::VER_SUITE_COMPUTE_SERVER, ReservedNames::Win32::API::System::VER_SUITE_DATACENTER, ReservedNames::Win32::API::System::VER_SUITE_EMBEDDEDNT, ReservedNames::Win32::API::System::VER_SUITE_ENTERPRISE, ReservedNames::Win32::API::System::VER_SUITE_PERSONAL, ReservedNames::Win32::API::System::VER_SUITE_SINGLEUSERTS, ReservedNames::Win32::API::System::VER_SUITE_SMALLBUSINESS, ReservedNames::Win32::API::System::VER_SUITE_SMALLBUSINESS_RESTRICTED, ReservedNames::Win32::API::System::VER_SUITE_STORAGE_SERVER, ReservedNames::Win32::API::System::VER_SUITE_TERMINAL, ReservedNames::Win32::API::System::VER_SUITE_WH_SERVER

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#action, #after_resource, #current_resource, #logger, #new_resource, #recipe_name, #run_context

Instance Method Summary collapse

Methods included from Mixin::WindowsEnvHelper

#broadcast_env_change, #expand_path

Methods included from Mixin::WideString

#utf8_to_wide, #wide_to_utf8, #wstring

Methods inherited from Chef::Provider

action, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #cookbook_name, #define_resource_requirements, #description, #events, include_resource_dsl?, include_resource_dsl_module, #introduced, #load_after_resource, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use, use_inline_resources, #validate_required_properties!, #whyrun_mode?

Methods included from Mixin::LazyModuleInclude

#descendants, #include, #included

Methods included from Mixin::PowershellOut

#powershell_out, #powershell_out!

Methods included from Mixin::WindowsArchitectureHelper

#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory

Methods included from Mixin::PowershellExec

#powershell_exec, #powershell_exec!

Methods included from DSL::Powershell

#ps_credential

Methods included from DSL::RegistryHelper

#registry_data_exists?, #registry_get_subkeys, #registry_get_values, #registry_has_subkeys?, #registry_key_exists?, #registry_value_exists?

Methods included from DSL::ChefVault

#chef_vault, #chef_vault_item, #chef_vault_item_for_environment

Methods included from DSL::DataQuery

#data_bag, #data_bag_item, #search, #tagged?

Methods included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Methods included from DSL::PlatformIntrospection

#older_than_win_2012_or_8?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Methods included from Mixin::Provides

#provided_as, #provides, #provides?

Methods included from Mixin::DescendantsTracker

#descendants, descendants, #direct_descendants, direct_descendants, #find_descendants_by_name, find_descendants_by_name, #inherited, store_inherited

Methods included from DSL::Recipe

#exec, #have_resource_class_for?, #resource_class_for

Methods included from DSL::Definitions

add_definition, #evaluate_resource_definition, #has_resource_definition?

Methods included from DSL::Resources

add_resource_dsl, remove_resource_dsl

Methods included from DSL::Cheffish

load_cheffish

Methods included from DSL::RebootPending

#reboot_pending?

Methods included from DSL::IncludeRecipe

#include_recipe, #load_recipe

Methods included from Mixin::NotifyingBlock

#notifying_block, #subcontext_block

Methods included from DSL::DeclareResource

#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #resources, #with_run_context

Constructor Details

#initialize(new_resource, run_context) ⇒ WindowsEnv

Returns a new instance of WindowsEnv.


36
37
38
39
# File 'lib/chef/provider/windows_env.rb', line 36

def initialize(new_resource, run_context)
  super
  @key_exists = true
end

Instance Attribute Details

#key_existsObject

Returns the value of attribute key_exists


27
28
29
# File 'lib/chef/provider/windows_env.rb', line 27

def key_exists
  @key_exists
end

Instance Method Details

#create_envObject


146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/chef/provider/windows_env.rb', line 146

def create_env
  obj = env_obj(@new_resource.key_name)
  unless obj
    obj = WIN32OLE.connect("winmgmts://").get("Win32_Environment").spawninstance_
    obj.name = @new_resource.key_name
    obj.username = new_resource.user
  end
  obj.variablevalue = @new_resource.value
  obj.put_
  value = @new_resource.value
  value = expand_path(value) if @new_resource.key_name.casecmp("PATH") == 0
  ENV[@new_resource.key_name] = value
  broadcast_env_change
end

#current_valuesObject

Returns the current values to split by delimiter


180
181
182
# File 'lib/chef/provider/windows_env.rb', line 180

def current_values
  @current_values ||= current_resource.value.split(new_resource.delim)
end

#delete_elementObject

e.g. delete a PATH element

==== Returns :: If we handled the element case and caller should not delete the key :: Caller should delete the key, either no :delim was specific or value was empty after we removed the element.


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/chef/provider/windows_env.rb', line 101

def delete_element
  return false unless new_resource.delim # no delim: delete the key

  needs_delete = new_values.any? { |v| current_values.include?(v) }
  if !needs_delete
    logger.trace("#{new_resource} element '#{new_resource.value}' does not exist")
    true # do not delete the key
  else
    new_value =
      current_values.select do |item|
        not new_values.include?(item)
      end.join(new_resource.delim)

    if new_value.empty?
      false # nothing left here, delete the key
    else
      old_value = new_resource.value(new_value)
      create_env
      logger.trace("#{new_resource} deleted #{old_value} element")
      new_resource.updated_by_last_action(true)
      true # we removed the element and updated; do not delete the key
    end
  end
end

#delete_envObject


161
162
163
164
165
166
167
168
169
170
# File 'lib/chef/provider/windows_env.rb', line 161

def delete_env
  obj = env_obj(@new_resource.key_name)
  if obj
    obj.delete_
    broadcast_env_change
  end
  if ENV[@new_resource.key_name]
    ENV.delete(@new_resource.key_name)
  end
end

#env_key_exists(key_name) ⇒ Object


55
56
57
# File 'lib/chef/provider/windows_env.rb', line 55

def env_key_exists(key_name)
  env_value(key_name) ? true : false
end

#env_obj(key_name) ⇒ Object


194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/chef/provider/windows_env.rb', line 194

def env_obj(key_name)
  return @env_obj if @env_obj

  wmi = WmiLite::Wmi.new
  # Note that by design this query is case insensitive with regard to key_name
  environment_variables = wmi.query("select * from Win32_Environment where name = '#{key_name}'")
  if environment_variables && environment_variables.length > 0
    environment_variables.each do |env|
      @env_obj = env.wmi_ole_object
      return @env_obj if @env_obj.username.split('\\').last.casecmp(new_resource.user) == 0
    end
  end
  @env_obj = nil
end

#env_value(key_name) ⇒ Object


189
190
191
192
# File 'lib/chef/provider/windows_env.rb', line 189

def env_value(key_name)
  obj = env_obj(key_name)
  obj.variablevalue if obj
end

#load_current_resourceObject


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chef/provider/windows_env.rb', line 41

def load_current_resource
  @current_resource = Chef::Resource::WindowsEnv.new(new_resource.name)
  current_resource.key_name(new_resource.key_name)

  if env_key_exists(new_resource.key_name)
    current_resource.value(env_value(new_resource.key_name))
  else
    @key_exists = false
    logger.trace("#{new_resource} key does not exist")
  end

  current_resource
end

#modify_envObject


172
173
174
175
176
177
# File 'lib/chef/provider/windows_env.rb', line 172

def modify_env
  if new_resource.delim
    new_resource.value((new_values + current_values).uniq.join(new_resource.delim))
  end
  create_env
end

#new_valuesObject

Returns the new values to split by delimiter


185
186
187
# File 'lib/chef/provider/windows_env.rb', line 185

def new_values
  @new_values ||= new_resource.value.split(new_resource.delim)
end

#requires_modify_or_create?Boolean Also known as: compare_value

Check to see if value needs any changes

==== Returns :: If a change is required :: If a change is not required

Returns:

  • (Boolean)

64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/chef/provider/windows_env.rb', line 64

def requires_modify_or_create?
  if new_resource.delim
    # e.g. check for existing value within PATH
    new_values.inject(0) do |index, val|
      next_index = current_values.find_index val
      return true if next_index.nil? || next_index < index

      next_index
    end
    false
  else
    new_resource.value != current_resource.value
  end
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)

32
33
34
# File 'lib/chef/provider/windows_env.rb', line 32

def whyrun_supported?
  false
end