forge
Explore
Log in

Don't you dare go Hollow

hezarfen_zeka

Clone
★ 0

0 watching0 forksPython1.9 MiBe01a4faAhmet Kaan Gümüş2 months ago25 commits

main.yml652 lines, 30.0 KiBHistoryRawDownload
1# VPS deploy -- ZEKA kopru servisi. Sekil, ailedeki Celebi/RAG deploy'uyla
2# AYNI (hata toparlama ucuz olsun):
3#
4# Katman 1..5 -- ci.yml'deki BES katmanin KENDISI; push'ta burada kosar.
5# Komutlar oradakiyle birebir aynidir (tek dogruluk
6# kaynagi: scripts/ci-yerel.sh).
7# Build and test -- bes katman yesilse imaji derler ve kosunun artefaktini
8# (imaj tarball'i + compose.yaml + systemd unit'i + tag)
9# yukler. Ad BILEREK "Build and test": workflow_dispatch
10# yolu en yeni yesil kosuyu tam bu ISIN ADIYLA arar
11# (`gh run view ... select(.name=="Build and test")`).
12# Deploy -- SSH: kapasite on kontrolu, imaji yukle, deploy sahipli
13# stack.env'in HEZARFEN_TAG'ini ona cevir, compose.yaml +
14# unit'i kur, stack'i yeniden baslat, servisin KENDI
15# saglik betigiyle dogrula, olmazsa onceki tag'e don,
16# ardindan bagimsiz dogrulama.
17#
18# KATMANLAR NEDEN AYRI ISLER -- gerekce ci.yml'in basindan aynen:
19# "Katmanlar bilincli olarak AYRI islerdir, tek bir dev is degil: bir kosucu
20# konteyner derleyemiyorsa testler yine de kosar, ve sozlesme denetimi
21# kirildiginda 'testler gecti ama sozlesme kaldi' bilgisi tek bakista
22# gorunur."
23# Push'ta ayni kapi IKI KEZ kosmasin diye ci.yml'in push tetikleyicisi main
24# dalini DISLAR (bkz. ci.yml `on:`): main push'u boylece tam olarak bir kez
25# kosar; diger dallar ve PR'lar katmanlari ci.yml'den alir.
26#
27# YESIL BIR MAIN PUSH'U OTOMATIK DAGITIR (Celebi'nin sekli; RAG'da dagitim
28# bilerek elleydi cunku orada ~4,5 GB model ve GPU'suz makinede gecmeyen bir
29# urun kapisi var -- ZEKA hafif bir koprudur, o gerekce burada yok).
30#
31# Elle yeniden dagitim -- Actions -> Run workflow, ya da:
32# gh workflow run main.yml --ref main
33# Bu yol suiti TEKRAR kosmaz: "Build and test" isi yesil olan en yeni kosunun
34# artefaktini dagitir (asagidaki "Find the newest green build" adimi).
35#
36# Bir seferlik on kosullar (workflow'un erisimi disinda):
37# * repo secret'lari SSH_PRIVATE_KEY / SSH_HOST / SSH_USER -- backend ve
38# frontend deploy'unun kullandigi uc isim;
39# * sunucuda `loginctl enable-linger <user>` (kullanici unit'leri icin);
40# * sunucuda podman + bir compose saglayici (deploy ikisini de kontrol eder);
41# * operatorun ~/hezarfen_zeka/hezarfen_zeka.env dosyasi (0600; sablon:
42# deploy/hezarfen_zeka.env.example). Deploy onu ASLA olusturmaz, yazmaz;
43# yalniz varligini kontrol eder ve iznini 0600'e ceker;
44# * backend tarafi acik olmali: hezarfen_backend.env icinde
45# AI_QUIC_ADDR=0.0.0.0:8090 ve AI_SHARED_TOKEN bu koprununkiyle AYNI.
46# Bu bir KAPI DEGILDIR -- asagida gerekcesiyle yazilidir.
47
48name: VPS deploy
49
50on:
51 push:
52 branches:
53 - main
54 # Elle yeniden deploy kapisi -- yukaridaki baslik yorumuna bakin.
55 workflow_dispatch:
56
57# Deploy serilestirmesi workflow'da degil deploy isinde: yeni bir push, ucustaki
58# VPS degisimine dokunmadan bayat katman/Build kosularini iptal edebilmeli.
59permissions:
60 contents: read
61 # workflow_dispatch yolu `gh run list/view` ile en yeni yesil kosuyu arar ve o
62 # kosunun artefaktini indirir; ikisi de Actions API'sidir ve YAZMA GEREKMEZ.
63 actions: read
64
65env:
66 # systemd kullanici unit'i. Bkz. deploy/hezarfen_zeka_compose.service.
67 SERVICE: hezarfen_zeka_compose
68 # Deploy, SSH kullanicisinin home'u altindaki bu alt dizinde yasar ($HOME
69 # sunucuda cozulur).
70 DEPLOY_SUBDIR: hezarfen_zeka
71 # Operatorun ayar dosyasi; deploy yalniz varligini kontrol eder, yazmaz.
72 ENV_FILE_NAME: hezarfen_zeka.env
73 # compose.yaml'daki `container_name` (bkz. service/compose.yaml:11).
74 CONTAINER_NAME: hezarfen_zeka
75 # Saglik kapisinin on kosulu: backend'in HTTP sertifika ucu. Backend'in host
76 # loopback portu (README: `127.0.0.1:7656:7656`).
77 CERT_PATH: /ai/certificate
78 BACKEND_HTTP_PORT: "7656"
79
80jobs:
81 # =========================================================================
82 # KATMAN 1..5 -- ci.yml'deki bes katman. Adlar, adim metinleri ve komutlar
83 # oradan BIREBIR tasindi; burada degisen tek sey `if` ve `concurrency`:
84 # dispatch bu suiti tekrar odemez (bkz. dosya basi).
85 # =========================================================================
86 sozdizimi:
87 name: "Katman 1 -- sozdizimi (her .py ayristirilabiliyor mu)"
88 if: github.event_name == 'push'
89 concurrency:
90 group: ci-${{ github.workflow }}-${{ github.ref }}-sozdizimi
91 cancel-in-progress: true
92 runs-on: ubuntu-latest
93 steps:
94 - uses: actions/checkout@v4
95 - uses: actions/setup-python@v5
96 with:
97 python-version: "3.13"
98 - name: "Butun kaynaklar ayristirilabiliyor mu"
99 # Testlerden ONCE kosar: bozuk bir dosya, testlerin toplanmasini
100 # engelleyip anlamsiz bir toplama hatasi uretir. Burada hangi dosya
101 # ve hangi satir oldugu tek satirda gorunur.
102 run: |
103 python -m compileall -q -x '(__pycache__|\.venv|venv)' \
104 service/src service/tests scripts
105
106 # =========================================================================
107 testler:
108 name: "Katman 2 -- birim testleri (saf unittest, bagimliliksiz)"
109 if: github.event_name == 'push'
110 concurrency:
111 group: ci-${{ github.workflow }}-${{ github.ref }}-testler
112 cancel-in-progress: true
113 runs-on: ubuntu-latest
114 steps:
115 - uses: actions/checkout@v4
116 - uses: actions/setup-python@v5
117 with:
118 python-version: "3.13"
119 - name: "python -m unittest discover"
120 # BAGIMLILIK KURULMAZ. Bu kasitli: test paketi saf `unittest`
121 # kullanir ve `requirements.txt` gerektirmez. Boylece bu is,
122 # bagimlilik cozumleme sorunlarindan BAGIMSIZ olarak kirmizi/yesil
123 # bilgisi verir; ag kesintisi testleri dusuremez.
124 #
125 # `aioquic` gerektiren canli kopru testleri kendiliginden ATLANIR
126 # (tests/test_bridge_live.py basindaki SkipTest). Onlari acikca
127 # kosturan is ayri dosyada: `istege-bagli-testler.yml`.
128 working-directory: service
129 run: python -m unittest discover -s tests -t . -v
130
131 # =========================================================================
132 sozlesme:
133 name: "Katman 3 -- sozlesme (model<->fiyat, yol<->izin listesi)"
134 if: github.event_name == 'push'
135 concurrency:
136 group: ci-${{ github.workflow }}-${{ github.ref }}-sozlesme
137 cancel-in-progress: true
138 runs-on: ubuntu-latest
139 steps:
140 - uses: actions/checkout@v4
141 - uses: actions/setup-python@v5
142 with:
143 python-version: "3.13"
144 - name: "Sozlesme denetimi"
145 # BU DENETIM BU PROJEDE IKI KEZ SESSIZ KUSUR YAKALARDI:
146 #
147 # 1. Model adi uyusmazligi. `cost_usd()` bilinmeyen bir model adi
148 # gorunce hata vermez, sessizce EN PAHALI tarifeye duser
149 # (provider.py:124-125). Butce raporu yanlis cikar, hicbir test
150 # kirilmaz.
151 #
152 # 2. Izin listesi sapmasi. `protocol.py:AI_API_ALLOWLIST`,
153 # backend'in `constant.rs:656-676` listesinin ELLE tutulan
154 # kopyasidir. Ayristiklarinda cagri `path_not_allowed` ile tele
155 # cikmadan reddedilir; testler de ayni kopyayi dogruladigi icin
156 # yesil kalir.
157 #
158 # NOT: GitHub kosucusunda backend deposu YOKTUR, bu yuzden gercek
159 # `constant.rs` karsilastirmasi burada ATLANIR ve betik bunu bir
160 # "not" olarak basar (hata degil). Depo ici pinli anlik goruntuye
161 # (tests/test_protocol.py:BACKEND_SNAPSHOT) karsi denetim yine kosar.
162 # Gercek karsilastirma yerelde yapilir:
163 # HEZARFEN_BACKEND=/yol/hezarfen_backend-main python scripts/ci-sozlesme.py
164 run: python scripts/ci-sozlesme.py
165
166 # =========================================================================
167 sirlar:
168 name: "Katman 4 -- sir taramasi (sizmis API anahtari)"
169 if: github.event_name == 'push'
170 concurrency:
171 group: ci-${{ github.workflow }}-${{ github.ref }}-sirlar
172 cancel-in-progress: true
173 runs-on: ubuntu-latest
174 steps:
175 - uses: actions/checkout@v4
176 - uses: actions/setup-python@v5
177 with:
178 python-version: "3.13"
179 - name: "Sir taramasi"
180 # `.env` `.gitignore` icinde, ama bu yalnizca YENI dosyalari korur:
181 # `git add -f` ile ya da `.gitignore`'dan once eklenmis bir dosya
182 # izlenmeye devam eder. Tarayici `sk-` / `sk_` desenlerini, ozel
183 # anahtar govdelerini ve yapilandirma dosyalarina gomulmus duz
184 # degerleri arar; ayrica compose'daki zorunlu sirlarin `${VAR:?}`
185 # bicimini korudugunu dogrular.
186 #
187 # Bulunan deger ASLA tam basilmaz -- yoksa CI kaydinin kendisi sir
188 # sizdiran yer olurdu.
189 run: python scripts/ci-sir-tara.py
190
191 # =========================================================================
192 konteyner:
193 name: "Katman 5 -- konteyner (derleniyor VE kosuyor mu)"
194 if: github.event_name == 'push'
195 concurrency:
196 group: ci-${{ github.workflow }}-${{ github.ref }}-konteyner
197 cancel-in-progress: true
198 runs-on: ubuntu-latest
199 steps:
200 - uses: actions/checkout@v4
201
202 - name: "Imaji derle"
203 # Taban imaj digest'e pinlidir (Containerfile:15); bu is ayni zamanda
204 # o digest'in hala cekilebildigini de dogrular.
205 working-directory: service
206 run: podman build -t hezarfen-zeka:ci -f Containerfile .
207
208 - name: "Imaj boyutu"
209 run: podman images --format '{{.Repository}}:{{.Tag}} {{.Size}}' hezarfen-zeka:ci
210
211 - name: "Kosu kaniti -- kap cikmiyor, non-root, saglik kontrolu olcuyor"
212 # DERLENDI, KOSTU DEMEK DEGILDIR. Denetimde kardes servis RAG'in
213 # konteynerinin hic ayaga kalkmadigi bulundu: komut bir metin basip
214 # cikiyor ve `restart: unless-stopped` ile sonsuz crash-loop
215 # uretiyordu. Bu adim farki olcer:
216 # - kap 50 sn sonra hala kosuyor mu,
217 # - backend YOKKEN cikmak yerine USTEL geri cekilmeyle yeniden
218 # deniyor mu (bekleme suresi buyuyor mu),
219 # - surec non-root mu (uid 10002),
220 # - saglik kontrolu kopru yasarken geciyor VE kopru yokken
221 # kaliyor mu (her zaman 0 donen bir kontrol hicbir sey kanitlamaz).
222 run: bash scripts/ci-konteyner-kanit.sh podman hezarfen-zeka:ci
223
224 - name: "compose.yaml gecerli mi ve zorunlu sirlar zorunlu mu"
225 # Iki yonlu denetim: (1) zorunlu env YOKKEN `config` PATLAMALI,
226 # (2) VARKEN gecerli bir yapilandirma uretmeli. Yalnizca ikincisini
227 # denetlemek, `${VAR:?}` yerine `${VAR:-}` yazilmasini yakalamaz.
228 #
229 # ZORUNLU SIRLAR LISTESI compose.yaml'IN KENDISINDEN OKUNUR ve TAM
230 # IKI TANEDIR: `AI_SHARED_TOKEN`, `LLM_API_KEY`. Liste burada elle
231 # tutulur; compose.yaml'a yeni bir `${VAR:?}` eklerken burayi da
232 # guncelleyin (ve yerel esdegeri `scripts/ci-compose.sh`'i; yerel
233 # esdeger AYNI kapi olmali).
234 working-directory: service
235 run: |
236 echo "--- zorunlu env eksikken patlamali ---"
237 if env -u AI_SHARED_TOKEN -u LLM_API_KEY \
238 docker compose config >/dev/null 2>&1; then
239 echo "HATA: zorunlu sirlar olmadan gecti;"
240 echo " zorunlu sirlar \${VAR:?} bicimini kaybetmis olabilir."
241 exit 1
242 fi
243 echo "ok: zorunlu env olmadan reddedildi"
244 echo "--- zorunlu env varken gecerli olmali ---"
245 AI_SHARED_TOKEN=ci-sahte LLM_API_KEY=ci-sahte docker compose config
246 echo "--- port ACILMAMALI (disa arama servisi) ---"
247 if AI_SHARED_TOKEN=ci-sahte LLM_API_KEY=ci-sahte docker compose config \
248 | grep -qE '^\s+ports:'; then
249 echo "HATA: compose port aciyor; ZEKA backend'e dial-out eder, port ACMAZ."
250 exit 1
251 fi
252 echo "ok: hicbir port yayimlanmiyor"
253
254 # =========================================================================
255 build:
256 name: Build and test
257 # Push'ta kosar. Dispatch, suiti tekrar odemek yerine en yeni yesil
258 # artefakti kullanir (bkz. deploy'un "Find the newest green build" adimi).
259 if: github.event_name == 'push'
260 # BES KATMANIN HEPSI yesil olmadan artefakt uretilmez: bozuk bir build
261 # sunucuya ASLA ulasmamali. Suiti burada TEKRAR kosmuyoruz -- `needs:`
262 # zinciri Katman 2'yi zaten sart kostu; bu isin ekledigi sey, ayni agactan
263 # uretilen SURUM paketidir.
264 needs: [sozdizimi, testler, sozlesme, sirlar, konteyner]
265 concurrency:
266 group: ci-${{ github.workflow }}-${{ github.ref }}-build
267 cancel-in-progress: true
268 runs-on: ubuntu-24.04
269 steps:
270 - name: Checkout
271 uses: actions/checkout@v4
272
273 - name: Build the image
274 # Laptop ve CI ayni Containerfile'i kullanir: ayri bir CI ikizi yok.
275 # Baglam `service/` -- Containerfile oradadir.
276 working-directory: service
277 run: |
278 set -euo pipefail
279 podman build -t localhost/hezarfen-zeka:$GITHUB_SHA -f Containerfile .
280
281 - name: Stage the release bundle
282 # Sunucuda kurulacak her sey burada olmali; eksik parca burada,
283 # sunucuda degil, patlar.
284 run: |
285 set -euo pipefail
286 mkdir -p stage
287 podman save localhost/hezarfen-zeka:$GITHUB_SHA | gzip > stage/hezarfen_zeka.tar.gz
288 cp service/compose.yaml stage/compose.yaml
289 cp deploy/hezarfen_zeka_compose.service stage/
290 echo -n "$GITHUB_SHA" > stage/tag
291 test -f stage/hezarfen_zeka.tar.gz
292 test -f stage/compose.yaml
293 test -f stage/hezarfen_zeka_compose.service
294 test -f stage/tag
295 ls -lh stage
296
297 # Deploy isi (ve elle yeniden deploy) bunu tuketir. 30 gun, dispatch
298 # kapisinin geri uzanma penceresidir.
299 - name: Upload the release
300 uses: actions/upload-artifact@v4
301 with:
302 name: release
303 path: stage
304 retention-days: 30
305
306 # =========================================================================
307 deploy:
308 name: Deploy
309 # Zincirin tamami kapi olur: build'in `needs` listesi bes katmani kapsar,
310 # yani `needs: [build]` "bes katman + imaj yesil" demektir.
311 needs: [build]
312 concurrency:
313 group: deploy
314 cancel-in-progress: false
315 # Push: yalniz Build and test yesilse. Dispatch: iki CI isi bilerek
316 # atlanir (if'lerine bakin) ve `always()` bu atlamanin bu kapidan
317 # gecmesini saglar.
318 if: >-
319 always() &&
320 ((github.event_name == 'push' && needs.build.result == 'success') ||
321 (github.event_name == 'workflow_dispatch' && needs.build.result == 'skipped'))
322 runs-on: ubuntu-24.04
323 steps:
324 # Push deploy'u bu kosunun urettigi ve kapiladigi surumu alir.
325 - name: Fetch this run's release
326 if: github.event_name == 'push'
327 uses: actions/download-artifact@v4
328 with:
329 name: release
330 path: stage
331
332 # Elle yeniden deploy, BUILD isi yesil olan en yeni kosuyu alir -- en
333 # yeni tamamen yesil kosuyu degil: bir kosu saglik kapisinda dusmus
334 # olabilir ama build'i tam da geri almak istedigimiz surumdur.
335 - name: Find the newest green build
336 if: github.event_name == 'workflow_dispatch'
337 env:
338 GH_TOKEN: ${{ github.token }}
339 run: |
340 set -euo pipefail
341 run_id=""
342 for rid in $(gh run list -R "$GITHUB_REPOSITORY" --workflow main.yml --branch main --event push --limit 15 --json databaseId --jq '.[].databaseId'); do
343 if [ "$(gh run view -R "$GITHUB_REPOSITORY" "$rid" --json jobs --jq '.jobs[] | select(.name=="Build and test") | .conclusion')" = "success" ]; then
344 run_id=$rid
345 break
346 fi
347 done
348 if [ -z "$run_id" ]; then
349 echo 'no run on main has a green Build-and-test job (or its artifact has expired)' >&2
350 exit 1
351 fi
352 echo "redeploying the release from run $run_id"
353 echo "run_id=$run_id" >> "$GITHUB_ENV"
354
355 - name: Fetch the green release
356 if: github.event_name == 'workflow_dispatch'
357 uses: actions/download-artifact@v4
358 with:
359 name: release
360 path: stage
361 run-id: ${{ env.run_id }}
362 github-token: ${{ github.token }}
363
364 # Dispatch yolu gunler oncesine ait bir artefakt indirebilir; sunucuya
365 # dokunmadan once paketin butun oldugunu yeniden kontrol et.
366 - name: Check the release is complete
367 run: |
368 set -euo pipefail
369 test -f stage/hezarfen_zeka.tar.gz
370 test -f stage/compose.yaml
371 test -f stage/hezarfen_zeka_compose.service
372 test -f stage/tag
373
374 - name: Install SSH keys
375 run: |
376 mkdir -p ~/.ssh
377 echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa
378 ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts
379
380 - name: Upload the release
381 run: |
382 set -euo pipefail
383 ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"
384
385 # Sunucunun login shell'i bash degil, ssh bu betigi ona verirdi;
386 # `bash -s` betigi stdin'den alir, login shell yalniz tek duz komut
387 # gorur.
388 ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR'" <<'REMOTE'
389 set -euo pipefail
390 case "$1" in
391 ''|/*|*..*)
392 echo "Refusing a broad deploy subdirectory: '$1'" >&2
393 exit 1
394 ;;
395 esac
396 deploy_path=$HOME/$1
397 # Bu dizin operatorun hezarfen_zeka.env dosyasini, compose
398 # dosyalarini ve tag dosyalarini tutar; hicbiri toptan silinmez.
399 mkdir -p -- "$deploy_path"
400 REMOTE
401 # stdin paketi tasiyor, betik tasiyamaz; cikarma yine bash -c ile
402 # cunku login shell bash degil ve $HOME'u genisletmez. Sunucuya
403 # yalnizca dort deploy dosyasi kurulur.
404 tar -C stage -czf - hezarfen_zeka.tar.gz compose.yaml hezarfen_zeka_compose.service tag | ssh "$ssh_target" "bash -c 'exec tar -xzf - -C \"\$HOME/\$1\"' -- '$DEPLOY_SUBDIR'"
405
406 - name: Load the image and restart the bridge
407 run: |
408 set -euo pipefail
409 ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"
410
411 # bash -s, yukaridakiyle ayni sebeple: login shell bu betigi asla
412 # gormemeli.
413 ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR' '$SERVICE' '$ENV_FILE_NAME' '$CONTAINER_NAME' '$CERT_PATH' '$BACKEND_HTTP_PORT'" <<'REMOTE'
414 set -euo pipefail
415 deploy_path=$HOME/$1
416 service=$2
417 env_file=$deploy_path/$3
418 container=$4
419 cert_path=$5
420 backend_http_port=$6
421 export XDG_RUNTIME_DIR=/run/user/$(id -u)
422 # compose.yaml calisma dizininden cozulur: unit'in ExecStart'i
423 # WorkingDirectory tasir ama rollback'in compose up'i ve ilk
424 # deploy'un down'i tam buradan kosar.
425 cd -- "$deploy_path"
426
427 # Hicbir sey oynamadan once oku: ayar dosyasi operatorun, sunucuda,
428 # elle bir kez yazilir (anahtarlar icin bkz.
429 # deploy/hezarfen_zeka.env.example). Deploy onu ASLA olusturmaz,
430 # ezmez.
431 if [ ! -f "$env_file" ]; then
432 echo "missing $env_file -- create it (0600) from deploy/hezarfen_zeka.env.example" >&2
433 exit 1
434 fi
435 chmod 0600 -- "$env_file"
436
437 # OPERATOR ORTAMI SURECE ALINIR. podman-compose interpolasyonu ORTAM
438 # DEGISKENLERINE bakar ve `--env-file` YALNIZ SON dosyayi cozer
439 # (podman_compose.py:2941-2946, :2559): iki dosyali cagri operator
440 # dosyasini tamamen dusuruyordu ve compose.yaml'da zorunlu sirlar
441 # (`${VAR:?}`) oldugu icin `up` DAHA BASLAMADAN reddediyordu (canlida
442 # yasandi, 2026-09-17). Bu yuzden dosya satir satir okunur -- kabuk
443 # genisletmesi YOK (deger icindeki `$` oldugu gibi kalir), dosya
444 # bicimi duz KEY=value oldugu icin tirnak/`export` aranmaz.
445 while IFS= read -r satir || [ -n "$satir" ]; do
446 case "$satir" in ''|'#'*) continue ;; esac
447 anahtar=${satir%%=*}
448 deger=${satir#*=}
449 case "$anahtar" in
450 ''|*[!A-Za-z0-9_]*)
451 echo "operator env: gecersiz anahtar adi -- harf/rakam/alt cizgi beklenirdi" >&2
452 exit 1
453 ;;
454 esac
455 export "$anahtar=$deger"
456 done < "$env_file"
457
458 # KAPASITE ON KONTROLU -- aile deseni (RAG'in ayni adimi 12 GB / 20 GB
459 # esikleri kullaniyor; orada ~4,5 GB model + birkac GB RSS var ve
460 # GPU'suz makinede urun kapisi gecmiyor). ZEKA hafiftir: olculen imaj
461 # 152 MB (docs/DAGITIM.md), calisma zamani tek bir Python sureci,
462 # model yok. Esikler bu yuzden olculenin ~10 katidir: pay birakip
463 # gereksiz reddetmemek icin. Yetersizse OOM ile canli stack'i
464 # dusurmek yerine burada, NEDENIYLE reddedilir.
465 avail_mb=$(awk '/MemAvailable/ {print int($2/1024)}' /proc/meminfo)
466 disk_gb=$(df -BG --output=avail "$deploy_path" | tail -1 | tr -dc '0-9')
467 if [ "$avail_mb" -lt 512 ] || [ "$disk_gb" -lt 2 ]; then
468 echo "refusing to deploy: ${avail_mb} MB RAM available (need >= 512) and ${disk_gb} GB disk (need >= 2)." >&2
469 echo 'ZEKA is light (152 MB image, one Python process, no models) but a host without headroom will not get it; free space or deploy elsewhere.' >&2
470 exit 1
471 fi
472
473 # Rootless podman + compose saglayici; uc adim sonra ciplak
474 # command-not-found yerine cozumu soyle.
475 command -v podman >/dev/null || {
476 echo "podman not found -- install podman and a compose provider (e.g. dnf install podman podman-compose)" >&2
477 exit 1
478 }
479 podman compose version >/dev/null 2>&1 || {
480 echo "'podman compose' unavailable -- install a compose provider (podman-compose or docker-compose)" >&2
481 exit 1
482 }
483
484 # ON KOSUL, KAPI DEGIL: backend'in kopru dinleyicisi acik mi
485 # (AI_QUIC_ADDR=0.0.0.0:8090) -- sertifika ucu bunun kanitidir.
486 # Celebi bunu KAPI yapar; ZEKA'da bilerek UYARIDIR, cunku kopru
487 # backend yokken CIKMAZ: ustel geri cekilmeyle bekler ve backend
488 # duzeltilince kendiliginden kaydolur
489 # (src/bridge.py::run_forever, docs/DAGITIM.md "Servis ne yapar").
490 # Backend bakimda diye ZEKA'nin dagitimini reddetmek, servisin kendi
491 # sozlesmesinden DAHA SIKI bir sart koymak olurdu -- yani urunun
492 # tanimadigi bir "kayit" kapisi.
493 served_cert=$(curl -fsS "http://127.0.0.1:${backend_http_port}${cert_path}" 2>/dev/null) || true
494 case "$served_cert" in
495 *certificate_pem*) echo "ok: backend sertifikayi sunuyor (kopru kayit olabilir)" ;;
496 *)
497 echo "UYARI: backend ${cert_path} sertifika dondurmedi -- AI koprusu kapali olabilir (hezarfen_backend.env icinde AI_QUIC_ADDR=0.0.0.0:8090). Kopru baglanana kadar bekleyecek; bu TASARIM GEREGI bir bekleme halidir, ariza degil." >&2
498 ;;
499 esac
500
501 # BU build'in imajini yukle ve build'in damgaladigi tag'i oku
502 # (build zamani ile deploy zamani neyin gittiginde anlasmali).
503 gunzip -c "$deploy_path/hezarfen_zeka.tar.gz" | podman load
504 tag=$(cat -- "$deploy_path/tag")
505
506 # Tag rotasyonu: previous_tag geri donus hedefidir (ilk deploy'da
507 # yok); current_tag su an calismasi gerekeni kaydeder.
508 if [ -f "$deploy_path/current_tag" ]; then
509 cp -- "$deploy_path/current_tag" "$deploy_path/previous_tag"
510 fi
511 printf '%s\n' "$tag" > "$deploy_path/current_tag"
512
513 # stack.env deploy sahipli (operatorun ayar dosyasindan farkli):
514 # yalniz imaj tag'ini tasir ve compose'a giden TEK `--env-file` odur
515 # -- operator sirlari yukarida surece alindi. Iki dosyali cagri bu
516 # saglayicida operator dosyasini dusurur (podman_compose.py:2941-2946,
517 # :2559); ortam degiskeni --env-file degerlerini ezer (:2562).
518 printf 'HEZARFEN_TAG=%s\n' "$tag" > "$deploy_path/stack.env"
519
520 chmod 0644 -- "$deploy_path/compose.yaml" "$deploy_path/hezarfen_zeka_compose.service"
521 # Imaj tarball'i yalniz tasiyiciydi; imaj artik podman deposunda.
522 rm -f -- "$deploy_path/hezarfen_zeka.tar.gz"
523
524 compose_up() {
525 podman compose --env-file "$deploy_path/stack.env" up -d --no-build
526 }
527 rollback() {
528 if [ -f "$deploy_path/previous_tag" ]; then
529 prev=$(cat -- "$deploy_path/previous_tag")
530 printf 'HEZARFEN_TAG=%s\n' "$prev" > "$deploy_path/stack.env"
531 printf '%s\n' "$prev" > "$deploy_path/current_tag"
532 compose_up
533 echo "rolled back to $prev" >&2
534 else
535 # Ilk deploy: geri donulecek bir sey yok, yari ayakta stack'i
536 # durdur. Duz down -- `-v` YOK: zeka-fixtures isimli volume
537 # stop'ta yasamali.
538 podman compose --env-file "$deploy_path/stack.env" down
539 echo 'first deploy failed; stack stopped, volumes kept' >&2
540 fi
541 }
542
543 # Unit surumuyle birlikte gelir: yeni kurulmus bir kutuda ilk
544 # deploy'a kadar unit yoktur ve degisen unit onu gerektiren compose
545 # ile iner.
546 mkdir -p -- "$HOME/.config/systemd/user"
547 install -m 0644 -- "$deploy_path/hezarfen_zeka_compose.service" "$HOME/.config/systemd/user/$service.service"
548 systemctl --user daemon-reload
549
550 # Unit RemainAfterExit'li bir oneshot: aktif (exited) unit'te `start`
551 # no-op olurdu -- her deploy ExecStart'i yeniden kosmak icin restart
552 # eder. Dusen bir restart (kotu interpolasyon, saglayici aksakligi)
553 # yine de asagidaki saglik kapisina ulasmali: karari o verir.
554 systemctl --user enable -- "$service"
555 systemctl --user restart "$service" || true
556
557 # SAGLIK KAPISI -- SERVISIN KENDI betigi. `scripts/saglik.py`
558 # konteynerde her 30 saniyede kosan kontrolun TA KENDISIDIR; portu
559 # olmayan bir serviste disaridan yoklanacak bir ucu yoktur.
560 #
561 # NE ARAR: kopru surecinin YASADIGINI (zombi degil). NE ARAMAZ:
562 # backend'e KAYITLI oldugunu -- bu KASITLIDIR ve compose.yaml'daki
563 # `healthcheck` blogunun gerekcesidir: hazir olma sarti aransaydi
564 # backend'in her yeniden dagitimi ZEKA'yi `unhealthy` isaretler ve
565 # restart politikasi kacinmaya calistigimiz crash-loop'u geri
566 # getirirdi. Deploy da bir kayit kapisi UYDURMAZ; uyduracagi kapi
567 # servisin kendi tanimindan daha siki olurdu.
568 #
569 # 60 deneme: yorumlayici + aioquic import'u ve kap acilisi icin
570 # comert.
571 ready_ok=0
572 for _ in $(seq 1 60); do
573 if podman exec "$container" python scripts/saglik.py >/dev/null 2>&1; then
574 ready_ok=1
575 break
576 fi
577 sleep 1
578 done
579 if [ "$ready_ok" -ne 1 ]; then
580 echo "the health script never passed ('src.bridge' process is not alive); rolling back" >&2
581 podman logs --tail 30 "$container" >&2 || true
582 rollback
583 exit 1
584 fi
585
586 # Eski bir stack de saglikli olabilir; calisan konteyner BU build'in
587 # imaji olmali, yoksa geri donulur.
588 image=$(podman inspect -f '{{.Config.Image}}' "$container")
589 case "$image" in
590 *"$tag"*) ;;
591 *)
592 echo "the health gate passed, but the container runs '$image' -- not this build ($tag); rolling back" >&2
593 rollback
594 exit 1
595 ;;
596 esac
597 # OPERATOR SIRLARI KONTEYNERDE MI -- bu iddia olmadan iki dosyali
598 # `--env-file` sinifi sessizce geri gelebilir. O zaman `up` ya
599 # reddeder ya da (varsayilani olan degiskenlerde) container'i
600 # SESSIZCE compose varsayilaniyla ayaga kaldirirdi; ikisi de ayni
601 # siniftir. Deger YAZDIRILMAZ, yalniz "var ve bos degil" olculur.
602 # Desen `^AD=.`: satir basi + tek karakter -- env dokumu satir satir
603 # oldugu icin BOS deger (satir sonu) eslesmez.
604 container_env=$(podman inspect -f '{{range .Config.Env}}{{println .}}{{end}}' "$container")
605 eksik=0
606 for ad in AI_SHARED_TOKEN LLM_API_KEY AI_BRIDGE_HOST AI_SERVICE_NAME; do
607 if ! printf '%s\n' "$container_env" | grep -qE "^${ad}=."; then
608 echo "container env: $ad yok ya da BOS -- operator dosyasi compose'a ulasmamis" >&2
609 eksik=1
610 fi
611 done
612 if [ "$eksik" -ne 0 ]; then
613 echo "the operator env did not reach the container; rolling back" >&2
614 rollback
615 exit 1
616 fi
617 echo 'ok: operator sirlari konteynerde (AI_SHARED_TOKEN / LLM_API_KEY / AI_BRIDGE_HOST / AI_SERVICE_NAME dolu)'
618 echo 'hezarfen_zeka_compose is up'
619 REMOTE
620
621 # Bagimsiz son kontrol: cevap veren konteyner hala guncel tag'in olmali
622 # ve kopru yasiyor olmali -- servisin KENDI saglik betigiyle, yeniden.
623 - name: Verify the bridge is alive
624 run: |
625 set -euo pipefail
626 ssh_target="${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"
627 ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR' '$CONTAINER_NAME'" <<'REMOTE'
628 set -euo pipefail
629 deploy_path=$HOME/$1
630 container=$2
631 export XDG_RUNTIME_DIR=/run/user/$(id -u)
632 cd -- "$deploy_path"
633 tag=$(cat -- "$deploy_path/current_tag")
634 image=$(podman inspect -f '{{.Config.Image}}' "$container")
635 case "$image" in
636 *"$tag"*) ;;
637 *)
638 echo "container runs '$image', not the current tag '$tag'" >&2
639 exit 1
640 ;;
641 esac
642 status=$(podman inspect -f '{{.State.Status}}' "$container")
643 [ "$status" = "running" ] || { echo "container status is '$status', not running" >&2; exit 1; }
644 podman exec "$container" python scripts/saglik.py || {
645 echo "the bridge process is not alive (scripts/saglik.py failed)" >&2
646 exit 1
647 }
648 echo 'bridge alive, container is this build'
649 REMOTE
650
651 - name: Cleaning
652 run: rm -rf ~/.ssh