From 30fc3e33834c7865f9e2a190355d7ac4170cdefe Mon Sep 17 00:00:00 2001 From: Ayush Date: Tue, 11 Dec 2018 05:02:08 +0530 Subject: [PATCH] Add utils and make some minor changes --- config/daemon.json | 2 +- src/chaos.py | 3 +-- src/chaosd.py | 11 ++++++---- src/config.py | 9 ++++---- utils/context_manager.py | 46 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 utils/context_manager.py diff --git a/config/daemon.json b/config/daemon.json index 462bf47..db056ba 100644 --- a/config/daemon.json +++ b/config/daemon.json @@ -2,5 +2,5 @@ "plaintext_only": false, "random_hit_chance": false, "random_instances": false, - "save_logs": false + "super_user": false } \ No newline at end of file diff --git a/src/chaos.py b/src/chaos.py index 6d474bf..1bc17a0 100644 --- a/src/chaos.py +++ b/src/chaos.py @@ -1,5 +1,4 @@ -#!/usr/bin/python3 -# -*- coding: utf-8 -*- +#!/usr/bin/env python3 import functools import os diff --git a/src/chaosd.py b/src/chaosd.py index 595317a..e2d5cea 100644 --- a/src/chaosd.py +++ b/src/chaosd.py @@ -1,14 +1,17 @@ +#!/usr/bin/env python3 + import daemon from chaos import initialize, execute """ Add/Fix: -1. Logging -2. pidfile -3. File descriptors -4. Tests and coverage +1. Daemon behaviour - privileges, pidfile, boot +2. Tests and coverage +3. CI +4. Dependency management 5. Build system +6. init.py """ context = daemon.DaemonContext(umask=0o002) diff --git a/src/config.py b/src/config.py index 430d895..020de08 100644 --- a/src/config.py +++ b/src/config.py @@ -11,7 +11,7 @@ class DaemonConfig: self.default_config = {"plaintext_only": False, "random_hit_chance": False, "random_instances": False, - "save_logs": False} + "super_user": False} self.custom_config = self.default_config def setup(self): @@ -31,19 +31,20 @@ class DaemonConfig: pass def valid(self): - """Return a bool indicating the validity of custom daemon configuration.""" + """Return a bool indicating whether the provided daemon + configuration file follows the specification.""" schemas = [{"plaintext_only": {"type": "boolean", "allowed": [False]}, "random_hit_chance": {"type": "boolean"}, "random_instances": {"type": "boolean", "forbidden": [True]}, - "save_logs": {"type": "boolean"}}, + "super_user": {"type": "boolean"}}, {"plaintext_only": {"type": "boolean", "allowed": [True]}, "random_hit_chance": {"type": "boolean"}, "random_instances": {"type": "boolean"}, - "save_logs": {"type": "boolean"}}] + "super_user": {"type": "boolean"}}] schema = {"c": {"oneof_schema": schemas, "type": "dict"}} v = Validator(schema) diff --git a/utils/context_manager.py b/utils/context_manager.py new file mode 100644 index 0000000..7428b9a --- /dev/null +++ b/utils/context_manager.py @@ -0,0 +1,46 @@ +# Context manager for a pid (process id) file used to tell whether +# a daemon process is still running. On entry, it writes the pid +# of the current process to the path. On exit, it removes the file. + +# Copyright 2013 Graham Poulter + +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. + + +import fcntl +import os + + +class PidFile(object): + + def __init__(self, path): + self.path = path + self.pidfile = None + + def __enter__(self): + self.pidfile = open(self.path, "a+") + try: + fcntl.flock(self.pidfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) + except IOError: + raise SystemExit("Already running according to " + self.path) + self.pidfile.seek(0) + self.pidfile.truncate() + self.pidfile.write(str(os.getpid())) + self.pidfile.flush() + self.pidfile.seek(0) + return self.pidfile + + def __exit__(self, exc_type=None, exc_value=None, exc_tb=None): + try: + self.pidfile.close() + except IOError as err: + if err.errno != 9: + raise + os.remove(self.path)