Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/handlers/servers/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ async def servers_info(callback_query: CallbackQuery, callback_data: BotCB, hetz
price_hourly = "➖"
price_monthly = "➖"
if prices:
price_hourly = prices[0]["price_hourly"]["gross"]
price_monthly = prices[0]["price_monthly"]["gross"]
price_hourly = f"{float(prices[0]['price_hourly']['gross']):.4f}"
price_monthly = f"{float(prices[0]['price_monthly']['gross']):.2f}"

update = await callback_query.message.edit(
text=Dialogs.SERVERS_INFO.format(
Expand Down
43 changes: 43 additions & 0 deletions src/handlers/servers/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ServerUpdateForm(StateGroup):
image = State()
ip = State()
input = State()
upgrade = State()


@router.callback_query(BotCB.filter(area=AreaType.SERVER, task=TaskType.UPDATE))
Expand Down Expand Up @@ -83,6 +84,24 @@ async def servers_update(
case StepType.SERVERS_REMARK:
text = Dialogs.SERVERS_ENTER_REMARK
_state = ServerUpdateForm.input
case StepType.SERVERS_UPGRADE:
server_types = hetzner.server_types.get_all()
current_type = server.server_type
upgrade_plans = [
st
for st in server_types
if st.architecture == current_type.architecture
and st.id != current_type.id
and (st.memory >= current_type.memory or st.cores >= current_type.cores or st.disk >= current_type.disk)
]
if not upgrade_plans:
return await callback_query.answer(text=Dialogs.SERVERS_UPGRADE_NOT_FOUND, show_alert=True)
upgrade_plans.sort(key=lambda x: (x.memory, x.cores, x.disk))
text = Dialogs.SERVERS_UPGRADE_SELECT.format(
current_plan=f"{current_type.name} [{current_type.memory}GB RAM, {current_type.cores} CPU, {current_type.disk}GB Disk]"
)
_state = ServerUpdateForm.upgrade
kb = BotKB.upgrade_plans_select(plans=upgrade_plans, server_id=server.id)
case _:
return await callback_query.answer(text="Invalid step!", show_alert=True)
await state.upsert_context(db=db, state=_state, step=callback_data.step, target=callback_data.target)
Expand Down Expand Up @@ -148,6 +167,30 @@ async def select_ip_handler(
return await callback_query.message.edit(text=Dialogs.ACTIONS_SUCCESS, reply_markup=BotKB.servers_back(server.id))


@router.callback_query(StateFilter(ServerUpdateForm.upgrade), BotCB.filter(area=AreaType.SERVER, task=TaskType.UPDATE))
async def select_upgrade_handler(
callback_query: CallbackQuery,
callback_data: BotCB,
db: AsyncSession,
state: StateManager,
hetzner: GetHetzner,
state_data: dict,
):
server_type = hetzner.server_types.get_by_id(int(callback_data.target))
if not server_type:
return await callback_query.answer(text=Dialogs.SERVERS_UPGRADE_NOT_FOUND, show_alert=True)
server = hetzner.servers.get_by_id(int(state_data["target"]))
if not server:
return await callback_query.answer(text=Dialogs.SERVERS_NOT_FOUND, show_alert=True)

if server.status != "off":
return await callback_query.answer(text=Dialogs.SERVERS_SHOULD_BE_OFF, show_alert=True)
await callback_query.message.edit(text=Dialogs.ACTIONS_WAITING)
server.change_type(server_type=server_type, upgrade_disk=True)
await state.clear_state(db=db)
return await callback_query.message.edit(text=Dialogs.SERVERS_UPGRADE_SUCCESS, reply_markup=BotKB.servers_back(server.id))


@router.callback_query(StateFilter(ServerUpdateForm.approval), BotCB.filter(area=AreaType.SERVER, task=TaskType.UPDATE))
async def approval_handler(
callback_query: CallbackQuery,
Expand Down
1 change: 1 addition & 0 deletions src/keys/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class StepType(StrEnum):
SERVERS_UNASSIGN_IPV6 = "sua6"
SERVERS_ASSIGN_IPV4 = "saa4"
SERVERS_ASSIGN_IPV6 = "saa6"
SERVERS_UPGRADE = "sup"
SNAPSHOTS_RESTORE = "srs"
SNAPSHOTS_DELETE = "sds"
SNAPSHOTS_REMARK = "srm"
Expand Down
19 changes: 18 additions & 1 deletion src/keys/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def servers_update(cls, server: Server) -> InlineKeyboardMarkup:
kb = InlineKeyboardBuilder()
update = {
StepType.SERVERS_REMARK: Buttons.SERVERS_REMARK,
StepType.SERVERS_UPGRADE: Buttons.SERVERS_UPGRADE,
StepType.SERVERS_POWER_OFF: Buttons.SERVERS_POWER_OFF,
StepType.SERVERS_POWER_ON: Buttons.SERVERS_POWER_ON,
StepType.SERVERS_CREATE_SNAPSHOT: Buttons.SERVERS_CREATE_SNAPSHOT,
Expand All @@ -189,7 +190,7 @@ def servers_update(cls, server: Server) -> InlineKeyboardMarkup:
step=step,
).pack(),
)
kb.adjust(1, 2, 1, 2, 1, 2, 1, 2, 2)
kb.adjust(1, 1, 2, 1, 2, 1, 2, 1, 2, 2)
cls._back(kb=kb, area=AreaType.SERVER)
return kb.as_markup()

Expand Down Expand Up @@ -247,6 +248,22 @@ def plans_select(cls, plans: List[ServerType]) -> InlineKeyboardMarkup:
cls._back(kb=kb, area=AreaType.SERVER)
return kb.as_markup()

@classmethod
def upgrade_plans_select(cls, plans: List[ServerType], server_id: int) -> InlineKeyboardMarkup:
kb = InlineKeyboardBuilder()
for plan in plans:
kb.add(
text=f"{plan.name} [{plan.memory} RAM, {plan.cores} CPU, {float(plan.prices[0]['price_monthly']['net'])} EUR]",
callback_data=BotCB(
area=AreaType.SERVER,
task=TaskType.UPDATE,
target=plan.id,
).pack(),
)
kb.adjust(1)
cls._back(kb=kb, area=AreaType.SERVER, target=server_id)
return kb.as_markup()

@classmethod
def snapshots_menu(cls, snapshots: List[Image]) -> InlineKeyboardMarkup:
kb = InlineKeyboardBuilder()
Expand Down
1 change: 1 addition & 0 deletions src/lang/_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Buttons(StrEnum):
SERVERS_ASSIGN_IPV6 = "🔗 Assign IPv6"
SERVERS_UNASSIGN_IPV4 = "❌ Unassign IPv4"
SERVERS_UNASSIGN_IPV6 = "❌ Unassign IPv6"
SERVERS_UPGRADE = "⬆️ Upgrade"

### Snapshots
SNAPSHOTS = "📸 Snapshots"
Expand Down
4 changes: 4 additions & 0 deletions src/lang/_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class Dialogs(StrEnum):
SERVERS_REMARK_VALIDATION = (
"<b>⚠️❌ Invalid remark format.</b>\n🔍 Please enter a valid remark without special characters and space."
)
SERVERS_UPGRADE_SELECT = "⬆️ Select a plan to upgrade the server:\n\n<b>Current:</b> <code>{current_plan}</code>"
SERVERS_UPGRADE_NOT_FOUND = "🔍❌ No upgrade plans available for this server."
SERVERS_UPGRADE_SUCCESS = "<b>🎉✅ Server upgraded successfully.</b>"
SERVERS_SHOULD_BE_OFF = "⚠️❌ Please power off the server and try again."

### Snapshots
SNAPSHOTS_MENU = "<b>📸 Snapshots Menu</b>\n👇 Select an action from the menu below."
Expand Down