From a43fb36f44655e7c142c7973d548e8505fb1cda4 Mon Sep 17 00:00:00 2001 From: Igor <2006giqhpmine.ru@gmail.com> Date: Sat, 29 Jan 2022 22:03:35 +0900 Subject: Add docker )) --- .gitignore | 5 +- Dockerfile | 14 +++ FunnyPineappleBot.sample.service | 18 --- config.py | 1 + dependencies | 2 - docker-compose.yaml | 10 ++ main.py | 20 ---- requirements.txt | 2 + samples.txt | 0 setup.py | 243 --------------------------------------- shared/config.sample.py | 3 - shared/instances.py | 3 +- 12 files changed, 31 insertions(+), 290 deletions(-) create mode 100644 Dockerfile delete mode 100644 FunnyPineappleBot.sample.service create mode 100644 config.py delete mode 100644 dependencies create mode 100644 docker-compose.yaml create mode 100644 requirements.txt create mode 100644 samples.txt delete mode 100644 setup.py delete mode 100644 shared/config.sample.py diff --git a/.gitignore b/.gitignore index 29385a3..a1bdc9b 100644 --- a/.gitignore +++ b/.gitignore @@ -133,6 +133,5 @@ dmypy.json .vscode FunnyPineappleBot.service -config.py -samples.txt -tmp/* \ No newline at end of file +tmp/* +data/* diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f66d997 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM python:bullseye + +RUN apt-get update +RUN apt-get upgrade +RUN pip install pip -U + +WORKDIR /app +COPY requirements.txt . +RUN pip install -r requirements.txt + +COPY . . +COPY data/config.py . + +CMD ["python", "main.py"] diff --git a/FunnyPineappleBot.sample.service b/FunnyPineappleBot.sample.service deleted file mode 100644 index 210b267..0000000 --- a/FunnyPineappleBot.sample.service +++ /dev/null @@ -1,18 +0,0 @@ -[Unit] -Description=Telegram bot from FunnyPineappleChat -After=syslog.target -After=network.target - -[Service] -Type=simple -User={username} -WorkingDirectory={path} -ExecStart={py_path} main.py -m -KillMode=control-group -KillSignal=15 -SendSIGKILL={send_kill} -RestartSec=5 -Restart=always - -[Install] -WantedBy=multi-user.target \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..5608add --- /dev/null +++ b/config.py @@ -0,0 +1 @@ +token="{}" diff --git a/dependencies b/dependencies deleted file mode 100644 index 888ef8a..0000000 --- a/dependencies +++ /dev/null @@ -1,2 +0,0 @@ -aiogram -mc.py diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..7383133 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,10 @@ +version: "3" + +services: + pineapple-bot: + build: "." + restart: "always" + volumes: + - "./requirements.txt:/app/requirements.txt" + - "./data/samples.txt:/app/samples.txt" + diff --git a/main.py b/main.py index 4ed98a7..64aad14 100644 --- a/main.py +++ b/main.py @@ -2,29 +2,9 @@ import optparse from aiogram import executor, types as t, Dispatcher import logging -from shared import config logging.basicConfig(level=logging.INFO) -parser = optparse.OptionParser(conflict_handler="resolve") # Делает прикол с аргументами -parser.add_option('-t', '--test', - action="store_true", - dest='test', - help='test token') -parser.add_option('-m', '--main', - action="store_true", - dest='main', - help='main token') -values, args = parser.parse_args() # Либа хуйня - -if values.test: - config.token = config.test_token -elif values.main: - config.token = config.main_token -else: - config.token = config.test_token - - async def on_start(dp: Dispatcher): from shared.commands import commands for scope, cmd in commands.items(): diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..888ef8a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +aiogram +mc.py diff --git a/samples.txt b/samples.txt new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py deleted file mode 100644 index b6fdfc1..0000000 --- a/setup.py +++ /dev/null @@ -1,243 +0,0 @@ -import os -import platform -import shutil -import sys -import traceback -import typing as p - -term = shutil.get_terminal_size() -sep = "=" * term.columns - -venv = False -default = 1 - -if os.name == "nt": - print(f"Setup not supported on {platform.system()}") - exit() - - -def _opt_selector(options: p.Dict[str, p.Any], default: p.Optional[int] = None, pmt: str = "") -> p.Tuple[int, p.Any]: - _options = {} - print("Choose variant") - print(sep) - - num = 1 - for opt in options: - if options[opt] is None: - print() - continue - - d = "*" if default == num else " " - - print(f" {d}{num} - {opt}") - _options[num] = options[opt] - num += 1 - print(sep) - - while True: - opt = input(f"{pmt}-> ") - if opt.isdigit(): - opt = int(opt) - if opt in _options: - return opt, _options[opt] - if opt == "" and default is not None: - return default, _options[default] - - print("Incorrect answer") - pmt = "" - - -def _yes(default: bool = True) -> bool: - while True: - if default: - it = "[Y|n]" - else: - it = "[y|N]" - - i = input(f"{it} -> ") - if i.lower() in ["y", "n"]: - return i.lower() == "y" - if i == "": - return default - - print("Incorrect answer") - - -def _input(prompt: str, default: str = None, required: bool = True) -> str: - if default: - print(f"Default {default}") - while True: - result = input(f"{prompt} -> ") - if not result: - if default: - result = default - break - elif required: - print("Parameter required") - else: - break - - return result - - -def _cmd(cmd: str, show_cmd: bool = True) -> bool: - if show_cmd: - input(f"Press enter for execute {cmd}") - result = os.system(cmd) - return True if result == 0 else False - - -def _clear(): - print() - print('\033c', end="") - - -def _enter(): - input("Press enter, to continue ...") - - -def create_samples_txt(): - if not os.path.isfile("samples.txt"): - with open("samples.txt", "w"): - print("File created") - else: - print("File exists") - _enter() - return True - - -def systemd_unit_generator(): - while True: - username = _input("Your username", os.environ["USER"]) - path = _input("Path to root of project", os.path.dirname(os.path.abspath(__file__))) - py_path = _input("Path to python 3.9", sys.executable) - - print("Send kill (if SIGTERM Timeout)") - send_kill = "off" - if _yes(True): - send_kill = "on" - - with open("FunnyPineappleBot.sample.service", "r") as file: - sample = file.read().format(username=username, path=path, py_path=py_path, send_kill=send_kill) - - _clear() - print( - f"{sep}", - f"{sample}", - f"{sep}", - f"Your username - {username}", - f"Path to root of project - {path}", - f"Path to python 3.9 - {py_path}", - f"Send kill - {send_kill}", - f"{sep}", - "", - "All correct ?", - sep="\n" - ) - if _yes(False): - with open("FunnyPineappleBot.service", "w") as file: - file.write(sample) - _clear() - - print("Link unit from /etc/systemd/system/ ?") - if _yes(): - _cmd("sudo mv FunnyPineappleBot.service /etc/systemd/system/") - _cmd("sudo ln /etc/systemd/system/FunnyPineappleBot.service ./ -s") - break - else: - _clear() - print(sep) - _enter() - return True - - -def config_generator(): - try: - import config - except ImportError: - config = object() - while True: - main_token = _input("Main bot token", getattr(config, "main_token", None)) - test_token = _input("Test bot token", getattr(config, "test_token", None)) - - with open("shared/config.sample.py", "r") as file: - sample = file.read().format(main_token=main_token, test_token=test_token) - - print( - f"{sep}", - f"{sample}", - f"{sep}", - "All correct ?", - sep="\n" - ) - - if _yes(False): - with open("shared/config.py", "w") as file: - file.write(sample) - break - else: - _clear() - return True - - -def install_dependencies(): - with open("dependencies", "r") as file: - dependencies = file.read() - print( - "Install this ?", - f"{sep}", - f"{dependencies}", - f"{sep}", - sep="\n" - ) - _cmd("pip install -U -r dependencies", False) - print(f"{sep}\nSuccessfully installed") - _enter() - return True - - -if __name__ == '__main__': - opts = { - "Create samples.txt file": create_samples_txt, - "Setup systemd unit": systemd_unit_generator, - "Setup config.py": config_generator, - "Install or Update dependencies": install_dependencies, - "Exit": exit, - } - exit_index = list(opts.keys()).index("Exit") + 1 - default = 1 - pmt = "" - - _clear() - while True: - if default >= exit_index: - default = exit_index - term = shutil.get_terminal_size() - sep = "=" * term.columns - try: - _clear() - num, opt = _opt_selector(opts, default, pmt) - _clear() - res = opt() - _clear() - - if num != default: - default = num + 1 - elif res is True: - default += 1 - - if res is not True: - pmt = f"{res!r} " - if num == default: - default = num + 1 - else: - pmt = "" - - except KeyboardInterrupt: - break - except Exception as e: - pmt = f"An error has occurred ({e.__class__.__name__}:{e.args[0]})" - trc = traceback.format_exc() - print(trc) - _enter() - _clear() diff --git a/shared/config.sample.py b/shared/config.sample.py deleted file mode 100644 index f4a3f09..0000000 --- a/shared/config.sample.py +++ /dev/null @@ -1,3 +0,0 @@ -TOKEN = "" -test_token = "{test_token}" -main_token = "{main_token}" diff --git a/shared/instances.py b/shared/instances.py index 25de59f..5351f9c 100644 --- a/shared/instances.py +++ b/shared/instances.py @@ -1,6 +1,7 @@ from aiogram import Bot, Dispatcher -from .config import token +from config import token +print(token) bot = Bot(token=token) dp = Dispatcher(bot) gen_chance = 10 -- cgit v1.2.3