#####################################################################################
# Copyright 2011 Normation SAS
#####################################################################################
#
# 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, see <http://www.gnu.org/licenses/>.
#
#####################################################################################

#
# Failsafe file
#

body common control
{
        bundlesequence     => { "check_uuid", "update_action" };

        inputs             => { "common/1.0/common.cf", "common/1.0/update.cf" };
        output_prefix      => "rudder";

        protocol_version   => "2";

        # force tls1.2
        tls_min_version => "1.2";
}

bundle common g
{
  vars:
      "rudder_base"              string => "/opt/rudder";
      "rudder_var"               string => "/var/rudder";
      "rudder_bin"               string => "${rudder_base}/bin";
      "rudder_sbin"              string => "${rudder_base}/sbin";
      "rudder_base_sbin"         string => "${rudder_base}/sbin"; #folder where tools are installed
      "rudder_ncf"               string => "${rudder_var}/ncf";
      "uuid_file"                string => "${rudder_base}/etc/uuid.hive";
      "rudder_disable_server_file" string => "${rudder_base}/etc/disable-policy-distribution";

    any::
      "uuid"                     string => readfile("${g.uuid_file}", 60);
      "ncf_excludedreps"          slist => { "50_techniques" };
      "rudder_ncf_origin_common" string => "/usr/share/ncf/tree";
      "rudder_ncf_origin_local"  string => "/tmp/test-rudder-config-repo-2019-08-13T15:43:57.745+02:00/configuration-repository/ncf";

      "rudder_ncf_hash_file"          string => "ncf_hash_file";
      "rudder_ncf_origin_common_hash" string => "${rudder_ncf_origin_common}/${rudder_ncf_hash_file}";
      "rudder_ncf_origin_local_hash"  string => "${rudder_ncf_origin_local}/${rudder_ncf_hash_file}";

      # We would like to use date's "--rfc-3339=second" option here, but it is not available on older OSes (RHEL 3/4, AIX 5...)
      "execRun"                  string => execresult("/bin/date -u \"+%Y-%m-%d %T+00:00\"", "noshell");

# definition of the node roles
  # This node doesn't have any specific role

  classes:
      # OS classes for compatibility
      "SUSE" expression => "sles";
      "SuSE" expression => "sles";
      "suse" expression => "sles";
}

bundle common rudder_roles
{
  classes:
      # Policy Server is a machine which delivers promises
      "policy_server"       expression => strcmp("root","${g.uuid}");
      # Root Server is the top policy server machine
      "root_server"         expression => strcmp("root","${g.uuid}");

      # We are in the failsafe phase
      "failsafe" expression => "any";
}

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

body depth_search recurse(d)
{
        depth => "${d}";
}

#perms validation
body perms u_p(p)
{
        mode  => "${p}";
}

#server may be a list
body copy_from cp(from,server)
{
        servers     => { "${server}" };
        source      => "${from}";
        compare     => "digest";
    community_edition::
        portnumber => "5309";

}

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

body depth_search recurse_ignore(d,list)
{
        depth => "${d}";
        exclude_dirs => { @{list} };
}

body delete tidy
{
        dirlinks => "delete";
        rmdirs   => "true";
}

body file_select exclude(name)
{
        leaf_name  => { "${name}"};
        file_result => "!leaf_name";
}

# Define a class no matter what the outcome of the promise is
body classes always(x)
{
  promise_repaired => { "${x}" };
  promise_kept => { "${x}" };
  repair_failed => { "${x}" };
  repair_denied => { "${x}" };
  repair_timeout => { "${x}" };
}
