Skip to content

Instantly share code, notes, and snippets.

Martin Gehrke martingehrke

View GitHub Profile
martingehrke /
Last active Aug 29, 2015
A lot of our data is outside our main policy, especially json data. This snippet will help manage dirs that need syncing in a central place. Classes are global
#this code has not been tested for minor bugs, but the idea is solid
"sync_data" data => parsejson('{ "dir1":"class1", "dir2":"class2" }');
"dirs" slist => getindices("sync_data");
create => "true",
copy_from => secure_cp("/base/$(dirs)", "$(sys.policy_hub)"),
if => "$(sync_data[dirs])";
martingehrke /
Last active Aug 29, 2015
How to get a list of values out of json in CFEngine 3.6+
#it can be non-intuitive to get a slist from a json data object in CFEngine
# this can be useful if you have a json with hostnames as keys
"data" data => parsejson("{ "key": ["one", "two", "three"] }");
"values" slist => getvalues("data[key]"); #just pass the name, not the variable itself
#just an example, I haven't loaded moredata in this gist
"info" slist => getvalues("moredata[$(sys.fqhost)]");
martingehrke /
Last active Aug 29, 2015
Reading a file in cfengine can be expensive. The code below shows how do it right.
#this requires cfengine 3.7 for the if and unless
"file" string => "/full/path/to/file.json";
classname:: #always gate file reads as tightly as possible
"data" data => readjson("$(file)", inf),
if => fileexists("$(file)"), #don't try to read if file is missing
unless => isvariable("$(this.promiser)"); #don't read file more than once
#you can't do all that in pre 3.7 but you can do some
# sometimes I prefer an error if the file is missing
martingehrke / btsync-upstart.conf
Created Mar 27, 2015
bittorrent sync, btsync, config file for upstart; put this in /etc/init/btsync.conf and put your config file in /etc/btsync/btsync.conf
View btsync-upstart.conf
description "BitTorrent Sync"
start on filesystem
stop on shutdown
expect fork
martingehrke / cfengine_complex_conditionals
Created Mar 27, 2015
An example of how to do complex conditionals in CFEngine
View cfengine_complex_conditionals
if x == 7 and (y<10 or y>20):
do something
#using classes
or_class_set or => {"less_than_ten", "greater_than_twenty"};
and_class_set and => {"or_class_set", "equals_seven"};
#is the same as
and_class_set and => {"equals_seven", "less_than_ten|greater_than_twenty"};
You can’t perform that action at this time.