diff --git a/installer/ogboot_installer.py b/installer/ogboot_installer.py index bc64553..933c0dc 100755 --- a/installer/ogboot_installer.py +++ b/installer/ogboot_installer.py @@ -7,40 +7,31 @@ import platform, os, sys, subprocess, datetime, shutil, pwd, glob, zipfile, urllib.request, logging, distro, re -global PACKAGES_TO_INSTALL -global UBUNTU_OS_VERSION -global PYTHON_VERSION -global INSTALL_OGBOOT_TARGET -global PROGRAM_DIR -global OPENGNSYS_SERVER - # Reload variables from bashrc -subprocess.run(['.', '~/.bashrc'], shell=True) +# 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" - -# 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" +env_ogCore_ServerIP = os.getenv('OGCORE_SERVER_IP') #"172.17.8.82" +env_ogCore_Server = os.getenv('OGCORE_SERVER') #"opengnsys.es" +env_ogCore_Dir = os.getenv('OGCORE_DIR') #{INSTALL_OPENGNSYS_TARGET} +env_ogBoot_Dir = os.getenv('OGBOOT_DIR') #{INSTALL_OGBOOT_TARGET} +env_ogBoot_GitRepo = os.getenv('OGBOOT_GIT_REPO') #"ssh://git@ognproject.evlt.uma.es:21987/opengnsys/ogboot.git" +#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])) PROGRAM_NAME = os.path.basename(sys.argv[0]) -SERVER_OPENGNSYS = env_ogCore_ServerIP -OPENGNSYS_SERVER = env_ogCore_Server -INSTALL_TARGET = env_ogBoot_Dir -INSTALL_OPENGNSYS_TARGET = env_ogCore_Dir -INSTALL_OGBOOT_TARGET = env_ogBoot_Dir -GIT_REPO = env_ogBoot_GitRepo +SERVER_OPENGNSYS = f"{env_ogCore_ServerIP}" +OPENGNSYS_SERVER = f"{env_ogCore_Server}" +INSTALL_TARGET = f"{env_ogBoot_Dir}" +INSTALL_OPENGNSYS_TARGET = f"{env_ogCore_Dir}" +INSTALL_OGBOOT_TARGET = f"{env_ogBoot_Dir}" +GIT_REPO = f"{env_ogBoot_GitRepo}" IPXE_DIR = "/tmp/ipxe" WORKDIR="/tmp/ogboot_installer" @@ -50,7 +41,7 @@ SAMBACFGDIR = "/etc/samba" TFTPCFGDIR = "/var/lib/tftpboot" INETDCFGDIR = "/etc/xinetd.d/" -DEFAULTDEV = "" +DEFAULTDEV = "enp0s3" PACKAGES_TO_INSTALL = ["htop"] DEVICE = [] SERVERIP = [] @@ -163,34 +154,33 @@ def get_missing_packages(): case "ubuntu": match OSVERSION: case "20.04": - PACKAGES = ["vim", "curl", "htop"] + PACKAGES_TO_INSTALL = ["vim", "curl", "htop"] case "18.04": - PACKAGES = ["nano", "wget", "tree"] + PACKAGES_TO_INSTALL = ["nano", "wget", "tree"] case "22.04": - PACKAGES_TO_INSTALL = ["xorriso", "genisoimage", "syslinux", "liblzma-dev", "nginx", "arp-scan", "automake", "build-essential", "btrfs-progs", "composer", "curl", "ctorrent", "debootstrap", "g++-multilib", "gawk", "gettext", "graphviz", "grub-efi-amd64-signed", "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", "php8.1-common", "php-pear", "php-xml", "php-zip", "procps", "coreutils", "rsync", "samba", "schroot", "shim-signed", "squashfs-tools", "subversion", "tftpd-hpa", "udpcast", "unzip", "wakeonlan", "wget", "xinetd", "jq", "moreutils", "net-tools"] + PACKAGES_TO_INSTALL = ["nfs-common", "xorriso", "genisoimage", "syslinux", "liblzma-dev", "nginx", "arp-scan", "automake", "build-essential", "btrfs-progs", "composer", "curl", "ctorrent", "debootstrap", "g++-multilib", "gawk", "gettext", "graphviz", "grub-efi-amd64-signed", "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", "php8.1-common", "php-pear", "php-xml", "php-zip", "procps", "coreutils", "rsync", "samba", "schroot", "shim-signed", "squashfs-tools", "subversion", "tftpd-hpa", "udpcast", "unzip", "wakeonlan", "wget", "xinetd", "jq", "moreutils", "net-tools"] case _: - PACKAGES = ["bash", "nc", "rsync"] + PACKAGES_TO_INSTALL = ["bash", "nc", "rsync"] case "suse": match OSVERSION: case "15": - PACKAGES = ["zypper", "mc", "gcc"] + PACKAGES_TO_INSTALL = ["zypper", "mc", "gcc"] case "12": - PACKAGES = ["perl", "tar", "man"] + PACKAGES_TO_INSTALL = ["perl", "tar", "man"] case _: - PACKAGES = ["openssl", "ncurses", "zip"] + PACKAGES_TO_INSTALL = ["openssl", "ncurses", "zip"] case "redhat": match OSVERSION: case "8": - PACKAGES = ["yum", "tar", "perl"] + PACKAGES_TO_INSTALL = ["yum", "tar", "perl"] case "7": - PACKAGES = ["bash", "rpm", "tcpdump"] + PACKAGES_TO_INSTALL = ["bash", "rpm", "tcpdump"] case _: - PACKAGES = ["grep", "sed", "awk"] + PACKAGES_TO_INSTALL = ["grep", "sed", "awk"] case _: logger.error("Distribution not supported by ogBoot.") exit(1) - for package in PACKAGES_TO_INSTALL: if not is_installed(package): faltantes.append(package) @@ -218,6 +208,7 @@ def install_packages(missing, log_packages_file=f"/tmp/installed_packages.log"): os.environ['DEBIAN_FRONTEND'] = original_debian_frontend else: del os.environ['DEBIAN_FRONTEND'] + def autoConfigure(): #distribution = platform.linux_distribution() #distribution = distro.linux_distribution() @@ -229,20 +220,6 @@ def autoConfigure(): logger.info(f"OSVERSION: {OSVERSION}") # Configuración según la distribución OSDISTRIB - - - - - - - - - - - - - - def generate_config_url(): with open('/etc/os-release', 'r') as file: lines = file.readlines() @@ -692,6 +669,21 @@ def mask2cidr(mask): cidr += bin(int(i)).count("1") return cidr +def get_first_network_interface_with_traffic(): + with open('/proc/net/dev') as f: + for line in f: + if ':' in line: + parts = line.split(':') + if len(parts) > 1: + interface = parts[0].strip() + if interface != "lo": + traffic_data = parts[1].strip().split() + # Recibidos y enviados + received_bytes = int(traffic_data[0]) + transmitted_bytes = int(traffic_data[8]) + if received_bytes > 0 or transmitted_bytes > 0: + return interface + def getNetworkSettings(): DHCPNET = "127.0.0.1" #print("getNetworkSettings(): Detecting network parameters.") @@ -724,6 +716,7 @@ def getNetworkSettings(): return (0) def openGnsysConfigure(): + global DEFAULTDEV i = 0 dev = "" CONSOLEURL = "" @@ -732,9 +725,9 @@ def openGnsysConfigure(): content = file.read() content = content.replace("SERVERIP", SERVER_OPENGNSYS) content = content.replace("OPENGNSYSURL", CONSOLEURL) - with open(f"{INSTALL_TARGET}/client/etc/ogAdmClient-ens160.cfg", "w") as outfile: + with open(f"{INSTALL_TARGET}/client/etc/ogAdmClient-{DEFAULTDEV}.cfg", "w") as outfile: outfile.write(content) - os.symlink(f"{INSTALL_TARGET}/client/etc/ogAdmClient-ens160.cfg", f"{INSTALL_TARGET}/client/etc/ogAdmClient.cfg") + os.symlink(f"{INSTALL_TARGET}/client/etc/ogAdmClient-{DEFAULTDEV}.cfg", f"{INSTALL_TARGET}/client/etc/ogAdmClient.cfg") TZ = subprocess.check_output(["timedatectl", "status"]).decode().split("\n")[2].split(":")[1].strip() with open(f"{INSTALL_TARGET}/client/etc/engine.cfg", "a") as file: file.write(f"# OpenGnsys Server timezone.\nTZ=\"{TZ.replace(' ', '')}\"\n") @@ -746,7 +739,7 @@ def mount_NFS(): else: logger.error("Could not mount the NFS system.") exit(1) - logger.info("copy ipxe::::::::::::::::::::::::::::") + logger.info("copying tftp and ipxe files") #subprocess.call(["ls", "/mnt/"]) subprocess.call(["sudo", "cp", "-r", "/mnt/srv/artefactos/ogboot/tftpboot/", "/tmp"]) if not os.path.exists(IPXE_DIR + "/tftpboot"): @@ -781,7 +774,8 @@ def mount_NFS(): subprocess.call(["sudo", "cp", "bin-x86_64-efi/ipxe.efi", f"{INSTALL_OPENGNSYS_TARGET}/tftpboot"]) def generate_ipxe_script(): - ip_address_server = subprocess.check_output(["ifconfig", "ens160"]).decode().split("\n")[1].split()[1] + global DEFAULTDEV + ip_address_server = subprocess.check_output(["ifconfig", DEFAULTDEV]).decode().split("\n")[1].split()[1] template = os.path.join(WORKDIR, "ogboot/etc/dhcp_boot.ipxe.tmpl") ipxe_output = f"{INSTALL_OGBOOT_TARGET}/tftpboot/ipxe_scripts/dhcp_boot.ipxe" os.makedirs(os.path.dirname(ipxe_output), mode=0o750, exist_ok=True) @@ -847,25 +841,11 @@ def smbConfigure(): logger.info(f"Added Samba configuration.") return 0 -def get_network_interfaces(): - interfaces = [] - with open('/proc/net/dev') as f: - for line in f: - if ':' in line: - parts = line.split(':') - if len(parts) > 1: - if parts[0].strip() != "lo": - interface = parts[0].strip() # Accessing the first part as the interface name - interfaces.append(interface) - return interfaces - -print(get_network_interfaces()) -#exit (0) - def setup_nginx(): + global DEFAULTDEV try: # Obtener la IP del servidor - ip_address_server = subprocess.check_output(["ifconfig", "ens160"]).decode().split("\n")[1].split()[1] + ip_address_server = subprocess.check_output(["ifconfig", DEFAULTDEV]).decode().split("\n")[1].split()[1] # Leer y modificar la plantilla de configuración de nginx template_path = os.path.join(WORKDIR, "ogboot/etc/nginxServer.conf.tmpl") @@ -1002,7 +982,6 @@ def install_oglive_daemon(): ###:::::::::::::::::::::::::::::::: MAIN :::::::::::::::::::::::::::::::::::### ############################################################################### - logger.info(f"Starting installation of ogBoot.") if os.geteuid() != 0: logger.error("This program must be run with root privileges..") @@ -1023,6 +1002,8 @@ Missing = get_missing_packages () install_packages(Missing) logger.info("Obtaining the default network configuration.") +DEFAULTDEV = get_first_network_interface_with_traffic() +logger.info(f"Network interface default:[{DEFAULTDEV}]") if getNetworkSettings() != 0: logger.error("Error reading default network settings.") exit(1)