Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
2672 rexy 1
#!/bin/bash
2
 
3
#######################
4
###### Variables ######
5
################################################################################
6
 
7
# Script variables
8
CURRENT_DIR="$(readlink -f "$(dirname $0)")"
9
RESSOURCES="$CURRENT_DIR/ressources"
10
SCRIPTS_DIR="$CURRENT_DIR/scripts"
11
SCRIPTS_TMP_DIR="$CURRENT_DIR/scripts_tmp"
12
BUILDER_SCRIPT="build-image.sh"
13
LOG_FILE="${CURRENT_DIR}/mageiarBuilder.log"
14
 
15
 
16
#######################
17
###### Functions ######
18
################################################################################
19
# Print the given error message and exit 1
20
function errorExit()
21
{
22
    tput bold
23
    echo -e "ERROR : $1 \nAborting\n" >&2
24
    echo "$1" > "$LOG_FILE"
25
    tput sgr0
26
    exit 1
27
}
28
#-------------------------------------------------------------------------------
29
function printBold()
30
{
31
    tput bold 2>/dev/null
32
    echo $1
33
    tput sgr0 2>/dev/null
34
}
35
#######################
36
######   Main    ######
37
################################################################################
38
 
39
# Check root
40
[[ $(whoami) != 'root' ]] && errorExit 'Please run as root'
41
 
42
# Check options
43
while getopts "a:h" option; do
44
    case $option in
45
        a) # Alcasar image
46
            alcasarTar="$(echo $OPTARG | rev | cut -d '/' -f 1 | rev)"
47
            [[ ! -f "$OPTARG" ]] && errorExit "could not find $OPTARG"
48
            # Copy the image in the ressource directory to be accessible from the docker
49
            printBold "Copying $OPTARG" in $RESSOURCES
50
            rsync -usP "$OPTARG" "$RESSOURCES/$alcasarTar" || \
51
            errorExit "could not copy $OPTARG to $RESSOURCES"
52
 
53
        ;;
54
        *)
55
            echo "Usage : buildMageiar [-a alcasar.tar.gz]"
56
            exit 1
57
        ;;
58
    esac
59
done
60
 
61
# Check if auto_inst.cfg.pl is present
62
[[ ! -f "$RESSOURCES/auto_inst.cfg.pl_template" ]] && errorExit "No auto_inst.cfg.pl_template file in $RESSOURCES"
63
 
64
#-------------------------------------------------------------------------------
65
# If needed, download Alcasar image
66
#-------------------------------------------------------------------------------
67
 
68
if [[ -z $alcasarTar ]]; then
69
    latestAlcasarVersion="-$(dig version.alcasar.net txt | \
70
        grep "ANSWER SECTION" -A 1 | \
71
        grep version | cut -d '"' -f2 || \
72
        errorExit "could not retrieve Alcasar latest version")"
73
    alcasarTar="alcasar$latestAlcasarVersion.tar.gz"
74
 
75
    printBold "Downloading Alcasar image"
76
    wget -O "$RESSOURCES/$alcasarTar" "ftp.alcasar.net:/pub/stable/$alcasarTar" || errorExit 'could not download Alcasar'
77
fi
78
 
79
#
80
# If Mageia image isn't provided here, it will be downloaded in the docker.
81
# This is because the version has to match the one in the docker
82
#
83
 
84
#-------------------------------------------------------------------------------
85
# Run docker image
86
#-------------------------------------------------------------------------------
87
systemctl start docker || errorExit 'could not start docker service'
88
 
89
# Copy script folder to temporary one
90
cp -r "$SCRIPTS_DIR" "$SCRIPTS_TMP_DIR"
91
 
92
 
93
printBold "Running mageia docker"
94
docker run --privileged --rm --volume "$RESSOURCES":/var/iso --volume "$SCRIPTS_TMP_DIR":/root/ -it mageia "/root/$BUILDER_SCRIPT" "$alcasarTar"
95
 
96
[[ -d "$SCRIPTS_TMP_DIR" ]] && rm -Rf "$SCRIPTS_TMP_DIR" 2>/dev/null
97
 
98
# Retrieve created iso image
99
mv "$RESSOURCES"/Mageia*Alcasar* "$CURRENT_DIR" 2>/dev/null
100
 
101
# Give it to SUDO_USER if exists
102
[[ ! -z $SUDO_USER ]] && chown -R $SUDO_USER "$CURRENT_DIR"/* 2>/dev/null
103
 
104
#-------------------------------------------------------------------------------
105
# Stopping
106
#-------------------------------------------------------------------------------
107
read -p 'Stop docker service ? [Y/n] : ' ynAnswer
108
[[ ! $ynAnswer = [nN] ]] && systemctl stop docker
109
#-------------------------------------------------------------------------------