Current File : //etc/rc6.d/K89stack-init
#!/bin/bash
#
# (c) 2014-2017 Ricardo J. Barberis para DonWeb by Dattatec.com
#
# stack-init   Script de inicializacion de Servidores CloudHosting
# chkconfig:   2345 11 99
# description: Script de inicializacion de Servidores CloudHosting.
#
### BEGIN INIT INFO
# Provides:          stack-init
# Required-Start:    $local_fs $network $syslog $networking
# Required-Stop:     $local_fs $network $syslog $networking
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Script de inicializacion de Servidores CloudHosting
# Description:       Script de inicializacion de Servidores CloudHosting
### END INIT INFO

PATH="/bin:/sbin:/usr/bin:/usr/sbin"
config_dir="/var/log/cloud"
config_file="${config_dir}/config_finished"

log_error() {
    echo -e "\n[ERROR]: $@" >&2
}

log_emit() {
    if [ $# -ne 0 ] ; then
        echo "[INFO]: $@"
    else
        while read linea ; do
            echo -e "[INFO]: ${linea}"
        done
    fi
}

case "$1" in
    start)

        trap 'exit_cleanup' EXIT INT

        exit_cleanup() {
            sleep 1
            [ -f /root/alta_cloud.sh ] && rm -f /root/alta_cloud.sh
        }

        # Esperamos a que levante IPv4
        for I in {1..5} ; do
            sleep 5
            IFACE=`grep ":" /proc/net/dev | grep -vw -e "lo" -e "sit0" | awk '{print $1}' | cut -d ":" -f 1 | sort | head -1`
            MYIP=`LANG=C ip -o -4 addr show ${IFACE} | awk '{print $4}' | cut -d "/" -f 1`
            MYNET=`echo "${MYIP}" | cut -d '.' -f -3`

            if [ -z "${MYIP}" -o "${MYNET}" == "192.168.122" ] ; then
                echo -e "\nIP no levantada, intento ${I} reintentando...\n"
                continue
            else
                break
            fi
        done

        if [ -z "${MYIP}" -o "${MYNET}" == "192.168.122" ] ; then
            echo -e "\nIP no levantada, saliendo sin configurar\n"
            exit 0
        fi

        curl --ipv4 --silent --show-error --output /usr/local/sbin/configurar_red_privada.sh.new https://ferozo.net/cloud_nova/configurar_red_privada.sh
        [ -s /usr/local/sbin/configurar_red_privada.sh.new ] &&
            grep -s -q -m1 -P "^\#\!" /usr/local/sbin/configurar_red_privada.sh.new &&
                mv -f /usr/local/sbin/configurar_red_privada.sh.new /usr/local/sbin/configurar_red_privada.sh
        chmod +x /usr/local/sbin/configurar_red_privada.sh
        /usr/local/sbin/configurar_red_privada.sh

        if [ -f "${config_file}" ] ; then
            echo -e "\nVPS ya configurado, agrandando particion raiz si hace falta y saliendo"
            if [ -e /dev/sda3 ] ; then
                echo -e "\nAgrandando particion 3"
                growpart --dry-run /dev/sda 3 >/dev/null && growpart /dev/sda 3 >/dev/null
            elif [ -e /dev/sda2 ] ; then
                echo -e "\nAgrandando particion 2"
                growpart --dry-run /dev/sda 2 >/dev/null && growpart /dev/sda 2 >/dev/null
            else
                echo -e "\nAgrandando particion 1"
                growpart --dry-run /dev/sda 1 >/dev/null && growpart /dev/sda 1 >/dev/null
            fi
            PART=`df / | awk '/^\/dev/ {print $1}'`
            BLOCKS_DISK=`LANG=C fdisk -lu | grep "${PART}" | awk '{print $4}'`
            BLOCKS_PART=`LANG=C df / | grep "^${PART}" | awk '{print $2}'`
            DIFF=$(( (${BLOCKS_DISK}-${BLOCKS_PART})/1000 ))

            if [ ${DIFF} -gt 500 ] ; then
                TYPE=`blkid -o export ${PART} | grep ^TYPE | cut -d = -f 2`
                if [ "${TYPE}" == "ext4" -o "${TYPE}" == "ext3" ] ; then
                    echo -e "\nAgrandando sistema de archivos ${TYPE}"
                    resize2fs -p ${PART}
                    tune2fs -r 1000 ${PART}
                elif [ "${TYPE}" == "xfs" ] ; then
                    echo -e "\nAgrandando sistema de archivos ${TYPE}"
                    xfs_growfs /
                else
                    echo -e "\nSistema de archivos desconocido"
                fi
            fi
            echo
            exit 0
        fi

        log_emit "Descargando user_data"
        URL_CONFIG="https://administracion.donweb.com/openstack/latest/user_data"
        for I in {1..5} ; do
            log_emit "- Intento numero ${I}"
            sleep 2
            curl --ipv4 --silent --show-error --output /root/alta_cloud.sh ${URL_CONFIG}
            if [ -s /root/alta_cloud.sh ] && grep -s -q -m1 -P "^\#\!" /root/alta_cloud.sh ; then
                break
            fi
        done

        if [ -s /root/alta_cloud.sh ] && grep -s -q -m1 -P "^\#\!" /root/alta_cloud.sh ; then
            log_emit "Configurando"
            chmod +x /root/alta_cloud.sh &&
            /root/alta_cloud.sh &&
            mkdir -pv "${config_dir}" &&
            date "+%s" > "${config_file}" &&
            ip addr show ${IFACE} | awk '/ether/{print $2}' >> "${config_file}" &&
            rm -vf /root/alta_cloud.sh &&
            reboot
        else
            log_error "No se pudo descargar user_data"
            rm -vf /root/alta_cloud.sh
        fi
        ;;

    stop)
        log_emit "Stopping $0..."
        ;;
esac

exit $?