Skip to content

Instantly share code, notes, and snippets.

View purplexa's full-sized avatar

Lexa Whitehurst purplexa

View GitHub Profile
@purplexa
purplexa / rtree.sh
Created September 28, 2017 20:16
do a reverse tree listing for a file
#!/bin/sh
find_parent() {
f="$(readlink -f "$1")"
parent="$(dirname $f)"
if [ "$f" != "$parent" ]; then
ls -ld "$parent"
find_parent "$parent"
fi
}
@purplexa
purplexa / class_node_inventory.pp
Last active August 15, 2017 22:01
PDB class node inventory
function myorg::pdb::class_node_inventory (
Pattern['\A[A-Za-z][A-Za-z0-9]*(::[A-Za-z][A-Za-z0-9]*)*\Z'] $classname,
Optional[String[1]] $env = undef,
) {
$query_template = @(EOT)
<%- | String $classname,
Optional[String] $env,
| -%>
inventory {
<%- if $env { -%>
@purplexa
purplexa / create_resources_with_metaparameters.pp
Last active May 13, 2017 06:00
create_resources in native puppet that works with metaparameters
function create_resources_with_metaparameters (
Variant[String, Type[Resource]] $type,
Hash[String, Hash[String, Any]] $resources,
Optional[Hash[String, Any]] $defaults = undef,
) {
$resources.each |$resource, $attributes| {
Resource[$type] {
$resource: * => $attributes.prepare_attributes();
default: * => $defaults.prepare_attributes();
}
@purplexa
purplexa / foobar.pp
Created May 13, 2017 04:12
metaparameters on hiera-defined resources
class foobar (
Hash[String,
Struct[{
attributes => Hash[String, Data],
Optional[requires] => Hash[String, String],
Optional[notifies] => Hash[String, String]
}]] $files,
Hash[String, Data] $file_defaults = {},
) {
$files.each |$key, $value| {
@purplexa
purplexa / before_refreshonly.pp
Last active April 24, 2017 18:11
puppet before/notify/refreshonly behavior
file { '/tmp/before_test':
ensure => present,
before => Exec['/bin/rm /tmp/before_test'],
}
exec { '/bin/rm /tmp/before_test':
refreshonly => true,
}
file { '/tmp/notify_test':
@purplexa
purplexa / containment_evaluation.pp
Created April 6, 2017 20:08
Puppet containment and evaluation
class alfa {
notice("Evaluating Class['alfa']")
notify { 'class alfa': }
include helium
include hydrogen
Class['hydrogen'] -> Class['helium']
}
class bravo {
foo = [1, 3, 5, 2, 3]
bar = [1, 3, 5, 2, 4, 8]
def generate_checks(num_list):
checks = []
if len(num_list) == 0:
return checks
for i in range(len(num_list[1:])):
checks.append(lambda: num_list[0] == num_list[i+1])
return checks + generate_checks(num_list[1:])
@purplexa
purplexa / pql.bash
Created August 17, 2016 20:02
Bash function for hitting the PuppetDB API, for when you want to use PQL but don't have the cli tool installed
function pql {
tmpfile=$(mktemp)
status_code=$(curl --silent --output /dev/stderr --write-out "%{http_code}" -XGET localhost:8080/pdb/query/v4 -d "query=$1" 2>$tmpfile)
if [ "$status_code" = "200" ]; then
jq $3 "$2" $tmpfile
else
cat $tmpfile
fi
rm $tmpfile
}
$var = if $facts['os']['family'] == 'Debian' and $facts['os']['release']['major'] == '8' {
'foo'
} else {
'bar'
}
@purplexa
purplexa / query_spec.rb
Created April 21, 2016 00:59
rspec-puppet JSON attribute value comparison
require 'spec_helper'
require 'json'
fixture_dir = File.expand_path(File.join(__FILE__, '..', '..', 'fixtures', 'data'))
describe 'jmxtrans::query' do
def check_json_string(expected)
return Proc.new do |actual|
begin
expected_obj = JSON.parse(expected)