Method: Puppet::Provider::Exec#run

Defined in:
lib/puppet/provider/exec.rb

#run(command, check = false) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/puppet/provider/exec.rb', line 7

def run(command, check = false)
  output = nil
  status = nil
  dir = nil
  sensitive = resource.parameters[:command].sensitive

  checkexe(command)

  if dir = resource[:cwd]
    unless File.directory?(dir)
      if check
        dir = nil
      else
        self.fail _("Working directory '%{dir}' does not exist") % { dir: dir }
      end
    end
  end

  dir ||= Dir.pwd

  debug "Executing#{check ? " check": ""} '#{sensitive ? '[redacted]' : command}'"
  begin
    # Do our chdir
    Dir.chdir(dir) do
      environment = {}

      environment[:PATH] = resource[:path].join(File::PATH_SEPARATOR) if resource[:path]

      if envlist = resource[:environment]
        envlist = [envlist] unless envlist.is_a? Array
        envlist.each do |setting|
          if setting =~ /^(\w+)=((.|\n)+)$/
            env_name = $1
            value = $2
            if environment.include?(env_name) || environment.include?(env_name.to_sym)
              warning _("Overriding environment setting '%{env_name}' with '%{value}'") % { env_name: env_name, value: value }
            end
            environment[env_name] = value
          else
            warning _("Cannot understand environment setting %{setting}") % { setting: setting.inspect }
          end
        end
      end

      # Ruby 2.1 and later interrupt execution in a way that bypasses error
      # handling by default. Passing Timeout::Error causes an exception to be
      # raised that can be rescued inside of the block by cleanup routines.
      #
      # This is backwards compatible all the way to Ruby 1.8.7.
      Timeout::timeout(resource[:timeout], Timeout::Error) do
        # note that we are passing "false" for the "override_locale" parameter, which ensures that the user's
        # default/system locale will be respected.  Callers may override this behavior by setting locale-related
        # environment variables (LANG, LC_ALL, etc.) in their 'environment' configuration.
        output = Puppet::Util::Execution.execute(command, :failonfail => false, :combine => true,
                                :uid => resource[:user], :gid => resource[:group],
                                :override_locale => false,
                                :custom_environment => environment,
                                :sensitive => sensitive)
      end
      # The shell returns 127 if the command is missing.
      if output.exitstatus == 127
        raise ArgumentError, output
      end

    end
  rescue Errno::ENOENT => detail
    self.fail Puppet::Error, detail.to_s, detail
  end

  # Return output twice as processstatus was returned before, but only exitstatus was ever called.
  # Output has the exitstatus on it so it is returned instead. This is here twice as changing this
  #  would result in a change to the underlying API.
  return output, output
end