SlideShare a Scribd company logo
1 of 17
Download to read offline
Replacing "exec" with a
  type and provider
  Return manifests to a declarative
           configuration



           Dominic Cleal <dcleal@redhat.com>
Puppet's declarative DSL

user { 'dcleal':
  ensure => present,
  comment => 'Dominic Cleal',
  shell   => '/bin/bash',
}
package { 'vim':
  ensure => 'present',
}
How execs can fail
$ puppet module install puppetlabs-apt
$ vim apt/manifests/key.pp
...
exec { "apt::key ${upkey} absent":
  command   => "apt-key del '${upkey}'",
  path      => '/bin:/usr/bin',
  onlyif    => "apt-key list | grep '${upkey}'",
  user      => 'root',
  group     => 'root',
  logoutput => 'on_failure',
}
Convert the exec to a type/provider
        'user' type
ensure
name
comment
                             'useradd'
shell
home                          provider
                      exists?, create, destroy
                      comment, comment=
                                                 useradd <user>
                      shell, shell=
                      home, home=
                                                 usermod <user>


                                                 userdel <user>
Types: properties and parameters
● Properties are changeable, e.g. a user's
  shell or a service's start-at-boot flag
● Parameters represent other required data, e.
  g. hasrestart on service
● All data can be validated and munged
● Types can be "ensurable", if the object can:
  ○ exist and not exist
  ○ be created
  ○ be destroyed
Convert the exec to a type/provider
 'apt_key' type
ensure
key               'keyring' provider
key_server        exists?
                  create                   apt-key list
                  destroy
                                       apt-key --recv-keys..


                                           apt-key del
Types: simple example
$ cat apt_key/lib/puppet/type/apt_key.rb
Puppet::Type.newtype(:apt_key) do
  @doc = "Manages apt keys"

  ensurable

  newparam(:key) do
    desc "The key ID"
    isnamevar
  end

  newparam(:key_server) do
    desc "Key server to download key form"
    defaultto "pgp.mit.edu"
  end
end
Types: known values
Puppet::Type.type(:file).newparam(:checksum) do
  desc "The checksum type to use when
determining whether to replace a file's conten
ts.
  The default checksum type is md5."

  newvalues "md5", "md5lite", "mtime", "ctime",
"none"

  defaultto :md5
end
Types: validation
module Puppet
  newtype(:schedule) do
    newparam(:repeat) do
      desc "How often a given resource may be applied
in this schedule's `period`. Defaults to 1; must be an
integer."

      validate do |value|
        unless value.is_a?(Integer) or value =~
/^d+$/
          raise Puppet::Error,
            "Repeat must be a number"
        end
      end
Providers: getters/setters, ensurable
● getters and setters are implemented for each
  property
● ensurable types also have exists?, create
  and destroy to manage its existence
● list of commands that are required to run
● confined to certain operating systems via
  facts
Providers: simple example
$ cat apt_key/lib/puppet/provider/apt_key/keyring.rb
Puppet::Type.type(:apt_key).provide(:keyring) do
  commands :aptkey => "/usr/bin/apt-key"

  def exists?
    aptkey("list").include? resource[:key].upcase
  end

  def create
    aptkey "adv", "--keyserver", resource[:key_server], "--recv-
keys", resource[:key].upcase
  end

  def destroy
    aptkey "del", resource[:key].upcase
  end
end
Providers: confinement
Puppet::Type.type(:group).provide :aix do
  desc "Group management for AIX."

 confine :operatingsystem => :aix
 defaultfor :operatingsystem => :aix



Puppet::Type.type(:exec).provide :posix do
  confine :feature => :posix
  defaultfor :feature => :posix
Providers: instances and ralsh
$ puppet resource host
host { 'argon':
  ensure     => 'present',
  host_aliases => ['foo'],
  ip         => '192.168.0.10',
  target     => '/etc/hosts',
}

host { 'iridium':
  ensure     => 'present',
  host_aliases => ['localhost.localdomain', 'localhost'],
  ip         => '127.0.0.1',
  target     => '/etc/hosts',
}
Providers: instances and ralsh
def self.instances
  resources = []
  aptkey("list").each_line { |k| resources << new({:
name => $1}) if k =~ /^pubs+w+/(w+)/ }
  resources
end

# puppet resource apt_key
apt_key { '1F41B907':
   ensure => 'present'
}
apt_key { '46925553':
   ensure => 'present'
Testing providers with rspec
prov_c = Puppet::Type.type(:apt_key).provider(:keyring)
describe prov_c do
  it "should remove key" do
    resource = Puppet::Type.type(:apt_key).new(
      :name => '16BA136C',
      :ensure => :absent
    )
    provider = prov_c.new(resource)
    provider.expects(:aptkey).with('del', '16BA136C')
    provider.destroy
  end
end
Creating a module
$ puppet module generate domcleal-apt_key
$ cd domcleal-apt_key
$ mkdir -p lib/puppet/type 
     lib/puppet/provider/apt_key
$ touch lib/puppet/type/apt_key.rb 
     lib/puppet/provider/apy_key/keyring.rb
$ puppet module build .

upload pkg/domcleal-apt_key-0.0.1.tar.gz
Resources
● docs.puppetlabs.com guides
   ○ Writing custom types & providers
   ○ Provider development
● Puppet Types and Providers
   ○ Dan Bode & Nan Liu, O'Reilly, 2012
● puppet source itself, spec/unit/provider/
● puppetlabs_spec_helper

More Related Content

What's hot

What's hot (20)

Perl: Hate it for the Right Reasons
Perl: Hate it for the Right ReasonsPerl: Hate it for the Right Reasons
Perl: Hate it for the Right Reasons
 
Oliver hookins puppetcamp2011
Oliver hookins puppetcamp2011Oliver hookins puppetcamp2011
Oliver hookins puppetcamp2011
 
Anatomy of a reusable module
Anatomy of a reusable moduleAnatomy of a reusable module
Anatomy of a reusable module
 
Puppet: What _not_ to do
Puppet: What _not_ to doPuppet: What _not_ to do
Puppet: What _not_ to do
 
BSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationBSDM with BASH: Command Interpolation
BSDM with BASH: Command Interpolation
 
Puppet modules for Fun and Profit
Puppet modules for Fun and ProfitPuppet modules for Fun and Profit
Puppet modules for Fun and Profit
 
Puppi. Puppet strings to the shell
Puppi. Puppet strings to the shellPuppi. Puppet strings to the shell
Puppi. Puppet strings to the shell
 
Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.
 
More tips n tricks
More tips n tricksMore tips n tricks
More tips n tricks
 
BASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic InterpolationBASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic Interpolation
 
Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.
 
Keeping objects healthy with Object::Exercise.
Keeping objects healthy with Object::Exercise.Keeping objects healthy with Object::Exercise.
Keeping objects healthy with Object::Exercise.
 
Follow the White Rabbit - Message Queues with PHP
Follow the White Rabbit - Message Queues with PHPFollow the White Rabbit - Message Queues with PHP
Follow the White Rabbit - Message Queues with PHP
 
Scaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
Scaling Symfony2 apps with RabbitMQ - Symfony UK MeetupScaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
Scaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
 
Tatsumaki
TatsumakiTatsumaki
Tatsumaki
 
Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018
 
Test-Driven Puppet Development - PuppetConf 2014
Test-Driven Puppet Development - PuppetConf 2014Test-Driven Puppet Development - PuppetConf 2014
Test-Driven Puppet Development - PuppetConf 2014
 
Puppet for Sys Admins
Puppet for Sys AdminsPuppet for Sys Admins
Puppet for Sys Admins
 
Get your teeth into Plack
Get your teeth into PlackGet your teeth into Plack
Get your teeth into Plack
 
Php on the Web and Desktop
Php on the Web and DesktopPhp on the Web and Desktop
Php on the Web and Desktop
 

Similar to Replacing "exec" with a type and provider: Return manifests to a declarative configuration

From Dev to DevOps - Apache Barcamp Spain 2011
From Dev to DevOps - Apache Barcamp Spain 2011From Dev to DevOps - Apache Barcamp Spain 2011
From Dev to DevOps - Apache Barcamp Spain 2011
Carlos Sanchez
 
Puppet for Java developers - JavaZone NO 2012
Puppet for Java developers - JavaZone NO 2012Puppet for Java developers - JavaZone NO 2012
Puppet for Java developers - JavaZone NO 2012
Carlos Sanchez
 
From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012
Carlos Sanchez
 
PM : code faster
PM : code fasterPM : code faster
PM : code faster
PHPPRO
 
From Dev to DevOps - ApacheCON NA 2011
From Dev to DevOps - ApacheCON NA 2011From Dev to DevOps - ApacheCON NA 2011
From Dev to DevOps - ApacheCON NA 2011
Carlos Sanchez
 

Similar to Replacing "exec" with a type and provider: Return manifests to a declarative configuration (20)

Learning Puppet basic thing
Learning Puppet basic thing Learning Puppet basic thing
Learning Puppet basic thing
 
Puppet
PuppetPuppet
Puppet
 
Writing and Publishing Puppet Modules - PuppetConf 2014
Writing and Publishing Puppet Modules - PuppetConf 2014Writing and Publishing Puppet Modules - PuppetConf 2014
Writing and Publishing Puppet Modules - PuppetConf 2014
 
Stanford Hackathon - Puppet Modules
Stanford Hackathon - Puppet ModulesStanford Hackathon - Puppet Modules
Stanford Hackathon - Puppet Modules
 
From Dev to DevOps - Apache Barcamp Spain 2011
From Dev to DevOps - Apache Barcamp Spain 2011From Dev to DevOps - Apache Barcamp Spain 2011
From Dev to DevOps - Apache Barcamp Spain 2011
 
Puppet for Java developers - JavaZone NO 2012
Puppet for Java developers - JavaZone NO 2012Puppet for Java developers - JavaZone NO 2012
Puppet for Java developers - JavaZone NO 2012
 
Puppet fundamentals
Puppet fundamentalsPuppet fundamentals
Puppet fundamentals
 
Integrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteIntegrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suite
 
Puppet: Eclipsecon ALM 2013
Puppet: Eclipsecon ALM 2013Puppet: Eclipsecon ALM 2013
Puppet: Eclipsecon ALM 2013
 
From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012
 
Absolute Beginners Guide to Puppet Through Types - PuppetConf 2014
Absolute Beginners Guide to Puppet Through Types - PuppetConf 2014Absolute Beginners Guide to Puppet Through Types - PuppetConf 2014
Absolute Beginners Guide to Puppet Through Types - PuppetConf 2014
 
Getting Started with PL/Proxy
Getting Started with PL/ProxyGetting Started with PL/Proxy
Getting Started with PL/Proxy
 
Ansible
AnsibleAnsible
Ansible
 
PM : code faster
PM : code fasterPM : code faster
PM : code faster
 
From Dev to DevOps - ApacheCON NA 2011
From Dev to DevOps - ApacheCON NA 2011From Dev to DevOps - ApacheCON NA 2011
From Dev to DevOps - ApacheCON NA 2011
 
A tour of Ansible
A tour of AnsibleA tour of Ansible
A tour of Ansible
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Does your configuration code smell?
Does your configuration code smell?Does your configuration code smell?
Does your configuration code smell?
 
Perl basics for pentesters part 2
Perl basics for pentesters part 2Perl basics for pentesters part 2
Perl basics for pentesters part 2
 
Puppet and the HashiStack
Puppet and the HashiStackPuppet and the HashiStack
Puppet and the HashiStack
 

More from Puppet

Puppet camp2021 testing modules and controlrepo
Puppet camp2021 testing modules and controlrepoPuppet camp2021 testing modules and controlrepo
Puppet camp2021 testing modules and controlrepo
Puppet
 
2021 04-15 operational verification (with notes)
2021 04-15 operational verification (with notes)2021 04-15 operational verification (with notes)
2021 04-15 operational verification (with notes)
Puppet
 
Enforce compliance policy with model-driven automation
Enforce compliance policy with model-driven automationEnforce compliance policy with model-driven automation
Enforce compliance policy with model-driven automation
Puppet
 

More from Puppet (20)

Puppet camp2021 testing modules and controlrepo
Puppet camp2021 testing modules and controlrepoPuppet camp2021 testing modules and controlrepo
Puppet camp2021 testing modules and controlrepo
 
Puppetcamp r10kyaml
Puppetcamp r10kyamlPuppetcamp r10kyaml
Puppetcamp r10kyaml
 
2021 04-15 operational verification (with notes)
2021 04-15 operational verification (with notes)2021 04-15 operational verification (with notes)
2021 04-15 operational verification (with notes)
 
Puppet camp vscode
Puppet camp vscodePuppet camp vscode
Puppet camp vscode
 
Modules of the twenties
Modules of the twentiesModules of the twenties
Modules of the twenties
 
Applying Roles and Profiles method to compliance code
Applying Roles and Profiles method to compliance codeApplying Roles and Profiles method to compliance code
Applying Roles and Profiles method to compliance code
 
KGI compliance as-code approach
KGI compliance as-code approachKGI compliance as-code approach
KGI compliance as-code approach
 
Enforce compliance policy with model-driven automation
Enforce compliance policy with model-driven automationEnforce compliance policy with model-driven automation
Enforce compliance policy with model-driven automation
 
Keynote: Puppet camp compliance
Keynote: Puppet camp complianceKeynote: Puppet camp compliance
Keynote: Puppet camp compliance
 
Automating it management with Puppet + ServiceNow
Automating it management with Puppet + ServiceNowAutomating it management with Puppet + ServiceNow
Automating it management with Puppet + ServiceNow
 
Puppet: The best way to harden Windows
Puppet: The best way to harden WindowsPuppet: The best way to harden Windows
Puppet: The best way to harden Windows
 
Simplified Patch Management with Puppet - Oct. 2020
Simplified Patch Management with Puppet - Oct. 2020Simplified Patch Management with Puppet - Oct. 2020
Simplified Patch Management with Puppet - Oct. 2020
 
Accelerating azure adoption with puppet
Accelerating azure adoption with puppetAccelerating azure adoption with puppet
Accelerating azure adoption with puppet
 
Puppet catalog Diff; Raphael Pinson
Puppet catalog Diff; Raphael PinsonPuppet catalog Diff; Raphael Pinson
Puppet catalog Diff; Raphael Pinson
 
ServiceNow and Puppet- better together, Kevin Reeuwijk
ServiceNow and Puppet- better together, Kevin ReeuwijkServiceNow and Puppet- better together, Kevin Reeuwijk
ServiceNow and Puppet- better together, Kevin Reeuwijk
 
Take control of your dev ops dumping ground
Take control of your  dev ops dumping groundTake control of your  dev ops dumping ground
Take control of your dev ops dumping ground
 
100% Puppet Cloud Deployment of Legacy Software
100% Puppet Cloud Deployment of Legacy Software100% Puppet Cloud Deployment of Legacy Software
100% Puppet Cloud Deployment of Legacy Software
 
Puppet User Group
Puppet User GroupPuppet User Group
Puppet User Group
 
Continuous Compliance and DevSecOps
Continuous Compliance and DevSecOpsContinuous Compliance and DevSecOps
Continuous Compliance and DevSecOps
 
The Dynamic Duo of Puppet and Vault tame SSL Certificates, Nick Maludy
The Dynamic Duo of Puppet and Vault tame SSL Certificates, Nick MaludyThe Dynamic Duo of Puppet and Vault tame SSL Certificates, Nick Maludy
The Dynamic Duo of Puppet and Vault tame SSL Certificates, Nick Maludy
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

Replacing "exec" with a type and provider: Return manifests to a declarative configuration

  • 1. Replacing "exec" with a type and provider Return manifests to a declarative configuration Dominic Cleal <dcleal@redhat.com>
  • 2. Puppet's declarative DSL user { 'dcleal': ensure => present, comment => 'Dominic Cleal', shell => '/bin/bash', } package { 'vim': ensure => 'present', }
  • 3. How execs can fail $ puppet module install puppetlabs-apt $ vim apt/manifests/key.pp ... exec { "apt::key ${upkey} absent": command => "apt-key del '${upkey}'", path => '/bin:/usr/bin', onlyif => "apt-key list | grep '${upkey}'", user => 'root', group => 'root', logoutput => 'on_failure', }
  • 4. Convert the exec to a type/provider 'user' type ensure name comment 'useradd' shell home provider exists?, create, destroy comment, comment= useradd <user> shell, shell= home, home= usermod <user> userdel <user>
  • 5. Types: properties and parameters ● Properties are changeable, e.g. a user's shell or a service's start-at-boot flag ● Parameters represent other required data, e. g. hasrestart on service ● All data can be validated and munged ● Types can be "ensurable", if the object can: ○ exist and not exist ○ be created ○ be destroyed
  • 6. Convert the exec to a type/provider 'apt_key' type ensure key 'keyring' provider key_server exists? create apt-key list destroy apt-key --recv-keys.. apt-key del
  • 7. Types: simple example $ cat apt_key/lib/puppet/type/apt_key.rb Puppet::Type.newtype(:apt_key) do @doc = "Manages apt keys" ensurable newparam(:key) do desc "The key ID" isnamevar end newparam(:key_server) do desc "Key server to download key form" defaultto "pgp.mit.edu" end end
  • 8. Types: known values Puppet::Type.type(:file).newparam(:checksum) do desc "The checksum type to use when determining whether to replace a file's conten ts. The default checksum type is md5." newvalues "md5", "md5lite", "mtime", "ctime", "none" defaultto :md5 end
  • 9. Types: validation module Puppet newtype(:schedule) do newparam(:repeat) do desc "How often a given resource may be applied in this schedule's `period`. Defaults to 1; must be an integer." validate do |value| unless value.is_a?(Integer) or value =~ /^d+$/ raise Puppet::Error, "Repeat must be a number" end end
  • 10. Providers: getters/setters, ensurable ● getters and setters are implemented for each property ● ensurable types also have exists?, create and destroy to manage its existence ● list of commands that are required to run ● confined to certain operating systems via facts
  • 11. Providers: simple example $ cat apt_key/lib/puppet/provider/apt_key/keyring.rb Puppet::Type.type(:apt_key).provide(:keyring) do commands :aptkey => "/usr/bin/apt-key" def exists? aptkey("list").include? resource[:key].upcase end def create aptkey "adv", "--keyserver", resource[:key_server], "--recv- keys", resource[:key].upcase end def destroy aptkey "del", resource[:key].upcase end end
  • 12. Providers: confinement Puppet::Type.type(:group).provide :aix do desc "Group management for AIX." confine :operatingsystem => :aix defaultfor :operatingsystem => :aix Puppet::Type.type(:exec).provide :posix do confine :feature => :posix defaultfor :feature => :posix
  • 13. Providers: instances and ralsh $ puppet resource host host { 'argon': ensure => 'present', host_aliases => ['foo'], ip => '192.168.0.10', target => '/etc/hosts', } host { 'iridium': ensure => 'present', host_aliases => ['localhost.localdomain', 'localhost'], ip => '127.0.0.1', target => '/etc/hosts', }
  • 14. Providers: instances and ralsh def self.instances resources = [] aptkey("list").each_line { |k| resources << new({: name => $1}) if k =~ /^pubs+w+/(w+)/ } resources end # puppet resource apt_key apt_key { '1F41B907': ensure => 'present' } apt_key { '46925553': ensure => 'present'
  • 15. Testing providers with rspec prov_c = Puppet::Type.type(:apt_key).provider(:keyring) describe prov_c do it "should remove key" do resource = Puppet::Type.type(:apt_key).new( :name => '16BA136C', :ensure => :absent ) provider = prov_c.new(resource) provider.expects(:aptkey).with('del', '16BA136C') provider.destroy end end
  • 16. Creating a module $ puppet module generate domcleal-apt_key $ cd domcleal-apt_key $ mkdir -p lib/puppet/type lib/puppet/provider/apt_key $ touch lib/puppet/type/apt_key.rb lib/puppet/provider/apy_key/keyring.rb $ puppet module build . upload pkg/domcleal-apt_key-0.0.1.tar.gz
  • 17. Resources ● docs.puppetlabs.com guides ○ Writing custom types & providers ○ Provider development ● Puppet Types and Providers ○ Dan Bode & Nan Liu, O'Reilly, 2012 ● puppet source itself, spec/unit/provider/ ● puppetlabs_spec_helper