and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London...

23
Back From The Brink - Can we Refactor Cookbooks and Nodes? Hemal Varambhia AbelsonInfo 4th November 2014

Transcript of and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London...

Page 1: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

Back From The Brink - Can we

Refactor Cookbooks and Nodes?

Hemal VarambhiaAbelsonInfo

4th November 2014

Page 2: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

Me

● @hnvarambhia● github.com/hemalvarambhia● RoR, Android developer and Chef at

AbelsonInfo

Page 3: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

Who We Are

● AbelsonInfo are a live sports data company - we provide JSON and XML APIs

● We mainly develop RoR (Ruby on Rails) applications

● Infrastructure managed by Chef (Hosted Chef)

Page 4: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

● Using Cookbooks from the community● Put together ‘just get something to work!’● Hard to read● Untested?

Where We Were

Page 5: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

● We needed to provision a production-like environment - CentOS

Then Came The Change

Page 6: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

● 29 ubuntu servers already managed by Chef (10.x)

● Too afraid to refactor - little idea of the consequences

● Infracode Smells

The Situation

Page 7: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

● What did the servers really require?

Buy The Ingredients

Page 8: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

● Created a new Chef organisation● The legacy cookbooks under the old

organisation were still ‘working’● Wanted to develop our own cookbooks

A Clean Chopping Board

Page 9: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

● Created a new Chef organisation● The legacy cookbooks under the old

organisation were still ‘working’● Wanted to develop our own cookbooks● TDD from the start - refactoring is a very

important step● Infracode will feed back into the business

and vice versa

A Clean Chopping Board

Page 10: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

● We made sure to keep our cookbook code AND tests clean

● Could we apply this to community cookbooks?

● consistent code style

Clean Infracode

Page 11: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

● We bootstrapped a new node under a new organisation to make sure it was successful - ran serverspec on the node

● Performed a dry run of moving a node to the new organisation

● A real node refactoring - the server should still run the rails app! Only 28 more to go!

Refactoring Nodes

Page 12: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

● Design decisions are as important in DevOps● Discovered the need for an xUnit pattern!● Value of unit testing is in confirming guard clauses

and configuration published in template resources● express the tests in the language of the tool●

What We Learned

Page 13: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

"Habitability is the characteristic of source code that enables programmers, coders, bug-fixers, and people coming to the code later in its life to understand its construction and intentions and to change it comfortably and confidently." (Richard Gabriel http://c2.com/cgi/wiki?EndUserHabitability)

Cookbook Quality?

Page 14: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

I will show some code samples demonstrating these points

Here’s What We Made Earlier

Page 15: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

# Determine which apps require the 'app' tier to be set up on this node:

tier_apps = ai_stack.apps_for_tier(node, 'app')

# Initialize an array to store user names that will later be added to the

# "sshlogin" group:

sshlogin_users = []

# Perform configuration required by all apps:

unless tier_apps.empty?

# Ensure all dependencies necessary to build Ruby are present:

include_recipe 'build-essential'

# Install packages useful for apps that use the asset pipeline functionality.

include_recipe 'ai_stack::asset_pipeline'

Here’s What We Made Earlier

Page 16: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

#

# Cookbook Name:: greyhounds

# Recipe:: default

#

# Copyright 2014, AbelsonInfo

#

# All rights reserved - Do Not Redistribute

#

include_recipe 'rails_app_server::default'

nginx

varnish

postgresql

install_ruby

Here’s What We Made Earlier

Page 17: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

Clean Tests - Domain AssertionsRSpec::Matchers.define :permit do |ip_addresses|

match do |content|

escaped_url = Regexp.escape @url

permitted_ip_addresses = ip_addresses.select { |ip_address|

content=~/location #{escaped_url} \{[^}]+allow #{ip_address};/m

}

permitted_ip_addresses == ip_addresses

end

chain :access_to do |url|

@url = url

end

failure_message_for_should do |content|

"expected Nginx to permit #{ip_addresses.join(", ")} access to '#{@url}'"

end

end

Page 18: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

describe "AI office" do

describe "/feed URL" do

its(:content) {

should permit(client_machines).access_to("/feed")

}

end

end

Clean Tests - Usage

Page 19: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

● Refactoring Katas - Take a cookbook from the supermarket and treat it as a refactoring kata

● TDD ping pong (with a dev) - try a ruby refactoring.

● Next meetups - examples of exemplary code

Ideas From The Dev Community

Page 20: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

● cleancoders.com - episodes 21 - 24 are about clean TDD

● https://github.com/emilybache - some nice refactoring katas here to get going

● Ruby Refactoring Book (co-authors by Martin Fowler)● POODR (Sandi Metz)● xUnit Test Patterns by Martin Mezaros

Useful Resources

Page 21: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

Who’s For Dessert?

Page 22: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

This man was whipping up awesome at atomic scales.

Now For Dessert

Page 23: and Nodes? Refactor Cookbooks Brink - Can we Back From The ...files.meetup.com/15470562/Chef London Meetup Presentation.pdf · # Determine which apps require the 'app' tier to be

Thanks to: 1. Tim Mackinnon2. Engineering Team @ AbelsonInfo3. Alex G. Harvey

Acknowledgements