y4shg commited on
Commit
2cc54c2
Β·
verified Β·
1 Parent(s): ce66618

Update watch_config.sh

Browse files
Files changed (1) hide show
  1. watch_config.sh +74 -41
watch_config.sh CHANGED
@@ -1,9 +1,6 @@
1
  #!/bin/bash
2
  # ─────────────────────────────────────────────
3
- # LiteLLM config watcher
4
- # Env vars required:
5
- # G_NAME – GitHub repo in "username/reponame" format
6
- # G_TOKEN – GitHub personal access token
7
  # ─────────────────────────────────────────────
8
 
9
  set -euo pipefail
@@ -11,15 +8,15 @@ set -euo pipefail
11
  # ── Config ────────────────────────────────────
12
  CONFIG_PATH="/app/config.yaml"
13
  BRANCH="${BRANCH:-main}"
14
- CHECK_INTERVAL="${CHECK_INTERVAL:-1200}" # seconds between polls (default: 20 min)
15
- FILE_PATH="${FILE_PATH:-config.yaml}" # path to file inside the repo
16
- # ─────────────────────────────────────────────
17
 
18
- # ── Validate required env vars ────────────────
19
  if [[ -z "${G_NAME:-}" ]]; then
20
- echo "[ERROR] G_NAME is not set (expected: username/reponame)" >&2
21
  exit 1
22
  fi
 
23
  if [[ -z "${G_TOKEN:-}" ]]; then
24
  echo "[ERROR] G_TOKEN is not set" >&2
25
  exit 1
@@ -28,73 +25,109 @@ fi
28
  API_BASE="https://api.github.com/repos/${G_NAME}/contents/${FILE_PATH}?ref=${BRANCH}"
29
 
30
  # ── Helpers ───────────────────────────────────
31
- log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"; }
 
 
32
 
33
  fetch_meta() {
34
- python3 -c "
35
- import urllib.request
 
36
  req = urllib.request.Request(
37
- '${API_BASE}',
38
- headers={
39
- 'Authorization': 'token ${G_TOKEN}',
40
- 'Accept': 'application/vnd.github.v3+json'
41
- }
42
  )
 
43
  with urllib.request.urlopen(req) as r:
44
- print(r.read().decode())
45
- "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  }
47
 
48
  download_config() {
49
- local download_url="$1"
50
- python3 -c "
 
51
  import urllib.request
 
52
  req = urllib.request.Request(
53
- '${download_url}',
54
- headers={'Authorization': 'token ${G_TOKEN}'}
55
  )
56
- with urllib.request.urlopen(req) as r, open('${CONFIG_PATH}', 'wb') as f:
57
- f.write(r.read())
58
- "
 
59
  }
60
 
61
  restart_litellm() {
62
  log "Restarting LiteLLM..."
63
- # Sends SIGTERM to the litellm process; the container's PID 1 / supervisor
64
- # should restart it. Adjust the signal or process name to match your setup.
65
  pkill -SIGTERM -x litellm 2>/dev/null || pkill -SIGTERM -f "litellm" 2>/dev/null || true
66
  }
67
 
68
- # ── Bootstrap: pull config on first run ───────
69
  log "Starting config watcher for ${G_NAME}/${FILE_PATH} (branch: ${BRANCH})"
70
 
71
  INITIAL_META=$(fetch_meta)
72
- CURRENT_SHA=$(echo "${INITIAL_META}" | grep '"sha"' | head -1 | sed 's/.*"sha": "\([^"]*\)".*/\1/')
73
- DOWNLOAD_URL=$(echo "${INITIAL_META}" | grep '"download_url"' | head -1 | sed 's/.*"download_url": "\([^"]*\)".*/\1/')
74
 
75
- log "Pulling initial config (sha: ${CURRENT_SHA})..."
76
- download_config "${DOWNLOAD_URL}"
 
 
 
 
 
77
  log "Config written to ${CONFIG_PATH}"
78
 
79
  # ── Poll loop ─────────────────────────────────
80
  while true; do
81
- sleep "${CHECK_INTERVAL}"
82
 
83
- META=$(fetch_meta 2>/dev/null) || { log "[WARN] GitHub API request failed, skipping check."; continue; }
 
 
 
 
 
 
 
 
84
 
85
- REMOTE_SHA=$(echo "${META}" | grep '"sha"' | head -1 | sed 's/.*"sha": "\([^"]*\)".*/\1/')
86
 
87
- if [[ "${REMOTE_SHA}" != "${CURRENT_SHA}" ]]; then
88
  log "Config change detected (old: ${CURRENT_SHA:0:7}, new: ${REMOTE_SHA:0:7})"
89
 
90
- NEW_DOWNLOAD_URL=$(echo "${META}" | grep '"download_url"' | head -1 | sed 's/.*"download_url": "\([^"]*\)".*/\1/')
91
- download_config "${NEW_DOWNLOAD_URL}"
 
 
92
  log "Config updated at ${CONFIG_PATH}"
93
 
94
- CURRENT_SHA="${REMOTE_SHA}"
 
95
  restart_litellm
96
- log "Restart signal sent."
 
 
97
  else
98
  log "No change detected (sha: ${CURRENT_SHA:0:7})"
99
  fi
 
100
  done
 
1
  #!/bin/bash
2
  # ─────────────────────────────────────────────
3
+ # LiteLLM config watcher (fixed version)
 
 
 
4
  # ─────────────────────────────────────────────
5
 
6
  set -euo pipefail
 
8
  # ── Config ────────────────────────────────────
9
  CONFIG_PATH="/app/config.yaml"
10
  BRANCH="${BRANCH:-main}"
11
+ CHECK_INTERVAL="${CHECK_INTERVAL:-1200}" # default 20 minutes
12
+ FILE_PATH="${FILE_PATH:-config.yaml}"
 
13
 
14
+ # ── Validate env vars ─────────────────────────
15
  if [[ -z "${G_NAME:-}" ]]; then
16
+ echo "[ERROR] G_NAME is not set (expected: username/repo)" >&2
17
  exit 1
18
  fi
19
+
20
  if [[ -z "${G_TOKEN:-}" ]]; then
21
  echo "[ERROR] G_TOKEN is not set" >&2
22
  exit 1
 
25
  API_BASE="https://api.github.com/repos/${G_NAME}/contents/${FILE_PATH}?ref=${BRANCH}"
26
 
27
  # ── Helpers ───────────────────────────────────
28
+ log() {
29
+ echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
30
+ }
31
 
32
  fetch_meta() {
33
+ python3 <<EOF
34
+ import urllib.request, json
35
+
36
  req = urllib.request.Request(
37
+ "${API_BASE}",
38
+ headers={
39
+ "Authorization": "token ${G_TOKEN}",
40
+ "Accept": "application/vnd.github.v3+json"
41
+ }
42
  )
43
+
44
  with urllib.request.urlopen(req) as r:
45
+ print(r.read().decode())
46
+ EOF
47
+ }
48
+
49
+ get_sha() {
50
+ python3 <<EOF
51
+ import json,sys
52
+ data=json.loads(sys.stdin.read())
53
+ print(data["sha"])
54
+ EOF
55
+ }
56
+
57
+ get_download_url() {
58
+ python3 <<EOF
59
+ import json,sys
60
+ data=json.loads(sys.stdin.read())
61
+ print(data["download_url"])
62
+ EOF
63
  }
64
 
65
  download_config() {
66
+ local url="$1"
67
+
68
+ python3 <<EOF
69
  import urllib.request
70
+
71
  req = urllib.request.Request(
72
+ "${url}",
73
+ headers={"Authorization": "token ${G_TOKEN}"}
74
  )
75
+
76
+ with urllib.request.urlopen(req) as r, open("${CONFIG_PATH}", "wb") as f:
77
+ f.write(r.read())
78
+ EOF
79
  }
80
 
81
  restart_litellm() {
82
  log "Restarting LiteLLM..."
 
 
83
  pkill -SIGTERM -x litellm 2>/dev/null || pkill -SIGTERM -f "litellm" 2>/dev/null || true
84
  }
85
 
86
+ # ── Bootstrap ─────────────────────────────────
87
  log "Starting config watcher for ${G_NAME}/${FILE_PATH} (branch: ${BRANCH})"
88
 
89
  INITIAL_META=$(fetch_meta)
 
 
90
 
91
+ CURRENT_SHA=$(echo "$INITIAL_META" | get_sha)
92
+ DOWNLOAD_URL=$(echo "$INITIAL_META" | get_download_url)
93
+
94
+ log "Pulling initial config (sha: ${CURRENT_SHA:0:7})..."
95
+
96
+ download_config "$DOWNLOAD_URL"
97
+
98
  log "Config written to ${CONFIG_PATH}"
99
 
100
  # ── Poll loop ─────────────────────────────────
101
  while true; do
 
102
 
103
+ # randomize slightly to avoid API sync spikes
104
+ sleep $((CHECK_INTERVAL + RANDOM % 60))
105
+
106
+ META=$(fetch_meta 2>/dev/null) || {
107
+ log "[WARN] GitHub API request failed, skipping check"
108
+ continue
109
+ }
110
+
111
+ REMOTE_SHA=$(echo "$META" | get_sha)
112
 
113
+ if [[ "$REMOTE_SHA" != "$CURRENT_SHA" ]]; then
114
 
 
115
  log "Config change detected (old: ${CURRENT_SHA:0:7}, new: ${REMOTE_SHA:0:7})"
116
 
117
+ NEW_DOWNLOAD_URL=$(echo "$META" | get_download_url)
118
+
119
+ download_config "$NEW_DOWNLOAD_URL"
120
+
121
  log "Config updated at ${CONFIG_PATH}"
122
 
123
+ CURRENT_SHA="$REMOTE_SHA"
124
+
125
  restart_litellm
126
+
127
+ log "Restart signal sent"
128
+
129
  else
130
  log "No change detected (sha: ${CURRENT_SHA:0:7})"
131
  fi
132
+
133
  done