refs #477 Adds new changes ogboot installer

ogboot_installer
Luis Gerardo Romero Garcia 2024-07-01 09:20:15 +00:00
parent 6905d2858b
commit eb5080da08
1 changed files with 131 additions and 71 deletions

View File

@ -7,10 +7,15 @@
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
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(['source', '~/.bashrc'], shell=True)
subprocess.run(['.', '~/.bashrc'], shell=True)
# Leer variables de entorno
#env_ogCore_ServerIP = os.environ.get('OGCORE_SERVER_IP') #"172.17.8.82"
@ -46,7 +51,7 @@ TFTPCFGDIR = "/var/lib/tftpboot"
INETDCFGDIR = "/etc/xinetd.d/"
DEFAULTDEV = ""
PACKAGES = []
PACKAGES_TO_INSTALL = ["htop"]
DEVICE = []
SERVERIP = []
NETIP = []
@ -147,11 +152,49 @@ def is_installed(package):
except subprocess.CalledProcessError:
return False
def get_missing_packages(packages):
def get_missing_packages():
global PACKAGES_TO_INSTALL
faltantes = []
for package in packages:
OSDISTRIB = distro.name()
OSVERSION = distro.version()
logger.info(f"OSDISTRIB: {OSDISTRIB}")
logger.info(f"OSVERSION: {OSVERSION}")
match OSDISTRIB.lower():
case "ubuntu":
match OSVERSION:
case "20.04":
PACKAGES = ["vim", "curl", "htop"]
case "18.04":
PACKAGES = ["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"]
case _:
PACKAGES = ["bash", "nc", "rsync"]
case "suse":
match OSVERSION:
case "15":
PACKAGES = ["zypper", "mc", "gcc"]
case "12":
PACKAGES = ["perl", "tar", "man"]
case _:
PACKAGES = ["openssl", "ncurses", "zip"]
case "redhat":
match OSVERSION:
case "8":
PACKAGES = ["yum", "tar", "perl"]
case "7":
PACKAGES = ["bash", "rpm", "tcpdump"]
case _:
PACKAGES = ["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)
logger.info(f"::::::Package to install: {package}...")
return faltantes
def install_packages(missing, log_packages_file=f"/tmp/installed_packages.log"):
@ -159,14 +202,22 @@ def install_packages(missing, log_packages_file=f"/tmp/installed_packages.log"):
logger.warning("All packages are already installed.")
return
logger.info("Upgrading the system...")
subprocess.run(["sudo", "apt-get", "update"], check=True)
with open(log_packages_file, "a") as log:
for package in missing:
subprocess.run(["sudo", "apt-get", "install", "--force-yes", "-y", package], check=True)
logger.info(f"{package} installed correctly.")
log.write(package + "\n")
logger.info("All missing packages have been installed.")
original_debian_frontend = os.environ.get('DEBIAN_FRONTEND')
try:
os.environ['DEBIAN_FRONTEND'] = 'noninteractive'
subprocess.run(["sudo", "apt-get", "update"], check=True)
with open(log_packages_file, "a") as log:
for package in missing:
subprocess.run(["sudo", "apt-get", "install", "--allow-change-held-packages", "-y", package], check=True)
logger.info(f"{package} installed correctly.")
log.write(package + "\n")
logger.info("All missing packages have been installed.")
finally:
# Restaurar el valor original de DEBIAN_FRONTEND
if original_debian_frontend is not None:
os.environ['DEBIAN_FRONTEND'] = original_debian_frontend
else:
del os.environ['DEBIAN_FRONTEND']
def autoConfigure():
#distribution = platform.linux_distribution()
#distribution = distro.linux_distribution()
@ -176,42 +227,21 @@ def autoConfigure():
#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
if OSVERSION == '18.04':
# Código para Ubuntu 18.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", "jq", "moreutils", "net-tools"]
pass
elif OSVERSION == '20.04':
# Código para Ubuntu 20.04
dfdf = ["htop"]
pass
elif OSVERSION == '22.04':
# Código para Ubuntu 22.04
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", "jq", "moreutils", "net-tools"]
pass
else:
logger.error("Ubuntu version not supported by ogBoot.")
exit(1)
elif OSDISTRIB == 'debian':
# Código para Debian
pass
elif OSDISTRIB == 'linuxmint':
# Código para Linux Mint
pass
elif OSDISTRIB == 'fedora':
# Código para Fedora
pass
elif OSDISTRIB == 'centos':
# Código para CentOS
pass
else:
logger.error("Distribution not supported by ogBoot.")
exit(1)
def generate_config_url():
with open('/etc/os-release', 'r') as file:
@ -490,7 +520,7 @@ def run_command(command):
def tftpConfigure():
# Update the package list
print("Updating package list...")
#print("Updating package list...")
run_command("sudo apt update")
run_command("sudo DEBIAN_FRONTEND=noninteractive apt install -y build-essential")
@ -555,8 +585,10 @@ TFTP_OPTIONS="--secure"
#Descargar oglive
logger.info("Downloading oglive...")
#Temporalmente se copia desde ~/oglive
subprocess.run(["cp", "-r", f"{PROGRAM_DIR}/../../oglive/ogLive-5.11.0-r20210413", f"{TFTPCFGDIR}/"])
subprocess.run(["cp", "-r", f"{PROGRAM_DIR}/../../tftpboot/ipxe_scripts", f"{TFTPCFGDIR}/"])
# copiar desde el montaje /mnt/srv/artefactos/ogboot/tftpboot/
logger.error(f"___________/home/qindel/ogboot/installer___________________Intentando copiar desde ...{PROGRAM_DIR} a {TFTPCFGDIR}")
subprocess.run(["cp", "-r", f"/tmp/tftpboot/tftpboot/ogLive-5.11.0-r20210413", f"{TFTPCFGDIR}/"])
subprocess.run(["cp", "-r", f"/tmp/tftpboot/ipxe", f"{TFTPCFGDIR}/"])
#Crear enlace simbólico de oglive-5.11.0-r20210413 a /var/lib/tftpboot/ogLive
subprocess.run(["ln", "-s", f"{TFTPCFGDIR}/ogLive-5.11.0-r20210413", "{TFTPCFGDIR}/ogLive"])
#Crear enlace simbólico de /var/lib/tftpboot/ogLive a /var/lib/tftpboot/ogLive/ogclient
@ -675,7 +707,7 @@ def openGnsysConfigure():
content = content.replace("OPENGNSYSURL", CONSOLEURL)
with open(f"{INSTALL_TARGET}/client/etc/ogAdmClient-ens160.cfg", "w") as outfile:
outfile.write(content)
os.link(f"{INSTALL_TARGET}/client/etc/ogAdmClient-ens160.cfg", f"{INSTALL_TARGET}/client/etc/ogAdmClient.cfg")
os.symlink(f"{INSTALL_TARGET}/client/etc/ogAdmClient-ens160.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")
@ -688,21 +720,33 @@ def mount_NFS():
logger.error("Could not mount the NFS system.")
exit(1)
logger.info("copy ipxe::::::::::::::::::::::::::::")
subprocess.call(["ls", "/mnt/"])
subprocess.call(["sudo", "cp", "-r", "/mnt/srv/artefactos/ipxe/", "/tmp"])
#subprocess.call(["ls", "/mnt/"])
subprocess.call(["sudo", "cp", "-r", "/mnt/srv/artefactos/ogboot/tftpboot/", "/tmp"])
if not os.path.exists(IPXE_DIR + "/tftpboot"):
os.makedirs(IPXE_DIR + "/tftpboot")
#os.chdir(f"{IPXE_DIR}/tftpboot")
subprocess.call(["sudo", "cp", "-r", "/mnt/srv/artefactos/ogboot/ipxe/", "/tmp"])
if not os.path.exists(IPXE_DIR + "/src"):
os.makedirs(IPXE_DIR + "/src")
os.chdir(f"{IPXE_DIR}/src")
if subprocess.call(["sudo", "make", "-j", "4"]) == 0:
if subprocess.call(["sudo", "make", "-s", "-j", "4"]) == 0:
logger.info(f"Directory {IPXE_DIR}/src correctly mounted.")
else:
logger.error(f"ERROR\tCould not mount the directory {IPXE_DIR}/src.")
exit(1)
if subprocess.call(["sudo", "make", "bin/undionly.kpxe", f"EMBED={INSTALL_OPENGNSYS_TARGET}/tftpboot/ipxe_scripts/dhcp_boot.ipxe"]) == 0:
logger.info("Boot file mounted correctly.")
else:
logger.error("Failed to mount boot file.")
exit(1)
if not os.path.exists("/opt/opengnsys"):
os.symlink("/opt/ogboot/", "/opt/opengnsys")
logger.info("Symbolic link created successfully.")
#logger.info(f"copy ipxe(2):::::::::::::::::::::::::::: EMBED={INSTALL_OPENGNSYS_TARGET}/tftpboot/ipxe_scripts/dhcp_boot.ipxe")
#if subprocess.call(["sudo", "make", "-s", "bin/undionly.kpxe", f"EMBED={INSTALL_OPENGNSYS_TARGET}/tftpboot/ipxe_scripts/dhcp_boot.ipxe"]) == 0:
# logger.info("Boot file mounted correctly.")
#else:
# logger.error("Failed to mount boot file.")
# exit(1)
logger.info("copy ipxe(3)::::::::::::::::::::::::::::")
subprocess.call(["sudo", "cp", "bin/undionly.kpxe", f"{INSTALL_OPENGNSYS_TARGET}/tftpboot"])
if subprocess.call(["sudo", "make", "bin-x86_64-efi/ipxe.efi", f"EMBED={INSTALL_OPENGNSYS_TARGET}/tftpboot/ipxe_scripts/dhcp_boot.ipxe"]) == 0:
if subprocess.call(["sudo", "make", "-s", "bin-x86_64-efi/ipxe.efi", f"EMBED={INSTALL_OPENGNSYS_TARGET}/tftpboot/ipxe_scripts/dhcp_boot.ipxe"]) == 0:
logger.info("Properly constructed EFI file.")
else:
logger.error("Could not build EFI file.")
@ -776,15 +820,30 @@ 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)
###############################################################################
###:::::::::::::::::::::::::::::::: MAIN :::::::::::::::::::::::::::::::::::###
###############################################################################
logger.info(f"Starting installation of ogBoot.")
if os.geteuid() != 0:
logger.error("This program must be run with root privileges..")
exit(1)
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)
@ -797,7 +856,7 @@ logger.info("Checking the operating system.")
autoConfigure()
logger.info("Installing necessary packages.")
Missing = get_missing_packages (PACKAGES)
Missing = get_missing_packages ()
install_packages(Missing)
logger.info("Obtaining the default network configuration.")
@ -813,10 +872,10 @@ if REMOTE == 1:
exit(1)
else:
if not os.path.exists(f"{WORKDIR}/ogboot"):
logger.info(f"Does not exist {WORKDIR}/ogboot")
logger.warning(f"Does not exist {WORKDIR}/ogboot")
if not os.path.exists(WORKDIR):
os.makedirs(WORKDIR, mode=0o750, exist_ok=True)
logger.error(f"ogBoot directory not found, create a symbolic link to the directory where the code is located {WORKDIR} To {os.path.dirname(PROGRAM_DIR)}")
logger.info(f"ogBoot directory not found, create a symbolic link to the directory where the code is located {WORKDIR} To {os.path.dirname(PROGRAM_DIR)}")
os.symlink(os.path.dirname(PROGRAM_DIR), f"{WORKDIR}/ogboot")
#logger.info("Creating directories.")
@ -836,10 +895,13 @@ og_boot_symfony_install(INSTALL_OGBOOT_TARGET)
logger.info("Copying installation files.")
og_boot_copy_files(INSTALL_OGBOOT_TARGET)
if os.system("echo $?") != 0:
logger.error("Error while creating skeleton directory!")
exit(1)
#if os.system("echo $?") != 0:
# logger.error("Error while creating skeleton directory!")
# exit(1)
logger.info("Setting up NFS system")
mount_NFS()
logger.info("Configuring TFTP service.")
tftpConfigure()
@ -866,8 +928,6 @@ generate_ipxe_script()
logger.info("Configuring ogCore")
openGnsysConfigure()
logger.info("Setting up NFS system")
mount_NFS()
logger.info("Configuring Samba")
smbConfigure()