Starting with Chef or Puppet as a System Administrator will lead you to a problem where you are not sure what’s the best solution of a problem in terms of software architecture.
We will give you a brief overview of general well known and battle tested software patterns, which also applies to infrastructure management code. In addition, we‘ll also show Antipatterns, and best practices.
14. OSDC 2014
node web-1 {
$role = 'web-app-a'
$has_apache = true;
include php5
}
node web-2 {
$role = 'web-app-b'
$has_fcgi=true
include php5
}
# has both enabled, but needs a special config
node web-3 {
$role = 'web-app-c'
$has_fcgi=true
$has_apache=true
include php5
}
Web
App 1
Web
App 2
Web
App 3
15. OSDC 2014
class php5 {
if ($::has_fcgi)
package { 'php5-cli' : ensure => installed }
}
if ($::has_apache) {
package { 'php5-apache' : ensure => installed }
}
if ($::role == 'web-app-c')
package { 'php5-web-app-c-special-module' }
file { '/etc/php5/php.ini' :
source => 'puppet:///modules/php5/php.ini-web-app-c'
}
}
}
Global variable!
Out of Context!
No abstraction
28. OSDC 2014
describe :node => 'web-1' do
it { should contain_package ('php5-fcgi') }
end
describe :node => 'web-2' do
it { should contain_package ('php5-apache2' ) }
end
describe :node => 'web-3' do
it { should contain_package ('php5-fcgi') }
it { should contain_package ('php5-apache2' ) }
end
Test php5 class:
Install Package
?
29. OSDC 2014
describe :class => 'php5::fcgi' do
it { should contain_package('php5-cli') }
end
describe :class => 'php5::apache' do
it { should contain_package('php5-apache2') }
end
class php5::fgci {
package { 'php5-cgi' : ensure => installed }
}
class php5::apache {
package { 'php5-apache' : ensure => installed }
}
31. OSDC 2014
describe :node => 'web-1' do
it { should include_class('php5::fcgi') }
it { should contain_package('php5-fcgi') }
end
describe :node => 'web-2' do
it { should include_class('php5::apache') }
it { should contain_package('php5-apache2') }
end
node web-1 {
include php5::apache
}
node web-2 {
include php5::fcgi
}
Tests
Code
35. OSDC 2014
new
requirements...
now web app 1 needs a second
node
node web-1 {
$role = 'web-app-a'
include php5::apache
}
node web-1a {
$role = 'web-app-a'
include php5::apache
}
38. OSDC 2014
class role::web-app-a {
include php5::apache
}
node web-1 {
include role::web-app-
a
}
node web-1a {
include role::web-app-
a
}
Our new role!
And nodes just
include it!
43. OSDC 2014
class role::web-app-c::special-php-stuff {
package { 'php5-web-app-c-special-module' }
file { '/etc/php5/php.ini' :
source => 'puppet:///modules/php5/php.ini-web-app-c'
}
}
1. Split out the special case into its own class.
44. OSDC 2014
class php5::fcgi($include_after_package_install = undef) {
package { 'php5-fcgi' : ensure => installed }
if ($include_after_package_install)
include $include_after_package_install
Package['php5-fcgi'] -> Class[$include_after_package_install]
}
}
2. Make the special case pluggable and
reusable.
2a. And name it abstract.
45. OSDC 2014
class role::web-app-c {
include php5::apache
class { 'php5::fcgi' :
include_after_package_install => 'profile::web-app-c::special-php-stuff'
}
}
3. Pass the special case as dependency in our
web-app-c.