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

http://github.com/opscode/chef · Ruby · 152 lines · 105 code · 17 blank · 30 comment · 14 complexity · f61ca035138c634302bf0669f0c8e34c 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. require 'chef/provider'
  19. require 'chef/mixin/command'
  20. require 'chef/resource/env'
  21. class Chef
  22. class Provider
  23. class Env < Chef::Provider
  24. include Chef::Mixin::Command
  25. attr_accessor :key_exists
  26. def initialize(new_resource, run_context)
  27. super
  28. @key_exists = true
  29. end
  30. def load_current_resource
  31. @current_resource = Chef::Resource::Env.new(@new_resource.name)
  32. @current_resource.key_name(@new_resource.key_name)
  33. if env_key_exists(@new_resource.key_name)
  34. @current_resource.value(env_value(@new_resource.key_name))
  35. else
  36. @key_exists = false
  37. Chef::Log.debug("#{@new_resource} key does not exist")
  38. end
  39. @current_resource
  40. end
  41. def env_value(key_name)
  42. raise Chef::Exceptions::Env, "#{self.to_s} provider does not implement env_value!"
  43. end
  44. def env_key_exists(key_name)
  45. env_value(key_name) ? true : false
  46. end
  47. # Check to see if value needs any changes
  48. #
  49. # ==== Returns
  50. # <true>:: If a change is required
  51. # <false>:: If a change is not required
  52. def compare_value
  53. if @new_resource.delim
  54. #e.g. check for existing value within PATH
  55. not @current_resource.value.split(@new_resource.delim).any? do |val|
  56. val == @new_resource.value
  57. end
  58. else
  59. @new_resource.value != @current_resource.value
  60. end
  61. end
  62. def action_create
  63. if @key_exists
  64. if compare_value
  65. modify_env
  66. Chef::Log.info("#{@new_resource} altered")
  67. @new_resource.updated_by_last_action(true)
  68. end
  69. else
  70. create_env
  71. Chef::Log.info("#{@new_resource} created")
  72. @new_resource.updated_by_last_action(true)
  73. end
  74. end
  75. #e.g. delete a PATH element
  76. #
  77. # ==== Returns
  78. # <true>:: If we handled the element case and caller should not delete the key
  79. # <false>:: Caller should delete the key, either no :delim was specific or value was empty
  80. # after we removed the element.
  81. def delete_element
  82. return false unless @new_resource.delim #no delim: delete the key
  83. if compare_value
  84. Chef::Log.debug("#{@new_resource} element '#{@new_resource.value}' does not exist")
  85. return true #do not delete the key
  86. else
  87. new_value =
  88. @current_resource.value.split(@new_resource.delim).select { |item|
  89. item != @new_resource.value
  90. }.join(@new_resource.delim)
  91. if new_value.empty?
  92. return false #nothing left here, delete the key
  93. else
  94. old_value = @new_resource.value(new_value)
  95. create_env
  96. Chef::Log.debug("#{@new_resource} deleted #{old_value} element")
  97. @new_resource.updated_by_last_action(true)
  98. return true #we removed the element and updated; do not delete the key
  99. end
  100. end
  101. end
  102. def action_delete
  103. if @key_exists && !delete_element
  104. delete_env
  105. Chef::Log.info("#{@new_resource} deleted")
  106. @new_resource.updated_by_last_action(true)
  107. end
  108. end
  109. def action_modify
  110. if @key_exists
  111. if compare_value
  112. modify_env
  113. Chef::Log.info("#{@new_resource} modified")
  114. @new_resource.updated_by_last_action(true)
  115. end
  116. else
  117. raise Chef::Exceptions::Env, "Cannot modify #{@new_resource} - key does not exist!"
  118. end
  119. end
  120. def create_env
  121. raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :#{@new_resource.action}"
  122. end
  123. def delete_env
  124. raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :delete"
  125. end
  126. def modify_env
  127. if @new_resource.delim
  128. #e.g. add to PATH
  129. @new_resource.value << @new_resource.delim << @current_resource.value
  130. end
  131. create_env
  132. end
  133. end
  134. end
  135. end