Puppet Provider usage for puppet resource

asked 2016-12-30 07:17:09 -0500

Eric_Anderson gravatar image

updated 2017-01-04 08:35:18 -0500

I am trying to develop a provider that will query the system for my custom resource myfile. However when I run
puppet resource myfile --environment tap --debug I get the following results:

Debug: Runtime environment: puppetversion=4.5.3, rubyversion=2.1.9, runmode=user, defaultencoding=UTF-8
Debug: Executing: '/bin/ls /folders'
Debug: #each property -> {:ensure=>:present, :name=>"/folders/otherfile", :owner=>"root"}
Debug: #each property -> {:ensure=>:present, :name=>"/folders/testf", :owner=>"root"}
myfile { '/folders/otherfile':
ensure => 'present',
}
myfile { '/folders/testf':
ensure => 'present',
}

This looks good except it is missing my owner property that I am also searching for. My provider is below:

Puppet::Type.type(:myfile).provide(:ruby) do
    commands :touch => 'touch', :ls => 'ls'

mk_resource_methods

def self.get_list_of_myfiles
  files = ls('/folders')
  files = files.gsub(/\s+/m, ' ').strip.split(" ")
  files
end

def self.get_properties(file)
properties = {}

begin
  output = `ls -l /folders/#{file}`
rescue Puppet::ExecutionFailure => e
  Puppet.debug "#get_properties had an error -> #{e.inspect}"
  return {}
end

output = output.gsub(/\s+/m, '::').strip.split("::")
#     0      1  2    3   4  5  6    7      8
#-rw-r--r-- 1 root root 0 Dec 30 07:28 /folders/otherfile

properties[:ensure] = :present
properties[:name] = output[8]
properties[:owner] = output[2]
properties
end

def self.instances
get_list_of_myfiles.collect do |each|
  properties = get_properties(each)
  Puppet.debug "#each property -> #{properties}"
  new(properties)
end
end
end

Why is my owner field missing?

edit retag flag offensive close merge delete

Comments

Use stat(1) for a scripted approach: /usr/bin/stat -c '%U' <filename>; For a programed approach use ruby's File class https://ruby-doc.org/core/File/Stat.html#method-i-uid The latter will be definitely more clean.

Kai Burghardt gravatar imageKai Burghardt ( 2017-01-02 18:14:17 -0500 )edit

I am not sure what you are telling me to do, neither of these is going to make my owner field be displayed when doing a puppet resource myfile. I need all of the properties of any of my custom resource types be displayed when someone does a puppet resource [myresource]

Eric_Anderson gravatar imageEric_Anderson ( 2017-01-04 06:39:39 -0500 )edit

Why are you defining self.get_list_of_myfiles twice?

Kai Burghardt gravatar imageKai Burghardt ( 2017-01-04 07:53:41 -0500 )edit

copy and paste mistake when posting this

Eric_Anderson gravatar imageEric_Anderson ( 2017-01-04 08:35:08 -0500 )edit