Merge branch 'ogboot_installer' of https://ognproject.evlt.uma.es/gitea/opengnsys/ogboot into ogboot_installer
commit
2e775b6dc0
|
@ -0,0 +1,575 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
#################################################################################
|
||||
##### ogBoot installer script ########
|
||||
##### Autor: Antonio Emmanuel Guerrero Silva <aguerrero@qindel.com> ########
|
||||
#################################################################################
|
||||
|
||||
import platform, os, sys, subprocess, datetime, shutil, argparse, time
|
||||
|
||||
global UBUNTU_OS_VERSION, OPENGNGYS_VERSION, PYTHON_VERSION, DEPENDENCIES2, INSTALL_OGBOOT_TARGET, WORK_DIR, LOG_FILE, CHECKPKG, INSTALLPKG, PATH, PROGRAM_DIR, OPENGNSYS_SERVER, UPDATEPKGLIST
|
||||
|
||||
UPDATEPKGLIST = ""
|
||||
UBUNTU_OS_VERSION = "22"
|
||||
OPENGNGYS_VERSION = "1.2.0"
|
||||
PYTHON_VERSION = 3
|
||||
INSTALL_TARGET="/opt/ogboot"
|
||||
INSTALL_OGBOOT_TARGET = "/opt/ogboot"
|
||||
WORK_DIR = "/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=""
|
||||
WORKDIR="/tmp/ogboot_installer"
|
||||
BRANCH = sys.argv[1] if len(sys.argv) > 1 else "main"
|
||||
TFTPSERV = "tftp" # Define the TFTPSERV variable with the appropriate value
|
||||
GIT_REPO="ssh://git@ognproject.evlt.uma.es:21987/opengnsys/ogboot.git"
|
||||
#DOWNLOAD_URL = f"https://{OPENGNSYS_SERVER}/trac/downloads"
|
||||
if os.path.isdir(f"{PROGRAM_DIR}/../installer"):
|
||||
REMOTE = 0
|
||||
else:
|
||||
REMOTE = 1
|
||||
#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"
|
||||
|
||||
ENABLESERVICE = "update-rc.d $service defaults"
|
||||
DISABLESERVICE = "update-rc.d $service disable"
|
||||
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"
|
||||
|
||||
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 :::::::::::::::::::::::::::::::::::###
|
||||
###############################################################################
|
||||
def echoAndLog(message):
|
||||
datetime_now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
#ssh_client = os.environ.get("SSH_CLIENT")
|
||||
log_file = LOG_FILE
|
||||
|
||||
print(message)
|
||||
with open(log_file, "a") as file:
|
||||
file.write(f"{datetime_now}\t{message}\n")
|
||||
#file.write(f"{datetime_now}\t{ssh_client}\t{message}\n")
|
||||
|
||||
def errorAndLog(message):
|
||||
datetime_now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
#client_address = socket.gethostbyname(socket.gethostname())
|
||||
log_message = f"ERROR: {message}"
|
||||
#log_entry = f"{datetime_now};{client_address};{log_message}"
|
||||
log_entry = f"{datetime_now}\t{log_message}"
|
||||
with open(LOG_FILE, "a") as log_file:
|
||||
log_file.write(log_entry + "\n")
|
||||
|
||||
print(log_message)
|
||||
|
||||
def check_python_version():
|
||||
try:
|
||||
# Obtener la versión de Python instalada
|
||||
python_version = platform.python_version()
|
||||
|
||||
# Convertir la versión a una tupla para comparar fácilmente
|
||||
python_version_tuple = tuple(map(int, python_version.split('.')))
|
||||
|
||||
# Verificar si la versión es al menos PYTHON_VERSION.x
|
||||
if python_version_tuple >= (PYTHON_VERSION, 0):
|
||||
echoAndLog(f"LOG\tVersión de python instalada: {python_version}")
|
||||
else:
|
||||
echoAndLog(f"ERROR\tVersión de python es inferior a la requerida. Version instalada: {python_version}" + ".")
|
||||
exit()
|
||||
except Exception as e:
|
||||
echoAndLog(f"ERROR\tProblema al verificar la versión de Python: {e}")
|
||||
exit()
|
||||
|
||||
def check_distribution():
|
||||
if os.path.exists("/etc/os-release"):
|
||||
with open("/etc/os-release", "r") as file:
|
||||
for line in file:
|
||||
if line.startswith("VERSION"):
|
||||
VERSION = line.split("=")[1].strip().strip('"')
|
||||
break
|
||||
if VERSION.startswith(UBUNTU_OS_VERSION + "."):
|
||||
return
|
||||
print(f"La instalación de OpenGnsys {OPENGNGYS_VERSION} se ha probado con funcionalidad completa sobre Ubuntu {UBUNTU_OS_VERSION}")
|
||||
go_on = input("Desea continuar? [s/N]: ")
|
||||
if go_on.upper() != "S":
|
||||
print("Abandonando la instalación.")
|
||||
exit()
|
||||
|
||||
def cidr2mask(cidr):
|
||||
# Number of args to shift, 255..255, first non-255 byte, zeroes
|
||||
args = [5 - (cidr // 8), 255, 255, 255, 255, (255 << (8 - (cidr % 8))) & 255, 0, 0, 0]
|
||||
if args[0] > 1:
|
||||
args = args[args[0]:]
|
||||
else:
|
||||
args = args[1:]
|
||||
return f"{args[0]}.{args[1]}.{args[2]}.{args[3]}"
|
||||
|
||||
def downloadCode(url):
|
||||
if len(url) != 1:
|
||||
errorAndLog("downloadCode(): invalid number of parameters")
|
||||
exit(1)
|
||||
|
||||
print(f"downloadCode(): downloading code from '{url}'...")
|
||||
|
||||
subprocess.run(["GIT_SSH_COMMAND=ssh -o StrictHostKeyChecking=accept-new git archive --remote=" + url + " --format zip --output opengnsys.zip --prefix=opengnsys/ " + BRANCH + " && unzip opengnsys.zip"], shell=True)
|
||||
if subprocess.returncode != 0:
|
||||
errorAndLog("downloadCode(): error getting OpenGnsys code from " + url)
|
||||
return 1
|
||||
subprocess.run(["rm -f opengnsys.zip"], shell=True)
|
||||
print("downloadCode(): code was downloaded")
|
||||
return 0
|
||||
|
||||
###############################################################################
|
||||
###:::::::::::::::::::::::::::::: INSTALL ::::::::::::::::::::::::::::::::::###
|
||||
###############################################################################
|
||||
def is_installed(package):
|
||||
try:
|
||||
# Verifica si el paquete está instalado
|
||||
subprocess.run(["dpkg", "-s", package], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
return True
|
||||
except subprocess.CalledProcessError:
|
||||
return False
|
||||
|
||||
def get_missing_packages(packages):
|
||||
faltantes = []
|
||||
for package in packages:
|
||||
if not is_installed(package):
|
||||
faltantes.append(package)
|
||||
return faltantes
|
||||
|
||||
def install_packages(missing, log_file="installed_packages.log"):
|
||||
if not missing:
|
||||
print("Todos los paquetes ya están instalados.")
|
||||
return
|
||||
|
||||
print("Paquetes a instalar:", ", ".join(missing))
|
||||
print("Actualizando el sistema...")
|
||||
subprocess.run(["sudo", "apt-get", "update"], check=True)
|
||||
with open(log_file, "a") as log:
|
||||
for package in missing:
|
||||
print(f"Instalando {package}")
|
||||
subprocess.run(["sudo", "apt-get", "install", "-y", package], check=True)
|
||||
print(f"{package} instalado correctamente.")
|
||||
log.write(package + "\n")
|
||||
print("Todos los paquetes faltantes han sido instalados.")
|
||||
|
||||
def autoConfigure():
|
||||
# Detectar sistema operativo del servidor
|
||||
os_info = platform.uname()
|
||||
OSDISTRIB = os_info.system.lower()
|
||||
OSVERSION = os_info.release.split('.')[0]
|
||||
# Configuración según la distribución GNU/Linux
|
||||
if OSDISTRIB in ['linux', 'linux2']:
|
||||
if OSDISTRIB == 'linux':
|
||||
# Obtener información del sistema desde el archivo os-release
|
||||
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]
|
||||
else:
|
||||
# Obtener información del sistema utilizando el comando lsb_release
|
||||
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)
|
||||
print (f"LOG\tDistribución de OS Linux: {OSDISTRIB}")
|
||||
if OSDISTRIB in ['ubuntu', 'debian', 'linuxmint']: #DEPENDENCIES = ['subversion', 'apache2', 'php', 'php-ldap', 'php-fpm', 'isc-dhcp-server', 'bittorrent', 'tftp-hpa', 'tftpd-hpa', 'xinetd', 'build-essential', 'g++-multilib', 'wget', 'curl', 'graphviz', 'bittornado', 'ctorrent', 'samba', 'rsync', 'unzip', 'netpipes', 'debootstrap', 'schroot', 'squashfs-tools', 'btrfs-tools', 'procps', 'arp-scan', 'realpath', 'php-curl', 'gettext', 'moreutils', 'jq', 'wakeonlan', 'udpcast', 'libev-dev', 'libjansson-dev', 'libssl-dev', 'shim-signed', 'grub-efi-amd64-signed', 'gawk', 'libdbi-dev', 'libdbi1', 'automake', 'liblz4-tool']
|
||||
#DEPENDENCIESX = ["vi"]
|
||||
DEPENDENCIES2= []
|
||||
#DEPENDENCIES2.append("htop")
|
||||
#DEPENDENCIES2.append("vi")
|
||||
#print (f"LOG\tDependencias identificadas: {DEPENDENCIES2}")
|
||||
#UPDATEPKGLIST = "apt-get update"
|
||||
# INSTALLPKG = "apt-get -y install --force-yes"
|
||||
# CHECKPKG = "dpkg -s $package 2>/dev/null | grep Status | grep -qw install"
|
||||
# STARTSERVICE = "service $service restart" if subprocess.call(['which', 'service'], stdout=subprocess.DEVNULL) == 0 else "/etc/init.d/$service restart"
|
||||
# STOPSERVICE = "service $service stop" if subprocess.call(['which', 'service'], stdout=subprocess.DEVNULL) == 0 else "/etc/init.d/$service stop"
|
||||
# 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 ssl rewrite proxy_fcgi fastcgi actions alias"
|
||||
# APACHEENABLESSL = "a2ensite default-ssl"
|
||||
# APACHEENABLEOG = "a2ensite $APACHEOGSITE"
|
||||
# APACHEMAKECERT = "make-ssl-cert generate-default-snakeoil --force-overwrite"
|
||||
# DHCPSERV = "isc-dhcp-server"
|
||||
# DHCPCFGDIR = "/etc/dhcp"
|
||||
# INETDSERV = "xinetd"
|
||||
# INETDCFGDIR = "/etc/xinetd.d"
|
||||
# PHPFPMSERV = "php-fpm"
|
||||
# RSYNCSERV = "rsync"
|
||||
# RSYNCCFGDIR = "/etc"
|
||||
# SAMBASERV = "smbd"
|
||||
# SAMBACFGDIR = "/etc/samba"
|
||||
# TFTPCFGDIR = "/var/lib/tftpboot"
|
||||
elif OSDISTRIB in ['fedora', 'centos']:
|
||||
# DEPENDENCIES = ['subversion', 'httpd', 'mod_ssl', 'php-ldap', 'php-fpm', 'dhcp', 'tftp-server', 'tftp', 'xinetd', 'binutils', 'gcc', 'gcc-c++', 'glibc-devel', 'glibc-devel.i686', 'glibc-static', 'glibc-static.i686', 'libstdc++-devel.i686', 'make', 'wget', 'curl', 'doxygen', 'graphviz', 'ctorrent', 'samba', 'samba-client', 'rsync', 'unzip', 'debootstrap', 'schroot', 'squashfs-tools', 'python-crypto', 'arp-scan', 'procps-ng', 'gettext', 'moreutils', 'jq', 'net-tools', 'udpcast', 'libev-devel', 'jansson-devel', 'openssl-devel', 'shim-x64', 'grub2-efi-x64', 'grub2-efi-x64-modules', 'gawk', 'libdbi-devel', 'libdbi', 'http://ftp.altlinux.org/pub/distributions/ALTLinux/5.1/branch/$(arch)/RPMS.classic/netpipes-4.2-alt1.$(arch).rpm']
|
||||
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:
|
||||
print("ERROR: Distribution not supported by OpenGnsys.")
|
||||
exit(1)
|
||||
else:
|
||||
print("ERROR: Unknown Linux distribution.")
|
||||
exit(1)
|
||||
|
||||
def generate_config_url():
|
||||
with open('/etc/os-release', 'r') as file:
|
||||
lines = file.readlines()
|
||||
codename = None
|
||||
version = None
|
||||
for line in lines:
|
||||
if line.startswith('VERSION_CODENAME') or line.startswith('UBUNTU_CODENAME'):
|
||||
codename = line.split('=')[1].strip().strip('"')
|
||||
elif line.startswith('VERSION_ID'):
|
||||
version = line.split('=')[1].strip().strip('"')
|
||||
arch = os.uname().machine
|
||||
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):
|
||||
# Cambia al usuario ogboot y crea el proyecto Symfony
|
||||
subprocess.run(["composer", "create-project", "symfony/website-skeleton", path_opengnsys_base])
|
||||
# Elimina el archivo composer.lock
|
||||
subprocess.run(["rm", f"{path_opengnsys_base}/composer.lock"])
|
||||
print("Esqueleto de la aplicación creado y archivo composer.lock eliminado.")
|
||||
|
||||
def createDirs(INSTALL_TARGET):
|
||||
if not os.path.exists(INSTALL_TARGET):
|
||||
try:
|
||||
os.makedirs(INSTALL_TARGET)
|
||||
except OSError:
|
||||
errorAndLog("Error while creating directory paths!")
|
||||
exit(1)
|
||||
else:
|
||||
echoAndLog(f"Directory {INSTALL_TARGET} already exists.")
|
||||
|
||||
###############################################################################
|
||||
###:::::::::::::::::::::::::::: CONFIGURE ::::::::::::::::::::::::::::::::::###
|
||||
###############################################################################
|
||||
|
||||
def install_isc_kea():
|
||||
url = generate_config_url()
|
||||
print(f"URL generada: {url}")
|
||||
print("Ejecutando curl para obtener el archivo de configuración del repositorio KEA...")
|
||||
subprocess.run(["curl", "-1sLf", url, ">", "/etc/apt/sources.list.d/isc-kea-2-0.list"], check=True)
|
||||
print("El archivo de configuración del repositorio KEA ha sido guardado en /etc/apt/sources.list.d/isc-kea-2-0.list")
|
||||
|
||||
print("Descargando y agregando la clave GPG del repositorio KEA...")
|
||||
subprocess.run(["curl", "-1sLf", "https://dl.cloudsmith.io/public/isc/kea-2-0/gpg.8029D4AFA58CBB5E.key", "|", "gpg", "--dearmor", ">>", "/usr/share/keyrings/isc-kea-2-0-archive-keyring.gpg"], check=True)
|
||||
print("La clave GPG del repositorio KEA ha sido descargada y agregada correctamente")
|
||||
|
||||
print("Ejecutando apt-get update...")
|
||||
subprocess.run(["apt-get", "update"], check=True)
|
||||
|
||||
print("Comprobando disponibilidad de los paquetes...")
|
||||
if subprocess.run(["apt-cache", "show", "isc-kea-common"], capture_output=True).returncode == 0:
|
||||
print("El paquete isc-kea-common está disponible para ser instalado.")
|
||||
print("Descargando e instalando el paquete isc-kea-common...")
|
||||
subprocess.run(["apt-get", "install", "-y", "isc-kea-common"], check=True)
|
||||
print("El paquete isc-kea-common ha sido descargado e instalado correctamente.")
|
||||
else:
|
||||
print("El paquete isc-kea-common no está disponible en los repositorios apt.")
|
||||
|
||||
if subprocess.run(["apt-cache", "show", "isc-kea-dhcp4-server"], capture_output=True).returncode == 0:
|
||||
print("El paquete isc-kea-dhcp4-server está disponible para ser instalado.")
|
||||
print("Descargando e instalando el paquete isc-kea-dhcp4-server...")
|
||||
subprocess.run(["apt-get", "install", "-y", "isc-kea-dhcp4-server"], check=True)
|
||||
print("El paquete isc-kea-dhcp4-server ha sido descargado e instalado correctamente.")
|
||||
else:
|
||||
print("El paquete isc-kea-dhcp4-server no está disponible en los repositorios apt.")
|
||||
|
||||
if subprocess.run(["apt-cache", "show", "isc-kea-dhcp6-server"], capture_output=True).returncode == 0:
|
||||
print("El paquete isc-kea-dhcp6-server está disponible para ser instalado.")
|
||||
print("Descargando e instalando el paquete isc-kea-dhcp6-server...")
|
||||
subprocess.run(["apt-get", "install", "-y", "isc-kea-dhcp6-server"], check=True)
|
||||
print("El paquete isc-kea-dhcp6-server ha sido descargado e instalado correctamente.")
|
||||
else:
|
||||
print("El paquete isc-kea-dhcp6-server no está disponible en los repositorios apt.")
|
||||
|
||||
if subprocess.run(["apt-cache", "show", "isc-kea-dhcp-ddns-server"], capture_output=True).returncode == 0:
|
||||
print("El paquete isc-kea-dhcp-ddns-server está disponible para ser instalado.")
|
||||
print("Descargando e instalando el paquete isc-kea-dhcp-ddns-server...")
|
||||
subprocess.run(["apt-get", "install", "-y", "isc-kea-dhcp-ddns-server"], check=True)
|
||||
print("El paquete isc-kea-dhcp-ddns-server ha sido descargado e instalado correctamente.")
|
||||
else:
|
||||
print("El paquete isc-kea-dhcp-ddns-server no está disponible en los repositorios apt.")
|
||||
|
||||
if subprocess.run(["apt-cache", "show", "isc-kea-ctrl-agent"], capture_output=True).returncode == 0:
|
||||
print("El paquete isc-kea-ctrl-agent está disponible para ser instalado.")
|
||||
print("Descargando e instalando el paquete isc-kea-ctrl-agent...")
|
||||
subprocess.run(["apt-get", "install", "-y", "isc-kea-ctrl-agent"], check=True)
|
||||
print("El paquete isc-kea-ctrl-agent ha sido descargado e instalado correctamente.")
|
||||
else:
|
||||
print("El paquete isc-kea-ctrl-agent no está disponible en los repositorios apt.")
|
||||
|
||||
def install_kea():
|
||||
print(f"{install_kea.__name__}(): Instalación de muestra para el servicio DHCP de Kea.")
|
||||
|
||||
def backupFile(file):
|
||||
|
||||
if not os.path.isfile(file):
|
||||
print(f"WARNING: file {file} doesn't exist")
|
||||
return
|
||||
|
||||
print(f"Making backup of {file}")
|
||||
shutil.copy2(file, f"{file}-LAST")
|
||||
|
||||
dateymd = datetime.datetime.now().strftime("%Y%m%d")
|
||||
backup_file = f"{file}-{dateymd}"
|
||||
if not os.path.isfile(backup_file):
|
||||
shutil.copy2(file, backup_file)
|
||||
|
||||
print(f"Backup of {file} successful")
|
||||
|
||||
def isc_keaDhcpConfigure():
|
||||
print(f"{isc_keaDhcpConfigure.__name__}(): Configuración de muestra para el servicio DHCP de Kea.")
|
||||
|
||||
errcode = 0
|
||||
i = 0
|
||||
interfaces = "["
|
||||
DEVICE = ["eth0", "eth1"] # Define the DEVICE variable with appropriate values
|
||||
NETMASK = "255.255.255.0" # Replace with the actual value of NETMASK
|
||||
NETIP = "192.168.0.1" # Replace with the actual value of NETIP
|
||||
ROUTERIP = "192.168.0.1" # Replace with the actual value of ROUTERIP
|
||||
DNSIP = "192.168.0.1" # Replace with the actual value of DNSIP
|
||||
SERVERIP = "192.168.0.1" # Replace with the actual value of SERVERIP
|
||||
|
||||
# Rest of the code...
|
||||
|
||||
# Define the NETMASK variable
|
||||
|
||||
# Construir la lista de interfaces
|
||||
print(NETIP)
|
||||
print(NETMASK)
|
||||
#CIDR = mask2cidr(NETMASK)
|
||||
#print(CIDR) # Salida: 24
|
||||
print(ROUTERIP)
|
||||
print(DNSIP)
|
||||
|
||||
with open("/etc/kea/kea-dhcp4.conf", "w") as file:
|
||||
file.write(
|
||||
f"""
|
||||
"interfaces": {interfaces},
|
||||
"SERVERIP": "{SERVERIP}",
|
||||
"NETIP": "{NETIP}",
|
||||
"NETMASK": "{NETMASK}",
|
||||
"ROUTERIP": "{ROUTERIP}",
|
||||
"DNSIP": "{DNSIP}"
|
||||
"""
|
||||
)
|
||||
# Si hubo errores al configurar, muestra un mensaje de error y sale de la función
|
||||
if errcode != 0:
|
||||
print(f"{isc_keaDhcpConfigure.__name__}(): Error al configurar el servicio DHCP de Kea.")
|
||||
return 1
|
||||
|
||||
print(f"{isc_keaDhcpConfigure.__name__}(): Configuración de muestra para el servicio DHCP de Kea configurada en \"/etc/kea/kea-dhcp4.conf\".")
|
||||
return 0
|
||||
|
||||
def testPxe():
|
||||
echoAndLog(f"{testPxe.__name__}(): Checking TFTP service... please wait.")
|
||||
subprocess.run(["echo", "test"], stdout=open(f"{TFTPCFGDIR}/testpxe", "w"))
|
||||
try:
|
||||
subprocess.run(["tftp", "-v", "127.0.0.1", "-c", "get", "testpxe", "/tmp/testpxe"], check=True)
|
||||
echoAndLog("TFTP service is OK.")
|
||||
except subprocess.CalledProcessError:
|
||||
errorAndLog("TFTP service is down.")
|
||||
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([ENABLESERVICE])
|
||||
subprocess.run([STARTSERVICE])
|
||||
service = INETDSERV
|
||||
subprocess.run([ENABLESERVICE])
|
||||
subprocess.run([STARTSERVICE])
|
||||
|
||||
# comprobamos el servicio tftp
|
||||
time.sleep(1)
|
||||
testPxe()
|
||||
|
||||
def smbConfigure():
|
||||
echoAndLog(f"{smbConfigure.__name__}(): Configuring Samba service.")
|
||||
|
||||
backupFile(f"{SAMBACFGDIR}/smb.conf")
|
||||
|
||||
# Copiar plantilla de recursos para OpenGnsys
|
||||
with open(os.path.join(WORKDIR, 'ogboot/etc/smb-ogboot.conf.tmpl'), 'r') as tmpl_file:
|
||||
template = tmpl_file.read()
|
||||
replaced_template = template.replace('OGBOOTDIR', INSTALL_OGBOOT_TARGET)
|
||||
|
||||
with open(os.path.join(SAMBACFGDIR, 'smb-ogboot.conf'), 'w') as conf_file:
|
||||
conf_file.write(replaced_template)
|
||||
|
||||
# Configurar y recargar Samba"
|
||||
subprocess.run(["perl", "-pi", "-e", "s/WORKGROUP/OPENGNSYS/; s/server string \=.*/server string \= ogBoot Samba Server/", f"{SAMBACFGDIR}/smb.conf"])
|
||||
with open(f"{SAMBACFGDIR}/smb.conf", "a") as file:
|
||||
if not any("smb-ogboot" in line for line in file):
|
||||
file.write(f"include = {SAMBACFGDIR}/smb-ogboot.conf\n")
|
||||
service = SAMBASERV
|
||||
subprocess.run([ENABLESERVICE])
|
||||
subprocess.run([STARTSERVICE])
|
||||
if subprocess.run(["echo", "$?"]).returncode != 0:
|
||||
errorAndLog(f"{smbConfigure.__name__}(): error while configure Samba")
|
||||
return 1
|
||||
# Crear clave para usuario de acceso a los recursos.
|
||||
smbpasswd_command = f"echo -ne \"{OPENGNSYS_CLIENT_PASSWD}\\n{OPENGNSYS_CLIENT_PASSWD}\\n\" | smbpasswd -a -s {OPENGNSYS_CLIENT_USER}"
|
||||
subprocess.run(smbpasswd_command, shell=True)
|
||||
|
||||
echoAndLog(f"{smbConfigure.__name__}(): Added Samba configuration.")
|
||||
return 0
|
||||
|
||||
###############################################################################
|
||||
###:::::::::::::::::::::::::::::::: MAIN :::::::::::::::::::::::::::::::::::###
|
||||
###############################################################################
|
||||
if os.geteuid() != 0:
|
||||
print("ERROR: este programa debe ejecutarse con privilegios de root.")
|
||||
exit(1)
|
||||
if os.path.exists(os.path.join(INSTALL_OGBOOT_TARGET + "/doc/")):
|
||||
echoAndLog(f"ERROR\togBoot ya esta instalado. Ejecute {INSTALL_OGBOOT_TARGET}/lib/ogboot_devel_update.py\" con privilegios de root para actualizar.")
|
||||
exit(2)
|
||||
check_distribution()
|
||||
check_python_version()
|
||||
autoConfigure()
|
||||
Missing = get_missing_packages (required_packages_22)
|
||||
install_packages(Missing)
|
||||
|
||||
if REMOTE == 1:
|
||||
downloadCode(GIT_REPO)
|
||||
if os.system("echo $?") != 0:
|
||||
errorAndLog("Error while getting code from the repository")
|
||||
exit(1)
|
||||
else:
|
||||
if not os.path.exists("ogboot"):
|
||||
os.symlink(os.path.dirname(PROGRAM_DIR), "ogboot")
|
||||
|
||||
create_ogboot_project(INSTALL_TARGET)
|
||||
if os.system("echo $?") != 0:
|
||||
errorAndLog("Error while creating skeleton directory!")
|
||||
exit(1)
|
||||
|
||||
createDirs(INSTALL_TARGET)
|
||||
if os.system("echo $?") != 0:
|
||||
errorAndLog("Error while creating directory paths!")
|
||||
exit(1)
|
||||
|
||||
# Configuración de TFTP.
|
||||
#tftpConfigure()
|
||||
|
||||
# Configuración de Samba.
|
||||
#smbConfigure()
|
||||
|
||||
if subprocess.run(["echo", "$?"]).returncode != 0:
|
||||
errorAndLog("Error while configuring Samba server!")
|
||||
exit(1)
|
||||
|
||||
###############################################################################
|
||||
###:::::::::::::::::::::::::::::::: DHCP :::::::::::::::::::::::::::::::::::###
|
||||
###############################################################################
|
||||
parser = argparse.ArgumentParser(description='Instalación de ogBoot', formatter_class=argparse.RawTextHelpFormatter)
|
||||
help_text_dhcp = """
|
||||
install\t\tInstala la versión de DHCP seleccionada con dhcp-version,
|
||||
configure\tConfigura dhcp sobre un servicio previamente instalado,
|
||||
none\t\tNo instala ni configura DHCP
|
||||
"""
|
||||
help_text_dhcp_version = """
|
||||
isc\t\tInstala la versión de DHCP isc-kea-dhcp, para Ubuntu 18.04
|
||||
kea\t\tInstala la versión de DHCP kea-dhcp, para Ubuntu 20.04
|
||||
none\t\tNo instala ni configura DHCP
|
||||
"""
|
||||
parser.add_argument('--dhcp', choices=['install', 'configure', 'none'], default='none', help=help_text_dhcp)
|
||||
parser.add_argument('--dhcp-version', choices=['isc', 'kea', 'none'], default='none', help=help_text_dhcp_version)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.dhcp == 'install':
|
||||
if args.dhcp_version == 'isc':
|
||||
print("Instalando isc-kea-dhcp")
|
||||
install_isc_kea()
|
||||
isc_keaDhcpConfigure()
|
||||
pass
|
||||
elif args.dhcp_version == 'kea':
|
||||
print("Instalando kea-dhcp")
|
||||
install_kea()
|
||||
pass
|
||||
elif args.dhcp_version == 'none':
|
||||
print("No se ha seleccionado ninguna versión de DHCP, utilice --dhcp-version para seleccionar una versión")
|
||||
pass
|
||||
pass
|
||||
elif args.dhcp == 'configure':
|
||||
print("Configurando DHCP")
|
||||
if args.dhcp_version == 'isc':
|
||||
isc_keaDhcpConfigure()
|
||||
pass
|
||||
elif args.dhcp_version == 'kea':
|
||||
print("Configurando kea-dhcp")
|
||||
#keaDhcpConfigure()
|
||||
pass
|
||||
elif args.dhcp_version == 'none':
|
||||
print("No se ha seleccionado ninguna versión de DHCP, utilice --dhcp-version para seleccionar una versión")
|
||||
pass
|
||||
pass
|
||||
elif args.dhcp == 'none':
|
||||
print("DHCP no será instalado ni configurado")
|
||||
pass
|
Loading…
Reference in New Issue