Skip to content

Commit

Permalink
Launching the bot now using ENTRYPOINT.SH
Browse files Browse the repository at this point in the history
  • Loading branch information
orenlab committed Aug 23, 2024
1 parent f67ece6 commit 99770e2
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .run/Dockerfile.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<settings>
<option name="imageTag" value="orenlab/pytmbot:latest" />
<option name="buildCliOptions" value="--target self_build" />
<option name="command" value="--log-level=DEBUG --mode=dev --colorize_logs=False" />
<option name="command" value="--log-level DEBUG --mode dev" />
<option name="containerName" value="pytmbot" />
<option name="showCommandPreview" value="true" />
<option name="sourceFilePath" value="Dockerfile" />
Expand Down
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ ENV PATH=/venv/bin:$PATH
# Copy bot files
COPY ./pytmbot ./pytmbot
COPY ./main.py ./main.py
COPY ./entrypoint.sh ./entrypoint.sh
RUN chmod +x ./entrypoint.sh


# Сopy only the necessary python files and directories from first stage
Expand All @@ -77,17 +79,17 @@ RUN source /venv/bin/activate && \
# Target for CI/CD image
FROM reliase_base AS production

ENTRYPOINT [ "/venv/bin/python3", "main.py" ]
ENTRYPOINT ["./entrypoint.sh"]

# Target for self biuld image, --mode = prod
FROM reliase_base AS self_build

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

ENTRYPOINT [ "/venv/bin/python3", "main.py" ]
ENTRYPOINT ["./entrypoint.sh"]

# Target for CI/CD stable tag (0.0.9, 0.1.1, latest)
FROM reliase_base AS prod

ENTRYPOINT [ "/venv/bin/python3", "main.py" ]
ENTRYPOINT ["./entrypoint.sh"]
39 changes: 39 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh

# (c) Copyright 2024, Denis Rozhnovskiy <pytelemonbot@mail.ru>
# pyTMBot - A simple Telegram bot to handle Docker containers and images,
# also providing basic information about the status of local servers.


while [[ $# -gt 0 ]]; do
case "$1" in
--log-level)
LOG_LEVEL="$2"
shift 2
;;
--mode)
MODE="$2"
shift 2
;;
--salt)
SALT="$2"
shift 2
;;
*)
echo "Unknown argument: $1"
exit 1
;;
esac
done

# Set default values if not provided
LOG_LEVEL=${LOG_LEVEL:INFO}
MODE=${MODE:prod}
SALT=${SALT:-false}

# Check if --salt was provided
if [ "$SALT" = true ]; then
/venv/bin/python3 pytmbot/utils/salt.py
else
/venv/bin/python3 main.py --log-level "$LOG_LEVEL" --mode "$MODE"
fi
11 changes: 2 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
also providing basic information about the status of local servers.
"""

from pytmbot.utils.utilities import parse_cli_args, generate_random_auth_salt

if __name__ == "__main__":
cli_args = parse_cli_args()
if cli_args.salt is True:
print("Your salt: " + generate_random_auth_salt())
exit(0)
else:
from pytmbot import pytmbot_instance
from pytmbot import pytmbot_instance

pytmbot_instance.start_bot_instance()
pytmbot_instance.start_bot_instance()
21 changes: 21 additions & 0 deletions pytmbot/utils/salt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import base64
import secrets


def generate_random_auth_salt(length=32):
"""
Generates a random authentication salt for the bot.
Args:
length (int, optional): The length of the salt in bytes. Defaults to 32.
Returns:
str: The generated authentication salt.
"""
random_bytes = secrets.token_bytes(length)
salt = base64.b32encode(random_bytes).decode('utf-8')
return salt


if __name__ == "__main__":
print(generate_random_auth_salt())
25 changes: 0 additions & 25 deletions pytmbot/utils/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
also providing basic information about the status of local servers.
"""
import argparse
import base64
import secrets
from datetime import datetime
from functools import cached_property
from typing import Any, Optional, Union
Expand All @@ -31,14 +29,6 @@ def parse_cli_args() -> argparse.Namespace:
# Create an ArgumentParser object
parser = argparse.ArgumentParser(description="PyTMBot CLI")

parser.add_argument(
"--salt",
choices=["True", "False"], # Only 'dev' and 'prod' are valid choices
type=str, # The argument should be a string
help="Generate salt for pytmbot.yaml", # Help message for the argument
default="False" # Default value for the argument
)

# Add the '--mode' argument
parser.add_argument(
"--mode",
Expand Down Expand Up @@ -343,18 +333,3 @@ def is_bot_development(app_version: str) -> bool:
bool: True if the bot is in development mode, False otherwise.
"""
return len(app_version) > 6


def generate_random_auth_salt(length=32):
"""
Generates a random authentication salt for the bot.
Args:
length (int, optional): The length of the salt in bytes. Defaults to 32.
Returns:
str: The generated authentication salt.
"""
random_bytes = secrets.token_bytes(length)
salt = base64.b32encode(random_bytes).decode('utf-8')
return salt

0 comments on commit 99770e2

Please sign in to comment.