eole-one-master/postservice/99-init-hapy-master

159 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
[ $(CreoleGet hapy_init_master) == non ] && exit 0
function error()
{
MSG=${1}
echo "[ERROR][${MSG}]"
exit 1
}
function get_user_home()
{
HOMEDIR=$(getent passwd ${1} | cut -d ':' -f 6)
echo ${HOMEDIR}
}
function get_one_auth()
{
CMD="cat"
HOME=$(get_user_home oneadmin)
FILE="${HOME}/.one/one_auth"
${CMD} ${FILE}
}
function runOneCmd()
{
local cmd=${1}
local opt=${2}
local auth=$(get_one_auth)
$cmd ${opt} --user ${auth%:*} --password ${auth#*:}
return ${?}
}
if [ $(CreoleGet activer_proxy_client 'non') = 'oui' ]
then
PROXY_SERVER=$(CreoleGet proxy_client_adresse)
PROXY_PORT=$(CreoleGet proxy_client_port)
PROXY_OPT="--proxy http://${PROXY_SERVER}:${PROXY_PORT}"
fi
DISKSIZE=$(CreoleGet hapy_init_master_disk_size)
DISKNAME="DSK-${DISKSIZE}-Go"
DISKDESC="Disque vide de ${DISKSIZE} Go"
VERSIONISO=$(CreoleGet hapy_init_master_iso_version)
MAJORVERSION=${VERSIONISO:0:3}
POINT_RELEASE=$(eole-iso ${PROXY_OPT} --release ${VERSIONISO} get-last-point-release)
ISONAME=$(eole-iso ${PROXY_OPT} --release ${VERSIONISO} get-iso-name)
ISODESC="Image ISO EOLE ${POINT_RELEASE} 64 bits"
ISOURL=$(eole-iso ${PROXY_OPT} --release ${VERSIONISO} get-iso-url)
TMPLNAME="install-eole-${POINT_RELEASE}-amd64"
TMPLDESC="Modèle de VM permettant d'installer un module EOLE ${POINT_RELEASE} 64 bits (non persistente)"
#echo "########### suppr ${DISKNAME} ###############"
#oneimage delete ${DISKNAME}
#echo
#echo "########### suppr ${ISONAME} ###################"
#oneimage delete ${ISONAME}
#echo
#echo "########### suppr ${TMPLNAME} ###################"
#onetemplate delete ${TMPLNAME}
#echo
#*****************************************************
# DISQUE VIDE
#*****************************************************
if ! runOneCmd "oneimage" "show ${DISKNAME}" &>/dev/null
then
echo "***** Création de ${DISKNAME} *****"
## Attention oneimage create --datastore ne fonctionne pas dans runOneCmd ...
auth=$(get_one_auth)
oneimage create --datastore $(CreoleGet one_ds_image_name) \
--name ${DISKNAME} --type DATABLOCK --description "${DISKDESC}" \
--size $((${DISKSIZE}*1024)) --prefix vd --driver qcow2 --user ${auth%:*} --password ${auth#*:}
runOneCmd "oneimage" "chtype ${DISKNAME} OS"
else
echo "***** ${DISKNAME} existe déjà *****"
fi
echo
#*****************************************************
# Image ISO EOLE Latest Stable
#*****************************************************
if ! runOneCmd "oneimage" "show ${ISONAME}" &>/dev/null
then
echo -ne "***** Création de l'image ${ISONAME} *****\n\n"
FILE=$(mktemp)
cat > ${FILE} <<EOF
NAME="${ISONAME}"
TYPE="CDROM"
DESCRIPTION="${ISODESC}"
DEV_PREFIX="hd"
PATH="${ISOURL}"
PERSISTENT="No"
EOF
runOneCmd "oneimage" "create -d $(CreoleGet one_ds_iso_name) ${FILE}"
/bin/rm -f ${FILE}
spin='-\|/'
i=0
while :
do
imgState=$(runOneCmd "oneimage" "show ${ISONAME}" | awk '{if ($1 == "STATE") {print $3}}')
if [[ ${imgState} == "lock" ]]
then
i=$(( (i+1) %4 ))
printf "\rInitialisation en cours, merci de patienter [${spin:$i:1}] "
sleep 2
else
printf "\r Création terminée, l'image a le statut : ${imgState}\n"
break
fi
done
rm -f "/tmp/${ISONAME}"
else
echo "***** ${ISONAME} existe déjà *****"
fi
echo
#*****************************************************
# Template EOLEBASE
#*****************************************************
if ! onetemplate show "${TMPLNAME}" &>/dev/null
then
echo "***** Création de ${TMPLNAME} *****"
FILE=$(mktemp)
cat > ${FILE} <<EOF
NAME="${TMPLNAME}"
DESCRIPTION="${TMPLDESC}"
INPUT=[BUS="usb",TYPE="tablet"]
CPU="0.2"
DISK=[DEV_PREFIX="vd",IMAGE_UNAME="oneadmin",IMAGE="${DISKNAME}",DRIVER="qcow2"]
DISK=[DEV_PREFIX="hd",IMAGE_UNAME="oneadmin",IMAGE="${ISONAME}",DRIVER="raw"]
FEATURES=[ACPI="yes",PAE="no"]
VCPU="1"
GRAPHICS=[LISTEN="0.0.0.0",TYPE="vnc",KEYMAP="fr"]
OS = [ ARCH = "x86_64", BOOT = "disk0,disk1" ]
MEMORY="1024"
EOF
#NIC=[MODEL="virtio",NETWORK="CR_eole"]
runOneCmd "onetemplate" "create ${FILE}"
/bin/rm -f ${FILE}
else
echo "***** ${TMPLNAME} existe déjà *****"
fi
echo
exit 0