refs #1014 remove "global" statements

oglivecli-no-daemon
Natalia Serrano 2024-10-22 10:23:41 +00:00
parent ff09ae25ec
commit 5c5a6a1e4d
1 changed files with 26 additions and 40 deletions

View File

@ -11,6 +11,7 @@ import platform, os, sys, subprocess, datetime, shutil, pwd, glob, logging, dist
ipxe_repo_url = "https://github.com/ipxe/ipxe.git"
oglive_iso_url = "https://ognproject.evlt.uma.es/trac/downloads/ogLive-focal-5.13.0-27-beta-amd64-r20210706.5b4bf5f.iso"
SAMBACFGDIR = "/etc/samba"
TFTPCFGDIR = "/var/lib/tftpboot"
PROGRAM = os.path.splitext(os.path.basename(sys.argv[0]))[0]
PROGRAM_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
@ -28,7 +29,6 @@ OPENGNSYS_CLIENT_PASSWD = config["ogBootSambaPass"]
IPXE_DIR = "/tmp/ogboot_ipxe"
DEFAULTDEV = ""
PACKAGES_TO_INSTALL = ["htop"]
DEVICE = []
SERVERIP = []
NETIP = []
@ -36,7 +36,6 @@ NETMASK = []
NETBROAD = []
ROUTERIP = []
BRANCH = sys.argv[1] if len(sys.argv) > 1 else "main"
INETDSERV = "xinetd"
UBUNTU_OS_VERSION = "24"
PYTHON_VERSION = 3
@ -96,8 +95,8 @@ def check_distribution():
###############################################################################
def get_missing_packages():
global PACKAGES_TO_INSTALL
faltantes = []
PACKAGES_TO_INSTALL = []
OSDISTRIB = distro.name()
OSVERSION = distro.version()
logger.info(f"OSDISTRIB: {OSDISTRIB}")
@ -148,31 +147,30 @@ def install_packages(log_packages_file="/tmp/installed_packages.log"):
if not missing:
logger.info("All packages are already installed.")
return
logger.info("Upgrading the system...")
try:
subprocess.run(["apt-get", "update"], check=True)
subprocess.run(
["apt-get", "install", "--allow-change-held-packages", "-y", "--no-install-recommends"] + missing + ['apache2-'],
check=True
)
with open(log_packages_file, "a") as log:
for package in missing:
logger.info(f"{package} installed correctly.")
log.write(package + "\n")
logger.info("All missing packages have been installed.")
# Check PHP version and install corresponding php-fpm package
php_version = subprocess.check_output(["php", "-v"]).decode("utf-8")
if "PHP 8.1" in php_version:
subprocess.run(["apt-get", "install", "-y", "php8.1-fpm"], check=True)
elif "PHP 8.3" in php_version:
subprocess.run(["apt-get", "install", "-y", "php8.3-fpm"], check=True)
else:
logger.warning("PHP version not supported.")
subprocess.run(["apt-get", "update"], check=True)
subprocess.run(
["apt-get", "install", "--allow-change-held-packages", "-y", "--no-install-recommends"] + missing + ['apache2-'],
check=True
)
with open(log_packages_file, "a") as log:
for package in missing:
logger.info(f"{package} installed correctly.")
log.write(package + "\n")
logger.info("All missing packages have been installed.")
# Check PHP version and install corresponding php-fpm package
php_version = subprocess.check_output(["php", "-v"]).decode("utf-8")
if "PHP 8.1" in php_version:
subprocess.run(["apt-get", "install", "-y", "php8.1-fpm"], check=True)
elif "PHP 8.3" in php_version:
subprocess.run(["apt-get", "install", "-y", "php8.3-fpm"], check=True)
else:
logger.warning("PHP version not supported.")
def add_sudoers_permissions():
sudoers_entry = """
@ -199,7 +197,6 @@ def og_core_create_user(u):
logger.info(f"User {u} created successfully.")
def og_boot_create_dirs():
global INSTALL_OGBOOT_TARGET
if os.path.exists(INSTALL_OGBOOT_TARGET):
if not os.path.isdir(INSTALL_OGBOOT_TARGET):
raise NotADirectoryError(f"{INSTALL_OGBOOT_TARGET} exists and is not a directory.")
@ -221,7 +218,6 @@ def og_boot_create_dirs():
exit(1)
def og_boot_symfony_install():
global INSTALL_OGBOOT_TARGET
logger.info("Creating Symfony application skeleton...")
try:
@ -368,9 +364,6 @@ def add_user_to_disk_group():
raise Exception("Error al añadir el usuario 'ogboot' al grupo 'disk'.")
def tftpConfigure():
global INETDSERV
TFTPCFGDIR = "/var/lib/tftpboot"
logger.info("Configuring tftpd-hpa...")
tftpd_config = """# /etc/default/tftpd-hpa
@ -428,8 +421,6 @@ TFTP_OPTIONS="--secure"
os.lchown(symlink_target_ogLive, pwd.getpwnam("tftp").pw_uid, pwd.getpwnam("ogboot").pw_gid)
os.lchown(symlink_target_ogclient, pwd.getpwnam("tftp").pw_uid, pwd.getpwnam("ogboot").pw_gid)
logger.info(f"Changing properties for {symlink_target_ogLive} and {symlink_target_ogclient}")
logger.error(f"{symlink_target_ogLive} link does not exists.")
else:
logger.error(f"{symlink_target_ogLive} link does not exist.")
except subprocess.CalledProcessError as e:
@ -490,7 +481,6 @@ def tzConfigure():
file.write(f"# OpenGnsys Server timezone.\nTZ=\"{TZ}\"\n")
def install_ipxe():
global IPXE_DIR, INSTALL_OGBOOT_TARGET
clone_dir = "/tmp/ogboot_ipxe"
if os.path.exists (f"{INSTALL_OGBOOT_TARGET}/tftpboot/undionly.kpxe"):
@ -555,8 +545,6 @@ def get_ip_address(interface):
return None
def generate_ipxe_script():
global DEFAULTDEV, INSTALL_OGBOOT_TARGET
ip_address_server = get_ip_address(DEFAULTDEV)
template = os.path.join(REPO_DIR, "etc/dhcp_boot.ipxe.tmpl")
ipxe_output = f"{INSTALL_OGBOOT_TARGET}/tftpboot/ipxe_scripts/dhcp_boot.ipxe"
@ -586,7 +574,6 @@ def user_exists(user):
return False
def smbConfigure():
global OPENGNSYS_CLIENT_PASSWD, OPENGNSYS_CLIENT_USER, PYTHON_VERSION
backupFile(f"{SAMBACFGDIR}/smb.conf")
# Copiar plantilla de recursos para OpenGnsys
with open(os.path.join(REPO_DIR, 'etc/smb-ogboot.conf.tmpl'), 'r') as tmpl_file:
@ -632,7 +619,6 @@ def smbConfigure():
return 0
def setup_nginx():
global DEFAULTDEV
try:
# Obtener la IP del servidor
ip_address_server = get_ip_address(DEFAULTDEV)