restart fix + slack commands
This commit is contained in:
1 parent
e90c9f324b
commit
25fcf85855
22 files changed
+1077
-119
No files matched your search
+116
-55
@@ -72,6 +72,8 @@ type Bot struct {
|
||||
busy map[string]int
|
||||
names sync.Map
|
||||
|
||||
updateAsked sync.Map // machine → asked to restart onto a newer system
|
||||
|
||||
channels sync.Map // channel ID → *slack.Channel
|
||||
running sync.Map // "channel/ts" of a prompt's message or reply → its cancel func
|
||||
viewing sync.Map // assistant "channel/thread" → channel the user has open
|
||||
@@ -326,8 +328,10 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
}
|
||||
st := NewStreamer(b.api, m.channel, root, ph)
|
||||
var sends tools.Calls
|
||||
cmds := b.agentCommands(u, m, root)
|
||||
stop, err := acp.Prompt(pctx, id, text, func(up acpUpdate) {
|
||||
sends.Update(up)
|
||||
cmds.Update(ctx, up)
|
||||
st.Update(up)
|
||||
})
|
||||
if pctx.Err() != nil && ctx.Err() == nil {
|
||||
@@ -335,7 +339,9 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
}
|
||||
st.Finish(stop, err)
|
||||
b.upload(ctx, u, m.channel, root, sends.Files())
|
||||
cmds.Finish(ctx)
|
||||
_ = b.store.Touch(u.ID)
|
||||
b.offerUpdate(ctx, u, m.channel, root)
|
||||
switch {
|
||||
case stop == "cancelled":
|
||||
outcome = ""
|
||||
@@ -344,28 +350,51 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) command(ctx context.Context, m incoming) bool {
|
||||
ownerID, kind := m.owner()
|
||||
verb, args, _ := strings.Cut(strings.TrimSpace(m.text), " ")
|
||||
func splitCommand(text string) (verb, args string) {
|
||||
verb, args, _ = strings.Cut(strings.TrimSpace(text), " ")
|
||||
verb = strings.ToLower(strings.TrimRight(verb, ".!?"))
|
||||
args = strings.TrimRight(strings.TrimSpace(args), " .!?")
|
||||
return verb, args
|
||||
}
|
||||
|
||||
func (b *Bot) command(ctx context.Context, m incoming) bool {
|
||||
verb, args := splitCommand(m.text)
|
||||
if verb == "move" || verb == "migrate" {
|
||||
return b.moveCommand(ctx, m, cmp.Or(m.threadTS, m.ts))
|
||||
}
|
||||
reply, ok := b.runCommand(ctx, m, verb, args, false)
|
||||
if reply != "" {
|
||||
b.say(ctx, m.channel, m.threadTS, reply)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
// runCommand returns ok=false when the text isn't a command, so it belongs to
|
||||
// the agent. An empty reply means it was posted already, with buttons. The
|
||||
// agent can't press buttons, so it gets plain text instead.
|
||||
func (b *Bot) runCommand(ctx context.Context, m incoming, verb, args string, agent bool) (reply string, ok bool) {
|
||||
ownerID, kind := m.owner()
|
||||
if verb == "grant" || verb == "revoke" || verb == "dm" || verb == "managers" || (verb == "permissions" && args != "") {
|
||||
return b.manageCommand(ctx, m, verb, args)
|
||||
}
|
||||
if need := commands.NeededPermission(verb, args); need != "" && !b.permitted(ctx, m.channel, m.threadTS, m.user, kind, need) {
|
||||
return true
|
||||
if commands.IsListen(verb) {
|
||||
c, err := commands.ParseListen(args)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return b.listenReply(m, c), true
|
||||
}
|
||||
if need := commands.NeededPermission(verb, args); need != "" {
|
||||
if denied := b.denial(ctx, m.channel, m.user, kind, need); denied != "" {
|
||||
return denied, true
|
||||
}
|
||||
}
|
||||
if verb == "stop" || verb == "scale" || verb == "storage" || verb == "disk" {
|
||||
if b.blocked(ctx, m.channel, m.threadTS, ownerID) {
|
||||
return true
|
||||
if reply, ok := b.blocked(ctx, m.channel, ownerID, agent); ok {
|
||||
return reply, true
|
||||
}
|
||||
}
|
||||
|
||||
reply := ""
|
||||
switch {
|
||||
case verb == "" || verb == "help" && args == "":
|
||||
reply = helpText
|
||||
@@ -375,34 +404,27 @@ func (b *Bot) command(ctx context.Context, m incoming) bool {
|
||||
case verb == "sessions" && args == "":
|
||||
reply = b.sessionList(ctx, ownerID)
|
||||
case verb == "status" && args == "":
|
||||
if agent {
|
||||
return b.statusText(ctx, ownerID, kind), true
|
||||
}
|
||||
b.postStatus(ctx, m.channel, m.threadTS, ownerID, kind)
|
||||
return true
|
||||
return "", true
|
||||
case verb == "stop" && args == "":
|
||||
reply = b.stopFor(ctx, ownerID)
|
||||
case verb == "scale":
|
||||
var ok bool
|
||||
if reply, ok = b.scaleFor(ctx, ownerID, kind, args); !ok {
|
||||
return false
|
||||
}
|
||||
return b.scaleFor(ctx, ownerID, kind, args)
|
||||
case verb == "keep":
|
||||
var ok bool
|
||||
if reply, ok = b.keepFor(ownerID, kind, args); !ok {
|
||||
return false
|
||||
}
|
||||
return b.keepFor(ownerID, kind, args)
|
||||
case verb == "nodes" && args == "":
|
||||
reply = b.nodesList(ctx, ownerID)
|
||||
case verb == "permissions" && args == "":
|
||||
reply = b.permissionsList(ctx, m.channel, kind)
|
||||
case verb == "storage" || verb == "disk":
|
||||
var ok bool
|
||||
if reply, ok = b.storageFor(ctx, ownerID, kind, args); !ok {
|
||||
return false
|
||||
}
|
||||
return b.storageFor(ctx, ownerID, kind, args)
|
||||
default:
|
||||
return false
|
||||
return "", false
|
||||
}
|
||||
b.say(ctx, m.channel, m.threadTS, reply)
|
||||
return true
|
||||
return reply, true
|
||||
}
|
||||
|
||||
func (b *Bot) onAction(ctx context.Context, cb slack.InteractionCallback) {
|
||||
@@ -446,6 +468,11 @@ func (b *Bot) onAction(ctx context.Context, cb slack.InteractionCallback) {
|
||||
b.waiting[u.Node][cb.Channel.ID] = true
|
||||
b.waitMu.Unlock()
|
||||
b.say(ctx, cb.Channel.ID, thread, fmt.Sprintf("OK, waiting for %s. I'll post here when it's back up.", u.Node))
|
||||
case "restart":
|
||||
b.restartFor(ctx, cb, ownerID, kind)
|
||||
case "later":
|
||||
b.settlePrompt(ctx, cb, "OK, not now. The update applies the next time the container starts, "+
|
||||
"for example after `stop` and a new message.")
|
||||
case "stop", "scale":
|
||||
if b.mgr.State(u) == manager.StateOffline {
|
||||
b.deadSlave(ctx, cb.Channel.ID, "", ownerID, u.Node)
|
||||
@@ -479,7 +506,7 @@ func (b *Bot) sessionList(ctx context.Context, ownerID string) string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func (b *Bot) postStatus(ctx context.Context, channel, thread, ownerID string, kind model.OwnerKind) {
|
||||
func (b *Bot) statusText(ctx context.Context, ownerID string, kind model.OwnerKind) string {
|
||||
u := b.store.Owner(ownerID)
|
||||
state := b.mgr.State(u)
|
||||
size := b.cfg.Container
|
||||
@@ -512,8 +539,17 @@ func (b *Bot) postStatus(ctx context.Context, channel, thread, ownerID string, k
|
||||
for _, name := range b.mgr.OfflineNodes() {
|
||||
fmt.Fprintf(&sb, "\n• %s: offline", name)
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
text := sb.String()
|
||||
func (b *Bot) postStatus(ctx context.Context, channel, thread, ownerID string, kind model.OwnerKind) {
|
||||
u := b.store.Owner(ownerID)
|
||||
state := b.mgr.State(u)
|
||||
size := b.cfg.Container
|
||||
if u != nil {
|
||||
size = u.Size
|
||||
}
|
||||
text := b.statusText(ctx, ownerID, kind)
|
||||
blocks := []slack.Block{slack.NewSectionBlock(slack.NewTextBlockObject(slack.MarkdownType, text, false, false), nil, nil)}
|
||||
|
||||
var buttons []slack.BlockElement
|
||||
@@ -712,26 +748,34 @@ func (b *Bot) moveTarget(args string) (target string, ok bool) {
|
||||
}
|
||||
|
||||
func (b *Bot) permitted(ctx context.Context, channel, thread, user string, kind model.OwnerKind, a model.Action) bool {
|
||||
p := b.store.Permissions()
|
||||
if p.Allowed(kind, channel, user, a) {
|
||||
text := b.denial(ctx, channel, user, kind, a)
|
||||
if text == "" {
|
||||
return true
|
||||
}
|
||||
var text string
|
||||
if kind == model.OwnerUser {
|
||||
text = fmt.Sprintf(":lock: Changing *%s* from DMs is turned off on this server.", a)
|
||||
} else if holders := p.Holders(channel, a); len(holders) == 0 {
|
||||
text = fmt.Sprintf(":lock: Nobody here has *%s* permission. An admin can grant it in the config.", a)
|
||||
} else {
|
||||
names := make([]string, len(holders))
|
||||
for i, h := range holders {
|
||||
names[i] = b.displayName(ctx, h)
|
||||
}
|
||||
text = fmt.Sprintf(":lock: Only super users with *%s* permission can do that here: %s.", a, strings.Join(names, ", "))
|
||||
}
|
||||
b.say(ctx, channel, thread, text)
|
||||
return false
|
||||
}
|
||||
|
||||
// denial explains why the user may not do a, or is empty when they may.
|
||||
func (b *Bot) denial(ctx context.Context, channel, user string, kind model.OwnerKind, a model.Action) string {
|
||||
p := b.store.Permissions()
|
||||
if p.Allowed(kind, channel, user, a) {
|
||||
return ""
|
||||
}
|
||||
if kind == model.OwnerUser {
|
||||
return fmt.Sprintf(":lock: Changing *%s* from DMs is turned off on this server.", a)
|
||||
}
|
||||
holders := p.Holders(channel, a)
|
||||
if len(holders) == 0 {
|
||||
return fmt.Sprintf(":lock: Nobody here has *%s* permission. An admin can grant it in the config.", a)
|
||||
}
|
||||
names := make([]string, len(holders))
|
||||
for i, h := range holders {
|
||||
names[i] = b.displayName(ctx, h)
|
||||
}
|
||||
return fmt.Sprintf(":lock: Only super users with *%s* permission can do that here: %s.", a, strings.Join(names, ", "))
|
||||
}
|
||||
|
||||
func (b *Bot) permissionsList(ctx context.Context, channel string, kind model.OwnerKind) string {
|
||||
p := b.store.Permissions()
|
||||
if kind == model.OwnerUser {
|
||||
@@ -775,37 +819,54 @@ func (b *Bot) permissionsList(ctx context.Context, channel string, kind model.Ow
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func (b *Bot) blocked(ctx context.Context, channel, thread, ownerID string) bool {
|
||||
// blocked explains why the container can't be changed right now. In Slack a
|
||||
// dead node gets its recreate/wait buttons instead, and the reply is empty.
|
||||
func (b *Bot) blocked(ctx context.Context, channel, ownerID string, agent bool) (reply string, ok bool) {
|
||||
if b.mgr.IsMigrating(ownerID) {
|
||||
b.say(ctx, channel, thread, b.explain(manager.ErrMigrating))
|
||||
return true
|
||||
return b.explain(manager.ErrMigrating), true
|
||||
}
|
||||
if u := b.store.Owner(ownerID); b.mgr.State(u) == manager.StateOffline {
|
||||
if agent {
|
||||
return fmt.Sprintf("Node %s is offline right now.", u.Node), true
|
||||
}
|
||||
b.deadSlave(ctx, channel, "", ownerID, u.Node)
|
||||
return true
|
||||
return "", true
|
||||
}
|
||||
return false
|
||||
return "", false
|
||||
}
|
||||
|
||||
func (b *Bot) moveCommand(ctx context.Context, m incoming, replyThread string) bool {
|
||||
verb, args, _ := strings.Cut(strings.TrimSpace(m.text), " ")
|
||||
verb = strings.ToLower(strings.TrimRight(verb, ".!?"))
|
||||
verb, args := splitCommand(m.text)
|
||||
if verb != "move" && verb != "migrate" {
|
||||
return false
|
||||
}
|
||||
target, ok := b.moveTarget(strings.TrimRight(strings.TrimSpace(args), " .!?"))
|
||||
reply, ok := b.move(ctx, m, args, replyThread, false)
|
||||
if reply != "" {
|
||||
b.say(ctx, m.channel, replyThread, reply)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
func (b *Bot) move(ctx context.Context, m incoming, args, replyThread string, agent bool) (reply string, ok bool) {
|
||||
target, ok := b.moveTarget(args)
|
||||
if !ok {
|
||||
return false
|
||||
return "", false
|
||||
}
|
||||
ownerID, kind := m.owner()
|
||||
if !b.permitted(ctx, m.channel, replyThread, m.user, kind, model.ActMove) {
|
||||
return true
|
||||
if denied := b.denial(ctx, m.channel, m.user, kind, model.ActMove); denied != "" {
|
||||
return denied, true
|
||||
}
|
||||
if b.blocked(ctx, m.channel, replyThread, ownerID) {
|
||||
return true
|
||||
if reply, ok := b.blocked(ctx, m.channel, ownerID, agent); ok {
|
||||
return reply, true
|
||||
}
|
||||
go b.moveFor(ctx, m.channel, ownerID, target, "<@"+m.user+">", replyThread)
|
||||
return true
|
||||
trigger := "<@" + m.user + ">"
|
||||
if !agent {
|
||||
go b.moveFor(ctx, m.channel, ownerID, target, trigger, replyThread)
|
||||
return "", true
|
||||
}
|
||||
go b.moveFor(ctx, m.channel, ownerID, target, "the agent, for "+trigger, replyThread)
|
||||
return "The move has started, and its progress is posted in Slack. The container is stopped briefly near the end, " +
|
||||
"so finish your reply now. The user can carry on in this thread once it's done.", true
|
||||
}
|
||||
|
||||
func (b *Bot) moveFor(ctx context.Context, channel, ownerID, target, trigger, replyThread string) {
|
||||
|
||||
Reference in new issue
Block a user