From 9e456f776f797bfbaeacdf35784fdf2e5ae6da34 Mon Sep 17 00:00:00 2001 From: Artem Astapenko <3767150+jamakase@users.noreply.github.com> Date: Tue, 10 Sep 2024 16:53:24 +0300 Subject: [PATCH] Update UI env (#36) * Add ui env vars * Add openrouter models * Add prod deployment * Ignore keyfiles --- .github/workflows/backend.yaml | 36 +- .github/workflows/ml.yaml | 55 + Makefile | 11 +- docker-compose.yaml | 52 +- frontend/.dockerignore | 1 + frontend/src/app/layout.tsx | 10 +- frontend/src/app/page.tsx | 17 +- frontend/src/{app => domain/api}/api.tsx | 26 +- frontend/src/domain/config/ConfigProvider.tsx | 26 + frontend/src/domain/config/clientConfig.ts | 3 + frontend/src/domain/config/index.tsx | 3 - infra/production/.gitignore | 2 + infra/production/.terraform.lock.hcl | 10 + infra/production/dns.tf | 23 + infra/production/files/docker-compose.yaml | 159 +-- infra/production/main.tf | 28 +- infra/production/vpc.tf | 41 + services/ml/app/config/config.py | 5 +- services/ml/app/server.py | 29 +- services/ml/poetry.lock | 1236 ++++++++++++++++- services/ml/pyproject.toml | 3 + 21 files changed, 1611 insertions(+), 165 deletions(-) create mode 100644 .github/workflows/ml.yaml create mode 100644 frontend/.dockerignore rename frontend/src/{app => domain/api}/api.tsx (54%) create mode 100644 frontend/src/domain/config/ConfigProvider.tsx create mode 100644 frontend/src/domain/config/clientConfig.ts delete mode 100644 frontend/src/domain/config/index.tsx create mode 100644 infra/production/.gitignore create mode 100644 infra/production/.terraform.lock.hcl create mode 100644 infra/production/dns.tf create mode 100644 infra/production/vpc.tf diff --git a/.github/workflows/backend.yaml b/.github/workflows/backend.yaml index 592ab57..79081a8 100644 --- a/.github/workflows/backend.yaml +++ b/.github/workflows/backend.yaml @@ -2,8 +2,8 @@ name: Backend CI workflow on: push: paths: - - "frontend/**" - - ".github/workflows/frontend.yaml" + - "services/backend/**" + - ".github/workflows/backend.yaml" branches: - main @@ -12,20 +12,23 @@ jobs: runs-on: ubuntu-latest defaults: run: - working-directory: ./frontend + working-directory: ./services/backend steps: - name: Clone repository uses: actions/checkout@v4 - - name: Use Node.js 20.x and cache dependencies - uses: actions/setup-node@v3 + - name: Set up Python 3.11 + uses: actions/setup-python@v4 with: - node-version: 20.x - cache: 'npm' - cache-dependency-path: '**/package-lock.json' + python-version: 3.11 - - run: npm ci - - run: npm run build + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + # - name: Run tests + # run: python -m unittest discover tests build-container: if: github.ref == 'refs/heads/main' @@ -34,7 +37,7 @@ jobs: runs-on: ubuntu-latest defaults: run: - working-directory: ./frontend + working-directory: ./services/backend steps: - name: Clone repository uses: actions/checkout@v4 @@ -42,12 +45,11 @@ jobs: id: login-cr uses: yc-actions/yc-cr-login@v2 with: - yc-sa-json-credentials: ${{ secrets.YANDEX_AUTH_KEY }} + yc-sa-json-credentials: ${{ secrets.YANDEX_SA_KEY }} - run: | echo "Packaging docker image ..." - tag="${DOCKER_REGISTRY}/app:${{ github.sha }}" - docker build -t $tag -t ${DOCKER_REGISTRY}/app:latest . - docker push ${DOCKER_REGISTRY}/app --all-tags + tag="${DOCKER_REGISTRY}/backend:${{ github.sha }}" + docker build -t $tag -t ${DOCKER_REGISTRY}/backend:latest . + docker push ${DOCKER_REGISTRY}/backend --all-tags env: - NEXT_PUBLIC_API_URL: https://api.afana-propdoc.ru - DOCKER_REGISTRY: cr.yandex/ + DOCKER_REGISTRY: cr.yandex/crpc50gkvq2bp251sfgb diff --git a/.github/workflows/ml.yaml b/.github/workflows/ml.yaml new file mode 100644 index 0000000..1701c81 --- /dev/null +++ b/.github/workflows/ml.yaml @@ -0,0 +1,55 @@ +name: ML CI workflow +on: + push: + paths: + - "services/ml/**" + - ".github/workflows/ml.yaml" + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + defaults: + run: + working-directory: ./services/ml + steps: + - name: Clone repository + uses: actions/checkout@v4 + + - name: Set up Python 3.11 + uses: actions/setup-python@v4 + with: + python-version: 3.11 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + # - name: Run tests + # run: python -m unittest discover tests + + build-container: + if: github.ref == 'refs/heads/main' + needs: + - build + runs-on: ubuntu-latest + defaults: + run: + working-directory: ./services/ml + steps: + - name: Clone repository + uses: actions/checkout@v4 + - name: Login to Yandex Cloud Container Registry + id: login-cr + uses: yc-actions/yc-cr-login@v2 + with: + yc-sa-json-credentials: ${{ secrets.YANDEX_SA_KEY }} + - run: | + echo "Packaging docker image ..." + tag="${DOCKER_REGISTRY}/ml:${{ github.sha }}" + docker build -t $tag -t ${DOCKER_REGISTRY}/ml:latest . + docker push ${DOCKER_REGISTRY}/ml --all-tags + env: + DOCKER_REGISTRY: cr.yandex/crpc50gkvq2bp251sfgb diff --git a/Makefile b/Makefile index 7910181..311c3f9 100644 --- a/Makefile +++ b/Makefile @@ -11,4 +11,13 @@ down: docker compose down build: - docker compose build \ No newline at end of file + docker compose build + +restart-service: + docker-compose -f docker-compose.yaml up -d $(svc) + +build-service: + docker-compose -f docker-compose.yaml build $(svc) + +rb: + make build-service $(svc) && make restart-service $(svc) \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 4b2e73d..2638279 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,12 +1,35 @@ version: '3.8' services: + traefik: + image: traefik:v2.11 + container_name: traefik + command: + - "--api.insecure=true" + - "--providers.docker=true" + - "--entrypoints.web.address=:80" + - "--entrypoints.websecure.address=:443" + ports: + - "80:80" + - "443:443" + - "8080:8080" + volumes: + - /var/run/docker.sock:/var/run/docker.sock + - ./data/ssl:/letsencrypt + restart: always + frontend_app: + container_name: frontend_app build: context: ./frontend dockerfile: Dockerfile - ports: - - "8080:3000" + environment: + - API_URL=http://api.${HOST} + labels: + - "traefik.enable=true" + - "traefik.http.routers.app.rule=Host(`${HOST}`)" + - "traefik.http.routers.app.entrypoints=web" + - "traefik.http.services.app.loadbalancer.server.port=3000" backend_app: restart: unless-stopped @@ -24,8 +47,11 @@ services: depends_on: - db - redis - ports: - - "5000:5000" + labels: + - "traefik.enable=true" + - "traefik.http.routers.api.rule=Host(`api.${HOST}`)" + - "traefik.http.routers.api.entrypoints=web" + - "traefik.http.services.api.loadbalancer.server.port=5000" db: container_name: db @@ -57,7 +83,7 @@ services: celery_worker: build: context: ./services/backend - dockerfile: Dockerfile-celery + dockerfile: Dockerfile command: celery -A celery_worker.celery worker --loglevel=info volumes: - ./services/backend:/app @@ -70,22 +96,6 @@ services: - redis - backend_app - - # naive_ml_app: - # build: - # context: ./services/backend - # dockerfile: Dockerfile - # volumes: - # - ./services/backend/naive_ML_for_tests:/app - # environment: - # - DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB} - # depends_on: - # - db - # command: python3 -m services.backend.naive_ML_for_tests.run - # ports: - # - "5001:5001" - # working_dir: /app - ml: build: context: ./services/ml diff --git a/frontend/.dockerignore b/frontend/.dockerignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/frontend/.dockerignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/frontend/src/app/layout.tsx b/frontend/src/app/layout.tsx index fb7ea61..15895c2 100644 --- a/frontend/src/app/layout.tsx +++ b/frontend/src/app/layout.tsx @@ -2,6 +2,7 @@ import type { Metadata } from "next"; import { Roboto_Mono } from "next/font/google"; import { Inter } from "next/font/google"; import "./globals.css"; +import { ConfigProvider } from "@/domain/config/ConfigProvider"; const robotoMono = Roboto_Mono({ subsets: ["latin", "cyrillic"], weight: ['400', '700'], display: 'swap' }); const inter = Inter({ subsets: ["latin", "cyrillic"] }); @@ -14,7 +15,7 @@ export const metadata: Metadata = { }, }; -export default function RootLayout({ +export default async function RootLayout({ children, }: Readonly<{ children: React.ReactNode; @@ -24,9 +25,12 @@ export default function RootLayout({
- - {children} + +