service-2.2.0
4 minute read
KOSI Plugin Service Version 2.2.0
Summary
The Service plugin allows you to manage system services by starting, stopping, restarting, and checking their status. It serves as a wrapper around 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 that it is running.
- Stop a service to terminate it.
- 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. Service names may vary depending on the Linux distribution. For example, Apache is typically httpd on RHEL-based systems and apache2 on Debian/Ubuntu. |
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 - Use the Service Plugin with Other Plugins
This example shows how the Service plugin can be used together with the osCheck, if, and print plugins.
In this example, the Service plugin is combined with osCheck, if, and print to check whether it is the correct operating system and display an appropriate message, and then execute the corresponding service operation.:
languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-service-oscheck-if-print";
description = "Example using service with oscheck, 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
rhelServiceservice 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
differentServiceservice has been stopped.