greenfield puppet: getting it right from the start

109
Greenfield Puppet David Danzilio @djdanzilio

Upload: david-danzilio

Post on 14-Apr-2017

213 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Greenfield Puppet: Getting it right from the start

Greenfield PuppetDavid Danzilio

@djdanzilio

Page 2: Greenfield Puppet: Getting it right from the start

$(whoami)

Page 3: Greenfield Puppet: Getting it right from the start

What’s this all about?

Page 4: Greenfield Puppet: Getting it right from the start

A collection of wisdom that I wish I had available when I first started using Puppet

Page 5: Greenfield Puppet: Getting it right from the start

This could change in future releases

Page 6: Greenfield Puppet: Getting it right from the start

Don’t just take my word for it

Page 7: Greenfield Puppet: Getting it right from the start
Page 8: Greenfield Puppet: Getting it right from the start

– Wikipedia

“a greenfield is a project that lacks any constraints imposed by prior work”

Page 9: Greenfield Puppet: Getting it right from the start
Page 10: Greenfield Puppet: Getting it right from the start

A (not so) hypothetical scenario…

Page 11: Greenfield Puppet: Getting it right from the start

Department of BasketweavingFU

Foo University

Page 12: Greenfield Puppet: Getting it right from the start

class apache {! package { [‘apache2’, ‘rails’, ‘libapache2-mod-passenger’]:! ensure => present,! }! file { ‘/etc/apache2/sites-enabled/bw-app.conf’:! ensure => file,! source => ‘puppet:///apache/bw-app.conf',! require => Package[‘apache2’],! }! file { ‘/var/www/bw-app’:! ensure => directory,! owner => www-data,! group => www-data,! source => ‘puppet:///apache/bw-app',! recurse => true,! require => File[‘/etc/apache2/sites-enabled/bw-app.conf’]! }! service { ‘apache2’:! ensure => running,! require => [! File[‘/var/www/bw-app’],! Package[’rails’, ‘libapache2-mod-passenger’],! ]! }!}

Page 13: Greenfield Puppet: Getting it right from the start

I want to use Graphitefor all my!

basket data

Page 14: Greenfield Puppet: Getting it right from the start

OH NOES!

Page 15: Greenfield Puppet: Getting it right from the start
Page 16: Greenfield Puppet: Getting it right from the start

–Doug McIlroy

“Write programs that do one thing and do it well. Write programs to work together.”

Page 17: Greenfield Puppet: Getting it right from the start

Modules

Page 18: Greenfield Puppet: Getting it right from the start

Don’t write modules unless you absolutely have to!

Page 19: Greenfield Puppet: Getting it right from the start
Page 20: Greenfield Puppet: Getting it right from the start

Check the Forge first

Page 21: Greenfield Puppet: Getting it right from the start

2,617

Page 22: Greenfield Puppet: Getting it right from the start

Don’t customize Forge modules!

Page 23: Greenfield Puppet: Getting it right from the start

Use the Roles and Profiles pattern

Page 24: Greenfield Puppet: Getting it right from the start

Roles and Profiles

Page 25: Greenfield Puppet: Getting it right from the start

Craig Dunn’s blog post: “Designing Puppet – Roles

and Profiles”

Page 26: Greenfield Puppet: Getting it right from the start

A node includes one role

Page 27: Greenfield Puppet: Getting it right from the start

node db01.example.com { include role::db::server } !

node db02.example.com { include role::db::server::dev }

Page 28: Greenfield Puppet: Getting it right from the start

A role includes one or more profiles

Page 29: Greenfield Puppet: Getting it right from the start

class role::base { include profile::base } !class role::db::server inherits role::base { include profile::mysql include profile::application::database } !class role::db::server::dev inherits role::base { include profile::mysql include profile::percona }

Page 30: Greenfield Puppet: Getting it right from the start

A profile manages modules

Page 31: Greenfield Puppet: Getting it right from the start

class profile::mysql { ! $mysql_version = hiera(‘mysql_version’) ! class { ‘mysql::server’: package_ensure => $mysql_version } ! class { ‘mysql::backup’: } class { ‘nagios::mysql’: } !}

Page 32: Greenfield Puppet: Getting it right from the start

Like MVC for Puppet

Page 33: Greenfield Puppet: Getting it right from the start

The Forge

Page 34: Greenfield Puppet: Getting it right from the start

Look for modules with lots of downloads and recent

updates

Page 35: Greenfield Puppet: Getting it right from the start

Lots of downloadsRecent update

Page 36: Greenfield Puppet: Getting it right from the start

Do some background research on the author of the

module

Page 37: Greenfield Puppet: Getting it right from the start

Give priority to modules written by Puppet Labs

Page 38: Greenfield Puppet: Getting it right from the start

Be weary of modules with strange dependencies

Page 39: Greenfield Puppet: Getting it right from the start

WHY!??!?!

Page 40: Greenfield Puppet: Getting it right from the start

Don’t use a module without vetting it

Page 41: Greenfield Puppet: Getting it right from the start
Page 42: Greenfield Puppet: Getting it right from the start

Puppet Deployment

Page 43: Greenfield Puppet: Getting it right from the start

Have a solid deployment pipeline

Page 44: Greenfield Puppet: Getting it right from the start
Page 45: Greenfield Puppet: Getting it right from the start

Use librarian-puppet or r10k to deploy your code to your

Puppet masters

Page 46: Greenfield Puppet: Getting it right from the start

Reliable metadata is key to a successful Puppet deployment

Page 47: Greenfield Puppet: Getting it right from the start

$ cat /etc/facter/facts.d/metadata.json { "datacenter": "Boston", "rack": "R23", "role": "webserver", "cluster": "C89" }

Page 48: Greenfield Puppet: Getting it right from the start

Use environments to keep your nodes safe

Page 49: Greenfield Puppet: Getting it right from the start

Understand the lifecycle of a module

Page 50: Greenfield Puppet: Getting it right from the start

Use Hiera from the start

Page 51: Greenfield Puppet: Getting it right from the start

Don’t get too crazy with your hierarchy

Page 52: Greenfield Puppet: Getting it right from the start

:hierarchy: - “%{::app}/%{::environment}/%{::datacenter}/%{::fqdn}” - “%{::app}/%{::environment}/%{::datacenter}” - “%{::app}/%{::environment}” - “%{::app}” - “%{::cluster}/%{::environment}/%{::datacenter}/%{::fqdn}” - “%{::cluster}/%{::environment}/%{::datacenter}” - “%{::cluster}/%{::environment}” - “%{::cluster}” - “%{::environment}/%{::datacenter}/%{::fqdn}” - “%{::environment}/%{::datacenter}” - “%{::environment}” - “%{::realm}/%{::region}/%{::datacenter}/%{::fqdn}” - “%{::realm}/%{::region}/%{::datacenter}” - “%{::realm}/%{::region}” - “%{::realm}” - “%{::region}” - “%{::datacenter}/%{::rack}/%{::cluster}/%{::fqdn}” - “%{::datacenter}/%{::rack}/%{::cluster}” - “%{::datacenter}/%{::rack}” - “%{::datacenter}” - “%{::rack}” - “%{::cluster}”

Page 53: Greenfield Puppet: Getting it right from the start

Puppet Development

Page 54: Greenfield Puppet: Getting it right from the start

Puppet code is real code

Page 55: Greenfield Puppet: Getting it right from the start

Puppet is Ruby

Page 56: Greenfield Puppet: Getting it right from the start
Page 57: Greenfield Puppet: Getting it right from the start

Puppet modules need a design specification

Page 58: Greenfield Puppet: Getting it right from the start

rspec-puppet for TDD

Page 59: Greenfield Puppet: Getting it right from the start

Design modules with other people in mind

Page 60: Greenfield Puppet: Getting it right from the start

Fail fast

Page 61: Greenfield Puppet: Getting it right from the start

fail(“${::osfamily} is not supported by this module.”)

Page 62: Greenfield Puppet: Getting it right from the start

Public classes should expose a stable API

Page 63: Greenfield Puppet: Getting it right from the start

Semantic Versioning is your friend

Page 64: Greenfield Puppet: Getting it right from the start

Major.Minor.Patch

Page 65: Greenfield Puppet: Getting it right from the start

X.0.0

Page 66: Greenfield Puppet: Getting it right from the start

0.X.0

Page 67: Greenfield Puppet: Getting it right from the start

0.0.X

Page 68: Greenfield Puppet: Getting it right from the start

Remember the UNIX philosophy

Page 69: Greenfield Puppet: Getting it right from the start

Share your custom modules with the community!

Page 70: Greenfield Puppet: Getting it right from the start

Manage your dependencies with care

Page 71: Greenfield Puppet: Getting it right from the start

Keep artifacts out of your Puppet modules

Page 72: Greenfield Puppet: Getting it right from the start

Embedding data makes your modules less modular

Page 73: Greenfield Puppet: Getting it right from the start

class foo ( $pkg_version = $foo::params::pkg_version, $pkg_name = $foo::params::pkg_name, ) inherits foo::params { ! ... !}

Page 74: Greenfield Puppet: Getting it right from the start

Keep business logic out of templates

Page 75: Greenfield Puppet: Getting it right from the start

<% if @app == ‘foo’ %> ... <% else %> ... <% end %>

Page 76: Greenfield Puppet: Getting it right from the start

$template = ? $app { ‘foo’ => ‘foo.conf.erb’, default => ‘generic.conf.erb’, } !file { ‘/path/to/app.conf’: ensure => file, content => template(“module/${template}”), }

Page 77: Greenfield Puppet: Getting it right from the start

Standard Library

Page 78: Greenfield Puppet: Getting it right from the start

Use the standard library to level-up your modules

Page 79: Greenfield Puppet: Getting it right from the start

Avoid duplicate resources with ensure_packages and ensure_resource

Page 80: Greenfield Puppet: Getting it right from the start

package { ‘apache2’: ensure => present, }

Page 81: Greenfield Puppet: Getting it right from the start

ensure_packages([‘apache2’])

Page 82: Greenfield Puppet: Getting it right from the start

Validate inputs with validate_array, validate_bool, validate_hash, validate_re, and validate_string

Page 83: Greenfield Puppet: Getting it right from the start

Protect private classes with private

Page 84: Greenfield Puppet: Getting it right from the start
Page 85: Greenfield Puppet: Getting it right from the start

Modules should be easy to use and hard to abuse

Page 86: Greenfield Puppet: Getting it right from the start

StyleSubstance

Page 87: Greenfield Puppet: Getting it right from the start

Style is important

Page 88: Greenfield Puppet: Getting it right from the start

puppet-lint

Page 89: Greenfield Puppet: Getting it right from the start

puppet-syntax

Page 90: Greenfield Puppet: Getting it right from the start

source 'https://rubygems.org' !

gem 'rake' gem 'puppet' gem 'puppet-lint' gem 'puppet-syntax'

Page 91: Greenfield Puppet: Getting it right from the start

require 'puppet-lint/tasks/puppet-lint' require 'puppet-syntax/tasks/puppet-syntax' !exclude_paths = [ "pkg/**/*", "vendor/**/*", "spec/**/*", ] !PuppetLint.configuration.ignore_paths = exclude_paths PuppetSyntax.exclude_paths = exclude_paths

Page 92: Greenfield Puppet: Getting it right from the start

$ bundle install Fetching gem metadata from https://rubygems.org/........ Resolving dependencies... Installing rake 10.3.2 Installing CFPropertyList 2.2.8 Installing facter 2.1.0 Installing json_pure 1.8.1 Installing hiera 1.3.4 Installing rgen 0.6.6 Installing puppet 3.6.2 Installing puppet-lint 0.3.2 Installing puppet-syntax 1.3.0 Using bundler 1.6.2 Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

Page 93: Greenfield Puppet: Getting it right from the start

$ bundle exec rake -T rake lint # Run puppet-lint rake syntax # Syntax check Puppet manifests and templates rake syntax:hiera # Syntax check Hiera config files rake syntax:manifests # Syntax check Puppet manifests rake syntax:templates # Syntax check Puppet templates

Page 94: Greenfield Puppet: Getting it right from the start

Documentation is important

Page 95: Greenfield Puppet: Getting it right from the start
Page 96: Greenfield Puppet: Getting it right from the start
Page 97: Greenfield Puppet: Getting it right from the start

We’re all Keynesians now.developers

Page 98: Greenfield Puppet: Getting it right from the start

Keeping Up

Page 99: Greenfield Puppet: Getting it right from the start

Puppet is evolving really fast

Page 100: Greenfield Puppet: Getting it right from the start

There is a fantastic community out there

Page 101: Greenfield Puppet: Getting it right from the start

Pay attention to thought leaders

Page 102: Greenfield Puppet: Getting it right from the start
Page 103: Greenfield Puppet: Getting it right from the start

Refactor your code as the language evolves

Page 104: Greenfield Puppet: Getting it right from the start
Page 105: Greenfield Puppet: Getting it right from the start

Contribute to modules on the Forge!

Page 106: Greenfield Puppet: Getting it right from the start

Questions?

Page 107: Greenfield Puppet: Getting it right from the start

Thank you!

Page 108: Greenfield Puppet: Getting it right from the start

Image Credits• http://officeimg.vo.msecnd.net/en-us/images/MP900430517.jpg • http://imgur.com/gallery/YNI5wud • http://www.reddit.com/r/funny/comments/1jgxtq/new_york_and_boston_the_difference/ • http://openclipart.org/detail/195046/ubuntu-geek-by-stephencuyos-195046 • http://design.ubuntu.com/downloads?metadata=element-logo+brand-ubuntu • http://commons.wikimedia.org/wiki/File:Ruby_on_Rails-logo.png • https://github.com/phusion/passenger • http://kaleidos.net/weapons/apache-webserver/ • http://puppetlabs.com/company/news/media-kit • http://copiousnotes.bloginky.com/2014/06/17/summer-classic-dr-strangelove-2/ • http://imgur.com/iWKad22 • http://cheezburger.com/6230961920 • http://www.craigdunn.org/stuff/puppet_big.png • http://www.quickmeme.com/meme/362un7 • http://programmerryangosling.tumblr.com/image/22790837971 • http://www.quickmeme.com/meme/3sogf9 • http://wall.alphacoders.com/big.php?i=238266

Page 109: Greenfield Puppet: Getting it right from the start

Further Reading• http://www.craigdunn.org/2012/05/239/ • https://www.youtube.com/user/PuppetLabsInc/playlists • https://github.com/puppetlabs/puppetlabs-stdlib • http://continuousdelivery.com • http://www.slideshare.net/PuppetLabs/tddforpuppet • http://www.slideshare.net/PuppetLabs/roles-rofiles • http://www.slideshare.net/PuppetLabs/steamlining-

puppetdevelopmentpuppetconfny2014 • http://garylarizza.com/blog/2013/12/08/when-to-hiera/ • http://www.devco.net/archives/2013/12/09/the-problem-with-params-pp.php • http://www.devco.net/archives/2013/12/08/better-puppet-modules-using-hiera-data.php • http://puppet-lint.com • https://github.com/gds-operations/puppet-syntax