refs #441 Function enhancement, removal of absolute paths and code cleanup
parent
9d7aa23312
commit
1590b20e1a
|
@ -5,9 +5,43 @@
|
|||
##### Autor: Antonio Emmanuel Guerrero Silva <aguerrero@qindel.com> ########
|
||||
#################################################################################
|
||||
|
||||
import platform, os, sys, subprocess, datetime, shutil, argparse, time, pwd, glob, zipfile, urllib.request, logging
|
||||
import platform, os, sys, subprocess, datetime, shutil, pwd, glob, zipfile, urllib.request, logging
|
||||
|
||||
global PACKAGES, UBUNTU_OS_VERSION, PYTHON_VERSION, INSTALL_OGBOOT_TARGET, PROGRAM_DIR, OPENGNSYS_SERVER
|
||||
|
||||
PROGRAM = os.path.splitext(os.path.basename(sys.argv[0]))[0]
|
||||
SERVER_OPENGNSYS = "172.17.8.82"
|
||||
UBUNTU_OS_VERSION = "18"
|
||||
PYTHON_VERSION = 3
|
||||
INSTALL_TARGET="/opt/ogboot"
|
||||
INSTALL_OPENGNSYS_TARGET = "/opt/opengnsys"
|
||||
INSTALL_OGBOOT_TARGET = "/opt/ogboot"
|
||||
WORKDIR="/tmp/ogboot_installer"
|
||||
PACKAGES = []
|
||||
PROGRAM_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
|
||||
PROGRAM_NAME = os.path.basename(sys.argv[0])
|
||||
OPENGNSYS_SERVER = "opengnsys.es"
|
||||
DEFAULTDEV = ""
|
||||
DEVICE = []
|
||||
SERVERIP = []
|
||||
NETIP = []
|
||||
NETMASK = []
|
||||
NETBROAD = []
|
||||
ROUTERIP = []
|
||||
BRANCH = sys.argv[1] if len(sys.argv) > 1 else "main"
|
||||
TFTPSERV = "tftpd-hpa"
|
||||
SAMBASERV = "smbd"
|
||||
SAMBACFGDIR = "/etc/samba"
|
||||
TFTPCFGDIR = "/var/lib/tftpboot"
|
||||
INETDCFGDIR = "/etc/xinetd.d/"
|
||||
INETDSERV = "xinetd"
|
||||
OPENGNSYS_CLIENT_PASSWD="og"
|
||||
OPENGNSYS_CLIENT_USER="opengnsys"
|
||||
GIT_REPO="ssh://git@ognproject.evlt.uma.es:21987/opengnsys/ogboot.git"
|
||||
if os.path.isdir(f"{PROGRAM_DIR}/../installer"):
|
||||
REMOTE = 0
|
||||
else:
|
||||
REMOTE = 1
|
||||
|
||||
#Configurar el registro
|
||||
logging.basicConfig(level=logging.DEBUG,
|
||||
|
@ -21,87 +55,14 @@ formatter = logging.Formatter('%(levelname)s\t%(message)s')
|
|||
console_handler.setFormatter(formatter)
|
||||
logger.addHandler(console_handler)
|
||||
|
||||
global UBUNTU_OS_VERSION, OPENGNGYS_VERSION, PYTHON_VERSION, DEPENDENCIES2, INSTALL_OGBOOT_TARGET, LOG_FILE, CHECKPKG, INSTALLPKG, PATH, PROGRAM_DIR, OPENGNSYS_SERVER, UPDATEPKGLIST
|
||||
# Leer variables de entorno
|
||||
ogCore_ServerIP = os.environ.get('OGCORE_SERVER_IP') #"172.17.8.82"
|
||||
ogCore_Dir = os.environ.get('OGCORE_DIR') #{INSTALL_OPENGNSYS_TARGET}
|
||||
ogBoot_Dir = os.environ.get('OGBOOT_DIR') #{INSTALL_OGBOOT_TARGET}
|
||||
|
||||
SERVER_OPENGNSYS = "172.17.8.82"
|
||||
UPDATEPKGLIST = ""
|
||||
UBUNTU_OS_VERSION = "18"
|
||||
OPENGNGYS_VERSION = "1.2.0"
|
||||
PYTHON_VERSION = 3
|
||||
INSTALL_TARGET="/opt/ogboot"
|
||||
INSTALL_OPENGNSYS_TARGET = "/opt/opengnsys"
|
||||
INSTALL_OGBOOT_TARGET = "/opt/ogboot"
|
||||
WORKDIR="/tmp/ogboot_installer"
|
||||
DEPENDENCIES2 = []
|
||||
INSTALLPKG=""
|
||||
PROGRAM_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
|
||||
PROGRAM_NAME = os.path.basename(sys.argv[0])
|
||||
OPENGNSYS_SERVER = "opengnsys.es"
|
||||
CHECKPKG=""
|
||||
DEFAULTDEV = ""
|
||||
DEVICE = []
|
||||
SERVERIP = []
|
||||
NETIP = []
|
||||
NETMASK = []
|
||||
NETBROAD = []
|
||||
ROUTERIP = []
|
||||
BRANCH = sys.argv[1] if len(sys.argv) > 1 else "main"
|
||||
TFTPSERV = "tftpd-hpa"
|
||||
ENABLESERVICE = "systemctl enable $service.service"
|
||||
DISABLESERVICE = "systemctl disable $service.service"
|
||||
APACHESERV = "apache2"
|
||||
APACHECFGDIR = "/etc/apache2"
|
||||
APACHESITESDIR = "sites-available"
|
||||
APACHEOGSITE = "ogboot"
|
||||
APACHEUSER = "www-data"
|
||||
APACHEGROUP = "www-data"
|
||||
APACHEENABLEMODS = "a2enmod headers ssl rewrite proxy_fcgi fastcgi actions alias"
|
||||
APACHEENABLESSL = "a2ensite default-ssl"
|
||||
APACHEENABLEOG = f"a2ensite {APACHEOGSITE}"
|
||||
APACHEMAKECERT = "make-ssl-cert generate-default-snakeoil --force-overwrite"
|
||||
SAMBASERV = "smbd"
|
||||
SAMBACFGDIR = "/etc/samba"
|
||||
TFTPCFGDIR = "/var/lib/tftpboot"
|
||||
PHPFPMSERV = "php7.2-fpm"
|
||||
INETDCFGDIR = "/etc/xinetd.d/"
|
||||
INETDSERV = "xinetd"
|
||||
OPENGNSYS_CLIENT_PASSWD="og"
|
||||
OPENGNSYS_CLIENT_USER="opengnsys"
|
||||
GIT_REPO="ssh://git@ognproject.evlt.uma.es:21987/opengnsys/ogboot.git"
|
||||
if os.path.isdir(f"{PROGRAM_DIR}/../installer"):
|
||||
REMOTE = 0
|
||||
else:
|
||||
REMOTE = 1
|
||||
#DOWNLOAD_URL = f"https://{OPENGNSYS_SERVER}/trac/downloads"
|
||||
#CODE_URL = f"https://codeload.github.com/opengnsys/OpenGnsys/zip/{BRANCH}"
|
||||
#API_URL = "https://api.github.com/repos/opengnsys/OpenGnsys"
|
||||
PATH = os.environ.get("PATH", "") + f":{INSTALL_OGBOOT_TARGET}/bin"
|
||||
OG_LOG_FILE = f"{INSTALL_OGBOOT_TARGET}/log/{PROGRAM_NAME[:-3]}.log"
|
||||
LOG_FILE = f"/tmp/{os.path.basename(OG_LOG_FILE)}"
|
||||
if shutil.which("service"):
|
||||
STARTSERVICE = "service $service restart"
|
||||
STOPSERVICE = "service $service stop"
|
||||
else:
|
||||
STARTSERVICE = "/etc/init.d/$service restart"
|
||||
STOPSERVICE = "/etc/init.d/$service stop"
|
||||
required_packages_18 = [
|
||||
"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"
|
||||
]
|
||||
required_packages_22 = [
|
||||
"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"
|
||||
]
|
||||
###############################################################################
|
||||
###::::::::::::::::::::::::::::::: UTILS :::::::::::::::::::::::::::::::::::###
|
||||
###############################################################################
|
||||
|
@ -185,36 +146,44 @@ def install_packages(missing, log_packages_file=f"/tmp/installed_packages.log"):
|
|||
logger.info("All missing packages have been installed.")
|
||||
|
||||
def autoConfigure():
|
||||
os_info = platform.uname()
|
||||
OSDISTRIB = os_info.system.lower()
|
||||
OSVERSION = os_info.release.split('.')[0]
|
||||
if OSDISTRIB in ['linux', 'linux2']:
|
||||
if OSDISTRIB == 'linux':
|
||||
with open('/etc/os-release', 'r') as f:
|
||||
os_release = dict(line.strip().split('=') for line in f)
|
||||
OSDISTRIB = os_release.get('ID', '').lower()
|
||||
OSVERSION = os_release.get('VERSION_ID', '').split('.')[0]
|
||||
distribution = platform.linux_distribution()
|
||||
OSDISTRIB = distribution[0]
|
||||
OSVERSION = distribution[1]
|
||||
logger.info(f"OSDISTRIB: {OSDISTRIB}")
|
||||
logger.info(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"]
|
||||
pass
|
||||
elif OSVERSION == '20.04':
|
||||
# Código para Ubuntu 20.04
|
||||
dfdf = ["htop"]
|
||||
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"]
|
||||
pass
|
||||
else:
|
||||
lsb_info = subprocess.check_output(['lsb_release', '-is']).decode().strip()
|
||||
OSDISTRIB = lsb_info.lower()
|
||||
lsb_version = subprocess.check_output(['lsb_release', '-rs']).decode().strip()
|
||||
OSVERSION = lsb_version.split('.')[0]
|
||||
# Convertir distribución a minúsculas y obtener solo el 1er número de versión
|
||||
OSDISTRIB = OSDISTRIB.lower()
|
||||
OSVERSION = OSVERSION.split('.')[0]
|
||||
# Configuración según la distribución GNU/Linux (usar minúsculas)
|
||||
logger.info(f"Linux OS distribution: {OSDISTRIB} {OSVERSION}")
|
||||
if OSDISTRIB in ['ubuntu', 'debian', 'linuxmint']:
|
||||
DEPENDENCIES2= []
|
||||
elif OSDISTRIB in ['fedora', 'centos']:
|
||||
DEPENDENCIES2 = ['htop']
|
||||
if OSDISTRIB == 'centos':
|
||||
UPDATEPKGLIST = f"yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-{OSVERSION}.noarch.rpm http://rpms.remirepo.net/enterprise/remi-release-{OSVERSION}.rpm"
|
||||
else:
|
||||
logger.error("Distribution not supported by OpenGnsys.")
|
||||
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("Unknown Linux distribution.")
|
||||
logger.error("Distribution not supported by ogBoot.")
|
||||
exit(1)
|
||||
|
||||
def generate_config_url():
|
||||
|
@ -237,7 +206,7 @@ def downloadComposer():
|
|||
if not os.path.isfile(os.path.join(WORKDIR, "ogboot", "bin", "composer.phar")):
|
||||
logger.error("Failed to download composer.phar")
|
||||
return 1
|
||||
logger.info("composer.phar downloaded to /opt/ogboot/bin")
|
||||
logger.info(f"composer.phar downloaded to {INSTALL_OGBOOT_TARGET}/bin")
|
||||
return 0
|
||||
|
||||
def install_swagger_ui():
|
||||
|
@ -271,11 +240,7 @@ def create_ogboot_project(path_opengnsys_base):
|
|||
logger.warning("User 'ogboot' already exists")
|
||||
except KeyError:
|
||||
subprocess.run(["sudo", "useradd", "-m", "ogboot"])
|
||||
#PROGRAM_DIR
|
||||
#logger.info(f"creando enlace simbólico de {PROGRAM_DIR} a {WORKDIR}/ogboot")
|
||||
#os.symlink(PROGRAM_DIR, f"{WORKDIR}/ogboot")
|
||||
#os.symlink(WORKDIR, path_opengnsys_base)
|
||||
|
||||
#########################
|
||||
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.")
|
||||
|
@ -284,23 +249,7 @@ def create_ogboot_project(path_opengnsys_base):
|
|||
else:
|
||||
os.makedirs(path_opengnsys_base)
|
||||
subprocess.run(["sudo", "chown", "-R", "ogboot:ogboot", path_opengnsys_base])
|
||||
|
||||
#with open('/etc/hosts', 'r') as hosts_file:
|
||||
# hosts_content = hosts_file.read()
|
||||
#if 'opengnsys' not in hosts_content:
|
||||
# with open('/etc/hosts', 'a') as hosts_file:
|
||||
# hosts_file.write(f'{SERVER_OPENGNSYS} opengnsys\n')
|
||||
# logger.info(f"Entrada {SERVER_OPENGNSYS} opengnsys' agregada a /etc/hosts")
|
||||
|
||||
with open('/etc/hosts', 'r') as hosts_file:
|
||||
hosts_content = hosts_file.read()
|
||||
if f'{SERVER_OPENGNSYS} opengnsys' not in hosts_content:
|
||||
with open('/etc/hosts', 'a') as hosts_file:
|
||||
hosts_file.write(f'{SERVER_OPENGNSYS} opengnsys\n')
|
||||
logger.info(f"Entry '{SERVER_OPENGNSYS} opengnsys' has been added to /etc/hosts")
|
||||
else:
|
||||
logger.warning(f"The entry '{SERVER_OPENGNSYS} opengnsys' already exists in /etc/hosts" )
|
||||
|
||||
########################
|
||||
logger.info("Creating Symfony application skeleton...")
|
||||
downloadComposer()
|
||||
# Copiar los archivos .env y composer.json primero
|
||||
|
@ -308,6 +257,7 @@ def create_ogboot_project(path_opengnsys_base):
|
|||
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"))
|
||||
logger.info(f".env and composer.json files copied to {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")
|
||||
|
@ -340,6 +290,7 @@ def create_ogboot_project(path_opengnsys_base):
|
|||
|
||||
subprocess.run(["sudo", "chmod", "-R", "755", path_opengnsys_base])
|
||||
subprocess.run(["sudo", "chown", "-R", "ogboot:ogboot", path_opengnsys_base])
|
||||
########################
|
||||
|
||||
# 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])
|
||||
|
@ -522,17 +473,17 @@ def tftpConfigure():
|
|||
#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"/var/lib/tftpboot/"])
|
||||
subprocess.run(["cp", "-r", f"{PROGRAM_DIR}/../../tftpboot/ipxe_scripts", f"/var/lib/tftpboot/"])
|
||||
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}/"])
|
||||
#Crear enlace simbólico de oglive-5.11.0-r20210413 a /var/lib/tftpboot/ogLive
|
||||
subprocess.run(["ln", "-s", f"/var/lib/tftpboot/ogLive-5.11.0-r20210413", "/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
|
||||
subprocess.run(["ln", "-s", "/var/lib/tftpboot/ogLive", "/var/lib/tftpboot/ogclient"])
|
||||
subprocess.run(["ln", "-s", "{TFTPCFGDIR}/ogLive", "{TFTPCFGDIR}/ogclient"])
|
||||
|
||||
symlink_target = f"{INSTALL_TARGET}/tftpboot"
|
||||
logger.info(f"Creating symbolic link from /var/lib/tftpboot to {symlink_target}")
|
||||
logger.info(f"Creating symbolic link from /{TFTPCFGDIR} to {symlink_target}")
|
||||
if not os.path.exists(symlink_target):
|
||||
os.symlink("/var/lib/tftpboot", 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}")
|
||||
|
@ -672,7 +623,7 @@ def mount_NFS():
|
|||
def generate_ipxe_script():
|
||||
ip_address_server = subprocess.check_output(["ifconfig", "eth0"]).decode().split("\n")[1].split()[1]
|
||||
template = os.path.join(WORKDIR, "ogboot/etc/dhcp_boot.ipxe.tmpl")
|
||||
ipxe_output = "/opt/ogboot/tftpboot/ipxe_scripts/dhcp_boot.ipxe"
|
||||
ipxe_output = "{INSTALL_OGBOOT_TARGET}/tftpboot/ipxe_scripts/dhcp_boot.ipxe"
|
||||
os.makedirs(os.path.dirname(ipxe_output), exist_ok=True)
|
||||
shutil.copy(template, ipxe_output)
|
||||
with open(ipxe_output, "r") as ipxe_file:
|
||||
|
@ -756,7 +707,7 @@ logger.info("Checking the operating system.")
|
|||
autoConfigure()
|
||||
|
||||
logger.info("Installing necessary packages.")
|
||||
Missing = get_missing_packages (required_packages_18)
|
||||
Missing = get_missing_packages (PACKAGES)
|
||||
install_packages(Missing)
|
||||
|
||||
logger.info("Obtaining the default network configuration.")
|
||||
|
|
Loading…
Reference in New Issue