Custom type and provider

65 views
Skip to first unread message

Rafael Tomelin

unread,
Sep 21, 2018, 4:34:23 PM9/21/18
to Puppet Users
Hi guys,

I am creating a type in custom provider, but I am not able to understand the following question.

I created a basic type to understand the concept of things, as follows:
Puppet::Type.newtype(:mydir) do
    @doc = "First custom type."
    
    ensurable do
        defaultvalues
        defaultto :present
    end

    newparam(:name, :namevar => true) do
    end

    newparam(:install_location) do
    end
end


After creating the provider, but does not recognize the same and displays the following error:
Error: Could not find a suitable provider for mydir
Notice: Applied catalog in 0.63 seconds

Puppet::Type.type(:mydir).provide(:linux) do

    defaultfor :operatingsystem => :linux
    confine    :operatingsystem => :linux

    commands :mkdir => "mkdir"
    commands :rm => "rm"
    
    def exists?
        Puppet.info("checking if is already deployed")
        deploy_dir = "#{resource[:install_location]}/#{resource[:name]}"

        File.directory?(deploy_dir) and Dir.entries(deploy_dir).size > 2
    end

    def create
        Puppet.info("Deploying the appliction")
        deploy_dir = "#{resource[:install_location]}/#{resource[:name]}"

        mkdir('-p', deploy_dir)
    end

    def destroy
        Puppet.info("Removing the appliction")
        deploy_dir = "#{resource[:install_location]}/#{resource[:name]}"
        rm('-rf',deploy_dir)
    end
end

  What I did not envisage is how Puppet identifies the provider to be used in the OS?
--

Atenciosamente,

Rafael Tomelin

skype: rafael.tomelin

E-mail: rafael....@gmail.com

RHCE      - Red Hat Certified Engineer
PPT-205 - Puppet Certified Professional 2017
Zabbix    - ZABBIX Certified Specialist
LPI3 
ITIL v3

Nick Lewis

unread,
Sep 21, 2018, 4:52:23 PM9/21/18
to puppet...@googlegroups.com
It looks like the trouble here is that "linux" isn't a valid value for operatingsystem. If you look at some examples in puppet, you can see that common values of operatingsystem are like "fedora", "centos", "suse". The fact you want to use here is osfamily, which is "linux".
 
--

Atenciosamente,

Rafael Tomelin

skype: rafael.tomelin

E-mail: rafael....@gmail.com

RHCE      - Red Hat Certified Engineer
PPT-205 - Puppet Certified Professional 2017
Zabbix    - ZABBIX Certified Specialist
LPI3 
ITIL v3

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CAGEUqbCtgp2bo7CN8STqDxq5L4WZLCJXNT%2Buq7-qzpvVYaL3%2Bg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Rafael Tomelin

unread,
Sep 21, 2018, 4:57:45 PM9/21/18
to puppet...@googlegroups.com
Yes,

Esuqeci had already altered or removed the following lines:
     defaultfor: operatingsystem =>: linux
     confine: operatingsystem =>: linux

But it had no effect


For more options, visit https://groups.google.com/d/optout.

Martin Alfke

unread,
Sep 22, 2018, 2:36:00 AM9/22/18
to puppet...@googlegroups.com


> On 21. Sep 2018, at 22:57, Rafael Tomelin <rafael....@gmail.com> wrote:
>
> Yes,
>
> Esuqeci had already altered or removed the following lines:
> defaultfor: operatingsystem =>: linux
> confine: operatingsystem =>: linux

defaultfor: kernel => :Linux

operatingsystem is something like suse, debian, centos
osfamily is something like redhat or debian

hth,
Martin
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CAGEUqbACVDjowt3yYWzKG7GFzP%2BjZKmxrrr5jg4P4g01dfciFQ%40mail.gmail.com.

David Schmitt

unread,
Sep 22, 2018, 4:29:27 AM9/22/18
to puppet...@googlegroups.com
Hi Rafael,

if you are just starting out with this, I'd highly recommend looking into the Resource API. It makes the development of types much easier, is a supported part of this week's puppet 6 release, and available as a separate download for previous puppet versions. The PDK also has support for getting you started with a ready to go skeleton using 'pdk new provider'.


Cheers, David

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CAGEUqbCtgp2bo7CN8STqDxq5L4WZLCJXNT%2Buq7-qzpvVYaL3%2Bg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
--

Rafael Tomelin

unread,
Sep 22, 2018, 10:19:14 PM9/22/18
to puppet...@googlegroups.com
Hi David Schmitt,

I create de new module, class and provider with pdk the according this site puppet.  My puppet is version 'puppet --version . 5.5.6'.

But, the return error: Could not autoload puppet/type/foo: no such file to load -- puppet/resource_api 

My type is:

cat lib/puppet/type/foo.rb 

require 'puppet/resource_api'


Puppet::ResourceApi.register_type(

  name: 'foo',

  docs: <<-EOS,

      This type provides Puppet with the capabilities to manage ...

    EOS

  features: [],

  attributes:   {

    ensure:      {

      type:    'Enum[present, absent]',

      desc:    'Whether this resource should be present or absent on the target system.',

      default: 'present',

    },

    name:        {

      type:      'String',

      desc:      'The name of the resource you want to manage.',

      behaviour: :namevar,

    },

  },

)


My provider:

cat lib/puppet/provider/foo/foo.rb 

require 'puppet/resource_api/simple_provider'


# Implementation for the foo type using the Resource API.

class Puppet::Provider::Foo::Foo < Puppet::ResourceApi::SimpleProvider

  def get(_context)

    [

      {

        name: 'foo',

        ensure: 'present',

      },

      {

        name: 'bar',

        ensure: 'present',

      },

    ]

  end


  def create(context, name, should)

    context.notice("Creating '#{name}' with #{should.inspect}")

  end


  def update(context, name, should)

    context.notice("Updating '#{name}' with #{should.inspect}")

  end


  def delete(context, name)

    context.notice("Deleting '#{name}'")

  end

end



My init.pp

class first_type {

  foo{ 'my_type':

    ensure => present,

  }

}


What`s problem : Could not autoload puppet/type/foo: no such file to load -- puppet/resource_api 

The provider write the ruby, is possible create provider with python? If yes, is change ruby to python?




For more options, visit https://groups.google.com/d/optout.
--

David Schmitt

unread,
Sep 24, 2018, 3:41:02 AM9/24/18
to puppet...@googlegroups.com
Hi Rafael,

on puppet versions prior to puppet6, you need to install the puppet-resource_api gem to support the provider. You can use the puppetlabs-resource_api module for that.

Cheers, David


For more options, visit https://groups.google.com/d/optout.

Rafael Tomelin

unread,
Sep 25, 2018, 1:25:12 PM9/25/18
to puppet...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages