Adding Chapter 1 : Packer will build for you

This commit is contained in:
2021-03-18 16:11:35 +01:00
commit 02e13dfb00
97 changed files with 4630 additions and 0 deletions

31
packer/script/build Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env bash
set -eo pipefail
PACKER_TEMPLATE=$1
shift
if [ -f '.env' ]; then
set -a
source .env
set +a
fi
export PACKER_KEY_INTERVAL=10ms
export PATH="$PATH:.bin"
function usage {
cat<<EOF
Usage: $0 </chemin/vers/template/packer>
Construit l'image correspondante au template Packer donné
EOF
exit 1
}
[ -z "${PACKER_TEMPLATE}" ] && usage
# Validate template
packer validate ${PACKER_TEMPLATE}
[[ $? -ne 0 ]] && exit 123
packer build $@ ${PACKER_TEMPLATE}

9
packer/script/clean Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -eo pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null && pwd )"
rm -rf "${DIR}/../images"
rm -rf "${DIR}/../.bin"
rm -rf "${DIR}/../packer_cache"

37
packer/script/install Executable file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env bash
#set -eo pipefail
set -o pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null && pwd )"
sparsify=$(which virt-sparsify)
if [ $? -ne 0 ]
then
echo "!!!"
echo "!!! Warning : virt-sparsify is not present, you may encounter build issues with cadoles recipes !"
echo "!!! Please install libguestfs if you want to build cadoles production images"
echo "!!!"
fi
# Install a local copy of packer
PACKER_VERSION=1.6.6
PACKER_ARCH=amd64
#PACKER_ARCHIVE_SHA256SUM=30da8dab9c526a6d15b037e2234f6f12cf3accfad77eb2c130738ec1a54cab6d
#PACKER_ARCHIVE_SHA256SUM=a678c995cb8dc232db3353881723793da5acc15857a807d96c52e96e671309d9
PACKER_ARCHIVE_SHA256SUM=721d119fd70e38d6f2b4ccd8a39daf6b4d36bf5f7640036acafcaaa967b00c3b
wget -O- https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_${PACKER_ARCH}.zip > packer.zip
echo "${PACKER_ARCHIVE_SHA256SUM} packer.zip" | sha256sum -c
mkdir -p "${DIR}/../.bin"
unzip -o -d "${DIR}/../.bin" packer.zip
rm -f packer.zip
# Cadoles Packer plugins
# OpenNebula - Post-Processor Image Template
PLUGIN_ARCHIVE=https://forge.cadoles.com/Cadoles/packer-opennebula/releases/download/4fa5cbc782/packer-post-processor-opennebula-image-template-rev.4fa5cbc-linux-amd64.tar.gz
wget -O- "$PLUGIN_ARCHIVE" > plugin.tar.gz
tar -C "$DIR/../.bin" --strip=1 --wildcards -xzf plugin.tar.gz --wildcards packer-*/packer-post-processor-opennebula-image-template
rm -f plugin.tar.gz

131
packer/script/onepublish Executable file
View File

@ -0,0 +1,131 @@
#!/bin/bash
#
# Publish builed images to OpenNebula
# Based on the work of "Julien Marchetti" from here : https://dev-eole.ac-dijon.fr/projects/packer/repository
#
set -eo pipefail
function help ()
{
cat <<EO
Usage : ${0} [options]
Add images to OpenNebula (market or datastore) using XMLRPC.
Options:
EO
cat <<EO | column -s\& -t
-h & print help message
-u [user] & user with correct privileges to push images into OpenNebula or OpenNebula market
-U [url] & URL du service XMLRPC OpenNebula (http://127.0.0.1:2633/RPC2)
-n [name] & Name for the image
-p [path] & Path of the image to upload
-t [type] & Image type (OS,CDROM)
-d [id|name] & Datastore id or name
-m [id|name] & Market id or name
-M [mode] & Tell the script what to do (datastore,market,all)
datastore: publish only in the datastore
market: publish only in the market
all: publish in datastore and in market
EO
}
function errorMsg(){
echo
echo "ERROR: $1"
echo
[[ -n ${3} ]] && help
exit $2
}
#
# Publish an image to the datastore
#
function datastorePush()
{
[[ -z ${datastore} ]] && errorMsg "You must specify a datastore id" 3 "help"
local cmd="oneimage"
local act="create"
local opt="--name ${name}"
opt="${opt} --path ${path}"
opt="${opt} --type ${itype}"
[[ -n ${user} ]] && opt="${opt} --user '${user}'"
opt="${opt} --driver ${driver}"
opt="${opt} --prefix ${prefix}"
opt="${opt} --endpoint ${url}"
opt="${opt} -d ${datastore}"
opt="${opt} -v"
${cmd} ${act} ${opt} --description "${desc}"
return ${?}
}
function marketPush()
{
[[ -z ${datastore} ]] && errorMsg "You must specify a datastore id" 3 "help"
[[ -z ${market} ]] && errorMsg "You must specify a market id" 3 "help"
ENDING=${1}
cmd="onemarketapp"
action="create"
file=$(mktemp)
datastorePush
echo "TEMPLATE" >> ${file}
echo ${cmd} ${action} ${file}
rm ${file}
if [[ ${ENDING} == "CLEAN" ]]
then
echo "MUST CLEAN IMAGE FROM DATASTORE"
fi
return 0
}
[ $# -eq 0 ] && help && exit 1
while getopts ":hU:u:n:D:p:t:P:d:m:M:" opt; do
case $opt in
U) url="${OPTARG}" >&2 ;;
u) user="${OPTARG}" >&2 ;;
n) name="${OPTARG}" >&2 ;;
D) desc="${OPTARG}" >&2 ;;
p) path="${OPTARG}" >&2 ;;
t) itype="${OPTARG}" >&2 ;;
P) prefix="${OPTARG}" >&2 ;;
d) datastore="${OPTARG}" >&2 ;;
m) market="${OPTARG}" >&2 ;;
M) mode="${OPTARG}" >&2 ;;
\?)
echo "Invalid option : -'${OPTARG}'" >&2
help
;;
h) help ; exit >&2 ;;
esac
done
[[ -z ${mode} ]] && errorMsg "Missing option -M" 2 "help"
[[ -z ${path} ]] && errorMsg "Missing option -p" 2 "help"
[[ -z ${name} ]] && name=$(basename ${path})
[[ -z ${desc} ]] && desc="Image ${name} ..."
[[ -z ${itype} ]] && itype="OS"
[[ -z ${prefix} ]] && prefix="vd"
[[ -z ${driver} ]] && driver="qcow2"
[[ -z ${url} ]] && url="http://127.0.0.1:2633/XMLRPC2"
case ${mode} in
all)
marketPush "KEEP"
;;
datastore) datastorePush ;;
market)
marketPush "CLEAN" ;;
esac

55
packer/script/run Executable file
View File

@ -0,0 +1,55 @@
#!/usr/bin/env bash
set -eo pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null && pwd )"
ORIG_DISK_IMAGE=$1
function usage {
cat<<EOF
Usage: $0 </chemin/vers/image_disque>
Éxecute l'image disque donnée avec Qemu
EOF
exit 1
}
function find_available_port {
read lower_port upper_port < /proc/sys/net/ipv4/ip_local_port_range
while :; do
for (( port = lower_port ; port <= upper_port ; port++ )); do
(echo > /dev/tcp/0.0.0.0/$port) 2>/dev/null >/dev/null
if [ $? -ne 0 ]; then
echo $port
return
fi
done
done
}
[ -z "$ORIG_DISK_IMAGE" ] && usage
DISK_IMAGE_DIR="$DIR/../.run/$(dirname $ORIG_DISK_IMAGE)"
ORIG_DISK_IMAGE=$(readlink -f "$ORIG_DISK_IMAGE")
DISK_IMAGE_NAME=$(date -r "$ORIG_DISK_IMAGE" | shasum - | cut -f1 -d ' ').img
DISK_IMAGE="$DISK_IMAGE_DIR/$DISK_IMAGE_NAME"
if [ -f "$DISK_IMAGE" ]; then
echo "Using existing qcow2 image '$DISK_IMAGE'..."
else
mkdir -p "$(dirname $DISK_IMAGE)"
rm -f "$DISK_IMAGE_DIR"/*.img
cd "$(dirname $DISK_IMAGE)"
echo "Generating qcow2 image '$DISK_IMAGE'..."
qemu-img create -f qcow2 -b "$ORIG_DISK_IMAGE" "$(basename $DISK_IMAGE)"
cd -
fi
SSH_PORT=$(find_available_port)
echo "Starting VM... SSH will be available on '127.0.0.1:${SSH_PORT}'"
qemu-system-x86_64 -boot once=d \
-device virtio-net,netdev=user.0 \
-m 1024M -machine type=pc,accel=kvm \
-display sdl \
-hda "${DISK_IMAGE}" \
-netdev user,id=user.0,hostfwd=tcp::${SSH_PORT}-:22