Slide 1

Slide 1 text

Refactoring Puppet 1

Slide 2

Slide 2 text

HAS THIS HAPPENED TO YOU? 2

Slide 3

Slide 3 text

3

Slide 4

Slide 4 text

Rockin’ It! 3

Slide 5

Slide 5 text

4

Slide 6

Slide 6 text

5

Slide 7

Slide 7 text

WIDGET 3000! 5

Slide 8

Slide 8 text

WIDGET 3000! Amazing! 5

Slide 9

Slide 9 text

WIDGET 3000! Features! Amazing! 5

Slide 10

Slide 10 text

WIDGET 3000! Features! Social! Amazing! 5

Slide 11

Slide 11 text

WIDGET 3000! Features! Social! Scale! Amazing! 5

Slide 12

Slide 12 text

6

Slide 13

Slide 13 text

7

Slide 14

Slide 14 text

7

Slide 15

Slide 15 text

8

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

10

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

12

Slide 22

Slide 22 text

12

Slide 23

Slide 23 text

James Fryman @jfryman 13

Slide 24

Slide 24 text

14

Slide 25

Slide 25 text

Why Refactor? 15

Slide 26

Slide 26 text

Why Fix your car? 16

Slide 27

Slide 27 text

Why Go To The Doctor? 17

Slide 28

Slide 28 text

Why Drink Beer!? 18

Slide 29

Slide 29 text

Why Refactor? 19

Slide 30

Slide 30 text

Why Refactor? 19

Slide 31

Slide 31 text

Why Refactor? Improve Readability 19

Slide 32

Slide 32 text

Why Refactor? Improve Readability Reduce Bugs 19

Slide 33

Slide 33 text

Why Refactor? Improve Readability Reduce Bugs Improve Velocity 19

Slide 34

Slide 34 text

Why Refactor? Improve Readability Reduce Bugs Improve Velocity 19

Slide 35

Slide 35 text

JUST DO IT 20

Slide 36

Slide 36 text

21

Slide 37

Slide 37 text

Code Reuse 21

Slide 38

Slide 38 text

Code Reuse Isolate Change 21

Slide 39

Slide 39 text

Iteration 22

Slide 40

Slide 40 text

23

Slide 41

Slide 41 text

SHIP IT 24

Slide 42

Slide 42 text

25

Slide 43

Slide 43 text

26

Slide 44

Slide 44 text

Abstract 26

Slide 45

Slide 45 text

Abstract Refactor as you go 26

Slide 46

Slide 46 text

Abstract Refactor as you go Refactor Refactors 26

Slide 47

Slide 47 text

27

Slide 48

Slide 48 text

Level 1: Hand Edited 28

Slide 49

Slide 49 text

Level 2: Central Location 29

Slide 50

Slide 50 text

Level 3: Configuration Management 30

Slide 51

Slide 51 text

Level 4: Templates 31

Slide 52

Slide 52 text

Level 5: Data Driven 32

Slide 53

Slide 53 text

Tools 33

Slide 54

Slide 54 text

rspec- puppet 34

Slide 55

Slide 55 text

@rodjek 35

Slide 56

Slide 56 text

I TESTS HATE LOVE I 36

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

Write Negative Tests 39

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

Test Flow Control 41

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

vagrant 44

Slide 66

Slide 66 text

@mitchellh 45

Slide 67

Slide 67 text

46

Slide 68

Slide 68 text

46

Slide 69

Slide 69 text

Code Smells 47

Slide 70

Slide 70 text

DRY 48

Slide 71

Slide 71 text

Extract Move 49

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

Common 51

Slide 74

Slide 74 text

Common 51

Slide 75

Slide 75 text

Common 51

Slide 76

Slide 76 text

Common Duplicated Logic 51

Slide 77

Slide 77 text

Common Duplicated Logic Duplicated Config 51

Slide 78

Slide 78 text

Common Duplicated Logic Duplicated Config Duplicated Tests 51

Slide 79

Slide 79 text

$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

Slide 80

Slide 80 text

# 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

Slide 81

Slide 81 text

# 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

Slide 82

Slide 82 text

# 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

Slide 83

Slide 83 text

# 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

Slide 84

Slide 84 text

# 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

Slide 85

Slide 85 text

DRY Partials with Templates 56

Slide 86

Slide 86 text

# 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

Slide 87

Slide 87 text

MVC Move 58

Slide 88

Slide 88 text

Move 59

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

class ntp::params { $defaults = { package => { version => hiera(‘ntp_package_version’), }, config => { servers => hiera(‘ntp_servers’), }, } } Move 62

Slide 92

Slide 92 text

Modularize 63

Slide 93

Slide 93 text

DRY Long Classes 64

Slide 94

Slide 94 text

require => Class[‘ntp’] require => File[‘/etc/ntp.conf’] Move Extract 65

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

Manifest Task Purpose 67

Slide 98

Slide 98 text

Organize By Function Move Extract 68

Slide 99

Slide 99 text

Organize By Function Move Extract 68

Slide 100

Slide 100 text

Organize By Function Move Extract 68

Slide 101

Slide 101 text

Share 69

Slide 102

Slide 102 text

Move Move Extract exec { ‘wget awesome file’: command => ‘wget -O /tmp/file.txt http://git.io/ YHs6eg’, creates => ‘/tmp/file.txt’, } Move 70

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

Move Move Extract Move Move Move Extract wget::download { ‘/tmp/file.txt’: source => ‘http://git.io/YHs6eg’, } Move 72

Slide 105

Slide 105 text

Modularize Move 73

Slide 106

Slide 106 text

74

Slide 107

Slide 107 text

75

Slide 108

Slide 108 text

Move frymanet.com mysql nginx rails ruby common admin package repos 76

Slide 109

Slide 109 text

Wrap Up 77

Slide 110

Slide 110 text

Why Refactor? 78

Slide 111

Slide 111 text

79

Slide 112

Slide 112 text

Improve Readability 79

Slide 113

Slide 113 text

Improve Readability Reduce Bugs 79

Slide 114

Slide 114 text

Improve Readability Reduce Bugs Improve Velocity 79

Slide 115

Slide 115 text

Improve Readability Reduce Bugs Improve Velocity 79

Slide 116

Slide 116 text

Level Up 80

Slide 117

Slide 117 text

@jfryman jamesfryman/freenode [email protected] 81

Slide 118

Slide 118 text

fin 82