class Puppet::Provider::Exec

Public Instance Methods

environment() click to toggle source
   # File lib/puppet/provider/exec.rb
 8 def environment
 9   env = {}
10 
11   if (path = resource[:path])
12     env[:PATH] = path.join(File::PATH_SEPARATOR)
13   end
14 
15   return env unless (envlist = resource[:environment])
16 
17   envlist = [envlist] unless envlist.is_a? Array
18   envlist.each do |setting|
19     unless (match = /^(\w+)=((.|\n)*)$/.match(setting))
20       warning _("Cannot understand environment setting %{setting}") % { setting: setting.inspect }
21       next
22     end
23     var = match[1]
24     value = match[2]
25 
26     if env.include?(var) || env.include?(var.to_sym)
27       warning _("Overriding environment setting '%{var}' with '%{value}'") % { var: var, value: value }
28     end
29 
30     if value.nil? || value.empty?
31       msg = _("Empty environment setting '%{var}'") % {var: var}
32       Puppet.warn_once('undefined_variables', "empty_env_var_#{var}", msg, resource.file, resource.line)
33     end
34 
35     env[var] = value
36   end
37 
38   env
39 end
extractexe(command) click to toggle source
   # File lib/puppet/provider/exec.rb
87 def extractexe(command)
88   if command.is_a? Array
89     command.first
90   else
91     match = /^"([^"]+)"|^'([^']+)'/.match(command)
92     if match
93       # extract whichever of the two sides matched the content.
94       match[1] or match[2]
95     else
96       command.split(/ /)[0]
97     end
98   end
99 end
run(command, check = false) click to toggle source
   # File lib/puppet/provider/exec.rb
41 def run(command, check = false)
42   output = nil
43   sensitive = resource.parameters[:command].sensitive
44 
45   checkexe(command)
46 
47   debug "Executing#{check ? " check": ""} '#{sensitive ? '[redacted]' : command}'"
48 
49   # Ruby 2.1 and later interrupt execution in a way that bypasses error
50   # handling by default. Passing Timeout::Error causes an exception to be
51   # raised that can be rescued inside of the block by cleanup routines.
52   #
53   # This is backwards compatible all the way to Ruby 1.8.7.
54   Timeout::timeout(resource[:timeout], Timeout::Error) do
55     cwd = resource[:cwd]
56     # It's ok if cwd is nil. In that case Puppet::Util::Execution.execute() simply will not attempt to
57     # change the working directory, which is exactly the right behavior when no cwd parameter is
58     # expressed on the resource.  Moreover, attempting to change to the directory that is already
59     # the working directory can fail under some circumstances, so avoiding the directory change attempt
60     # is preferable to defaulting cwd to that directory.
61 
62     # note that we are passing "false" for the "override_locale" parameter, which ensures that the user's
63     # default/system locale will be respected.  Callers may override this behavior by setting locale-related
64     # environment variables (LANG, LC_ALL, etc.) in their 'environment' configuration.
65     output = Puppet::Util::Execution.execute(
66       command,
67       :failonfail => false,
68       :combine => true,
69       :cwd => cwd,
70       :uid => resource[:user], :gid => resource[:group],
71       :override_locale => false,
72       :custom_environment => environment(),
73       :sensitive => sensitive
74     )
75   end
76   # The shell returns 127 if the command is missing.
77   if output.exitstatus == 127
78     raise ArgumentError, output
79   end
80 
81   # Return output twice as processstatus was returned before, but only exitstatus was ever called.
82   # Output has the exitstatus on it so it is returned instead. This is here twice as changing this
83   #  would result in a change to the underlying API.
84   return output, output
85 end
validatecmd(command) click to toggle source
    # File lib/puppet/provider/exec.rb
101 def validatecmd(command)
102   exe = extractexe(command)
103   # if we're not fully qualified, require a path
104   self.fail _("'%{exe}' is not qualified and no path was specified. Please qualify the command or specify a path.") % { exe: exe } if !absolute_path?(exe) and resource[:path].nil?
105 end