Class: Chef::Provider::Script

Inherits:
Execute show all
Extended by:
Forwardable
Defined in:
lib/chef/provider/script.rb

Direct Known Subclasses

WindowsScript

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 inherited from Execute

#define_resource_requirements, #timeout, #whyrun_supported?

Methods inherited from Chef::Provider

#action_nothing, #cleanup_after_converge, #converge_by, #define_resource_requirements, #events, #node, node_map, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, #whyrun_mode?, #whyrun_supported?

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 Mixin::ShellOut

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

Constructor Details

#initialize(new_resource, run_context) ⇒ Script

Returns a new instance of Script.



39
40
41
42
# File 'lib/chef/provider/script.rb', line 39

def initialize(new_resource, run_context)
  super
  self.code = new_resource.code
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



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

def code
  @code
end

Instance Method Details

#action_runObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/chef/provider/script.rb', line 56

def action_run
  script_file.puts(code)
  script_file.close

  set_owner_and_group

  super

  unlink_script_file
end

#commandObject



44
45
46
# File 'lib/chef/provider/script.rb', line 44

def command
  "\"#{interpreter}\" #{flags} \"#{script_file.path}\""
end

#load_current_resourceObject



48
49
50
51
52
53
54
# File 'lib/chef/provider/script.rb', line 48

def load_current_resource
  super
  # @todo Chef-13: change this to an exception
  if code.nil?
    Chef::Log.warn "#{@new_resource}: No code attribute was given, resource does nothing, this behavior is deprecated and will be removed in Chef-13"
  end
end

#script_fileObject



74
75
76
# File 'lib/chef/provider/script.rb', line 74

def script_file
  @script_file ||= Tempfile.open("chef-script")
end

#set_owner_and_groupObject



67
68
69
70
71
72
# File 'lib/chef/provider/script.rb', line 67

def set_owner_and_group
  # FileUtils itself implements a no-op if +user+ or +group+ are nil
  # You can prove this by running FileUtils.chown(nil,nil,'/tmp/file')
  # as an unprivileged user.
  FileUtils.chown(new_resource.user, new_resource.group, script_file.path)
end


78
79
80
# File 'lib/chef/provider/script.rb', line 78

def unlink_script_file
  script_file && script_file.close!
end