diff --git a/cookcli/Dockerfile b/cookcli/Dockerfile index bd7f4c2..edaad3e 100644 --- a/cookcli/Dockerfile +++ b/cookcli/Dockerfile @@ -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 diff --git a/cookcli/config.yaml b/cookcli/config.yaml index e547659..cb03671 100644 --- a/cookcli/config.yaml +++ b/cookcli/config.yaml @@ -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: / \ No newline at end of file diff --git a/cookcli/rootfs/etc/services.d/git-sync/run b/cookcli/rootfs/etc/services.d/git-sync/run index e6429c5..809bb08 100644 --- a/cookcli/rootfs/etc/services.d/git-sync/run +++ b/cookcli/rootfs/etc/services.d/git-sync/run @@ -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 \ No newline at end of file +while true; do + /usr/local/bin/git-sync-now.sh || bashio::log.warning "Échec de la synchro Git" + sleep "$INTERVAL" +done \ No newline at end of file diff --git a/cookcli/rootfs/etc/services.d/reload-listener/run b/cookcli/rootfs/etc/services.d/reload-listener/run new file mode 100644 index 0000000..c548dfc --- /dev/null +++ b/cookcli/rootfs/etc/services.d/reload-listener/run @@ -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 \ No newline at end of file diff --git a/cookcli/rootfs/usr/local/bin/git-sync-now.sh b/cookcli/rootfs/usr/local/bin/git-sync-now.sh new file mode 100644 index 0000000..0dc6654 --- /dev/null +++ b/cookcli/rootfs/usr/local/bin/git-sync-now.sh @@ -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." \ No newline at end of file diff --git a/cookcli/rootfs/usr/local/bin/reload-http.sh b/cookcli/rootfs/usr/local/bin/reload-http.sh new file mode 100644 index 0000000..06f2b5c --- /dev/null +++ b/cookcli/rootfs/usr/local/bin/reload-http.sh @@ -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" \ No newline at end of file