help-cfengine
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Determine file to copy based on classes


From: Kai Großjohann
Subject: Re: Determine file to copy based on classes
Date: Sat, 26 Oct 2002 22:04:16 +0200
User-agent: Gnus/5.090008 (Oort Gnus v0.08) Emacs/21.3.50 (i686-pc-linux-gnu)

kai.grossjohann@uni-duisburg.de (Kai Großjohann) writes:

> Is there a way to implement something like the following algorithm in
> CFengine?

I have now concocted the following abomination which does this in a
shell script, of all things.  You call it from cfagent.conf in a
shell command like this:

shellcommands:
    "copycf.sh /your/repository $(allclasses)"

The idea is that /your/repository/etc/foo is a directory which
contains files named after classes.  For each host the right file is
chosen and installed as /etc/foo.  Suggestions for improvements
welcome.

#!/bin/sh
# $Id: copycf.sh,v 1.1 2002/10/26 19:30:46 kai Exp $

# Most of the documentation for this file can be found in
# cfagent.conf.  See the section "Directory structure" in the comment
# near the beginning of the file.

host=`uname -n`
priority="$host kbd_de burn mirror_sw homesys dell ibm laptop desktop any"

files="$1"
cfallclasses="$2"

cfallclasses=$(echo $cfallclasses | cut -d= -f2)

case $cfallclasses in
    (:*:)
    ;;
    (*:)
    cfallclasses=:$cfallclasses
    ;;
    (:*)
    cfallclasses=${cfallclasses}:
    ;;
    (*)
    cfallclasses=:${cfallclasses}:
    ;;
esac

# Usage: copyfile <FILE>
# Iterate over list of classes in order ($priority) and install the
# first config file that is found.
copyfile () {
    file="$1"
    for c in $priority; do
        case $cfallclasses in
            (*:${c}:*)
            if [ -f $files/$file/$c ]; then
                if cmp -s $files/$file/$c /$file; then
                    :
                else
                    echo "Installing /$file for class $c"
                    install -p $files/$file/$c /$file
                    return
                fi
            fi
            ;;
        esac
    done
    return 1
}

# Compute candidate directory names as follows.  Get list of files in
# tree, then take the directory of each file, then eliminate
# duplicates.  Make sure that resulting dir names are relative to
# $files.

candidates=$(
    find $files -name CVS -prune -o -type f -print | {
        while read f; do
            dirname $f
        done;
    } | uniq | sed -e s:$files/:: )

for f in $candidates; do
    copyfile $f
done

# copycf.sh ends here

kai (why didn't I do it in Perl)
-- 
~/.signature is: umop ap!sdn    (Frank Nobis)






reply via email to

[Prev in Thread] Current Thread [Next in Thread]