WIP add proper logging

This commit is contained in:
Marcel Otte 2020-01-06 13:26:52 +01:00
parent b12d85e7f1
commit 52f52a61c7
4 changed files with 57 additions and 23 deletions

View File

@ -3,11 +3,26 @@
"""
Service startup script.
"""
import sys
import os
import asyncio
import logging
from backive.core.events import EventInterface
from backive.config.config import Config
rootlogger = logging.getLogger()
rootlogger.setLevel(logging.DEBUG)
consoleHandler = logging.StreamHandler(sys.stdout)
fileHandler = logging.FileHandler("/var/log/backive/backive.log")
if not os.path.exists("/var/log/backive"):
os.makedirs("/var/log/backive")
rootlogger.addHandler(consoleHandler)
rootlogger.addHandler(fileHandler)
logging.info("Backive starting.")
class Backive:
def __init__(self):
self._config = Config()
@ -22,7 +37,17 @@ class Backive:
loop.run_forever()
pass
def __del__(self):
del self._events
if __name__ == "__main__":
backive = Backive()
backive.serve()
backive = None
try:
backive = Backive()
backive.serve()
except Exception as e:
raise e
finally:
logging.info("Backive exited.")
del backive

View File

@ -1,3 +1,4 @@
import logging
import os
import pwd
from ruamel.yaml import YAML
@ -9,23 +10,25 @@ from backive.core.device import Device
class Config:
__shared_state = dict
__shared_state = dict()
_config = dict()
def __init__(self):
self.__dict__ = self.__shared_state
self._config = dict()
self._schema = dict()
self._backups = list()
self._devices = list()
file_path = os.path.realpath(__file__)
schema_path = os.path.join(
os.path.dirname(
file_path
),
"schema.yml"
)
with open(schema_path, "r") as stream:
self._schema = YAML().load(stream)
if not self._config:
logging.info("Loading configuration...")
self._schema = dict()
self._backups = list()
self._devices = list()
file_path = os.path.realpath(__file__)
schema_path = os.path.join(
os.path.dirname(
file_path
),
"schema.yml"
)
with open(schema_path, "r") as stream:
self._schema = YAML().load(stream)
def find_config(self):
# who are we?

View File

@ -5,17 +5,18 @@ import asyncio
class EventInterface:
def __init__(self, event_callback, unix_socket_path=None, loop=None):
self.event_callback = event_callback
if not unix_socket_path:
unix_socket_path = "/tmp/backive/backive.sock"
if not os.path.exists(os.path.dirname(unix_socket_path)):
os.makedirs(os.path.dirname(unix_socket_path))
self.unix_socket_path = unix_socket_path
if not self.unix_socket_path:
self.unix_socket_path = "/tmp/backive/backive.sock"
if not os.path.exists(os.path.dirname(self.unix_socket_path)):
os.makedirs(os.path.dirname(self.unix_socket_path))
try:
os.remove(unix_socket_path)
os.remove(self.unix_socket_path)
except OSError:
pass
if not loop:
loop = asyncio.get_event_loop()
loop.create_task(asyncio.start_unix_server(self.client_connected, unix_socket_path))
loop.create_task(asyncio.start_unix_server(self.client_connected, self.unix_socket_path))
async def client_connected(self, reader, writer):
print("client_connected")
@ -23,4 +24,6 @@ class EventInterface:
data = (await reader.read()).decode('utf8')
await self.event_callback(data)
def __del__(self):
print("Removing socket file...")
os.remove(self.unix_socket_path)

View File

@ -1,5 +1,6 @@
import os
import json
import logging
from datetime import datetime
@ -12,8 +13,10 @@ class Scheduler():
# who are we?
uid = os.getuid()
if uid == 0:
logging.info("Executing as root.")
self._data_file = "/var/lib/backive/data.json"
else:
logging.info("Executing as user.")
self._data_file = os.path.join(
os.path.expanduser("~"),
".config",