service-1.7.0

KOSI Plugin Service Version 1.7.0

Summary

The Service plugin allows you to manage system services by starting, stopping, restarting and checking their status. It acts as a wrapper for the systemctl command, which is commonly used on Linux-based systems to control services.

This plugin is particularly useful for managing important services such as web servers (e.g. nginx, httpd), databases (e.g. mysql, postgresql) and other system daemons within a KOSI package, if the desired service is available on your system.

With the Service plugin, you can:

  • Start a service to ensure it is running.
  • Stop a service to terminate its execution.
  • Restart a service to apply configuration changes.
  • Check the status of a service to verify its current state.

Keys

Key Required Description
name Yes Specifies the name of the service to be managed.
state Yes Defines the operation to perform on the service. Supported values: start, stop, restart, status.
sudo Optional If set to true, the command will be executed with elevated privileges.
sudoPassword Yes if sudo is true Provides the password for sudo authentication.

Examples

Note: The services in the following examples assume that the corresponding services exist in the system.

Example 1 - Start a Service

This example starts the Apache HTTP Server (httpd).

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
    input = "template.yaml";
}

containers =
{
    example = ["docker.io", "nginx", "latest"];
}

install
{
    service
    (
        name = "httpd";
        state = "start";
        sudo = "true";
        sudoPassword = "securePassword";
    );
}

Example 2 - Stop a Service

This example stops the Apache HTTP Server.

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
    input = "template.yaml";
}

containers =
{
    example = ["docker.io", "nginx", "latest"];
}

install
{
    service
    (
        name = "httpd";
        state = "stop";
        sudo = "true";
        sudoPassword = "securePassword";
    );
}

Example 3 - Restart a Service

Restarting a service can be useful after configuration changes.

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
    input = "template.yaml";
}

containers =
{
    example = ["docker.io", "nginx", "latest"];
}

install
{
    service
    (
        name = "httpd";
        state = "restart";
        sudo = "true";
        sudoPassword = "securePassword";
    );
}

Example 4 - Check Service Status

This command checks whether the httpd service is currently running.

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
    input = "template.yaml";
}

containers =
{
    example = ["docker.io", "nginx", "latest"];
}

install
{
    service
    (
        name = "httpd";
        state = "status";
        sudo = "true";
        sudoPassword = "securePassword";
    );
}

Example 5 - Using Service Plugin with other plugins

Using service with the osCheck, if and print plugins

This example shows the interaction of several plugins. In this example, we use the service plugin together with the osCheck, if and print plugins to check whether it is the correct operating system and to output a specific message and execute a corresponding operation with the service plugin:

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-service-oscheck-if-print";
description = "Example using service with oscheck and if and print";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
    input = "template.yaml";
}

install
{
    osCheck
    (
        getOSVar = "os";
        getOSVersionVar = "version";
    );

    if (condition = "$os$ = 'Red Hat Enterprise Linux'") then
    {
        print(message = "Performing stop service for RHEL 8..");

        service
        (
            name = "rhelService";
            state = "stop";
            sudo = "true";
            sudoPassword = "securePassword";
        );
    }
    else
    {
        print(message = "Other OS recognized instead of RHEL 8, performing stop for an alternative service..");
      
        service
        (
            name = "differentService";
            state = "stop";
            sudo = "true";
            sudoPassword = "securePassword";
        );
    }
}

Expected Behavior

If os is Red Hat Enterprise Linux:

2025-02-26 15:18:28 Info:      Performing stop service for RHEL 8..
  • The rhelService service has been stopped.

Otherwise:

2025-02-26 15:18:28 Info:      Other OS recognized instead of RHEL 8, performing stop for an alternative service..
  • The differentService service has been stopped.