-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from udx/UAT-60
Services Management Improvements, CLI [UAT-60, UAT-61]
- Loading branch information
Showing
8 changed files
with
254 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
### Commands | ||
|
||
* `worker service list` | ||
Lists all available services. | ||
|
||
* `worker service status <service_name>` | ||
Displays the status of a specified service. | ||
|
||
* `worker service logs <service_name>` | ||
Tails the output logs for a specific service. | ||
|
||
* `worker service errors <service_name>` | ||
Tails the error logs for a specific service. | ||
|
||
* `worker service config` | ||
Shows the services configuration settings. | ||
|
||
* `worker service start <service_name>` | ||
Starts a specified service. | ||
|
||
* `worker service stop <service_name>` | ||
Stops a specified service. | ||
|
||
* `worker service restart <service_name>` | ||
Restarts a specified service. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#!/bin/bash | ||
|
||
# Function to list all services | ||
list_services() { | ||
# Capture the output of supervisorctl status | ||
local services_status | ||
services_status=$(supervisorctl status 2>&1) # Also capture stderr to handle error messages | ||
|
||
# Check if Supervisor is not running, not accessible, or if there are no managed services | ||
if [[ -z "$services_status" ]] || echo "$services_status" | grep -Eq 'no such|ERROR'; then | ||
echo "No services are currently managed." | ||
exit 1 | ||
fi | ||
|
||
echo "Listing all managed services:" | ||
local i=1 | ||
echo "$services_status" | while read -r line; do | ||
echo "$i. $line" | ||
((i++)) | ||
done | ||
} | ||
|
||
# Function to check the status of one or all services | ||
check_status() { | ||
# Require a service name for this function | ||
if [ -z "$1" ]; then | ||
echo "Error: No service name provided." | ||
echo "Usage: $0 status <service_name>" | ||
exit 1 | ||
fi | ||
|
||
# Attempt to capture the status of the specific service, including errors | ||
local service_status | ||
service_status=$(supervisorctl status "$1" 2>&1) | ||
|
||
# Check if Supervisor is not running, not accessible, or if the service does not exist | ||
if [[ -z "$service_status" ]] || echo "$service_status" | grep -Eq 'no such|ERROR'; then | ||
echo "The service '$1' does not exist." | ||
exit 1 | ||
fi | ||
|
||
# Directly output the captured service status | ||
echo "$service_status" | ||
} | ||
|
||
# Function to follow logs for a specific service | ||
follow_logs() { | ||
if [ -z "$1" ]; then | ||
echo "Error: No service name provided." | ||
echo "Usage: $0 logs <service_name>" | ||
exit 1 | ||
fi | ||
|
||
local logfile="/var/log/supervisor/$1" | ||
local type=${2:-out} # Default to 'out' if not specified | ||
logfile="$logfile.$type.log" | ||
|
||
if [ ! -f "$logfile" ]; then | ||
echo "Log file does not exist: $logfile" | ||
exit 1 | ||
fi | ||
|
||
tail -f "$logfile" | ||
} | ||
|
||
# Function to show supervisor configuration | ||
show_config() { | ||
if [ ! -f "/etc/supervisord.conf" ]; then | ||
echo "Configuration file is not generated since no services are managed." | ||
exit 1 | ||
fi | ||
cat /etc/supervisord.conf | ||
} | ||
|
||
# Function to start, stop, or restart a service | ||
manage_service() { | ||
if [ -z "$2" ]; then | ||
echo "Error: No service name provided." | ||
echo "Usage: $0 $1 <service_name>" | ||
exit 1 | ||
fi | ||
|
||
if [ ! -e "/var/run/supervisor/supervisord.sock" ]; then | ||
echo "Error: Service doesn't exist." | ||
exit 1 | ||
fi | ||
|
||
supervisorctl "$1" "$2" | ||
} | ||
|
||
# CLI Interface | ||
case $1 in | ||
service) | ||
shift # Shift the arguments to the left, so $2 becomes $1, $3 becomes $2, etc. | ||
case $1 in | ||
list) | ||
list_services | ||
;; | ||
status) | ||
check_status "$2" | ||
;; | ||
logs) | ||
follow_logs "$2" | ||
;; | ||
errors) | ||
follow_logs "$2" "err" | ||
;; | ||
config) | ||
show_config | ||
;; | ||
start) | ||
manage_service start "$2" | ||
;; | ||
stop) | ||
manage_service stop "$2" | ||
;; | ||
restart) | ||
manage_service restart "$2" | ||
;; | ||
*) | ||
echo "Usage: $0 service {list|status [service_name]|logs <service_name>|config|start <service_name>|stop <service_name>|restart <service_name>}" | ||
exit 1 | ||
;; | ||
esac | ||
;; | ||
*) | ||
echo "Usage: $0 {service {list|status [service_name]|logs <service_name>|config|start <service_name>|stop <service_name>|restart <service_name>}}" | ||
exit 1 | ||
;; | ||
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
## Services | ||
|
||
The `services.yml` file, located in the `src/configs` directory, is a critical component for configuring service behavior in the UDX Worker environment. This file follows the `workerService` kind specification, designed to manage and orchestrate services dynamically, based on the `udx.io/worker-v1/service` version schema. Below, we break down the structure and explain the significance of each field, including default values as defined in the `lib/process_manager.sh` script. | ||
|
||
```yaml | ||
--- | ||
kind: workerService | ||
version: udx.io/worker-v1/service | ||
services: | ||
- name: "<service_name>" | ||
ignore: "<true/false>" | ||
command: "<command_to_run>" | ||
autostart: "<true/false>" | ||
autorestart: "<true/false>" | ||
envs: | ||
- "<ENV_VAR_NAME>=<value>" | ||
``` | ||
### Service Fields Explanation | ||
* `name`: Unique identifier for the service. This is used to reference and manage the service within the system. | ||
|
||
* `ignore`: Determines whether the service should be ignored. If set to "true", the service will not be managed by the worker. The default value is "false", which means the service is considered for management. | ||
|
||
* `command`: The command that the service will execute. This could be a shell script or any executable along with its arguments. | ||
|
||
* `autostart`: Indicates whether the service should start automatically when the worker starts. The default is "true", meaning the service will start automatically. | ||
|
||
* `autorestart`: Specifies whether the service should automatically restart if it stops. If "true", the service will restart according to the policy defined by the worker management system. The default value is "false", indicating the service will not restart automatically. | ||
|
||
* `envs`: An array of environment variables passed to the service in the format `KEY=value`. These variables are made available to the service at runtime. | ||
|
||
## Usage | ||
|
||
To use these configuration file, ensure that the `services.yml` file is correctly configured and placed in the appropriate directory (`/etc/worker`) within the container. | ||
|
||
### Volume Mount | ||
|
||
If you have a worker configuration outside of the worker image, you can mount it as a volume into the container: | ||
|
||
```shell | ||
docker run -d --name udx-worker \ | ||
--env-file .env \ | ||
-v $(pwd)/my-tasks:/usr/src/app \ | ||
-v $(pwd)/src/configs/services.yml:/etc/worker \ | ||
usabilitydynamics/udx-worker:latest | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters