class Puppet::Provider::Exec

Public Instance Methods

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