diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..66434b1 --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +ENV=local \ No newline at end of file diff --git a/.gitignore b/.gitignore index 8d44196..84f55c9 100644 --- a/.gitignore +++ b/.gitignore @@ -220,3 +220,5 @@ FodyWeavers.xsd *.msp *.sln.iml .pydevproject +*/.venv/ +*/venv/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a1121bb --- /dev/null +++ b/Makefile @@ -0,0 +1,47 @@ +SHELL := /bin/bash +DOCKER_USER=mesteriis +DOCKER_REPO=hassio +GIT_SHA=$(shell git rev-parse --short HEAD) +BASE_IMAGE_DOCKERFILES=$(shell find ./base_docker_images/ -name 'Dockerfile') + + +ifneq ("$(wildcard .env)", "") + include .env + export $(shell sed 's/=.*//' .env) + +else + $(shell cp .env.example .env) + include .env + export $(shell sed 's/=.*//' .env) +endif + +ifneq ("$(wildcard .env)", "") + $(info $(shell echo "Loading environment variables from .env file...")) + $(info $(shell cat .env)) + $(info $(shell echo "***********************************************")) +endif + +.PHONY: build push all + +all: build push + +build: + @echo "Собираем все Docker образы..." + @for dockerfile in $(BASE_IMAGE_DOCKERFILES); do \ + dir=$$(dirname $$dockerfile); \ + tag_name=$$(echo $$dir | sed 's|^\./||' | tr '/' '-' | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]'); \ + base_tag="$(DOCKER_USER)/$(DOCKER_REPO)-$$tag_name"; \ + echo "Собираем образ $$base_tag:$(GIT_SHA)"; \ + docker build -t $$base_tag:$(GIT_SHA) -t $$base_tag:latest $$dir; \ + done + +push: + @echo "Отправляем образы в Docker Hub..." + @for dockerfile in $(BASE_IMAGE_DOCKERFILES); do \ + dir=$$(dirname $$dockerfile); \ + tag_name=$$(echo $$dir | sed 's|^\./||' | tr '/' '-' | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]'); \ + base_tag="$(DOCKER_USER)/$(DOCKER_REPO)-$$tag_name"; \ + echo "Пушим образ $$base_tag:$(GIT_SHA)"; \ + docker push $$base_tag:$(GIT_SHA); \ + docker push $$base_tag:latest; \ + done \ No newline at end of file diff --git a/base_docker_images/build_local.sh b/base_docker_images/build_local.sh new file mode 100755 index 0000000..a566507 --- /dev/null +++ b/base_docker_images/build_local.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +DOCKER_USER="mesteriis" +DOCKER_REPO="hassio" + +find . -name 'Dockerfile' | while read dockerfile; do + dir=$(dirname "$dockerfile") + tag_name=$(echo "$dir" | sed 's|^\./||' | tr '/' '-' | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]') + git_sha=$(git rev-parse --short HEAD) + + base_tag="${DOCKER_USER}/${DOCKER_REPO}-${tag_name}" + command="docker build -t ${base_tag}:${git_sha} -t ${base_tag}:latest $dir" + echo $command + docker build -t ${base_tag}:${git_sha} -t ${base_tag}:latest $dir + docker push ${base_tag}:${git_sha} + docker push ${base_tag}:latest + +done diff --git a/base_docker_images/python/3.11/Makefile b/base_docker_images/python/3.11/Makefile new file mode 100644 index 0000000..0770c43 --- /dev/null +++ b/base_docker_images/python/3.11/Makefile @@ -0,0 +1,3 @@ +build: + @echo "Building base docker images..." + @docker build Dockerfile_python -t base_python:latest \ No newline at end of file diff --git a/blank-fastapi-vue-hassio/Dockerfile b/blank-fastapi-vue-hassio/Dockerfile old mode 100755 new mode 100644 index d576a32..e8d0223 --- a/blank-fastapi-vue-hassio/Dockerfile +++ b/blank-fastapi-vue-hassio/Dockerfile @@ -1,49 +1,25 @@ -ARG BUILD_FROM=hassioaddons/base-python:5.2.0 -# hadolint ignore=DL3006 -FROM ${BUILD_FROM} +FROM python:3.11-buster -# Set shell -SHELL ["/bin/bash", "-o", "pipefail", "-c"] +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + build-essential \ + libssl-dev \ + libffi-dev \ + python3-dev \ + libpq-dev \ + libjpeg-dev \ + libwebp-dev \ + libtiff5-dev \ + zlib1g-dev \ + libxml2-dev \ + libxslt1-dev \ + libldap2-dev \ + libsasl2-dev \ + libmysqlclient-dev \ + default-libmysqlclient-dev && \ + apt-get clean && rm -rf /var/lib/apt/lists/* -# Copy Python requirements file -COPY hello_flask/requirements.txt /tmp/ -COPY hello_flask/app.py / - -# Setup base -ARG BUILD_ARCH=amd64 -RUN \ - apk add --no-cache --virtual .build-dependencies \ - g++=9.2.0-r4 \ - gcc=9.2.0-r4 \ - make=4.2.1-r2 \ - \ - && apk add --no-cache \ - nginx-mod-http-lua=1.16.1-r6 \ - lua-resty-http=0.15-r0 \ - nginx=1.16.1-r6 \ - cython=0.29.14-r0 \ - \ - && pip3 install \ - --no-cache-dir \ - --prefer-binary \ - --find-links "https://wheels.hass.io/alpine-3.11/${BUILD_ARCH}/" \ - -r /tmp/requirements.txt \ - \ - && find /usr/local \ - \( -type d -a -name test -o -name tests -o -name '__pycache__' \) \ - -o \( -type f -a -name '*.pyc' -o -name '*.pyo' \) \ - -exec rm -rf '{}' + \ - \ - && apk del --purge .build-dependencies \ - && rm -f -r \ - /etc/nginx \ - /tmp/* - -# Copy data for add-on -COPY run.sh / -COPY hello_flask/requirements.txt /tmp/ -# Install requirements for add-on -RUN pip install -r /tmp/requirements.txt - -RUN chmod a+x /run.sh -CMD [ "/run.sh" ] \ No newline at end of file +RUN pip install --upgrade pip +RUN pip install --upgrade setuptools +RUN pip install --upgrade wheel +RUN pip install uv \ No newline at end of file diff --git a/blank-fastapi-vue-hassio/Makefile b/blank-fastapi-vue-hassio/Makefile index e403b55..9fdfa61 100644 --- a/blank-fastapi-vue-hassio/Makefile +++ b/blank-fastapi-vue-hassio/Makefile @@ -28,3 +28,6 @@ run: @echo "Running server..." uvicorn app.main:app --host localhost --port 8000 @echo "Server running at http://localhost:8000" + + + diff --git a/blank-fastapi-vue-hassio/_Dockerfile b/blank-fastapi-vue-hassio/_Dockerfile new file mode 100755 index 0000000..5033643 --- /dev/null +++ b/blank-fastapi-vue-hassio/_Dockerfile @@ -0,0 +1,49 @@ +#ARG BUILD_FROM=hassioaddons/base-python:5.2.0 +# hadolint ignore=DL3006 +FROM hassioaddons/base-python:5.2.0 + +# Set shell +SHELL ["/bin/bash", "-o", "pipefail", "-c"] + +# Copy Python requirements file +COPY requirements.txt /tmp/ +COPY src /app + +# Setup base +ARG BUILD_ARCH=amd64 +RUN \ + apk add --no-cache --virtual .build-dependencies \ + g++ \ + gcc \ + make \ + \ + && apk add --no-cache \ + nginx-mod-http-lua=1.16.1-r6 \ + lua-resty-http=0.15-r0 \ + nginx=1.16.1-r6 \ + cython=0.29.14-r0 \ + \ + && pip3 install \ + --no-cache-dir \ + --prefer-binary \ + --find-links "https://wheels.hass.io/alpine-3.11/${BUILD_ARCH}/" \ + -r /tmp/requirements.txt \ + \ + && find /usr/local \ + \( -type d -a -name test -o -name tests -o -name '__pycache__' \) \ + -o \( -type f -a -name '*.pyc' -o -name '*.pyo' \) \ + -exec rm -rf '{}' + \ + \ + && apk del --purge .build-dependencies \ + && rm -f -r \ + /etc/nginx \ + /tmp/* + +# Copy data for add-on +COPY run.sh / +COPY requirements.txt /tmp/ +# Install requirements for add-on +RUN pip install -r /tmp/requirements.txt + +RUN chmod a+x /run.sh +CMD [ "/run.sh" ] \ No newline at end of file diff --git a/blank-fastapi-vue-hassio/docker-compose.yml b/blank-fastapi-vue-hassio/docker-compose.yml index ace4d97..48b6838 100644 --- a/blank-fastapi-vue-hassio/docker-compose.yml +++ b/blank-fastapi-vue-hassio/docker-compose.yml @@ -1,147 +1,16 @@ version: "3" services: - db: - image: postgres:13-alpine - container_name: social_db - restart: always - environment: - - POSTGRES_HOST_AUTH_METHOD=trust - volumes: - - ./docker/data/db/data:/var/lib/postgresql/data - - ./docker/data/db/backup:/backup - - ./etc/init-user.sql:/docker-entrypoint-initdb.d/init-user.sql - ports: - - "8432:5432" - healthcheck: - test: ["CMD-SHELL", "pg_isready -U postgres"] - interval: 10s - timeout: 5s - retries: 5 - - app: build: context: . args: DEV: "true" ports: - - "8080:8080" + - "8000:8000" restart: always container_name: social_app env_file: - .env - links: - - db - depends_on: - db: - condition: service_healthy - command: sh -c "aerich upgrade && uvicorn main:app --reload --host 0.0.0.0 --port 8080" - volumes: - - ./src:/src/ + command: sh -c "uvicorn main:app --reload --host 0.0.0.0 --port 8000" - rabbitmq: - container_name: social_rabbitmq - image: rabbitmq:3-management-alpine - ports: - - "3672:5672" - - "14672:15672" - healthcheck: - test: ["CMD", "rabbitmqctl", "status"] - interval: 10s - timeout: 5s - retries: 5 - - taskiq-worker: - build: - context: . - dockerfile: Dockerfile - image: social_taskiq - container_name: social_taskiq - env_file: .env - restart: always - volumes: - - ./src:/src - links: - - db - - rabbitmq - depends_on: - db: - condition: service_healthy - rabbitmq: - condition: service_healthy - command: [ taskiq, worker, core.tkq:broker, -fsd] - ports: [] - - taskiq-faststream-scheduler: - build: - context: . - dockerfile: Dockerfile - image: social_taskiq_faststream_scheduler - container_name: social_taskiq_faststream_scheduler - env_file: .env - restart: always - volumes: - - ./src:/src - links: - - db - - rabbitmq - depends_on: - db: - condition: service_healthy - rabbitmq: - condition: service_healthy - command: [ taskiq, scheduler, core.tkq_faststream:scheduler, -fsd] - ports: [] - - taskiq-scheduler: - build: - context: . - dockerfile: Dockerfile - image: social_taskiq_scheduler - container_name: social_taskiq_scheduler - env_file: .env - restart: always - volumes: - - ./src:/src - links: - - db - - rabbitmq - depends_on: - db: - condition: service_healthy - rabbitmq: - condition: service_healthy - command: [ taskiq, scheduler, core.tkq:scheduler, -fsd] - ports: [] - - redis: - image: redis:6.0-alpine - container_name: social_redis - restart: always - volumes: - - ./docker/data/redis:/data - ports: - - 6377:6379 - healthcheck: - test: ["CMD", "redis-cli", "ping"] - interval: 10s - timeout: 5s - retries: 5 - - faststream_worker: - build: - context: . - dockerfile: Dockerfile - env_file: .env - links: - - db - - redis - depends_on: - app: - condition: service_started - db: - condition: service_healthy - redis: - condition: service_healthy - command: python core/faststream_worker.py diff --git a/blank-fastapi-vue-hassio/requirements.txt b/blank-fastapi-vue-hassio/requirements.txt index 8099dae..ac0787c 100644 --- a/blank-fastapi-vue-hassio/requirements.txt +++ b/blank-fastapi-vue-hassio/requirements.txt @@ -21,6 +21,7 @@ pre-commit==4.1.0 pydantic==2.10.6 pydantic-core==2.27.2 pytest==8.3.5 +pytest-asyncio==0.25.3 pytest-cov==6.0.0 pytest-mock==3.14.0 pyyaml==6.0.2 diff --git a/blank-fastapi-vue-hassio/run.sh b/blank-fastapi-vue-hassio/run.sh index 99c4538..2962152 100755 --- a/blank-fastapi-vue-hassio/run.sh +++ b/blank-fastapi-vue-hassio/run.sh @@ -1,5 +1,5 @@ #!/usr/bin/with-contenv bashio echo Running flask hello world -uvicorn app.main:app --host localhost --port 8000 +uvicorn main:app --host localhost --port 8000