From dfa432fd6696e1005a4cc38db9a3c7e2acfb6612 Mon Sep 17 00:00:00 2001 From: aguerrero Date: Mon, 17 Jun 2024 04:50:46 +0000 Subject: [PATCH] Refs #438 Add changes for Ubuntu 22.04 --- installer/ogboot_installer.py | 159 +++++++++++++++++++++++++--------- 1 file changed, 117 insertions(+), 42 deletions(-) diff --git a/installer/ogboot_installer.py b/installer/ogboot_installer.py index 4bbb87d..123f970 100755 --- a/installer/ogboot_installer.py +++ b/installer/ogboot_installer.py @@ -5,16 +5,26 @@ ##### Autor: Antonio Emmanuel Guerrero Silva ######## ################################################################################# -import platform, os, sys, subprocess, datetime, shutil, pwd, glob, zipfile, urllib.request, logging +import platform, os, sys, subprocess, datetime, shutil, pwd, glob, zipfile, urllib.request, logging, distro global PACKAGES, UBUNTU_OS_VERSION, PYTHON_VERSION, INSTALL_OGBOOT_TARGET, PROGRAM_DIR, OPENGNSYS_SERVER +# Reload variables from bashrc +subprocess.run(['source', '~/.bashrc'], shell=True) + # Leer variables de entorno -env_ogCore_ServerIP = os.environ.get('OGCORE_SERVER_IP') #"172.17.8.82" -env_ogCore_Server = os.environ.get('OGCORE_SERVER') #"opengnsys.es" -env_ogCore_Dir = os.environ.get('OGCORE_DIR') #{INSTALL_OPENGNSYS_TARGET} -env_ogBoot_Dir = os.environ.get('OGBOOT_DIR') #{INSTALL_OGBOOT_TARGET} -env_ogBoot_GitRepo = os.environ.get('OGBOOT_GIT_REPO') #"ssh://git@ognproject.evlt.uma.es:21987/opengnsys/ogboot.git" +#env_ogCore_ServerIP = os.environ.get('OGCORE_SERVER_IP') #"172.17.8.82" +#env_ogCore_Server = os.environ.get('OGCORE_SERVER') #"opengnsys.es" +#env_ogCore_Dir = os.environ.get('OGCORE_DIR') #{INSTALL_OPENGNSYS_TARGET} +#env_ogBoot_Dir = os.environ.get('OGBOOT_DIR') #{INSTALL_OGBOOT_TARGET} +#env_ogBoot_GitRepo = os.environ.get('OGBOOT_GIT_REPO') #"ssh://git@ognproject.evlt.uma.es:21987/opengnsys/ogboot.git" + +# Leer variables de entorno +env_ogCore_ServerIP = "172.17.8.82" #os.getenv('OGCORE_SERVER_IP') #"172.17.8.82" +env_ogCore_Server = "opengnsys.es" #os.getenv('OGCORE_SERVER') #"opengnsys.es" +env_ogCore_Dir = "/opt/opengnsys" #os.getenv('OGCORE_DIR') #{INSTALL_OPENGNSYS_TARGET} +env_ogBoot_Dir = "/opt/ogboot" #os.getenv('OGBOOT_DIR') #{INSTALL_OGBOOT_TARGET} +env_ogBoot_GitRepo = "ssh://git@ognproject.evlt.uma.es:21987/opengnsys/ogboot.git" #os.getenv('OGBOOT_GIT_REPO') #"ssh://git@ognproject.evlt.uma.es:21987/opengnsys/ogboot.git" PROGRAM = os.path.splitext(os.path.basename(sys.argv[0]))[0] PROGRAM_DIR = os.path.dirname(os.path.realpath(sys.argv[0])) @@ -48,7 +58,7 @@ INETDSERV = "xinetd" OPENGNSYS_CLIENT_PASSWD="og" OPENGNSYS_CLIENT_USER="opengnsys" -UBUNTU_OS_VERSION = "18" +UBUNTU_OS_VERSION = "22" PYTHON_VERSION = 3 if os.path.isdir(f"{PROGRAM_DIR}/../installer"): @@ -56,6 +66,13 @@ if os.path.isdir(f"{PROGRAM_DIR}/../installer"): else: REMOTE = 1 +# Create log file with sudo and set owner to ogboot +log_file = f'/var/log/{PROGRAM}.log' +os.makedirs(os.path.dirname(log_file), exist_ok=True) +subprocess.run(['sudo', 'touch', log_file]) +#subprocess.run(['sudo', 'chown', 'ogboot', log_file]) +subprocess.run(['sudo', 'chmod', '777', log_file]) + #Configurar el registro logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s\t%(message)s', @@ -151,12 +168,16 @@ def install_packages(missing, log_packages_file=f"/tmp/installed_packages.log"): logger.info("All missing packages have been installed.") def autoConfigure(): - distribution = platform.linux_distribution() - OSDISTRIB = distribution[0] - OSVERSION = distribution[1] + #distribution = platform.linux_distribution() + #distribution = distro.linux_distribution() + #OSDISTRIB = distribution[0] + OSDISTRIB = distro.name() + OSVERSION = distro.version() + #OSVERSION = distribution[1] logger.info(f"OSDISTRIB: {OSDISTRIB}") logger.info(f"OSVERSION: {OSVERSION}") - + print(f'{OSDISTRIB}...............{OSDISTRIB}') + print(f'{OSVERSION}...............{OSVERSION}') # Configuración según la distribución OSDISTRIB if OSDISTRIB == 'Ubuntu': # Código para Ubuntu @@ -170,7 +191,8 @@ def autoConfigure(): pass elif OSVERSION == '22.04': # Código para Ubuntu 22.04 - PACKAGES = ["apache2", "arp-scan", "automake", "build-essential", "btrfs-progs", "composer", "curl", "ctorrent", "debootstrap", "g++-multilib", "gawk", "gettext", "graphviz", "grub-efi-amd64-signed", "isc-dhcp-server", "jq", "libapache2-mod-php", "libdbi-dev", "libdbi1", "libev-dev", "libjansson-dev", "liblz4-tool", "libssl-dev", "moreutils", "netpipes", "php", "php-bcmath", "php-cli", "php-curl", "php-fpm", "php-gd", "php-json", "php-ldap", "php-mbstring", "php-mysql", "php-pdo", "php-pear", "php-xml", "php-zip", "procps", "coreutils", "rsync", "samba", "schroot", "shim-signed", "squashfs-tools", "subversion", "tftp-hpa", "tftpd-hpa", "udpcast", "unzip", "wakeonlan", "wget", "xinetd"] + print(f'ubu 22.04') + PACKAGES = ["apache2", "arp-scan", "automake", "build-essential", "btrfs-progs", "composer", "curl", "ctorrent", "debootstrap", "g++-multilib", "gawk", "gettext", "graphviz", "grub-efi-amd64-signed", "kea-dhcp-server", "jq", "libapache2-mod-php", "libdbi-dev", "libdbi1", "libev-dev", "libjansson-dev", "liblz4-tool", "libssl-dev", "moreutils", "netpipes", "php", "php-bcmath", "php-cli", "php-curl", "php-fpm", "php-gd", "php-json", "php-ldap", "php-mbstring", "php-mysql", "php-pdo", "php-pear", "php-xml", "php-zip", "procps", "coreutils", "rsync", "samba", "schroot", "shim-signed", "squashfs-tools", "subversion", "tftpd-hpa", "udpcast", "unzip", "wakeonlan", "wget", "xinetd"] pass else: logger.error("Ubuntu version not supported by ogBoot.") @@ -458,28 +480,74 @@ def testPxe(): logger.error("TFTP service is down.") os.remove(f"{TFTPCFGDIR}/testpxe") +def run_command(command): + result = subprocess.run(command, shell=True, text=True, capture_output=True) + if result.returncode != 0: + print(f"Error executing command: {command}") + print(result.stderr) + exit(1) + print(result.stdout) + def tftpConfigure(): - if TFTPSERV: - logger.info(f"TFTPSERV is configured: {TFTPSERV}") - inetd_cfg_path = f"{INETDCFGDIR}/{TFTPSERV}" - if os.path.isfile(inetd_cfg_path): - logger.error(f"The inetd configuration file exists: {inetd_cfg_path}") - with open(inetd_cfg_path, "r+") as file: - content = file.read() - new_content = content.replace("disable.*", "disable = no") - file.seek(0) - file.write(new_content) - file.truncate() - logger.info(f"Modified inetd configuration file: {inetd_cfg_path}") - else: - service = TFTPSERV - logger.info(f"Enabling and starting the service {service}.service") - subprocess.run(["systemctl", "enable", f"{service}.service"], check=True) - subprocess.run(["systemctl", "start", f"{service}.service"], check=True) - service = INETDSERV - logger.info(f"Enabling and starting the service {service}.service") - subprocess.run(["systemctl", "enable", f"{service}.service"], check=True) - subprocess.run(["systemctl", "start", f"{service}.service"], check=True) +# Update the package list + print("Updating package list...") + run_command("sudo apt update") + + run_command("sudo DEBIAN_FRONTEND=noninteractive apt install -y build-essential") + + # Install tftpd-hpa in non-interactive mode + print("Installing tftpd-hpa...") + run_command("sudo DEBIAN_FRONTEND=noninteractive apt install -y tftpd-hpa") + + # Configure tftpd-hpa + print("Configuring tftpd-hpa...") + tftpd_config = """ + # /etc/default/tftpd-hpa + +TFTP_USERNAME="tftp" +TFTP_DIRECTORY="/var/lib/tftpboot" +TFTP_ADDRESS="0.0.0.0:69" +TFTP_OPTIONS="--secure" + """ + with open("/tmp/tftpd-hpa", "w") as config_file: + config_file.write(tftpd_config) + + run_command("sudo mv /tmp/tftpd-hpa /etc/default/tftpd-hpa") + + # Create and set permissions for the TFTP directory + print("Creating and setting permissions for the TFTP directory...") + run_command("sudo mkdir -p /var/lib/tftpboot") + run_command("sudo chown -R tftp:tftp /var/lib/tftpboot") + run_command("sudo chmod -R 755 /var/lib/tftpboot") + + # Restart the tftpd-hpa service + print("Restarting tftpd-hpa service...") + run_command("sudo systemctl restart tftpd-hpa") + + # Check the status of the tftpd-hpa service + print("Checking tftpd-hpa service status...") + run_command("sudo systemctl status tftpd-hpa") +# if TFTPSERV: +# logger.info(f"TFTPSERV is configured: {TFTPSERV}") +# inetd_cfg_path = f"{INETDCFGDIR}/{TFTPSERV}" +# if os.path.isfile(inetd_cfg_path): +# logger.error(f"The inetd configuration file exists: {inetd_cfg_path}") +# with open(inetd_cfg_path, "r+") as file: +# content = file.read() +# new_content = content.replace("disable.*", "disable = no") +# file.seek(0) +# file.write(new_content) +# file.truncate() +# logger.info(f"Modified inetd configuration file: {inetd_cfg_path}") +# else: +# service = TFTPSERV +# logger.info(f"Enabling and starting the service {service}.service") +# subprocess.run(["systemctl", "enable", f"{service}.service"], check=True) +# subprocess.run(["systemctl", "start", f"{service}.service"], check=True) +# service = INETDSERV +# logger.info(f"Enabling and starting the service {service}.service") + #subprocess.run(["systemctl", "enable", f"{service}.service"], check=True) +# subprocess.run(["systemctl", "start", f"{service}.service"], check=True) #Crear directorio /var/lib/tftpboot if not os.path.exists(TFTPCFGDIR): os.makedirs(TFTPCFGDIR, mode=0o750, exist_ok=True) @@ -494,13 +562,19 @@ def tftpConfigure(): #Crear enlace simbólico de /var/lib/tftpboot/ogLive a /var/lib/tftpboot/ogLive/ogclient subprocess.run(["ln", "-s", "{TFTPCFGDIR}/ogLive", "{TFTPCFGDIR}/ogclient"]) - symlink_target = f"{INSTALL_TARGET}/tftpboot" - logger.info(f"Creating symbolic link from /{TFTPCFGDIR} to {symlink_target}") - if not os.path.exists(symlink_target): - os.symlink("{TFTPCFGDIR}", symlink_target) - os.chown(symlink_target, pwd.getpwnam("ogboot").pw_uid, pwd.getpwnam("ogboot").pw_gid) - else: - logger.warning(f"The symbolic link already exists: {symlink_target}") + symlink_target = f"{INSTALL_OGBOOT_TARGET}/tftpboot" + logger.info(f"Creating symbolic link from {TFTPCFGDIR} to {symlink_target}") + #if not os.path.exists(symlink_target): + # os.symlink("{TFTPCFGDIR}", symlink_target) + # os.chown(symlink_target, pwd.getpwnam("ogboot").pw_uid, pwd.getpwnam("ogboot").pw_gid) + #else: + # logger.warning(f"The symbolic link already exists: {symlink_target}") + # Create the symbolic link + logger.info("Creating symbolic link from /opt/ogboot/tftpboot to /var/lib/tftpboot...") + if os.path.islink(symlink_target): + run_command(f"sudo rm -rf {symlink_target}") + elif os.path.exists(symlink_target): + run_command(f"sudo ln -s {TFTPCFGDIR} {symlink_target}") def servicesCompilation(): hayErrores = 0 @@ -709,7 +783,8 @@ logger.info(f"Starting installation of ogBoot.") if os.geteuid() != 0: logger.error("This program must be run with root privileges..") exit(1) -if os.path.exists(os.path.join(INSTALL_OGBOOT_TARGET + "/doc/")): +print(f':INSTALL_OGBOOT_TARGET:::${INSTALL_OGBOOT_TARGET}') +if os.path.exists(os.path.join(INSTALL_OGBOOT_TARGET, "/doc/")): logger.warning(f"ogBoot is already installed. Run {INSTALL_OGBOOT_TARGET}/lib/ogboot_devel_update.py” with root privileges to update..") exit(2) @@ -799,4 +874,4 @@ smbConfigure() logger.info(f"ogBoot installation finished.") logging.shutdown() -console_handler.close() \ No newline at end of file +console_handler.close()