Skip to content

Instantly share code, notes, and snippets.

Avatar

Henrik Lindberg hlindberg

  • Bebalon Ltd
  • Għarb, Gozo, Malta
View GitHub Profile
@hlindberg
hlindberg / fizzbuzz.pp
Last active Jul 9, 2020
FizzBuzz in Puppet 4.1
View fizzbuzz.pp
notice Integer[0,99].map |$x| {
case [$x % 3, $x % 5] {
[0, 0] : { fizzbuzz }
[0, default] : { fizz }
[default, 0] : { buzz }
default : { $x }
}
}
View default_content(1.8.7).rb
Puppet::Parser::Functions.newfunction(:default_content,
:type => :rvalue,
:doc => <<-'ENDOFDOC'
Takes an optional content and an optional template name and returns the
contents of a file.
Examples:
$config_file_content = default_content($file_content, $template_location)
file { '/tmp/x':
@hlindberg
hlindberg / sample_mocking_spec.rb
Last active May 8, 2020
An rspec sample showing some override and mocking of 4.x functions/function loading
View sample_mocking_spec.rb
require 'spec_helper'
# Example rspec_puppet function rspec test (i.e. subject is the function 'min')
# This works for other rspec subjects as well as a compiler is always involved.
# This kind of mocking can be required when it is not enough to simply override
# a function with another implementation (which can be done with a `let(:pre_condition) { 'function min($x, $y) { ... }'}`
#
# The main difficulty that this overcomes is the need to let the compiler initialize and
# create the context in which it will operate before making any mocks.
#
View puppet.ebnf
# Puppet EBNF (kind of)
# Try with: http://www.bottlecaps.de/rr/ui
ACTOR ::= 'actor' ( '(' PARAMS ')')? ('inherits' NAME)?
'{'
(QUEUE | INBOX | ACTION | SEQUENCE | PARALLEL | FUNCTION | STATE | CONSTANT | LOCAL_TYPE)*
'}'
SEQUENCE ::= 'sequence' '{' (ACTION | SEQUENCE | PARALLEL)* '}'
PARALLEL ::= 'parallel' '{' (ACTION | SEQUENCE | PARALLEL)* '}'
View gist:e033ab9c8e20282cc494db6f04afc308
# The latch function remembers what it was called with
# and returns what it was previously called with.
#
# This is using global namespace - in your code use a module namespace
#
Puppet::Functions.create_function(:latch) do
dispatch :example do
repeated_param 'Any', :arg
end
@hlindberg
hlindberg / sorthash.pp
Created Nov 25, 2019
This is an example of how to recursively sort a Hash in the puppet language using the tree_each() function
View sorthash.pp
# When you need to process a hash with keys in sorted order
# you may have found the `sort()` function - but it only
# operates on an Array.
#
# Most of the time what is wanted is simply achieved
# by taking the keys of a hash and sorting those and
# then iterating over the keys.
#
# When, however the wanted result is a new Hash
# with all keys sorted recursively then this becomes
@hlindberg
hlindberg / set_github_labels.js
Last active Jun 16, 2019
Script to run in browser dev-mode when on label edit page for a github repo
View set_github_labels.js
[
{
'name': 'bug',
'color': 'd63230',
'desc': 'Does not work (according to specification)'
},
{
'name': 'improvement',
'color': '39a9db',
'desc': 'improvement - neither bug nor new feature'
View example.pp
$h = { 'b' => ['banana', 'bandana'], 'o' => ['orange', 'ovaltine'] }
$h2 = $h.map |$k, $v | { [$k,$v[0]] }
notice "h2 = ${h2}"
notice Hash($h2)
View puppet_grammar.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<meta name="generator" content="Railroad Diagram Generator 1.52.1729" />
<style type="text/css">
::-moz-selection
{
color: #FFFEF8;
background: #1E1800;
}
@hlindberg
hlindberg / any2bool.rb
Created Nov 2, 2018
Example: Puppet function calling new to create a Boolean from a String
View any2bool.rb
Puppet::Functions.create_function(:any2bool) do
dispatch :any2bool do
param 'Any', :to_be_converted
end
def any2bool(to_be_converted)
call_function('new', Puppet::Pops::Types::PBooleanType::DEFAULT, to_be_converted)
end
end