#  Copyright 2021 Northern.tech AS
#  This file is part of CFEngine 3 - written and maintained by Northern.tech 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.
#####################################################################
#
# Concept of standard services promises
#
#####################################################################
body common control
{
  bundlesequence => { "example" };
}

bundle agent example
{
  vars:
    "mail" slist => { "milter", "spamassassin", "postfix" };

  services:
    "www"
      service_policy => "start",
      service_method => service_test;

    "$(mail)"
      service_policy => "stop",
      service_method => service_test;
}

####################################################################
body service_method service_test
{
  service_bundle => non_standard_services(
    "$(this.promiser)", "$(this.service_policy)"
  );
}

####################################################################
bundle agent standard_services(service, state)
{
  # DATA, 
  vars:
    suse|redhat::
      "startcommand[www]" string => "/etc/init.d/apache2 start";
      "stopcommand[www]" string => "/etc/init.d/apache2 stop";

    debian|ubuntu::
      "startcommand[www]" string => "/etc/init.d/httpd start";
      "stopcommand[www]" string => "/etc/init.d/httpd stop";

    linux::
      "startcommand[postfix]" string => "/etc/init.d/postfix start";
      "stopcommand[postfix]" string => "/etc/init.d/postfix stop";

  # METHODS
  classes:
    "start" expression => strcmp("start", "$(state)");
    "stop" expression => strcmp("stop", "$(state)");

  processes:
    start::
      ".*$(service).*"
        comment => "Verify that the service appears in the process table",
        restart_class => "restart_$(service)";

    stop::
      ".*$(service).*"
        comment => "Verify that the service does not appear in the process",
        process_stop => "$(stopcommand[$(service)])",
        signals => { "term", "kill" };

  commands:
    "$(startcommand[$(service)])"
      comment => "Execute command to restart the $(service) service",
      if => "restart_$(service)";
}

######################################################################
bundle agent non_standard_services(service, state)
{
  reports:
    !done::
      "Test service promise for \"$(service)\" -> $(state)";
}
