| 1 | #!/bin/bash |
|---|
| 2 | |
|---|
| 3 | ##################################################################### |
|---|
| 4 | ####### Script instalador OpenGnsys |
|---|
| 5 | ####### Autor: Luis Guillén <lguillen@unizar.es> |
|---|
| 6 | ##################################################################### |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | #### AVISO: Puede editar configuración de acceso por defecto. |
|---|
| 10 | #### WARNING: Edit default access configuration if you wish. |
|---|
| 11 | DEFAULT_MYSQL_ROOT_PASSWORD="passwordroot" # Clave por defecto root de MySQL |
|---|
| 12 | DEFAULT_OPENGNSYS_DB_USER="usuog" # Usuario por defecto de acceso a la base de datos |
|---|
| 13 | DEFAULT_OPENGNSYS_DB_PASSWD="passusuog" # Clave por defecto de acceso a la base de datos |
|---|
| 14 | DEFAULT_OPENGNSYS_CLIENT_PASSWD="og" # Clave por defecto de acceso del cliente |
|---|
| 15 | |
|---|
| 16 | # Sólo ejecutable por usuario root |
|---|
| 17 | if [ "$(whoami)" != 'root' ]; then |
|---|
| 18 | echo "ERROR: this program must run under root privileges!!" |
|---|
| 19 | exit 1 |
|---|
| 20 | fi |
|---|
| 21 | |
|---|
| 22 | echo -e "\\nOpenGnsys Installation" |
|---|
| 23 | echo "==============================" |
|---|
| 24 | |
|---|
| 25 | # Clave root de MySQL |
|---|
| 26 | while : ; do |
|---|
| 27 | echo -n -e "\\nEnter root password for MySQL (${DEFAULT_MYSQL_ROOT_PASSWORD}): "; |
|---|
| 28 | read -r MYSQL_ROOT_PASSWORD |
|---|
| 29 | if [ -n "${MYSQL_ROOT_PASSWORD//[a-zA-Z0-9]/}" ]; then # Comprobamos que sea un valor alfanumerico |
|---|
| 30 | echo -e "\\aERROR: Must be alphanumeric, try again..." |
|---|
| 31 | else |
|---|
| 32 | # Si esta vacio ponemos el valor por defecto |
|---|
| 33 | MYSQL_ROOT_PASSWORD="${MYSQL_ROOT_PASSWORD:-$DEFAULT_MYSQL_ROOT_PASSWORD}" |
|---|
| 34 | break |
|---|
| 35 | fi |
|---|
| 36 | done |
|---|
| 37 | |
|---|
| 38 | # Usuario de acceso a la base de datos |
|---|
| 39 | while : ; do |
|---|
| 40 | echo -n -e "\\nEnter username for OpenGnsys console (${DEFAULT_OPENGNSYS_DB_USER}): " |
|---|
| 41 | read -r OPENGNSYS_DB_USER |
|---|
| 42 | if [ -n "${OPENGNSYS_DB_USER//[a-zA-Z0-9]/}" ]; then # Comprobamos que sea un valor alfanumerico |
|---|
| 43 | echo -e "\\aERROR: Must be alphanumeric, try again..." |
|---|
| 44 | else |
|---|
| 45 | # Si esta vacio ponemos el valor por defecto |
|---|
| 46 | OPENGNSYS_DB_USER="${OPENGNSYS_DB_USER:-$DEFAULT_OPENGNSYS_DB_USER}" |
|---|
| 47 | break |
|---|
| 48 | fi |
|---|
| 49 | done |
|---|
| 50 | |
|---|
| 51 | # Clave de acceso a la base de datos |
|---|
| 52 | while : ; do |
|---|
| 53 | echo -n -e "\\nEnter password for OpenGnsys console (${DEFAULT_OPENGNSYS_DB_PASSWD}): " |
|---|
| 54 | read -r OPENGNSYS_DB_PASSWD |
|---|
| 55 | if [ -n "${OPENGNSYS_DB_PASSWD//[a-zA-Z0-9]/}" ]; then # Comprobamos que sea un valor alfanumerico |
|---|
| 56 | echo -e "\\aERROR: Must be alphanumeric, try again..." |
|---|
| 57 | else |
|---|
| 58 | # Si esta vacio ponemos el valor por defecto |
|---|
| 59 | OPENGNSYS_DB_PASSWD="${OPENGNSYS_DB_PASSWD:-$DEFAULT_OPENGNSYS_DB_PASSWD}" |
|---|
| 60 | break |
|---|
| 61 | fi |
|---|
| 62 | done |
|---|
| 63 | |
|---|
| 64 | # Clave de acceso del cliente |
|---|
| 65 | while : ; do |
|---|
| 66 | echo -n -e "\\nEnter root password for OpenGnsys client (${DEFAULT_OPENGNSYS_CLIENT_PASSWD}): " |
|---|
| 67 | read -r OPENGNSYS_CLIENT_PASSWD |
|---|
| 68 | if [ -n "${OPENGNSYS_CLIENT_PASSWD//[a-zA-Z0-9]/}" ]; then # Comprobamos que sea un valor alfanumerico |
|---|
| 69 | echo -e "\\aERROR: Must be alphanumeric, try again..." |
|---|
| 70 | else |
|---|
| 71 | # Si esta vacio ponemos el valor por defecto |
|---|
| 72 | OPENGNSYS_CLIENT_PASSWD="${OPENGNSYS_CLIENT_PASSWD:-$DEFAULT_OPENGNSYS_CLIENT_PASSWD}" |
|---|
| 73 | break |
|---|
| 74 | fi |
|---|
| 75 | done |
|---|
| 76 | |
|---|
| 77 | # Selección de clientes ogLive para descargar. |
|---|
| 78 | while : ; do |
|---|
| 79 | echo -e "\\nChoose ogLive client to install." |
|---|
| 80 | echo -e "1) Kernel 4.13, 64-bit, EFI-compatible" |
|---|
| 81 | echo -e "2) Kernel 3.2, 32-bit" |
|---|
| 82 | echo -e "3) Both" |
|---|
| 83 | echo -n -e "Please, type a valid number (1): " |
|---|
| 84 | read -r OPT |
|---|
| 85 | case "$OPT" in |
|---|
| 86 | 1|"") OGLIVE="ogLive-xenial-4.13.0-17-generic-amd64-r5520.iso" |
|---|
| 87 | break ;; |
|---|
| 88 | 2) OGLIVE="ogLive-precise-3.2.0-23-generic-r5159.iso" |
|---|
| 89 | break ;; |
|---|
| 90 | 3) OGLIVE="ogLive-xenial-4.13.0-17-generic-amd64-r5520.iso ogLive-precise-3.2.0-23-generic-r5159.iso"; |
|---|
| 91 | break ;; |
|---|
| 92 | *) echo -e "\\aERROR: unknown option, try again." |
|---|
| 93 | esac |
|---|
| 94 | done |
|---|
| 95 | |
|---|
| 96 | echo -e "\\n==============================" |
|---|
| 97 | |
|---|
| 98 | # Comprobar si se ha descargado el paquete comprimido (REMOTE=0) o sólo el instalador (REMOTE=1). |
|---|
| 99 | PROGRAMDIR=$(readlink -e "$(dirname "$0")") |
|---|
| 100 | PROGRAMNAME=$(basename "$0") |
|---|
| 101 | OPENGNSYS_SERVER="opengnsys.es" |
|---|
| 102 | DOWNLOADURL="https://$OPENGNSYS_SERVER/trac/downloads" |
|---|
| 103 | if [ -d "$PROGRAMDIR/../installer" ]; then |
|---|
| 104 | REMOTE=0 |
|---|
| 105 | else |
|---|
| 106 | REMOTE=1 |
|---|
| 107 | fi |
|---|
| 108 | BRANCH="devel" |
|---|
| 109 | CODE_URL="https://codeload.github.com/opengnsys/OpenGnsys/zip/$BRANCH" |
|---|
| 110 | API_URL="https://api.github.com/repos/opengnsys/OpenGnsys/branches/$BRANCH" |
|---|
| 111 | |
|---|
| 112 | WORKDIR=/tmp/opengnsys_installer |
|---|
| 113 | mkdir -p $WORKDIR |
|---|
| 114 | |
|---|
| 115 | # Directorio destino de OpenGnsys. |
|---|
| 116 | INSTALL_TARGET=/opt/opengnsys |
|---|
| 117 | PATH=$PATH:$INSTALL_TARGET/bin |
|---|
| 118 | |
|---|
| 119 | # Registro de incidencias. |
|---|
| 120 | OGLOGFILE=$INSTALL_TARGET/log/${PROGRAMNAME%.sh}.log |
|---|
| 121 | LOG_FILE=/tmp/$(basename $OGLOGFILE) |
|---|
| 122 | |
|---|
| 123 | # Usuario del cliente para acceso remoto. |
|---|
| 124 | OPENGNSYS_CLIENT_USER="opengnsys" |
|---|
| 125 | |
|---|
| 126 | # Nombre de la base datos y fichero SQL para su creación. |
|---|
| 127 | OPENGNSYS_DATABASE="ogAdmBD" |
|---|
| 128 | OPENGNSYS_DB_CREATION_FILE=opengnsys/admin/Database/${OPENGNSYS_DATABASE}.sql |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | ##################################################################### |
|---|
| 132 | ####### Funciones de configuración |
|---|
| 133 | ##################################################################### |
|---|
| 134 | |
|---|
| 135 | # Generar variables de configuración del instalador |
|---|
| 136 | # Variables globales: |
|---|
| 137 | # - OSDISTRIB, OSVERSION - tipo y versión de la distribución GNU/Linux |
|---|
| 138 | # - DEPENDENCIES - array de dependencias que deben estar instaladas |
|---|
| 139 | # - UPDATEPKGLIST, INSTALLPKGS, CHECKPKGS - comandos para gestión de paquetes |
|---|
| 140 | # - INSTALLEXTRADEPS - instalar dependencias no incluidas en la distribución |
|---|
| 141 | # - STARTSERVICE, ENABLESERVICE - iniciar y habilitar un servicio |
|---|
| 142 | # - STOPSERVICE, DISABLESERVICE - parar y deshabilitar un servicio |
|---|
| 143 | # - APACHESERV, APACHECFGDIR, APACHESITESDIR, APACHEUSER, APACHEGROUP - servicio y configuración de Apache |
|---|
| 144 | # - APACHESSLMOD, APACHEENABLESSL, APACHEMAKECERT - habilitar módulo Apache y certificado SSL |
|---|
| 145 | # - APACHEENABLEOG, APACHEOGSITE, - habilitar sitio web de OpenGnsys |
|---|
| 146 | # - INETDSERV - servicio Inetd |
|---|
| 147 | # - FIREWALLSERV - servicio de cortabuegos IPTables/FirewallD |
|---|
| 148 | # - DHCPSERV, DHCPCFGDIR - servicio y configuración de DHCP |
|---|
| 149 | # - MYSQLSERV, TMPMYCNF - servicio MySQL y fichero temporal con credenciales de acceso |
|---|
| 150 | # - MARIADBSERV - servicio MariaDB (sustituto de MySQL en algunas distribuciones) |
|---|
| 151 | # - RSYNCSERV, RSYNCCFGDIR - servicio y configuración de Rsync |
|---|
| 152 | # - SAMBASERV, SAMBACFGDIR - servicio y configuración de Samba |
|---|
| 153 | # - TFTPSERV, TFTPCFGDIR - servicio y configuración de TFTP/PXE |
|---|
| 154 | function autoConfigure() |
|---|
| 155 | { |
|---|
| 156 | # Detectar sistema operativo del servidor (compatible con fichero os-release y con LSB). |
|---|
| 157 | if [ -f /etc/os-release ]; then |
|---|
| 158 | source /etc/os-release |
|---|
| 159 | OSDISTRIB="$ID" |
|---|
| 160 | OSVERSION="$VERSION_ID" |
|---|
| 161 | else |
|---|
| 162 | OSDISTRIB=$(lsb_release -is 2>/dev/null) |
|---|
| 163 | OSVERSION=$(lsb_release -rs 2>/dev/null) |
|---|
| 164 | fi |
|---|
| 165 | # Convertir distribución a minúsculas y obtener solo el 1er número de versión. |
|---|
| 166 | OSDISTRIB="${OSDISTRIB,,}" |
|---|
| 167 | OSVERSION="${OSVERSION%%.*}" |
|---|
| 168 | |
|---|
| 169 | # Configuración según la distribución GNU/Linux (usar minúsculas). |
|---|
| 170 | case "$OSDISTRIB" in |
|---|
| 171 | ubuntu|debian|linuxmint) |
|---|
| 172 | DEPENDENCIES=( apache2 php php-ldap libapache2-mod-php mysql-server php-mysql isc-dhcp-server bittorrent tftp-hpa tftpd-hpa xinetd build-essential g++-multilib libmysqlclient-dev wget curl doxygen graphviz bittornado ctorrent samba rsync unzip netpipes debootstrap schroot squashfs-tools btrfs-tools procps arp-scan realpath php-curl gettext moreutils jq wakeonlan udpcast shim-signed grub-efi-amd64-signed ) |
|---|
| 173 | UPDATEPKGLIST="apt-get update" |
|---|
| 174 | INSTALLPKG="apt-get -y install --force-yes" |
|---|
| 175 | CHECKPKG="dpkg -s \$package 2>/dev/null | grep Status | grep -qw install" |
|---|
| 176 | if which service &>/dev/null; then |
|---|
| 177 | STARTSERVICE="eval service \$service restart" |
|---|
| 178 | STOPSERVICE="eval service \$service stop" |
|---|
| 179 | else |
|---|
| 180 | STARTSERVICE="eval /etc/init.d/\$service restart" |
|---|
| 181 | STOPSERVICE="eval /etc/init.d/\$service stop" |
|---|
| 182 | fi |
|---|
| 183 | ENABLESERVICE="eval update-rc.d \$service defaults" |
|---|
| 184 | DISABLESERVICE="eval update-rc.d \$service disable" |
|---|
| 185 | APACHESERV=apache2 |
|---|
| 186 | APACHECFGDIR=/etc/apache2 |
|---|
| 187 | APACHESITESDIR=sites-available |
|---|
| 188 | APACHEOGSITE=opengnsys |
|---|
| 189 | APACHEUSER="www-data" |
|---|
| 190 | APACHEGROUP="www-data" |
|---|
| 191 | APACHESSLMOD="a2enmod ssl" |
|---|
| 192 | APACHEREWRITEMOD="a2enmod rewrite" |
|---|
| 193 | APACHEENABLESSL="a2ensite default-ssl" |
|---|
| 194 | APACHEENABLEOG="a2ensite $APACHEOGSITE" |
|---|
| 195 | APACHEMAKECERT="make-ssl-cert generate-default-snakeoil --force-overwrite" |
|---|
| 196 | DHCPSERV=isc-dhcp-server |
|---|
| 197 | DHCPCFGDIR=/etc/dhcp |
|---|
| 198 | INETDSERV=xinetd |
|---|
| 199 | INETDCFGDIR=/etc/xinetd.d |
|---|
| 200 | MYSQLSERV=mysql |
|---|
| 201 | MARIADBSERV=mariadb |
|---|
| 202 | RSYNCSERV=rsync |
|---|
| 203 | RSYNCCFGDIR=/etc |
|---|
| 204 | SAMBASERV=smbd |
|---|
| 205 | SAMBACFGDIR=/etc/samba |
|---|
| 206 | TFTPCFGDIR=/var/lib/tftpboot |
|---|
| 207 | ;; |
|---|
| 208 | fedora|centos) |
|---|
| 209 | DEPENDENCIES=( httpd mod_ssl php php-ldap mysql-server mysql-devel mysql-devel.i686 php-mysql 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 http://ftp.altlinux.org/pub/distributions/ALTLinux/5.1/branch/$(arch)/RPMS.classic/netpipes-4.2-alt1.$(arch).rpm ) |
|---|
| 210 | INSTALLEXTRADEPS=( 'pushd /tmp; wget -t3 http://download.bittornado.com/download/BitTornado-0.3.18.tar.gz && tar xvzf BitTornado-0.3.18.tar.gz && cd BitTornado-CVS && python setup.py install && ln -fs btlaunchmany.py /usr/bin/btlaunchmany && ln -fs bttrack.py /usr/bin/bttrack; popd' ) |
|---|
| 211 | INSTALLPKG="yum install -y libstdc++ libstdc++.i686" |
|---|
| 212 | CHECKPKG="rpm -q --quiet \$package" |
|---|
| 213 | SYSTEMD=$(which systemctl 2>/dev/null) |
|---|
| 214 | if [ -n "$SYSTEMD" ]; then |
|---|
| 215 | STARTSERVICE="eval systemctl start \$service.service" |
|---|
| 216 | STOPSERVICE="eval systemctl stop \$service.service" |
|---|
| 217 | ENABLESERVICE="eval systemctl enable \$service.service" |
|---|
| 218 | DISABLESERVICE="eval systemctl disable \$service.service" |
|---|
| 219 | else |
|---|
| 220 | STARTSERVICE="eval service \$service start" |
|---|
| 221 | STOPSERVICE="eval service \$service stop" |
|---|
| 222 | ENABLESERVICE="eval chkconfig \$service on" |
|---|
| 223 | DISABLESERVICE="eval chkconfig \$service off" |
|---|
| 224 | fi |
|---|
| 225 | APACHESERV=httpd |
|---|
| 226 | APACHECFGDIR=/etc/httpd/conf.d |
|---|
| 227 | APACHEOGSITE=opengnsys.conf |
|---|
| 228 | APACHEUSER="apache" |
|---|
| 229 | APACHEGROUP="apache" |
|---|
| 230 | APACHEREWRITEMOD="sed -i '/rewrite/s/^#//' $APACHECFGDIR/../*.conf" |
|---|
| 231 | DHCPSERV=dhcpd |
|---|
| 232 | DHCPCFGDIR=/etc/dhcp |
|---|
| 233 | if firewall-cmd --state &>/dev/null; then |
|---|
| 234 | FIREWALLSERV=firewalld |
|---|
| 235 | else |
|---|
| 236 | FIREWALLSERV=iptables |
|---|
| 237 | fi |
|---|
| 238 | INETDSERV=xinetd |
|---|
| 239 | INETDCFGDIR=/etc/xinetd.d |
|---|
| 240 | MYSQLSERV=mysqld |
|---|
| 241 | MARIADBSERV=mariadb |
|---|
| 242 | RSYNCSERV=rsync |
|---|
| 243 | RSYNCCFGDIR=/etc |
|---|
| 244 | SAMBASERV=smb |
|---|
| 245 | SAMBACFGDIR=/etc/samba |
|---|
| 246 | TFTPSERV=tftp |
|---|
| 247 | TFTPCFGDIR=/var/lib/tftpboot |
|---|
| 248 | ;; |
|---|
| 249 | "") echo "ERROR: Unknown Linux distribution, please install \"lsb_release\" command." |
|---|
| 250 | exit 1 ;; |
|---|
| 251 | *) echo "ERROR: Distribution not supported by OpenGnsys." |
|---|
| 252 | exit 1 ;; |
|---|
| 253 | esac |
|---|
| 254 | |
|---|
| 255 | # Fichero de credenciales de acceso a MySQL. |
|---|
| 256 | TMPMYCNF=/tmp/.my.cnf.$$ |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | # Modificar variables de configuración tras instalar paquetes del sistema. |
|---|
| 261 | function autoConfigurePost() |
|---|
| 262 | { |
|---|
| 263 | local f |
|---|
| 264 | |
|---|
| 265 | # Configuraciones específicas para Samba y TFTP en Debian 6. |
|---|
| 266 | [ -z "$SYSTEMD" -a ! -e /etc/init.d/$SAMBASERV ] && SAMBASERV=samba |
|---|
| 267 | [ ! -e $TFTPCFGDIR ] && TFTPCFGDIR=/srv/tftp |
|---|
| 268 | |
|---|
| 269 | # Configuraciones específicas para SELinux permisivo en distintas versiones. |
|---|
| 270 | [ -f /selinux/enforce ] && echo 0 > /selinux/enforce |
|---|
| 271 | for f in /etc/sysconfig/selinux /etc/selinux/config; do |
|---|
| 272 | [ -f $f ] && perl -pi -e 's/SELINUX=enforcing/SELINUX=permissive/g' $f |
|---|
| 273 | done |
|---|
| 274 | selinuxenabled 2>/dev/null && setenforce 0 2>/dev/null |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | # Cargar lista de paquetes del sistema y actualizar algunas variables de configuración |
|---|
| 279 | # dependiendo de la versión instalada. |
|---|
| 280 | function updatePackageList() |
|---|
| 281 | { |
|---|
| 282 | local DHCPVERSION PHP7VERSION |
|---|
| 283 | |
|---|
| 284 | # Si es necesario, actualizar la lista de paquetes disponibles. |
|---|
| 285 | [ -n "$UPDATEPKGLIST" ] && eval $UPDATEPKGLIST |
|---|
| 286 | |
|---|
| 287 | # Configuración personallizada de algunos paquetes. |
|---|
| 288 | case "$OSDISTRIB" in |
|---|
| 289 | ubuntu|linuxmint) # Postconfiguación personalizada para Ubuntu. |
|---|
| 290 | # Configuración para DHCP v3. |
|---|
| 291 | DHCPVERSION=$(apt-cache show $(apt-cache pkgnames|egrep "dhcp.?-server$") | \ |
|---|
| 292 | awk '/Version/ {print substr($2,1,1);}' | \ |
|---|
| 293 | sort -n | tail -1) |
|---|
| 294 | if [ $DHCPVERSION = 3 ]; then |
|---|
| 295 | DEPENDENCIES=( ${DEPENDENCIES[@]/isc-dhcp-server/dhcp3-server} ) |
|---|
| 296 | DHCPSERV=dhcp3-server |
|---|
| 297 | DHCPCFGDIR=/etc/dhcp3 |
|---|
| 298 | fi |
|---|
| 299 | # Configuración para PHP 7 en Ubuntu. |
|---|
| 300 | if [ -z "$(apt-cache pkgnames php7)" ]; then |
|---|
| 301 | eval $INSTALLPKG software-properties-common |
|---|
| 302 | add-apt-repository -y ppa:ondrej/php |
|---|
| 303 | eval $UPDATEPKGLIST |
|---|
| 304 | PHP7VERSION=$(apt-cache pkgnames php7 | sort | head -1) |
|---|
| 305 | DEPENDENCIES=( ${DEPENDENCIES[@]//php/$PHP7VERSION} ) |
|---|
| 306 | fi |
|---|
| 307 | # Adaptar dependencias para libmysqlclient. |
|---|
| 308 | [ -z "$(apt-cache pkgnames libmysqlclient-dev)" ] && [ -n "$(apt-cache pkgnames libmysqlclient15)" ] && DEPENDENCIES=( ${DEPENDENCIES[@]//libmysqlclient-dev/libmysqlclient15} ) |
|---|
| 309 | ;; |
|---|
| 310 | centos) # Postconfiguación personalizada para CentOS. |
|---|
| 311 | # Configuración para PHP 7. |
|---|
| 312 | if ! yum list php7 &>/dev/null; then |
|---|
| 313 | if [ $OSVERSION -lt 7 ]; then |
|---|
| 314 | yum install -y https://mirror.webtatic.com/yum/el$OSVERSION/latest.rpm |
|---|
| 315 | PHP7VERSION=$(yum list -q php7\*w | awk -F. '/^php/ {p=$1} END {print p}') |
|---|
| 316 | DEPENDENCIES=( ${DEPENDENCIES[@]//php/$PHP5VERSION} ) |
|---|
| 317 | fi |
|---|
| 318 | fi |
|---|
| 319 | # Cambios a aplicar a partir de CentOS 7. |
|---|
| 320 | if [ $OSVERSION -ge 7 ]; then |
|---|
| 321 | # Sustituir MySQL por MariaDB. |
|---|
| 322 | DEPENDENCIES=( ${DEPENDENCIES[*]/mysql-/mariadb-} ) |
|---|
| 323 | # Instalar ctorrent de EPEL para CentOS 6 (no disponible en CentOS 7). |
|---|
| 324 | DEPENDENCIES=( ${DEPENDENCIES[*]/ctorrent/http://dl.fedoraproject.org/pub/epel/6/$(arch)/Packages/c/ctorrent-1.3.4-14.dnh3.3.2.el6.$(arch).rpm} ) |
|---|
| 325 | fi |
|---|
| 326 | ;; |
|---|
| 327 | fedora) # Postconfiguación personalizada para Fedora. |
|---|
| 328 | # Incluir paquetes específicos. |
|---|
| 329 | DEPENDENCIES=( ${DEPENDENCIES[@]} btrfs-progs ) |
|---|
| 330 | # Sustituir MySQL por MariaDB a partir de Fedora 20. |
|---|
| 331 | [ $OSVERSION -ge 20 ] && DEPENDENCIES=( ${DEPENDENCIES[*]/mysql-/mariadb-} ) |
|---|
| 332 | ;; |
|---|
| 333 | esac |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | ##################################################################### |
|---|
| 338 | ####### Algunas funciones útiles de propósito general: |
|---|
| 339 | ##################################################################### |
|---|
| 340 | |
|---|
| 341 | function getDateTime() |
|---|
| 342 | { |
|---|
| 343 | date "+%Y%m%d-%H%M%S" |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | # Escribe a fichero y muestra por pantalla |
|---|
| 347 | function echoAndLog() |
|---|
| 348 | { |
|---|
| 349 | local DATETIME=`getDateTime` |
|---|
| 350 | echo "$1" |
|---|
| 351 | echo "$DATETIME;$SSH_CLIENT;$1" >> $LOG_FILE |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | # Escribe a fichero y muestra mensaje de error |
|---|
| 355 | function errorAndLog() |
|---|
| 356 | { |
|---|
| 357 | local DATETIME=`getDateTime` |
|---|
| 358 | echo "ERROR: $1" |
|---|
| 359 | echo "$DATETIME;$SSH_CLIENT;ERROR: $1" >> $LOG_FILE |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | # Escribe a fichero y muestra mensaje de aviso |
|---|
| 363 | function warningAndLog() |
|---|
| 364 | { |
|---|
| 365 | local DATETIME=`getDateTime` |
|---|
| 366 | echo "Warning: $1" |
|---|
| 367 | echo "$DATETIME;$SSH_CLIENT;Warning: $1" >> $LOG_FILE |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | # Comprueba si el elemento pasado en $2 está en el array $1 |
|---|
| 371 | function isInArray() |
|---|
| 372 | { |
|---|
| 373 | if [ $# -ne 2 ]; then |
|---|
| 374 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 375 | exit 1 |
|---|
| 376 | fi |
|---|
| 377 | |
|---|
| 378 | local deps |
|---|
| 379 | local is_in_array=1 |
|---|
| 380 | local element="$2" |
|---|
| 381 | |
|---|
| 382 | echoAndLog "${FUNCNAME}(): checking if $2 is in $1" |
|---|
| 383 | eval "deps=( \"\${$1[@]}\" )" |
|---|
| 384 | |
|---|
| 385 | # Copia local del array del parámetro 1. |
|---|
| 386 | for (( i = 0 ; i < ${#deps[@]} ; i++ )); do |
|---|
| 387 | if [ "${deps[$i]}" = "${element}" ]; then |
|---|
| 388 | echoAndLog "isInArray(): $element found in array" |
|---|
| 389 | is_in_array=0 |
|---|
| 390 | fi |
|---|
| 391 | done |
|---|
| 392 | |
|---|
| 393 | if [ $is_in_array -ne 0 ]; then |
|---|
| 394 | echoAndLog "${FUNCNAME}(): $element NOT found in array" |
|---|
| 395 | fi |
|---|
| 396 | |
|---|
| 397 | return $is_in_array |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | ##################################################################### |
|---|
| 402 | ####### Funciones de manejo de paquetes Debian |
|---|
| 403 | ##################################################################### |
|---|
| 404 | |
|---|
| 405 | function checkPackage() |
|---|
| 406 | { |
|---|
| 407 | package=$1 |
|---|
| 408 | if [ -z $package ]; then |
|---|
| 409 | errorAndLog "${FUNCNAME}(): parameter required" |
|---|
| 410 | exit 1 |
|---|
| 411 | fi |
|---|
| 412 | echoAndLog "${FUNCNAME}(): checking if package $package exists" |
|---|
| 413 | eval $CHECKPKG |
|---|
| 414 | if [ $? -eq 0 ]; then |
|---|
| 415 | echoAndLog "${FUNCNAME}(): package $package exists" |
|---|
| 416 | return 0 |
|---|
| 417 | else |
|---|
| 418 | echoAndLog "${FUNCNAME}(): package $package doesn't exists" |
|---|
| 419 | return 1 |
|---|
| 420 | fi |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | # Recibe array con dependencias |
|---|
| 424 | # por referencia deja un array con las dependencias no resueltas |
|---|
| 425 | # devuelve 1 si hay alguna dependencia no resuelta |
|---|
| 426 | function checkDependencies() |
|---|
| 427 | { |
|---|
| 428 | if [ $# -ne 2 ]; then |
|---|
| 429 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 430 | exit 1 |
|---|
| 431 | fi |
|---|
| 432 | |
|---|
| 433 | echoAndLog "${FUNCNAME}(): checking dependences" |
|---|
| 434 | uncompletedeps=0 |
|---|
| 435 | |
|---|
| 436 | # copia local del array del parametro 1 |
|---|
| 437 | local deps |
|---|
| 438 | eval "deps=( \"\${$1[@]}\" )" |
|---|
| 439 | |
|---|
| 440 | declare -a local_notinstalled |
|---|
| 441 | |
|---|
| 442 | for (( i = 0 ; i < ${#deps[@]} ; i++ )) |
|---|
| 443 | do |
|---|
| 444 | checkPackage ${deps[$i]} |
|---|
| 445 | if [ $? -ne 0 ]; then |
|---|
| 446 | local_notinstalled[$uncompletedeps]=$package |
|---|
| 447 | let uncompletedeps=uncompletedeps+1 |
|---|
| 448 | fi |
|---|
| 449 | done |
|---|
| 450 | |
|---|
| 451 | # relleno el array especificado en $2 por referencia |
|---|
| 452 | for (( i = 0 ; i < ${#local_notinstalled[@]} ; i++ )) |
|---|
| 453 | do |
|---|
| 454 | eval "${2}[$i]=${local_notinstalled[$i]}" |
|---|
| 455 | done |
|---|
| 456 | |
|---|
| 457 | # retorna el numero de paquetes no resueltos |
|---|
| 458 | echoAndLog "${FUNCNAME}(): dependencies uncompleted: $uncompletedeps" |
|---|
| 459 | return $uncompletedeps |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | # Recibe un array con las dependencias y lo instala |
|---|
| 463 | function installDependencies() |
|---|
| 464 | { |
|---|
| 465 | if [ $# -ne 1 ]; then |
|---|
| 466 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 467 | exit 1 |
|---|
| 468 | fi |
|---|
| 469 | echoAndLog "${FUNCNAME}(): installing uncompleted dependencies" |
|---|
| 470 | |
|---|
| 471 | # copia local del array del parametro 1 |
|---|
| 472 | local deps |
|---|
| 473 | eval "deps=( \"\${$1[@]}\" )" |
|---|
| 474 | |
|---|
| 475 | local string_deps="" |
|---|
| 476 | for (( i = 0 ; i < ${#deps[@]} ; i++ )) |
|---|
| 477 | do |
|---|
| 478 | string_deps="$string_deps ${deps[$i]}" |
|---|
| 479 | done |
|---|
| 480 | |
|---|
| 481 | if [ -z "${string_deps}" ]; then |
|---|
| 482 | errorAndLog "${FUNCNAME}(): array of dependeces is empty" |
|---|
| 483 | exit 1 |
|---|
| 484 | fi |
|---|
| 485 | |
|---|
| 486 | OLD_DEBIAN_FRONTEND=$DEBIAN_FRONTEND # Debian/Ubuntu |
|---|
| 487 | export DEBIAN_FRONTEND=noninteractive |
|---|
| 488 | |
|---|
| 489 | echoAndLog "${FUNCNAME}(): now $string_deps will be installed" |
|---|
| 490 | eval $INSTALLPKG $string_deps |
|---|
| 491 | if [ $? -ne 0 ]; then |
|---|
| 492 | errorAndLog "${FUNCNAME}(): error installing dependencies" |
|---|
| 493 | return 1 |
|---|
| 494 | fi |
|---|
| 495 | |
|---|
| 496 | DEBIAN_FRONTEND=$OLD_DEBIAN_FRONTEND # Debian/Ubuntu |
|---|
| 497 | test grep -q "EPEL temporal" /etc/yum.repos.d/epel.repo 2>/dev/null || mv -f /etc/yum.repos.d/epel.repo.rpmnew /etc/yum.repos.d/epel.repo 2>/dev/null # CentOS/RedHat EPEL |
|---|
| 498 | |
|---|
| 499 | echoAndLog "${FUNCNAME}(): dependencies installed" |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | # Hace un backup del fichero pasado por parámetro |
|---|
| 503 | # deja un -last y uno para el día |
|---|
| 504 | function backupFile() |
|---|
| 505 | { |
|---|
| 506 | if [ $# -ne 1 ]; then |
|---|
| 507 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 508 | exit 1 |
|---|
| 509 | fi |
|---|
| 510 | |
|---|
| 511 | local file="$1" |
|---|
| 512 | local dateymd=`date +%Y%m%d` |
|---|
| 513 | |
|---|
| 514 | if [ ! -f "$file" ]; then |
|---|
| 515 | warningAndLog "${FUNCNAME}(): file $file doesn't exists" |
|---|
| 516 | return 1 |
|---|
| 517 | fi |
|---|
| 518 | |
|---|
| 519 | echoAndLog "${FUNCNAME}(): making $file backup" |
|---|
| 520 | |
|---|
| 521 | # realiza una copia de la última configuración como last |
|---|
| 522 | cp -a "$file" "${file}-LAST" |
|---|
| 523 | |
|---|
| 524 | # si para el día no hay backup lo hace, sino no |
|---|
| 525 | if [ ! -f "${file}-${dateymd}" ]; then |
|---|
| 526 | cp -a "$file" "${file}-${dateymd}" |
|---|
| 527 | fi |
|---|
| 528 | |
|---|
| 529 | echoAndLog "${FUNCNAME}(): $file backup success" |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | ##################################################################### |
|---|
| 533 | ####### Funciones para el manejo de bases de datos |
|---|
| 534 | ##################################################################### |
|---|
| 535 | |
|---|
| 536 | # This function set password to root |
|---|
| 537 | function mysqlSetRootPassword() |
|---|
| 538 | { |
|---|
| 539 | if [ $# -ne 1 ]; then |
|---|
| 540 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 541 | exit 1 |
|---|
| 542 | fi |
|---|
| 543 | |
|---|
| 544 | local root_mysql="$1" |
|---|
| 545 | echoAndLog "${FUNCNAME}(): setting root password in MySQL server" |
|---|
| 546 | mysqladmin -u root password "$root_mysql" |
|---|
| 547 | if [ $? -ne 0 ]; then |
|---|
| 548 | errorAndLog "${FUNCNAME}(): error while setting root password in MySQL server" |
|---|
| 549 | return 1 |
|---|
| 550 | fi |
|---|
| 551 | echoAndLog "${FUNCNAME}(): root password saved!" |
|---|
| 552 | return 0 |
|---|
| 553 | } |
|---|
| 554 | |
|---|
| 555 | # Si el servicio mysql esta ya instalado cambia la variable de la clave del root por la ya existente |
|---|
| 556 | function mysqlGetRootPassword() |
|---|
| 557 | { |
|---|
| 558 | local pass_mysql |
|---|
| 559 | local pass_mysql2 |
|---|
| 560 | # Comprobar si MySQL está instalado con la clave de root por defecto. |
|---|
| 561 | if mysql -u root -p"$MYSQL_ROOT_PASSWORD" <<<"quit" 2>/dev/null; then |
|---|
| 562 | echoAndLog "${FUNCNAME}(): Using default mysql root password." |
|---|
| 563 | else |
|---|
| 564 | stty -echo |
|---|
| 565 | echo "There is a MySQL service already installed." |
|---|
| 566 | read -p "Enter MySQL root password: " pass_mysql |
|---|
| 567 | echo "" |
|---|
| 568 | read -p "Confrim password:" pass_mysql2 |
|---|
| 569 | echo "" |
|---|
| 570 | stty echo |
|---|
| 571 | if [ "$pass_mysql" == "$pass_mysql2" ] ;then |
|---|
| 572 | MYSQL_ROOT_PASSWORD="$pass_mysql" |
|---|
| 573 | return 0 |
|---|
| 574 | else |
|---|
| 575 | echo "The keys don't match. Do not configure the server's key," |
|---|
| 576 | echo "transactions in the database will give error." |
|---|
| 577 | return 1 |
|---|
| 578 | fi |
|---|
| 579 | fi |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | # comprueba si puede conectar con mysql con el usuario root |
|---|
| 583 | function mysqlTestConnection() |
|---|
| 584 | { |
|---|
| 585 | if [ $# -ne 1 ]; then |
|---|
| 586 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 587 | exit 1 |
|---|
| 588 | fi |
|---|
| 589 | |
|---|
| 590 | local root_password="$1" |
|---|
| 591 | echoAndLog "${FUNCNAME}(): checking connection to mysql..." |
|---|
| 592 | # Componer fichero con credenciales de conexión a MySQL. |
|---|
| 593 | touch $TMPMYCNF |
|---|
| 594 | chmod 600 $TMPMYCNF |
|---|
| 595 | cat << EOT > $TMPMYCNF |
|---|
| 596 | [client] |
|---|
| 597 | user=root |
|---|
| 598 | password=$root_password |
|---|
| 599 | EOT |
|---|
| 600 | # Borrar el fichero temporal si termina el proceso de instalación. |
|---|
| 601 | trap "rm -f $TMPMYCNF" 0 1 2 3 6 9 15 |
|---|
| 602 | # Comprobar conexión a MySQL. |
|---|
| 603 | echo "" | mysql --defaults-extra-file=$TMPMYCNF |
|---|
| 604 | if [ $? -ne 0 ]; then |
|---|
| 605 | errorAndLog "${FUNCNAME}(): connection to mysql failed, check root password and if daemon is running!" |
|---|
| 606 | return 1 |
|---|
| 607 | else |
|---|
| 608 | echoAndLog "${FUNCNAME}(): connection success" |
|---|
| 609 | return 0 |
|---|
| 610 | fi |
|---|
| 611 | } |
|---|
| 612 | |
|---|
| 613 | # comprueba si la base de datos existe |
|---|
| 614 | function mysqlDbExists() |
|---|
| 615 | { |
|---|
| 616 | if [ $# -ne 1 ]; then |
|---|
| 617 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 618 | exit 1 |
|---|
| 619 | fi |
|---|
| 620 | |
|---|
| 621 | local database="$1" |
|---|
| 622 | echoAndLog "${FUNCNAME}(): checking if $database exists..." |
|---|
| 623 | echo "show databases" | mysql --defaults-extra-file=$TMPMYCNF | grep "^${database}$" |
|---|
| 624 | if [ $? -ne 0 ]; then |
|---|
| 625 | echoAndLog "${FUNCNAME}():database $database doesn't exists" |
|---|
| 626 | return 1 |
|---|
| 627 | else |
|---|
| 628 | echoAndLog "${FUNCNAME}():database $database exists" |
|---|
| 629 | return 0 |
|---|
| 630 | fi |
|---|
| 631 | } |
|---|
| 632 | |
|---|
| 633 | # Comprueba si la base de datos está vacía. |
|---|
| 634 | function mysqlCheckDbIsEmpty() |
|---|
| 635 | { |
|---|
| 636 | if [ $# -ne 1 ]; then |
|---|
| 637 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 638 | exit 1 |
|---|
| 639 | fi |
|---|
| 640 | |
|---|
| 641 | local database="$1" |
|---|
| 642 | echoAndLog "${FUNCNAME}(): checking if $database is empty..." |
|---|
| 643 | num_tablas=`echo "show tables" | mysql --defaults-extra-file=$TMPMYCNF "${database}" | wc -l` |
|---|
| 644 | if [ $? -ne 0 ]; then |
|---|
| 645 | errorAndLog "${FUNCNAME}(): error executing query, check database and root password" |
|---|
| 646 | exit 1 |
|---|
| 647 | fi |
|---|
| 648 | |
|---|
| 649 | if [ $num_tablas -eq 0 ]; then |
|---|
| 650 | echoAndLog "${FUNCNAME}():database $database is empty" |
|---|
| 651 | return 0 |
|---|
| 652 | else |
|---|
| 653 | echoAndLog "${FUNCNAME}():database $database has tables" |
|---|
| 654 | return 1 |
|---|
| 655 | fi |
|---|
| 656 | |
|---|
| 657 | } |
|---|
| 658 | |
|---|
| 659 | # Importa un fichero SQL en la base de datos. |
|---|
| 660 | # Parámetros: |
|---|
| 661 | # - 1: nombre de la BD. |
|---|
| 662 | # - 2: fichero a importar. |
|---|
| 663 | # Nota: el fichero SQL puede contener las siguientes palabras reservadas: |
|---|
| 664 | # - SERVERIP: se sustituye por la dirección IP del servidor. |
|---|
| 665 | # - DBUSER: se sustituye por usuario de conexión a la BD definido en este script. |
|---|
| 666 | # - DBPASSWD: se sustituye por la clave de conexión a la BD definida en este script. |
|---|
| 667 | function mysqlImportSqlFileToDb() |
|---|
| 668 | { |
|---|
| 669 | if [ $# -ne 2 ]; then |
|---|
| 670 | errorAndLog "${FNCNAME}(): invalid number of parameters" |
|---|
| 671 | exit 1 |
|---|
| 672 | fi |
|---|
| 673 | |
|---|
| 674 | local database="$1" |
|---|
| 675 | local sqlfile="$2" |
|---|
| 676 | local tmpfile=$(mktemp) |
|---|
| 677 | local i=0 |
|---|
| 678 | local dev="" |
|---|
| 679 | local status |
|---|
| 680 | # Claves aleatorias para acceso a las APIs REST. |
|---|
| 681 | local OPENGNSYS_APIKEY=$(php -r 'echo md5(uniqid(rand(), true));') |
|---|
| 682 | OPENGNSYS_REPOKEY=$(php -r 'echo md5(uniqid(rand(), true));') |
|---|
| 683 | |
|---|
| 684 | if [ ! -f $sqlfile ]; then |
|---|
| 685 | errorAndLog "${FUNCNAME}(): Unable to locate $sqlfile!!" |
|---|
| 686 | return 1 |
|---|
| 687 | fi |
|---|
| 688 | |
|---|
| 689 | echoAndLog "${FUNCNAME}(): importing SQL file to ${database}..." |
|---|
| 690 | chmod 600 $tmpfile |
|---|
| 691 | for dev in ${DEVICE[*]}; do |
|---|
| 692 | if [ "${DEVICE[i]}" == "$DEFAULTDEV" ]; then |
|---|
| 693 | sed -e "s/SERVERIP/${SERVERIP[i]}/g" \ |
|---|
| 694 | -e "s/DBUSER/$OPENGNSYS_DB_USER/g" \ |
|---|
| 695 | -e "s/DBPASSWORD/$OPENGNSYS_DB_PASSWD/g" \ |
|---|
| 696 | -e "s/APIKEY/$OPENGNSYS_APIKEY/g" \ |
|---|
| 697 | -e "s/REPOKEY/$OPENGNSYS_REPOKEY/g" \ |
|---|
| 698 | $sqlfile > $tmpfile |
|---|
| 699 | fi |
|---|
| 700 | let i++ |
|---|
| 701 | done |
|---|
| 702 | mysql --defaults-extra-file=$TMPMYCNF --default-character-set=utf8 "${database}" < $tmpfile |
|---|
| 703 | status=$? |
|---|
| 704 | rm -f $tmpfile |
|---|
| 705 | if [ $status -ne 0 ]; then |
|---|
| 706 | errorAndLog "${FUNCNAME}(): error while importing $sqlfile in database $database" |
|---|
| 707 | return 1 |
|---|
| 708 | fi |
|---|
| 709 | echoAndLog "${FUNCNAME}(): file imported to database $database" |
|---|
| 710 | return 0 |
|---|
| 711 | } |
|---|
| 712 | |
|---|
| 713 | # Crea la base de datos |
|---|
| 714 | function mysqlCreateDb() |
|---|
| 715 | { |
|---|
| 716 | if [ $# -ne 1 ]; then |
|---|
| 717 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 718 | exit 1 |
|---|
| 719 | fi |
|---|
| 720 | |
|---|
| 721 | local database="$1" |
|---|
| 722 | |
|---|
| 723 | echoAndLog "${FUNCNAME}(): creating database..." |
|---|
| 724 | mysqladmin --defaults-extra-file=$TMPMYCNF create $database |
|---|
| 725 | if [ $? -ne 0 ]; then |
|---|
| 726 | errorAndLog "${FUNCNAME}(): error while creating database $database" |
|---|
| 727 | return 1 |
|---|
| 728 | fi |
|---|
| 729 | # Quitar modo ONLY_FULL_GROUP_BY de MySQL (ticket #730). |
|---|
| 730 | mysql --defaults-extra-file=$TMPMYCNF -e "SET GLOBAL sql_mode=(SELECT TRIM(BOTH ',' FROM REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','')));" |
|---|
| 731 | |
|---|
| 732 | echoAndLog "${FUNCNAME}(): database $database created" |
|---|
| 733 | return 0 |
|---|
| 734 | } |
|---|
| 735 | |
|---|
| 736 | # Comprueba si ya está definido el usuario de acceso a la BD. |
|---|
| 737 | function mysqlCheckUserExists() |
|---|
| 738 | { |
|---|
| 739 | if [ $# -ne 1 ]; then |
|---|
| 740 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 741 | exit 1 |
|---|
| 742 | fi |
|---|
| 743 | |
|---|
| 744 | local userdb="$1" |
|---|
| 745 | |
|---|
| 746 | echoAndLog "${FUNCNAME}(): checking if $userdb exists..." |
|---|
| 747 | echo "select user from user where user='${userdb}'\\G" |mysql --defaults-extra-file=$TMPMYCNF mysql | grep user |
|---|
| 748 | if [ $? -ne 0 ]; then |
|---|
| 749 | echoAndLog "${FUNCNAME}(): user doesn't exists" |
|---|
| 750 | return 1 |
|---|
| 751 | else |
|---|
| 752 | echoAndLog "${FUNCNAME}(): user already exists" |
|---|
| 753 | return 0 |
|---|
| 754 | fi |
|---|
| 755 | |
|---|
| 756 | } |
|---|
| 757 | |
|---|
| 758 | # Crea un usuario administrativo para la base de datos |
|---|
| 759 | function mysqlCreateAdminUserToDb() |
|---|
| 760 | { |
|---|
| 761 | if [ $# -ne 3 ]; then |
|---|
| 762 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 763 | exit 1 |
|---|
| 764 | fi |
|---|
| 765 | |
|---|
| 766 | local database="$1" |
|---|
| 767 | local userdb="$2" |
|---|
| 768 | local passdb="$3" |
|---|
| 769 | |
|---|
| 770 | echoAndLog "${FUNCNAME}(): creating admin user ${userdb} to database ${database}" |
|---|
| 771 | |
|---|
| 772 | cat > $WORKDIR/create_${database}.sql <<EOF |
|---|
| 773 | GRANT USAGE ON *.* TO '${userdb}'@'localhost' IDENTIFIED BY '${passdb}' ; |
|---|
| 774 | GRANT ALL PRIVILEGES ON ${database}.* TO '${userdb}'@'localhost' WITH GRANT OPTION ; |
|---|
| 775 | FLUSH PRIVILEGES ; |
|---|
| 776 | EOF |
|---|
| 777 | mysql --defaults-extra-file=$TMPMYCNF < $WORKDIR/create_${database}.sql |
|---|
| 778 | if [ $? -ne 0 ]; then |
|---|
| 779 | errorAndLog "${FUNCNAME}(): error while creating user in mysql" |
|---|
| 780 | rm -f $WORKDIR/create_${database}.sql |
|---|
| 781 | return 1 |
|---|
| 782 | else |
|---|
| 783 | echoAndLog "${FUNCNAME}(): user created ok" |
|---|
| 784 | rm -f $WORKDIR/create_${database}.sql |
|---|
| 785 | return 0 |
|---|
| 786 | fi |
|---|
| 787 | } |
|---|
| 788 | |
|---|
| 789 | |
|---|
| 790 | ##################################################################### |
|---|
| 791 | ####### Funciones para la descarga de código |
|---|
| 792 | ##################################################################### |
|---|
| 793 | |
|---|
| 794 | # Obtiene el código fuente del proyecto desde el repositorio de GitHub. |
|---|
| 795 | function downloadCode() |
|---|
| 796 | { |
|---|
| 797 | if [ $# -ne 1 ]; then |
|---|
| 798 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 799 | exit 1 |
|---|
| 800 | fi |
|---|
| 801 | |
|---|
| 802 | local url="$1" |
|---|
| 803 | |
|---|
| 804 | echoAndLog "${FUNCNAME}(): downloading code..." |
|---|
| 805 | |
|---|
| 806 | curl "${url}" -o opengnsys.zip && unzip opengnsys.zip && mv "OpenGnsys-$BRANCH" opengnsys |
|---|
| 807 | if [ $? -ne 0 ]; then |
|---|
| 808 | errorAndLog "${FUNCNAME}(): error getting OpenGnsys code from $url" |
|---|
| 809 | return 1 |
|---|
| 810 | fi |
|---|
| 811 | rm -f opengnsys.zip |
|---|
| 812 | echoAndLog "${FUNCNAME}(): code was downloaded" |
|---|
| 813 | return 0 |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | |
|---|
| 817 | ############################################################ |
|---|
| 818 | ### Detectar red |
|---|
| 819 | ############################################################ |
|---|
| 820 | |
|---|
| 821 | # Comprobar si existe conexión. |
|---|
| 822 | function checkNetworkConnection() |
|---|
| 823 | { |
|---|
| 824 | echoAndLog "${FUNCNAME}(): Disabling Firewall: $FIREWALLSERV." |
|---|
| 825 | if [ -n "$FIREWALLSERV" ]; then |
|---|
| 826 | service=$FIREWALLSERV |
|---|
| 827 | $STOPSERVICE; $DISABLESERVICE |
|---|
| 828 | fi |
|---|
| 829 | |
|---|
| 830 | echoAndLog "${FUNCNAME}(): Checking OpenGnsys server conectivity." |
|---|
| 831 | OPENGNSYS_SERVER=${OPENGNSYS_SERVER:-"opengnsys.es"} |
|---|
| 832 | if which wget &>/dev/null; then |
|---|
| 833 | wget --spider -q $OPENGNSYS_SERVER |
|---|
| 834 | elif which curl &>/dev/null; then |
|---|
| 835 | curl --connect-timeout 10 -s $OPENGNSYS_SERVER -o /dev/null |
|---|
| 836 | else |
|---|
| 837 | echoAndLog "${FUNCNAME}(): Cannot execute \"wget\" nor \"curl\"." |
|---|
| 838 | return 1 |
|---|
| 839 | fi |
|---|
| 840 | } |
|---|
| 841 | |
|---|
| 842 | # Convierte nº de bits (notación CIDR) en máscara de red (gracias a FriedZombie en openwrt.org). |
|---|
| 843 | cidr2mask () |
|---|
| 844 | { |
|---|
| 845 | # Number of args to shift, 255..255, first non-255 byte, zeroes |
|---|
| 846 | set -- $[ 5 - ($1 / 8) ] 255 255 255 255 $[ (255 << (8 - ($1 % 8))) & 255 ] 0 0 0 |
|---|
| 847 | [ $1 -gt 1 ] && shift $1 || shift |
|---|
| 848 | echo ${1-0}.${2-0}.${3-0}.${4-0} |
|---|
| 849 | } |
|---|
| 850 | |
|---|
| 851 | # Obtener los parámetros de red de la interfaz por defecto. |
|---|
| 852 | function getNetworkSettings() |
|---|
| 853 | { |
|---|
| 854 | # Arrays globales definidas: |
|---|
| 855 | # - DEVICE: nombres de dispositivos de red activos. |
|---|
| 856 | # - SERVERIP: IPs locales del servidor. |
|---|
| 857 | # - NETIP: IPs de redes. |
|---|
| 858 | # - NETMASK: máscaras de red. |
|---|
| 859 | # - NETBROAD: IPs de difusión de redes. |
|---|
| 860 | # - ROUTERIP: IPs de routers. |
|---|
| 861 | # Otras variables globales: |
|---|
| 862 | # - DEFAULTDEV: dispositivo de red por defecto. |
|---|
| 863 | # - DNSIP: IP del servidor DNS principal. |
|---|
| 864 | |
|---|
| 865 | local i=0 |
|---|
| 866 | local dev="" |
|---|
| 867 | |
|---|
| 868 | echoAndLog "${FUNCNAME}(): Detecting network parameters." |
|---|
| 869 | DEVICE=( $(ip -o link show up | awk '!/loopback/ {sub(/:.*/,"",$2); print $2}') ) |
|---|
| 870 | if [ -z "$DEVICE" ]; then |
|---|
| 871 | errorAndLog "${FUNCNAME}(): Network devices not detected." |
|---|
| 872 | exit 1 |
|---|
| 873 | fi |
|---|
| 874 | for dev in ${DEVICE[*]}; do |
|---|
| 875 | SERVERIP[i]=$(ip -o addr show dev "$dev" | awk '$3~/inet$/ {sub (/\/.*/, ""); print ($4)}') |
|---|
| 876 | if [ -n "${SERVERIP[i]}" ]; then |
|---|
| 877 | NETMASK[i]=$( cidr2mask $(ip -o addr show dev "$dev" | awk '$3~/inet$/ {sub (/.*\//, "", $4); print ($4)}') ) |
|---|
| 878 | NETBROAD[i]=$(ip -o addr show dev "$dev" | awk '$3~/inet$/ {print ($6)}') |
|---|
| 879 | NETIP[i]=$(ip route | awk -v d="$dev" '$3==d && /src/ {sub (/\/.*/,""); print $1}') |
|---|
| 880 | ROUTERIP[i]=$(ip route | awk -v d="$dev" '$1=="default" && $5==d {print $3}') |
|---|
| 881 | DEFAULTDEV=${DEFAULTDEV:-"$dev"} |
|---|
| 882 | fi |
|---|
| 883 | let i++ |
|---|
| 884 | done |
|---|
| 885 | DNSIP=$(awk '/nameserver/ {print $2}' /etc/resolv.conf | head -n1) |
|---|
| 886 | if [ -z "${NETIP[*]}" -o -z "${NETMASK[*]}" ]; then |
|---|
| 887 | errorAndLog "${FUNCNAME}(): Network not detected." |
|---|
| 888 | exit 1 |
|---|
| 889 | fi |
|---|
| 890 | |
|---|
| 891 | # Variables de ejecución de Apache |
|---|
| 892 | # - APACHE_RUN_USER |
|---|
| 893 | # - APACHE_RUN_GROUP |
|---|
| 894 | if [ -f $APACHECFGDIR/envvars ]; then |
|---|
| 895 | source $APACHECFGDIR/envvars |
|---|
| 896 | fi |
|---|
| 897 | APACHE_RUN_USER=${APACHE_RUN_USER:-"$APACHEUSER"} |
|---|
| 898 | APACHE_RUN_GROUP=${APACHE_RUN_GROUP:-"$APACHEGROUP"} |
|---|
| 899 | |
|---|
| 900 | echoAndLog "${FUNCNAME}(): Default network device: $DEFAULTDEV." |
|---|
| 901 | } |
|---|
| 902 | |
|---|
| 903 | |
|---|
| 904 | ############################################################ |
|---|
| 905 | ### Esqueleto para el Servicio pxe y contenedor tftpboot ### |
|---|
| 906 | ############################################################ |
|---|
| 907 | |
|---|
| 908 | function tftpConfigure() |
|---|
| 909 | { |
|---|
| 910 | echoAndLog "${FUNCNAME}(): Configuring TFTP service." |
|---|
| 911 | # Habilitar TFTP y reiniciar Inetd. |
|---|
| 912 | if [ -n "$TFTPSERV" ]; then |
|---|
| 913 | if [ -f $INETDCFGDIR/$TFTPSERV ]; then |
|---|
| 914 | perl -pi -e 's/disable.*/disable = no/' $INETDCFGDIR/$TFTPSERV |
|---|
| 915 | else |
|---|
| 916 | service=$TFTPSERV |
|---|
| 917 | $ENABLESERVICE; $STARTSERVICE |
|---|
| 918 | fi |
|---|
| 919 | fi |
|---|
| 920 | service=$INETDSERV |
|---|
| 921 | $ENABLESERVICE; $STARTSERVICE |
|---|
| 922 | |
|---|
| 923 | # comprobamos el servicio tftp |
|---|
| 924 | sleep 1 |
|---|
| 925 | testPxe |
|---|
| 926 | } |
|---|
| 927 | |
|---|
| 928 | # Comprueba que haya conexión al servicio TFTP/PXE. |
|---|
| 929 | function testPxe () |
|---|
| 930 | { |
|---|
| 931 | echoAndLog "${FUNCNAME}(): Checking TFTP service... please wait." |
|---|
| 932 | echo "test" >$TFTPCFGDIR/testpxe |
|---|
| 933 | tftp -v 127.0.0.1 -c get testpxe /tmp/testpxe && echoAndLog "TFTP service is OK." || errorAndLog "TFTP service is down." |
|---|
| 934 | rm -f $TFTPCFGDIR/testpxe /tmp/testpxe |
|---|
| 935 | } |
|---|
| 936 | |
|---|
| 937 | |
|---|
| 938 | ######################################################################## |
|---|
| 939 | ## Configuración servicio Samba |
|---|
| 940 | ######################################################################## |
|---|
| 941 | |
|---|
| 942 | # Configurar servicios Samba. |
|---|
| 943 | function smbConfigure() |
|---|
| 944 | { |
|---|
| 945 | echoAndLog "${FUNCNAME}(): Configuring Samba service." |
|---|
| 946 | |
|---|
| 947 | backupFile $SAMBACFGDIR/smb.conf |
|---|
| 948 | |
|---|
| 949 | # Copiar plantailla de recursos para OpenGnsys |
|---|
| 950 | sed -e "s/OPENGNSYSDIR/${INSTALL_TARGET//\//\\/}/g" \ |
|---|
| 951 | $WORKDIR/opengnsys/server/etc/smb-og.conf.tmpl > $SAMBACFGDIR/smb-og.conf |
|---|
| 952 | # Configurar y recargar Samba" |
|---|
| 953 | perl -pi -e "s/WORKGROUP/OPENGNSYS/; s/server string \=.*/server string \= OpenGnsys Samba Server/" $SAMBACFGDIR/smb.conf |
|---|
| 954 | if ! grep -q "smb-og" $SAMBACFGDIR/smb.conf; then |
|---|
| 955 | echo "include = $SAMBACFGDIR/smb-og.conf" >> $SAMBACFGDIR/smb.conf |
|---|
| 956 | fi |
|---|
| 957 | service=$SAMBASERV |
|---|
| 958 | $ENABLESERVICE; $STARTSERVICE |
|---|
| 959 | if [ $? -ne 0 ]; then |
|---|
| 960 | errorAndLog "${FUNCNAME}(): error while configure Samba" |
|---|
| 961 | return 1 |
|---|
| 962 | fi |
|---|
| 963 | # Crear clave para usuario de acceso a los recursos. |
|---|
| 964 | echo -ne "$OPENGNSYS_CLIENT_PASSWD\n$OPENGNSYS_CLIENT_PASSWD\n" | smbpasswd -a -s $OPENGNSYS_CLIENT_USER |
|---|
| 965 | |
|---|
| 966 | echoAndLog "${FUNCNAME}(): Added Samba configuration." |
|---|
| 967 | return 0 |
|---|
| 968 | } |
|---|
| 969 | |
|---|
| 970 | |
|---|
| 971 | ######################################################################## |
|---|
| 972 | ## Configuración servicio Rsync |
|---|
| 973 | ######################################################################## |
|---|
| 974 | |
|---|
| 975 | # Configurar servicio Rsync. |
|---|
| 976 | function rsyncConfigure() |
|---|
| 977 | { |
|---|
| 978 | echoAndLog "${FUNCNAME}(): Configuring Rsync service." |
|---|
| 979 | |
|---|
| 980 | backupFile $RSYNCCFGDIR/rsyncd.conf |
|---|
| 981 | |
|---|
| 982 | # Configurar acceso a Rsync. |
|---|
| 983 | sed -e "s/CLIENTUSER/$OPENGNSYS_CLIENT_USER/g" \ |
|---|
| 984 | $WORKDIR/opengnsys/repoman/etc/rsyncd.conf.tmpl > $RSYNCCFGDIR/rsyncd.conf |
|---|
| 985 | sed -e "s/CLIENTUSER/$OPENGNSYS_CLIENT_USER/g" \ |
|---|
| 986 | -e "s/CLIENTPASSWORD/$OPENGNSYS_CLIENT_PASSWD/g" \ |
|---|
| 987 | $WORKDIR/opengnsys/repoman/etc/rsyncd.secrets.tmpl > $RSYNCCFGDIR/rsyncd.secrets |
|---|
| 988 | chown root.root $RSYNCCFGDIR/rsyncd.secrets |
|---|
| 989 | chmod 600 $RSYNCCFGDIR/rsyncd.secrets |
|---|
| 990 | |
|---|
| 991 | # Habilitar Rsync y reiniciar Inetd. |
|---|
| 992 | if [ -n "$RSYNCSERV" ]; then |
|---|
| 993 | if [ -f /etc/default/rsync ]; then |
|---|
| 994 | perl -pi -e 's/RSYNC_ENABLE=.*/RSYNC_ENABLE=inetd/' /etc/default/rsync |
|---|
| 995 | fi |
|---|
| 996 | if [ -f $INETDCFGDIR/rsync ]; then |
|---|
| 997 | perl -pi -e 's/disable.*/disable = no/' $INETDCFGDIR/rsync |
|---|
| 998 | else |
|---|
| 999 | cat << EOT > $INETDCFGDIR/rsync |
|---|
| 1000 | service rsync |
|---|
| 1001 | { |
|---|
| 1002 | disable = no |
|---|
| 1003 | socket_type = stream |
|---|
| 1004 | wait = no |
|---|
| 1005 | user = root |
|---|
| 1006 | server = $(which rsync) |
|---|
| 1007 | server_args = --daemon |
|---|
| 1008 | log_on_failure += USERID |
|---|
| 1009 | flags = IPv6 |
|---|
| 1010 | } |
|---|
| 1011 | EOT |
|---|
| 1012 | fi |
|---|
| 1013 | service=$RSYNCSERV $ENABLESERVICE |
|---|
| 1014 | service=$INETDSERV $STARTSERVICE |
|---|
| 1015 | fi |
|---|
| 1016 | |
|---|
| 1017 | echoAndLog "${FUNCNAME}(): Added Rsync configuration." |
|---|
| 1018 | return 0 |
|---|
| 1019 | } |
|---|
| 1020 | |
|---|
| 1021 | |
|---|
| 1022 | ######################################################################## |
|---|
| 1023 | ## Configuración servicio DHCP |
|---|
| 1024 | ######################################################################## |
|---|
| 1025 | |
|---|
| 1026 | # Configurar servicios DHCP. |
|---|
| 1027 | function dhcpConfigure() |
|---|
| 1028 | { |
|---|
| 1029 | echoAndLog "${FUNCNAME}(): Sample DHCP configuration." |
|---|
| 1030 | |
|---|
| 1031 | local errcode=0 |
|---|
| 1032 | local i=0 |
|---|
| 1033 | local dev="" |
|---|
| 1034 | |
|---|
| 1035 | backupFile $DHCPCFGDIR/dhcpd.conf |
|---|
| 1036 | for dev in ${DEVICE[*]}; do |
|---|
| 1037 | if [ -n "${SERVERIP[i]}" ]; then |
|---|
| 1038 | backupFile $DHCPCFGDIR/dhcpd-$dev.conf |
|---|
| 1039 | sed -e "s/SERVERIP/${SERVERIP[i]}/g" \ |
|---|
| 1040 | -e "s/NETIP/${NETIP[i]}/g" \ |
|---|
| 1041 | -e "s/NETMASK/${NETMASK[i]}/g" \ |
|---|
| 1042 | -e "s/NETBROAD/${NETBROAD[i]}/g" \ |
|---|
| 1043 | -e "s/ROUTERIP/${ROUTERIP[i]}/g" \ |
|---|
| 1044 | -e "s/DNSIP/$DNSIP/g" \ |
|---|
| 1045 | $WORKDIR/opengnsys/server/etc/dhcpd.conf.tmpl > $DHCPCFGDIR/dhcpd-$dev.conf || errcode=1 |
|---|
| 1046 | fi |
|---|
| 1047 | let i++ |
|---|
| 1048 | done |
|---|
| 1049 | if [ $errcode -ne 0 ]; then |
|---|
| 1050 | errorAndLog "${FUNCNAME}(): error while configuring DHCP server" |
|---|
| 1051 | return 1 |
|---|
| 1052 | fi |
|---|
| 1053 | ln -f $DHCPCFGDIR/dhcpd-$DEFAULTDEV.conf $DHCPCFGDIR/dhcpd.conf |
|---|
| 1054 | service=$DHCPSERV |
|---|
| 1055 | $ENABLESERVICE; $STARTSERVICE |
|---|
| 1056 | echoAndLog "${FUNCNAME}(): Sample DHCP configured in \"$DHCPCFGDIR\"." |
|---|
| 1057 | return 0 |
|---|
| 1058 | } |
|---|
| 1059 | |
|---|
| 1060 | |
|---|
| 1061 | ##################################################################### |
|---|
| 1062 | ####### Funciones específicas de la instalación de Opengnsys |
|---|
| 1063 | ##################################################################### |
|---|
| 1064 | |
|---|
| 1065 | # Copiar ficheros del OpenGnsys Web Console. |
|---|
| 1066 | function installWebFiles() |
|---|
| 1067 | { |
|---|
| 1068 | local COMPATDIR f |
|---|
| 1069 | local SLIMFILE="slim-2.6.1.zip" |
|---|
| 1070 | local SWAGGERFILE="swagger-ui-2.2.5.zip" |
|---|
| 1071 | |
|---|
| 1072 | echoAndLog "${FUNCNAME}(): Installing web files..." |
|---|
| 1073 | # Copiar ficheros. |
|---|
| 1074 | cp -a $WORKDIR/opengnsys/admin/WebConsole/* $INSTALL_TARGET/www #*/ comentario para Doxygen. |
|---|
| 1075 | if [ $? != 0 ]; then |
|---|
| 1076 | errorAndLog "${FUNCNAME}(): Error copying web files." |
|---|
| 1077 | exit 1 |
|---|
| 1078 | fi |
|---|
| 1079 | |
|---|
| 1080 | # Descomprimir librerías: Slim y Swagger-UI. |
|---|
| 1081 | unzip -o $WORKDIR/opengnsys/admin/$SLIMFILE -d $INSTALL_TARGET/www/rest |
|---|
| 1082 | unzip -o $WORKDIR/opengnsys/admin/$SWAGGERFILE -d $INSTALL_TARGET/www/rest |
|---|
| 1083 | |
|---|
| 1084 | # Compatibilidad con dispositivos móviles. |
|---|
| 1085 | COMPATDIR="$INSTALL_TARGET/www/principal" |
|---|
| 1086 | for f in acciones administracion aula aulas hardwares imagenes menus repositorios softwares; do |
|---|
| 1087 | sed 's/clickcontextualnodo/clicksupnodo/g' $COMPATDIR/$f.php > $COMPATDIR/$f.device.php |
|---|
| 1088 | done |
|---|
| 1089 | cp -a $COMPATDIR/imagenes.device.php $COMPATDIR/imagenes.device4.php |
|---|
| 1090 | # Cambiar permisos para ficheros especiales. |
|---|
| 1091 | chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP $INSTALL_TARGET/www/images/{fotos,iconos} |
|---|
| 1092 | chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP $INSTALL_TARGET/www/tmp/ |
|---|
| 1093 | # Ficheros de log de la API REST. |
|---|
| 1094 | touch $INSTALL_TARGET/log/{ogagent,remotepc,rest}.log |
|---|
| 1095 | chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP $INSTALL_TARGET/log/{ogagent,remotepc,rest}.log |
|---|
| 1096 | |
|---|
| 1097 | echoAndLog "${FUNCNAME}(): Web files installed successfully." |
|---|
| 1098 | } |
|---|
| 1099 | |
|---|
| 1100 | # Copiar ficheros en la zona de descargas de OpenGnsys Web Console. |
|---|
| 1101 | function installDownloadableFiles() |
|---|
| 1102 | { |
|---|
| 1103 | INSTVERSION=1.1.0 ### Temporal. |
|---|
| 1104 | local FILENAME=ogagentpkgs-$INSTVERSION.tar.gz |
|---|
| 1105 | local TARGETFILE=$WORKDIR/$FILENAME |
|---|
| 1106 | |
|---|
| 1107 | # Descargar archivo comprimido, si es necesario. |
|---|
| 1108 | if [ -s $PROGRAMDIR/$FILENAME ]; then |
|---|
| 1109 | echoAndLog "${FUNCNAME}(): Moving $PROGRAMDIR/$FILENAME file to $(dirname $TARGETFILE)" |
|---|
| 1110 | mv $PROGRAMDIR/$FILENAME $TARGETFILE |
|---|
| 1111 | else |
|---|
| 1112 | echoAndLog "${FUNCNAME}(): Downloading $FILENAME" |
|---|
| 1113 | curl $DOWNLOADURL/$FILENAME -o $TARGETFILE |
|---|
| 1114 | fi |
|---|
| 1115 | if [ ! -s $TARGETFILE ]; then |
|---|
| 1116 | errorAndLog "${FUNCNAME}(): Cannot download $FILENAME" |
|---|
| 1117 | return 1 |
|---|
| 1118 | fi |
|---|
| 1119 | |
|---|
| 1120 | # Descomprimir fichero en zona de descargas. |
|---|
| 1121 | tar xvzf $TARGETFILE -C $INSTALL_TARGET/www/descargas |
|---|
| 1122 | if [ $? != 0 ]; then |
|---|
| 1123 | errorAndLog "${FUNCNAME}(): Error uncompressing archive." |
|---|
| 1124 | exit 1 |
|---|
| 1125 | fi |
|---|
| 1126 | } |
|---|
| 1127 | |
|---|
| 1128 | # Configuración específica de Apache. |
|---|
| 1129 | function installWebConsoleApacheConf() |
|---|
| 1130 | { |
|---|
| 1131 | if [ $# -ne 2 ]; then |
|---|
| 1132 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 1133 | exit 1 |
|---|
| 1134 | fi |
|---|
| 1135 | |
|---|
| 1136 | local path_opengnsys_base="$1" |
|---|
| 1137 | local path_apache2_confd="$2" |
|---|
| 1138 | local CONSOLEDIR=${path_opengnsys_base}/www |
|---|
| 1139 | |
|---|
| 1140 | if [ ! -d $path_apache2_confd ]; then |
|---|
| 1141 | errorAndLog "${FUNCNAME}(): path to apache2 conf.d can not found, verify your server installation" |
|---|
| 1142 | return 1 |
|---|
| 1143 | fi |
|---|
| 1144 | |
|---|
| 1145 | mkdir -p $path_apache2_confd/{sites-available,sites-enabled} |
|---|
| 1146 | |
|---|
| 1147 | echoAndLog "${FUNCNAME}(): creating apache2 config file.." |
|---|
| 1148 | |
|---|
| 1149 | # Activar HTTPS. |
|---|
| 1150 | $APACHESSLMOD |
|---|
| 1151 | $APACHEENABLESSL |
|---|
| 1152 | $APACHEMAKECERT |
|---|
| 1153 | # Activar módulo Rewrite. |
|---|
| 1154 | $APACHEREWRITEMOD |
|---|
| 1155 | # Genera configuración de consola web a partir del fichero plantilla. |
|---|
| 1156 | if [ -n "$(apachectl -v | grep "2\.[0-2]")" ]; then |
|---|
| 1157 | # Configuración para versiones anteriores de Apache. |
|---|
| 1158 | sed -e "s,CONSOLEDIR,$CONSOLEDIR,g" \ |
|---|
| 1159 | $WORKDIR/opengnsys/server/etc/apache-prev2.4.conf.tmpl > $path_apache2_confd/$APACHESITESDIR/${APACHEOGSITE} |
|---|
| 1160 | else |
|---|
| 1161 | # Configuración específica a partir de Apache 2.4 |
|---|
| 1162 | sed -e "s,CONSOLEDIR,$CONSOLEDIR,g" \ |
|---|
| 1163 | $WORKDIR/opengnsys/server/etc/apache.conf.tmpl > $path_apache2_confd/$APACHESITESDIR/${APACHEOGSITE}.conf |
|---|
| 1164 | fi |
|---|
| 1165 | $APACHEENABLEOG |
|---|
| 1166 | if [ $? -ne 0 ]; then |
|---|
| 1167 | errorAndLog "${FUNCNAME}(): config file can't be linked to apache conf, verify your server installation" |
|---|
| 1168 | return 1 |
|---|
| 1169 | fi |
|---|
| 1170 | echoAndLog "${FUNCNAME}(): config file created and linked, restarting apache daemon" |
|---|
| 1171 | service=$APACHESERV |
|---|
| 1172 | $ENABLESERVICE; $STARTSERVICE |
|---|
| 1173 | return 0 |
|---|
| 1174 | } |
|---|
| 1175 | |
|---|
| 1176 | |
|---|
| 1177 | # Crear documentación Doxygen para la consola web. |
|---|
| 1178 | function makeDoxygenFiles() |
|---|
| 1179 | { |
|---|
| 1180 | echoAndLog "${FUNCNAME}(): Making Doxygen web files..." |
|---|
| 1181 | $WORKDIR/opengnsys/installer/ogGenerateDoc.sh \ |
|---|
| 1182 | $WORKDIR/opengnsys/client/engine $INSTALL_TARGET/www |
|---|
| 1183 | if [ ! -d "$INSTALL_TARGET/www/html" ]; then |
|---|
| 1184 | errorAndLog "${FUNCNAME}(): unable to create Doxygen web files." |
|---|
| 1185 | return 1 |
|---|
| 1186 | fi |
|---|
| 1187 | mv "$INSTALL_TARGET/www/html" "$INSTALL_TARGET/www/api" |
|---|
| 1188 | chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP $INSTALL_TARGET/www/api |
|---|
| 1189 | echoAndLog "${FUNCNAME}(): Doxygen web files created successfully." |
|---|
| 1190 | } |
|---|
| 1191 | |
|---|
| 1192 | |
|---|
| 1193 | # Crea la estructura base de la instalación de opengnsys |
|---|
| 1194 | function createDirs() |
|---|
| 1195 | { |
|---|
| 1196 | if [ $# -ne 1 ]; then |
|---|
| 1197 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 1198 | exit 1 |
|---|
| 1199 | fi |
|---|
| 1200 | |
|---|
| 1201 | local path_opengnsys_base="$1" |
|---|
| 1202 | |
|---|
| 1203 | # Crear estructura de directorios. |
|---|
| 1204 | echoAndLog "${FUNCNAME}(): creating directory paths in $path_opengnsys_base" |
|---|
| 1205 | mkdir -p $path_opengnsys_base |
|---|
| 1206 | mkdir -p $path_opengnsys_base/bin |
|---|
| 1207 | mkdir -p $path_opengnsys_base/client/{cache,images,log} |
|---|
| 1208 | mkdir -p $path_opengnsys_base/doc |
|---|
| 1209 | mkdir -p $path_opengnsys_base/etc |
|---|
| 1210 | mkdir -p $path_opengnsys_base/lib |
|---|
| 1211 | mkdir -p $path_opengnsys_base/log/clients |
|---|
| 1212 | ln -fs $path_opengnsys_base/log /var/log/opengnsys |
|---|
| 1213 | mkdir -p $path_opengnsys_base/sbin |
|---|
| 1214 | mkdir -p $path_opengnsys_base/www |
|---|
| 1215 | mkdir -p $path_opengnsys_base/images/groups |
|---|
| 1216 | mkdir -p $TFTPCFGDIR |
|---|
| 1217 | ln -fs $TFTPCFGDIR $path_opengnsys_base/tftpboot |
|---|
| 1218 | mkdir -p $path_opengnsys_base/tftpboot/{menu.lst,grub} |
|---|
| 1219 | if [ $? -ne 0 ]; then |
|---|
| 1220 | errorAndLog "${FUNCNAME}(): error while creating dirs. Do you have write permissions?" |
|---|
| 1221 | return 1 |
|---|
| 1222 | fi |
|---|
| 1223 | |
|---|
| 1224 | # Crear usuario ficticio. |
|---|
| 1225 | if id -u $OPENGNSYS_CLIENT_USER &>/dev/null; then |
|---|
| 1226 | echoAndLog "${FUNCNAME}(): user \"$OPENGNSYS_CLIENT_USER\" is already created" |
|---|
| 1227 | else |
|---|
| 1228 | echoAndLog "${FUNCNAME}(): creating OpenGnsys user" |
|---|
| 1229 | useradd $OPENGNSYS_CLIENT_USER 2>/dev/null |
|---|
| 1230 | if [ $? -ne 0 ]; then |
|---|
| 1231 | errorAndLog "${FUNCNAME}(): error creating OpenGnsys user" |
|---|
| 1232 | return 1 |
|---|
| 1233 | fi |
|---|
| 1234 | fi |
|---|
| 1235 | |
|---|
| 1236 | # Establecer los permisos básicos. |
|---|
| 1237 | echoAndLog "${FUNCNAME}(): setting directory permissions" |
|---|
| 1238 | chmod -R 775 $path_opengnsys_base/{log/clients,images} |
|---|
| 1239 | chown -R :$OPENGNSYS_CLIENT_USER $path_opengnsys_base/{log/clients,images} |
|---|
| 1240 | if [ $? -ne 0 ]; then |
|---|
| 1241 | errorAndLog "${FUNCNAME}(): error while setting permissions" |
|---|
| 1242 | return 1 |
|---|
| 1243 | fi |
|---|
| 1244 | |
|---|
| 1245 | # Mover el fichero de registro de instalación al directorio de logs. |
|---|
| 1246 | echoAndLog "${FUNCNAME}(): moving installation log file" |
|---|
| 1247 | mv $LOG_FILE $OGLOGFILE && LOG_FILE=$OGLOGFILE |
|---|
| 1248 | chmod 600 $LOG_FILE |
|---|
| 1249 | |
|---|
| 1250 | echoAndLog "${FUNCNAME}(): directory paths created" |
|---|
| 1251 | return 0 |
|---|
| 1252 | } |
|---|
| 1253 | |
|---|
| 1254 | # Copia ficheros de configuración y ejecutables genéricos del servidor. |
|---|
| 1255 | function copyServerFiles () |
|---|
| 1256 | { |
|---|
| 1257 | if [ $# -ne 1 ]; then |
|---|
| 1258 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 1259 | exit 1 |
|---|
| 1260 | fi |
|---|
| 1261 | |
|---|
| 1262 | local path_opengnsys_base="$1" |
|---|
| 1263 | |
|---|
| 1264 | # Lista de ficheros y directorios origen y de directorios destino. |
|---|
| 1265 | local SOURCES=( server/tftpboot \ |
|---|
| 1266 | /usr/lib/shim/shimx64.efi.signed \ |
|---|
| 1267 | /usr/lib/grub/x86_64-efi-signed/grubnetx64.efi.signed \ |
|---|
| 1268 | server/bin \ |
|---|
| 1269 | repoman/bin \ |
|---|
| 1270 | server/lib \ |
|---|
| 1271 | admin/Sources/Services/ogAdmServerAux |
|---|
| 1272 | admin/Sources/Services/ogAdmRepoAux |
|---|
| 1273 | installer/opengnsys_uninstall.sh \ |
|---|
| 1274 | installer/opengnsys_update.sh \ |
|---|
| 1275 | installer/opengnsys_export.sh \ |
|---|
| 1276 | installer/opengnsys_import.sh \ |
|---|
| 1277 | doc ) |
|---|
| 1278 | local TARGETS=( tftpboot \ |
|---|
| 1279 | tftpboot \ |
|---|
| 1280 | tftpboot/grubx64.efi \ |
|---|
| 1281 | bin \ |
|---|
| 1282 | bin \ |
|---|
| 1283 | lib \ |
|---|
| 1284 | sbin \ |
|---|
| 1285 | sbin \ |
|---|
| 1286 | lib \ |
|---|
| 1287 | lib \ |
|---|
| 1288 | lib \ |
|---|
| 1289 | lib \ |
|---|
| 1290 | doc ) |
|---|
| 1291 | |
|---|
| 1292 | if [ ${#SOURCES[@]} != ${#TARGETS[@]} ]; then |
|---|
| 1293 | errorAndLog "${FUNCNAME}(): inconsistent number of array items" |
|---|
| 1294 | exit 1 |
|---|
| 1295 | fi |
|---|
| 1296 | |
|---|
| 1297 | # Copiar ficheros. |
|---|
| 1298 | echoAndLog "${FUNCNAME}(): copying files to server directories" |
|---|
| 1299 | |
|---|
| 1300 | pushd $WORKDIR/opengnsys |
|---|
| 1301 | local i |
|---|
| 1302 | for (( i = 0; i < ${#SOURCES[@]}; i++ )); do |
|---|
| 1303 | if [ -f "${SOURCES[$i]}" ]; then |
|---|
| 1304 | echoAndLog "Copying ${SOURCES[$i]} to $path_opengnsys_base/${TARGETS[$i]}" |
|---|
| 1305 | cp -a "${SOURCES[$i]}" "${path_opengnsys_base}/${TARGETS[$i]}" |
|---|
| 1306 | elif [ -d "${SOURCES[$i]}" ]; then |
|---|
| 1307 | echoAndLog "Copying content of ${SOURCES[$i]} to $path_opengnsys_base/${TARGETS[$i]}" |
|---|
| 1308 | cp -a "${SOURCES[$i]}"/* "${path_opengnsys_base}/${TARGETS[$i]}" |
|---|
| 1309 | else |
|---|
| 1310 | warningAndLog "Unable to copy ${SOURCES[$i]} to $path_opengnsys_base/${TARGETS[$i]}" |
|---|
| 1311 | fi |
|---|
| 1312 | done |
|---|
| 1313 | |
|---|
| 1314 | popd |
|---|
| 1315 | } |
|---|
| 1316 | |
|---|
| 1317 | #################################################################### |
|---|
| 1318 | ### Funciones de compilación de código fuente de servicios |
|---|
| 1319 | #################################################################### |
|---|
| 1320 | |
|---|
| 1321 | # Compilar los servicios de OpenGnsys |
|---|
| 1322 | function servicesCompilation () |
|---|
| 1323 | { |
|---|
| 1324 | local hayErrores=0 |
|---|
| 1325 | |
|---|
| 1326 | # Compilar OpenGnsys Server |
|---|
| 1327 | echoAndLog "${FUNCNAME}(): Compiling OpenGnsys Admin Server" |
|---|
| 1328 | pushd $WORKDIR/opengnsys/admin/Sources/Services/ogAdmServer |
|---|
| 1329 | make && mv ogAdmServer $INSTALL_TARGET/sbin |
|---|
| 1330 | if [ $? -ne 0 ]; then |
|---|
| 1331 | echoAndLog "${FUNCNAME}(): error while compiling OpenGnsys Admin Server" |
|---|
| 1332 | hayErrores=1 |
|---|
| 1333 | fi |
|---|
| 1334 | popd |
|---|
| 1335 | # Compilar OpenGnsys Repository Manager |
|---|
| 1336 | echoAndLog "${FUNCNAME}(): Compiling OpenGnsys Repository Manager" |
|---|
| 1337 | pushd $WORKDIR/opengnsys/admin/Sources/Services/ogAdmRepo |
|---|
| 1338 | make && mv ogAdmRepo $INSTALL_TARGET/sbin |
|---|
| 1339 | if [ $? -ne 0 ]; then |
|---|
| 1340 | echoAndLog "${FUNCNAME}(): error while compiling OpenGnsys Repository Manager" |
|---|
| 1341 | hayErrores=1 |
|---|
| 1342 | fi |
|---|
| 1343 | popd |
|---|
| 1344 | # Compilar OpenGnsys Agent |
|---|
| 1345 | echoAndLog "${FUNCNAME}(): Compiling OpenGnsys Agent" |
|---|
| 1346 | pushd $WORKDIR/opengnsys/admin/Sources/Services/ogAdmAgent |
|---|
| 1347 | make && mv ogAdmAgent $INSTALL_TARGET/sbin |
|---|
| 1348 | if [ $? -ne 0 ]; then |
|---|
| 1349 | echoAndLog "${FUNCNAME}(): error while compiling OpenGnsys Agent" |
|---|
| 1350 | hayErrores=1 |
|---|
| 1351 | fi |
|---|
| 1352 | popd |
|---|
| 1353 | # Compilar OpenGnsys Client |
|---|
| 1354 | echoAndLog "${FUNCNAME}(): Compiling OpenGnsys Admin Client" |
|---|
| 1355 | pushd $WORKDIR/opengnsys/admin/Sources/Clients/ogAdmClient |
|---|
| 1356 | make && mv ogAdmClient ../../../../client/shared/bin |
|---|
| 1357 | if [ $? -ne 0 ]; then |
|---|
| 1358 | echoAndLog "${FUNCNAME}(): error while compiling OpenGnsys Admin Client" |
|---|
| 1359 | hayErrores=1 |
|---|
| 1360 | fi |
|---|
| 1361 | popd |
|---|
| 1362 | |
|---|
| 1363 | return $hayErrores |
|---|
| 1364 | } |
|---|
| 1365 | |
|---|
| 1366 | #################################################################### |
|---|
| 1367 | ### Funciones de copia de la Interface de administración |
|---|
| 1368 | #################################################################### |
|---|
| 1369 | |
|---|
| 1370 | # Copiar carpeta de Interface |
|---|
| 1371 | function copyInterfaceAdm () |
|---|
| 1372 | { |
|---|
| 1373 | local hayErrores=0 |
|---|
| 1374 | |
|---|
| 1375 | # Crear carpeta y copiar Interface |
|---|
| 1376 | echoAndLog "${FUNCNAME}(): Copying Administration Interface Folder" |
|---|
| 1377 | cp -ar $WORKDIR/opengnsys/admin/Interface $INSTALL_TARGET/client/interfaceAdm |
|---|
| 1378 | if [ $? -ne 0 ]; then |
|---|
| 1379 | echoAndLog "${FUNCNAME}(): error while copying Administration Interface Folder" |
|---|
| 1380 | hayErrores=1 |
|---|
| 1381 | fi |
|---|
| 1382 | chown $OPENGNSYS_CLIENT_USER:$OPENGNSYS_CLIENT_USER $INSTALL_TARGET/client/interfaceAdm/CambiarAcceso |
|---|
| 1383 | chmod 700 $INSTALL_TARGET/client/interfaceAdm/CambiarAcceso |
|---|
| 1384 | |
|---|
| 1385 | return $hayErrores |
|---|
| 1386 | } |
|---|
| 1387 | |
|---|
| 1388 | #################################################################### |
|---|
| 1389 | ### Funciones instalacion cliente opengnsys |
|---|
| 1390 | #################################################################### |
|---|
| 1391 | |
|---|
| 1392 | function copyClientFiles() |
|---|
| 1393 | { |
|---|
| 1394 | local errstatus=0 |
|---|
| 1395 | |
|---|
| 1396 | echoAndLog "${FUNCNAME}(): Copying OpenGnsys Client files." |
|---|
| 1397 | cp -a $WORKDIR/opengnsys/client/shared/* $INSTALL_TARGET/client |
|---|
| 1398 | if [ $? -ne 0 ]; then |
|---|
| 1399 | errorAndLog "${FUNCNAME}(): error while copying client estructure" |
|---|
| 1400 | errstatus=1 |
|---|
| 1401 | fi |
|---|
| 1402 | |
|---|
| 1403 | echoAndLog "${FUNCNAME}(): Copying OpenGnsys Cloning Engine files." |
|---|
| 1404 | mkdir -p $INSTALL_TARGET/client/lib/engine/bin |
|---|
| 1405 | cp -a $WORKDIR/opengnsys/client/engine/*.lib* $INSTALL_TARGET/client/lib/engine/bin |
|---|
| 1406 | if [ $? -ne 0 ]; then |
|---|
| 1407 | errorAndLog "${FUNCNAME}(): error while copying engine files" |
|---|
| 1408 | errstatus=1 |
|---|
| 1409 | fi |
|---|
| 1410 | |
|---|
| 1411 | if [ $errstatus -eq 0 ]; then |
|---|
| 1412 | echoAndLog "${FUNCNAME}(): client copy files success." |
|---|
| 1413 | else |
|---|
| 1414 | errorAndLog "${FUNCNAME}(): client copy files with errors" |
|---|
| 1415 | fi |
|---|
| 1416 | |
|---|
| 1417 | return $errstatus |
|---|
| 1418 | } |
|---|
| 1419 | |
|---|
| 1420 | |
|---|
| 1421 | # Crear cliente OpenGnsys. |
|---|
| 1422 | function clientCreate() |
|---|
| 1423 | { |
|---|
| 1424 | if [ $# -ne 1 ]; then |
|---|
| 1425 | errorAndLog "${FUNCNAME}(): invalid number of parameters" |
|---|
| 1426 | exit 1 |
|---|
| 1427 | fi |
|---|
| 1428 | |
|---|
| 1429 | local FILENAME="$1" |
|---|
| 1430 | local TARGETFILE=$INSTALL_TARGET/lib/$FILENAME |
|---|
| 1431 | |
|---|
| 1432 | # Descargar cliente, si es necesario. |
|---|
| 1433 | if [ -s $PROGRAMDIR/$FILENAME ]; then |
|---|
| 1434 | echoAndLog "${FUNCNAME}(): Moving $PROGRAMDIR/$FILENAME file to $(dirname $TARGETFILE)" |
|---|
| 1435 | mv $PROGRAMDIR/$FILENAME $TARGETFILE |
|---|
| 1436 | else |
|---|
| 1437 | echoAndLog "${FUNCNAME}(): Downloading $FILENAME" |
|---|
| 1438 | oglivecli download $FILENAME |
|---|
| 1439 | fi |
|---|
| 1440 | if [ ! -s $TARGETFILE ]; then |
|---|
| 1441 | errorAndLog "${FUNCNAME}(): Error loading $FILENAME" |
|---|
| 1442 | return 1 |
|---|
| 1443 | fi |
|---|
| 1444 | |
|---|
| 1445 | # Montar imagen, copiar cliente ogclient y desmontar. |
|---|
| 1446 | echoAndLog "${FUNCNAME}(): Installing ogLive Client" |
|---|
| 1447 | echo -ne "$OPENGNSYS_CLIENT_PASSWD\n$OPENGNSYS_CLIENT_PASSWD\n" | \ |
|---|
| 1448 | oglivecli install $FILENAME |
|---|
| 1449 | # Adaptar permisos. |
|---|
| 1450 | chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP $INSTALL_TARGET/tftpboot/menu.lst |
|---|
| 1451 | |
|---|
| 1452 | echoAndLog "${FUNCNAME}(): Client generation success" |
|---|
| 1453 | } |
|---|
| 1454 | |
|---|
| 1455 | |
|---|
| 1456 | # Configuración básica de servicios de OpenGnsys |
|---|
| 1457 | function openGnsysConfigure() |
|---|
| 1458 | { |
|---|
| 1459 | local i=0 |
|---|
| 1460 | local dev="" |
|---|
| 1461 | local CONSOLEURL |
|---|
| 1462 | |
|---|
| 1463 | echoAndLog "${FUNCNAME}(): Copying init files." |
|---|
| 1464 | cp -a $WORKDIR/opengnsys/admin/Sources/Services/opengnsys.init /etc/init.d/opengnsys |
|---|
| 1465 | cp -a $WORKDIR/opengnsys/admin/Sources/Services/opengnsys.default /etc/default/opengnsys |
|---|
| 1466 | # Deshabilitar servicios de BitTorrent si no están instalados. |
|---|
| 1467 | if [ ! -e /usr/bin/bttrack ]; then |
|---|
| 1468 | sed -i 's/RUN_BTTRACKER="yes"/RUN_BTTRACKER="no"/; s/RUN_BTSEEDER="yes"/RUN_BTSEEDER="no"/' \ |
|---|
| 1469 | /etc/default/opengnsys |
|---|
| 1470 | fi |
|---|
| 1471 | echoAndLog "${FUNCNAME}(): Creating cron files." |
|---|
| 1472 | echo "* * * * * root [ -x $INSTALL_TARGET/bin/opengnsys.cron ] && $INSTALL_TARGET/bin/opengnsys.cron" > /etc/cron.d/opengnsys |
|---|
| 1473 | echo "* * * * * root [ -x $INSTALL_TARGET/bin/torrent-creator ] && $INSTALL_TARGET/bin/torrent-creator" > /etc/cron.d/torrentcreator |
|---|
| 1474 | echo "5 * * * * root [ -x $INSTALL_TARGET/bin/torrent-tracker ] && $INSTALL_TARGET/bin/torrent-tracker" > /etc/cron.d/torrenttracker |
|---|
| 1475 | echo "* * * * * root [ -x $INSTALL_TARGET/bin/deletepreimage ] && $INSTALL_TARGET/bin/deletepreimage" > /etc/cron.d/imagedelete |
|---|
| 1476 | echo "* * * * * root [ -x $INSTALL_TARGET/bin/ogagentqueue.cron ] && $INSTALL_TARGET/bin/ogagentqueue.cron" > /etc/cron.d/ogagentqueue |
|---|
| 1477 | |
|---|
| 1478 | echoAndLog "${FUNCNAME}(): Creating logrotate configuration file." |
|---|
| 1479 | sed -e "s/OPENGNSYSDIR/${INSTALL_TARGET//\//\\/}/g" \ |
|---|
| 1480 | $WORKDIR/opengnsys/server/etc/logrotate.tmpl > /etc/logrotate.d/opengnsys |
|---|
| 1481 | |
|---|
| 1482 | echoAndLog "${FUNCNAME}(): Creating OpenGnsys config files." |
|---|
| 1483 | for dev in ${DEVICE[*]}; do |
|---|
| 1484 | if [ -n "${SERVERIP[i]}" ]; then |
|---|
| 1485 | sed -e "s/SERVERIP/${SERVERIP[i]}/g" \ |
|---|
| 1486 | -e "s/DBUSER/$OPENGNSYS_DB_USER/g" \ |
|---|
| 1487 | -e "s/DBPASSWORD/$OPENGNSYS_DB_PASSWD/g" \ |
|---|
| 1488 | -e "s/DATABASE/$OPENGNSYS_DATABASE/g" \ |
|---|
| 1489 | $WORKDIR/opengnsys/admin/Sources/Services/ogAdmServer/ogAdmServer.cfg > $INSTALL_TARGET/etc/ogAdmServer-$dev.cfg |
|---|
| 1490 | sed -e "s/SERVERIP/${SERVERIP[i]}/g" \ |
|---|
| 1491 | -e "s/REPOKEY/$OPENGNSYS_REPOKEY/g" \ |
|---|
| 1492 | $WORKDIR/opengnsys/admin/Sources/Services/ogAdmRepo/ogAdmRepo.cfg > $INSTALL_TARGET/etc/ogAdmRepo-$dev.cfg |
|---|
| 1493 | sed -e "s/SERVERIP/${SERVERIP[i]}/g" \ |
|---|
| 1494 | -e "s/DBUSER/$OPENGNSYS_DB_USER/g" \ |
|---|
| 1495 | -e "s/DBPASSWORD/$OPENGNSYS_DB_PASSWD/g" \ |
|---|
| 1496 | -e "s/DATABASE/$OPENGNSYS_DATABASE/g" \ |
|---|
| 1497 | $WORKDIR/opengnsys/admin/Sources/Services/ogAdmAgent/ogAdmAgent.cfg > $INSTALL_TARGET/etc/ogAdmAgent-$dev.cfg |
|---|
| 1498 | CONSOLEURL="https://${SERVERIP[i]}/opengnsys" |
|---|
| 1499 | sed -e "s/SERVERIP/${SERVERIP[i]}/g" \ |
|---|
| 1500 | -e "s/DBUSER/$OPENGNSYS_DB_USER/g" \ |
|---|
| 1501 | -e "s/DBPASSWORD/$OPENGNSYS_DB_PASSWD/g" \ |
|---|
| 1502 | -e "s/DATABASE/$OPENGNSYS_DATABASE/g" \ |
|---|
| 1503 | -e "s/OPENGNSYSURL/${CONSOLEURL//\//\\/}/g" \ |
|---|
| 1504 | $INSTALL_TARGET/www/controlacceso.php > $INSTALL_TARGET/www/controlacceso-$dev.php |
|---|
| 1505 | sed -e "s/SERVERIP/${SERVERIP[i]}/g" \ |
|---|
| 1506 | -e "s/OPENGNSYSURL/${CONSOLEURL//\//\\/}/g" \ |
|---|
| 1507 | $WORKDIR/opengnsys/admin/Sources/Clients/ogAdmClient/ogAdmClient.cfg > $INSTALL_TARGET/client/etc/ogAdmClient-$dev.cfg |
|---|
| 1508 | if [ "$dev" == "$DEFAULTDEV" ]; then |
|---|
| 1509 | OPENGNSYS_CONSOLEURL="$CONSOLEURL" |
|---|
| 1510 | fi |
|---|
| 1511 | fi |
|---|
| 1512 | let i++ |
|---|
| 1513 | done |
|---|
| 1514 | ln -f $INSTALL_TARGET/etc/ogAdmServer-$DEFAULTDEV.cfg $INSTALL_TARGET/etc/ogAdmServer.cfg |
|---|
| 1515 | ln -f $INSTALL_TARGET/etc/ogAdmRepo-$DEFAULTDEV.cfg $INSTALL_TARGET/etc/ogAdmRepo.cfg |
|---|
| 1516 | ln -f $INSTALL_TARGET/etc/ogAdmAgent-$DEFAULTDEV.cfg $INSTALL_TARGET/etc/ogAdmAgent.cfg |
|---|
| 1517 | ln -f $INSTALL_TARGET/client/etc/ogAdmClient-$DEFAULTDEV.cfg $INSTALL_TARGET/client/etc/ogAdmClient.cfg |
|---|
| 1518 | ln -f $INSTALL_TARGET/www/controlacceso-$DEFAULTDEV.php $INSTALL_TARGET/www/controlacceso.php |
|---|
| 1519 | chown root:root $INSTALL_TARGET/etc/{ogAdmServer,ogAdmAgent}*.cfg |
|---|
| 1520 | chmod 600 $INSTALL_TARGET/etc/{ogAdmServer,ogAdmAgent}*.cfg |
|---|
| 1521 | chown $APACHE_RUN_USER:$APACHE_RUN_GROUP $INSTALL_TARGET/www/controlacceso*.php |
|---|
| 1522 | chmod 600 $INSTALL_TARGET/www/controlacceso*.php |
|---|
| 1523 | |
|---|
| 1524 | # Configuración del motor de clonación. |
|---|
| 1525 | # - Zona horaria del servidor. |
|---|
| 1526 | TZ=$(timedatectl status|awk -F"[:()]" '/Time.*zone/ {print $2}') |
|---|
| 1527 | cat << EOT >> $INSTALL_TARGET/client/etc/engine.cfg |
|---|
| 1528 | # OpenGnsys Server timezone. |
|---|
| 1529 | TZ="${TZ// /}" |
|---|
| 1530 | EOT |
|---|
| 1531 | |
|---|
| 1532 | # Revisar permisos generales. |
|---|
| 1533 | if [ -x $INSTALL_TARGET/bin/checkperms ]; then |
|---|
| 1534 | echoAndLog "${FUNCNAME}(): Checking permissions." |
|---|
| 1535 | OPENGNSYS_DIR="$INSTALL_TARGET" OPENGNSYS_USER="$OPENGNSYS_CLIENT_USER" APACHE_USER="$APACHE_RUN_USER" APACHE_GROUP="$APACHE_RUN_GROUP" checkperms |
|---|
| 1536 | fi |
|---|
| 1537 | |
|---|
| 1538 | # Evitar inicio de duplicado en Ubuntu 14.04 (Upstart y SysV Init). |
|---|
| 1539 | if [ -f /etc/init/${MYSQLSERV}.conf -a -n "$(which initctl 2>/dev/null)" ]; then |
|---|
| 1540 | service=$MYSQLSERV |
|---|
| 1541 | $DISABLESERVICE |
|---|
| 1542 | fi |
|---|
| 1543 | |
|---|
| 1544 | echoAndLog "${FUNCNAME}(): Starting OpenGnsys services." |
|---|
| 1545 | service="opengnsys" |
|---|
| 1546 | $ENABLESERVICE; $STARTSERVICE |
|---|
| 1547 | } |
|---|
| 1548 | |
|---|
| 1549 | |
|---|
| 1550 | ##################################################################### |
|---|
| 1551 | ####### Función de resumen informativo de la instalación |
|---|
| 1552 | ##################################################################### |
|---|
| 1553 | |
|---|
| 1554 | function installationSummary() |
|---|
| 1555 | { |
|---|
| 1556 | # Crear fichero de versión y revisión, si no existe. |
|---|
| 1557 | local VERSIONFILE="$INSTALL_TARGET/doc/VERSION.txt" |
|---|
| 1558 | [ -f $VERSIONFILE ] || echo "OpenGnsys Server" >$VERSIONFILE |
|---|
| 1559 | # Incluir datos de revisión, si se está instaladno desde el repositorio |
|---|
| 1560 | # de código o si no está incluida en el fichero de versión. |
|---|
| 1561 | if [ $REMOTE -eq 1 ] || [ -z "$(awk '$3~/r[0-9]*/ {print}' $VERSIONFILE)" ]; then |
|---|
| 1562 | local REVISION=$(curl -s "$API_URL" | jq -r ".commit.commit.committer.date" | awk '{gsub(/[^0-9]/,""); print}') |
|---|
| 1563 | sed -ri "s/($| r[0-9]*)/ $REVISION/" $VERSIONFILE |
|---|
| 1564 | fi |
|---|
| 1565 | |
|---|
| 1566 | # Mostrar información. |
|---|
| 1567 | echo |
|---|
| 1568 | echoAndLog "OpenGnsys Installation Summary" |
|---|
| 1569 | echo "==============================" |
|---|
| 1570 | echoAndLog "Project version: $(cat $VERSIONFILE 2>/dev/null)" |
|---|
| 1571 | echoAndLog "Installation directory: $INSTALL_TARGET" |
|---|
| 1572 | echoAndLog "Installation log file: $LOG_FILE" |
|---|
| 1573 | echoAndLog "Repository directory: $INSTALL_TARGET/images" |
|---|
| 1574 | echoAndLog "DHCP configuration directory: $DHCPCFGDIR" |
|---|
| 1575 | echoAndLog "TFTP configuration directory: $TFTPCFGDIR" |
|---|
| 1576 | echoAndLog "Installed ogLive client(s): $(oglivecli list | awk '{print $2}')" |
|---|
| 1577 | echoAndLog "Samba configuration directory: $SAMBACFGDIR" |
|---|
| 1578 | echoAndLog "Web Console URL: $OPENGNSYS_CONSOLEURL" |
|---|
| 1579 | echoAndLog "Web Console access data: specified in installer script" |
|---|
| 1580 | if grep -q "^RUN_BTTRACK.*no" /etc/default/opengnsys; then |
|---|
| 1581 | echoAndLog "BitTorrent service is disabled." |
|---|
| 1582 | fi |
|---|
| 1583 | echo |
|---|
| 1584 | echoAndLog "Post-Installation Instructions:" |
|---|
| 1585 | echo "===============================" |
|---|
| 1586 | echoAndLog "Firewall service has been disabled and SELinux mode set to" |
|---|
| 1587 | echoAndLog " permissive during OpenGnsys installation. Please check" |
|---|
| 1588 | echoAndLog " ${FIREWALLSERV:-firewall} and SELinux configuration, if needed." |
|---|
| 1589 | echoAndLog "It's strongly recommended to synchronize this server with an NTP server." |
|---|
| 1590 | echoAndLog "Review or edit all configuration files." |
|---|
| 1591 | echoAndLog "Insert DHCP configuration data and restart service." |
|---|
| 1592 | echoAndLog "Optional: Log-in as Web Console admin user." |
|---|
| 1593 | echoAndLog " - Review default Organization data and assign access to users." |
|---|
| 1594 | echoAndLog "Log-in as Web Console organization user." |
|---|
| 1595 | echoAndLog " - Insert OpenGnsys data (labs, computers, menus, etc)." |
|---|
| 1596 | echo |
|---|
| 1597 | } |
|---|
| 1598 | |
|---|
| 1599 | |
|---|
| 1600 | |
|---|
| 1601 | ##################################################################### |
|---|
| 1602 | ####### Proceso de instalación de OpenGnsys |
|---|
| 1603 | ##################################################################### |
|---|
| 1604 | |
|---|
| 1605 | echoAndLog "OpenGnsys installation begins at $(date)" |
|---|
| 1606 | pushd $WORKDIR |
|---|
| 1607 | |
|---|
| 1608 | # Detectar datos iniciales de auto-configuración del instalador. |
|---|
| 1609 | autoConfigure |
|---|
| 1610 | |
|---|
| 1611 | # Detectar parámetros de red y comprobar si hay conexión. |
|---|
| 1612 | getNetworkSettings |
|---|
| 1613 | if [ $? -ne 0 ]; then |
|---|
| 1614 | errorAndLog "Error reading default network settings." |
|---|
| 1615 | exit 1 |
|---|
| 1616 | fi |
|---|
| 1617 | checkNetworkConnection |
|---|
| 1618 | if [ $? -ne 0 ]; then |
|---|
| 1619 | errorAndLog "Error connecting to server. Causes:" |
|---|
| 1620 | errorAndLog " - Network is unreachable, review devices parameters." |
|---|
| 1621 | errorAndLog " - You are inside a private network, configure the proxy service." |
|---|
| 1622 | errorAndLog " - Server is temporally down, try agian later." |
|---|
| 1623 | exit 1 |
|---|
| 1624 | fi |
|---|
| 1625 | |
|---|
| 1626 | # Detener servicios de OpenGnsys, si están activos previamente. |
|---|
| 1627 | [ -f /etc/init.d/opengnsys ] && /etc/init.d/opengnsys stop |
|---|
| 1628 | |
|---|
| 1629 | # Actualizar repositorios |
|---|
| 1630 | updatePackageList |
|---|
| 1631 | |
|---|
| 1632 | # Instalación de dependencias (paquetes de sistema operativo). |
|---|
| 1633 | declare -a notinstalled |
|---|
| 1634 | checkDependencies DEPENDENCIES notinstalled |
|---|
| 1635 | if [ $? -ne 0 ]; then |
|---|
| 1636 | installDependencies notinstalled |
|---|
| 1637 | if [ $? -ne 0 ]; then |
|---|
| 1638 | echoAndLog "Error while installing some dependeces, please verify your server installation before continue" |
|---|
| 1639 | exit 1 |
|---|
| 1640 | fi |
|---|
| 1641 | fi |
|---|
| 1642 | if [ -n "$INSTALLEXTRADEPS" ]; then |
|---|
| 1643 | echoAndLog "Installing extra dependencies" |
|---|
| 1644 | for (( i=0; i<${#INSTALLEXTRADEPS[*]}; i++ )); do |
|---|
| 1645 | eval ${INSTALLEXTRADEPS[i]} |
|---|
| 1646 | done |
|---|
| 1647 | fi |
|---|
| 1648 | |
|---|
| 1649 | # Detectar datos de auto-configuración después de instalar paquetes. |
|---|
| 1650 | autoConfigurePost |
|---|
| 1651 | |
|---|
| 1652 | # Arbol de directorios de OpenGnsys. |
|---|
| 1653 | createDirs ${INSTALL_TARGET} |
|---|
| 1654 | if [ $? -ne 0 ]; then |
|---|
| 1655 | errorAndLog "Error while creating directory paths!" |
|---|
| 1656 | exit 1 |
|---|
| 1657 | fi |
|---|
| 1658 | |
|---|
| 1659 | # Si es necesario, descarga el repositorio de código en directorio temporal |
|---|
| 1660 | if [ $REMOTE -eq 1 ]; then |
|---|
| 1661 | downloadCode $CODE_URL |
|---|
| 1662 | if [ $? -ne 0 ]; then |
|---|
| 1663 | errorAndLog "Error while getting code from the repository" |
|---|
| 1664 | exit 1 |
|---|
| 1665 | fi |
|---|
| 1666 | else |
|---|
| 1667 | ln -fs "$(dirname $PROGRAMDIR)" opengnsys |
|---|
| 1668 | fi |
|---|
| 1669 | |
|---|
| 1670 | # Compilar código fuente de los servicios de OpenGnsys. |
|---|
| 1671 | servicesCompilation |
|---|
| 1672 | if [ $? -ne 0 ]; then |
|---|
| 1673 | errorAndLog "Error while compiling OpenGnsys services" |
|---|
| 1674 | exit 1 |
|---|
| 1675 | fi |
|---|
| 1676 | |
|---|
| 1677 | # Copiar carpeta Interface entre administración y motor de clonación. |
|---|
| 1678 | copyInterfaceAdm |
|---|
| 1679 | if [ $? -ne 0 ]; then |
|---|
| 1680 | errorAndLog "Error while copying Administration Interface" |
|---|
| 1681 | exit 1 |
|---|
| 1682 | fi |
|---|
| 1683 | |
|---|
| 1684 | # Configuración de TFTP. |
|---|
| 1685 | tftpConfigure |
|---|
| 1686 | |
|---|
| 1687 | # Configuración de Samba. |
|---|
| 1688 | smbConfigure |
|---|
| 1689 | if [ $? -ne 0 ]; then |
|---|
| 1690 | errorAndLog "Error while configuring Samba server!" |
|---|
| 1691 | exit 1 |
|---|
| 1692 | fi |
|---|
| 1693 | |
|---|
| 1694 | # Configuración de Rsync. |
|---|
| 1695 | rsyncConfigure |
|---|
| 1696 | |
|---|
| 1697 | # Configuración ejemplo DHCP. |
|---|
| 1698 | dhcpConfigure |
|---|
| 1699 | if [ $? -ne 0 ]; then |
|---|
| 1700 | errorAndLog "Error while copying your dhcp server files!" |
|---|
| 1701 | exit 1 |
|---|
| 1702 | fi |
|---|
| 1703 | |
|---|
| 1704 | # Copiar ficheros de servicios OpenGnsys Server. |
|---|
| 1705 | copyServerFiles ${INSTALL_TARGET} |
|---|
| 1706 | if [ $? -ne 0 ]; then |
|---|
| 1707 | errorAndLog "Error while copying the server files!" |
|---|
| 1708 | exit 1 |
|---|
| 1709 | fi |
|---|
| 1710 | INSTVERSION=$(awk '{print $2}' $INSTALL_TARGET/doc/VERSION.txt) |
|---|
| 1711 | |
|---|
| 1712 | # Instalar base de datos de OpenGnsys Admin. |
|---|
| 1713 | isInArray notinstalled "mysql-server" || isInArray notinstalled "mariadb-server" |
|---|
| 1714 | if [ $? -eq 0 ]; then |
|---|
| 1715 | # Habilitar gestor de base de datos (MySQL, si falla, MariaDB). |
|---|
| 1716 | service=$MYSQLSERV |
|---|
| 1717 | $ENABLESERVICE |
|---|
| 1718 | if [ $? != 0 ]; then |
|---|
| 1719 | service=$MARIADBSERV |
|---|
| 1720 | $ENABLESERVICE |
|---|
| 1721 | fi |
|---|
| 1722 | # Activar gestor de base de datos. |
|---|
| 1723 | $STARTSERVICE |
|---|
| 1724 | # Asignar clave del usuario "root". |
|---|
| 1725 | mysqlSetRootPassword "${MYSQL_ROOT_PASSWORD}" |
|---|
| 1726 | else |
|---|
| 1727 | # Si ya está instalado el gestor de bases de datos, obtener clave de "root", |
|---|
| 1728 | mysqlGetRootPassword |
|---|
| 1729 | fi |
|---|
| 1730 | |
|---|
| 1731 | mysqlTestConnection "${MYSQL_ROOT_PASSWORD}" |
|---|
| 1732 | if [ $? -ne 0 ]; then |
|---|
| 1733 | errorAndLog "Error while connection to mysql" |
|---|
| 1734 | exit 1 |
|---|
| 1735 | fi |
|---|
| 1736 | mysqlDbExists ${OPENGNSYS_DATABASE} |
|---|
| 1737 | if [ $? -ne 0 ]; then |
|---|
| 1738 | echoAndLog "Creating Web Console database" |
|---|
| 1739 | mysqlCreateDb ${OPENGNSYS_DATABASE} |
|---|
| 1740 | if [ $? -ne 0 ]; then |
|---|
| 1741 | errorAndLog "Error while creating Web Console database" |
|---|
| 1742 | exit 1 |
|---|
| 1743 | fi |
|---|
| 1744 | else |
|---|
| 1745 | echoAndLog "Web Console database exists, ommiting creation" |
|---|
| 1746 | fi |
|---|
| 1747 | |
|---|
| 1748 | mysqlCheckUserExists ${OPENGNSYS_DB_USER} |
|---|
| 1749 | if [ $? -ne 0 ]; then |
|---|
| 1750 | echoAndLog "Creating user in database" |
|---|
| 1751 | mysqlCreateAdminUserToDb ${OPENGNSYS_DATABASE} ${OPENGNSYS_DB_USER} "${OPENGNSYS_DB_PASSWD}" |
|---|
| 1752 | if [ $? -ne 0 ]; then |
|---|
| 1753 | errorAndLog "Error while creating database user" |
|---|
| 1754 | exit 1 |
|---|
| 1755 | fi |
|---|
| 1756 | |
|---|
| 1757 | fi |
|---|
| 1758 | |
|---|
| 1759 | mysqlCheckDbIsEmpty ${OPENGNSYS_DATABASE} |
|---|
| 1760 | if [ $? -eq 0 ]; then |
|---|
| 1761 | echoAndLog "Creating tables..." |
|---|
| 1762 | if [ -f $WORKDIR/$OPENGNSYS_DB_CREATION_FILE ]; then |
|---|
| 1763 | mysqlImportSqlFileToDb ${OPENGNSYS_DATABASE} $WORKDIR/$OPENGNSYS_DB_CREATION_FILE |
|---|
| 1764 | else |
|---|
| 1765 | errorAndLog "Unable to locate $WORKDIR/$OPENGNSYS_DB_CREATION_FILE!!" |
|---|
| 1766 | exit 1 |
|---|
| 1767 | fi |
|---|
| 1768 | else |
|---|
| 1769 | # Si existe fichero ogBDAdmin-VersLocal-VersRepo.sql; aplicar cambios. |
|---|
| 1770 | REPOVERSION=$(awk '{print $2}' $WORKDIR/opengnsys/doc/VERSION.txt) |
|---|
| 1771 | OPENGNSYS_DB_UPDATE_FILE="opengnsys/admin/Database/$OPENGNSYS_DATABASE-$INSTVERSION-$REPOVERSION.sql" |
|---|
| 1772 | if [ -f $WORKDIR/$OPENGNSYS_DB_UPDATE_FILE ]; then |
|---|
| 1773 | echoAndLog "Updating tables from version $INSTVERSION to $REPOVERSION" |
|---|
| 1774 | mysqlImportSqlFileToDb ${OPENGNSYS_DATABASE} $WORKDIR/$OPENGNSYS_DB_UPDATE_FILE |
|---|
| 1775 | else |
|---|
| 1776 | echoAndLog "Database unchanged." |
|---|
| 1777 | fi |
|---|
| 1778 | fi |
|---|
| 1779 | # Eliminar fichero temporal con credenciales de acceso a MySQL. |
|---|
| 1780 | rm -f $TMPMYCNF |
|---|
| 1781 | |
|---|
| 1782 | # Copiando páqinas web. |
|---|
| 1783 | installWebFiles |
|---|
| 1784 | # Descargar/descomprimir archivos descargables. |
|---|
| 1785 | installDownloadableFiles |
|---|
| 1786 | # Generar páqinas web de documentación de la API |
|---|
| 1787 | makeDoxygenFiles |
|---|
| 1788 | |
|---|
| 1789 | # Creando configuración de Apache. |
|---|
| 1790 | installWebConsoleApacheConf $INSTALL_TARGET $APACHECFGDIR |
|---|
| 1791 | if [ $? -ne 0 ]; then |
|---|
| 1792 | errorAndLog "Error configuring Apache for OpenGnsys Admin" |
|---|
| 1793 | exit 1 |
|---|
| 1794 | fi |
|---|
| 1795 | |
|---|
| 1796 | popd |
|---|
| 1797 | |
|---|
| 1798 | # Crear la estructura de los accesos al servidor desde el cliente (shared) |
|---|
| 1799 | copyClientFiles |
|---|
| 1800 | if [ $? -ne 0 ]; then |
|---|
| 1801 | errorAndLog "Error creating client structure" |
|---|
| 1802 | fi |
|---|
| 1803 | |
|---|
| 1804 | # Crear la estructura del cliente de OpenGnsys. |
|---|
| 1805 | for i in $OGLIVE; do |
|---|
| 1806 | if ! clientCreate "$i"; then |
|---|
| 1807 | errorAndLog "Error creating client $i" |
|---|
| 1808 | exit 1 |
|---|
| 1809 | fi |
|---|
| 1810 | done |
|---|
| 1811 | |
|---|
| 1812 | # Configuración de servicios de OpenGnsys |
|---|
| 1813 | openGnsysConfigure |
|---|
| 1814 | |
|---|
| 1815 | # Mostrar sumario de la instalación e instrucciones de post-instalación. |
|---|
| 1816 | installationSummary |
|---|
| 1817 | |
|---|
| 1818 | #rm -rf $WORKDIR |
|---|
| 1819 | echoAndLog "OpenGnsys installation finished at $(date)" |
|---|
| 1820 | exit 0 |
|---|
| 1821 | |
|---|