2023-08-23 09:09:51 +08:00
#!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2024-01-26 10:02:53 +08:00
#
2023-08-23 09:09:51 +08:00
# OpenIM CronTask Control Script
2024-01-26 10:02:53 +08:00
#
2023-08-23 09:09:51 +08:00
# Description:
# This script provides a control interface for the OpenIM CronTask service within a Linux environment. It supports two installation methods: installation via function calls to systemctl, and direct installation through background processes.
2024-01-26 10:02:53 +08:00
#
2023-08-23 09:09:51 +08:00
# Features:
# 1. Robust error handling leveraging Bash built-ins such as 'errexit', 'nounset', and 'pipefail'.
# 2. Capability to source common utility functions and configurations, ensuring environmental consistency.
# 3. Comprehensive logging tools, offering clear operational insights.
# 4. Support for creating, managing, and interacting with Linux systemd services.
# 5. Mechanisms to verify the successful running of the service.
#
# Usage:
# 1. Direct Script Execution:
# This will start the OpenIM CronTask directly through a background process.
# Example: ./openim-crontask.sh openim::crontask::start
2024-01-26 10:02:53 +08:00
#
2023-08-23 09:09:51 +08:00
# 2. Controlling through Functions for systemctl operations:
# Specific operations like installation, uninstallation, and status check can be executed by passing the respective function name as an argument to the script.
# Example: ./openim-crontask.sh openim::crontask::install
2024-01-26 10:02:53 +08:00
#
2023-08-23 09:09:51 +08:00
# Note: Ensure that the appropriate permissions and environmental variables are set prior to script execution.
2024-01-26 10:02:53 +08:00
#
2023-08-23 09:09:51 +08:00
OPENIM_ROOT = $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " /../.. && pwd -P)
2023-08-23 16:45:52 +08:00
[ [ -z ${ COMMON_SOURCED } ] ] && source " ${ OPENIM_ROOT } " /scripts/install/common.sh
2023-08-23 09:09:51 +08:00
SERVER_NAME = "openim-crontask"
2023-11-10 19:37:25 +08:00
function openim::crontask::start( ) {
2024-02-02 21:59:12 +08:00
rm -rf " $TMP_LOG_FILE "
2024-01-26 10:02:53 +08:00
openim::log::info " Start OpenIM Cron, binary root: ${ SERVER_NAME } "
openim::log::status " Start OpenIM Cron, path: ${ OPENIM_CRONTASK_BINARY } "
2024-02-27 17:58:54 +08:00
2024-01-26 10:02:53 +08:00
openim::log::status " start cron_task process, path: ${ OPENIM_CRONTASK_BINARY } "
2024-02-02 21:59:12 +08:00
2024-02-06 15:53:03 +08:00
nohup ${ OPENIM_CRONTASK_BINARY } -c ${ OPENIM_PUSH_CONFIG } >> ${ LOG_FILE } 2> >( tee -a " ${ STDERR_LOG_FILE } " " $TMP_LOG_FILE " >& 2) &
2024-02-27 17:58:54 +08:00
return 0
2024-02-02 21:59:12 +08:00
2023-08-23 09:09:51 +08:00
}
###################################### Linux Systemd ######################################
SYSTEM_FILE_PATH = " /etc/systemd/system/ ${ SERVER_NAME } .service "
# Print the necessary information after installation
function openim::crontask::info( ) {
cat << EOF
openim-crontask listen on: ${OPENIM_CRONTASK_HOST}
EOF
}
# install openim-crontask
2023-11-10 19:37:25 +08:00
function openim::crontask::install( ) {
2023-08-23 16:45:52 +08:00
pushd " ${ OPENIM_ROOT } "
2024-01-26 10:02:53 +08:00
2023-08-23 09:09:51 +08:00
# 1. Build openim-crontask
make build BINS = ${ SERVER_NAME }
2024-01-26 10:02:53 +08:00
2023-10-30 10:16:37 +08:00
openim::common::sudo " cp -r ${ OPENIM_OUTPUT_HOSTBIN } / ${ SERVER_NAME } ${ OPENIM_INSTALL_DIR } / ${ SERVER_NAME } "
openim::log::status " ${ SERVER_NAME } binary: ${ OPENIM_INSTALL_DIR } / ${ SERVER_NAME } / ${ SERVER_NAME } "
2024-01-26 10:02:53 +08:00
2023-08-23 09:09:51 +08:00
# 2. Generate and install the openim-crontask configuration file (openim-crontask.yaml)
2023-10-30 10:16:37 +08:00
openim::log::status " ${ SERVER_NAME } config file: ${ OPENIM_CONFIG_DIR } /config.yaml "
2024-01-26 10:02:53 +08:00
2023-08-23 09:09:51 +08:00
# 3. Create and install the ${SERVER_NAME} systemd unit file
echo ${ LINUX_PASSWORD } | sudo -S bash -c \
2024-01-26 10:02:53 +08:00
" SERVER_NAME= ${ SERVER_NAME } ./scripts/genconfig.sh ${ ENV_FILE } deployments/templates/openim.service > ${ SYSTEM_FILE_PATH } "
2023-08-23 09:09:51 +08:00
openim::log::status " ${ SERVER_NAME } systemd file: ${ SYSTEM_FILE_PATH } "
2024-01-26 10:02:53 +08:00
2023-08-23 09:09:51 +08:00
# 4. Start the openim-crontask service
openim::common::sudo "systemctl daemon-reload"
openim::common::sudo " systemctl restart ${ SERVER_NAME } "
openim::common::sudo " systemctl enable ${ SERVER_NAME } "
openim::crontask::status || return 1
openim::crontask::info
2024-01-26 10:02:53 +08:00
2023-08-23 09:09:51 +08:00
openim::log::info " install ${ SERVER_NAME } successfully "
popd
}
# Unload
2023-11-10 19:37:25 +08:00
function openim::crontask::uninstall( ) {
2023-08-23 09:09:51 +08:00
set +o errexit
openim::common::sudo " systemctl stop ${ SERVER_NAME } "
openim::common::sudo " systemctl disable ${ SERVER_NAME } "
2023-10-30 10:16:37 +08:00
openim::common::sudo " rm -f ${ OPENIM_INSTALL_DIR } / ${ SERVER_NAME } "
2023-08-23 09:09:51 +08:00
openim::common::sudo " rm -f ${ OPENIM_CONFIG_DIR } / ${ SERVER_NAME } .yaml "
openim::common::sudo " rm -f /etc/systemd/system/ ${ SERVER_NAME } .service "
2024-02-27 17:58:54 +08:00
2023-08-23 09:09:51 +08:00
openim::log::info " uninstall ${ SERVER_NAME } successfully "
}
# Status Check
2023-11-10 19:37:25 +08:00
function openim::crontask::status( ) {
2023-08-23 09:09:51 +08:00
# Check the running status of the ${SERVER_NAME}. If active (running) is displayed, the ${SERVER_NAME} is started successfully.
2023-10-30 10:16:37 +08:00
if systemctl is-active --quiet " ${ SERVER_NAME } " ; then
openim::log::info " ${ SERVER_NAME } is running successfully. "
else
2023-08-23 09:09:51 +08:00
openim::log::error " ${ SERVER_NAME } failed to start, maybe not installed properly "
return 1
fi
}
if [ [ " $* " = ~ openim::crontask:: ] ] ; then
eval $*
fi