Skip to content

Commit

Permalink
Development (#38)
Browse files Browse the repository at this point in the history
* Fix error logging format

* Fix #28

* Update README.md

* Add inline_query in update_types

* Update dep

* Update dep (in requests fix bug "Unknown URL-schema http+docker")

* Downgrade requests to version 2.31.0. Tests failed.

* Update Dockerfiles

* Refactoring code

* Remove unused handler

* Update README.md

* Redesigned logic in Dockerfiles

* Update docker-compose.yml

* Update hub.alpine.Dockerfile

* Update installation.md

* Add correct logger stage in telebot polling arg

* Update docker_image_release.yml

* Refactoring dockerfiles

* Fix CI/CD error "buildx failed with: ERROR: failed to solve: process"

* Fix CI/CD error "buildx failed with: ERROR: failed to solve: process"

* Fix CI/CD error "buildx failed with: ERROR: failed to solve: process"

* update dep and fix #37

* Freeze deps in requirements.txt

* Refactoring code in docker section

* Refactoring code in docker section

* Refactoring code in docker section

* Update docs

* Update bot tree in README.md

* Bump version to 0.0.6 release
  • Loading branch information
orenlab committed May 23, 2024
1 parent bd57705 commit fe2f1d1
Show file tree
Hide file tree
Showing 33 changed files with 270 additions and 661 deletions.
5 changes: 1 addition & 4 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,9 @@ setup_req.txt
### Docker
# .dockerignore
.dockerignore
dev.Dockerfile
docker-compose.yml
Dockerfile
hub.alpine.Dockerfile
hub.ubuntu.Dockerfile
ubuntu.Dockerfile
hub.Dockerfile
#######################################
### pyTMbot
# app dir
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docker_build_on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Build the Docker image
run: docker build . --file hub.alpine.Dockerfile --tag orenlab/pytmbot:$(date +%s)
run: docker build . --target prod --tag orenlab/pytmbot:$(date +%s)
3 changes: 2 additions & 1 deletion .github/workflows/docker_dev_alpine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
- name: Build and push
uses: docker/build-push-action@v5
with:
file: hub.alpine.Dockerfile
build-args: --target prod
file: hub.Dockerfile
push: true
tags: orenlab/pytmbot:alpine-dev
26 changes: 0 additions & 26 deletions .github/workflows/docker_dev_ubuntu.yml

This file was deleted.

5 changes: 3 additions & 2 deletions .github/workflows/docker_image_release_cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ jobs:
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: orenlab/pytmbot
images: ${{ github.repository }}

- name: Build and push Docker image
id: push
uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
with:
context: .
file: hub.alpine.Dockerfile
file: Dockerfile
build-args: --target prod
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Expand Down
58 changes: 39 additions & 19 deletions .run/Dockerfile.run.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Dockerfile" type="docker-deploy" factoryName="dockerfile" server-name="Docker">
<deployment type="dockerfile">
<settings>
<option name="imageTag" value="latest"/>
<option name="containerName" value="pytmbot"/>
<option name="sourceFilePath" value="Dockerfile"/>
<option name="volumeBindings">
<list>
<DockerVolumeBindingImpl>
<option name="containerPath" value="/var/run/docker.sock"/>
<option name="hostPath" value="/var/run/docker.sock"/>
<option name="readOnly" value="true"/>
</DockerVolumeBindingImpl>
</list>
</option>
</settings>
</deployment>
<method v="2"/>
</configuration>
<configuration default="false" name="Dockerfile" type="docker-deploy" factoryName="dockerfile" server-name="Docker">
<deployment type="dockerfile">
<settings>
<option name="imageTag" value="latest" />
<option name="containerName" value="pytmbot" />
<option name="sourceFilePath" value="Dockerfile" />
<option name="volumeBindings">
<list>
<DockerVolumeBindingImpl>
<option name="containerPath" value="/var/run/docker.sock" />
<option name="hostPath" value="/var/run/docker.sock" />
<option name="readOnly" value="true" />
</DockerVolumeBindingImpl>
</list>
</option>
</settings>
</deployment>
<method v="2" />
</configuration>
<configuration default="false" name="Dockerfile" type="docker-deploy" factoryName="dockerfile" server-name="Docker">
<deployment type="dockerfile">
<settings>
<option name="imageTag" value="dev-latest-loc" />
<option name="buildCliOptions" value="--target selfbuild_dev" />
<option name="containerName" value="pytmbot" />
<option name="sourceFilePath" value="Dockerfile" />
<option name="volumeBindings">
<list>
<DockerVolumeBindingImpl>
<option name="containerPath" value="/var/run/docker.sock" />
<option name="hostPath" value="/var/run/docker.sock" />
<option name="readOnly" value="true" />
</DockerVolumeBindingImpl>
</list>
</option>
</settings>
</deployment>
<method v="2" />
</configuration>
</component>
21 changes: 0 additions & 21 deletions .run/Dockerfile_dev.run.xml

This file was deleted.

125 changes: 72 additions & 53 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
#######################################
# pyTMbot Dockerfile (based on Alpine)
# image size: ~ 90Mb
#############################################################
## pyTMbot Dockerfile
# https://github.com/orenlab/pytmbot
#######################################
#
# To launch with a production token. Default way:
# docker --target prod build -t orenlab/pytmbot:latest .
#
# To launch with a development token. Only for development:
# docker --target dev build -t orenlab/pytmbot:latest .
#############################################################

# Set Alpine tag version for first and second stage
ARG IMAGE_VERSION_FIRST=3.12.3-alpine3.19
ARG IMAGE_VERSION_SECOND=3.19.1
# Set base images tag
ARG PYTHON_IMAGE=3.12.3-alpine3.19
ARG ALPINE_IMAGE=3.19.1

# First stage
FROM python:$IMAGE_VERSION_FIRST AS builder
# Python version (minimal - 3.12)
ARG PYTHON_VERSION=3.12
########################################################################################################################
######################### BUILD ALPINE BASED IMAGE #####################################################################
########################################################################################################################

COPY requirements.txt .
# Zero Alpine stage - setup base image
FROM alpine:${ALPINE_IMAGE} AS alpine_base

# Update base os components and install all deps (need to build psutil)
# Update base os components
RUN apk --no-cache update && \
apk --no-cache upgrade && \
apk --no-cache add gcc python3-dev musl-dev linux-headers

# Install dependencies to the venv path
RUN python$PYTHON_VERSION -m venv --without-pip venv
RUN pip install --no-cache --target="/venv/lib/python$PYTHON_VERSION/site-packages" -r requirements.txt

RUN python -m pip uninstall pip setuptools python3-wheel python3-dev -y

# Second unnamed stage
FROM alpine:$IMAGE_VERSION_SECOND
# Python version (minimal - 3.12)
ARG PYTHON_VERSION=3.12

# Add Timezone support in Alpine image
RUN apk --no-cache add tzdata
apk --no-cache add tzdata

# App workdir
WORKDIR /opt/pytmbot/
Expand All @@ -43,42 +35,69 @@ ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONPATH=/opt/pytmbot
ENV PATH=/venv/bin:$PATH

# Copy lisence
COPY LICENSE /opt/pytmbot/

# Copy bot files
COPY ./app ./app/
COPY ./logs /opt/logs/

# First Alpine stage - build Python deps
FROM python:${PYTHON_IMAGE} AS builder

# Python version (minimal - 3.12)
ARG PYTHON_VERSION=3.12

COPY requirements.txt .

# Install all deps (need to build psutil)
RUN apk --no-cache add gcc python3-dev musl-dev linux-headers

# Install dependencies to the venv path
RUN python${PYTHON_VERSION} -m venv --without-pip venv
RUN pip install --no-cache-dir --no-deps --target="/venv/lib/python${PYTHON_VERSION}/site-packages" -r requirements.txt

RUN python${PYTHON_VERSION} -m pip uninstall pip setuptools python3-wheel python3-dev musl-dev -y

# Second Alpine stage - based on the base stage. Setup bot
FROM alpine_base AS reliase_base

# Python version (minimal - 3.12)
ARG PYTHON_VERSION=3.12

# Сopy only the necessary python files and directories from first stage
COPY --from=builder /usr/local/bin/python3 /usr/local/bin/python3
COPY --from=builder /usr/local/bin/python$PYTHON_VERSION /usr/local/bin/python$PYTHON_VERSION
COPY --from=builder /usr/local/lib/python$PYTHON_VERSION /usr/local/lib/python$PYTHON_VERSION
COPY --from=builder /usr/local/lib/libpython$PYTHON_VERSION.so.1.0 /usr/local/lib/libpython$PYTHON_VERSION.so.1.0
COPY --from=builder /usr/local/bin/python${PYTHON_VERSION} /usr/local/bin/python${PYTHON_VERSION}
COPY --from=builder /usr/local/lib/python${PYTHON_VERSION} /usr/local/lib/python${PYTHON_VERSION}
COPY --from=builder /usr/local/lib/libpython${PYTHON_VERSION}.so.1.0 /usr/local/lib/libpython${PYTHON_VERSION}.so.1.0
COPY --from=builder /usr/local/lib/libpython3.so /usr/local/lib/libpython3.so

# Copy only the dependencies installation from the first stage image
COPY --from=builder /venv /venv

# Copy .pytmbotenv file with token (prod, dev)
COPY .pytmbotenv /opt/pytmbot
# activate venv
RUN source /venv/bin/activate && \
# forward logs to Docker's log collector
ln -sf /dev/stdout /opt/logs/pytmbot.log

# Copy lisence
COPY LICENSE /opt/pytmbot

# Copy bot files
COPY ./app ./app/
COPY ./logs /opt/logs/
# Target for CI/CD image, --mode = prod
FROM reliase_base AS prod

CMD [ "/venv/bin/python3", "app/main.py", "--log-level=INFO", "--mode=prod" ]

# Update base os components
RUN apk --no-cache update && \
apk --no-cache upgrade && \
# activate venv
source /venv/bin/activate && \
# forward logs to Docker's log collector
ln -sf /dev/stdout /opt/logs/pytmbot.log
# Target for self biuld image, --mode = prod
FROM reliase_base AS selfbuild_prod

# Copy .pytmbotenv file with token (prod, dev)
COPY .pytmbotenv /opt/pytmbot/

# Run app
# !!! needed set log level:
# - DEBUG
# - INFO (default)
# - ERROR
# - CRITICAL
# !!! needed set pyTMBot mode:
# - dev
# - prod (default)
CMD [ "/venv/bin/python3", "app/main.py", "--log-level=INFO", "--mode=prod" ]

# Target for self biuld image, --mode = dev
FROM reliase_base AS selfbuild_dev

# Copy .pytmbotenv file with token (prod, dev)
COPY .pytmbotenv /opt/pytmbot/

CMD [ "/venv/bin/python3", "app/main.py", "--log-level=INFO", "--mode=dev" ]
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The bot operates synchronously. It does not use webhooks.
[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=orenlab_pytmbot&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=orenlab_pytmbot)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/abe0314bb5c24cfda8db9c0a293d17c0)](https://app.codacy.com/gh/orenlab/pytmbot/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
[![Docker Image Build CI/CD](https://github.com/orenlab/pytmbot/actions/workflows/docker_build_on_push.yml/badge.svg)](https://github.com/orenlab/pytmbot/actions/workflows/docker_build_on_push.yml)
![Docker Pulls](https://img.shields.io/docker/pulls/orenlab/pytmbot?link=https%3A%2F%2Fhub.docker.com%2Fr%2Forenlab%2Fpytmbot)

The bot was written using the [pyTelegramBotAPI](https://github.com/eternnoir/pyTelegramBotAPI).
Use [psutil](https://github.com/giampaolo/psutil) and [docker-py](https://github.com/docker/docker-py) libraries for
Expand Down Expand Up @@ -88,8 +89,7 @@ All failed attempts to authorize are logged with an `ERROR` flag.
│ │ │ ├── handler.py - Base handler class (abc)
│ │ │ ├── handlers_aggregator.py - Main handlers aggregator
│ │ │ └── inline_handlers
│ │ │ ├── __init__.py - Import all inline handlers
│ │ │ ├── inline_query_handler.py - Deprecated in next release
│ │ │ ├── __init__.py
│ │ │ └── swap_handler.py - Swap inline handler
│ │ ├── jinja2
│ │ │ ├── __init__.py
Expand Down Expand Up @@ -124,15 +124,13 @@ All failed attempts to authorize are logged with an `ERROR` flag.
│ ├── cfg_templates
│ │ └── env.py - Template for initial setup
│ └── fs.py - Filesystem utility
├── dev.Dockerfile - Dockerfile with mode=dev
├── docker-compose.yml - Docker Compose file (used main Dockerfile)
├── docs
│ ├── docker.md - README for hub.docker.com
│ ├── installation.md - Installation guide
│ ├── roadmap.md - Roadmap guide
│ └── screenshots.md - Bots screenshot
├── hub.alpine.Dockerfile - Dockerfile for Docker CI/CD based on Alpine
├── hub.ubuntu.Dockerfile - Dockerfile for Docker CI/CD based on Ubuntu
├── hub.Dockerfile - Dockerfile for Docker CI/CD based on Alpine
├── logs
│ └── pytmbot.log - Main logs file
├── poetry.lock - Poetry file
Expand All @@ -142,7 +140,6 @@ All failed attempts to authorize are logged with an `ERROR` flag.
├── setup_req.txt - Setup requirements
├── tests
│ └── bot_tests.py - Bots tests
└── ubuntu.Dockerfile - Dockerfile, based on Ubuntu image
```

## 📈 Roadmap
Expand Down
4 changes: 2 additions & 2 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

| Version | Supported |
|---------|--------------------|
| 0.0.5 | :white_check_mark: |
| < 0.0.5 | :x: |
| 0.0.6 | :white_check_mark: |
| < 0.0.6 | :x: |

## Reporting a Vulnerability

Expand Down
Loading

0 comments on commit fe2f1d1

Please sign in to comment.