Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@martingehrke
martingehrke / readfileright.cf
Last active August 29, 2015 14:26
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
vars:
"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
martingehrke / jsonslist.cf
Last active August 29, 2015 14:26
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
vars:
"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
martingehrke / central_ext_file_sync.cf
Last active August 29, 2015 14:26
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
vars:
"sync_data" data => parsejson('{ "dir1":"class1", "dir2":"class2" }');
"dirs" slist => getindices("sync_data");
files:
"$(dirs)"
create => "true",
copy_from => secure_cp("/base/$(dirs)", "$(sys.policy_hub)"),
if => "$(sync_data[dirs])";
@martingehrke
martingehrke / btsync-upstart.conf
Created March 27, 2015 21:34
bittorrent sync, btsync, config file for upstart; put this in /etc/init/btsync.conf and put your config file in /etc/btsync/btsync.conf
description "BitTorrent Sync"
start on filesystem
stop on shutdown
respawn
expect fork
script
@martingehrke
martingehrke / cfengine_complex_conditionals
Created March 27, 2015 19:58
An example of how to do complex conditionals in CFEngine
#pseudocode
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"};