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 9136cd7 commit 200537b
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 72 deletions.
24 changes: 11 additions & 13 deletions .github/workflows/build-deb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build and test deb package for linupdate

on:
push:
branches: [ devel ]
branches: [ python ]
pull_request:
push:
branches: [ main ]
Expand All @@ -29,21 +29,19 @@ jobs:
mkdir -p /tmp/linupdate-build/DEBIAN
mkdir -p /tmp/linupdate-build/etc/linupdate/modules
mkdir -p /tmp/linupdate-build/opt/linupdate
mkdir -p /tmp/linupdate-build/opt/linupdate/.src/
mkdir -p /tmp/linupdate-build/opt/linupdate/mods-available/
mkdir -p /tmp/linupdate-build/opt/linupdate/mods-enabled/
mkdir -p /tmp/linupdate-build/opt/linupdate/agents-available/
mkdir -p /tmp/linupdate-build/opt/linupdate/service/
mkdir -p /tmp/linupdate-build/lib/systemd/system/
mkdir -p /tmp/linupdate-build/opt/linupdate/src/
# TODO: service not working for now
# mkdir -p /tmp/linupdate-build/opt/linupdate/service/
# mkdir -p /tmp/linupdate-build/lib/systemd/system/

- name: Copy files to include in the build
run: |
cp -r ${GITHUB_WORKSPACE}/src/* /tmp/linupdate-build/opt/linupdate/.src/
cp -r ${GITHUB_WORKSPACE}/mods-available/* /tmp/linupdate-build/opt/linupdate/mods-available/
cp -r ${GITHUB_WORKSPACE}/service/* /tmp/linupdate-build/opt/linupdate/service/
cp ${GITHUB_WORKSPACE}/linupdate /tmp/linupdate-build/opt/linupdate/linupdate
cp -r ${GITHUB_WORKSPACE}/src/* /tmp/linupdate-build/opt/linupdate/src/
cp ${GITHUB_WORKSPACE}/linupdate.py /tmp/linupdate-build/opt/linupdate/linupdate.py
cp ${GITHUB_WORKSPACE}/version /tmp/linupdate-build/opt/linupdate/version
cp -r ${GITHUB_WORKSPACE}/service/linupdate.systemd.template /tmp/linupdate-build/lib/systemd/system/linupdate.service
# TODO: service not working for now
# cp -r ${GITHUB_WORKSPACE}/service/* /tmp/linupdate-build/opt/linupdate/service/
# cp -r ${GITHUB_WORKSPACE}/service/linupdate.systemd.template /tmp/linupdate-build/lib/systemd/system/linupdate.service

- name: Copy control file
run: |
Expand Down Expand Up @@ -145,4 +143,4 @@ jobs:
- name: Install package
run: |
sudo apt-get update -y
sudo apt-get install -y ./linupdate-test-build_${{ env.VERSION }}_all.deb
sudo apt-get install -y ./linupdate-test-build_${{ env.VERSION }}_all.deb
4 changes: 2 additions & 2 deletions .github/workflows/packaging/deb/control
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Version: __VERSION__
Section: main
Priority: optional
Architecture: all
Depends: curl, git, apt-transport-https, aptitude, mutt, locales, ngrep, inotify-tools, jq, virt-what, net-tools, dnsutils, locales-all
Depends: apt-transport-https, locales, ngrep, inotify-tools, virt-what, net-tools, dnsutils, locales-all, python3-tabulate, python3-colorama, python3-dateutil
Maintainer: Ludovic <[email protected]>
Description: Linupdate package updater - Repomanager client side agent
Description: Linupdate 3+ (python version) - A package updater and Repomanager's client side agent
Homepage: https://github.com/lbr38/linupdate
46 changes: 21 additions & 25 deletions .github/workflows/packaging/deb/postinst
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ DATA_DIR="/opt/linupdate"
SERVICE="$DATA_DIR/service/linupdate-agent"

# Restore configuration file if exists
if [ -f "/tmp/linupdate.conf.debsave" ];then
rm -f /etc/linupdate/linupdate.conf
mv /tmp/linupdate.conf.debsave /etc/linupdate/linupdate.conf
if [ -f "/tmp/linupdate.yml.debsave" ];then
rm -f /etc/linupdate/linupdate.yml
mv /tmp/linupdate.yml.debsave /etc/linupdate/linupdate.yml
fi

# Create a symlink to main script
ln -sf /opt/linupdate/linupdate /usr/bin/linupdate

# Delete old 'functions' directory if exists
if [ -d "$DATA_DIR/functions" ];then
rm -rf "$DATA_DIR/functions"
fi
ln -sf /opt/linupdate/linupdate.py /usr/bin/linupdate

# Install en_US.UTF-8 locale if not present
if ! locale -a | grep -q "en_US.UTF-8";then
Expand All @@ -27,19 +22,20 @@ chmod 750 /etc/linupdate
chmod 750 /opt/linupdate

# Only if systemd is installed (not the case on github runners)
if [ -f "/usr/bin/systemctl" ];then

# Enable service script by creating a symlink
ln -sf /lib/systemd/system/linupdate.service /etc/systemd/system/linupdate.service
chmod 550 "$SERVICE"
chown root:root "$SERVICE"

/usr/bin/systemctl --quiet daemon-reload

# Start service
if /usr/bin/systemctl is-active --quiet linupdate;then
/usr/bin/systemctl restart --quiet linupdate
else
/usr/bin/systemctl start --quiet linupdate
fi
fi
# TODO: service is not working for now
# if [ -f "/usr/bin/systemctl" ];then

# # Enable service script by creating a symlink
# ln -sf /lib/systemd/system/linupdate.service /etc/systemd/system/linupdate.service
# chmod 550 "$SERVICE"
# chown root:root "$SERVICE"

# /usr/bin/systemctl --quiet daemon-reload

# # Start service
# if /usr/bin/systemctl is-active --quiet linupdate;then
# /usr/bin/systemctl restart --quiet linupdate
# else
# /usr/bin/systemctl start --quiet linupdate
# fi
# fi
4 changes: 2 additions & 2 deletions .github/workflows/packaging/deb/preinst
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash

# Save current configuration file if exists
if [ -f "/etc/linupdate/linupdate.conf" ];then
cp /etc/linupdate/linupdate.conf /tmp/linupdate.conf.debsave
if [ -f "/etc/linupdate/linupdate.yml" ];then
cp /etc/linupdate/linupdate.yml /tmp/linupdate.yml.debsave
fi

# Only if systemd is installed (not the case on github runners)
Expand Down
20 changes: 20 additions & 0 deletions linupdate-agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/python
# coding: utf-8

# Import libraries
import time

# Import classes
from src.controllers.App.Service import Service

# Leave some time for the system to boot
# TODO
# time.sleep(60)

# Instantiate Service class
my_service = Service()

# Execute main function
my_service.main()

exit(0)
2 changes: 1 addition & 1 deletion service/linupdate.systemd.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Description=linupdate-agent

[Service]
Type=simple
ExecStart=/opt/linupdate/service/linupdate-agent
ExecStart=/opt/linupdate/service/linupdate-agent.py

[Install]
WantedBy=multi-user.target
19 changes: 0 additions & 19 deletions src/controllers/Agent.py

This file was deleted.

124 changes: 124 additions & 0 deletions src/controllers/App/Service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# coding: utf-8

# Import libraries
import subprocess
import signal
import sys
import importlib
import subprocess
from pathlib import Path

# Import classes
from src.controllers.Module.Module import Module

class Service:
def __init__(self):
self.child_processes = []
self.moduleController = Module()

#-------------------------------------------------------------------------------------------------------------------
#
# Check if a restart of this service is needed, and restart it if needed
#
#-------------------------------------------------------------------------------------------------------------------
def restart_self_if_needed(self):
if Path('/tmp/linupdate-service.restart').is_file():
# Only restart the service if linupdate is not running otherwise it could cut off a running update...
if Path('/tmp/linupdate.lock').is_file():
return

print('A restart of this service is required. Restarting...')
Path('/tmp/linupdate-service.restart').unlink()
subprocess.run(["systemctl", "restart", "linupdate.service"])

result = subprocess.run(
["systemctl", "--quiet", "restart", "linupdate.service"],
capture_output = True,
text = True
)

# If service could not be restarted, print error and exit
if result.returncode != 0:
print('Error: could not restart linupdate service: ' + result.stderr)
exit(1)


#-------------------------------------------------------------------------------------------------------------------
#
# Service main function
#
#-------------------------------------------------------------------------------------------------------------------
def main(self):
try:
# Check if a restart of this service is needed
self.restart_self_if_needed()

# Retrieve enabled modules
enabled_modules = self.moduleController.getEnabled()

# For each enabled module, check if its agent is enabled
for module in enabled_modules:
# Convert module name to uppercase first letter
module_name = module.capitalize()

# Import python module config class
module_import_path = importlib.import_module('src.controllers.Module.' + module_name + '.Config')
module_class = getattr(module_import_path, 'Config')

# Instantiate module and get module configuration
my_module = module_class()
module_configuration = my_module.getConf()

# Check if agent is enabled
if module_configuration['agent']['enabled']:
print('Executing agent for module ' + module_name)

# Import python module agent class
module_import_path = importlib.import_module('src.controllers.Module.' + module_name + '.Agent')
my_module_agent_class = getattr(module_import_path, 'Agent')()

# Instantiate module and call module agent main method in a child process
# process = subprocess.Popen(['python3', '-c', 'import src.controllers.Module.' + module_name + '.Agent; src.controllers.Module.' + module_name + '.Agent.main()'])
# self.child_processes.append(process)

except Exception as e:
print('Linupdate service error:' + str(e))
exit(1)


#-------------------------------------------------------------------------------------------------------------------
#
# Stop all child processes
#
#-------------------------------------------------------------------------------------------------------------------
def stop_child_processes(self):
for process in self.child_processes:
process.terminate()
try:
process.wait(timeout=5)
except subprocess.TimeoutExpired:
process.kill()


#-------------------------------------------------------------------------------------------------------------------
#
# Signal handler
# This function is called when the service receives a SIGTERM or SIGINT signal
#
#-------------------------------------------------------------------------------------------------------------------
def signal_handler(sig, frame):
print('Linupdate service received signal ' + str(sig) + '. Stopping all child processes')
service.stop_child_processes()
sys.exit(0)


#-------------------------------------------------------------------------------------------------------------------
#
# Main
#
#-------------------------------------------------------------------------------------------------------------------
if __name__ == "__main__":
service = Service()
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
service.main()
13 changes: 4 additions & 9 deletions src/controllers/Module/Reposerver/Agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def setListenEnable(self, value: bool):







Expand Down Expand Up @@ -187,17 +187,13 @@ def main(self):
# 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()
self.reposerverStatusController.sendGeneralStatus()
self.reposerverStatusController.sendPackagesStatus()

# Reset counter
counter = 0
Expand All @@ -211,7 +207,7 @@ def main(self):
# inotify_package_event()

# If ngrep scans are enabled, then execute them in background
if configuration['agent']['listen']['enabled']:
# if configuration['agent']['listen']['enabled']:
# Monitor general informations sending requests
# TODO
# ngrep_general_update_request
Expand All @@ -223,7 +219,6 @@ def main(self):
# Monitor package update requests
# TODO
# ngrep_packages_update_requested
print('toto')

time.sleep(5)

Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.13
3.0.0

0 comments on commit 200537b

Please sign in to comment.