Class: Bolt::PuppetDBInventory::CLI
- Inherits:
-
Object
- Object
- Bolt::PuppetDBInventory::CLI
- Defined in:
- lib/bolt_ext/puppetdb_inventory.rb
Instance Method Summary collapse
- #build_parser ⇒ Object
-
#initialize(args) ⇒ CLI
constructor
A new instance of CLI.
- #resolve_group(group) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(args) ⇒ CLI
Returns a new instance of CLI.
13 14 15 16 17 18 19 |
# File 'lib/bolt_ext/puppetdb_inventory.rb', line 13 def initialize(args) Bolt::Logger.initialize_logging @logger = Logging.logger[self] @args = args @cli_opts = {} @parser = build_parser end |
Instance Method Details
#build_parser ⇒ Object
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 |
# File 'lib/bolt_ext/puppetdb_inventory.rb', line 21 def build_parser parser = OptionParser.new('') do |opts| opts.on('--cacert CACERT', "Path to the CA certificate") do |cacert| @cli_opts['cacert'] = cacert end opts.on('--cert CERT', "Path to the certificate") do |cert| @cli_opts['cert'] = cert end opts.on('--key KEY', "Path to the private key") do |key| @cli_opts['key'] = key end opts.on('--token-file TOKEN', "Path to the token file", "Default: #{Bolt::PuppetDB::Config::DEFAULT_TOKEN} if present") do |token| @cli_opts['token'] = token end opts.on('--url URL', "The URL of the PuppetDB server to connect to") do |url| @cli_opts['server_urls'] = [url] end opts.on('--config CONFIG', "The puppetdb.conf file to read configuration from", "Defaults: #{Bolt::PuppetDB::Config::DEFAULT_CONFIG[:user]} or", "#{Bolt::PuppetDB::Config::DEFAULT_CONFIG[:global]} if present") do |file| @config_file = File.(file) end opts.on('--output FILE', '-o FILE', "Where to write the generated inventory file, defaults to stdout") do |file| @output_file = file end opts.on('--trace', "Show stacktraces for exceptions") do |trace| @trace = trace end opts.on('-h', '--help', "Display help") do |_| @show_help = true end end parser. = <<-BANNER Usage: bolt-inventory-pdb <input-file> [--output <output-file>] [--url <url>] [auth-options] Populate the nodes in an inventory file based on PuppetDB queries. The input file should be a Bolt inventory file, where each 'nodes' entry is replaced with a 'query' entry to be executed against PuppetDB. The output will be the input file, with the 'nodes' entry for each group populated with the query results. BANNER parser end |
#resolve_group(group) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/bolt_ext/puppetdb_inventory.rb', line 118 def resolve_group(group) group['nodes'] = @puppetdb_client.query_certnames(group['query']) group.fetch('groups', []).each do |child| resolve_group(child) end group end |
#run ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/bolt_ext/puppetdb_inventory.rb', line 71 def run Bolt::Logger.configure({ "console" => {} }, true) msg = <<~MSG Deprecation Warning: Starting with Bolt 2.0, inventory file v1 and the bolt-inventory-pdb command will no longer be supported. Use a v2 inventory file instead with the PuppetDB plugin to lookup targets from PuppetDB. v1 inventory files can be automatically migrated to v2 using 'bolt project migrate'. Inventory files are modified in place and will not preserve formatting or comments. MSG @logger.warn(msg) positional_args = @parser.permute(@args) if @show_help puts @parser.help return end inventory_file = positional_args.shift unless inventory_file raise "Please specify an input file (see --help for details)" end if positional_args.any? raise "Unknown argument(s) #{positional_args.join(', ')}" end config = Bolt::PuppetDB::Config.load_config(@config_file, @cli_opts) @puppetdb_client = Bolt::PuppetDB::Client.new(config) unless File.readable?(inventory_file) raise "Can't read the inventory file #{inventory_file}" end inventory = YAML.load_file(inventory_file) resolve_group(inventory) result = inventory.to_yaml if @output_file File.write(@output_file, result) else puts result end end |