Browse Source

new: [compose-core] add service state tracking

master
Valentin Lab 1 month ago
parent
commit
69ed33afcb
  1. 106
      bin/compose-core

106
bin/compose-core

@ -104,6 +104,8 @@ else
fi fi
export VARDIR CACHEDIR export VARDIR CACHEDIR
export SERVICE_STATE_PATH=${SERVICE_STATE_PATH:-/var/lib/compose/state}
md5_compat() { md5sum | cut -c -32; } md5_compat() { md5sum | cut -c -32; }
quick_cat_file() { quick_cat_stdin < "$1"; } quick_cat_file() { quick_cat_stdin < "$1"; }
@ -2672,6 +2674,26 @@ relation:get() {
} }
export -f relation:get export -f relation:get
service:state() {
local service="$1" states state
project_name=$(get_default_project_name) || return 1
states=()
for state in "$SERVICE_STATE_PATH"/"$project_name"/"$service"/*; do
[ -e "$state" ] || continue
state=${state##*/}
states+=("$state")
done
if [[ " ${states[*]} " == *" deploying "* ]]; then
echo "deploying"
elif [[ " ${states[*]} " == *" up "* ]]; then
echo "up"
else
echo "down"
fi
}
_get_charm_metadata_uses() { _get_charm_metadata_uses() {
@ -5253,6 +5275,58 @@ case "$action" in
;; ;;
esac esac
case "$action" in
up)
PROJECT_NAME=$(get_default_project_name) || exit 1
## Remove all intents (*ing states)
rm -f "$SERVICE_STATE_PATH/$PROJECT_NAME"/*/*ing || true
## Notify that we have the intent to bring up all these
## This will be use in inner or concurrent 'run' to include the
## services that are supposed to be up.
mkdir -p "$SERVICE_STATE_PATH/$PROJECT_NAME" || exit 1
services_args_deps=($(get_ordered_service_dependencies "${services_args[@]}")) || exit 1
for service in "${services_args_deps[@]}"; do
mkdir -p "$SERVICE_STATE_PATH/$PROJECT_NAME"/"$service" || exit 1
[ -e "$SERVICE_STATE_PATH/$PROJECT_NAME"/"$service"/up ] || {
touch "$SERVICE_STATE_PATH/$PROJECT_NAME"/"$service"/deploying || exit 1
}
done
## remove services not included in compose.yml anymore
all_services_deps=($(get_ordered_service_dependencies "${all_services[@]}")) || exit 1
for service in "$SERVICE_STATE_PATH/$PROJECT_NAME"/*/up; do
[ -e "$service" ] || continue
state=${service##*/}
service=${service%/$state}
service=${service##*/}
if [[ " ${all_services_deps[*]} " != *" ${service} "* ]]; then
touch "$SERVICE_STATE_PATH/$PROJECT_NAME"/"${service}"/orphaning || exit 1
fi
done
;;
run)
PROJECT_NAME=$(get_default_project_name) || return 1
if [ -d "$SERVICE_STATE_PATH/$PROJECT_NAME" ]; then
## Notify that we have the intent to bring up all these
## This will be use in inner or concurrent 'run' to include the
## services that are supposed to be up.
for service in "$SERVICE_STATE_PATH/$PROJECT_NAME"/*/{up,deploying}; do
[ -e "$service" ] || continue
state=${service##*/}
service=${service%/$state}
service=${service##*/}
## don't add if orphaning
[ -e "$SERVICE_STATE_PATH/$PROJECT_NAME"/"${service}"/orphaning ] && continue
done
fi
;;
esac
if [ -n "$is_docker_compose_action_multi_service" ]; then if [ -n "$is_docker_compose_action_multi_service" ]; then
if [ -n "$DEBUG" ]; then if [ -n "$DEBUG" ]; then
Elt "get relation subset" Elt "get relation subset"
@ -5459,6 +5533,38 @@ if [ "$action" == "run" -a "${#services_args}" != 0 ]; then
fi fi
fi fi
case "$action" in
up)
## Notify that services in 'deploying' states have been deployed
for service in "$SERVICE_STATE_PATH/$PROJECT_NAME"/*/deploying; do
[ -e "$service" ] || continue
state=${service##*/}
service=${service%/$state}
service=${service##*/}
mv "$SERVICE_STATE_PATH/$PROJECT_NAME"/"${service}"/{deploying,up} || exit 1
done
## Notify that services in 'orphaning' states have been removed
for service in "$SERVICE_STATE_PATH/$PROJECT_NAME"/*/orphaning; do
[ -e "$service" ] || continue
state=${service##*/}
service=${service%/$state}
service=${service##*/}
rm "$SERVICE_STATE_PATH/$PROJECT_NAME"/"${service}"/orphaning || exit 1
done
;;
down)
PROJECT_NAME=$(get_default_project_name) || return 1
if [ -d "$SERVICE_STATE_PATH/$PROJECT_NAME" ]; then
if ! dir_is_empty "$SERVICE_STATE_PATH/$PROJECT_NAME"; then
rm -f "$SERVICE_STATE_PATH/$PROJECT_NAME"/*/*
fi
rmdir "$SERVICE_STATE_PATH/$PROJECT_NAME"/{*,}
fi
;;
esac
clean_unused_docker_compose || exit 1 clean_unused_docker_compose || exit 1
exit "$errlvl" exit "$errlvl"
Loading…
Cancel
Save