diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..28b9a68 --- /dev/null +++ b/Makefile @@ -0,0 +1,55 @@ +OS := $(shell uname -s) +CURRENT_USER := $(shell whoami) +PYTHON_VERSION := $(shell python3 -V) +HOME_DIR := $(shell echo $$HOME) +SHELL := /bin/bash + +install: +ifeq ($(wildcard config/configuration.yaml),) + $(error You must provide the Configuration file: config/configuration.yaml) +endif + +ifeq ($(OS), Linux) + $(info Installation for os : $(OS)) + $(info Python version: $(PYTHON_VERSION)) + $(info Home directory: $(HOME_DIR)) + + @python3 core/install.py --check-version + @if [ $$? -eq 0 ]; then \ + echo "Python Version OK! Well done :)"; \ + else \ + echo "Error: Script failed with exit code $$?"; \ + exit 1; \ + fi + + $(info Creating the systemd user folder...) + mkdir -p $(HOME_DIR)/.config/systemd/user + + $(info Creating Python Virtual Environment...) + python3 -m venv .pyenv + @. .pyenv/bin/activate && \ + python -m pip install --upgrade pip && \ + pip cache purge && \ + pip install -r requirements.txt + + @. .pyenv/bin/activate && python core/install.py --install + loginctl enable-linger $(CURRENT_USER) + @sleep 2 + @export echo $DBUS_SESSION_BUS_ADDRESS && \ + systemctl --user daemon-reload && \ + systemctl --user start defender + +endif + +clean: +ifeq ($(OS), Linux) + @if [ -e .pyenv ]; then \ + rm -rf .pyenv; \ + echo "Virtual Env has been removed!"; \ + fi + @if [ -e $(HOME_DIR)/.config/systemd/user/defender.service ]; then \ + rm $(HOME_DIR)/.config/systemd/user/defender.service; \ + echo "Systemd file has been removed!"; \ + fi + @export echo $DBUS_SESSION_BUS_ADDRESS && systemctl --user daemon-reload && echo "Systemd Daemon reloaded!" +endif diff --git a/core/install.py b/core/install.py new file mode 100644 index 0000000..dd5457c --- /dev/null +++ b/core/install.py @@ -0,0 +1,70 @@ +import argparse +import os +import sys +from pathlib import Path +from platform import python_version_tuple + +parser = argparse.ArgumentParser(description="Python Installation Code") +parser.add_argument('--check-version', action='store_true', help='Check if the python version is ok!') +parser.add_argument('--install', action='store_true', help='Run the installation') +args = parser.parse_args() + +PYTHON_REQUIRED_VERSION = (3, 10, 0) +PYTHON_SYSTEM_VERSION = tuple(map(int, python_version_tuple())) +ROOT_PATH = os.getcwd() +PYENV = Path(f'{ROOT_PATH}/.pyenv/bin/python') +USER_HOME_DIRECTORY = Path.home() +SYSTEMD_PATH = Path(USER_HOME_DIRECTORY).joinpath('.config', 'systemd', 'user') +PY_EXEC = 'defender.py' +SERVICE_FILE_NAME = 'defender.service' + +def check_python_requirement(): + if PYTHON_SYSTEM_VERSION < PYTHON_REQUIRED_VERSION: + raise RuntimeError(f"Your Python Version is not meeting the requirement, System Version: {PYTHON_SYSTEM_VERSION} < Required Version {PYTHON_REQUIRED_VERSION}") + +def create_service_file(): + + pyenv = PYENV + systemd_path = SYSTEMD_PATH + py_exec = PY_EXEC + service_file_name = SERVICE_FILE_NAME + + if not Path(systemd_path).exists(): + print("[!] Folder not available") + sys.exit(1) + + contain = f'''[Unit] +Description=Defender IRC Service + +[Service] +ExecStart={pyenv} {py_exec} +WorkingDirectory={ROOT_PATH} +SyslogIdentifier=Defender +Restart=on-failure + +[Install] +WantedBy=default.target +''' + with open(Path(systemd_path).joinpath(service_file_name), "w") as file: + file.write(contain) + print('Service file generated with current configuration') + print('Running IRC Service ...') + + print(f"#"*24) + print("Installation complete ...") + print("If the configuration is correct, then you must see your service connected to your irc server") + print(f"If any issue, you can see the log file for debug {ROOT_PATH}{os.sep}logs{os.sep}defender.log") + print(f"#"*24) + +def main(): + if args.check_version: + check_python_requirement() + sys.exit(0) + + if args.install: + create_service_file() + sys.exit(0) + + +if __name__ == "__main__": + main() diff --git a/core/irc.py b/core/irc.py index 5602238..8ee2e96 100644 --- a/core/irc.py +++ b/core/irc.py @@ -1220,7 +1220,7 @@ class Irc: self.Protocol.send_notice( nick_from=dnickname, nick_to=fromuser, - msg=f">> Defender V.{self.Config.CURRENT_VERSION} Developped by adatorĀ®." + msg=f">> Defender V{self.Config.CURRENT_VERSION} Developped by adatorĀ®." ) return None diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f8c2d3a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +Faker==33.1.2 +psutil==6.1.1 +PyYAML==6.0.2 +requests==2.32.3 +SQLAlchemy==2.0.36 +unrealircd_rpc_py==3.0.2 \ No newline at end of file