Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Refactoring Puppet

Refactoring Puppet

How do you prevent Bit-rot within Puppet code as complexity grows? This presentation talks about some common code-smells within Puppet and how to apply common programming principles to Infrastructure as Code

View this presentation recorded at https://www.youtube.com/watch?feature=player_embedded&v=foiF3wd9Kn4

James Fryman

April 05, 2013
Tweet

More Decks by James Fryman

Other Decks in Programming

Transcript

  1. 3

  2. 4

  3. 5

  4. 6

  5. 7

  6. 7

  7. 8

  8. err: Could not apply complete catalog: Found 1 dependency cycle:

    (Exec[apt-update] => Package[lvm2] => Class[Lvm::Setup] => Stage[stage3] => Stage[main] => Class[Main] => Exec[apt-update]) 9
  9. err: Could not apply complete catalog: Found 1 dependency cycle:

    (Exec[apt-update] => Package[lvm2] => Class[Lvm::Setup] => Stage[stage3] => Stage[main] => Class[Main] => Exec[apt-update]) 9
  10. 10

  11. err: Could not apply complete catalog: Found 1 dependency cycle:

    (Exec[apt-update] => Package[lvm2] => Class[Lvm::Setup] => Stage[stage3] => Stage[main] => Class[Main] => Exec[apt-update]) 11
  12. err: Could not apply complete catalog: Found 1 dependency cycle:

    (Exec[apt-update] => Package[lvm2] => Class[Lvm::Setup] => Stage[stage3] => Stage[main] => Class[Main] => Exec[apt-update]) 11
  13. 12

  14. 12

  15. 14

  16. 21

  17. 23

  18. 25

  19. 26

  20. 27

  21. $ puppet module \ generate jfryman-octokittens Generating module at ..

    jfryman-octokittens/spec/spec_helper.rb jfryman-octokittens/tests/init.pp 37
  22. $ puppet module \ generate jfryman-octokittens Generating module at ..

    jfryman-octokittens/spec/spec_helper.rb jfryman-octokittens/tests/init.pp Tests Here 37
  23. describe 'logrotate::rule' do let(:title) { 'nginx' } it { should

    include_class('logrotate::setup') } it do should contain_file('/etc/logrotate.d/ nginx').with({ 'ensure' => 'present', 'owner' => 'root', 'group' => 'root', 'mode' => '0444', }) end end 38
  24. describe 'logrotate::rule' do let(:title) { 'nginx' } let(:parameters) {{ :enabled

    => false }} it { should_not include_class('logrotate::setup') it do should contain_file('/etc/logrotate.d/ nginx').with({ 'ensure' => 'absent', }) end end 40
  25. if $::datacenter =~ /ec2/ { $apt_repository = ‘https://hi:[email protected]/’ } else

    { $apt_repository = ‘http://foo.local/’ } file { ‘/etc/apt/sources.list.d/100basho.list’: content => “deb $apt_repository $::lsbdistcodename main”, } 42
  26. internal_package_server = ‘https://hi:[email protected]/’ external_package_server = ‘http://foo.internal/’ context 'on internal nodes'

    do it 'should contain basho package repo' do should contain_file('/etc/apt/sources.list.d/ 100basho.list').with_content(internal_package_server) end end context 'on external nodes' do it 'should contain basho package repo' do should contain_file('/etc/apt/sources.list.d/ 100basho.list').with_content(external_package_server) end end 43
  27. 46

  28. 46

  29. java -jar simian-2.3.33.jar \ -threshold=2 \ ~/puppet/**/*.erb \ ~/puppet/**/*.rb \

    ~/puppet/**/*.pp Found 28542 duplicate lines in 7696 blocks in 1340 files 50
  30. $monitor = $rails_env ? { 'production' => true, 'staging' =>

    true, default => false } # module/lib/facter/should_monitor.rb ... case Facter.value(‘rails_env’) when ‘production’, ‘staging’ true else false end ... Extract 52
  31. # foo/manifests/init.pp class foo($ssl=true) { file { “/etc/foo.conf”: content =>

    template(‘foo/foo.conf.erb’), monitor => should_monitor(), } } if should_monitor() { ... } Extract 53
  32. # foo/manifests/init.pp class foo($ssl=true) { file { “/etc/foo.conf”: content =>

    template(‘foo/foo.conf.erb’), monitor => should_monitor(), } } if should_monitor() { ... } THAT’S READABLE WAT?! Extract 53
  33. # foo/manifests/init.pp class foo($ssl=true) { file { “/etc/foo.conf”: content =>

    template(‘foo/foo.conf.erb’), monitor => should_monitor(), } } if $rnx_prb_alpha() { ... } Extract 54
  34. # foo/manifests/init.pp class foo($ssl=true) { file { “/etc/foo.conf”: content =>

    template(‘foo/foo.conf.erb’), monitor => should_monitor(), } } if $rnx_prb_alpha() { ... } Extract 54
  35. # foo/manifests/init.pp class foo($ssl=true) { file { “/etc/foo.conf”: content =>

    template(‘foo/foo.conf.erb’), monitor => $::should_monitor, } } if $::should_monitor { ... } 55
  36. # foo/manifests/init.pp class foo($ssl=true) { file { “/etc/foo.conf”: content =>

    template(‘foo/foo.conf.erb’), } } # foo/templates/foo.conf.erb <% if @ssl -%> <%= scope.function_template(‘foo/ssl.conf.erb’) %> <% end -%> 57
  37. class ntp ( $options = $ntp::params::defaults, ) inherits ntp::params {

    class { 'ntp::package': options => $options, } -> class { 'ntp::config': options => $options, } ~> class { 'ntp::service': options => $options, } -> Class[‘ntp’] } Move 60
  38. class ntp::params { $defaults = { package => { version

    => ‘latest’, }, config => { servers => [‘pool.ntp.org’], }, } } Move 61
  39. class ntp::params { $defaults = { package => { version

    => hiera(‘ntp_package_version’), }, config => { servers => hiera(‘ntp_servers’), }, } } Move 62
  40. err: Could not apply complete catalog: Found 1 dependency cycle:

    (Exec[apt-update] => Package[lvm2] => Class[Lvm::Setup] => Stage[stage3] => Stage[main] => Class[Main] => Exec[apt-update]) 66
  41. err: Could not apply complete catalog: Found 1 dependency cycle:

    (Exec[apt-update] => Package[lvm2] => Class[Lvm::Setup] => Stage[stage3] => Stage[main] => Class[Main] => Exec[apt-update]) 66
  42. Move Move Extract exec { ‘wget awesome file’: command =>

    ‘wget -O /tmp/file.txt http://git.io/ YHs6eg’, creates => ‘/tmp/file.txt’, } Move 70
  43. Move Move Extract # modules/wget/manifests/download.pp define wget::download ( $source =

    undef, $dest = $name, ) include wget exec { “wget download ${name}”: command => “wget -O ${dest} ${source}” creates => $dest, } } Move 71
  44. 74

  45. 75

  46. 79