mirror of
https://github.com/aydwi/chaos.git
synced 2025-12-29 09:50:56 -06:00
Add utils and make some minor changes
This commit is contained in:
@@ -2,5 +2,5 @@
|
||||
"plaintext_only": false,
|
||||
"random_hit_chance": false,
|
||||
"random_instances": false,
|
||||
"save_logs": false
|
||||
"super_user": false
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import functools
|
||||
import os
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
46
utils/context_manager.py
Normal file
46
utils/context_manager.py
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user