From 144265b66ddba548fdaead2977fe3a08f118b735 Mon Sep 17 00:00:00 2001 From: qindel Date: Tue, 28 May 2024 15:38:39 +0000 Subject: [PATCH] refs #401 adds news directories before composer install --- installer/ogboot_installer.py | 134 ++++++++++++++++++++++++++-------- 1 file changed, 103 insertions(+), 31 deletions(-) diff --git a/installer/ogboot_installer.py b/installer/ogboot_installer.py index 7be99d6..05aec0e 100755 --- a/installer/ogboot_installer.py +++ b/installer/ogboot_installer.py @@ -5,7 +5,7 @@ ##### Autor: Antonio Emmanuel Guerrero Silva ######## ################################################################################# -import platform, os, sys, subprocess, datetime, shutil, argparse, time +import platform, os, sys, subprocess, datetime, shutil, argparse, time, pwd global UBUNTU_OS_VERSION, OPENGNGYS_VERSION, PYTHON_VERSION_LAST, PYTHON_VERSION, DEPENDENCIES2, INSTALL_OGBOOT_TARGET, WORK_DIR, LOG_FILE, CHECKPKG, INSTALLPKG, PATH, PROGRAM_DIR, OPENGNSYS_SERVER, UPDATEPKGLIST @@ -285,15 +285,66 @@ def generate_config_url(): return f"https://dl.cloudsmith.io/public/isc/kea-2-0/config.deb.txt?distro=ubuntu&codename={codename}&version={version}&arch={arch}" def create_ogboot_project(path_opengnsys_base): - subprocess.run(["sudo", "useradd", "-m", "ogboot"]) - #subprocess.run(["composer", "create-project", "--no-interaction", "symfony/website-skeleton", path_opengnsys_base], user="root") - #parent_dir = os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0]))) - #composer_json_path = os.path.join(parent_dir, 'composer.json') - subprocess.run(["composer", "create-project", "symfony/website-skeleton", path_opengnsys_base]) - os.chdir(path_opengnsys_base) - shutil.copy(f"{WORKDIR}/ogboot/.env", INSTALL_TARGET) - subprocess.run(["rm", f"{path_opengnsys_base}/composer.lock"]) - print("Esqueleto de la aplicación creado y archivo composer.lock eliminado.") + # Verificar si el usuario 'ogboot' ya existe + try: + pwd.getpwnam('ogboot') + print("El usuario 'ogboot' ya existe") + except KeyError: + subprocess.run(["sudo", "useradd", "-m", "ogboot"]) + + # Verificar si el directorio ya existe + if os.path.exists(path_opengnsys_base): + if not os.path.isdir(path_opengnsys_base): + raise NotADirectoryError(f"{path_opengnsys_base} existe y no es un directorio.") + else: + print(f"El directorio {path_opengnsys_base} ya existe.") + else: + os.makedirs(path_opengnsys_base) + # Cambiar el propietario del directorio a 'ogboot' si se creó por primera vez + subprocess.run(["sudo", "chown", "-R", "ogboot:ogboot", path_opengnsys_base]) + + # Copiar los archivos .env y composer.json primero + shutil.copy(f"{WORKDIR}/ogboot/.env", os.path.join(path_opengnsys_base, ".env")) + shutil.copy(f"{WORKDIR}/ogboot/composer.json", os.path.join(path_opengnsys_base, "composer.json")) + print(".env y composer.json copiados a", path_opengnsys_base) + bin_source = os.path.join(WORKDIR, "ogboot/bin") + bin_dest = os.path.join(path_opengnsys_base, "bin") + src_source = os.path.join(WORKDIR, "ogboot/src") + src_dest = os.path.join(path_opengnsys_base, "src") + + config_source = os.path.join(WORKDIR, "ogboot/config") + config_dest = os.path.join(path_opengnsys_base, "config") + if os.path.exists(bin_dest): + shutil.rmtree(bin_dest) + shutil.copytree(bin_source, bin_dest) + + if os.path.exists(src_dest): + shutil.rmtree(src_dest) + shutil.copytree(src_source, src_dest) + + if os.path.exists(config_dest): + shutil.rmtree(config_dest) + shutil.copytree(config_source, config_dest) + + subprocess.run(["sudo", "chmod", "-R", "755", path_opengnsys_base]) + subprocess.run(["sudo", "chown", "-R", "ogboot:ogboot", path_opengnsys_base]) + print("Esperando 20 segundos...") + time.sleep(20) + # Ejecutar Composer como el usuario 'ogboot' para instalar el proyecto Symfony + result = subprocess.run(["sudo", "-u", "ogboot", "composer", "install", "--no-interaction", "--working-dir", path_opengnsys_base]) + if result.returncode != 0: + print("Error al crear el proyecto Symfony usando Composer") + return + + os.chdir(path_opengnsys_base) + + # Eliminar composer.lock si existe + composer_lock_path = os.path.join(path_opengnsys_base, "composer.lock") + if os.path.exists(composer_lock_path): + os.remove(composer_lock_path) + + print("Esqueleto de la aplicación creado y archivo composer.lock eliminado.") + def createDirs(INSTALL_TARGET): if not os.path.exists(INSTALL_TARGET): @@ -438,28 +489,49 @@ def testPxe(): os.remove(f"{TFTPCFGDIR}/testpxe") def tftpConfigure(): - echoAndLog(f"{tftpConfigure.__name__}(): Configuring TFTP service.") - # Habilitar TFTP y reiniciar Inetd. - if TFTPSERV: - if os.path.isfile(f"{INETDCFGDIR}/{TFTPSERV}"): - with open(f"{INETDCFGDIR}/{TFTPSERV}", "r+") as file: - content = file.read() - content = content.replace("disable.*", "disable = no") - file.seek(0) - file.write(content) - file.truncate() - else: - service = TFTPSERV - subprocess.run(["systemctl", "enable", f"{service}.service"]) - subprocess.run(["systemctl", "start", f"{service}.service"]) - service = INETDSERV - subprocess.run(["systemctl", "enable", f"{service}.service"]) - subprocess.run(["systemctl", "start", f"{service}.service"]) + echoAndLog(f"{tftpConfigure.__name__}(): Configurando el servicio TFTP.") + + # Habilitar TFTP y reiniciar Inetd. + if TFTPSERV: + echoAndLog(f"TFTPSERV está configurado: {TFTPSERV}") + + inetd_cfg_path = f"{INETDCFGDIR}/{TFTPSERV}" + if os.path.isfile(inetd_cfg_path): + echoAndLog(f"El archivo de configuración de inetd existe: {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() + + echoAndLog(f"Archivo de configuración de inetd modificado: {inetd_cfg_path}") + else: + service = TFTPSERV + echoAndLog(f"Habilitando y arrancando el servicio {service}.service") + + subprocess.run(["systemctl", "enable", f"{service}.service"], check=True) + subprocess.run(["systemctl", "start", f"{service}.service"], check=True) + + service = INETDSERV + echoAndLog(f"Habilitando y arrancando el servicio {service}.service") + + subprocess.run(["systemctl", "enable", f"{service}.service"], check=True) + subprocess.run(["systemctl", "start", f"{service}.service"], check=True) - os.symlink("/var/lib/tftpboot", f"{INSTALL_TARGET}/tftpboot") - # comprobamos el servicio tftp - #time.sleep(1) - #testPxe() + symlink_target = f"{INSTALL_TARGET}/tftpboot" + echoAndLog(f"Creando enlace simbólico de /var/lib/tftpboot a {symlink_target}") + + if not os.path.exists(symlink_target): + os.symlink("/var/lib/tftpboot", symlink_target) + else: + echoAndLog(f"El enlace simbólico ya existe: {symlink_target}") + + # Comprobamos el servicio tftp + # time.sleep(1) + # testPxe() def mount_NFS(): if subprocess.call(["sudo", "mount", "-t", "nfs", "ognartefactos.evlt.uma.es:/", "/mnt"]) == 0: