#!/bin/sh set -e set -x . /usr/share/debconf/confmodule # Cargar variables de configuración db_get opengnsys/ogrepository_ogrepoIp OGREPO_IP="$RET" db_get opengnsys/ogrepository_ogcoreIp OGCORE_IP="$RET" db_get opengnsys/ogrepository_sambaUser SAMBA_USER="$RET" db_get opengnsys/ogrepository_sambaUserPass SAMBA_PASS="$RET" # Asegurarse de que el usuario exista USER="opengnsys" # Provisionar base de datos si es necesario en caso de instalación. # Detectar si es una instalación nueva o una actualización if [ "$1" = "configure" ] && [ -z "$2" ]; then systemd-run --no-block /bin/bash -c " sleep 10; apt update -y; for pkg in bittorrent bittornado ctorrent; do if ! dpkg -l | grep -qw \"\$pkg\"; then apt install -y \"\$pkg\" fi done " sed -i "s/%%OGREPOSITORY_USER%%/$SAMBA_USER/g" /etc/systemd/system/ogrepo-api.service sed -i "s/%%OGREPOSITORY_USER%%/$SAMBA_USER/g" /etc/samba/ogrepo-smb.conf # Install ssh keys on opengnsys user # Get opengnsys home directory OPENGNSYS_HOME=$(getent passwd opengnsys | cut -d: -f6) # Create .ssh directory mkdir -p $OPENGNSYS_HOME/.ssh # Copy ssh keys cp /opt/opengnsys/ogrepository/etc/opengnsys $OPENGNSYS_HOME/.ssh/id_ed25519 cp /opt/opengnsys/ogrepository/etc/opengnsys.pub $OPENGNSYS_HOME/.ssh/id_ed25519.pub # Change permissions chmod 700 $OPENGNSYS_HOME/.ssh chmod 600 $OPENGNSYS_HOME/.ssh/id_ed25519 chmod 644 $OPENGNSYS_HOME/.ssh/id_ed25519.pub # Genera authorized_keys cat $OPENGNSYS_HOME/.ssh/id_ed25519.pub >> $OPENGNSYS_HOME/.ssh/authorized_keys chmod 600 $OPENGNSYS_HOME/.ssh/authorized_keys chown -R opengnsys:opengnsys $OPENGNSYS_HOME/.ssh if [ -f /etc/samba/smb.conf ]; then dpkg-divert --package ogrepository --add --rename --divert /etc/samba/smb.conf.orig /etc/samba/smb.conf cp /etc/samba/smb.conf.orig /etc/samba/smb.conf if ! grep -q "include = /etc/samba/ogrepo-smb.conf" /etc/samba/smb.conf; then echo "include = /etc/samba/ogrepo-smb.conf" >> /etc/samba/smb.conf fi (echo "$SAMBA_PASS"; echo "$SAMBA_PASS") | smbpasswd -a $SAMBA_USER fi # Configure Repo cp /opt/opengnsys/ogrepository/etc/ogAdmRepo.cfg.tmpl /opt/opengnsys/ogrepository/etc/ogAdmRepo.cfg sed -i "s/SERVERIP/$OGREPO_IP/g" /opt/opengnsys/ogrepository/etc/ogAdmRepo.cfg sed -i "s/OGCOREIP/$OGCORE_IP/g" /opt/opengnsys/ogrepository/etc/ogAdmRepo.cfg # Copiar sudoers file ogrepository/etc/opengnsys-repository cp /opt/opengnsys/ogrepository/etc/opengnsys-repository /etc/sudoers.d/opengnsys elif [ "$1" = "configure" ] && [ -n "$2" ]; then echo "Actualización desde la versión $2" # Recopy static files without configuration cp /opt/opengnsys/ogrepository/etc/opengnsys-repository /etc/sudoers.d/opengnsys-repository OPENGNSYS_HOME=$(getent passwd opengnsys | cut -d: -f6) # Create .ssh directory mkdir -p $OPENGNSYS_HOME/.ssh # Copy ssh keys cp /opt/opengnsys/ogrepository/etc/opengnsys $OPENGNSYS_HOME/.ssh/id_ed25519 cp /opt/opengnsys/ogrepository/etc/opengnsys.pub $OPENGNSYS_HOME/.ssh/id_ed25519.pub cat $OPENGNSYS_HOME/.ssh/id_ed25519.pub >> $OPENGNSYS_HOME/.ssh/authorized_keys fi # Cambiar la propiedad de los archivos al usuario especificado chown opengnsys:www-data /opt/opengnsys/ chown -R opengnsys:www-data /opt/opengnsys/ogrepository chmod 755 /opt/opengnsys/ogrepository/bin/* # Install http server stuff # Reiniciar servicios si es necesario # systemctl restart nombre_del_servicio systemctl daemon-reload systemctl enable ogrepo-api systemctl restart ogrepo-api systemctl restart smbd # echo "ogrepository-trigger" > /var/lib/dpkg/triggers/ogrepository.trigger # POST_INVOKE_SCRIPT="/var/lib/dpkg/info/ogrepository.trigger" # echo "Registrando post-invoke para instalación de paquetes adicionales..." # cat < "$POST_INVOKE_SCRIPT" # #!/bin/sh # set -e # echo "Ejecutando post-trigger: Instalando bittorrent, bittornado y ctorrent..." # export DEBIAN_FRONTEND=noninteractive # apt-get update # apt-get install -y bittorrent bittornado ctorrent # echo "Instalación de paquetes adicionales completada." # exit 0 # EOF # chmod +x "$POST_INVOKE_SCRIPT" # # Intentar ejecutar el script ahora si no hay otro proceso de apt corriendo # if ! pgrep -x "apt" > /dev/null; then # echo "Ejecutando instalación de paquetes adicionales inmediatamente..." # "$POST_INVOKE_SCRIPT" # else # echo "Apt está en uso, los paquetes se instalarán después." # fi # find /opt/opengnsys/ogrepository -type f -name "*.py" -exec chmod +x {} \; # dpkg-trigger --by-package=ogrepository ogrepository-trigger exit 0