restart containers after rotation
Build and Push Docker Images / build-and-push (push) Successful in 23s

This commit is contained in:
2026-06-04 18:39:34 +02:00
parent 2b25780f3e
commit 34ee842d34
2 changed files with 52 additions and 17 deletions
+41 -10
View File
@@ -41,17 +41,54 @@ pick_random_region() {
printf '%s\n' "${regions[@]}" | shuf -n 1
}
parse_restart_containers() {
# CSV: "m3u-filter-vpn,m3u-editor,xtream-proxy" or JSON array.
# Falls back to GLUETUN_CONTAINER when unset.
local raw="${RESTART_CONTAINERS:-}"
if [[ -z "$raw" ]]; then
echo "${GLUETUN_CONTAINER:-m3u-filter-vpn}"
return
fi
if [[ "$raw" =~ ^\[.*\]$ ]]; then
jq -r '.[]' <<<"$raw"
return
fi
tr ',' '\n' <<<"$raw" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | awk 'NF'
}
restart_containers() {
local container restarted=()
while IFS= read -r container; do
[[ -z "$container" ]] && continue
log "Restarting container: $container"
if ! docker restart "$container"; then
>&2 echo "docker restart failed for container=$container"
exit 1
fi
restarted+=("$container")
done < <(parse_restart_containers)
if [[ "${#restarted[@]}" -eq 0 ]]; then
>&2 echo "No containers configured to restart"
exit 1
fi
}
write_state() {
local region="$1"
local state_path="${ROTATOR_STATE_PATH:-/config/rotator-state.json}"
local rotated_at
local rotated_at restarted_json
rotated_at="$(date -Is)"
restarted_json="$(parse_restart_containers | jq -R -s 'split("\n") | map(select(length > 0))')"
mkdir -p "$(dirname "$state_path")"
jq -n \
--arg region "$region" \
--arg rotated_at "$rotated_at" \
--arg wg_config "${WG_CONFIG_PATH:-/config/wireguard/wg0.conf}" \
'{region: $region, rotated_at: $rotated_at, wg_config: $wg_config}' >"$state_path"
--argjson restarted_containers "$restarted_json" \
'{region: $region, rotated_at: $rotated_at, wg_config: $wg_config, restarted_containers: $restarted_containers}' >"$state_path"
chmod 644 "$state_path"
}
@@ -60,10 +97,9 @@ rotate_once() {
require_env PIA_PASS
require_env PIA_REGIONS
local region wg_path tmp_dir gluetun_container
local region wg_path tmp_dir
region="$(pick_random_region)"
wg_path="${WG_CONFIG_PATH:-/config/wireguard/wg0.conf}"
gluetun_container="${GLUETUN_CONTAINER:-m3u-filter-vpn}"
log "Selected region: $region"
@@ -87,14 +123,9 @@ rotate_once() {
chmod 600 "$wg_path"
log "Wrote $wg_path"
restart_containers
write_state "$region"
log "Restarting gluetun container: $gluetun_container"
if ! docker restart "$gluetun_container"; then
>&2 echo "docker restart failed for container=$gluetun_container"
exit 1
fi
log "Rotation complete for region=$region"
}