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

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #not_if, #only_if, #output_of_command, #run_command, #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, build_from_file, #cookbook_name, #node, #resource_collection

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #search, #value_for_platform

Constructor Details

#initialize(new_resource, run_context) ⇒ Env

Returns a new instance of Env.



29
30
31
32
# File 'lib/chef/provider/env.rb', line 29

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

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



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chef/provider/env.rb', line 72

def action_create
  if @key_exists
    if compare_value
      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



115
116
117
118
119
120
121
# File 'lib/chef/provider/env.rb', line 115

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



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

def action_modify
  if @key_exists
    if compare_value
      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

#compare_valueObject

Check to see if value needs any changes

Returns

<true>

If a change is required

<false>

If a change is not required



61
62
63
64
65
66
67
68
69
70
# File 'lib/chef/provider/env.rb', line 61

def compare_value
  if @new_resource.delim
    #e.g. check for existing value within PATH
    not @current_resource.value.split(@new_resource.delim).any? do |val|
      val == @new_resource.value
    end
  else
    @new_resource.value != @current_resource.value
  end
end

#create_envObject



135
136
137
# File 'lib/chef/provider/env.rb', line 135

def create_env
  raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :#{@new_resource.action}"
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.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/chef/provider/env.rb', line 92

def delete_element
  return false unless @new_resource.delim #no delim: delete the key
  if compare_value
    Chef::Log.debug("#{@new_resource} element '#{@new_resource.value}' does not exist")
    return true #do not delete the key
  else
    new_value =
      @current_resource.value.split(@new_resource.delim).select { |item|
      item != @new_resource.value
    }.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



139
140
141
# File 'lib/chef/provider/env.rb', line 139

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

#env_key_exists(key_name) ⇒ Object



52
53
54
# File 'lib/chef/provider/env.rb', line 52

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

#env_value(key_name) ⇒ Object



48
49
50
# File 'lib/chef/provider/env.rb', line 48

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

#load_current_resourceObject



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

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



143
144
145
146
147
148
149
# File 'lib/chef/provider/env.rb', line 143

def modify_env
  if @new_resource.delim
    #e.g. add to PATH
    @new_resource.value << @new_resource.delim << @current_resource.value
  end
  create_env
end