Skip to content

Commit

Permalink
Merge pull request #4 from the-anweddol-project/beta-1.2.7
Browse files Browse the repository at this point in the history
Pull request for beta-1.2.7
  • Loading branch information
Anweddol authored Jul 8, 2023
2 parents 4bcd608 + 116dc6d commit 3962b50
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 114 deletions.
2 changes: 1 addition & 1 deletion anwdlserver/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "beta-1.1.7"
__version__ = "beta-1.2.7"
156 changes: 94 additions & 62 deletions anwdlserver/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
CLI : Main anwdlserver CLI process
"""
from subprocess import Popen, PIPE
from datetime import datetime
import daemon.pidfile
import argparse
Expand Down Expand Up @@ -101,8 +102,11 @@ def __init__(self):
exit(-1)

try:
getattr(self, args.command.replace("-", "_"))()
exit(0)
if getattr(self, args.command.replace("-", "_"))() != -1:
exit(0)

else:
exit(-1)

except KeyboardInterrupt:
self.__log_stdout("")
Expand All @@ -126,6 +130,13 @@ def __log_stdout(self, message, bypass=False):
def __log_json(self, status, message, data={}):
print(json.dumps({"status": status, "message": message, "data": data}))

def __is_libvirt_daemon_running(self):
out = Popen(
["/bin/systemctl", "status", "libvirtd.service"], shell=False, stdout=PIPE
).communicate()

return True if "Active: active (running)" in out[0].decode() else False

def __create_file_recursively(self, path, is_folder=False):
try:
os.makedirs(os.path.dirname(path) if not is_folder else path)
Expand Down Expand Up @@ -171,33 +182,19 @@ def start(self):

self.json = args.json

if os.path.exists(self.config_content["paths"].get("pid_file_path")):
self.__log_stdout(
f"A PID file already exists on {self.config_content['paths'].get('pid_file_path')}",
bypass=args.json,
)
choice = (
input(" ↳ Kill the affiliated processus (y/n) ? : ")
if not args.assume_yes and not args.assume_no
else ("y" if args.assume_yes else "n")
)

if choice == "y":
with open(self.config_content["paths"].get("pid_file_path"), "r") as fd:
os.kill(int(fd.read()), signal.SIGTERM)

while 1:
if isPortBindable(self.config_content["server"].get("listen_port")):
break

time.sleep(1)

self.__log_stdout("", bypass=args.json)

if not args.skip_check:
counter = 0
errors_list = []

if not self.__is_libvirt_daemon_running():
self.__log_stdout(
"- Libvirt daemon is not running",
bypass=args.json,
)

counter += 1
errors_list.append("Libvirt daemon is not running")

if not isPortBindable(self.config_content["server"].get("listen_port")):
self.__log_stdout(
f"- Port {self.config_content['server'].get('listen_port')} is not bindable",
Expand All @@ -217,9 +214,19 @@ def start(self):
bypass=args.json,
)

if (
self.config_content["container"].get("nat_interface_name")
== "virbr0"
):
self.__log_stdout(
" ↳ Try to start the libvirt daemon to fix this error",
bypass=args.json,
)

counter += 1
errors_list.append(
f"Interface '{self.config_content['container'].get('nat_interface_name')}' does not exists on system"
f"Interface '{self.config_content['container'].get('nat_interface_name')}' does not exists on system",
data={"hint": "Try to start the libvirt daemon to fix this error"},
)

if not isInterfaceExists(
Expand Down Expand Up @@ -247,16 +254,16 @@ def start(self):
)

if not os.path.exists(
self.config_content["paths"].get("container_iso_path")
self.config_content["container"].get("container_iso_path")
):
self.__log_stdout(
f"- {self.config_content['paths'].get('container_iso_path')} was not found on system",
f"- {self.config_content['container'].get('container_iso_path')} was not found on system",
bypass=args.json,
)

counter += 1
errors_list.append(
f"{self.config_content['paths'].get('container_iso_path')} was not found on system"
f"{self.config_content['container'].get('container_iso_path')} was not found on system"
)

if args.c:
Expand All @@ -272,7 +279,7 @@ def start(self):
f"\nCheck done. {counter} error(s) recorded\n", bypass=args.json
)

return
return 0

if counter != 0:
if args.json:
Expand All @@ -281,13 +288,38 @@ def start(self):
"Errors detected on server environment",
data={"errors_recorded": counter, "errors_list": errors_list},
)
return
return -1

else:
raise EnvironmentError(
f"{counter} error(s) detected on server environment"
)

if os.path.exists(self.config_content["server"].get("pid_file_path")):
self.__log_stdout(
f"A PID file already exists on {self.config_content['server'].get('pid_file_path')}",
bypass=args.json,
)
choice = (
input(" ↳ Kill the affiliated processus (y/n) ? : ")
if not args.assume_yes and not args.assume_no
else ("y" if args.assume_yes else "n")
)

if choice == "y":
with open(
self.config_content["server"].get("pid_file_path"), "r"
) as fd:
os.kill(int(fd.read()), signal.SIGTERM)

while 1:
if isPortBindable(self.config_content["server"].get("listen_port")):
break

time.sleep(1)

self.__log_stdout("", bypass=args.json)

if args.d:
self.__log_stdout(
"Direct execution mode enabled. Use CTRL+C to stop the server.",
Expand All @@ -307,7 +339,7 @@ def start(self):
uid=pwd.getpwnam(self.config_content["server"].get("user")).pw_uid,
gid=pwd.getpwnam(self.config_content["server"].get("user")).pw_gid,
pidfile=daemon.pidfile.PIDLockFile(
self.config_content["paths"].get("pid_file_path")
self.config_content["server"].get("pid_file_path")
),
):
launchServerProcess(self.config_content)
Expand All @@ -327,20 +359,20 @@ def stop(self):

self.json = args.json

if not os.path.exists(self.config_content["paths"].get("pid_file_path")):
if not os.path.exists(self.config_content["server"].get("pid_file_path")):
if args.json:
self.__log_json(LOG_JSON_STATUS_SUCCESS, "Server is already stopped")
return
return 0

self.__log_stdout("Server is already stopped\n")
return
return 0

with open(self.config_content["paths"].get("pid_file_path"), "r") as fd:
with open(self.config_content["server"].get("pid_file_path"), "r") as fd:
os.kill(int(fd.read()), signal.SIGTERM)

if args.json:
self.__log_json(LOG_JSON_STATUS_SUCCESS, "Server is stopped")
return
return 0

def restart(self):
parser = argparse.ArgumentParser(
Expand All @@ -355,15 +387,15 @@ def restart(self):

self.json = args.json

if not os.path.exists(self.config_content["paths"].get("pid_file_path")):
if not os.path.exists(self.config_content["server"].get("pid_file_path")):
if args.json:
self.__log_json(LOG_JSON_STATUS_SUCCESS, "Server is already stopped")
return
return 0

self.__log_stdout("Server is already stopped\n")
return
return 0

with open(self.config_content["paths"].get("pid_file_path"), "r") as fd:
with open(self.config_content["server"].get("pid_file_path"), "r") as fd:
os.kill(int(fd.read()), signal.SIGTERM)

while 1:
Expand All @@ -374,13 +406,13 @@ def restart(self):

if args.json:
self.__log_json(LOG_JSON_STATUS_SUCCESS, "Server is started")
return
return 0

with daemon.DaemonContext(
uid=pwd.getpwnam(self.config_content["server"].get("user")).pw_uid,
gid=pwd.getpwnam(self.config_content["server"].get("user")).pw_gid,
pidfile=daemon.pidfile.PIDLockFile(
self.config_content["paths"].get("pid_file_path")
self.config_content["server"].get("pid_file_path")
),
):
launchServerProcess(self.config_content)
Expand Down Expand Up @@ -427,14 +459,14 @@ def access_tk(self):
self.json = args.json

if not os.path.exists(
self.config_content["paths"].get("access_tokens_database_path")
self.config_content["access_token"].get("access_tokens_database_path")
):
self.__create_file_recursively(
self.config_content["paths"].get("access_tokens_database_path")
self.config_content["access_token"].get("access_tokens_database_path")
)

access_token_manager = AccessTokenManager(
self.config_content["paths"].get("access_tokens_database_path")
self.config_content["access_token"].get("access_tokens_database_path")
)

if args.a:
Expand All @@ -451,7 +483,7 @@ def access_tk(self):
"access_token": new_entry_tuple[2],
},
)
return
return 0

self.__log_stdout(
f"New access token created (Entry ID : {new_entry_tuple[0]})"
Expand All @@ -467,7 +499,7 @@ def access_tk(self):
"Recorded entries ID",
data={"entry_list": entry_list},
)
return
return 0

for entries in access_token_manager.listEntries():
self.__log_stdout(f"Entry ID {entries[0]}")
Expand All @@ -482,18 +514,18 @@ def access_tk(self):
LOG_JSON_STATUS_ERROR,
f"Entry ID {args.delete_entry} does not exists on database\n",
)
return
return 0

self.__log_stdout(
f"Entry ID {args.delete_entry} does not exists on database\n"
)
return
return 0

access_token_manager.deleteEntry(args.delete_entry)

if args.json:
self.__log_json(LOG_JSON_STATUS_SUCCESS, "Entry ID was deleted\n")
return
return 0

elif args.enable_entry:
if not access_token_manager.getEntry(args.enable_entry):
Expand All @@ -502,18 +534,18 @@ def access_tk(self):
LOG_JSON_STATUS_ERROR,
f"Entry ID {args.enable_entry} does not exists on database\n",
)
return
return 0

self.__log_stdout(
f"Entry ID {args.enable_entry} does not exists on database\n"
)
return
return 0

access_token_manager.enableEntry(args.enable_entry)

if args.json:
self.__log_json(LOG_JSON_STATUS_SUCCESS, "Entry ID was enabled")
return
return 0

else:
if args.disable_entry:
Expand All @@ -523,20 +555,20 @@ def access_tk(self):
LOG_JSON_STATUS_ERROR,
f"Entry ID {args.disable_entry} does not exists on database\n",
)
return
return 0

self.__log_stdout(
f"Entry ID {args.disable_entry} does not exists on database\n"
)
return
return 0

access_token_manager.disableEntry(args.disable_entry)

if args.json:
self.__log_json(
LOG_JSON_STATUS_SUCCESS, "Entry ID was disabled"
)
return
return 0

access_token_manager.closeDatabase()

Expand All @@ -562,21 +594,21 @@ def regen_rsa(self):
key_size=args.key_size if args.key_size else DEFAULT_RSA_KEY_SIZE
)

if not os.path.exists(self.config_content["paths"].get("rsa_keys_root_path")):
if not os.path.exists(self.config_content["server"].get("rsa_keys_root_path")):
self.__create_file_recursively(
self.config_content["paths"].get("rsa_keys_root_path"), is_folder=True
self.config_content["server"].get("rsa_keys_root_path"), is_folder=True
)

with open(
self.config_content["paths"].get("rsa_keys_root_path")
self.config_content["server"].get("rsa_keys_root_path")
+ "/"
+ PRIVATE_PEM_KEY_FILENAME,
"w",
) as fd:
fd.write(new_rsa_wrapper.getPrivateKey().decode())

with open(
self.config_content["paths"].get("rsa_keys_root_path")
self.config_content["server"].get("rsa_keys_root_path")
+ "/"
+ PUBLIC_PEM_KEY_FILENAME,
"w",
Expand All @@ -593,7 +625,7 @@ def regen_rsa(self):
).hexdigest()
},
)
return
return 0

self.__log_stdout("RSA keys re-generated")
self.__log_stdout(
Expand Down
Loading

0 comments on commit 3962b50

Please sign in to comment.