83 lines
3.8 KiB
Python
83 lines
3.8 KiB
Python
from datetime import timedelta
|
|
from html import escape
|
|
|
|
from ..agent.tools import ToolArgumentError, validate_host, validate_service
|
|
from ..chat import BACKEND_TELEGRAM, Button, callback_data
|
|
from .arguments import ArgumentError, parse_quiet_args
|
|
from .constants import PREFIXES
|
|
from .context import CommandContext, DowntimePort
|
|
from .duration import format_duration
|
|
from .models import Reply
|
|
from .render import plain
|
|
from .targets import TargetError, resolve_reply_target
|
|
|
|
USAGE = "Verwendung: {p}downtime <host> <service> <dauer> [kommentar] oder als Antwort auf einen Alarm: {p}downtime 2h"
|
|
UNAVAILABLE = "Downtime ist nicht verfügbar."
|
|
TOKEN_USAGE = "Verwendung: {p}{name} <code>"
|
|
TOKEN_MAX = 32
|
|
BUTTON_CONFIRM = "Bestätigen"
|
|
BUTTON_CANCEL = "Abbrechen"
|
|
|
|
|
|
def confirmation_reply(backend: str, host: str, service: str, duration: timedelta, comment: str, token: str,
|
|
ttl_seconds: int) -> Reply:
|
|
prefix = PREFIXES.get(backend, "/")
|
|
rows = (("Host", host), ("Service", service), ("Dauer", format_duration(duration)), ("Kommentar", comment))
|
|
text = "Downtime in Checkmk setzen?\n" + "\n".join(f"{label}: {value}" for label, value in rows)
|
|
html = "<b>Downtime in Checkmk setzen?</b>\n" + "\n".join(
|
|
f"{label}: {escape(value)}" for label, value in rows)
|
|
footer = f"Gilt {ttl_seconds} s, nur für dich."
|
|
if backend == BACKEND_TELEGRAM:
|
|
buttons = ((Button(BUTTON_CONFIRM, callback_data.encode(callback_data.ACTION_CONFIRM, token)),
|
|
Button(BUTTON_CANCEL, callback_data.encode(callback_data.ACTION_CANCEL, token))),)
|
|
return Reply(f"{text}\n{footer}", f"{html}\n{footer}", buttons)
|
|
footer += f" Bestätigen mit {prefix}confirm {token}, abbrechen mit {prefix}cancel {token}."
|
|
return Reply(f"{text}\n{footer}", f"{html}\n{escape(footer)}")
|
|
|
|
|
|
async def start_downtime(downtime: DowntimePort, backend: str, sender_id: str, host: str, service: str,
|
|
duration: timedelta, comment: str) -> Reply:
|
|
result = await downtime.request(backend, sender_id, host, service, duration, comment)
|
|
if not result.ok:
|
|
return plain(result.message)
|
|
request = result.request
|
|
return confirmation_reply(backend, request.host, request.service, request.duration, request.comment,
|
|
result.token, result.ttl_seconds)
|
|
|
|
|
|
async def downtime_command(ctx: CommandContext) -> Reply:
|
|
downtime = ctx.deps.downtime
|
|
if downtime is None:
|
|
return plain(UNAVAILABLE)
|
|
usage = USAGE.format(p=PREFIXES.get(ctx.message.backend, "/"))
|
|
try:
|
|
parsed = parse_quiet_args(ctx.command.args, downtime.max_duration(), service_required=True)
|
|
host, service = parsed.host, parsed.service
|
|
if host is None:
|
|
host, service = await resolve_reply_target(ctx, escape_globs=False, whole_host_hint=False)
|
|
host, service = validate_host(host), validate_service(service)
|
|
except ArgumentError as exc:
|
|
return plain(f"{exc} {usage}")
|
|
except (TargetError, ToolArgumentError) as exc:
|
|
return plain(str(exc))
|
|
return await start_downtime(downtime, ctx.message.backend, ctx.message.sender_id, host, service,
|
|
parsed.duration, parsed.comment)
|
|
|
|
|
|
async def _resolve(ctx: CommandContext, approve: bool) -> Reply:
|
|
downtime = ctx.deps.downtime
|
|
if downtime is None:
|
|
return plain(UNAVAILABLE)
|
|
if len(ctx.command.args) != 1 or len(ctx.command.args[0]) > TOKEN_MAX:
|
|
return plain(TOKEN_USAGE.format(p=PREFIXES.get(ctx.message.backend, "/"), name=ctx.command.name))
|
|
result = await downtime.resolve(ctx.command.args[0], ctx.message.backend, ctx.message.sender_id, approve)
|
|
return plain(result.message)
|
|
|
|
|
|
async def confirm_command(ctx: CommandContext) -> Reply:
|
|
return await _resolve(ctx, True)
|
|
|
|
|
|
async def cancel_command(ctx: CommandContext) -> Reply:
|
|
return await _resolve(ctx, False)
|