Первый стрим
This commit is contained in:
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
0
app/handlers/__init__.py
Normal file
0
app/handlers/__init__.py
Normal file
48
app/handlers/docker_commands.py
Normal file
48
app/handlers/docker_commands.py
Normal file
@ -0,0 +1,48 @@
|
||||
import subprocess
|
||||
|
||||
from aiogram.types import Message, CallbackQuery
|
||||
|
||||
from app.keyboards.docker_keyboards import container_names_keyboard, container_actions_keyboard
|
||||
|
||||
|
||||
async def get_containers(message: Message):
|
||||
sub = subprocess.check_output("docker ps -a").decode()
|
||||
keyboard = container_names_keyboard(sub)
|
||||
await message.answer(
|
||||
text=f"<pre><code>{sub}</code></pre>",
|
||||
parse_mode="HTML",
|
||||
reply_markup=keyboard.as_markup()
|
||||
)
|
||||
|
||||
|
||||
async def container_actions(call: CallbackQuery):
|
||||
name = call.data.split("_")[-1]
|
||||
keyboard = container_actions_keyboard(name)
|
||||
await call.message.answer(
|
||||
text=f"Выберите действие для контейнера {name}",
|
||||
parse_mode="HTML",
|
||||
reply_markup=keyboard.as_markup()
|
||||
)
|
||||
|
||||
|
||||
async def do_container_action(call: CallbackQuery):
|
||||
_, action, name = call.data.split("_")
|
||||
match action:
|
||||
case "start":
|
||||
subprocess.run(f"docker start {name}")
|
||||
message = f"Контейнер {name} успешно запущен"
|
||||
case "stop":
|
||||
subprocess.run(f"docker stop {name}")
|
||||
message = f"Контейнер {name} успешно остановлен"
|
||||
case "restart":
|
||||
subprocess.run(f"docker restart {name}")
|
||||
message = f"Контейнер {name} успешно перезапущен"
|
||||
case "delete":
|
||||
subprocess.run(f"docker rm -f {name}")
|
||||
message = f"Контейнер {name} успешно удалён"
|
||||
case _:
|
||||
message = f"Произошла необъяснимая ошибка"
|
||||
|
||||
await call.message.answer(
|
||||
text=message,
|
||||
)
|
11
app/handlers/user_command.py
Normal file
11
app/handlers/user_command.py
Normal file
@ -0,0 +1,11 @@
|
||||
import subprocess
|
||||
|
||||
from aiogram.types import Message
|
||||
|
||||
|
||||
async def execute_command(message: Message):
|
||||
user_command = message.text.split()[1:]
|
||||
print(user_command)
|
||||
sub = subprocess.check_output(user_command)
|
||||
# print(sub)
|
||||
await message.answer(f"<pre><code>{sub.decode()}</code></pre>", parse_mode="HTML")
|
0
app/keyboards/__init__.py
Normal file
0
app/keyboards/__init__.py
Normal file
42
app/keyboards/docker_keyboards.py
Normal file
42
app/keyboards/docker_keyboards.py
Normal file
@ -0,0 +1,42 @@
|
||||
from aiogram.types import InlineKeyboardButton
|
||||
from aiogram.utils.keyboard import InlineKeyboardBuilder
|
||||
|
||||
|
||||
def container_names_keyboard(stdout: str):
|
||||
container_names = [line.split(" ")[-1].strip() for line in stdout.splitlines()[1:]]
|
||||
builder = InlineKeyboardBuilder()
|
||||
for name in container_names:
|
||||
data = f"container_{name}"
|
||||
builder.add(
|
||||
InlineKeyboardButton(
|
||||
text=name,
|
||||
callback_data=data,
|
||||
)
|
||||
)
|
||||
builder.adjust(1)
|
||||
|
||||
return builder
|
||||
|
||||
|
||||
def container_actions_keyboard(name: str):
|
||||
builder = InlineKeyboardBuilder()
|
||||
|
||||
builder.add(InlineKeyboardButton(
|
||||
text="Запустить контейнер",
|
||||
callback_data=f"action_start_{name}",
|
||||
))
|
||||
builder.add(InlineKeyboardButton(
|
||||
text="Остановить контейнер",
|
||||
callback_data=f"action_stop_{name}",
|
||||
))
|
||||
builder.add(InlineKeyboardButton(
|
||||
text="Перезапустить контейнер",
|
||||
callback_data=f"action_restart_{name}",
|
||||
))
|
||||
builder.add(InlineKeyboardButton(
|
||||
text="Удалить контейнер",
|
||||
callback_data=f"action_delete_{name}",
|
||||
))
|
||||
builder.adjust(1)
|
||||
|
||||
return builder
|
16
app/settings.py
Normal file
16
app/settings.py
Normal file
@ -0,0 +1,16 @@
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
|
||||
from aiogram import Bot
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
@dataclass
|
||||
class Secrets:
|
||||
token: str = os.environ.get("TOKEN")
|
||||
admin_id: int = os.environ.get("ADMIN_ID")
|
||||
|
||||
|
||||
bot = Bot(token=Secrets.token)
|
Reference in New Issue
Block a user