Slide 1

Slide 1 text

Test-Driven Infrastructure

Slide 2

Slide 2 text

Slide 3

Slide 3 text

1WORKSTATION SETUP BBB

Slide 4

Slide 4 text

1#LEARNCHEF BBB

Slide 5

Slide 5 text

1BBB

Slide 6

Slide 6 text

1cl.ly/PiLZ sha: 821ecd0 BBB

Slide 7

Slide 7 text

$ knife client list

Slide 8

Slide 8 text

1GITHUB  

Slide 9

Slide 9 text

2CREATE THE COOKBOOK fdd

Slide 10

Slide 10 text

$ knife cookbook create {username}-myface

Slide 11

Slide 11 text

$ knife cookbook create sethvargo-myface

Slide 12

Slide 12 text

fmyface d metadata.rb frecipes d default.rb

Slide 13

Slide 13 text

fmyface d metadata.rb frecipes d default.rb

Slide 14

Slide 14 text

fmyface d metadata.rb frecipes d default.rb

Slide 15

Slide 15 text

fmyface d metadata.rb frecipes d default.rb

Slide 16

Slide 16 text

fmyface d metadata.rb frecipes d default.rb

Slide 17

Slide 17 text

imyface

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

$ (sudo) gem install bundler

Slide 20

Slide 20 text

u gist.github.com/5737141

Slide 21

Slide 21 text

fmyface d Gemfile

Slide 22

Slide 22 text

Gemfile source 'https://rubygems.org' group :test do gem 'chefspec', '~> 1.3' gem 'foodcritic', '~> 2.1' gem 'strainer', '~> 3.0' gem 'test-kitchen', '~> 1.0.0.alpha' gem 'kitchen-lxc', '~> 0.0.1.beta1' gem 'knife-spork', '~> 1.0.17' gem 'hipchat', '~> 0.10.0' gem 'guard', '~> 1.8' gem 'guard-foodcritic', '~> 1.0' gem 'guard-rspec', '~> 3.0' end

Slide 23

Slide 23 text

$ (sudo) bundle

Slide 24

Slide 24 text

fmyface d spec_helper.rb fspec d default_spec.rb

Slide 25

Slide 25 text

fmyface d spec_helper.rb fspec d default_spec.rb

Slide 26

Slide 26 text

fmyface d spec_helper.rb fspec d default_spec.rb

Slide 27

Slide 27 text

spec/default_spec.rb require 'spec_helper' describe 'sethvargo-myface::default' do let(:chef_run) do run = ChefSpec::ChefRunner.new(platfrom: 'ubuntu', version: '12.04') run.converge('sethvargo-myface::default') end it 'installs apache2' do expect(chef_run).to install_package('apache2') end # ... end

Slide 28

Slide 28 text

fmyface d spec_helper.rb fspec d default_spec.rb

Slide 29

Slide 29 text

spec/spec_helper.rb require 'chefspec'

Slide 30

Slide 30 text

fmyface d Guardfile d Strainerfile

Slide 31

Slide 31 text

fmyface d Guardfile d Strainerfile

Slide 32

Slide 32 text

Guardfile guard :rspec, all_on_start: false do watch(%r{^spec/.+_spec\.rb$}) watch(%r{^(recipes)/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { 'spec' } end guard :foodcritic, cookbook_paths: '.', all_on_start: false do watch(%r{attributes/.+\.rb$}) watch(%r{providers/.+\.rb$}) watch(%r{recipes/.+\.rb$}) watch(%r{resources/.+\.rb$}) end

Slide 33

Slide 33 text

$ bundle exec guard

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

⌘ + S

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

0.0134s

Slide 39

Slide 39 text

fmyface d Guardfile d Strainerfile

Slide 40

Slide 40 text

Strainerfile knife: bundle exec knife cookbook test $COOKBOOK foodcritic: bundle exec foodcritic $SANDBOX/$COOKBOOK -f any rspec: (cd $COOKBOOK && bundle exec rspec --color)

Slide 41

Slide 41 text

fmyface d metadata.rb frecipes d default.rb

Slide 42

Slide 42 text

fmyface d metadata.rb frecipes d default.rb

Slide 43

Slide 43 text

package 'apache2' service 'apache2' do action [:enable, :start] end recipes/default.rb

Slide 44

Slide 44 text

⌘ + S

Slide 45

Slide 45 text

POLICY CHANGE G

Slide 46

Slide 46 text

RUN UNIT TESTS MASTER BEFORE MERGING TO

Slide 47

Slide 47 text

! RUN UNIT TESTS MASTER BEFORE MERGING TO

Slide 48

Slide 48 text

u HUMAN (PROS) D J h a K

Slide 49

Slide 49 text

7 r c L M t HUMAN (CONS)

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

1CREATE REPO

Slide 56

Slide 56 text

1GITHUB INTERFACE ?

Slide 57

Slide 57 text

1 myface T

Slide 58

Slide 58 text

1 (anything) T

Slide 59

Slide 59 text

1Create repository J

Slide 60

Slide 60 text

1

Slide 61

Slide 61 text

1

Slide 62

Slide 62 text

$ git add . $ git commit -m "myface" $ git push

Slide 63

Slide 63 text

1

Slide 64

Slide 64 text

2SETUP JOB

Slide 65

Slide 65 text

S

Slide 66

Slide 66 text

I, _________, solemnly swear under penalty of my GPU catching fire, that I will not touch anyone's build but my own.  NO ASSHOLE PLEDGE

Slide 67

Slide 67 text

2ci.trychef.com O

Slide 68

Slide 68 text

2 New Job J

Slide 69

Slide 69 text

2

Slide 70

Slide 70 text

2{username}-myface T

Slide 71

Slide 71 text

2Build a free-style... J

Slide 72

Slide 72 text

2 OK J

Slide 73

Slide 73 text

2

Slide 74

Slide 74 text

2(github URL) T

Slide 75

Slide 75 text

2 Git J

Slide 76

Slide 76 text

2git://github.com/... T

Slide 77

Slide 77 text

2Build when a change... J

Slide 78

Slide 78 text

2Color ANSI Console... J

Slide 79

Slide 79 text

2Add Build Step J

Slide 80

Slide 80 text

2Execute Shell J

Slide 81

Slide 81 text

2 bundle install --path ../../support/vendor bundle exec strainer test T

Slide 82

Slide 82 text

2Email Notification J

Slide 83

Slide 83 text

2(your email) T

Slide 84

Slide 84 text

2 Save J

Slide 85

Slide 85 text

2

Slide 86

Slide 86 text

2 Build Now J

Slide 87

Slide 87 text

2 (the build) J

Slide 88

Slide 88 text

3SETUP HOOK

Slide 89

Slide 89 text

3 Settings J

Slide 90

Slide 90 text

3

Slide 91

Slide 91 text

3Service Hooks J

Slide 92

Slide 92 text

3 Jenkins (GitHub Plugin) J

Slide 93

Slide 93 text

3http://ci.trychef.com/github-webhook/ T

Slide 94

Slide 94 text

3http://ci.trychef.com/github-webhook/ T IMPORTANT

Slide 95

Slide 95 text

3 Active J

Slide 96

Slide 96 text

3Update settings J

Slide 97

Slide 97 text

$ git checkout -b add_site

Slide 98

Slide 98 text

fmyface d spec_helper.rb fspec d default_spec.rb

Slide 99

Slide 99 text

spec/default_spec.rb describe 'sethvargo-myface::default' do # pre-existing tests # ... it 'creates the default template' do expect(chef_run).to create_file('/var/www/index.html') end it 'creates the site with the correct content' do template = chef_run.template('/var/www/index.html') expect(template.owner).to eq('root') expect(template.group).to eq('root') end end

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

fmyface d metadata.rb frecipes d default.rb

Slide 102

Slide 102 text

recipes/default.rb package 'apache2' service 'apache2' do action [:enable, :start] end template '/var/www/index.html' do owner 'root' group 'root' mode '0755' source 'index.html.erb' end

Slide 103

Slide 103 text

fmyface ftemplates d index.html.erb fdefault

Slide 104

Slide 104 text

templates/default/index.html.erb Welcome to <%= node['fqdn'] %>

Here's everything you need to know about <%= node['fqdn'] %>:

<%= JSON.pretty_generate(node.to_hash) %>

Slide 105

Slide 105 text

$ git add . $ git commit -m "Write out default site"

Slide 106

Slide 106 text

$ git push origin add_site

Slide 107

Slide 107 text

Slide 108

Slide 108 text

Pull Request J

Slide 109

Slide 109 text

Slide 110

Slide 110 text

Send pull request J

Slide 111

Slide 111 text

Slide 112

Slide 112 text

c

Slide 113

Slide 113 text

 Merge pull request J

Slide 114

Slide 114 text

J

Slide 115

Slide 115 text

J (the build) J

Slide 116

Slide 116 text

JConsole Output J

Slide 117

Slide 117 text

J

Slide 118

Slide 118 text

J

Slide 119

Slide 119 text

J

Slide 120

Slide 120 text

LET'S FIX THAT Y

Slide 121

Slide 121 text

$ git pull origin master

Slide 122

Slide 122 text

metadata.rb name 'myface' maintainer 'YOUR_COMPANY_NAME' maintainer_email 'YOUR_EMAIL' license 'All rights reserved' description 'Installs/Configures myface' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version '0.1.0'

Slide 123

Slide 123 text

metadata.rb name 'myface' maintainer 'Seth Vargo' maintainer_email '[email protected]' license 'All rights reserved' description 'Installs/Configures myface' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version '0.1.0'

Slide 124

Slide 124 text

$ git add metadata.rb $ git commit -m "Fix metadata"

Slide 125

Slide 125 text

$ git push origin master

Slide 126

Slide 126 text

AWESOME

Slide 127

Slide 127 text

$ knife cookbook upload myface

Slide 128

Slide 128 text

S

Slide 129

Slide 129 text

WHY?

Slide 130

Slide 130 text

1 WE HAVE TESTS

Slide 131

Slide 131 text

1 WE HAVE TESTS 2 WE HAVE JENKINS

Slide 132

Slide 132 text

1 WE HAVE TESTS 2 WE HAVE JENKINS 3 WE HAVE THE TECHNOLOGY

Slide 133

Slide 133 text

$ knife cookbook upload myface

Slide 134

Slide 134 text

$ knife cookbook upload myface G

Slide 135

Slide 135 text

$ knife cookbook upload myface G M

Slide 136

Slide 136 text

J Configure J

Slide 137

Slide 137 text

J

Slide 138

Slide 138 text

J

Slide 139

Slide 139 text

JAdd post-build action J

Slide 140

Slide 140 text

JPost build task J

Slide 141

Slide 141 text

J

Slide 142

Slide 142 text

J Strainer marked build OK T

Slide 143

Slide 143 text

J bundle exec knife cookbook upload {username}-myface T

Slide 144

Slide 144 text

J Save J

Slide 145

Slide 145 text

J

Slide 146

Slide 146 text

J Build Now J

Slide 147

Slide 147 text

J

Slide 148

Slide 148 text

J (the build) J

Slide 149

Slide 149 text

JConsole Output J

Slide 150

Slide 150 text

J

Slide 151

Slide 151 text

J > knife cookbook list | grep sethvargo-myface sethvargo-myface 0.1.0

Slide 152

Slide 152 text

AWESOME

Slide 153

Slide 153 text

R

Slide 154

Slide 154 text

WE KNOW WHEN OUR BUILD FAILS BECAUSE WE GET AN EMAIL FROM JENKINS

Slide 155

Slide 155 text

BUT DO WE KNOW WHEN OUR COOKBOOKS ARE UPLOADED TO OUR CHEF SERVER?

Slide 156

Slide 156 text

BUT DO WE KNOW WHEN OUR COOKBOOKS ARE UPLOADED TO OUR CHEF SERVER?

Slide 157

Slide 157 text

 jonlives/knife-spork

Slide 158

Slide 158 text

jonlives/knife-spork

Slide 159

Slide 159 text

jonlives/knife-spork

Slide 160

Slide 160 text

J Configure J

Slide 161

Slide 161 text

J

Slide 162

Slide 162 text

J

Slide 163

Slide 163 text

J

Slide 164

Slide 164 text

J bundle exec knife spork upload {username}-myface T

Slide 165

Slide 165 text

J Escalate script execution... J

Slide 166

Slide 166 text

J Save J

Slide 167

Slide 167 text

q hipchat.com/sign_in

Slide 168

Slide 168 text

q Launch the web app J

Slide 169

Slide 169 text

qTry Chef J

Slide 170

Slide 170 text

q

Slide 171

Slide 171 text

J Build Now J

Slide 172

Slide 172 text

q

Slide 173

Slide 173 text

N THERE'S MORE!

Slide 174

Slide 174 text

N THERE'S MORE!

Slide 175

Slide 175 text

N TEST KITCHEN!

Slide 176

Slide 176 text

fmyface d .kitchen.yml

Slide 177

Slide 177 text

.kitchen.yml driver_plugin: lxc driver_config: use_sudo: true platforms: - name: ubuntu-12.04 driver_config: base_container: ubuntu_12.04 username: ubuntu password: ubuntu suites: - name: default run_list: ["recipe[myface]"] attributes: {}

Slide 178

Slide 178 text

Strainerfile knife: bundle exec knife cookbook test $COOKBOOK foodcritic: bundle exec foodcritic $SANDBOX/$COOKBOOK -f any rspec: (cd $COOKBOOK && bundle exec rspec --color) kitchen: (cd $COOKBOOK && bundle exec kitchen test)

Slide 179

Slide 179 text

metadata.rb name 'myface' maintainer 'Seth Vargo' maintainer_email '[email protected]' license 'All rights reserved' description 'Installs/Configures myface' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version '0.1.0'

Slide 180

Slide 180 text

metadata.rb name 'myface' maintainer 'Seth Vargo' maintainer_email '[email protected]' license 'All rights reserved' description 'Installs/Configures myface' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version '0.1.1'

Slide 181

Slide 181 text

$ git add . $ git commit -m "Add Test Kitchen"

Slide 182

Slide 182 text

$ git push origin master

Slide 183

Slide 183 text

q

Slide 184

Slide 184 text

No content

Slide 185

Slide 185 text

qUH OH!

Slide 186

Slide 186 text

$ git revert HEAD

Slide 187

Slide 187 text

$ git revert HEAD

Slide 188

Slide 188 text

some_recipe.rb template '/etc/foo/bar' do owner 'root' group 'root' source 'bar.erb' end

Slide 189

Slide 189 text

some_recipe.rb template '/etc/foo/bar' do owner 'root' group 'root' source 'bar.erb' end IF WE REMOVE THIS WILL THE FILE BE DELETED?

Slide 190

Slide 190 text

some_recipe.rb template '/etc/foo/bar' do owner 'root' group 'root' source 'bar.erb' end NO

Slide 191

Slide 191 text

FAST FORWARD CHANGES ARE ABSOLUTELY ALWAYS BETTER THAN REVERTING

Slide 192

Slide 192 text

some_recipe.rb template '/etc/foo/bar' do owner 'root' group 'root' source 'bar.erb' end

Slide 193

Slide 193 text

some_recipe.rb template '/etc/foo/bar' do owner 'root' group 'root' source 'bar.erb' action :delete end

Slide 194

Slide 194 text

$ git add . $ git commit -m "Revert abc123" $ git push origin master

Slide 195

Slide 195 text

chef + environments safer infrastructure

Slide 196

Slide 196 text

No content

Slide 197

Slide 197 text

No content

Slide 198

Slide 198 text

No content

Slide 199

Slide 199 text

No content

Slide 200

Slide 200 text

environments/production.json { "name" : "production", "description" : "Production cluster in EC2", "override_attributes" : { ... }, "default_attributes" : { ... } }

Slide 201

Slide 201 text

environments/production.json { "name" : "production", "description" : "Production cluster in EC2", "override_attributes" : { ... }, "default_attributes" : { ... }, "cookbook_versions" : { "myface": "0.1.0" }, }

Slide 202

Slide 202 text

CD

Slide 203

Slide 203 text

CD PUSH

Slide 204

Slide 204 text

CD PUSH

Slide 205

Slide 205 text

Slide 206

Slide 206 text

youtube.com/watch?v=UMnZiTL0tUc pdiffs: travis-ci.org Travis CI: