Files
felhom-controller/controller/internal/integrations/manager.go
T
admin 7c05b59708
gates / gates (push) Successful in 24s
v0.253.0 — errors carry the key of the sentence they are (R-557 slice 2 release B)
179 Hungarian sentences were built deep inside a package with fmt.Errorf and printed by
whoever caught them: too late to translate where they are shown, too early where they are
made. Every one now carries its key across that gap. ZERO Hungarian error literals remain.

util.MsgError does three things at once, each earned:
  - Error() is the Hungarian, byte for byte, so every un-converted printer is unchanged;
  - errors.Is answers for the kind AND for a wrapped cause (KindErrorf dropped the cause);
  - an error ARGUMENT renders recursively, so "formázás sikertelen: %w" translates whole.
A foreign error — restic, docker, ssh, the stdlib — prints verbatim. It is not ours.

76 display sites go through errText, and TestNoErrErrorInPageOutput convicts any that do
not. memoryVerdict returns an error rather than a sentence, so the deploy's 409 and the
household's language come from one value; UpdateRefusal gained a Cause to carry it.

Plurals, one rule, stated once: a key with .one/.other takes its COUNT first. Not a
per-call-site flag — the producer somebody forgot would read "3 app is not running". The
guard caught a real key collision (alert.deadapp.one) the day the rule landed.

TWO DEFECTS FOUND IN MY OWN TOOLING, recorded rather than quietly fixed. The bulk converter
silently dropped multi-line concatenations, damaging 7 producers — and the parity gate could
not see it, because every surviving fragment WAS a real base literal while the CALL had lost
text; two behaviour tests caught it. And the counting script was case-sensitive, so it said
"0 left" while five remained.

MinAgent: 0.131.0 (unchanged). No hub release needed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
2026-09-18 11:44:30 +02:00

325 lines
10 KiB
Go

package integrations
import (
"context"
"gitea.dooplex.hu/admin/felhom-controller/internal/util"
"log"
"path/filepath"
"sync"
"time"
"gitea.dooplex.hu/admin/felhom-controller/internal/crypto"
"gitea.dooplex.hu/admin/felhom-controller/internal/settings"
"gitea.dooplex.hu/admin/felhom-controller/internal/stacks"
)
// StackProvider abstracts stacks.Manager to break circular imports.
type StackProvider interface {
GetStack(name string) (*stacks.Stack, bool)
GetStacks() []stacks.Stack
RestartStack(name string) error
}
// Manager coordinates integration apply/revoke and lifecycle hooks.
type Manager struct {
sett *settings.Settings
stacks StackProvider
domain string
stacksDir string
encKey []byte
logger *log.Logger
handlers map[string]Handler // key: "provider:target" -> Handler
mu sync.Mutex // serialize apply/revoke operations
debug bool
}
// NewManager creates an integration manager and registers built-in handlers.
func NewManager(sett *settings.Settings, sp StackProvider, domain, stacksDir string, encKey []byte, logger *log.Logger) *Manager {
m := &Manager{
sett: sett,
stacks: sp,
domain: domain,
stacksDir: stacksDir,
encKey: encKey,
logger: logger,
handlers: make(map[string]Handler),
}
// Register built-in handlers
m.RegisterHandler("onlyoffice:filebrowser", &OnlyOfficeFileBrowserHandler{})
m.RegisterHandler("onlyoffice:nextcloud", &OnlyOfficeNextcloudHandler{})
if m.isDebug() {
keys := make([]string, 0, len(m.handlers))
for k := range m.handlers {
keys = append(keys, k)
}
m.logger.Printf("[DEBUG] [integrations] NewManager: registered handlers: %v", keys)
}
return m
}
// SetDebug enables or disables debug logging.
func (m *Manager) SetDebug(debug bool) {
m.debug = debug
}
// isDebug returns whether debug logging is enabled.
func (m *Manager) isDebug() bool {
return m.debug
}
// RegisterHandler registers a handler for a provider:target integration key.
func (m *Manager) RegisterHandler(key string, h Handler) {
m.handlers[key] = h
}
// Toggle enables or disables an integration.
func (m *Manager) Toggle(ctx context.Context, provider, target string, enable bool) (settings.IntegrationState, error) {
m.mu.Lock()
defer m.mu.Unlock()
key := IntegrationKey(provider, target)
if m.isDebug() {
m.logger.Printf("[DEBUG] [integrations] Toggle: key=%s provider=%s target=%s enable=%v", key, provider, target, enable)
}
handler, ok := m.handlers[key]
if !ok {
return settings.IntegrationState{}, util.MsgError("err.integrations.nincs_kezelo_a_z_integraciohoz", key)
}
ac, err := m.buildApplyContext(provider, target)
if err != nil {
return settings.IntegrationState{}, err
}
var state settings.IntegrationState
if enable {
// Validate: provider must be deployed and running
provStack, pOk := m.stacks.GetStack(provider)
if m.isDebug() {
m.logger.Printf("[DEBUG] [integrations] Toggle: provider %s found=%v deployed=%v state=%v", provider, pOk, pOk && provStack.Deployed, func() stacks.ContainerState {
if pOk {
return provStack.State
}
return ""
}())
}
if !pOk || !provStack.Deployed {
return state, util.MsgError("err.integrations.a_szolgaltato_alkalmazas_nincs_telepitve", provider)
}
if provStack.State != stacks.StateRunning {
return state, util.MsgError("err.integrations.a_szolgaltato_alkalmazas_nem_fut", provider)
}
// Validate: target must be deployed and running (filebrowser is infra, always present)
if target != "filebrowser" {
tgtStack, tOk := m.stacks.GetStack(target)
if m.isDebug() {
m.logger.Printf("[DEBUG] [integrations] Toggle: target %s found=%v deployed=%v state=%v", target, tOk, tOk && tgtStack.Deployed, func() stacks.ContainerState {
if tOk {
return tgtStack.State
}
return ""
}())
}
if !tOk || !tgtStack.Deployed {
return state, util.MsgError("err.integrations.a_celalkalmazas_nincs_telepitve", target)
}
if tgtStack.State != stacks.StateRunning {
return state, util.MsgError("err.integrations.a_celalkalmazas_nem_fut", target)
}
}
start := time.Now()
if err := handler.Apply(ac); err != nil {
m.logger.Printf("[ERROR] [integrations] Integration %s apply failed: %v", key, err)
if m.isDebug() {
m.logger.Printf("[DEBUG] [integrations] Toggle: Apply failed for %s in %v: %v", key, time.Since(start), err)
}
state.Enabled = true
state.Status = "error"
state.LastError = err.Error()
state.EnabledAt = time.Now().UTC().Format(time.RFC3339)
_ = m.sett.SetIntegrationState(key, state)
return state, util.MsgError("err.integrations.integracio_alkalmazasa_sikertelen", err)
}
if m.isDebug() {
m.logger.Printf("[DEBUG] [integrations] Toggle: Apply succeeded for %s in %v", key, time.Since(start))
}
state.Enabled = true
state.Status = "active"
state.EnabledAt = time.Now().UTC().Format(time.RFC3339)
m.logger.Printf("[INFO] [integrations] Integration %s enabled", key)
} else {
start := time.Now()
if err := handler.Revoke(ac); err != nil {
m.logger.Printf("[WARN] [integrations] Integration revoke failed for %s: %v", key, err)
state.LastError = err.Error()
}
if m.isDebug() {
m.logger.Printf("[DEBUG] [integrations] Toggle: Revoke for %s completed in %v", key, time.Since(start))
}
state.Enabled = false
state.Status = "disabled"
m.logger.Printf("[INFO] [integrations] Integration %s disabled", key)
}
if err := m.sett.SetIntegrationState(key, state); err != nil {
return state, util.MsgError("err.integrations.integracio_allapotanak_mentese_sikertelen", err)
}
return state, nil
}
// ListForProvider returns StatusInfo for all integrations defined in a provider's metadata.
func (m *Manager) ListForProvider(providerSlug string) []StatusInfo {
provStack, ok := m.stacks.GetStack(providerSlug)
if !ok {
return nil
}
if m.isDebug() {
m.logger.Printf("[DEBUG] [integrations] ListForProvider: provider=%s integrationDefs=%d", providerSlug, len(provStack.Meta.Integrations))
}
var result []StatusInfo
for _, idef := range provStack.Meta.Integrations {
key := IntegrationKey(providerSlug, idef.Target)
state, _ := m.sett.GetIntegrationState(key)
targetDeployed := false
targetRunning := false
if tgt, tOk := m.stacks.GetStack(idef.Target); tOk {
// FileBrowser is infrastructure — if its stack exists, it's "deployed"
if idef.Target == "filebrowser" {
targetDeployed = true
} else {
targetDeployed = tgt.Deployed
}
targetRunning = tgt.State == stacks.StateRunning
}
result = append(result, StatusInfo{
Key: key,
Provider: providerSlug,
Target: idef.Target,
Label: idef.Label,
Description: idef.Description,
Enabled: state.Enabled,
Status: state.Status,
LastError: state.LastError,
TargetDeployed: targetDeployed,
TargetRunning: targetRunning,
})
}
return result
}
// buildApplyContext gathers all data needed by a handler.
func (m *Manager) buildApplyContext(provider, target string) (*ApplyContext, error) {
provStack, ok := m.stacks.GetStack(provider)
if !ok {
m.logger.Printf("[WARN] [integrations] Failed to build context for %s:%s: provider stack not found", provider, target)
return nil, util.MsgError("err.integrations.szolgaltato_stack_nem_talalhato", provider)
}
// Load decrypted env from provider's app.yaml
provEnv := m.loadDecryptedEnv(provStack)
if m.isDebug() {
envKeyCount := len(provEnv)
m.logger.Printf("[DEBUG] [integrations] buildApplyContext: provider=%s target=%s domain=%s envKeys=%d", provider, target, m.domain, envKeyCount)
}
provMeta := provStack.Meta
return &ApplyContext{
ProviderName: provider,
TargetName: target,
Domain: m.domain,
ProviderEnv: provEnv,
ProviderMeta: &provMeta,
StacksDir: m.stacksDir,
Logger: m.logger,
RestartStack: m.stacks.RestartStack,
}, nil
}
// ReapplyConfigForTarget applies all active integrations targeting the given stack,
// writing config changes only (no container restart). Use this when the caller
// handles container restart separately (e.g. SyncFileBrowserMounts).
func (m *Manager) ReapplyConfigForTarget(targetName string) {
m.mu.Lock()
defer m.mu.Unlock()
all := m.sett.GetIntegrationsForTarget(targetName)
if m.isDebug() {
m.logger.Printf("[DEBUG] [integrations] ReapplyConfigForTarget: target=%s integrations=%d", targetName, len(all))
}
for key, state := range all {
if !state.Enabled || state.Status == "disabled" {
continue
}
provider, target, ok := ParseIntegrationKey(key)
if !ok {
continue
}
handler, hOk := m.handlers[key]
if !hOk {
continue
}
if m.isDebug() {
m.logger.Printf("[DEBUG] [integrations] ReapplyConfigForTarget: reapplying %s (status=%s)", key, state.Status)
}
ac, err := m.buildApplyContext(provider, target)
if err != nil {
m.logger.Printf("[WARN] [integrations] Cannot build context for integration %s reapply: %v", key, err)
continue
}
// Override RestartStack to no-op — caller handles restart
ac.RestartStack = func(string) error { return nil }
if err := handler.Apply(ac); err != nil {
m.logger.Printf("[WARN] [integrations] Integration config reapply failed for %s: %v", key, err)
if m.isDebug() {
m.logger.Printf("[DEBUG] [integrations] ReapplyConfigForTarget: %s failed: %v", key, err)
}
state.Status = "error"
state.LastError = err.Error()
_ = m.sett.SetIntegrationState(key, state)
continue
}
if m.isDebug() {
m.logger.Printf("[DEBUG] [integrations] ReapplyConfigForTarget: %s succeeded", key)
}
state.Status = "active"
state.LastError = ""
_ = m.sett.SetIntegrationState(key, state)
m.logger.Printf("[INFO] [integrations] Integration %s config reapplied for %s", key, targetName)
}
}
// loadDecryptedEnv reads app.yaml for a stack and decrypts sensitive values.
func (m *Manager) loadDecryptedEnv(s *stacks.Stack) map[string]string {
stackDir := filepath.Dir(s.ComposePath)
cfg := stacks.LoadAppConfig(stackDir)
if cfg == nil {
m.logger.Printf("[WARN] [integrations] Failed to load env for %s: app config is nil", s.Name)
return nil
}
if m.encKey != nil {
cfg.Env = crypto.DecryptMap(m.encKey, cfg.Env)
}
if m.isDebug() {
m.logger.Printf("[DEBUG] [integrations] loadDecryptedEnv: stack=%s envKeys=%d", s.Name, len(cfg.Env))
}
return cfg.Env
}