Class: Chef::Provider::Env

Inherits:
Chef::Provider show all
Includes:
Mixin::Command
Defined in:
lib/chef/provider/env.rb,
lib/chef/provider/env/windows.rb

Direct Known Subclasses

Windows

Defined Under Namespace

Classes: Windows

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#action, #cookbook_name, #current_resource, #new_resource, #recipe_name, #run_context

Instance Method Summary collapse

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_and_return_stdout_stderr, #run_command_with_systems_locale

Methods included from Mixin::Command::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, #check_resource_semantics!, #cleanup_after_converge, #converge_by, #converge_if_changed, #define_resource_requirements, #events, include_resource_dsl, include_resource_dsl_module, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use_inline_resources, #whyrun_mode?, #whyrun_supported?

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 DeprecatedLWRPClass

#const_missing, #deprecated_constants, #register_deprecated_lwrp_class

Methods included from Mixin::LazyModuleInclude

#descendants, #include, #included

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!, #with_run_context

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

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 DSL::PlatformIntrospection

#docker?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Constructor Details

#initialize(new_resource, run_context) ⇒ Env

Returns a new instance of Env.



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

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/env.rb', line 27

def key_exists
  @key_exists
end

Instance Method Details

#action_createObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chef/provider/env.rb', line 79

def action_create
  if @key_exists
    if requires_modify_or_create?
      modify_env
      Chef::Log.info("#{@new_resource} altered")
      @new_resource.updated_by_last_action(true)
    end
  else
    create_env
    Chef::Log.info("#{@new_resource} created")
    @new_resource.updated_by_last_action(true)
  end
end

#action_deleteObject



123
124
125
126
127
128
129
# File 'lib/chef/provider/env.rb', line 123

def action_delete
  if @key_exists && !delete_element
    delete_env
    Chef::Log.info("#{@new_resource} deleted")
    @new_resource.updated_by_last_action(true)
  end
end

#action_modifyObject



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef/provider/env.rb', line 131

def action_modify
  if @key_exists
    if requires_modify_or_create?
      modify_env
      Chef::Log.info("#{@new_resource} modified")
      @new_resource.updated_by_last_action(true)
    end
  else
    raise Chef::Exceptions::Env, "Cannot modify #{@new_resource} - key does not exist!"
  end
end

#create_envObject



143
144
145
# File 'lib/chef/provider/env.rb', line 143

def create_env
  raise Chef::Exceptions::UnsupportedAction, "#{self} does not support :#{@new_resource.action}"
end

#current_valuesObject

Returns the current values to split by delimiter



159
160
161
# File 'lib/chef/provider/env.rb', line 159

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

#delete_elementObject

e.g. delete a PATH element

Returns

<true>

If we handled the element case and caller should not delete the key

<false>

Caller should delete the key, either no :delim was specific or value was empty after we removed the element.



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

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
    Chef::Log.debug("#{@new_resource} element '#{@new_resource.value}' does not exist")
    return 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?
      return false #nothing left here, delete the key
    else
      old_value = @new_resource.value(new_value)
      create_env
      Chef::Log.debug("#{@new_resource} deleted #{old_value} element")
      @new_resource.updated_by_last_action(true)
      return true #we removed the element and updated; do not delete the key
    end
  end
end

#delete_envObject



147
148
149
# File 'lib/chef/provider/env.rb', line 147

def delete_env
  raise Chef::Exceptions::UnsupportedAction, "#{self} does not support :delete"
end

#env_key_exists(key_name) ⇒ Object



54
55
56
# File 'lib/chef/provider/env.rb', line 54

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

#env_value(key_name) ⇒ Object



50
51
52
# File 'lib/chef/provider/env.rb', line 50

def env_value(key_name)
  raise Chef::Exceptions::Env, "#{self} provider does not implement env_value!"
end

#load_current_resourceObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef/provider/env.rb', line 36

def load_current_resource
  @current_resource = Chef::Resource::Env.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
    Chef::Log.debug("#{@new_resource} key does not exist")
  end

  @current_resource
end

#modify_envObject



151
152
153
154
155
156
# File 'lib/chef/provider/env.rb', line 151

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



164
165
166
# File 'lib/chef/provider/env.rb', line 164

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

<true>

If a change is required

<false>

If a change is not required

Returns:

  • (Boolean)


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

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