From c23314e1200323ee467b21f37330de4636b99081 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Tue, 19 Apr 2022 17:27:33 +0100 Subject: [PATCH] Add openrc service script --- builder | 10 +++++- env.example | 3 ++ rigel/README.md | 6 +++- rigel/server.py | 64 ++++++++++++++++++++++++-------------- rigel/service/openrc/rigel | 21 +++++++++++++ 5 files changed, 79 insertions(+), 25 deletions(-) create mode 100644 env.example create mode 100644 rigel/service/openrc/rigel diff --git a/builder b/builder index fdc8358..b0f57bb 100755 --- a/builder +++ b/builder @@ -124,7 +124,7 @@ function run() { ## Setup the working directory and make it ready for developmen function rigel() { ## Run the rigel server # shellcheck source=.venv/bin/activate . "$ME/.venv/bin/activate" - "$ME/rigel/server.py" "$ME/.env" $1 + "$ME/rigel/server.py" --env "$ME/.env" $1 deactivate } @@ -136,6 +136,14 @@ function help() { ## Print out this help message awk 'BEGIN {FS=":"}; {printf "\033[36m%-30s\033[0m %s\n", $1, $2}' } +function install-openrc() { ## Install openrc service for Rigel(doesn't enable it) + # shellcheck source=.venv/bin/activate + cp "$ME/rigel/service/openrc/rigel" /tmp/rigel + sed -i "s%%$ME%g" /tmp/rigel + sudo cp "/tmp/rigel" /etc/init.d/ + sudo chmod 0755 /etc/init.d/rigel +} + # ----------------------------------------------------------------------------- # Main logic # ----------------------------------------------------------------------------- diff --git a/env.example b/env.example new file mode 100644 index 0000000..7fc7aab --- /dev/null +++ b/env.example @@ -0,0 +1,3 @@ +BOT_TOKEN= +HOST=localhost +PORT=6666 diff --git a/rigel/README.md b/rigel/README.md index 4681824..372e14b 100644 --- a/rigel/README.md +++ b/rigel/README.md @@ -12,7 +12,7 @@ In order to use it, first you need to setup the project which you can just call script like: ```bash -./builder rigel [start|stop|status] +./builder rigel ``` Rigel will listen to the tcp port provided by HOST and PORT vars in `.env`(at the root of @@ -24,3 +24,7 @@ It will reply back with "OK". Also if it get the message "//close" then it will close the connection to that client. Have Fun! + +## TODO +* Fix the bug when client closes the connect prematurely. +* Make Rigel install friendly diff --git a/rigel/server.py b/rigel/server.py index f739562..fc243ab 100755 --- a/rigel/server.py +++ b/rigel/server.py @@ -12,11 +12,12 @@ # 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 + +import argparse +import asyncio import os import sys import signal -import asyncio - import daemon import dotenv @@ -33,28 +34,45 @@ def main(conf): if __name__ == "__main__": - config = dotenv.dotenv_values(sys.argv[1]) - action = sys.argv[2] - pfile = config.get("PID_FILE", "/var/run/rigel.pid") - logs = config.get("LOGFILE") - logfile = open(logs, 'a+') if logs else sys.stdout + parser = argparse.ArgumentParser(description='Rigel, Text to Speech engine.') + # parser.add_argument('--daemon', action='store_true', + # help='Run Rigel in background') - pidlock = pidfile.PIDFile(pfile) + parser.add_argument('--env', + help='The env file to use.') - if action == "status": - print("Running" if pidlock.is_running else "Not running.") - elif action == "stop": - if pidlock.is_running: - print("Terminating...") - f = open(pfile, "r") - pid = int(f.read()) - os.kill(pid, signal.SIGTERM) - else: - print("Not running.") - elif action == "start": + # parser.add_argument('--log-dir', type=argparse.FileType('r'), + # help='The env file to use.') - with daemon.DaemonContext(stdout=logfile, - stderr=logfile, - pidfile=pidlock): + # parser.add_argument('--pid', type=argparse.FileType('a'), + # help='The pid file path to create') - main(config) + args = parser.parse_args() + #print(args.pid) + + config = dotenv.dotenv_values(args.env) + # action = sys.argv[2] + # pfile = config.get("PID_FILE", "/var/run/rigel.pid") + # logs = config.get("LOGFILE") + # logfile = open(logs, 'a+') if logs else sys.stdout + + # pidlock = pidfile.PIDFile(pfile) + + # if action == "status": + # print("Running" if pidlock.is_running else "Not running.") + # elif action == "stop": + # if pidlock.is_running: + # print("Terminating...") + # f = open(pfile, "r") + # pid = int(f.read()) + # os.kill(pid, signal.SIGTERM) + # else: + # print("Not running.") + # elif action == "start": + + # with daemon.DaemonContext(stdout=logfile, + # stderr=logfile, + # pidfile=pidlock): + + # main(config) + main(config) diff --git a/rigel/service/openrc/rigel b/rigel/service/openrc/rigel new file mode 100644 index 0000000..70a4145 --- /dev/null +++ b/rigel/service/openrc/rigel @@ -0,0 +1,21 @@ +#!/sbin/openrc-run +# +# Orion Rigel --- Text to Speech engine +# +# Copyright (c) 2022 Sameer Rahmani +# +# 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, either version 2 of the License. +# 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 + +command="/.venv/bin/python" +command_args="/rigel/server.py --env /.env" +pidfile="/run/${RC_SVCNAME}.pid" +command_background=true +output_log="/var/log/${RC_SVCNAME}.log" +error_log="/var/log/${RC_SVCNAME}.log"