]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/commitdiff
make the apt_key2 type work read-only
authorDavid Schmitt <david.schmitt@puppet.com>
Tue, 25 Jul 2017 15:00:30 +0000 (16:00 +0100)
committerDavid Schmitt <david.schmitt@puppet.com>
Mon, 11 Sep 2017 09:53:34 +0000 (10:53 +0100)
lib/puppet/provider/apt_key2/apt_key2.rb [moved from lib/puppet/provider/apt_key/apt_key2.rb with 94% similarity]
lib/puppet_x/apt_key/resource_api.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 9811e5c5d3eb6d06e23edea307261d565c31e0dc..6f7a26c39ce8249f1acc2b0f3b33ebf919d4a257 100644 (file)
@@ -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
index 8aa737e384d7d5ee4121641c835798a672ea15f2..990d5aabe51a9ac4000c37e26359369edc1aa7f6 100644 (file)
@@ -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