Files
2026-11-01 15:10:39 -05:00

81 lines
3.2 KiB
Go

package bot
import (
"context"
"log"
"github.com/slack-go/slack"
"slackerbot/internal/manager"
"slackerbot/internal/model"
)
const updateText = ":arrows_counterclockwise: A system update is waiting for this container. " +
"Restarting applies it. Files in /root and your sessions are kept."
// offerUpdate asks once per container whether to restart onto the node's
// current system. Asking again needs a newer system or a bot restart.
func (b *Bot) offerUpdate(ctx context.Context, u *model.Owner, channel, thread string) {
if !b.mgr.Outdated(ctx, u) {
b.updateAsked.Delete(u.Machine)
return
}
if _, asked := b.updateAsked.LoadOrStore(u.Machine, true); asked {
return
}
restart := slack.NewButtonBlockElement("restart", u.ID, slack.NewTextBlockObject(slack.PlainTextType, "Restart now", false, false))
restart.Style = slack.StylePrimary
later := slack.NewButtonBlockElement("later", u.ID, slack.NewTextBlockObject(slack.PlainTextType, "Later", false, false))
blocks := []slack.Block{
slack.NewSectionBlock(slack.NewTextBlockObject(slack.MarkdownType, updateText, false, false), nil, nil),
slack.NewActionBlock("update", restart, later),
}
if _, _, err := b.api.PostMessageContext(ctx, channel, slack.MsgOptionText(updateText, false),
slack.MsgOptionBlocks(blocks...), slack.MsgOptionTS(thread)); err != nil {
log.Printf("slack post: %v", err)
}
}
// settlePrompt replaces the update prompt, so its buttons can't be pressed twice.
func (b *Bot) settlePrompt(ctx context.Context, cb slack.InteractionCallback, text string) {
block := slack.NewSectionBlock(slack.NewTextBlockObject(slack.MarkdownType, text, false, false), nil, nil)
if _, _, _, err := b.api.UpdateMessageContext(ctx, cb.Channel.ID, cb.Message.Timestamp,
slack.MsgOptionText(text, false), slack.MsgOptionBlocks(block)); err != nil {
log.Printf("slack update: %v", err)
}
}
func (b *Bot) restartFor(ctx context.Context, cb slack.InteractionCallback, ownerID string, kind model.OwnerKind) {
channel, thread := cb.Channel.ID, cb.Message.ThreadTimestamp
u := b.store.Owner(ownerID)
switch b.mgr.State(u) {
case manager.StateOffline:
b.deadSlave(ctx, channel, "", ownerID, u.Node)
return
case manager.StateRunning:
default:
b.settlePrompt(ctx, cb, ":white_check_mark: The container isn't running, so it gets the update when it next starts.")
return
}
if b.isBusy(u.Machine) {
b.say(ctx, channel, thread, "The agent is still replying in this container. Press *Restart now* again once it's done.")
return
}
b.settlePrompt(ctx, cb, ":arrows_counterclockwise: Restarting the container to apply the update…")
b.currentLifecycle(ctx, u).post(ctx, ":octagonal_sign: Shutting down to apply a system update, asked by <@"+cb.User.ID+">.")
b.dropClient(u)
sl := &startLog{b: b, m: incoming{user: cb.User.ID, channel: channel}, root: thread, ownerID: ownerID}
u, err := b.mgr.Restart(ctx, ownerID, kind, func(ev manager.StartEvent) { sl.event(ctx, ev) })
if err == nil {
_, err = b.client(ctx, u)
}
if err != nil {
sl.failed(ctx, b.explain(err))
b.say(ctx, channel, thread, ":warning: The restart didn't finish: "+b.explain(err))
return
}
sl.up(ctx)
b.say(ctx, channel, thread, ":white_check_mark: Restarted with the update. Carry on in any thread.")
}