Smoothing Troubles
Custom Types and Providers
Chad Thompson
PuppetCamp Chicago
August 18, 2014
Chad Thompson
“DevOps Engineer”
http://chadthompson.me
node default {
!
file{"/etc/config.conf:
ensure => "present",
owner => "chad.thompson",
content => "Key Value”,
}
!
}
From...
node default {
include custommod
}
class custommod {
file{“/etc/config.conf”:
ensure => "present",
owner => "chad.thompson...
node default {
custommod::config{“config”:
config_dir => “/etc/“,
}
define custommod::config(
$config_dir = “/etc/“
)
{
fi...
!
user{“chad.thompson”:
ensure => "present",
managehome => true,
}
Encapsulation
package{“nginx”:
ensure => installed,
}
!
file{“/etc/nginx/nginx.conf”:
ensure => "present",
content => template(“..”),
re...
# Copy SSH keys into place
file{"/home/devuser/.ssh":
ensure => "directory",
owner => 'dice',
}->
exec{"cp /opt/ssh-keys/*...
Difficult Things..
• Executing deployment scripts.
• Deployment scripts (“php artisan…”)
• Database migrations
• Third par...
A different way..
!
wpcli{“/usr/local/bin”:
ensure => present,
}
http://wp-cli.org
A Custom Type
!
# lib/puppet/type/wpcli.rb
Puppet::Type.newtype(:wpcli) do
@doc = "Install The WP-CLI Tool"
ensurable
!
# ...
A Provider
!
require 'mkmf'
!
Puppet::Type.type(:wpcli).provide :ruby do
desc "..."
!
# Required executables for the modul...
Some Background..
• The Ruby Language (http://ruby-lang.org)
• Libraries, Objects, Gem Files (oh, my!)
• There can be many...
.
!"" Modulefile
!"" README
!"" lib
#   $"" puppet
#   !"" provider
#   #   $"" wpcli
#   #   $"" ruby.rb
#   $"" type
#  ...
The Custom Type
# lib/puppet/type/wpcli.rb
Puppet::Type.newtype(:wpcli) do
@doc = "Install The WP-CLI Tool"
ensurable
!
# ...
The Provider
require 'mkmf'
!
Puppet::Type.type(:wpcli).provide :ruby do
desc "..."
!
def create
end
!
def destroy
end
!
#...
## This provider will create a binary called 'wp'
def exists?
executable = find_executable 'wp'
if executable.nil?
return ...
# Required executables for the module
commands :curl => 'curl'
commands :php => 'php'
!
@@wp_url = 'https://raw.githubuser...
Conclusion
Q: How do I mix scripting with Puppet?
!
A: With Custom Types/Providers!
THank you!
http://chadthompson.me
@cha...
Upcoming SlideShare
Loading in …5
×

Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers (Beginner)

711 views

Published on

"Smoothing Troubles With Custom Types and Providers" (Beginner) presented at Puppet Camp Chicago 2014 by Chad Thompson, Dice Holdings

Published in: Software
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
711
On SlideShare
0
From Embeds
0
Number of Embeds
45
Actions
Shares
0
Downloads
9
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers (Beginner)

  1. 1. Smoothing Troubles Custom Types and Providers Chad Thompson PuppetCamp Chicago August 18, 2014
  2. 2. Chad Thompson “DevOps Engineer” http://chadthompson.me
  3. 3. node default { ! file{"/etc/config.conf: ensure => "present", owner => "chad.thompson", content => "Key Value”, } ! } From puppet manifests…
  4. 4. node default { include custommod } class custommod { file{“/etc/config.conf”: ensure => "present", owner => "chad.thompson", content => "Key Value”, } } … to Modules
  5. 5. node default { custommod::config{“config”: config_dir => “/etc/“, } define custommod::config( $config_dir = “/etc/“ ) { file{“/etc/config.conf: […] } } … to Defined Types
  6. 6. ! user{“chad.thompson”: ensure => "present", managehome => true, } Encapsulation
  7. 7. package{“nginx”: ensure => installed, } ! file{“/etc/nginx/nginx.conf”: ensure => "present", content => template(“..”), require => Package[“nginx”], notify => Service[“nginx”], } ! service{“nginx”: ensure => running, require => Package[“nginx”], } Dependency Management
  8. 8. # Copy SSH keys into place file{"/home/devuser/.ssh": ensure => "directory", owner => 'dice', }-> exec{"cp /opt/ssh-keys/* /home/devuser/.ssh/": path => "/bin:/usr/bin", user => 'devuser', creates => '/home/devuser/.ssh/id_rsa.pub', }-> exec{"chown devuser:devuser /home/devuser/.ssh/*": path => "/bin:/usr/bin", user => "devuser", }-> exec{"chmod -R 0400 /home/devuser/.ssh/*": path => "/bin:/usr/bin", }-> Procedural Routines exec{} -> exec{} -> exec{}->
  9. 9. Difficult Things.. • Executing deployment scripts. • Deployment scripts (“php artisan…”) • Database migrations • Third party service integrations. (“Send a note to HipChat…”)
  10. 10. A different way.. ! wpcli{“/usr/local/bin”: ensure => present, } http://wp-cli.org
  11. 11. A Custom Type ! # lib/puppet/type/wpcli.rb Puppet::Type.newtype(:wpcli) do @doc = "Install The WP-CLI Tool" ensurable ! # A single parameter - # the path to install the wp-cli binary. newparam(:path, :namevar => true) do desc "The wp-cli install location." end end Defines the behavior of the ‘wpcli’ type.
  12. 12. A Provider ! require 'mkmf' ! Puppet::Type.type(:wpcli).provide :ruby do desc "..." ! # Required executables for the module commands :curl => 'curl' commands :php => 'php' … end Defines the Steps for Execution
  13. 13. Some Background.. • The Ruby Language (http://ruby-lang.org) • Libraries, Objects, Gem Files (oh, my!) • There can be many providers for a type. • Test for features / facts. • A convention for the ‘lib’ directory.
  14. 14. . !"" Modulefile !"" README !"" lib #   $"" puppet #   !"" provider #   #   $"" wpcli #   #   $"" ruby.rb #   $"" type #   $"" wpcli.rb !"" manifests #   $"" init.pp !"" spec #   $"" spec_helper.rb $"" tests !"" init.pp $"" wpcli.pp
  15. 15. The Custom Type # lib/puppet/type/wpcli.rb Puppet::Type.newtype(:wpcli) do @doc = "Install The WP-CLI Tool" ensurable ! # A single parameter - # the path to install the wp-cli binary. newparam(:path, :namevar => true) do desc "The wp-cli install location." end end
  16. 16. The Provider require 'mkmf' ! Puppet::Type.type(:wpcli).provide :ruby do desc "..." ! def create end ! def destroy end ! ## This provider will create a binary called 'wp' def exists? end ! end
  17. 17. ## This provider will create a binary called 'wp' def exists? executable = find_executable 'wp' if executable.nil? return false else return true end end
  18. 18. # Required executables for the module commands :curl => 'curl' commands :php => 'php' ! @@wp_url = 'https://raw.githubusercontent.com/wp-cli/ builds/gh-pages/phar/wp-cli.phar' ! def create # Using the puppet logger to output # information - sends information # to the puppet master debug("CREATE #{resource[:path]}") ! curl(['-o', "#{resource[:path]}/wp", @@wp_url]) File.chmod(0755, "#{resource[:path]}/wp") end
  19. 19. Conclusion Q: How do I mix scripting with Puppet? ! A: With Custom Types/Providers! THank you! http://chadthompson.me @chadothompson chad_thompson@mac.com

×