(#21738) Revert "(#2157) Remove support for executable external facts on Windows"
This reverts commit 008cc6cdf79df1589c060e2a088caffc9cf6da77.
Conflicts:
lib/facter/util/directory_loader.rb
lib/facter/util/parser.rb
Rob Reynolds
10 years ago
0 | 0 |
# A Facter plugin that loads external facts.
|
1 | 1 |
#
|
2 | 2 |
# Default Unix Directories:
|
3 | |
# /etc/facter/facts.d, /etc/puppetlbas/facter/facts.d
|
|
3 |
# /etc/facter/facts.d", "/etc/puppetlabs/facter/facts.d"
|
4 | 4 |
#
|
5 | 5 |
# Default Windows Direcotires:
|
6 | 6 |
# C:\ProgramData\Puppetlabs\facter\facts.d (2008)
|
124 | 124 |
end
|
125 | 125 |
|
126 | 126 |
register(ScriptParser) do |filename|
|
127 | |
if not Facter::Util::Config.is_windows?
|
128 | |
File.executable?(filename) && File.file?(filename)
|
|
127 |
if Facter::Util::Config.is_windows?
|
|
128 |
extension_matches?(filename, %w{bat com exe})
|
|
129 |
else
|
|
130 |
File.executable?(filename)
|
129 | 131 |
end
|
130 | 132 |
end
|
131 | 133 |
|
|
134 |
# Executes and parses the key value output of Powershell scripts
|
|
135 |
#
|
|
136 |
# Before you can run unsigned ps1 scripts it requires a change to execution
|
|
137 |
# policy:
|
|
138 |
#
|
|
139 |
# Set-ExecutionPolicy RemoteSigned -Scope LocalMachine
|
|
140 |
# Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
|
|
141 |
#
|
|
142 |
class PowershellParser < Base
|
|
143 |
# Returns a hash of facts from powershell output
|
|
144 |
def results
|
|
145 |
shell_command = "powershell -File #{filename}"
|
|
146 |
output = Facter::Util::Resolution.exec(shell_command)
|
132 | 147 |
|
|
148 |
result = {}
|
|
149 |
output.split("\n").each do |line|
|
|
150 |
if line =~ /^(.+)=(.+)$/
|
|
151 |
result[$1] = $2
|
|
152 |
end
|
|
153 |
end
|
|
154 |
|
|
155 |
result
|
|
156 |
rescue Exception => e
|
|
157 |
Facter.warn("Failed to handle #{filename} as powershell facts: #{e.class}: #{e}")
|
|
158 |
Facter.debug(e.backtrace.join("\n\t"))
|
|
159 |
end
|
|
160 |
end
|
|
161 |
|
|
162 |
register(PowershellParser) do |filename|
|
|
163 |
Facter::Util::Config.is_windows? && extension_matches?(filename, "ps1")
|
|
164 |
end
|
|
165 |
|
133 | 166 |
# A parser that is used when there is no other parser that can handle the file
|
134 | 167 |
# The return from results indicates to the caller the file was not parsed correctly.
|
135 | 168 |
class NothingParser
|