Skip to main content
  1. Blog/

Implementing "single-copy nirvana" in cfengine3

·74 words·1 min·
Diego Zamboni
Author
Diego Zamboni
Here is a sample implementation in cfengine3 of the technique described in the cfengine wiki as "Singlecopy Nirvana" for cfengine2. Cfengine3 makes the technique more powerful by allowing modularization, so that you can specify both the suffixes to try, and the files to copy using those suffixes, as variables. Also, cf3's automatic list expansion allows us to write a single copy statement that will be automatically expanded into all the different values to try.

#
# Sample cf3 implementation of "single copy nirvana" from
# http://cfwiki.org/cfwiki/index.php/Singlecopy_Nirvana
#
# Time-stamp: <[cf3-singlecopy.cf] modified by Diego Zamboni on Monday, 2010.08.23 at 01:44:19>
#
body common control
{
bundlesequence => { "copyfiles" };
}
# Use single copy for all files
body agent control
{
files_single_copy => { ".*" };
}
######################################################################
bundle agent copyfiles
{
vars:
# Suffixes to try, in most-specific to most-general order. This must include the
# empty suffix at the end, for the most general file.
# In this example I'm taking them from the system variables, but they could
# be anything.
"copysuffixes" slist => { ".${sys.uqhost}", ".${sys.domain}", ".${sys.os}", "" };
# List of files to copy
"filestocopy" slist => { "test1", "test2", "test3" };
# Source of the files
"repo" string => "/tmp/testrepo";
# Destination for the files
"dest" string => "/tmp/testdest";
files:
"${dest}/${filestocopy}"
copy_from => hierarchical_copy("${filestocopy}");
}
body copy_from hierarchical_copy(from)
{
source => "${repo}/${from}${copysuffixes}";
compare => "digest";
}