-
Notifications
You must be signed in to change notification settings - Fork 222
/
Copy pathquery_resources.rb
executable file
·74 lines (60 loc) · 2.41 KB
/
query_resources.rb
1
2
3
4
5
6
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
#! /opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true
require 'json'
require 'puppet'
require 'puppet/module_tool/tar'
require 'tempfile'
args = JSON.parse(STDIN.read)
RESOURCE_INSTANCE = /^([^\[]+)\[([^\]]+)\]$/.freeze
def instance(type_name, resource_name)
resource = Puppet::Resource.indirection.find("#{type_name}/#{resource_name}")
stringify_resource(resource)
end
Dir.mktmpdir do |puppet_root|
# Create temporary directories for all core Puppet settings so we don't clobber
# existing state or read from puppet.conf. Also create a temporary modulepath.
moduledir = File.join(puppet_root, 'modules')
Dir.mkdir(moduledir)
cli = Puppet::Settings::REQUIRED_APP_SETTINGS.flat_map do |setting|
["--#{setting}", File.join(puppet_root, setting.to_s.chomp('dir'))]
end
cli << '--modulepath' << moduledir
Puppet.initialize_settings(cli)
Tempfile.open('plugins.tar.gz') do |plugins|
File.binwrite(plugins, Base64.decode64(args['plugins']))
Puppet::ModuleTool::Tar.instance.unpack(plugins, moduledir, Etc.getlogin || Etc.getpwuid.name)
end
env = Puppet.lookup(:environments).get('production')
env.each_plugin_directory do |dir|
$LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
end
if (conn_info = args['_target'])
unless (type = conn_info['remote-transport'])
puts "Cannot discover resources for a remote target without knowing it's the remote-transport type."
exit 1
end
begin
require 'puppet/resource_api/transport'
rescue LoadError
msg = "Could not load 'puppet/resource_api/transport', puppet-resource_api "\
"gem version 1.8.0 or greater is required on the proxy target"
puts msg
exit 1
end
# Transport.connect will modify this hash!
transport_conn_info = conn_info.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
transport = Puppet::ResourceApi::Transport.connect(type, transport_conn_info)
Puppet::ResourceApi::Transport.inject_device(type, transport)
Puppet[:facts_terminus] = :network_device
Puppet[:certname] = conn_info['uri']
end
resources = args['resources'].flat_map do |resource_desc|
if (match = RESOURCE_INSTANCE.match(resource_desc))
Puppet::Resource.indirection.find("#{match[1]}/#{match[2]}", environment: env)
else
Puppet::Resource.indirection.search(resource_desc, environment: env)
end
end
puts({ 'resources' => resources }.to_json)
end
exit 0