Refactor postinst and prerm

pull/9/head
Nicolas Arenas 2025-03-10 20:37:44 +01:00
parent 07d86be9b0
commit 703c28e4ce
2 changed files with 90 additions and 26 deletions

View File

@ -36,6 +36,7 @@ fi
USER_UID=$(echo "$USER_INFO" | cut -d: -f3)
USER_GID=$(echo "$USER_INFO" | cut -d: -f4)
INSTALL_OGBOOT_TARGET="/opt/opengnsys/ogboot"
fstab_entries=(
"$INSTALL_OGBOOT_TARGET/lib/oglive.iso $INSTALL_OGBOOT_TARGET/mnt iso9660 loop,ro,users,uid=$USER_UID,gid=$USER_GID,noauto 0 0"
@ -43,11 +44,8 @@ fstab_entries=(
)
fstab_file="/etc/fstab"
# Detectar si es una instalación nueva o una actualización
if [ "$1" = "configure" ] && [ -z "$2" ]; then
# Copy sudo configuration
echo "Primera instalación"
configure_sudo() {
echo "Copiando configuración de sudo"
if [ ! -f /etc/sudoers.d/opengnsys ]; then
cp /opt/opengnsys/ogboot/etc/ogboot.sudoers /etc/sudoers.d/opengnsys
@ -56,8 +54,9 @@ if [ "$1" = "configure" ] && [ -z "$2" ]; then
else
echo "El archivo /etc/sudoers.d/opengnsys ya existe."
fi
# Add fstab entries
}
add_fstab_entries(){
echo "Añadiendo entradas a /etc/fstab"
for entry in "${fstab_entries[@]}"; do
if ! grep -Fxq "$entry" "$fstab_file"; then
@ -67,9 +66,15 @@ if [ "$1" = "configure" ] && [ -z "$2" ]; then
echo "La entrada ya existe en /etc/fstab: $entry"
fi
done
}
update_opengnsys_user() {
echo "Actualizando grupos del usuario opengnsys"
usermod -aG disk "$USER"
}
configure_tftp(){
echo "Modificando el archivo tftpboot"
if ! dpkg-divert --list /etc/default/tftpd-hpa >/dev/null 2>&1; then
@ -78,10 +83,14 @@ if [ "$1" = "configure" ] && [ -z "$2" ]; then
if [ ! -f /etc/default/tftpd-hpa ]; then
cp /opt/opengnsys/ogboot/etc/default/tftpd-hpa /etc/default/tftpd-hpa
fi
}
install_oglive(){
echo "Download ogLive"
/opt/opengnsys/ogboot/bin/oglivecli download "$DEFAULT_OGLIVE"
}
configure_ipxe(){
echo "Configure ipxe templates"
cp $OGBOOT_DIR/etc/dhcp_boot.ipxe.tmpl $OGBOOT_DIR/tftpboot/ipxe_scripts/dhcp_boot.ipxe
cp $OGBOOT_DIR/etc/default.ipxe.tmpl $OGBOOT_DIR/tftpboot/ipxe_scripts/default.ipxe
@ -98,6 +107,9 @@ if [ "$1" = "configure" ] && [ -z "$2" ]; then
cp bin-x86_64-efi/ipxe.efi $OGBOOT_DIR/tftpboot/
fi
}
configure_api(){
echo "Modify env.local.php"
if ! dpkg-divert --list /opt/opengnsys/ogboot/.env.local.php >/dev/null 2>&1; then
dpkg-divert --add --rename --divert /opt/opengnsys/ogboot/.env.local.php.orig /opt/opengnsys/ogboot/.env.local.php
@ -108,7 +120,9 @@ if [ "$1" = "configure" ] && [ -z "$2" ]; then
sed -i "s|'OGBOOT_IP' => '.*'|'OGBOOT_IP' => '$OGBOOT_IP'|" "$ENV_FILE"
sed -i "s|'OGBOOT_PORT' => '.*'|'OGBOOT_PORT' => '$OGBOOT_PORT'|" "$ENV_FILE"
sed -i "s|'OGCORE_API_URL' => '.*'|'OGCORE_API_URL' => '$OGCORE_API_URL'|" "$ENV_FILE"
}
configure_nginx_and_fpm() {
echo "Configure nginx"
PHP_VERSION=$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')
if [ ! -f /etc/nginx/sites-available/ogboot.conf ]; then
@ -127,9 +141,11 @@ if [ "$1" = "configure" ] && [ -z "$2" ]; then
if [ ! -f /etc/php/$PHP_VERSION/fpm/pool.d/ogboot.conf ]; then
cp /opt/opengnsys/ogboot/etc/ogboot-fpm.conf /etc/php/$PHP_VERSION/fpm/pool.d/ogboot.conf
fi
}
configure_samba(){
echo "Configurando Samba"
dpkg-divert --packege ogboot --add --rename --divert /etc/samba/smb.conf.orig /etc/samba/smb.conf
dpkg-divert --package ogboot --add --rename --divert /etc/samba/smb.conf.orig /etc/samba/smb.conf
cp /etc/samba/smb.conf.orig /etc/samba/smb.conf
mkdir -p /etc/samba/opengsys/
cp /opt/opengnsys/ogboot/etc/samba/smb-ogboot.conf /etc/samba/opengsys/
@ -138,25 +154,48 @@ if [ "$1" = "configure" ] && [ -z "$2" ]; then
if ! grep -q "$INCLUDE_LINE" /etc/samba/smb.conf; then
echo "$INCLUDE_LINE" | sudo tee -a /etc/samba/smb.conf > /dev/null
fi
}
elif [ "$1" = "configure" ] && [ -n "$2" ]; then
echo "Actualización desde la versión $2"
fi
# Cambiar la propiedad de los archivos al usuario especificado
configure_permissions(){
echo "Cambiando la propiedad de los archivos al usuario $USER"
chown opengnsys:www-data /opt/opengnsys/
chown -R opengnsys:www-data /opt/opengnsys/ogboot
}
# Install http server stuff
# Reiniciar servicios si es necesario
# systemctl restart nombre_del_servicio
restart_services(){
systemctl daemon-reload
systemctl restart nginx
systemctl restart tftpd-hpa
systemctl restart php8.3-fpm
systemctl restart samba
}
# Detectar si es una instalación nueva o una actualización
if [ "$1" = "configure" ] && [ -z "$2" ]; then
# Copy sudo configuration
echo "Primera instalación"
configure_sudo
add_fstab_entries
update_opengnsys_user
configure_tftp
install_oglive
configure_ipxe
configure_api
configure_nginx_and_fpm
configure_samba
elif [ "$1" = "configure" ] && [ -n "$2" ]; then
echo "Actualización desde la versión $2"
configure_sudo
configure_tftp
configure_api
configure_nginx_and_fpm
configure_samba
fi
configure_permissions
restart_services
exit 0

25
debian/ogboot.prerm vendored 100644
View File

@ -0,0 +1,25 @@
#!/bin/bash
set -e
case "$1" in
remove|upgrade|deconfigure)
echo "Deteniendo servicios antes de la eliminación o actualización..."
systemctl stop nginx || true
systemctl stop tftpd-hpa || true
systemctl stop php8.3-fpm || true
systemctl stop samba || true
echo "Eliminando configuraciones específicas..."
if [ "$1" = "remove" ]; then
rm -f /etc/nginx/sites-enabled/ogboot.conf
rm -f /etc/nginx/sites-available/ogboot.conf
rm -f /etc/sudoers.d/opengnsys
rm -f /etc/php/8.3/fpm/pool.d/ogboot.conf
dpkg-divert --remove --rename --divert /etc/default/tftpd-hpa.orig /etc/default/tftpd-hpa || true
dpkg-divert --remove --rename --divert /opt/opengnsys/ogboot/.env.local.php.orig /opt/opengnsys/ogboot/.env.local.php || true
fi
;;
esac
exit 0