fabrica/.packer/script/onepublish

132 lines
2.9 KiB
Bash
Executable File

#!/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