-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline_step_app.go
More file actions
150 lines (130 loc) · 4.89 KB
/
pipeline_step_app.go
File metadata and controls
150 lines (130 loc) · 4.89 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package module
import (
"context"
"fmt"
"github.com/CrisisTextLine/modular"
)
// ─── app_deploy ───────────────────────────────────────────────────────────────
// AppDeployStep deploys an app.container module.
type AppDeployStep struct {
name string
app string
appMod modular.Application
}
// NewAppDeployStepFactory returns a StepFactory for step.app_deploy.
func NewAppDeployStepFactory() StepFactory {
return func(name string, cfg map[string]any, app modular.Application) (PipelineStep, error) {
appName, _ := cfg["app"].(string)
if appName == "" {
return nil, fmt.Errorf("app_deploy step %q: 'app' is required", name)
}
return &AppDeployStep{name: name, app: appName, appMod: app}, nil
}
}
func (s *AppDeployStep) Name() string { return s.name }
func (s *AppDeployStep) Execute(_ context.Context, _ *PipelineContext) (*StepResult, error) {
m, err := resolveAppContainerModule(s.appMod, s.app, s.name)
if err != nil {
return nil, err
}
result, err := m.Deploy()
if err != nil {
return nil, fmt.Errorf("app_deploy step %q: %w", s.name, err)
}
return &StepResult{Output: map[string]any{
"result": result,
"app": s.app,
"status": result.Status,
"endpoint": result.Endpoint,
"image": result.Image,
"replicas": result.Replicas,
"platform": result.Platform,
}}, nil
}
// ─── app_status ───────────────────────────────────────────────────────────────
// AppStatusStep returns the current deployment status of an app.container module.
type AppStatusStep struct {
name string
app string
appMod modular.Application
}
// NewAppStatusStepFactory returns a StepFactory for step.app_status.
func NewAppStatusStepFactory() StepFactory {
return func(name string, cfg map[string]any, app modular.Application) (PipelineStep, error) {
appName, _ := cfg["app"].(string)
if appName == "" {
return nil, fmt.Errorf("app_status step %q: 'app' is required", name)
}
return &AppStatusStep{name: name, app: appName, appMod: app}, nil
}
}
func (s *AppStatusStep) Name() string { return s.name }
func (s *AppStatusStep) Execute(_ context.Context, _ *PipelineContext) (*StepResult, error) {
m, err := resolveAppContainerModule(s.appMod, s.app, s.name)
if err != nil {
return nil, err
}
result, err := m.Status()
if err != nil {
return nil, fmt.Errorf("app_status step %q: %w", s.name, err)
}
return &StepResult{Output: map[string]any{
"result": result,
"app": s.app,
"status": result.Status,
"platform": result.Platform,
"replicas": result.Replicas,
"image": result.Image,
}}, nil
}
// ─── app_rollback ─────────────────────────────────────────────────────────────
// AppRollbackStep rolls back an app.container module to the previous deployment state.
type AppRollbackStep struct {
name string
app string
appMod modular.Application
}
// NewAppRollbackStepFactory returns a StepFactory for step.app_rollback.
func NewAppRollbackStepFactory() StepFactory {
return func(name string, cfg map[string]any, app modular.Application) (PipelineStep, error) {
appName, _ := cfg["app"].(string)
if appName == "" {
return nil, fmt.Errorf("app_rollback step %q: 'app' is required", name)
}
return &AppRollbackStep{name: name, app: appName, appMod: app}, nil
}
}
func (s *AppRollbackStep) Name() string { return s.name }
func (s *AppRollbackStep) Execute(_ context.Context, _ *PipelineContext) (*StepResult, error) {
m, err := resolveAppContainerModule(s.appMod, s.app, s.name)
if err != nil {
return nil, err
}
result, err := m.Rollback()
if err != nil {
return nil, fmt.Errorf("app_rollback step %q: %w", s.name, err)
}
return &StepResult{Output: map[string]any{
"result": result,
"app": s.app,
"status": result.Status,
"rolled_back": true,
"image": result.Image,
"platform": result.Platform,
}}, nil
}
// ─── helpers ──────────────────────────────────────────────────────────────────
func resolveAppContainerModule(app modular.Application, appName, stepName string) (*AppContainerModule, error) {
if app == nil {
return nil, fmt.Errorf("step %q: no application context", stepName)
}
svc, ok := app.SvcRegistry()[appName]
if !ok {
return nil, fmt.Errorf("step %q: app service %q not found in registry", stepName, appName)
}
m, ok := svc.(*AppContainerModule)
if !ok {
return nil, fmt.Errorf("step %q: service %q is not a *AppContainerModule (got %T)", stepName, appName, svc)
}
return m, nil
}