WIP add proper logging
This commit is contained in:
parent
b12d85e7f1
commit
52f52a61c7
|
@ -3,11 +3,26 @@
|
||||||
"""
|
"""
|
||||||
Service startup script.
|
Service startup script.
|
||||||
"""
|
"""
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import logging
|
||||||
from backive.core.events import EventInterface
|
from backive.core.events import EventInterface
|
||||||
from backive.config.config import Config
|
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:
|
class Backive:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._config = Config()
|
self._config = Config()
|
||||||
|
@ -22,7 +37,17 @@ class Backive:
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
del self._events
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
backive = Backive()
|
backive = None
|
||||||
backive.serve()
|
try:
|
||||||
|
backive = Backive()
|
||||||
|
backive.serve()
|
||||||
|
except Exception as e:
|
||||||
|
raise e
|
||||||
|
finally:
|
||||||
|
logging.info("Backive exited.")
|
||||||
|
del backive
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import pwd
|
import pwd
|
||||||
from ruamel.yaml import YAML
|
from ruamel.yaml import YAML
|
||||||
|
@ -9,23 +10,25 @@ from backive.core.device import Device
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
__shared_state = dict
|
__shared_state = dict()
|
||||||
|
_config = dict()
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.__dict__ = self.__shared_state
|
self.__dict__ = self.__shared_state
|
||||||
self._config = dict()
|
if not self._config:
|
||||||
self._schema = dict()
|
logging.info("Loading configuration...")
|
||||||
self._backups = list()
|
self._schema = dict()
|
||||||
self._devices = list()
|
self._backups = list()
|
||||||
file_path = os.path.realpath(__file__)
|
self._devices = list()
|
||||||
schema_path = os.path.join(
|
file_path = os.path.realpath(__file__)
|
||||||
os.path.dirname(
|
schema_path = os.path.join(
|
||||||
file_path
|
os.path.dirname(
|
||||||
),
|
file_path
|
||||||
"schema.yml"
|
),
|
||||||
)
|
"schema.yml"
|
||||||
with open(schema_path, "r") as stream:
|
)
|
||||||
self._schema = YAML().load(stream)
|
with open(schema_path, "r") as stream:
|
||||||
|
self._schema = YAML().load(stream)
|
||||||
|
|
||||||
def find_config(self):
|
def find_config(self):
|
||||||
# who are we?
|
# who are we?
|
||||||
|
|
|
@ -5,17 +5,18 @@ import asyncio
|
||||||
class EventInterface:
|
class EventInterface:
|
||||||
def __init__(self, event_callback, unix_socket_path=None, loop=None):
|
def __init__(self, event_callback, unix_socket_path=None, loop=None):
|
||||||
self.event_callback = event_callback
|
self.event_callback = event_callback
|
||||||
if not unix_socket_path:
|
self.unix_socket_path = unix_socket_path
|
||||||
unix_socket_path = "/tmp/backive/backive.sock"
|
if not self.unix_socket_path:
|
||||||
if not os.path.exists(os.path.dirname(unix_socket_path)):
|
self.unix_socket_path = "/tmp/backive/backive.sock"
|
||||||
os.makedirs(os.path.dirname(unix_socket_path))
|
if not os.path.exists(os.path.dirname(self.unix_socket_path)):
|
||||||
|
os.makedirs(os.path.dirname(self.unix_socket_path))
|
||||||
try:
|
try:
|
||||||
os.remove(unix_socket_path)
|
os.remove(self.unix_socket_path)
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
if not loop:
|
if not loop:
|
||||||
loop = asyncio.get_event_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):
|
async def client_connected(self, reader, writer):
|
||||||
print("client_connected")
|
print("client_connected")
|
||||||
|
@ -23,4 +24,6 @@ class EventInterface:
|
||||||
data = (await reader.read()).decode('utf8')
|
data = (await reader.read()).decode('utf8')
|
||||||
await self.event_callback(data)
|
await self.event_callback(data)
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
print("Removing socket file...")
|
||||||
|
os.remove(self.unix_socket_path)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,8 +13,10 @@ class Scheduler():
|
||||||
# who are we?
|
# who are we?
|
||||||
uid = os.getuid()
|
uid = os.getuid()
|
||||||
if uid == 0:
|
if uid == 0:
|
||||||
|
logging.info("Executing as root.")
|
||||||
self._data_file = "/var/lib/backive/data.json"
|
self._data_file = "/var/lib/backive/data.json"
|
||||||
else:
|
else:
|
||||||
|
logging.info("Executing as user.")
|
||||||
self._data_file = os.path.join(
|
self._data_file = os.path.join(
|
||||||
os.path.expanduser("~"),
|
os.path.expanduser("~"),
|
||||||
".config",
|
".config",
|
||||||
|
|
Loading…
Reference in New Issue