chouchouvs commited on
Commit
04b8ce3
·
verified ·
1 Parent(s): a353012

Update entrypoint.sh

Browse files
Files changed (1) hide show
  1. entrypoint.sh +213 -80
entrypoint.sh CHANGED
@@ -1,119 +1,252 @@
1
  #!/usr/bin/env bash
2
- set -euo pipefail
3
 
4
- # --- Réglages (surchageables en Variables/Secrets HF) ---
 
 
5
  : "${GITHUB_REPO_API_URL:=https://github.com/chourmovs/RFPmasterApi.git}"
6
  : "${GITHUB_REPO_CORE_URL:=https://github.com/chourmovs/RFPmaster.git}"
7
- : "${GITHUB_TOKEN:=}" # si besoin pour repos privés
8
- : "${API_MODULE:=rfp_api_app}" # module exposant 'app'
9
  : "${API_APP_ATTR:=app}"
10
- : "${BRANCH_API:=main}" # ex: main ; vide = HEAD
11
- : "${BRANCH_CORE:=main}" # ex: main
12
  : "${WORKSPACE:=/home/user/workspace}"
13
 
14
- # Assure que ~/.local/bin (pip --user) est dans le PATH
 
 
 
15
  export PATH="/home/user/.local/bin:/usr/local/bin:/usr/bin:/bin:${PATH}"
16
 
17
  API_DIR="${WORKSPACE}/RFPmasterApi"
18
- CORE_DIR="${WORKSPACE}/RFPmaster" # contiendra rfp_parser/
19
 
20
- echo "===== Application Startup at $(date '+%F %T') ====="
21
- echo "[startup] WORKSPACE=${WORKSPACE}"
22
- mkdir -p "${WORKSPACE}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- clone_or_update () {
25
- local repo_url="$1" target="$2" branch="$3"
26
 
27
- if [ ! -d "${target}/.git" ]; then
28
- echo "[git] cloning ${repo_url} -> ${target}"
29
- if [ -n "${GITHUB_TOKEN}" ]; then
30
  git -c http.extraHeader="Authorization: Basic $(printf 'oauth2:%s' "${GITHUB_TOKEN}" | base64 -w0)" \
31
- clone --depth=1 ${branch:+-b "${branch}"} "${repo_url}" "${target}"
32
  else
33
- git clone --depth=1 ${branch:+-b "${branch}"} "${repo_url}" "${target}"
34
  fi
35
  else
36
- echo "[git] updating ${target}"
37
- git -C "${target}" fetch --depth=1 origin
38
- if [ -n "${branch}" ]; then
39
- git -C "${target}" checkout -q "${branch}" || true
 
 
 
 
 
 
 
40
  fi
41
- git -C "${target}" reset --hard ${branch:+origin/"${branch}"} ${branch:+"$(true)"} || git -C "${target}" reset --hard origin/HEAD
42
  fi
43
- }
44
 
45
- # --- Clone/update des deux repos ---
46
- clone_or_update "${GITHUB_REPO_API_URL}" "${API_DIR}" "${BRANCH_API}"
47
- clone_or_update "${GITHUB_REPO_CORE_URL}" "${CORE_DIR}" "${BRANCH_CORE}"
48
 
49
- # --- Charge les .env éventuels des repos clonés ---
50
- load_env_file () {
 
 
 
 
 
 
 
51
  local env_file="$1"
52
- if [ -f "$env_file" ]; then
53
- echo "[env] loading $env_file"
54
- set -a
55
- # shellcheck disable=SC1090
56
- . "$env_file"
57
- set +a
58
- else
59
- echo "[env] no env file at $env_file"
60
  fi
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  }
62
 
63
- load_env_file "${CORE_DIR}/.env"
64
- load_env_file "${API_DIR}/.env"
 
 
 
 
 
 
65
 
66
- echo "[env] effective model settings:"
67
- echo " LLM_MODEL=${LLM_MODEL:-<unset>}"
68
- echo " DEEPINFRA_MODEL=${DEEPINFRA_MODEL:-<unset>}"
69
- echo " DEEPINFRA_BASE_URL=${DEEPINFRA_BASE_URL:-<unset>}"
70
- echo " LLM_TEMPERATURE=${LLM_TEMPERATURE:-<unset>}"
71
-
72
- # Safe dir pour Git (HF)
73
- git config --global --add safe.directory "${API_DIR}" || true
74
- git config --global --add safe.directory "${CORE_DIR}" || true
75
-
76
- # --- Installation des dépendances Python (au run) ---
77
- echo "[pip] Checking uvicorn presence..."
78
- if ! command -v uvicorn >/dev/null 2>&1; then
79
- echo "[pip] uvicorn not found → installing requirements"
80
- if [ -f "${API_DIR}/requirements.txt" ]; then
81
- # --user pour installer dans ~/.local et éviter les droits root
82
- python -m pip install --user --no-cache-dir -r "${API_DIR}/requirements.txt"
83
- else
84
- # garde-fou minimal
85
- python -m pip install --user --no-cache-dir fastapi "uvicorn[standard]" requests pandas openpyxl
86
  fi
87
- else
88
- echo "[pip] uvicorn OK"
89
- fi
 
90
 
91
- # (Optionnel) si ton repo CORE a aussi un requirements.txt :
92
- if [ -f "${CORE_DIR}/requirements.txt" ]; then
93
- echo "[pip] Installing CORE requirements"
94
- python -m pip install --user --no-cache-dir -r "${CORE_DIR}/requirements.txt" || true
95
- fi
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
- # --- PYTHONPATH : on expose les deux répertoires (API + CORE/rfp_parser) ---
98
- export PYTHONPATH="${API_DIR}:${CORE_DIR}:${PYTHONPATH:-}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
- # --- Sanity checks imports ---
101
- echo "[check] importing ${API_MODULE}"
102
- python - <<'PY' "${API_MODULE}"
103
- import importlib, sys
104
- m = sys.argv[1]
 
 
 
 
 
 
 
 
 
105
  importlib.import_module(m)
106
  print(f"[check] import {m}: OK")
 
 
 
 
107
  PY
108
 
109
- echo "[check] importing rfp_parser"
110
- python - <<'PY'
111
  import importlib
112
  importlib.import_module("rfp_parser")
113
  print("[check] import rfp_parser: OK")
114
  PY
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
- # --- Lancement Uvicorn ---
117
- cd "${API_DIR}"
118
- echo "[uvicorn] launching ${API_MODULE}:${API_APP_ATTR} on 0.0.0.0:7860"
119
- exec uvicorn "${API_MODULE}:${API_APP_ATTR}" --host 0.0.0.0 --port 7860
 
1
  #!/usr/bin/env bash
2
+ set -Eeuo pipefail
3
 
4
+ # ------------------------------------------------------------
5
+ # Runtime config
6
+ # ------------------------------------------------------------
7
  : "${GITHUB_REPO_API_URL:=https://github.com/chourmovs/RFPmasterApi.git}"
8
  : "${GITHUB_REPO_CORE_URL:=https://github.com/chourmovs/RFPmaster.git}"
9
+ : "${GITHUB_TOKEN:=}"
10
+ : "${API_MODULE:=rfp_api_app}"
11
  : "${API_APP_ATTR:=app}"
12
+ : "${BRANCH_API:=main}"
13
+ : "${BRANCH_CORE:=main}"
14
  : "${WORKSPACE:=/home/user/workspace}"
15
 
16
+ # repo = le .env du repo écrase l'existant
17
+ # hf = les Variables/Secrets HF gardent la priorité
18
+ : "${ENV_PRIORITY:=repo}"
19
+
20
  export PATH="/home/user/.local/bin:/usr/local/bin:/usr/bin:/bin:${PATH}"
21
 
22
  API_DIR="${WORKSPACE}/RFPmasterApi"
23
+ CORE_DIR="${WORKSPACE}/RFPmaster"
24
 
25
+ log() {
26
+ echo "[$1] $2"
27
+ }
28
+
29
+ die() {
30
+ echo "[fatal] $1" >&2
31
+ exit 1
32
+ }
33
+
34
+ trim() {
35
+ local s="$1"
36
+ s="${s#"${s%%[![:space:]]*}"}"
37
+ s="${s%"${s##*[![:space:]]}"}"
38
+ printf '%s' "$s"
39
+ }
40
+
41
+ # ------------------------------------------------------------
42
+ # Git helpers
43
+ # ------------------------------------------------------------
44
+ clone_or_update() {
45
+ local repo_url="$1"
46
+ local target="$2"
47
+ local branch="$3"
48
 
49
+ mkdir -p "$(dirname "$target")"
 
50
 
51
+ if [[ ! -d "${target}/.git" ]]; then
52
+ log git "cloning ${repo_url} -> ${target}"
53
+ if [[ -n "${GITHUB_TOKEN}" && "${repo_url}" =~ ^https://github.com/ ]]; then
54
  git -c http.extraHeader="Authorization: Basic $(printf 'oauth2:%s' "${GITHUB_TOKEN}" | base64 -w0)" \
55
+ clone --depth=1 ${branch:+--branch "$branch"} "$repo_url" "$target"
56
  else
57
+ git clone --depth=1 ${branch:+--branch "$branch"} "$repo_url" "$target"
58
  fi
59
  else
60
+ log git "updating ${target}"
61
+ git -C "$target" fetch --prune origin
62
+
63
+ if [[ -n "$branch" ]]; then
64
+ git -C "$target" checkout -q "$branch"
65
+ git -C "$target" reset --hard "origin/$branch"
66
+ else
67
+ local default_branch
68
+ default_branch="$(git -C "$target" symbolic-ref --short refs/remotes/origin/HEAD | sed 's#^origin/##')"
69
+ git -C "$target" checkout -q "$default_branch"
70
+ git -C "$target" reset --hard "origin/$default_branch"
71
  fi
 
72
  fi
 
73
 
74
+ git config --global --add safe.directory "$target" || true
 
 
75
 
76
+ local sha
77
+ sha="$(git -C "$target" rev-parse --short HEAD 2>/dev/null || echo unknown)"
78
+ log git "${target} @ ${sha}"
79
+ }
80
+
81
+ # ------------------------------------------------------------
82
+ # .env loader
83
+ # ------------------------------------------------------------
84
+ load_env_file() {
85
  local env_file="$1"
86
+
87
+ if [[ ! -f "$env_file" ]]; then
88
+ log env "no env file at ${env_file}"
89
+ return 0
 
 
 
 
90
  fi
91
+
92
+ log env "loading ${env_file} (priority=${ENV_PRIORITY})"
93
+
94
+ while IFS= read -r line || [[ -n "$line" ]]; do
95
+ line="${line%$'\r'}"
96
+ [[ -z "$(trim "$line")" ]] && continue
97
+ [[ "$line" =~ ^[[:space:]]*# ]] && continue
98
+
99
+ if [[ "$line" =~ ^[[:space:]]*([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]]; then
100
+ local key="${BASH_REMATCH[1]}"
101
+ local raw="${BASH_REMATCH[2]}"
102
+ local val
103
+
104
+ raw="$(trim "$raw")"
105
+
106
+ if [[ "$raw" =~ ^\"(.*)\"$ ]]; then
107
+ val="${BASH_REMATCH[1]}"
108
+ elif [[ "$raw" =~ ^\'(.*)\'$ ]]; then
109
+ val="${BASH_REMATCH[1]}"
110
+ else
111
+ val="$raw"
112
+ fi
113
+
114
+ if [[ "$ENV_PRIORITY" == "repo" ]]; then
115
+ export "${key}=${val}"
116
+ else
117
+ if [[ -z "${!key+x}" ]]; then
118
+ export "${key}=${val}"
119
+ fi
120
+ fi
121
+ fi
122
+ done < "$env_file"
123
  }
124
 
125
+ canonicalize_env() {
126
+ # Modèle canonique
127
+ if [[ -n "${DEEPINFRA_MODEL:-}" && -z "${LLM_MODEL:-}" ]]; then
128
+ export LLM_MODEL="${DEEPINFRA_MODEL}"
129
+ fi
130
+ if [[ -n "${LLM_MODEL:-}" && -z "${DEEPINFRA_MODEL:-}" ]]; then
131
+ export DEEPINFRA_MODEL="${LLM_MODEL}"
132
+ fi
133
 
134
+ # Base URL canonique
135
+ if [[ -n "${DEEPINFRA_BASE_URL:-}" && -z "${OPENAI_BASE_URL:-}" ]]; then
136
+ export OPENAI_BASE_URL="${DEEPINFRA_BASE_URL}"
137
+ fi
138
+
139
+ # Compat fréquente
140
+ if [[ -n "${LLM_MODEL:-}" && -z "${OPENAI_MODEL:-}" ]]; then
141
+ export OPENAI_MODEL="${LLM_MODEL}"
 
 
 
 
 
 
 
 
 
 
 
 
142
  fi
143
+ if [[ -n "${LLM_MODEL:-}" && -z "${MODEL:-}" ]]; then
144
+ export MODEL="${LLM_MODEL}"
145
+ fi
146
+ }
147
 
148
+ print_effective_env() {
149
+ log env "effective model settings:"
150
+ for k in \
151
+ LLM_MODEL \
152
+ DEEPINFRA_MODEL \
153
+ OPENAI_MODEL \
154
+ MODEL \
155
+ DEEPINFRA_BASE_URL \
156
+ OPENAI_BASE_URL \
157
+ LLM_TEMPERATURE \
158
+ LLM_TOP_P \
159
+ LLM_MAX_TOKENS \
160
+ LLM_RESPONSE_FORMAT \
161
+ ENV_PRIORITY
162
+ do
163
+ printf ' %s=%s\n' "$k" "${!k:-<unset>}"
164
+ done
165
+ }
166
 
167
+ python_env_probe() {
168
+ log pyenv "what Python sees before app import:"
169
+ python - <<'PY'
170
+ import os
171
+ keys = [
172
+ "LLM_MODEL",
173
+ "DEEPINFRA_MODEL",
174
+ "OPENAI_MODEL",
175
+ "MODEL",
176
+ "DEEPINFRA_BASE_URL",
177
+ "OPENAI_BASE_URL",
178
+ "LLM_TEMPERATURE",
179
+ "LLM_TOP_P",
180
+ "LLM_MAX_TOKENS",
181
+ "LLM_RESPONSE_FORMAT",
182
+ ]
183
+ for k in keys:
184
+ print(f" {k}={os.getenv(k, '<unset>')}")
185
+ PY
186
+ }
187
+
188
+ install_requirements() {
189
+ local label="$1"
190
+ local req_file="$2"
191
 
192
+ if [[ -f "$req_file" ]]; then
193
+ log pip "installing ${label} requirements from ${req_file}"
194
+ python -m pip install --user --no-cache-dir -r "$req_file"
195
+ else
196
+ log pip "no ${label} requirements file at ${req_file}"
197
+ fi
198
+ }
199
+
200
+ sanity_imports() {
201
+ log check "importing ${API_MODULE}"
202
+ python - <<PY
203
+ import importlib
204
+ import os
205
+ m = "${API_MODULE}"
206
  importlib.import_module(m)
207
  print(f"[check] import {m}: OK")
208
+ print(f"[check] resolved LLM_MODEL={os.getenv('LLM_MODEL')}")
209
+ print(f"[check] resolved DEEPINFRA_MODEL={os.getenv('DEEPINFRA_MODEL')}")
210
+ print(f"[check] resolved OPENAI_MODEL={os.getenv('OPENAI_MODEL')}")
211
+ print(f"[check] resolved MODEL={os.getenv('MODEL')}")
212
  PY
213
 
214
+ log check "importing rfp_parser"
215
+ python - <<'PY'
216
  import importlib
217
  importlib.import_module("rfp_parser")
218
  print("[check] import rfp_parser: OK")
219
  PY
220
+ }
221
+
222
+ launch() {
223
+ cd "${API_DIR}"
224
+ log uvicorn "launching ${API_MODULE}:${API_APP_ATTR} on 0.0.0.0:7860"
225
+ exec python -m uvicorn "${API_MODULE}:${API_APP_ATTR}" --host 0.0.0.0 --port 7860
226
+ }
227
+
228
+ # ------------------------------------------------------------
229
+ # Startup
230
+ # ------------------------------------------------------------
231
+ echo "===== Application Startup at $(date '+%F %T') ====="
232
+ log startup "WORKSPACE=${WORKSPACE}"
233
+ mkdir -p "${WORKSPACE}"
234
+
235
+ clone_or_update "${GITHUB_REPO_API_URL}" "${API_DIR}" "${BRANCH_API}"
236
+ clone_or_update "${GITHUB_REPO_CORE_URL}" "${CORE_DIR}" "${BRANCH_CORE}"
237
+
238
+ load_env_file "${CORE_DIR}/.env"
239
+ load_env_file "${API_DIR}/.env"
240
+ canonicalize_env
241
+ print_effective_env
242
+
243
+ # Toujours installer les deps API + CORE
244
+ install_requirements "API" "${API_DIR}/requirements.txt"
245
+ install_requirements "CORE" "${CORE_DIR}/requirements.txt"
246
+
247
+ export PYTHONPATH="${API_DIR}:${CORE_DIR}:${PYTHONPATH:-}"
248
+ log startup "PYTHONPATH=${PYTHONPATH}"
249
 
250
+ python_env_probe
251
+ sanity_imports
252
+ launch