#  Copyright (C) Cfengine AS

#  This file is part of Cfengine 3 - written and maintained by Cfengine AS.

#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; version 3.

#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

# To the extent this program is licensed as part of the Enterprise
# versions of Cfengine, the applicable Commercial Open Source License
# (COSL) may apply to this file if you as a licensee so wish it. See
# included file COSL.txt.

#cop update,example
#cop What should a failsafe and update file contain?,example

# Minimum failsafe

body common control
{
      bundlesequence => { "update" };
}

bundle agent update
{
  vars:

      "master_location" string => "$(sys.workdir)/masterfiles";

      "policy_server"   string => readfile("$(sys.workdir)/policy_server.dat",40),
      comment => "IP address to locate your policy host.";

  classes:

      "policy_host" or => { 
			    classmatch(canonify("ipv4_$(policy_server)")),
			    classmatch(canonify("$(policy_server)"))
      },

      comment => "Define the ip identity of the policy source host";

      "have_ppkeys" expression => fileexists("$(sys.workdir)/ppkeys/localhost.pub");

      "nofile" expression => fileexists("$(sys.workdir)/policy_server.dat");

  commands:

    !have_ppkeys::

      "/var/cfengine/bin/cf-key";

  files:

      "/var/cfengine/inputs" 

      handle => "update_policy",
      perms => u_p("600"),
      copy_from => u_scp("$(master_location)"),
      depth_search => u_recurse("inf"),
      action => immediate;

  processes:

    any::

      "cf-execd" restart_class => "start_exec";

    policy_host::

      "cf-serverd" restart_class => "start_server";

  commands:

    start_exec::
      "$(sys.workdir)/bin/cf-execd",
      action => logme("executor");

    start_server::
      "$(sys.workdir)/bin/cf-serverd",
      action => logme("server");

  reports:

    bootstrap_mode.policy_host::

      "I am the policy host - i.e. with ipv4 address $(policy_server)";

}

############################################

body action logme(x)
{
      log_repaired => "stdout";
      log_string => " -> Started the $(x) (success)";
}

############################################

body perms u_p(p)

{
      mode  => "$(p)";
}

#############################################

body copy_from u_scp(from)

{
      source      => "$(from)";
      compare     => "digest";
      trustkey    => "true";

    !policy_host::

      servers => { "$(policy_server)" };
}

#########################################################

body action immediate
{
      ifelapsed => "1";
}

############################################

body depth_search u_recurse(d)

{
      depth => "$(d)";
}
