* server abstration
* default config file value
This commit is contained in:
@@ -1,11 +1,27 @@
|
|||||||
|
from distutils.command.config import config
|
||||||
import os
|
import os
|
||||||
import psutil
|
import psutil
|
||||||
import logging
|
import logging
|
||||||
|
import yaml
|
||||||
from typing import List
|
from typing import List
|
||||||
|
from onlyone import namedpipes
|
||||||
|
|
||||||
|
DEFAULT_CONFIG_PATH = "/etc/config.yaml"
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def config_file_to_dict(path:str):
|
||||||
|
if not os.path.isfile(path):
|
||||||
|
log.error("missing file " + path)
|
||||||
|
raise "missing file " + path
|
||||||
|
return
|
||||||
|
log.info("trying to read file : " + path)
|
||||||
|
configTxt = open(path, "r").read()
|
||||||
|
log.info("config file content : " + configTxt)
|
||||||
|
log.info("trying read yaml")
|
||||||
|
retval = yaml.safe_load(configTxt)
|
||||||
|
return retval
|
||||||
|
|
||||||
def __is_process(cmd:str, arr):
|
def __is_process(cmd:str, arr):
|
||||||
item = " ".join(arr)
|
item = " ".join(arr)
|
||||||
#log.info("[onlyone][startprocess] -> " + item + " ends with " + cmd)
|
#log.info("[onlyone][startprocess] -> " + item + " ends with " + cmd)
|
||||||
@@ -64,20 +80,6 @@ def killprocesses(killcmds:List):
|
|||||||
return __killprocesses(killcmds, psutil.process_iter())
|
return __killprocesses(killcmds, psutil.process_iter())
|
||||||
|
|
||||||
|
|
||||||
# def __forceoneprocessinstance(cmd:str, processes:List):
|
|
||||||
# found = False
|
|
||||||
# for proc in processes:
|
|
||||||
# if __is_process(cmd, proc.cmdline()):
|
|
||||||
# if found:
|
|
||||||
# proc.kill()
|
|
||||||
# log.info("[onlyone][__forceoneprocessinstance] -> processed killed : " + proc.name + "(" + proc.id + ")")
|
|
||||||
# else :
|
|
||||||
# found = True
|
|
||||||
# if not found:
|
|
||||||
# startprocess(cmd)
|
|
||||||
|
|
||||||
# def forceoneprocessinstance(cmd:str):
|
|
||||||
# return __forceoneprocessinstance(cmd, psutil.process_iter())
|
|
||||||
|
|
||||||
def onlyone(cmd:str, killcmds:list):
|
def onlyone(cmd:str, killcmds:list):
|
||||||
log.info("[onlyone][onlyone] -> invoked start " + cmd + ", kill " + str(killcmds))
|
log.info("[onlyone][onlyone] -> invoked start " + cmd + ", kill " + str(killcmds))
|
||||||
@@ -126,3 +128,11 @@ class Manager:
|
|||||||
self._current = item["key"]
|
self._current = item["key"]
|
||||||
|
|
||||||
|
|
||||||
|
class Server:
|
||||||
|
def __init__(self, configPath):
|
||||||
|
self.manager = Manager()
|
||||||
|
if(configPath != None and configPath!=""):
|
||||||
|
configDict = config_file_to_dict(configPath)
|
||||||
|
self.manager.load(configDict, True)
|
||||||
|
self.npserver = namedpipes.Server(self.manager)
|
||||||
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from asyncio.base_events import Server
|
||||||
import logging
|
import logging
|
||||||
import argparse
|
import argparse
|
||||||
from types import new_class
|
from types import new_class
|
||||||
@@ -21,27 +22,10 @@ args = parser.parse_args()
|
|||||||
|
|
||||||
log.info("arguments - " + str(sys.argv))
|
log.info("arguments - " + str(sys.argv))
|
||||||
log.info("Current Path:" + os.getcwd())
|
log.info("Current Path:" + os.getcwd())
|
||||||
manager=None
|
|
||||||
if args.configFile:
|
|
||||||
manager = onlyone.Manager()
|
|
||||||
if os.path.isfile(args.configFile):
|
|
||||||
log.info("trying to read file : " + args.configFile)
|
|
||||||
configTxt = open(args.configFile, "r").read()
|
|
||||||
log.info("config file content : " + configTxt)
|
|
||||||
log.info("trying read yaml")
|
|
||||||
configDict = yaml.safe_load(configTxt)
|
|
||||||
manager.load(configDict, True)
|
|
||||||
else:
|
|
||||||
log.error("Current Path:" + os.getcwd())
|
|
||||||
log.error("File dont exists:" + args.configfile)
|
|
||||||
|
|
||||||
if args.server:
|
if args.server:
|
||||||
if(manager==None):
|
configPath = onlyone.DEFAULT_CONFIG_PATH
|
||||||
log.error("server mode selected, but manager not loaded maybe missing --configFile")
|
if args.configFile:
|
||||||
else:
|
configPath = args.configFile
|
||||||
npServer = None
|
server = onlyone.Server(configPath)
|
||||||
try:
|
|
||||||
npServer = onlyone.namedpipes.Server(manager, "/tmp/onlyone_fifo")
|
|
||||||
log.info("server loaded...")
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
log.info("KeyboardInterrupt")
|
|
||||||
@@ -2,24 +2,61 @@
|
|||||||
import os
|
import os
|
||||||
import errno
|
import errno
|
||||||
import logging
|
import logging
|
||||||
|
from signal import default_int_handler
|
||||||
import threading
|
import threading
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
FIFO_PATH="/tmp/onlyone_fifo"
|
||||||
|
SECURITY_GROUP="onlyone"
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def securitygroup_check(groupName:str):
|
||||||
|
if not securitygroup_exists(groupName):
|
||||||
|
securitygroup_create(groupName)
|
||||||
|
|
||||||
|
def securitygroup_create(groupName:str):
|
||||||
|
try:
|
||||||
|
|
||||||
|
subprocess.run(['groupadd ', 'groupName'])
|
||||||
|
except:
|
||||||
|
print(f"Failed to create group " + groupName)
|
||||||
|
raise
|
||||||
|
|
||||||
|
def securityfifo_checkpermissions(fifoPath:str, groupName:str):
|
||||||
|
try:
|
||||||
|
|
||||||
|
subprocess.run(['chown', 'root:' + groupName, fifoPath])
|
||||||
|
subprocess.run(['chmod', '660', fifoPath])
|
||||||
|
except:
|
||||||
|
print(f"Failed to set permissions")
|
||||||
|
raise
|
||||||
|
|
||||||
|
def securitygroup_exists(groupName:str):
|
||||||
|
with open("/etc/group", "r") as groupFile:
|
||||||
|
for line in groupFile:
|
||||||
|
if line.startswith(groupName):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def createfifo(path):
|
||||||
|
try:
|
||||||
|
os.mkfifo(path)
|
||||||
|
except OSError as oe:
|
||||||
|
if oe.errno != errno.EEXIST:
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Server:
|
class Server:
|
||||||
def __init__(self, manager, fifoname:str):
|
def __init__(self, manager):
|
||||||
self.manager=manager
|
self.manager=manager
|
||||||
self__fifoname=fifoname
|
self__fifoname=""
|
||||||
try:
|
securitygroup_check(SECURITY_GROUP)
|
||||||
os.mkfifo(fifoname)
|
createfifo(FIFO_PATH)
|
||||||
except OSError as oe:
|
securityfifo_checkpermissions(FIFO_PATH, SECURITY_GROUP)
|
||||||
if oe.errno != errno.EEXIST:
|
|
||||||
raise
|
|
||||||
|
|
||||||
readTask = ReadTask(self)
|
readTask = ReadTask(self)
|
||||||
self.__readThread = threading.Thread(target=readTask.run, args = (fifoname,) )
|
self.__readThread = threading.Thread(target=readTask.run, args = (FIFO_PATH,) )
|
||||||
self.__readThread.start()
|
self.__readThread.start()
|
||||||
|
|
||||||
def kill(self):
|
def kill(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user