Add openrc service script

This commit is contained in:
Sameer Rahmani 2022-04-19 17:27:33 +01:00
parent 5eebcb6815
commit c23314e120
5 changed files with 79 additions and 25 deletions

10
builder
View File

@ -124,7 +124,7 @@ function run() { ## Setup the working directory and make it ready for developmen
function rigel() { ## Run the rigel server function rigel() { ## Run the rigel server
# shellcheck source=.venv/bin/activate # shellcheck source=.venv/bin/activate
. "$ME/.venv/bin/activate" . "$ME/.venv/bin/activate"
"$ME/rigel/server.py" "$ME/.env" $1 "$ME/rigel/server.py" --env "$ME/.env" $1
deactivate 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}' 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%<root-project>%$ME%g" /tmp/rigel
sudo cp "/tmp/rigel" /etc/init.d/
sudo chmod 0755 /etc/init.d/rigel
}
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Main logic # Main logic
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------

3
env.example Normal file
View File

@ -0,0 +1,3 @@
BOT_TOKEN=<telegrom-bot-token>
HOST=localhost
PORT=6666

View File

@ -12,7 +12,7 @@ In order to use it, first you need to setup the project which you can just call
script like: script like:
```bash ```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 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. close the connection to that client.
Have Fun! Have Fun!
## TODO
* Fix the bug when client closes the connect prematurely.
* Make Rigel install friendly

View File

@ -12,11 +12,12 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
import argparse
import asyncio
import os import os
import sys import sys
import signal import signal
import asyncio
import daemon import daemon
import dotenv import dotenv
@ -33,28 +34,45 @@ def main(conf):
if __name__ == "__main__": if __name__ == "__main__":
config = dotenv.dotenv_values(sys.argv[1]) parser = argparse.ArgumentParser(description='Rigel, Text to Speech engine.')
action = sys.argv[2] # parser.add_argument('--daemon', action='store_true',
pfile = config.get("PID_FILE", "/var/run/rigel.pid") # help='Run Rigel in background')
logs = config.get("LOGFILE")
logfile = open(logs, 'a+') if logs else sys.stdout
pidlock = pidfile.PIDFile(pfile) parser.add_argument('--env',
help='The env file to use.')
if action == "status": # parser.add_argument('--log-dir', type=argparse.FileType('r'),
print("Running" if pidlock.is_running else "Not running.") # help='The env file to use.')
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, # parser.add_argument('--pid', type=argparse.FileType('a'),
stderr=logfile, # help='The pid file path to create')
pidfile=pidlock):
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)

View File

@ -0,0 +1,21 @@
#!/sbin/openrc-run
#
# Orion Rigel --- Text to Speech engine
#
# Copyright (c) 2022 Sameer Rahmani <lxsameer@gnu.org>
#
# 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="<root-project>/.venv/bin/python"
command_args="<root-project>/rigel/server.py --env <root-project>/.env"
pidfile="/run/${RC_SVCNAME}.pid"
command_background=true
output_log="/var/log/${RC_SVCNAME}.log"
error_log="/var/log/${RC_SVCNAME}.log"