Post on 24-Jun-2015
Managing Resources and their Providers
Resources and their Providers by Miah Johnson is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Hi, I'm Miah!miah@chia-pet.org@miah_
Operations Engineer @ HotelTonight
I <3 automation, learning to solve all the problems, and look forward to the singularity.
1st Generation Cookbook Pattern
nginx/recipes/default.rb:
package 'nginx'service 'nginx'
nginx/recipes/default.rb:
include_recipe 'nginx::ohai_plugin' case node['nginx']['install_method']when 'source' include_recipe 'nginx::source'when 'package' case node['platform'] when 'redhat','centos','scientific','amazon','oracle' if node['nginx']['repo_source'] == 'epel' include_recipe 'yum::epel' else include_recipe 'nginx::repo' end end package node['nginx']['package_name'] service 'nginx' do supports :status => true, :restart => true, :reload => true action :enable end include_recipe 'nginx::commons'end
This isn't flexible.
● What if I want more than one nginx instance?
● What if I want to change something?● Code layout can make debugging difficult.● Often have embedded search.● Many code paths to test.● Over 9000 attributes.● Limited to existing resources.
2nd Generation Composable Pattern
The 'composable' recipe
nginx/recipes/default.rb:
include_recipe 'nginx::_group'include_recipe 'nginx::_user.rb'include_recipe 'nginx::_server_install_from_source.rb'include_recipe 'nginx::_server_config'include_recipe 'nginx::_server_runit.rb'
More flexible but.. ?
● Still limited to single instance● Have to wrap or fork to include local
customizations.corp_nginx/recipes/default.rb:
include_recipe 'nginx::_group'include_recipe 'nginx::_user.rb'include_recipe 'nginx::_server_config'include_recipe 'nginx::_server_install_from_source.rb'include_recipe 'corp_nginx::_server_bluepill.rb'
3rd Generation Library Pattern
nginx_instance 'example.com' do path '/opt/example.com' user 'example' port 8080 server_alias 'www.example.com' action [:create, :enable]end
Most flexible.
● Resources have unique names (yay instances).
● Resource attributes can have default values (less node attributes).
● Stable Public Interface.
A Provider can have many actions.
● :create● :delete● :enable● :disable● :modify● :deploy● :rollback● :run● :nothing
Providers can be overridden
nginx_instance 'example.com' do provider 'Chef::Provider::CorpNginx::Instance'end
Wrap it up!
nginx/recipes/default.rb:
sites = data_bag('sites')
sites.each do |instance| nginx_instance instance['name'] do port instance['port'] endend
Thanks!
● Mathieu Sauve-Frankel @kisoku● Ashe Dryden @ashedryden● Julie Pagano @juliepagano● Julia Elman @juliaelman● Lindsey Bieda @lindseybieda
Questions?