Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
lbr38 committed Jul 16, 2024
1 parent d7a6990 commit 9136cd7
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 40 deletions.
38 changes: 0 additions & 38 deletions src/00_help

This file was deleted.

19 changes: 19 additions & 0 deletions src/controllers/Agent.py
Original file line number Diff line number Diff line change
@@ -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:

9 changes: 9 additions & 0 deletions src/controllers/Module/Module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
135 changes: 135 additions & 0 deletions src/controllers/Module/Reposerver/Agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

#-------------------------------------------------------------------------------------------------------------------
#
Expand Down Expand Up @@ -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
2 changes: 0 additions & 2 deletions src/controllers/Module/Reposerver/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 9136cd7

Please sign in to comment.