From: David Schmitt Date: Tue, 25 Jul 2017 15:00:30 +0000 (+0100) Subject: make the apt_key2 type work read-only X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=9e78bef853f75a08b2919567af50223698612ae2;p=puppet-modules%2Fpuppetlabs-apt.git make the apt_key2 type work read-only --- diff --git a/lib/puppet/provider/apt_key/apt_key2.rb b/lib/puppet/provider/apt_key2/apt_key2.rb similarity index 94% rename from lib/puppet/provider/apt_key/apt_key2.rb rename to lib/puppet/provider/apt_key2/apt_key2.rb index 9811e5c..6f7a26c 100644 --- a/lib/puppet/provider/apt_key/apt_key2.rb +++ b/lib/puppet/provider/apt_key2/apt_key2.rb @@ -28,19 +28,23 @@ register_provider('apt_key2') do def get(names = []) cli_args = %w(adv --list-keys --with-colons --fingerprint --fixed-list-mode) - key_output = apt_key(cli_args).encode('UTF-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '') + key_output = apt_key_lines(*cli_args) pub_line = nil fpr_line = nil - result = key_output.split("\n").collect do |line| + result = key_output.collect do |line| + line = line.encode('UTF-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '') if line.start_with?('pub') pub_line = line elsif line.start_with?('fpr') fpr_line = line end + # puts "debug: parsing #{line}; fpr: #{fpr_line.inspect}; pub: #{pub_line.inspect}" next unless (pub_line and fpr_line) + # puts "debug: key_line_to_hash" + hash = key_line_to_hash(pub_line, fpr_line) # reset scanning diff --git a/lib/puppet_x/apt_key/resource_api.rb b/lib/puppet_x/apt_key/resource_api.rb index 8aa737e..990d5aa 100644 --- a/lib/puppet_x/apt_key/resource_api.rb +++ b/lib/puppet_x/apt_key/resource_api.rb @@ -38,9 +38,7 @@ module Puppet::SimpleResource end def to_manifest - [ - "apt_key { #{values[:name].inspect}: ", - ] + values.keys.select { |k| k != :name }.collect { |k| " #{k} => #{values[k].inspect}," } + ['}'] + (["apt_key { #{values[:name].inspect}: "] + values.keys.select { |k| k != :name }.collect { |k| " #{k} => #{values[k].inspect}," } + ['}']).join("\n") end end end @@ -83,7 +81,7 @@ def register_type(definition) end if options[:namevar] - # puts 'setting namevar' + puts 'setting namevar' isnamevar has_namevar = true namevar_name = name @@ -166,15 +164,17 @@ def register_type(definition) end end - def self.instances + define_singleton_method(:instances) do puts 'instances' # klass = Puppet::Type.type(:api) + # force autoloading of the provider + autoloaded_provider = provider(name) get.collect do |resource_hash| Puppet::SimpleResource::TypeShim.new(resource_hash[namevar_name], resource_hash) end end - def retrieve + define_method(:retrieve) do puts 'retrieve' result = Puppet::Resource.new(self.class, title) current_state = self.class.get.find { |h| h[namevar_name] == title } @@ -203,7 +203,29 @@ def register_type(definition) end def self.commands(*args) - puts "registering command: #{args.inspect}" + args.each do |command_group| + command_group.each do |command_name, command| + puts "registering command: #{command_name}, using #{command}" + define_singleton_method(command_name) do |*args| + puts "spawn([#{command.to_s}, #{command.to_s}], #{args.inspect})" + # TODO: capture output to debug stream + p = Process.spawn([command, command], *args) + Process.wait(p) + unless $?.exitstatus == 0 + raise Puppet::ResourceError, "#{command} failed with exit code #{$?.exitstatus}" + end + end + + define_singleton_method("#{command_name}_lines") do |*args| + puts "capture3([#{command.to_s}, #{command.to_s}], #{args.inspect})" + stdin_str, stderr_str, status = Open3.capture3([command, command], *args) + unless status.exitstatus == 0 + raise Puppet::ResourceError, "#{command} failed with exit code #{$?.exitstatus}" + end + stdin_str.split("\n") + end + end + end end end end @@ -211,4 +233,5 @@ end def register_provider(typename, &block) type = Puppet::Type.type(typename.to_sym) type.instance_eval &block + # require'pry';binding.pry end