Subversion Repositories ALCASAR

Rev

Rev 2672 | Blame | Last modification | View Log

# Mageia (Mageia + ALCASAR) ISO builder
# By Aurélien DUBOIS 

#!/bin/bash

#######################
###### Variables ######
################################################################################

# Script variables
CURRENT_DIR="$(readlink -f "$(dirname $0)")"
RESSOURCES="$CURRENT_DIR/ressources"
SCRIPTS_DIR="$CURRENT_DIR/scripts"
SCRIPTS_TMP_DIR="$CURRENT_DIR/scripts_tmp"
BUILDER_SCRIPT="build-image.sh"
LOG_FILE="${CURRENT_DIR}/mageiarBuilder.log"


#######################
###### Functions ######
################################################################################
# Print the given error message and exit 1
function errorExit()
{
    tput bold
    echo -e "ERROR : $1 \nAborting\n" >&2
    echo "$1" > "$LOG_FILE"
    tput sgr0
    exit 1
}
#-------------------------------------------------------------------------------
function printBold()
{
    tput bold 2>/dev/null
    echo $1
    tput sgr0 2>/dev/null
}
#######################
######   Main    ######
################################################################################

# Check root
[[ $(whoami) != 'root' ]] && errorExit 'Please run as root'

# Check options
while getopts "a:h" option; do
    case $option in
        a) # Alcasar image
            alcasarTar="$(echo $OPTARG | rev | cut -d '/' -f 1 | rev)"
            [[ ! -f "$OPTARG" ]] && errorExit "could not find $OPTARG"
            # Copy the image in the ressource directory to be accessible from the docker
            printBold "Copying $OPTARG" in $RESSOURCES
            rsync -usP "$OPTARG" "$RESSOURCES/$alcasarTar" || \
            errorExit "could not copy $OPTARG to $RESSOURCES"

        ;;
        *)
            echo "Usage : buildMageiar [-a alcasar.tar.gz]"
            exit 1
        ;;
    esac
done

# Check if auto_inst.cfg.pl is present
[[ ! -f "$RESSOURCES/auto_inst.cfg.pl_template" ]] && errorExit "No auto_inst.cfg.pl_template file in $RESSOURCES"

#-------------------------------------------------------------------------------
# If needed, download Alcasar image
#-------------------------------------------------------------------------------

if [[ -z $alcasarTar ]]; then
    latestAlcasarVersion="-$(dig version.alcasar.net txt | \
        grep "ANSWER SECTION" -A 1 | \
        grep version | cut -d '"' -f2 || \
        errorExit "could not retrieve Alcasar latest version")"
    alcasarTar="alcasar$latestAlcasarVersion.tar.gz"

    printBold "Downloading Alcasar image"
    wget -O "$RESSOURCES/$alcasarTar" "ftp.alcasar.net:/pub/stable/$alcasarTar" || errorExit 'could not download Alcasar'
fi

#
# If Mageia image isn't provided here, it will be downloaded in the docker.
# This is because the version has to match the one in the docker
#

#-------------------------------------------------------------------------------
# Run docker image
#-------------------------------------------------------------------------------
systemctl start docker || errorExit 'could not start docker service'

# Copy script folder to temporary one
cp -r "$SCRIPTS_DIR" "$SCRIPTS_TMP_DIR"


printBold "Running mageia docker"
docker run --privileged --rm --volume "$RESSOURCES":/var/iso --volume "$SCRIPTS_TMP_DIR":/root/ -it mageia "/root/$BUILDER_SCRIPT" "$alcasarTar"

[[ -d "$SCRIPTS_TMP_DIR" ]] && rm -Rf "$SCRIPTS_TMP_DIR" 2>/dev/null

# Retrieve created iso image
mv "$RESSOURCES"/Mageia*Alcasar* "$CURRENT_DIR" 2>/dev/null

# Give it to SUDO_USER if exists
[[ ! -z $SUDO_USER ]] && chown -R $SUDO_USER "$CURRENT_DIR"/* 2>/dev/null

#-------------------------------------------------------------------------------
# Stopping
#-------------------------------------------------------------------------------
read -p 'Stop docker service ? [Y/n] : ' ynAnswer
[[ ! $ynAnswer = [nN] ]] && systemctl stop docker
#-------------------------------------------------------------------------------

Generated by GNU Enscript 1.6.6.