-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodebuild.go
More file actions
352 lines (305 loc) · 11.1 KB
/
codebuild.go
File metadata and controls
352 lines (305 loc) · 11.1 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package module
import (
"fmt"
"strings"
"time"
"github.com/CrisisTextLine/modular"
)
// CodeBuildProjectState holds the current state of a managed CodeBuild project.
type CodeBuildProjectState struct {
Name string `json:"name"`
Region string `json:"region"`
ServiceRole string `json:"serviceRole"`
ComputeType string `json:"computeType"`
Image string `json:"image"`
Status string `json:"status"` // pending, creating, ready, deleting, deleted
SourceType string `json:"sourceType"`
BuildspecPath string `json:"buildspecPath"`
EnvVars map[string]string `json:"envVars,omitempty"`
CreatedAt time.Time `json:"createdAt"`
ARN string `json:"arn"`
}
// CodeBuildBuild represents a single CodeBuild build invocation.
type CodeBuildBuild struct {
ID string `json:"id"`
ProjectName string `json:"projectName"`
Status string `json:"status"` // IN_PROGRESS, SUCCEEDED, FAILED, STOPPED
Phase string `json:"phase"`
StartTime time.Time `json:"startTime"`
EndTime *time.Time `json:"endTime,omitempty"`
Logs []string `json:"logs"`
EnvVars map[string]string `json:"envVars,omitempty"`
BuildNumber int64 `json:"buildNumber"`
}
// CodeBuildModule manages AWS CodeBuild projects and builds via pluggable backends.
// Config:
//
// account: name of a cloud.account module (resolved from service registry)
// region: AWS region (e.g. us-east-1)
// service_role: IAM role ARN for CodeBuild
// compute_type: BUILD_GENERAL1_SMALL, BUILD_GENERAL1_MEDIUM, etc.
// image: CodeBuild managed image (e.g. aws/codebuild/standard:7.0)
// source_type: GITHUB, CODECOMMIT, BITBUCKET, NO_SOURCE (default: NO_SOURCE)
type CodeBuildModule struct {
name string
config map[string]any
provider CloudCredentialProvider
state *CodeBuildProjectState
builds map[string]*CodeBuildBuild
backend codebuildBackend
}
// codebuildBackend is the internal interface for CodeBuild backends.
type codebuildBackend interface {
createProject(m *CodeBuildModule) error
deleteProject(m *CodeBuildModule) error
startBuild(m *CodeBuildModule, envOverrides map[string]string) (*CodeBuildBuild, error)
getBuildStatus(m *CodeBuildModule, buildID string) (*CodeBuildBuild, error)
getBuildLogs(m *CodeBuildModule, buildID string) ([]string, error)
listBuilds(m *CodeBuildModule) ([]*CodeBuildBuild, error)
}
// NewCodeBuildModule creates a new CodeBuildModule.
func NewCodeBuildModule(name string, cfg map[string]any) *CodeBuildModule {
return &CodeBuildModule{
name: name,
config: cfg,
builds: make(map[string]*CodeBuildBuild),
}
}
// Name returns the module name.
func (m *CodeBuildModule) Name() string { return m.name }
// Init resolves the cloud.account service and initializes the backend.
func (m *CodeBuildModule) Init(app modular.Application) error {
region, _ := m.config["region"].(string)
if region == "" {
region = "us-east-1"
}
serviceRole, _ := m.config["service_role"].(string)
if serviceRole == "" {
serviceRole = fmt.Sprintf("arn:aws:iam::123456789012:role/CodeBuildServiceRole-%s", m.name)
}
computeType, _ := m.config["compute_type"].(string)
if computeType == "" {
computeType = "BUILD_GENERAL1_SMALL"
}
image, _ := m.config["image"].(string)
if image == "" {
image = "aws/codebuild/standard:7.0"
}
sourceType, _ := m.config["source_type"].(string)
if sourceType == "" {
sourceType = "NO_SOURCE"
}
accountName, _ := m.config["account"].(string)
if accountName != "" {
svc, ok := app.SvcRegistry()[accountName]
if !ok {
return fmt.Errorf("aws.codebuild %q: account service %q not found", m.name, accountName)
}
provider, ok := svc.(CloudCredentialProvider)
if !ok {
return fmt.Errorf("aws.codebuild %q: service %q does not implement CloudCredentialProvider", m.name, accountName)
}
m.provider = provider
}
m.state = &CodeBuildProjectState{
Name: m.name,
Region: region,
ServiceRole: serviceRole,
ComputeType: computeType,
Image: image,
Status: "pending",
SourceType: sourceType,
ARN: fmt.Sprintf("arn:aws:codebuild:%s:123456789012:project/%s", region, m.name),
}
m.backend = &codebuildMockBackend{}
return app.RegisterService(m.name, m)
}
// ProvidesServices declares the service this module provides.
func (m *CodeBuildModule) ProvidesServices() []modular.ServiceProvider {
return []modular.ServiceProvider{
{Name: m.name, Description: "CodeBuild project: " + m.name, Instance: m},
}
}
// RequiresServices returns nil — cloud.account is resolved by name, not declared.
func (m *CodeBuildModule) RequiresServices() []modular.ServiceDependency {
return nil
}
// CreateProject creates or updates the CodeBuild project.
func (m *CodeBuildModule) CreateProject() error {
return m.backend.createProject(m)
}
// DeleteProject removes the CodeBuild project.
func (m *CodeBuildModule) DeleteProject() error {
return m.backend.deleteProject(m)
}
// StartBuild starts a new build, optionally overriding environment variables.
func (m *CodeBuildModule) StartBuild(envOverrides map[string]string) (*CodeBuildBuild, error) {
return m.backend.startBuild(m, envOverrides)
}
// GetBuildStatus returns the current status of a build.
func (m *CodeBuildModule) GetBuildStatus(buildID string) (*CodeBuildBuild, error) {
return m.backend.getBuildStatus(m, buildID)
}
// GetBuildLogs retrieves the log lines for a build.
func (m *CodeBuildModule) GetBuildLogs(buildID string) ([]string, error) {
return m.backend.getBuildLogs(m, buildID)
}
// ListBuilds returns all builds for this project.
func (m *CodeBuildModule) ListBuilds() ([]*CodeBuildBuild, error) {
return m.backend.listBuilds(m)
}
// GenerateBuildspec translates a pipeline step config into a CodeBuild buildspec YAML string.
// Supported config keys: install_commands, pre_build_commands, build_commands,
// post_build_commands, artifact_files, artifact_dir, cache_paths, env_variables.
func (m *CodeBuildModule) GenerateBuildspec(pipelineConfig map[string]any) string {
lines := []string{"version: 0.2", ""}
// Env section
if vars, ok := pipelineConfig["env_variables"].(map[string]any); ok && len(vars) > 0 {
lines = append(lines, "env:", " variables:")
for k, v := range vars {
lines = append(lines, fmt.Sprintf(" %s: %q", k, fmt.Sprint(v)))
}
lines = append(lines, "")
}
lines = append(lines, "phases:")
// install phase
if cmds := codebuildExtractStringSlice(pipelineConfig, "install_commands"); len(cmds) > 0 {
lines = append(lines, " install:", " commands:")
for _, cmd := range cmds {
lines = append(lines, " - "+cmd)
}
}
// pre_build phase
if cmds := codebuildExtractStringSlice(pipelineConfig, "pre_build_commands"); len(cmds) > 0 {
lines = append(lines, " pre_build:", " commands:")
for _, cmd := range cmds {
lines = append(lines, " - "+cmd)
}
}
// build phase (required — always present)
buildCmds := codebuildExtractStringSlice(pipelineConfig, "build_commands")
if len(buildCmds) == 0 {
buildCmds = []string{"echo Build started", "echo Build complete"}
}
lines = append(lines, " build:", " commands:")
for _, cmd := range buildCmds {
lines = append(lines, " - "+cmd)
}
// post_build phase
if cmds := codebuildExtractStringSlice(pipelineConfig, "post_build_commands"); len(cmds) > 0 {
lines = append(lines, " post_build:", " commands:")
for _, cmd := range cmds {
lines = append(lines, " - "+cmd)
}
}
// Artifacts section
if files := codebuildExtractStringSlice(pipelineConfig, "artifact_files"); len(files) > 0 {
lines = append(lines, "", "artifacts:", " files:")
for _, f := range files {
lines = append(lines, " - "+f)
}
if dir, ok := pipelineConfig["artifact_dir"].(string); ok && dir != "" {
lines = append(lines, " base-directory: "+dir)
}
}
// Cache section
if paths := codebuildExtractStringSlice(pipelineConfig, "cache_paths"); len(paths) > 0 {
lines = append(lines, "", "cache:", " paths:")
for _, p := range paths {
lines = append(lines, " - "+p)
}
}
return strings.Join(lines, "\n")
}
// codebuildExtractStringSlice extracts a []string from a map value that may be []any or []string.
func codebuildExtractStringSlice(m map[string]any, key string) []string {
v, ok := m[key]
if !ok {
return nil
}
switch val := v.(type) {
case []string:
return val
case []any:
result := make([]string, 0, len(val))
for _, item := range val {
if s, ok := item.(string); ok {
result = append(result, s)
}
}
return result
}
return nil
}
// ─── mock backend ─────────────────────────────────────────────────────────────
// codebuildMockBackend implements codebuildBackend using in-memory state.
// Real implementation would use aws-sdk-go-v2/service/codebuild.
type codebuildMockBackend struct {
buildCounter int64
}
func (b *codebuildMockBackend) createProject(m *CodeBuildModule) error {
if m.state.Status == "pending" || m.state.Status == "deleted" {
m.state.Status = "creating"
m.state.CreatedAt = time.Now()
// In-memory: immediately transition to ready.
m.state.Status = "ready"
}
return nil
}
func (b *codebuildMockBackend) deleteProject(m *CodeBuildModule) error {
if m.state.Status == "deleted" {
return nil
}
m.state.Status = "deleting"
// In-memory: immediately mark deleted.
m.state.Status = "deleted"
return nil
}
func (b *codebuildMockBackend) startBuild(m *CodeBuildModule, envOverrides map[string]string) (*CodeBuildBuild, error) {
if m.state.Status != "ready" {
return nil, fmt.Errorf("codebuild: project %q is not ready (status=%s)", m.name, m.state.Status)
}
b.buildCounter++
buildID := fmt.Sprintf("%s:%x", m.name, b.buildCounter)
now := time.Now()
end := now.Add(30 * time.Second)
build := &CodeBuildBuild{
ID: buildID,
ProjectName: m.name,
Status: "SUCCEEDED",
Phase: "COMPLETED",
StartTime: now,
EndTime: &end,
Logs: []string{
"[Container] Build started",
"[Container] Running install commands",
"[Container] Running build commands",
"[Container] Build succeeded",
},
EnvVars: envOverrides,
BuildNumber: b.buildCounter,
}
m.builds[buildID] = build
return build, nil
}
func (b *codebuildMockBackend) getBuildStatus(m *CodeBuildModule, buildID string) (*CodeBuildBuild, error) {
build, ok := m.builds[buildID]
if !ok {
return nil, fmt.Errorf("codebuild: build %q not found in project %q", buildID, m.name)
}
return build, nil
}
func (b *codebuildMockBackend) getBuildLogs(m *CodeBuildModule, buildID string) ([]string, error) {
build, ok := m.builds[buildID]
if !ok {
return nil, fmt.Errorf("codebuild: build %q not found in project %q", buildID, m.name)
}
return build.Logs, nil
}
func (b *codebuildMockBackend) listBuilds(m *CodeBuildModule) ([]*CodeBuildBuild, error) {
result := make([]*CodeBuildBuild, 0, len(m.builds))
for _, build := range m.builds {
result = append(result, build)
}
return result, nil
}