From 5b529de8dd9d591cd588d27db7ceec7e6d19b674 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Thu, 2 May 2024 15:55:22 +0200 Subject: [PATCH] new: [odoo-tecnativa] add ``update`` action --- odoo-tecnativa/actions/update | 89 +++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 odoo-tecnativa/actions/update diff --git a/odoo-tecnativa/actions/update b/odoo-tecnativa/actions/update new file mode 100755 index 0000000..4a1f9b4 --- /dev/null +++ b/odoo-tecnativa/actions/update @@ -0,0 +1,89 @@ +#!/bin/bash + +## Load action gets a first argument a DIRECTORY holding the necessary files. +## +## + +if [ -z "$SERVICE_DATASTORE" ]; then + echo "This script is meant to be run through 'compose' to work properly." >&2 + exit 1 +fi + +usage="$exname [-h|--help] DBNAME [MODULE ...]" + +dbname= +modules=() +while [ "$1" ]; do + case "$1" in + "--help"|"-h") + print_usage + exit 0 + ;; + *) + [ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; } + modules+=("$1") + ;; + esac + shift +done + +[ "${modules[*]}" ] || modules=("all") + +modules="$(echo "${modules[@]}" | tr " " ",")" + +## This can work only if ~/.my.cnf is correctly created by init. + +if [ -z "$dbname" ]; then + + ## + ## Fetch default dbname in relation to postgres-database + ## + + ## XXXvlab: can't get real config here + if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "postgres-database"); then + err "Couldn't find relation ${DARKCYAN}postgres-database${NORMAL}." + exit 1 + fi + + relation_file=$(get_relation_data_dir "$SERVICE_NAME" "$ts" "postgres-database") || { + err "Failed to find relation file" + exit 1 + } + + postgres_config=$(cat "$relation_file"/data) || exit 2 + + dbname="$(e "$postgres_config" | shyaml get-value dbname)" || { + err "Couldn't retrieve information of ${DARKCYAN}postgres-database${NORMAL}'s relation." + exit 1 + } +fi + +running_containers=($(get_running_containers_for_service "$SERVICE_NAME")) || { + err "Failed to get running containers for service $DARKYELLOW$SERVICE_NAME$NORMAL." + exit 1 +} + +stopped_containers=() +for container in "${running_containers[@]}"; do + ## stop container + docker stop "$container" || { + err "Failed to stop container $container." + exit 1 + } + stopped_containers+=("$container") +done + +restart_stopped_container() { + for container in "${stopped_containers[@]}"; do + docker start "$container" || { + err "Failed to start container $container." + exit 1 + } + done +} +trap restart_stopped_container EXIT + +set -e +launch_docker_compose run "$SERVICE_NAME" --update="$modules" -d "$dbname" --stop-after-init + +info "Updated '$modules' module(s) into database '$dbname'."