You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

124 lines
2.7 KiB

#!/bin/bash
## compose: no-hooks
if [ -z "$SERVICE_DATASTORE" ]; then
echo "This script is meant to be run through 'compose' to work properly." >&2
exit 1
fi
version=0.1
usage="$exname [-h|--help] [--force|-f] [DBNAME]"
help="
USAGE:
$usage
DESCRIPTION:
Outputs to stdout the odoo zip dump of DBNAME. If DBNAME is not
provided, it'll take the default odoo database from the
${DARKCYAN}postgres-database${NORMAL} relation of current service.
EXAMPLES:
$exname > dump.zip
$exname odoo2 > odoo2.zip
"
dbname=
output=
while [ "$1" ]; do
case "$1" in
"--help"|"-h")
print_help >&2
exit 0
;;
"--force"|"-f")
force=yes
;;
--*|-*)
err "Unexpected optional argument '$1'"
print_usage >&2
exit 1
;;
*)
[ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; }
err "Unexpected positional argument '$1'"
print_usage >&2
exit 1
;;
esac
shift
done
if [ -z "$dbname" ]; then
dbname=$(relation:get "$SERVICE_NAME:postgres-database" dbname) || {
err "Couldn't retrieve information of" \
"${DARKYELLOW}$SERVICE_NAME${NORMAL}-->${DARKCYAN}postgres-database${NORMAL}."
exit 1
}
fi
. $CHARM_PATH/lib/common
set -e
ADMIN_PASSWORD=$(odoo:get-admin-password) || {
err "Couldn't retrieve admin password for $SERVICE_NAME."
exit 1
}
container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 8069 4) || {
err "Please ensure that $DARKYELLOW$SERVICE_NAME$NORMAL is running before using '$exname'."
exit 1
}
container_ip=${container_network_ip##*:}
container_network=${container_network_ip%%:*}
DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
check_output() {
local chars
read -n 2 -r chars
if [ "$chars" != "PK" ]; then
out=$(cat)
errmsg=$(echo "$out" | grep "alert-danger")
errmsg=${errmsg#*>}
errmsg=${errmsg%%<*}
if [ -n "$errmsg" ]; then
errmsg=$(echo "$errmsg" | recode html..utf8)
die "$errmsg"
fi
err "Unexpected output not matching ZIP signature. Dump probably failed."
exit 1
fi
{
echo -n "$chars"
cat
}
}
cmd=(
docker run --rm --network "$container_network"
"$DEFAULT_CURL_IMAGE"
-sS
-X POST
-F "master_pwd=${ADMIN_PASSWORD}"
-F "name=${dbname}"
-F "backup_format=zip"
http://${container_ip}:8069/web/database/backup
)
## XXXvlab: contains password, left only for advanced debug
#debug "${cmd[@]}"
set -o pipefail
"${cmd[@]}" | check_output &&
info "Requested odoo '$dbname' dump and outputted result to stdout."