41

Thinking through styling and layout of modules, class and defines by Tom Doran

  • Upload
    netways

  • View
    674

  • Download
    4

Embed Size (px)

DESCRIPTION

How do you structure your puppet code? Where do you put the files, how do you name things, how do you structure parameters and hieradata? I’m still stuck on puppet 2.7, how do I write code now to take advantage of puppet 3.0 features when I upgrade? This talk will go through a set of guidelines for writing classes, defines, having defaults and how to use hieradata right. It’ll be focusing on real code with real examples taken from modules I’ve helped write or code review. Whilst suitable for beginners, this talk should hopefully be helpful for more advanced users as whilst most of us have a shared vision of ‘well known’ ways things can be done - we rarely step back and think through exactly why in detail.

Citation preview

Page 1: Thinking through styling and layout of modules, class and defines by Tom Doran
Page 2: Thinking through styling and layout of modules, class and defines by Tom Doran

Thinking through code layout - Tomas Doran

@bobtfish

[email protected]

28/11/2013

Page 3: Thinking through styling and layout of modules, class and defines by Tom Doran

• Still takes effort to understand

•More than one right way to do it

• KISS

• Get data out of manifests

• Follow conventions

Lots of great material

http://www.slideshare.net/PuppetLabs/modern-module-development-ken-barber-2012-edinburgh-puppet-camp http://www.slideshare.net/slideshow/embed_code/25536833 https://speakerdeck.com/player/889a6450ee6b0130026036a0670cc949

Page 4: Thinking through styling and layout of modules, class and defines by Tom Doran

• No manifests except site.pp

• autoload structure

•Modules work with environments

•Well known higher level reuse patterns! (Later)

Modules only!

All code should be in modules, always - with no exceptions except a site.pp

Page 5: Thinking through styling and layout of modules, class and defines by Tom Doran

•Most important for forge modules!

• For your own modules, KISS

• Allow package install to be overridden!

• Allow service to be not managed!

• Don’t manage users!

Be flexible!

Page 6: Thinking through styling and layout of modules, class and defines by Tom Doran

• Puppet 0.x •… upgrade?

• Puppet 2.7 • Parameterized classes • Dynamic scope

• Puppet 3.0 • NO dynamic scope • Data bindings (automatic hiera)

• Puppet 3.3 • Hiera in modules

Version compatibility

http://docs.puppetlabs.com/guides/parameterized_classes.html

Page 7: Thinking through styling and layout of modules, class and defines by Tom Doran

Dynamic scope

Page 8: Thinking through styling and layout of modules, class and defines by Tom Doran

Dynamic scope

Page 9: Thinking through styling and layout of modules, class and defines by Tom Doran

NO Dynamic scope

Page 10: Thinking through styling and layout of modules, class and defines by Tom Doran

Data binding

Puppet 2.7

Page 11: Thinking through styling and layout of modules, class and defines by Tom Doran

Data binding

Puppet 2.7

Puppet 3.x

Page 12: Thinking through styling and layout of modules, class and defines by Tom Doran

ARM9

Page 13: Thinking through styling and layout of modules, class and defines by Tom Doran

ARM9

?????

Eh? Is that a processor type?

Page 14: Thinking through styling and layout of modules, class and defines by Tom Doran

ARM9

Or one of these?

Page 15: Thinking through styling and layout of modules, class and defines by Tom Doran

• In puppet 3.3: modules/foo/data modules/foo/data/common.yaml modules/foo/data/os/Linux.yaml modules/foo/hiera.yaml

• Doesn’t really affect design if you did it right!

ARM9 - hiera in modules

https://github.com/puppetlabs/armatures/blob/master/arm-9.data_in_modules/index.md

Page 16: Thinking through styling and layout of modules, class and defines by Tom Doran

Package / File / Service

Starting at the basics….

Page 17: Thinking through styling and layout of modules, class and defines by Tom Doran

Package / File / Service

Most modules do something like this. Note the ${module_name} variable for DRY http://docs.puppetlabs.com/puppet/3/reference/lang_variables.html#parser-set-variables http://docs.puppetlabs.com/learning/ordering.html#packagefileservice

Page 18: Thinking through styling and layout of modules, class and defines by Tom Doran

Metaparameters

http://docs.puppetlabs.com/learning/ordering.html#packagefileservice

Page 19: Thinking through styling and layout of modules, class and defines by Tom Doran

Package/Config/Service classes

Putting each component in it’s own class makes dependencies simpler / more declarative http://www.devco.net/archives/2009/09/28/simple_puppet_module_structure.php

Page 20: Thinking through styling and layout of modules, class and defines by Tom Doran

• Entry point should always be init.pp: include foo

One entry point

!http://www.devco.net/archives/2009/09/28/simple_puppet_module_structure.php

Page 21: Thinking through styling and layout of modules, class and defines by Tom Doran

• Put inter-class dependencies in init.pp

Externalize dependencies

Some people don’t like this (I do), as it makes the dependencies obvious from the entry point you include. http://www.devco.net/archives/2012/12/13/simple-puppet-module-structure-redux.php

Page 22: Thinking through styling and layout of modules, class and defines by Tom Doran

Separate parameters

This is the traditional method, having a ::params class. This is good when you have conditional logic around parameters. http://docs.puppetlabs.com/puppet/3/reference/lang_classes.html#inheritance http://docs.puppetlabs.com/guides/parameterized_classes.html#appendix-smart-parameter-defaults

Page 23: Thinking through styling and layout of modules, class and defines by Tom Doran

Hiera parameters (2.7)

For simple (non conditional) values, you can just use a hiera lookup. If you’re shipping to the forge you still need params class, if you’re not you can just use hiera.

Page 24: Thinking through styling and layout of modules, class and defines by Tom Doran

Hiera parameters (3.x)

http://docs.puppetlabs.com/puppet/3/reference/lang_classes.html#inheritance http://docs.puppetlabs.com/guides/parameterized_classes.html#appendix-smart-parameter-defaults

Page 25: Thinking through styling and layout of modules, class and defines by Tom Doran

Hiera parameter variance

Data in params.pp is kinda gross. Move it out to common.yaml? Logic for which key to look in can be embedded in the hiera call (instead of params.pp).

Page 26: Thinking through styling and layout of modules, class and defines by Tom Doran

Hiera parameter variance

If your hierarchy includes: os/%{::operatingsystem} common

!You can just use: foo::package_name !

If you have ${::operating_system} in your hierarchy, you can do this simpler thing. Possible to argue this both ways round, but I’d recommend this as it’s more forward compatible (ARM9)

Page 27: Thinking through styling and layout of modules, class and defines by Tom Doran

Hiera parameter variance

If your hierarchy includes: os/%{::operatingsystem} common

!You can just use: foo::package_name !

Unless it’s a forge module :(

If you’re shipping to the forge, you can’t (sanely) rely on puppet 3.3, and you don’t want to force people to add to their hieradata - fallback to params.pp

Page 28: Thinking through styling and layout of modules, class and defines by Tom Doran

Puppet 3.3

Per module hierarchy: foo/data/os/%{::operatingsystem}.yaml foo/data/common.yaml

Page 29: Thinking through styling and layout of modules, class and defines by Tom Doran

• str2bool • any2array • ensure_packages • ensure_resource • get_module_path • getparam • facts.d •many many more!

Use stdlib

https://github.com/puppetlabs/puppetlabs-stdlib

Page 30: Thinking through styling and layout of modules, class and defines by Tom Doran

• ‘true’ vs true • All class parameters • All hiera data

• Other backends may not have booleans!

str2bool

https://github.com/puppetlabs/puppetlabs-stdlib

Page 31: Thinking through styling and layout of modules, class and defines by Tom Doran

• Any time you take an array parameter!

any2array

https://github.com/puppetlabs/puppetlabs-stdlib

Page 32: Thinking through styling and layout of modules, class and defines by Tom Doran

!

• Avoids duplicate resources

!!!

• Same for generic resources

ensure_packages / ensure_resource

https://github.com/puppetlabs/puppetlabs-stdlib

Page 33: Thinking through styling and layout of modules, class and defines by Tom Doran

• nginx::vhost {} • docker::run {}

Toolkits

Page 34: Thinking through styling and layout of modules, class and defines by Tom Doran

• nginx::vhost {} • docker::run {}

• create_resources helper

Toolkits

Page 35: Thinking through styling and layout of modules, class and defines by Tom Doran

Multiple instances

•May have more than one nginx running!

• This is where it gets complex :)

• Class or define params override hiera

• Default params in foo::bar

• Instance params: foo::instance::${instance_name}::bar

Page 36: Thinking through styling and layout of modules, class and defines by Tom Doran

Toolkits + Multiple instances

Page 37: Thinking through styling and layout of modules, class and defines by Tom Doran

getparam (stdlib)

https://github.com/puppetlabs/puppetlabs-stdlib

Page 38: Thinking through styling and layout of modules, class and defines by Tom Doran

Toolkits + Multiple instances

Page 39: Thinking through styling and layout of modules, class and defines by Tom Doran

• Role = node classifier (contains >= 1 profile) • role::webserver

• Profile = use multiple modules + hiera data • profile::apache • profile::tomcat

•Module = Single concern • apache • tomcat

Roles and profiles

http://www.craigdunn.org/2012/05/239/ http://blog.keepingyouhonest.net/?p=443 https://puppetlabs.com/learn/roles-profiles-introduction

Page 40: Thinking through styling and layout of modules, class and defines by Tom Doran

tags for overriding

http://docs.puppetlabs.com/puppet/3/reference/lang_resources.html#amending-attributes-with-a-collector

Page 41: Thinking through styling and layout of modules, class and defines by Tom Doran

Thanks!

$864

http://www.yelp.com/careers?jvi=ogVTXfwL

Questions?

Slides will be up shortly at:

http://slideshare.net/bobtfish/

Mail: [email protected]

Twitter: @bobtfish