diff --git a/src/00_help b/src/00_help deleted file mode 100644 index 05b903e..0000000 --- a/src/00_help +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env bash - -# Print help - -function help { - echo -e "Available parameters:\n" - echo -e " Main:" - echo -e " --vv|-vv → Enable verbose mode" - echo -e " --version|-v → Print current version" - echo -e " --profile|--type|--print-profile PROFILE → Configure host profile (leave empty to print actual)" - echo -e " --environment|--env ENV → Configure host environment (leave empty to print actual)" - echo -e "" - echo -e " Package update configuration" - echo -e " --exclude-major|-em PACKAGE → Configure packages to exclude on major release update, separated by a comma. Specify 'none' to clean." - echo -e " --exclude|-e PACKAGE → Configure packages to exclude, separated by a comma. Specify 'none' to clean." - echo -e "" - echo -e " Package update execution" - echo -e " --check-updates|-cu → Check packages to be updated and quit" - echo -e " --assume-yes|--force → Enable 'assume yes' (answer 'yes' to every confirm prompt)" - echo -e " --dist-upgrade|-du → Enable 'dist-upgrade' for apt (Debian only)" - echo -e " --keep-oldconf|-ko → Keep actual configuration file when attempting to be overwrited by apt during package update (Debian only)" - echo -e " --ignore-exclude|-ie → Ignore all packages minor or major release update exclusions" - echo -e "" - echo -e " Modules" - echo -e " --list-modules|--list-mod|-m → List available modules" - echo -e " --mod-enable|-mod-enable|-me MODULE → Enable specified module" - echo -e " --mod-disable|-mod-disable|-md MODULE → Disable specified module" - echo -e " --mod-configure|-mc|--mod-exec MODULE → Configure specified module (using module commands, see module help or documentation)" - echo -e " --mod-configure MODULE --help → Print module help" - echo -e "" - echo -e " Agent" - echo -e " --agent-start → Start linupdate agent" - echo -e " --agent-stop → Stop linupdate agent" - echo -e " --agent-restart → Restart linupdate agent" - echo -e " --agent-enable → Enable linupdate agent start on boot" - echo -e " --agent-disable → Disable linupdate agent start on boot" - echo -e "" -} \ No newline at end of file diff --git a/src/controllers/Agent.py b/src/controllers/Agent.py new file mode 100644 index 0000000..72ac5a2 --- /dev/null +++ b/src/controllers/Agent.py @@ -0,0 +1,19 @@ +# coding: utf-8 + +# Import libraries +import time + +class Agent(): + #------------------------------------------------------------------------------------------------------------------- + # + # Agent main function + # + #------------------------------------------------------------------------------------------------------------------- + def main(): + # Leave some time for the system to boot + time.sleep(60) + + try: + + except Exception as e: + \ No newline at end of file diff --git a/src/controllers/Module/Module.py b/src/controllers/Module/Module.py index 3f616ee..f4a2236 100644 --- a/src/controllers/Module/Module.py +++ b/src/controllers/Module/Module.py @@ -15,6 +15,15 @@ def __init__(self): self.exitController = Exit() self.loadedModules = [] + #------------------------------------------------------------------------------------------------------------------- + # + # Return list of enabled modules + # + #------------------------------------------------------------------------------------------------------------------- + def getEnabled(self): + return self.configController.getConf()['modules']['enabled'] + + #------------------------------------------------------------------------------------------------------------------- # # List available modules diff --git a/src/controllers/Module/Reposerver/Agent.py b/src/controllers/Module/Reposerver/Agent.py index 1271b94..6de141e 100644 --- a/src/controllers/Module/Reposerver/Agent.py +++ b/src/controllers/Module/Reposerver/Agent.py @@ -2,13 +2,20 @@ # Import libraries from colorama import Fore, Style +import subprocess +import time +from pathlib import Path # Import classes +from src.controllers.Module.Module import Module +from src.controllers.Module.Reposerver.Status import Status from src.controllers.Module.Reposerver.Config import Config class Agent: def __init__(self): + self.moduleController = Module() self.configController = Config() + self.reposerverStatusController = Status() #------------------------------------------------------------------------------------------------------------------- # @@ -93,3 +100,131 @@ def setListenEnable(self, value: bool): except Exception as e: raise Exception('could not set agent listening enable to ' + str(value) + ': ' + str(e)) + + + + + + + + + + + + + + def general_checks(self): + enabled_modules = self.moduleController.getEnabled() + + # Checking that reposerver module is enabled + if 'reposerver' not in enabled_modules: + raise Exception('reposerver module is not enabled') + + # Checking that a configuration file exists for reposerver module + if not Path('/etc/linupdate/modules/reposerver.yml').is_file(): + raise Exception('reposerver module configuration file does not exist') + + # Checking that a log file exists for yum/dnf or apt + if Path('/var/log/yum.log').is_file(): + self.log_file = '/var/log/yum.log' + elif Path('/var/log/dnf.log').is_file(): + self.log_file = '/var/log/dnf.log' + elif Path('/var/log/apt/history.log').is_file(): + self.log_file = '/var/log/apt/history.log' + else: + raise Exception('no log file found for yum/dnf or apt') + + + #------------------------------------------------------------------------------------------------------------------- + # + # Reposerver agent main function + # + #------------------------------------------------------------------------------------------------------------------- + def main(self): + self.ngrep_cmd = 'ngrep -q -t -W byline' + self.ngrep_interface = None + counter = 0 + + # Checking that all the necessary elements are present for the agent execution + self.general_checks() + + # Get current configuration + configuration = self.configController.getConf() + + # If ngrep scans are enabled + if configuration['agent']['listen']['enabled']: + # Retrieving network interface to scan if specified + interface = configuration['agent']['listen']['interface'] + + # If network interface is specified with "auto" or is empty, then try to automatically retrieve default interface + if interface == 'auto' or not interface: + # Get default network interface + result = subprocess.run( + ["/usr/sbin/route | /usr/bin/grep '^default' | /usr/bin/grep -o '[^ ]*$'"], + capture_output = True, + text = True, + shell = True + ) + + if result.returncode != 0: + raise Exception('could not determine default network interface on which to listen: ' + result.stderr) + + interface = result.stdout.strip() + + # Count the number of lines returned + lines = interface.split('\n') + + # If more than one line is returned, then there is a problem + if len(lines) > 1: + raise Exception('could not determine default network interface on which to listen: multiple default interfaces have been detected') + + # Taking into account the network interface + self.ngrep_interface = interface + + # Executing regular tasks + while True: + # Checking that all the necessary elements are present for the agent execution. + # This is checked every time in case that a configuration change has been made in the configuration file + self.general_checks() + + # Check if a restart of this service is needed + # TODO + # checkRestartNeeded + + # Regulary sending data to the Repomanager server (every hour) + # 3600 / 5sec (sleep 5) = 720 + if counter == 0 or counter == 720: + # Sending full status + print('Periodically sending informations about this host to the repomanager server') + self.sendFullStatus() + self.reposerverStatusController.sendFullStatus() + + # Reset counter + counter = 0 + + # If no inotify process is running, then execute it in background + # TODO + # inotify_package_event() + + # If no inotify process is running, then execute it in background + # TODO + # inotify_package_event() + + # If ngrep scans are enabled, then execute them in background + if configuration['agent']['listen']['enabled']: + # Monitor general informations sending requests + # TODO + # ngrep_general_update_request + + # Monitor packages informations sending requests + # TODO + # ngrep_packages_status_request + + # Monitor package update requests + # TODO + # ngrep_packages_update_requested + print('toto') + + time.sleep(5) + + counter+=1 diff --git a/src/controllers/Module/Reposerver/Config.py b/src/controllers/Module/Reposerver/Config.py index 9bac747..aa01bed 100644 --- a/src/controllers/Module/Reposerver/Config.py +++ b/src/controllers/Module/Reposerver/Config.py @@ -185,8 +185,6 @@ def checkConf(self): if configuration['client']['get_profile_pkg_conf_from_reposerver'] not in [True, False]: raise Exception('client ' + Fore.YELLOW + 'get_profile_pkg_conf_from_reposerver' + Style.RESET_ALL + ' must be set to True or False') - - # Check if get_profile_repos_from_reposerver exists and is set (True or False) if 'get_profile_repos_from_reposerver' not in configuration['client']: raise Exception(Fore.YELLOW + 'client get_profile_repos_from_reposerver' + Style.RESET_ALL + ' not found in configuration file')