sync-service #3

Merged
TheThomaas merged 10 commits from sync-service into main 2026-09-20 16:53:25 +00:00
6 changed files with 55 additions and 76 deletions

View file

@ -18,6 +18,7 @@ RUN apk add --no-cache \
openssh-client \
nginx \
nginx-mod-stream \
socat \
file
# Détection de l'architecture et téléchargement de la bonne version

View file

@ -1,7 +1,7 @@
---
name: CookCLI Server
slug: cookcli
version: 0.11.4
version: 0.11.5
description: "Serveur CookCLI avec synchronisation Git"
url: "https://git.thethomaas.net/TheThomaas/home-assistant-apps/src/branch/main/cookcli"
arch:
@ -15,10 +15,10 @@ host_network: false
discovery:
- cookcli
#--- Configuration Ingress ---
ingress: true
ingress_port: 9080
ingress_entry: /
panel_icon: mdi:chef-hat
panel_title: "CookCLI"
@ -39,9 +39,3 @@ schema:
git_sync_interval: int(60,86400)?
git_username: str?
git_password: password?
#--- Permissions Ingress ---
ingress:
- port: 9080
protocol: http
path: /

View file

@ -1,70 +1,8 @@
#!/usr/bin/with-contenv bashio
set -e
INTERVAL=$(bashio::config 'git_sync_interval')
[ -z "$INTERVAL" ] || [ "$INTERVAL" -eq 0 ] && INTERVAL=3600
# --- Fonction de synchronisation Git ---
sync_git() {
local REPO=$(bashio::config 'git_repository')
local BRANCH=$(bashio::config 'git_branch')
local USERNAME=$(bashio::config 'git_username')
local PASSWORD=$(bashio::config 'git_password')
local RECIPES_PATH=/config/recipes
if [ -z "$REPO" ]; then
bashio::log.info "Aucun dépôt Git configuré. Attente..."
sleep infinity
return
fi
bashio::log.info "Synchronisation avec ${REPO} (branche: ${BRANCH})"
# Construction de l'URL avec authentification si nécessaire
local AUTH_URL="$REPO"
if [ -n "$USERNAME" ] && [ -n "$PASSWORD" ]; then
# Supposons que l'URL est de type https://github.com/user/repo.git
local PROTOCOL="https://"
local REPO_WITHOUT_PROTOCOL="${REPO#https://}"
REPO_WITHOUT_PROTOCOL="${REPO_WITHOUT_PROTOCOL#http://}"
AUTH_URL="${PROTOCOL}${USERNAME}:${PASSWORD}@${REPO_WITHOUT_PROTOCOL}"
bashio::log.info "Authentification configurée pour ${USERNAME}"
fi
# Clone ou pull
if [ ! -d "$RECIPES_PATH/.git" ]; then
bashio::log.info "Premier clone du dépôt..."
git clone --branch "$BRANCH" "$AUTH_URL" "$RECIPES_PATH" 2>&1 | bashio::log.info
else
bashio::log.info "Mise à jour du dépôt existant..."
cd "$RECIPES_PATH"
# Réinitialisation pour éviter les conflits
git fetch origin "$BRANCH" 2>&1 | bashio::log.info
git reset --hard "origin/$BRANCH" 2>&1 | bashio::log.info
cd - > /dev/null
fi
bashio::log.info "Synchronisation Git terminée."
}
# --- Fonction de synchronisation périodique ---
periodic_sync() {
local INTERVAL=$(bashio::config 'git_sync_interval')
if [ -z "$INTERVAL" ] || [ "$INTERVAL" -eq 0 ]; then
INTERVAL=3600
fi
bashio::log.info "Synchronisation périodique configurée toutes les ${INTERVAL}s"
while true; do
sleep "$INTERVAL"
bashio::log.info "=== Synchronisation Git périodique ==="
sync_git
done
}
# --- Exécution ---
bashio::log.info "=== Service de synchronisation Git ==="
# Premier sync au démarrage
sync_git
# Lancement du sync périodique en arrière-plan
periodic_sync
while true; do
/usr/local/bin/git-sync-now.sh || bashio::log.warning "Échec de la synchro Git"
sleep "$INTERVAL"
done

View file

@ -0,0 +1,4 @@
#!/usr/bin/with-contenv bashio
bashio::log.info "Démarrage du listener de reload sur le port 8098"
exec socat TCP-LISTEN:8098,fork,reuseaddr EXEC:/usr/local/bin/reload-http.sh

View file

@ -0,0 +1,31 @@
#!/usr/bin/with-contenv bashio
RECIPES_PATH=/config/recipes
exec 9>/tmp/git-sync.lock
flock -n 9 || { bashio::log.info "Synchro déjà en cours"; exit 0; }
REPO=$(bashio::config 'git_repository')
BRANCH=$(bashio::config 'git_branch')
USERNAME=$(bashio::config 'git_username')
PASSWORD=$(bashio::config 'git_password')
if [ -z "$REPO" ]; then
bashio::log.info "Aucun dépôt Git configuré."
exit 0
fi
AUTH_URL="$REPO"
if [ -n "$USERNAME" ] && [ -n "$PASSWORD" ]; then
STRIPPED="${REPO#https://}"; STRIPPED="${STRIPPED#http://}"
AUTH_URL="https://${USERNAME}:${PASSWORD}@${STRIPPED}"
fi
if [ ! -d "$RECIPES_PATH/.git" ]; then
bashio::log.info "Premier clone du dépôt..."
git clone --branch "$BRANCH" "$AUTH_URL" "$RECIPES_PATH"
else
bashio::log.info "Mise à jour du dépôt existant..."
git -C "$RECIPES_PATH" fetch origin "$BRANCH"
git -C "$RECIPES_PATH" reset --hard "origin/$BRANCH"
fi
bashio::log.info "Synchronisation Git terminée."

View file

@ -0,0 +1,11 @@
#!/usr/bin/with-contenv bashio
# Consomme la requête jusqu'à la ligne vide
while IFS= read -r line; do line="${line%$'\r'}"; [ -z "$line" ] && break; done
if /usr/local/bin/git-sync-now.sh >> /proc/1/fd/1 2>&1; then
status="200 OK"; body='{"status":"ok"}'
else
status="500 Internal Server Error"; body='{"status":"error"}'
fi
printf 'HTTP/1.1 %s\r\nContent-Type: application/json\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s' \
"$status" "${#body}" "$body"