Skip to content

Instantly share code, notes, and snippets.

View hlindberg's full-sized avatar

Henrik Lindberg hlindberg

  • Bebalon Ltd
  • Għarb, Gozo, Malta
  • X @hel
View GitHub Profile
@hlindberg
hlindberg / haddock.rb
Created July 6, 2023 15:57
Ruby code to return random captain haddock curse
class Haddock
CURSES = [
"Aardvark",
"Abecedarians",
"Addle-pated lumps of anthracite",
"Anachronisms",
"Anacoluthons",
"Antediluvian bulldozer",
"Anthracite",
"Anthropithecus",
Puppet::Functions.create_function(:env_data) do
# @since 4.8.0
dispatch :env_data do
param 'Struct[{}]', :options
param 'Puppet::LookupContext', :context
end
def env_data(options, context)
ENV.to_h
end
[ 1000, 2000, 3000, "", 4000, "", 5000, 6000, "", 7000, 8000, 9000, "", 10000,
].reduce([0]) |$memo, $v| { if $v == "" { $memo << 0 } else { $memo[0,-2] << $memo[-1]+$v } }
.map() |$i,$x| { [$i,$x] }
.sort() |$a,$b| { compare($b[1], $a[1]) }
.then() |$x| { $x[0] }
.then() |$x| { "Elf ${x[0]} has ${x[1]} calories" }
.notice()
# 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)* '}'
# 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
Last active July 20, 2022 12:36
This is an example of how to recursively sort a Hash in the puppet language using the tree_each() function. Note that in versions before Puppet 6.0 the `convert_to` function does not accept the extra 'hash_tree' arg.ument.
# 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 / example.pp
Created February 12, 2019 21:20
mapping a hash
$h = { 'b' => ['banana', 'bandana'], 'o' => ['orange', 'ovaltine'] }
$h2 = $h.map |$k, $v | { [$k,$v[0]] }
notice "h2 = ${h2}"
notice Hash($h2)
<!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 November 2, 2018 15:14
Example: Puppet function calling new to create a Boolean from a String
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
@hlindberg
hlindberg / functional_puppet.pp
Created October 5, 2018 09:32
A fun example using Deferred to write puppet code in a functional style
# Resolves a given value - if given a Deferred it will be called and all other values are returned as is.
#
function resolve(Any $x) {
if $x =~ Deferred { $x.call } else { $x }
}
# Resolves and evaluates a condition and either resolves and evaluates the given when_true,
# or the given when_false depending on the outcome of the evaluation of the condition.
# The result is undef if the selected when_true/when_false is undefined.
#