Migrating to Puppet 4.0

65
Migrating to Puppet 4.0

Transcript of Migrating to Puppet 4.0

Page 1: Migrating to Puppet 4.0

Migrating to Puppet 4.0

Page 2: Migrating to Puppet 4.0
Page 3: Migrating to Puppet 4.0

0.27

Page 4: Migrating to Puppet 4.0

2.7.x

Page 5: Migrating to Puppet 4.0

3.x

Page 6: Migrating to Puppet 4.0

3.x future parser

Page 7: Migrating to Puppet 4.0

4.x

Page 8: Migrating to Puppet 4.0
Page 9: Migrating to Puppet 4.0

Puppet 4.0 Language

[email protected]

@hel

Page 10: Migrating to Puppet 4.0

notice 1 => 1notice 1 + 2 => syntax errornotice(1 + 2) => 3notice 1,2,3 => 1 2 3notice [1,2,3] => 1 2 3notice [1,2],3 => 1 2 3notice 5 in [5] => truenotice (4+1) in [5] => error, not a stringnotice 5 in [4+1] => falsenotice 5.0 in [5] => falsenotice(0xff =~ /5/) => falsenotice((0xfe+1) =~/5/) => truenotice { a=>10 } => error, no title

Page 11: Migrating to Puppet 4.0

Godzilla on TypeJuggling….

Page 12: Migrating to Puppet 4.0

Puppet 4.0• Language Cleanup– Sanity, Principle of Least Surprise– Expressions, Expressions, Expressions

• Features– Misc enhancements– Resource Expression Features– Iteration– Type System / Optional Typing– Embedded Puppet Templates (EPP)– Heredoc– Data in Modules and Environments

Page 13: Migrating to Puppet 4.0

Cleanup

• Language Specification – yes we have one– https://github.com/puppetlabs/puppet-specifications

• Numbers are Numbers• No magic to-string for =~ !~ in• Upper case bare words are Type References• Empty string is not undef (and thus "thruty")• Interpolation follows specified rules• +=, []= removed, no more mutation

Page 14: Migrating to Puppet 4.0

Migrating

Page 15: Migrating to Puppet 4.0

Signs Your Goldfish is Dead

– Goldfish skin easily cracks when flexed

– Goldfish is completely dried out

– Eyes of goldfish are concave instead of convex

– Pupils of goldfish are gray– Body parts of the goldfish

are clearly missing

Page 16: Migrating to Puppet 4.0

4.0

+ future

3.8

2.7

Page 17: Migrating to Puppet 4.0

To 4.x from < 3.8 no parser=future• First Migrate to latest 3.8.x– Coming from < 3.0 (released 2012)

• Dynamic Scoping removed • Address Deprecations

– Switch to Directory Environments• Legacy Environments removed in 4.x

– Use PE Catalog Preview with same code in two environments • (demos on youtube)• Turn on future parser in one to help with migration to 4.x Language• FOSS users can use similar free catalog compare tools (with more manual work)

– Do changes in backwards compatible way– Update modules– Gradually migrate nodes – "future parser" production safe

Page 18: Migrating to Puppet 4.0

To 4.x from 3.8 with parser=future

• Relatively painless– Standup a new 4.x master

• Packaging and paths changed in 4.x– The "all in one" agent– Same Ruby everywhere– New location for config files, hiera data etc.

• Enjoy new PE 2015.3 / 4.3 features– Application Management– Data in Modules

Page 19: Migrating to Puppet 4.0

Migrating to Puppet 4.0

Page 20: Migrating to Puppet 4.0

Language Changes

Page 21: Migrating to Puppet 4.0

concat/append/merge/delete 1(6)• Concatenate Arrays with +

[1,2,3] + [4,5,6] => [1,2,3,4,5,6]

• Merge Hashes with +{a=>1} + {b=>2} => {a=>1, b=>2}

• Delete with -[1,2,3] – [2, 3] => [1]{a=>1, b=>2, c=>3} – [a,c] => {b=>2}

• Append to Array with <<[1,2,3] << 4 => [1,2,3,4]

[1,2,3] << [4,5,6] => [1,2,3,[4,5,6]]

Page 22: Migrating to Puppet 4.0

unfold/splat 2(6)• Unfold with unary *

$a = [2,3]$b = [1, *$a, 4] => [1,2,3,4]

• Unfold in case option, selector and call$a = [1,2,3]case 1 { *$a : { # 1 or 2 or 3

}}notice *$a => 1,2,3notice $a => [1,2,3]

Page 23: Migrating to Puppet 4.0

substrings 3(6)

• Substring in string'cd' in "abcde" => true

• Substring with []"xyzabcdef"[3,3] => "abc""xyzabcdef"[3] => "a""xyzabcdef"[3,-2] => "abcde"

Page 24: Migrating to Puppet 4.0

regexp string/interpolate 4(6)

• Matches with Regexp in String form $a = "example.com" $url =~ "http://$a.*"

Page 25: Migrating to Puppet 4.0

Error message improvements 5(6)

• Detailed Error Messages– Semantic validation unless lex or syntax errors – Outputs position on line– Can report more than one error

Page 26: Migrating to Puppet 4.0

Expression Based Grammar 6(6)

• if, unless, case are expressions notice if 1 > 2 { true } else { false } # => false

$a = case 2 { 1, 2, 3: { yes } default: { no } } # => $a == yes

Page 27: Migrating to Puppet 4.0

Resource Expression

Page 28: Migrating to Puppet 4.0

Local Defaultsfile { default: mode => '444', owner => 'admin'; title: . . . ;}

Page 29: Migrating to Puppet 4.0

Unfold Hash of attributesfile { default: * => $defaults_hash; 'tmp/foo': mode => '666', * => $params_hash;}

Page 30: Migrating to Puppet 4.0

Create Resources Equiv. in PuppetResource[$type] { default: * => $defaults_hash; $titles: * => $params_hash;}

Page 31: Migrating to Puppet 4.0
Page 32: Migrating to Puppet 4.0

LOOOOOOOPS

• Iterate over:– Arrays– Hashes– Strings– Integer ranges

• Implemented as functions taking callable code blocks (lambdas) = open design

• Calls can now be expressed from left to right using '.' notation

Page 33: Migrating to Puppet 4.0

each

• Do something with each element• Returns LHS

[1,2,3].each |$x| { notice $x }

each([1,2,3]) |$x| { notice $x }

Page 34: Migrating to Puppet 4.0

map

• Transform each element• Returns transformed result

[1,2,3].map |$x| { $x*10 }

=> [10,20,30]

Page 35: Migrating to Puppet 4.0

filter

• Produces elements that match• Returns filtered result

[1,2,3].filter|$x| { $x >= 2 }

=> [2,3]

Page 36: Migrating to Puppet 4.0

reduce

• Transforms / reduces many to one• Feeds seed/previous result into next iteration • Returns transformed result

[1,2,3].reduce |$result, $x| {$result + $x

}

=> 6

Page 37: Migrating to Puppet 4.0

What do you see? Shout out the answer….

Page 38: Migrating to Puppet 4.0
Page 39: Migrating to Puppet 4.0

Next one….

Are you ready?

Page 40: Migrating to Puppet 4.0
Page 41: Migrating to Puppet 4.0

Two more…

ok?

Page 42: Migrating to Puppet 4.0
Page 43: Migrating to Puppet 4.0
Page 44: Migrating to Puppet 4.0

The Puppet Type System

Cow

Integer

Page 45: Migrating to Puppet 4.0

Puppet Types

• Puppet Types are first order objects (they can be assigned and passed around in variables)

• Uses syntax familiar from Resource – i.e. Class, File, User, where [ ] applied to the type makes it more specific – e.g. File['foo']

Page 46: Migrating to Puppet 4.0

Example Integer

# All integersInteger

# All integers >=42Integer[42]

# All integers >=42 and <=142Integer[42,142]

Page 47: Migrating to Puppet 4.0

Automatic type checking!

define mytype(Integer[80,443] $port){ # port guaranteed to be integer # and between 80 and 443 # otherwise an error}

Page 48: Migrating to Puppet 4.0

Manual type checking!define mytype($port) { assert_type(Integer[80,443], $port) |$expected, $got| { warn("Bad port $got, expected $expected. Using port 80.") 80 } }

• Code block called if given does not match• …do what you want, fail, warn, return default

Page 49: Migrating to Puppet 4.0

Operations on Type• Since a Type is a kind of Pattern…– Match with =~ and !~– Match in case expression

• Since Types are defined in a hierarchy:– Compare types with <, <=, >, >=

# is $x an integer ?$x =~ Integer

# is $t more specific than Integer$t = Integer[80, 144]$t < Integer

Page 50: Migrating to Puppet 4.0

Type HierarchyAny |- Scalar | |- Numeric | | |- Integer[from, to] | | |- Float[from, to] | | | |- String[from, to] | | |- Enum[*strings] | | |- Pattern[*patterns] | | | |- Boolean | |- Regexp[pattern_string]

Page 51: Migrating to Puppet 4.0

Type HierarchyAny |- Collection | |- Array[T] | | |- Tuple[T*, from, to] | | | |- Hash[K, V] | | |- Struct[{ key => T, ...}] | |- Variant[T*] |- Optional[T] | |- Undef |- Default | |- Type[T]

Page 52: Migrating to Puppet 4.0

Type HierarchyAny |- CatalogEntry | |- Resource[type_name, title] | |- Class[class_name] | |- Undef |- Data | |- Scalar | |- Array[Data] | |- Hash[Scalar, Data] | |- Undef

Page 53: Migrating to Puppet 4.0

EPP

Page 54: Migrating to Puppet 4.0

EPP – Templates in Puppet

• Same template markup as ERB – Logic is Puppet instead of Ruby

AND• Can be parameterized !• Use functions

epp(template)inline_epp(string)

• instead oftemplate()inline_template()

Page 55: Migrating to Puppet 4.0

Example EPP

$x = 'human'inline_epp('This is not the <%= $x %> you are looking for.', { 'x' => 'droid'})

# => 'This is not the droid you are looking for.'

<%- |$x = 'human'| -%>This is not the <%= $x %> you are looking for.

$x = 'human'inline_epp('This is not the <%= $x %> you are looking for.')

# => 'This is not the human you are looking for.'

Page 56: Migrating to Puppet 4.0

Heredoc

Page 57: Migrating to Puppet 4.0

Puppet Heredoc

• For more detailed control over a block of text• No, or selected set of escapes• Interpolation or no interpolation• Can be syntax checked by parser (JSon in core,

can add plugin language support)• Control over left margin

Page 58: Migrating to Puppet 4.0

Heredoc - Syntax

@( ["]<endtag>["] [:<syntax>] [/<escapes>] )<text>[|][-] <endtag>

ENDS-HEREanything not in <text>

"ENDS-HERE"with interpolation

:jsonsyntax check result

/tsrn$L turns on escape/ turns on all

|set left margin

-trim trailing

t tabs spacer returnn new-line$ $L <end of line>

Page 59: Migrating to Puppet 4.0

Puppet Heredoc Example

#.........1.........2.........3.........4.........5....$a = @(END) This is indented 2 spaces in the source, but produces a result flush left with the initial 'T' This line is thus indented 2 spaces. | END

#.........1.........2.........3.........4.........5....foo(@(FIRST), @(SECOND)) This is the text for the first heredoc FIRST This is the text for the second SECOND

Page 60: Migrating to Puppet 4.0

Ruby API

Page 61: Migrating to Puppet 4.0

Ruby API

• 4x Function API– type checked– dispatch to impl based on given types– more powerful– safer

• Binder– composable type safe injection– for plugins and data (e.g. syntax checkers, data

providers)

Page 62: Migrating to Puppet 4.0

Summary

• Language Cleanup• More strict• New Features• Better Error Messages• Iteration• Type System• Puppet Templates – EPP• Heredoc

Page 63: Migrating to Puppet 4.0

Enjoy Puppet 4

Page 64: Migrating to Puppet 4.0

Links

• github/puppetlabs/puppet-specifications• http://puppet-on-the-edge.blogspot.com/• Twitter @hel• IRC helindbe

Page 65: Migrating to Puppet 4.0

Just use PE the next time, ok?