using puppet on linux, windows, and mac osx

40
Using puppet on Linux, Windows and Mac OSX Hein Couwet 2improveIT [email protected] @heincouwet

Upload: puppet-labs

Post on 26-Jun-2015

2.730 views

Category:

Technology


1 download

DESCRIPTION

Hein Couwet's presentation "Using Puppet on Linux, Windows, and Mac OSX" from Puppet Camp Ghent 2013.

TRANSCRIPT

Page 1: Using Puppet on Linux, Windows, and Mac OSX

Using puppet on Linux, Windows

and Mac OSXHein [email protected]@heincouwet

Page 2: Using Puppet on Linux, Windows, and Mac OSX

Lessons learned from installing a development

stack on the 3 major operating systems :

Linux, Mac OSX and Windows.

Page 3: Using Puppet on Linux, Windows, and Mac OSX

Context

Page 4: Using Puppet on Linux, Windows, and Mac OSX

installing a development stack @iDalko

• automated

• asap : < 15 minutes

Page 5: Using Puppet on Linux, Windows, and Mac OSX

Installing an integrated stack

• Atlassian tools (JIRA/Confluence/Fisheye/Crucible/Bamboo/Stash)

• Other OpenSource Tools : Sonar / BIRT/ Nexus

• Database MySQL

• Frontend : Apache

Page 6: Using Puppet on Linux, Windows, and Mac OSX

Choice between

• Puppet

• Chef

• scripting : ant / bash / ...

Page 7: Using Puppet on Linux, Windows, and Mac OSX

Why Puppet

• Declarative syntax

• No ‘programming’ skills ...

• so the customer can understand without learning a new language

Page 8: Using Puppet on Linux, Windows, and Mac OSX

Example : Nexus

Page 9: Using Puppet on Linux, Windows, and Mac OSX

Episode I : Ubuntu

Page 10: Using Puppet on Linux, Windows, and Mac OSX

Platform : Ubuntu

• Downloading using wget

• uncompress using tar -zxf

• starting up using linux services

• installing packages mysql and apache

• configuring vproxy in apache

Page 11: Using Puppet on Linux, Windows, and Mac OSX

Download define download($url,$location,$filename) {

exec { "download-$filename":

cwd => "$location",

command => "/usr/bin/wget $url",

creates => "$location/$filename",

logoutput => "on_failure",

timeout => 0,

require => File["$location"],

}

}

Page 12: Using Puppet on Linux, Windows, and Mac OSX

Uncompress define untargz($location,$filename,$creates) {

exec { "untargz":

cwd => "$location",

command => "/bin/tar -zxvf $filename",

creates => "$creates",

logoutput => "on_failure",

timeout => 0,

}

}

Page 13: Using Puppet on Linux, Windows, and Mac OSX

Starting up linux services

file { "/etc/init.d/nexus":

ensure => link,

target => "${nexus_install}/bin/nexus",

}

service { "nexus":

ensure => running,

require => File["/etc/init.d/nexus"],

}

Page 14: Using Puppet on Linux, Windows, and Mac OSX

Installing packages Apache

package { "apache2-mpm-prefork":

ensure =>installed

}

service { "apache2":

enable => true,

ensure => running,

require => Package["apache2-mpm-prefork"],

}

Page 15: Using Puppet on Linux, Windows, and Mac OSX

Lessons learned ...• instead of using

a { “xxx”:

...

}

b { “yyy” :

....

require => A[“xxx”]

}

Page 16: Using Puppet on Linux, Windows, and Mac OSX

Lessons learned ...• start using

a { “xxx”:

...

}

->

b { “yyy” :

....

}

Page 17: Using Puppet on Linux, Windows, and Mac OSX

Episode II : CentOS

Page 18: Using Puppet on Linux, Windows, and Mac OSX

Challenge 1

• Another customer has a CentOS server instead of Ubuntu ...

• can we also use puppet ?

Page 19: Using Puppet on Linux, Windows, and Mac OSX

Problem 1

• other naming of apache packages

• apache2 <-> httpd

Page 20: Using Puppet on Linux, Windows, and Mac OSX

Solution

case    $operatingsystem  {

         "CentOS":    {                $packages  =  [  "httpd"  ]                package  {  $packages:                                    ensure  =>  installed,                }                service  {  "httpd":                                ensure  =>  running,                                require  =>  Package["httpd"],                }            }            "Debian":  {                $packages  =  [  "apache2-­‐mpm-­‐prefork"  ]

               package  {  $packages:                                    ensure  =>  installed,                }                  service  {  "apache2":                                ensure  =>  running,                                require  =>  Package["apache2-­‐mpm-­‐prefork"]                }        }

Page 21: Using Puppet on Linux, Windows, and Mac OSX

Opportunity 1

• start using hiera

Page 22: Using Puppet on Linux, Windows, and Mac OSX

Episode III : OSX

Page 23: Using Puppet on Linux, Windows, and Mac OSX

Why ?

• Just for local testing on my development machine

• ➟ limited scope, no longer apache, service ...

Page 24: Using Puppet on Linux, Windows, and Mac OSX

small adaptations ...• instead of

exec { ...:

command => "/usr/bin/wget $url",

}

• now use

exec { ... :

command => "wget $url",

path => “$::path”,

}

Page 25: Using Puppet on Linux, Windows, and Mac OSX

Episode IV : Windows

Page 26: Using Puppet on Linux, Windows, and Mac OSX

Windows

• Back to basics

• no longer ‘standard’ commands

• no longer unix paths

• limited set of puppet resources available

Page 27: Using Puppet on Linux, Windows, and Mac OSX

Window Paths

Windows file paths must be written in different ways at different times, due to various tools’ conflicting rules for backslash use.

1 Windows file system APIs accept both the backslash (\) and forwardslash (/) to separate directory and file components in a path.

2 Some Windows programs only accept backslashes in file paths.

3 *nix shells and many programming languages — including the Puppet language — use the backslash as an escape character.

As a result, any system that interacts with *nix and Windows systems as equal peers will unavoidably have complicated behavior around backslashes.

The following guidelines will help you use backslashes safely in Windows file paths with Puppet.

Forward Slashes vs. Backslashes

In many cases, you can use forward slashes instead of backslashes when specifying file paths.

Forward slashes MUST be used in:

1 Template paths passed to the template function. For example:    file  {'C:/warning.txt':

2        ensure    =>  present,3        content  =>  template('my_module/warning.erb'),4    }

6 Puppet URLs in a file resource’s source attribute.

Page 28: Using Puppet on Linux, Windows, and Mac OSX

Window PathsForward slashes SHOULD be used in:

1 The title or path attribute of a file resource

2 The source attribute of a package resource

3 Local paths in a file resource’s source attribute

4 The command of an exec resource, unless the executable requires backslashes, e.g. cmd.exe

Forward slashes MUST NOT be used in:

1 The command of a scheduled_task resource.

2 The install_options of a package resource.

The Rule

If Puppet itself is interpreting the file path, forward slashes are okay. If the file path is being passed directly to a Windows program, forward slashes may not be okay.

Using Backslashes in Double-Quoted Strings

Puppet supports two kinds of string quoting. Strings surrounded by double quotes (") allow variable interpretation and many escape sequences (including the common \n for a newline), so care must be taken to prevent backslashes from being mistaken for escape sequences.

When using backslashes in a double-quoted string, you must always use two backslashes for each literal backslash. There are no exceptions and no special cases.

Using Backslashes in Single-Quoted Strings

Strings surrounded by single quotes (') do not allow variable interpretation, and the only escape sequences permitted are \' (a literal single quote) and \\ (a literal backslash).

Lone backslashes can usually be used in single-quoted strings. However:

1 When a backslash occurs at the very end of a single-quoted string, a double backslash must be used instead of a single backslash. For example: path  =>  'C:\Program  Files(x86)\\'

Page 29: Using Puppet on Linux, Windows, and Mac OSX

Window PathsThe Rule

If Puppet itself is interpreting the file path, forward slashes are okay. If the file path is being passed directly to a Windows program, forward slashes may not be okay.

Using Backslashes in Double-Quoted Strings

Puppet supports two kinds of string quoting. Strings surrounded by double quotes (") allow variable interpretation and many escape sequences (including the common \n for a newline), so care must be taken to prevent backslashes from being mistaken for escape sequences.

When using backslashes in a double-quoted string, you must always use two backslashes for each literal backslash. There are no exceptions and no special cases.

Using Backslashes in Single-Quoted Strings

Strings surrounded by single quotes (') do not allow variable interpretation, and the only escape sequences permitted are \' (a literal single quote) and \\ (a literal backslash).

Lone backslashes can usually be used in single-quoted strings. However:

1 When a backslash occurs at the very end of a single-quoted string, a double backslash must be used instead of a single backslash. For example: path  =>  'C:\Program  Files(x86)\\'

2 When a literal double backslash is intended, a quadruple backslash must be used.The Rule

In single-quoted strings:

1 A double backslash always means a literal backslash.

2 A single backslash usually means a literal backslash, unless it is followed by a single quote or another backslash.

Page 30: Using Puppet on Linux, Windows, and Mac OSX

Conclusion: Paths on Windows

• I don’t even want to show you my first attempt

• Too complicated !!!!

Page 31: Using Puppet on Linux, Windows, and Mac OSX

Basic tools doesn’t exist

• tar

• unzip

• wget

Page 32: Using Puppet on Linux, Windows, and Mac OSX

Ruby to the rescue

• Puppet providers /resources

• creating new ones

• replacing wget

• replacing tar / unzip

• programming in Ruby, but still understandable for the customer

Page 33: Using Puppet on Linux, Windows, and Mac OSX

puppet/type/install.rbPuppet::Type.newtype(:install) do

ensurable newparam (:path) do desc "The file name where to install" end

newparam(:uri) do! desc "The uri from where to get the file" isnamevar validate do |value| if value =~ /^http:/ resource[:provider] = :http end end end

newparam(:download) do! desc "The intermediary file as download" end

newparam(:unzip) do desc "Need to unzip" endend

Page 34: Using Puppet on Linux, Windows, and Mac OSX

puppet/provider/install/http.rb

require 'fileutils'require 'net/http'require 'uri'require 'zip/zip'

Puppet::Type.type(:install).provide(:http) do def create path = resource[:path] if resource[:download] temp_path = resource[:download] else temp_path = "#{path}.download" end uri = URI(resource[:uri]) download( uri, temp_path) if resource[:unzip] unzip_file(temp_path,path) else FileUtils.mv temp_path path end! FileUtils.touch "#{temp_path}.done" end

def exists? path = resource[:path] if resource[:download] temp_path = resource[:download] else temp_path = "#{path}.download" end! check_path = "#{temp_path}.done"! File.exist? check_path end

def unzip_file (file, destination) end

def download (uri , path)end end

Page 35: Using Puppet on Linux, Windows, and Mac OSX

Pitfalls

• Use the correct gem

• not zip

• buggy ... :-(

• but rubyzip

Page 36: Using Puppet on Linux, Windows, and Mac OSX

Pitfalls

• Augeas

• not supporting xml with mixed quotes

• not yet supported on puppet - windows

Page 37: Using Puppet on Linux, Windows, and Mac OSX

Ruby to the rescue

• adding providers for

• text replacement

• xml-path replacement

Page 38: Using Puppet on Linux, Windows, and Mac OSX

My Conclusion

• Never use exec

• Core Puppet needs some basic functionality support for ALL platforms such as

• downloading files from the web : wget

• zip / tar

• search/replace in file/xml ...

Page 39: Using Puppet on Linux, Windows, and Mac OSX

My Puppet Opportunities for 2013

• hiera

• Testing my puppet scripts

Page 40: Using Puppet on Linux, Windows, and Mac OSX

Questions ?