puppet camp dc 2014: puppet type and provider execution presentation

31
Puppet Type and Provider Execution Puppet Type and Provider Execution Trevor Vaughan - License: Onyx Point, Inc. Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) 0

Upload: puppet-labs

Post on 10-May-2015

637 views

Category:

Software


0 download

DESCRIPTION

Puppet Camp DC 2014: "Puppet Type and Provider Execution Presentation" Trevor Vaughn, OnyxPoint, Inc.

TRANSCRIPT

Page 1: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Puppet Type and Provider Execution

Trevor Vaughan - License:

Onyx Point, Inc.Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)

0

Page 2: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

What We're Doing Here

Follow Along at Home!Introduction to Types and ProvidersBasic Type Code WalkthroughBasic Provider Code WalkthroughType Execution OrderProvider Execution Order

Page 3: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Follow Along at Home!

Snag a copy of the walkthrough code athttp://git.io/_Bjamg

git clone [email protected]:onyxpoint/onyxpoint.com.git git checkout type-and-provider-execution-walkthrough

Page 4: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

What are Types? - The Basics

PropertiesSomething that can be measured and changed on thesystem

ParametersChange how Puppet manages resources through thePropertiesThink of them as variables

ValidationAnything that you need to do to validate yourresource

AutorequiresProvides the ability to require other catalog resourcesif they happen to be definedDoes not require resources to be defined!

Page 5: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

What are Types? - Advanced

InitializerProvides the ability to manipulate the resource mid-compile

FinishAnything that you want to do to your resource at theend of the compilation

Post-Resource EvaluationMagic! Allows for a resource cleanup (Puppet > 3.4.0only)

Page 6: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

What are Providers?

InitializerA place to do all sorts of fun things that persist acrossthe provider

Property MethodsAutomatically referenced methods for manipulatingproperties on the system

MiscellaneousAny other method that is useful to your type

Page 7: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

(Star Trek: The Original Series. Season 2, Episode 7. (Oct 27, 1967))

Page 8: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider ExecutionType Scaffold

module Puppet newtype(:ex_order) do def initialize(args) super(args) end

def finish(args) super end

newparam(:name) do desc "Everybody needs a name!" isnamevar end

newparam(:foo) do desc "Foo!" end

newproperty(:baz) do desc "Property Baz" end

newparam(:bar) do desc "Parameter Bar" end

validate do true end

autorequire(:file) do ['/tmp/foo'] end end

end

Page 9: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Provider ScaffoldPuppet::Type.type(:ex_order).provide(:ruby) do @@ex_order_classvars = { :example => true }

def initialize(*args) super(*args) end

def baz # Get the state of the 'baz' property from the system end

def baz=(should) # Set the system to the value of the 'baz' property in Puppet end

def flush # If *any* property is not in sync, this will be called endend

Page 10: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Sample Execution Manifestex_order { 'test': foo => 'foo', bar => 'bar', baz => 'baz'}

Page 11: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Full Execution WalkthroughYou can find all code and output at http://git.io/ZPuZNQ

Page 12: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Type Properties and Parameters

Output

newparam(:name) do isnamevar end

newparam(:foo) do Puppet.warning("Param :foo -> Starting")end

newproperty(:baz) do Puppet.warning("Property :baz -> Starting")end

newparam(:bar) do Puppet.warning("Param :bar -> Starting")end

warning: Param :foo -> Startingwarning: Property :baz -> Startingwarning: Param :bar -> Starting

Page 13: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Provider Globals

Output

Puppet::Type.type(:ex_order).provide(:ruby) do Puppet.warning("Setting Property Class Variables")

@@ex_order_classvars = { :example => true }

(...)end

warning: Setting Property Class Variables

Page 14: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Type Name Parameter Munge

Output

newparam(:name) do isnamevar munge do |value| Puppet.warning("#{value}: In the name parameter.") value endend

warning: test: In the name parameter.

Page 15: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Provider Initializer

Output

def initialize(*args) super(*args) Puppet.warning("Provider Initialization :name= '#{@resource[:name]}'") Puppet.warning("Provider Initialization :foo = '#{@resource[:foo]}'") Puppet.warning("Provider Initialization :bar = '#{@resource[:bar]}'")end

warning: Provider Initialization :name= 'test'warning: Provider Initialization :foo = ''warning: Provider Initialization :bar = ''

Page 16: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Type Property|Baz

Output

# newparam(:foo) do# newproperty(:baz) do# newparam(:bar) do

newproperty(:baz) do validate do |value| Puppet.warning("#{resource[:name]}: :baz -> Validating") Puppet.warning("#{resource[:name]}: Foo -> '#{resource[:foo]}'") endend

warning: test: Property :baz -> Validatingwarning: test: Foo -> ''

Page 17: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Type Parameter|Foo

Output

# newparam(:foo) do# newproperty(:baz) do# newparam(:bar) do

newparam(:foo) do validate do |value| Puppet.warning("#{resource[:name]}: Param :foo -> Validating") end

munge do |value| Puppet.warning("#{resource[:name]}: Param :foo -> Munging") Puppet.warning("Where's Param :bar? Bar is '#{resource[:bar]}'") value endend

warning: test: Param :foo -> Validatingwarning: test: Param :foo -> Munging

warning: Where's Param :bar? Bar is ''

Page 18: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Type Parameter|Bar

Output

# newparam(:foo) do# newproperty(:baz) do# newparam(:bar) do

newparam(:bar) do Puppet.warning("Param :bar -> Starting") validate do |value| Puppet.warning("#{resource[:name]}: Param :bar -> Validating") end munge do |value| Puppet.warning("#{resource[:name]}: Param :bar -> Munging") Puppet.warning("Where's Param :foo? Foo is '#{resource[:foo]}'") value endend

warning: test: Param :bar -> Validatingwarning: test: Param :bar -> Munging

warning: test: Where's Param :foo? Foo is 'foo' <- Order Matters!

Page 19: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Type Validation

Output

validate do required_params = [:foo, :bar, :baz] Puppet.warning("#{self[:name]}: Validating") required_params.each do |param| if not self[param] then # Note how we show the user *where* the error is. raise Puppet::ParseError,"Oh noes! #{self.file}:#{self.line}" end endend

Warning: test: Param :bar -> Validating

Page 20: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

ARE WE THEREYET?

(Arrested Development, Fox/Netflix)

Page 21: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Type Initializing

Output

def initialize(args) super(args)

Puppet.warning("#{self[:name]}: Type Initializing")

num_ex_orders = @catalog.resources.find_all { |r| r.is_a?(Puppet::Type.type(:ex_order)) }.count Puppet.warning("Ex_order's in the catalog: '#{num_ex_orders+1}'")end

warning: test: Type Initializingwarning: Ex_order's in the catalog: '1'

Page 22: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Type Finish

Output

def finish Puppet.warning("#{self[:name]}: Type Finishing")

# Don't forget to call this *at the end* superend

warning: test: Type Finishing

Page 23: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Type Autorequires

Output

autorequire(:file) do Puppet.warning("#{self[:name]}: Autorequring") ["/tmp/foo"]end

warning: test: Autorequring

Page 24: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Provider Baz|Getter

Output

Puppet::Type.type(:ex_order).provide(:ruby) do def baz # This is what 'is' ends up being in insync? Puppet.warning("#{@resource[:name]}: In getter for :baz") endend

warning: test: In getter for :baz

Page 25: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Type Baz|Insync?

Output

newproperty(:baz) do def insync?(is) # This is simply what the native provider code would do. is == @should

# Note, you have to use @resource, not resource here since # you're not in the property any more, you're in the provider. Puppet.warning("#{@resource[:name]}: In 'insync?' for :baz")

# We're returning false just to see the rest of the components # fire off. false endend

warning: test: In 'insync?' for :baz

Page 26: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Provider Baz|Setter

Output

Puppet::Type.type(:ex_order).provide(:ruby) do def baz=(should) # Called if insync? is false Puppet.warning("#{@resource[:name]}: In setter for :baz") endend

warning: test: In setter for :baznotice: /Stage[main]/Main/Ex_order[test]/baz: baz changed 'test: In getter for :baz' to 'baz'

Page 27: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Provider Flush

Output

Puppet::Type.type(:ex_order).provide(:ruby) do def flush # This happens if *any* property was modified. Puppet.warning("Time to flush #{@resource[:name]}") endend

warning: Time to flush test

Page 28: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Provider Post-Resource Eval

Output

# This is new in Puppet 3.4.0 and can be used for# cleaning up any resource persistence since it happens after# *all* resources of this type have been evaluated.

def self.post_resource_eval Puppet.warning("FINIS!!!!")end

warning: FINIS!!!!

Page 29: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

(The Legend of Zelda, Nintendo)

Page 30: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Resources

The original article upon which this presentation isbased

The official documentation on custom types.

The Puppet Types and Providers book by and

The source code for this presentation

Type and Provider Execution Walkthrough

Puppet Labs' Custom Type Documentation

Puppet Types and Providers BookDan Bode

Nan LiuPresentation Source

Page 31: Puppet Camp DC 2014: Puppet Type and Provider Execution Presentation

Puppet Type and Provider Execution

Presentation Information

This presentation was made possible by:by

by Reveal.js Hakim El HattabReveal.js Modifications José Manuel Ciges Regueiro