doing the refactor dance - making your puppet modules more modular - puppetconf 2014

77
2014 Presented by The Refactor Dance Gary Larizza Professional Services | Puppet Labs @glarizza

Upload: puppet-labs

Post on 04-Jul-2015

2.915 views

Category:

Technology


1 download

DESCRIPTION

Workshop: Doing the Refactor Dance - Making Your Puppet Modules More Modular - Gary Larizza, Puppet Labs

TRANSCRIPT

Page 1: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

2014

Presented by

The Refactor DanceGary Larizza Professional Services | Puppet Labs @glarizza

Page 2: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

http://bit.ly/refactordance

Page 3: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Page 4: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Worst. Hands-on. Ever

Page 5: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

• Abstraction and data separation

• Data Hierarchy

• Classification

• Workflow

Page 6: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Classification

Implementation Implementation

Page 7: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Role

Profile ProfileHiera

Component Modules

Page 8: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

roles::application_server

profiles::java profiles::tomcat

Hiera

Component Modules

Page 9: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Component Modules

Page 10: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Stop writing custom goddamn component

modules

Page 11: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

$httpd_root = "/opt/corp/data/http" package { ‘httpd’: ensure => latest, } file { “/opt/corp/data/http/conf.d”: owner => "httpd", ensure => directory; “/opt/corp/data/http/conf.d/corp.conf”: owner => "httpd", ensure => file; }

Page 12: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Stop writing component modules

• Too many ‘okay’ modules

• Maintenance & upkeep

• You are not unique

• You are entirely too lazy

Page 13: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Parameterize your classes

Page 14: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class apache {! case $::osfamily {! 'RedHat': {! $confdir = ‘/etc/httpd/conf‘! $conffile = “${confdir}/httpd.conf”! }! 'Debian': {! $confdir = ‘/etc/apache2/conf‘! $conffile = “${confdir}/apache2.conf”! }! }!}

Page 15: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class apache (! $confdir = $apache::params::confdir,! $conffile = $apache::params::conffile,!) inherits apache::params {! file { $confdir:! ensure => directory,! }! file { $conffile:! ensure => file,! content => template(’apache/apache.conf.erb’),! }!}

Page 16: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Parameterize classes

• Parameters = API

• Single-entry classes

• The ‘Forge test’

Page 17: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Params & shareable data

Page 18: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class apache::params {! case $::osfamily {! 'RedHat': {! $confdir = ‘/etc/httpd/conf‘! $conffile = “${confdir}/httpd.conf”! }! 'Debian': {! $confdir = ‘/etc/apache2/conf‘! $conffile = “${confdir}/apache2.conf”! }! }!}

Page 19: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Shareable data

• OS-specific data != private data

• Sane defaults

• Validation…

Page 20: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Validation

Page 21: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class xinetd (! $confdir = $apache::params::confdir,! $conffile = $apache::params::conffile,!) inherits xinetd::params {! file { $confdir:! ensure => directory,! }! file { $conffile:! ensure => file,! content => template(’apache/apache.conf.erb’),! }!}

Page 22: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class xinetd (! $confdir = $apache::params::confdir,! $conffile = $apache::params::conffile,!) inherits xinetd::params {! validate_absolute_path($confdir)! validate_absolute_path($conffile)! file { $confdir:! ensure => directory,! }! file { $conffile:! ensure => file,! content => template(’apache/apache.conf.erb’),! }!}

Page 23: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Validation

• Functions in puppetlabs-stdlib

• Never pass unvalidated data to resources

Page 24: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Class

Page 25: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class mysql::server (! ## params here!) inherits mysql::params {!!

include ::mysql::server::install! include ::mysql::server::config! include ::mysql::server::service!!

}

Page 26: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class mysql::server (! ## params here!) inherits mysql::params {!!

include ::mysql::server::install! include ::mysql::server::config! include ::mysql::server::service!!

anchor { ‘mysql:start’: }! -> Class[‘mysql::server::install’]! -> Class[‘mysql::server::config’]! -> Class[‘mysql::server::service’]! -> anchor { ‘mysql:end’: }!}

Page 27: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class mysql::server (! ## params here!) inherits mysql::params {!!

contain ::mysql::server::install! contain ::mysql::server::config! contain ::mysql::server::service!!

}* Puppet ≥ 3.4.0

Page 28: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Class containment

• Before Puppet 3.4.0 - use anchors

• After Puppet 3.4.0 - use contain

Page 29: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Hiera

Page 30: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class data_in_code {! case $::application_tier {! 'dev': {! $java_version = '6.0.3'! $tomcat_version = '6.0'! }!!

'test': {! $java_version = '7.0.1'! $tomcat_version = '7.0'! }! }!}

Page 31: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class data_in_code {! $java_version = hiera(’java_version’)! $tomcat_version = hiera(’tomcat_version’)!}

Page 32: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

--- :backends: - yaml

:yaml: :datadir: /etc/puppetlabs/puppet/hieradata

:hierarchy: - “nodes/%{::clientcert}” - “location/%{::location}" - “tier/%{::application_tier}" - common

hiera.yaml

Page 33: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

--- java_version: 7.0 tomcat_version: 8.0

dev.yaml

--- java_version: 6.0 tomcat_version: 7.0

prod.yaml

Page 34: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Where’s

Page 35: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

What’s an Application

Page 36: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

‘Application tier’

• Long lived • Data usually separate • ‘The Data’

!

!

!

‘Environment’

• Short lived • Migration path to ‘production’ • ‘The Model’

!

!

!

Page 37: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Hierarchy structure?

• How/where is data different?

• Most -> least specific

• Folders are your friends

Page 38: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Profiles

Page 39: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

But first…

Page 40: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

include apache!

class { ‘apache’: }!

vs.!

Page 41: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

include apache!include apache!include apache!include apache!include apache!

Page 42: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class { ‘apache’: }!include apache!

Page 43: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

include apache!class { ‘apache’: }!include apache!

Page 44: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Namespacing

Page 45: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class data_in_code {! $java_version = hiera(’java_version’)! $tomcat_version = hiera(’tomcat_version’)!!

notify { “Java is: ${java_version}”: }!}

Page 46: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class data_in_code {! $java_version = hiera(’java_version’)! $tomcat_version = hiera(’tomcat_version’)!!

notify { “Java is: ${data_in_code::java_version}”: }!}

Page 47: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class profiles::jenkins {! include jenkins!}

Page 48: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class profiles::jenkins {! include ???????!}

Page 49: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class profiles::jenkins {! include ::jenkins!}

Page 50: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Data

Page 51: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class data_in_code {! case $::application_tier {! 'dev': {! $java_version = '6.0.3'! $tomcat_version = '6.0'! }!!

'test': {! $java_version = '7.0.1'! $tomcat_version = '7.0'! }! }!}

Page 52: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class profiles::tomcat {! $java_version = hiera(’java_version’)! $tomcat_version = hiera(’tomcat_version’)!!

class { ’::tomcat’:! version => $tomcat_version,! }!!

class { ’::java’:! version => $java_version,! }!}

Page 53: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class apache {! file { ‘/opt/custom/key.pem’:! ensure => file,! source => ’puppet:///modules/apache/key.pem'! }!!

file { ‘/things/that/dont/belong/in/apache’:! ensure => file,! source => ’puppet:///modules/apache/blargh'! } !}

class apache {! file { ‘/opt/custom/key.pem’:! ensure => file,! source => ’puppet:///modules/apache/key.pem'! }!!

file { ‘/things/that/dont/belong/in/apache’:! ensure => file,! source => ’puppet:///modules/apache/blargh'! } !}

Page 54: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class profiles::apache {! include apache! $keypath = hiera(’apache_keypath’)!!

file { “${keypath}/key.pem”:! ensure => file,! source => ’puppet:///modules/profiles/key.pem'! }!!

file { ‘/things/that/dont/belong/in/apache’:! ensure => file,! source => ’puppet:///modules/profiles/blargh'! }!}

Page 55: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Dependencies

Page 56: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class tomcat {! class { ‘java’:! version => ‘6.0’,! }!!

Class[‘java’]! -> Class[‘tomcat’]!}

Page 57: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class profiles::tomcat {! $java_version = hiera(’java_version’)! $tomcat_version = hiera(’tomcat_version’)!!

class { ‘::java’:! version => $java_version,! }! class { ‘::tomcat’:! version => $tomcat_version,! }!!

Class[‘::java’]! -> Class[‘::tomcat’]!}

Page 58: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class profiles::tomcat {! include profiles::java! $tomcat_version = hiera(’tomcat_version’)! ! class { ‘::tomcat’:! version => $tomcat_version,! }!!

Class[‘profiles::java’]! -> Class[‘::tomcat’]!}

Page 59: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Profiles

• Hiera for business-specific data

• Proprietary resources

• Inter-class dependencies and containment

Page 60: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Roles

Page 61: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Classification

Page 62: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

denuatapp06p

falcor

Page 63: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

roles::app_server::pci

roles::proxy

Page 64: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class roles {! include profiles::security::base! include profiles::mycorp::users! include profiles::mycorp::os_base!}

Page 65: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class roles::app_server inherits roles {! include profiles::tomcat! include profiles::our_app! include profiles::shibboleth!!

Class[‘profiles::tomcat’]! -> Class[‘profiles::our_app’]! -> Class[‘profiles::shibboleth’]!}

Page 66: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class roles::app_server::pci inherits roles::app_server {! include profiles::pci!}

Page 67: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

class roles::app_server::pci {! include profiles::security::base! include profiles::mycorp::users! include profiles::mycorp::os_base! include profiles::pci! include profiles::tomcat! include profiles::our_app! include profiles::shibboleth! include profiles::pci!!

Class[‘profiles::java’]! -> Class[‘profiles::our_app’]! -> Class[‘profiles::shibboleth’]!}

Page 68: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Roles

• Hostnames minus Hiera

• Technology-independent

• Inheritance makes sense (or not)

Page 69: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Workflow

Page 70: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Module

Page 71: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

forge "http://forge.puppetlabs.com"!!

# Modules from the Puppet Forge!mod "puppetlabs/apache"!mod "puppetlabs/ntp"!!

# Modules from Github using various references!mod 'notifyme',! :git => 'git://github.com/glarizza/puppet-notifyme',! :ref => '50c01703b2e3e352520a9a2271ea4947fe17a51f'!!

mod 'profiles',! :git => 'git://github.com/glarizza/puppet-profiles',! :ref => '3611ae4253ff01762f9bda1d93620edf8f9a3b22'

Page 72: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

R10k - Bad name, good robot

1. Ensuring modules based on a Puppetfile 2. Dynamically creating Puppet environments

Page 73: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

PuppetfileManifestHieradata

Control Repository

Page 74: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

PuppetfileManifestHieradata

PuppetfileManifestHieradata

PuppetfileManifestHieradata

Page 75: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

PuppetfileManifestHieradata

PuppetfileManifestHieradata

PuppetfileManifestHieradata

Puppet Environment Puppet Environment

BranchBranch

Page 76: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Demo

Page 77: Doing the Refactor Dance - Making Your Puppet Modules More Modular - PuppetConf 2014

Presented by

Summary• Simple, generic component modules

• Extract company-specific data with Hiera

• Layer implementation with Profiles

• Classification with Profiles