Browse Source

new: [vps] add ``cyclos {dump,restore,set-root-url}`` commands

Signed-off-by: Valentin Lab <valentin.lab@kalysto.org>
rc1
Valentin Lab 3 years ago
parent
commit
c58d681e48
  1. 156
      bin/vps

156
bin/vps

@ -495,6 +495,39 @@ mailcow:get_default_backup_host_ident() {
echo "$dest $ident" echo "$dest $ident"
} }
compose:get_url() {
local service="$1"
(
set -o pipefail
cat "/var/lib/compose/relations/myc/${service}-frontend/web-proxy/data" |
shyaml get-value url
) || {
err "Failed querying ${service} to frontend relation to get url."
return 1
}
}
export -f compose:get_url
compose:psql() {
local dbname="$1"
docker exec -i myc_postgres_1 psql -U postgres "$dbname"
}
export -f compose:psql
cyclos:set_root_url() {
local dbname="$1" cyclos_service="$2" url
url=$(compose:get_url "${cyclos_service}") || return 1
echo "UPDATE configurations SET root_url = '$url';" |
compose:psql "$dbname" || {
err "Failed to set cyclos url value in '$dbname' database."
return 1
}
}
export -f cyclos:set_root_url
compose:get_cron_docker_cmd() { compose:get_cron_docker_cmd() {
local cron_line cmd_line docker_cmd local cron_line cmd_line docker_cmd
@ -983,22 +1016,127 @@ cmdline.spec:odoo:cmd:set-cyclos-url:run() {
dbname=${opt_database:-odoo} dbname=${opt_database:-odoo}
cyclos_service="${opt_service:-cyclos}" cyclos_service="${opt_service:-cyclos}"
URL=$(
set -o pipefail
cat /var/lib/compose/relations/myc/${cyclos_service}-frontend/web-proxy/data |
shyaml get-value url
) || {
err "Failed querying ${cyclos_service} to frontend relation to get url."
URL=$(compose:get_url "${cyclos_service}") || exit 1
echo "UPDATE res_company SET cyclos_server_url = '$URL/api' WHERE id=1;" |
compose:psql "$dbname" || {
err "Failed to set cyclos url value in '$dbname' database."
exit 1 exit 1
} }
echo "UPDATE res_company SET cyclos_server_url = '$URL' WHERE id=1;" |
docker exec -i myc_postgres_1 psql -U postgres "$dbname" || {
err "Failed to set cyclos url value in '$dbname' database."
}
cmdline.spec.gnu cyclos
cmdline.spec::cmd:cyclos:run() {
:
}
cmdline.spec:cyclos:cmd:dump:run() {
: :posarg: DUMP_GZFILE 'Target path to store odoo dump gz file.'
: :optval: --database,-d "Target database ('cyclos' if not specified)"
: :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
cyclos_service="${opt_service:-cyclos}"
cyclos_database="${opt_database:-cyclos}"
Wrap -d "stop container 'myc_${cyclos_service}_1'" -- \
docker stop "myc_${cyclos_service}_1" || exit 1
Wrap -d "Dump postgres database '${cyclos_database}'." -- \
pgm cp "$cyclos_database" "$DUMP_GZFILE" || exit 1
Wrap -d "start container 'myc_${cyclos_service}_1'." -- \
docker start "myc_${cyclos_service}_1" || exit 1
}
cmdline.spec.gnu restore
cmdline.spec:cyclos:cmd:restore:run() {
: :posarg: GZ_DUMP_LOCATION 'Source cyclos dump file to restore
(can be a local file or an url)'
: :optval: --service,-s "The service (defaults to 'cyclos')"
: :optval: --database,-d 'Target database (default if not specified)'
local out
cyclos_service="${opt_service:-cyclos}"
cyclos_database="${opt_database:-cyclos}"
if [[ "$GZ_DUMP_LOCATION" == "http://"* ]]; then
settmpdir GZ_TMP_LOCATION
tmp_location="$GZ_TMP_LOCATION/dump.gz"
Wrap -d "get '$GZ_DUMP_LOCATION'" <<EOF
curl -k -s -L "$GZ_DUMP_LOCATION" > "$tmp_location" || exit 1
if [[ "\$(dd if="$tmp_location" count=2 bs=1 2>/dev/null |
hexdump -v -e "/1 \"%02x\"")" != "1f8b" ]]; then
err "Download doesn't seem to be a gzip file."
dd if="$tmp_location" count=1 bs=256 | hd | prefix " ${GRAY}|${NORMAL} " >&2
exit 1
fi
EOF
GZ_DUMP_LOCATION="$tmp_location"
fi
[ -e "$GZ_DUMP_LOCATION" ] || {
err "No file '$GZ_DUMP_LOCATION' found." >&2
exit 1 exit 1
} }
Wrap -d "stop container 'myc_${cyclos_service}_1'" -- \
docker stop "myc_${cyclos_service}_1" || exit 1
## XXXvlab: making the assumption that the postgres username should
## be the same as the cyclos service selected (which is the default,
## but not always the case).
Wrap -d "restore postgres database '${cyclos_database}'." -- \
pgm cp -f "$GZ_DUMP_LOCATION" "${cyclos_service}@${cyclos_database}" || exit 1
## ensure that the database is not locked
## XXXvlab: 70 is uid of user postgres, this avoids the docker bug
## but introduce hardwritten value
Wrap -d "check and remove database lock if any" <<EOF
echo "delete from database_lock;" | compose:psql "${cyclos_database}" || exit 1
EOF
Wrap -d "set root url" -- \
cyclos:set_root_url "$cyclos_database" "$cyclos_service" || exit 1
Wrap -d "start container 'myc_${cyclos_service}_1'." -- \
docker start "myc_${cyclos_service}_1" || exit 1
}
cmdline.spec.gnu set-root-url
cmdline.spec:cyclos:cmd:set-root-url:run() {
: :optval: --database,-d "Target database ('cyclos' if not specified)"
: :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
local URL
cyclos_database=${opt_database:-cyclos}
cyclos_service="${opt_service:-cyclos}"
Wrap -d "stop container 'myc_${cyclos_service}_1'" -- \
docker stop "myc_${cyclos_service}_1" || exit 1
Wrap -d "set root url" -- \
cyclos:set_root_url "$cyclos_database" "$cyclos_service" || exit 1
Wrap -d "start container 'myc_${cyclos_service}_1'." -- \
docker start "myc_${cyclos_service}_1" || exit 1
} }
cmdline::parse "$@" cmdline::parse "$@"
Loading…
Cancel
Save