-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiac_module.go
More file actions
84 lines (72 loc) · 2.16 KB
/
iac_module.go
File metadata and controls
84 lines (72 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package module
import (
"context"
"fmt"
"github.com/CrisisTextLine/modular"
)
// IaCModule registers an IaCStateStore in the service registry.
// Supported backends: "memory" (default) and "filesystem".
//
// Config example:
//
// modules:
// - name: iac-state
// type: iac.state
// config:
// backend: filesystem
// directory: /var/lib/workflow/iac-state
type IaCModule struct {
name string
backend string
config map[string]any
store IaCStateStore
}
// NewIaCModule creates a new IaC state module.
func NewIaCModule(name string, cfg map[string]any) *IaCModule {
return &IaCModule{name: name, config: cfg}
}
// Name returns the module name.
func (m *IaCModule) Name() string { return m.name }
// Init constructs the state store backend and registers it as a service.
func (m *IaCModule) Init(app modular.Application) error {
m.backend, _ = m.config["backend"].(string)
if m.backend == "" {
m.backend = "memory"
}
switch m.backend {
case "memory":
m.store = NewMemoryIaCStateStore()
case "filesystem":
dir, _ := m.config["directory"].(string)
if dir == "" {
dir = "/var/lib/workflow/iac-state"
}
m.store = NewFSIaCStateStore(dir)
default:
return fmt.Errorf("iac.state %q: unsupported backend %q (use 'memory' or 'filesystem')", m.name, m.backend)
}
return app.RegisterService(m.name, m.store)
}
// ProvidesServices declares the IaCStateStore service.
func (m *IaCModule) ProvidesServices() []modular.ServiceProvider {
return []modular.ServiceProvider{
{
Name: m.name,
Description: "IaC state store (" + m.backend + "): " + m.name,
Instance: m.store,
},
}
}
// RequiresServices returns nil — iac.state has no service dependencies.
func (m *IaCModule) RequiresServices() []modular.ServiceDependency { return nil }
// Start is a no-op for the memory backend; the filesystem backend creates the directory.
func (m *IaCModule) Start(_ context.Context) error {
if fs, ok := m.store.(*FSIaCStateStore); ok {
if err := fs.ensureDir(); err != nil {
return fmt.Errorf("iac.state %q: Start: %w", m.name, err)
}
}
return nil
}
// Stop is a no-op.
func (m *IaCModule) Stop(_ context.Context) error { return nil }