diff --git a/daemon-common/daemon_lib/ansiiprint.py b/daemon-common/daemon_lib/ansiiprint.py deleted file mode 100644 index 69cc1b0e..00000000 --- a/daemon-common/daemon_lib/ansiiprint.py +++ /dev/null @@ -1,80 +0,0 @@ -#!/usr/bin/env python3 - -# ansiprint.py - Printing function for formatted messages -# Part of the Parallel Virtual Cluster (PVC) system -# -# Copyright (C) 2018 Joshua M. Boniface -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -############################################################################### - -import datetime - -# ANSII colours for output -def red(): - return '\033[91m' -def blue(): - return '\033[94m' -def green(): - return '\033[92m' -def yellow(): - return '\033[93m' -def purple(): - return '\033[95m' -def bold(): - return '\033[1m' -def end(): - return '\033[0m' - -# Print function -def echo(message, prefix, state): - # Get the date - date = '{} - '.format(datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S.%f')) - endc = end() - - # Continuation - if state == 'c': - date = '' - colour = '' - prompt = ' ' - # OK - elif state == 'o': - colour = green() - prompt = '>>> ' - # Error - elif state == 'e': - colour = red() - prompt = '>>> ' - # Warning - elif state == 'w': - colour = yellow() - prompt = '>>> ' - # Tick - elif state == 't': - colour = purple() - prompt = '>>> ' - # Information - elif state == 'i': - colour = blue() - prompt = '>>> ' - else: - colour = bold() - prompt = '>>> ' - - # Append space to prefix - if prefix != '': - prefix = prefix + ' ' - - print(colour + prompt + endc + date + prefix + message) diff --git a/daemon-common/daemon_lib/common.py b/daemon-common/daemon_lib/common.py deleted file mode 100644 index aa2ef492..00000000 --- a/daemon-common/daemon_lib/common.py +++ /dev/null @@ -1,82 +0,0 @@ -#!/usr/bin/env python3 - -# common.py - PVC daemon function library, common fuctions -# Part of the Parallel Virtual Cluster (PVC) system -# -# Copyright (C) 2018 Joshua M. Boniface -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -############################################################################### - -import subprocess -import threading -import signal -import os -import time - -import daemon_lib.ansiiprint as ansiiprint - -class OSDaemon(object): - def __init__(self, command, environment): - self.proc = subprocess.Popen( - command, - env=environment, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) - - def signal(self, sent_signal): - signal_map = { - 'hup': signal.SIGHUP, - 'int': signal.SIGINT, - 'term': signal.SIGTERM - } - self.proc.send_signal(signal_map[sent_signal]) - -def run_os_daemon(command_string, background=False, environment=None, return_pid=False): - command = command_string.split() - daemon = OSDaemon(command, environment) - return daemon - -# Run a oneshot command, optionally without blocking -def run_os_command(command_string, background=False, environment=None): - command = command_string.split() - if background: - def runcmd(): - subprocess.run( - command, - env=environment, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) - thread = threading.Thread(target=runcmd, args=()) - thread.start() - return 0, None, None - else: - command_output = subprocess.run( - command, - env=environment, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) - return command_output.returncode, command_output.stdout.decode('ascii'), command_output.stderr.decode('ascii') - -# Reload the firewall rules of the system -def reload_firewall_rules(rules_dir): - ansiiprint.echo('Updating firewall rules', '', 'o') - rules_file = '{}/base.nft'.format(rules_dir) - retcode, stdout, stderr = run_os_command('/usr/sbin/nft -f {}'.format(rules_file)) - if retcode != 0: - ansiiprint.echo('Failed to reload rules: {}'.format(stderr), '', 'e') diff --git a/daemon-common/daemon_lib/zkhandler.py b/daemon-common/daemon_lib/zkhandler.py deleted file mode 100644 index 690aa770..00000000 --- a/daemon-common/daemon_lib/zkhandler.py +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/env python3 - -# zkhandler.py - Secure versioned ZooKeeper updates -# Part of the Parallel Virtual Cluster (PVC) system -# -# Copyright (C) 2018 Joshua M. Boniface -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -############################################################################### - -import kazoo.client -import daemon_lib.ansiiprint as ansiiprint - -# Child list function -def listchildren(zk_conn, key): - children = zk_conn.get_children(key) - return children - -# Key deletion function -def delete(zk_conn, key): - zk_conn.delete(key, recursive=True) - -# Data read function -def readdata(zk_conn, key): - data_raw = zk_conn.get(key) - data = data_raw[0].decode('ascii') - meta = data_raw[1] - return data - -# Data write function -def writedata(zk_conn, kv): - # Start up a transaction - zk_transaction = zk_conn.transaction() - - # Proceed one KV pair at a time - for key in sorted(kv): - data = kv[key] - if not data: - data = '' - - # Check if this key already exists or not - if not zk_conn.exists(key): - # We're creating a new key - zk_transaction.create(key, data.encode('ascii')) - else: - # We're updating a key with version validation - orig_data = zk_conn.get(key) - version = orig_data[1].version - - # Set what we expect the new version to be - new_version = version + 1 - - # Update the data - zk_transaction.set_data(key, data.encode('ascii')) - - # Set up the check - try: - zk_transaction.check(key, new_version) - except TypeError: - print('Zookeeper key "{}" does not match expected version'.format(key)) - return False - - # Commit the transaction - try: - zk_transaction.commit() - return True - except Exception: - return False -