-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider.go
More file actions
599 lines (529 loc) · 17.6 KB
/
provider.go
File metadata and controls
599 lines (529 loc) · 17.6 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
package dockercompose
import (
"context"
"fmt"
"os"
"path/filepath"
"sync"
"github.com/GoCodeAlone/workflow/platform"
"github.com/GoCodeAlone/workflow/platform/providers/dockercompose/drivers"
)
const (
// ProviderName is the identifier for the Docker Compose provider.
ProviderName = "docker-compose"
// ProviderVersion is the current version of the Docker Compose provider.
ProviderVersion = "0.1.0"
)
// DockerComposeProvider implements platform.Provider by mapping abstract
// capabilities to Docker Compose services, networks, and volumes. It executes
// docker compose commands via exec.Command and persists state to the local
// filesystem. No Docker SDK dependency is required.
type DockerComposeProvider struct {
mu sync.RWMutex
config map[string]any
executor ComposeExecutor
mapper *ComposeCapabilityMapper
stateStore *FileStateStore
drivers map[string]platform.ResourceDriver
// projectDir is the directory where docker-compose.yml is written.
projectDir string
// stateDir is the directory where state JSON files are stored.
stateDir string
initialized bool
}
// NewProvider creates a new DockerComposeProvider. This is the ProviderFactory
// function registered with the engine.
func NewProvider() platform.Provider {
return &DockerComposeProvider{
executor: NewShellExecutor(),
mapper: NewCapabilityMapper(),
drivers: make(map[string]platform.ResourceDriver),
}
}
// NewProviderWithExecutor creates a DockerComposeProvider with a custom executor,
// enabling tests to inject a mock.
func NewProviderWithExecutor(exec ComposeExecutor) *DockerComposeProvider {
return &DockerComposeProvider{
executor: exec,
mapper: NewCapabilityMapper(),
drivers: make(map[string]platform.ResourceDriver),
}
}
// Name returns the provider identifier.
func (p *DockerComposeProvider) Name() string {
return ProviderName
}
// Version returns the provider version string.
func (p *DockerComposeProvider) Version() string {
return ProviderVersion
}
// Initialize validates that Docker is available and sets up the project directory.
// Config keys:
// - project_dir: directory for docker-compose.yml (default: current directory)
// - state_dir: directory for state files (default: project_dir/.platform-state)
func (p *DockerComposeProvider) Initialize(ctx context.Context, config map[string]any) error {
p.mu.Lock()
defer p.mu.Unlock()
if err := p.executor.IsAvailable(ctx); err != nil {
return fmt.Errorf("docker compose provider initialization failed: %w", err)
}
p.config = config
// Resolve project directory
p.projectDir = "."
if dir, ok := config["project_dir"].(string); ok && dir != "" {
p.projectDir = dir
}
// Resolve state directory
p.stateDir = filepath.Join(p.projectDir, ".platform-state")
if dir, ok := config["state_dir"].(string); ok && dir != "" {
p.stateDir = dir
}
// Ensure project directory exists
if err := os.MkdirAll(p.projectDir, 0o750); err != nil {
return fmt.Errorf("create project directory: %w", err)
}
// Initialize state store
store, err := NewFileStateStore(p.stateDir)
if err != nil {
return fmt.Errorf("initialize state store: %w", err)
}
p.stateStore = store
// Register resource drivers
p.drivers["docker-compose.service"] = drivers.NewServiceDriver(p.executor, p.projectDir)
p.drivers["docker-compose.network"] = drivers.NewNetworkDriver(p.executor, p.projectDir)
p.drivers["docker-compose.volume"] = drivers.NewVolumeDriver(p.executor, p.projectDir)
p.drivers["docker-compose.stub"] = drivers.NewStubDriver()
p.initialized = true
return nil
}
// Capabilities returns the set of capability types this provider supports.
func (p *DockerComposeProvider) Capabilities() []platform.CapabilityType {
return []platform.CapabilityType{
{
Name: CapContainerRuntime,
Description: "Docker Compose service from a container image",
Tier: platform.TierApplication,
Properties: []platform.PropertySchema{
{Name: "image", Type: "string", Required: true, Description: "Docker image name"},
{Name: "replicas", Type: "int", Required: false, Description: "Number of service replicas", DefaultValue: 1},
{Name: "memory", Type: "string", Required: false, Description: "Memory limit (e.g., 512M)"},
{Name: "cpu", Type: "string", Required: false, Description: "CPU limit (e.g., 0.5)"},
{Name: "ports", Type: "list", Required: false, Description: "Port mappings"},
{Name: "health_check", Type: "map", Required: false, Description: "Container health check"},
{Name: "env", Type: "map", Required: false, Description: "Environment variables"},
{Name: "command", Type: "string", Required: false, Description: "Override container command"},
},
Fidelity: platform.FidelityPartial,
},
{
Name: CapDatabase,
Description: "Database service (PostgreSQL, MySQL, Redis)",
Tier: platform.TierSharedPrimitive,
Properties: []platform.PropertySchema{
{Name: "engine", Type: "string", Required: false, Description: "Database engine", DefaultValue: "postgresql"},
{Name: "version", Type: "string", Required: false, Description: "Engine version"},
{Name: "storage_gb", Type: "int", Required: false, Description: "Storage in GB"},
},
Fidelity: platform.FidelityPartial,
},
{
Name: CapMessageQueue,
Description: "Message queue service (RabbitMQ, Redis, Kafka)",
Tier: platform.TierSharedPrimitive,
Properties: []platform.PropertySchema{
{Name: "engine", Type: "string", Required: false, Description: "Queue engine", DefaultValue: "rabbitmq"},
{Name: "version", Type: "string", Required: false, Description: "Engine version"},
},
Fidelity: platform.FidelityPartial,
},
{
Name: CapNetwork,
Description: "Docker Compose network",
Tier: platform.TierInfrastructure,
Properties: []platform.PropertySchema{
{Name: "driver", Type: "string", Required: false, Description: "Network driver", DefaultValue: "bridge"},
{Name: "cidr", Type: "string", Required: false, Description: "Subnet CIDR"},
},
Fidelity: platform.FidelityPartial,
},
{
Name: CapKubernetesCluster,
Description: "Kubernetes cluster (stubbed -- not supported in Docker Compose)",
Tier: platform.TierInfrastructure,
Fidelity: platform.FidelityStub,
},
{
Name: CapLoadBalancer,
Description: "Load balancer as nginx or traefik compose service",
Tier: platform.TierSharedPrimitive,
Properties: []platform.PropertySchema{
{Name: "type", Type: "string", Required: false, Description: "Load balancer type (nginx, traefik)", DefaultValue: "nginx"},
{Name: "ports", Type: "list", Required: false, Description: "Port mappings"},
},
Fidelity: platform.FidelityPartial,
},
{
Name: CapNamespace,
Description: "Namespace mapped to a Docker network (approximate)",
Tier: platform.TierSharedPrimitive,
Fidelity: platform.FidelityStub,
},
{
Name: CapPersistentVolume,
Description: "Docker Compose named volume",
Tier: platform.TierSharedPrimitive,
Properties: []platform.PropertySchema{
{Name: "driver", Type: "string", Required: false, Description: "Volume driver", DefaultValue: "local"},
},
Fidelity: platform.FidelityFull,
},
}
}
// MapCapability resolves an abstract capability declaration to provider-specific
// resource plans using the capability mapper.
func (p *DockerComposeProvider) MapCapability(ctx context.Context, decl platform.CapabilityDeclaration, pctx *platform.PlatformContext) ([]platform.ResourcePlan, error) {
p.mu.RLock()
defer p.mu.RUnlock()
if !p.initialized {
return nil, platform.ErrProviderNotInitialized
}
if !p.mapper.CanMap(decl.Type) {
return nil, &platform.CapabilityUnsupportedError{
Capability: decl.Type,
Provider: ProviderName,
}
}
return p.mapper.Map(decl, pctx)
}
// ResourceDriver returns the driver for a specific resource type.
func (p *DockerComposeProvider) ResourceDriver(resourceType string) (platform.ResourceDriver, error) {
p.mu.RLock()
defer p.mu.RUnlock()
driver, ok := p.drivers[resourceType]
if !ok {
return nil, &platform.ResourceDriverNotFoundError{
ResourceType: resourceType,
Provider: ProviderName,
}
}
return driver, nil
}
// CredentialBroker returns nil because Docker Compose does not support
// credential brokering.
func (p *DockerComposeProvider) CredentialBroker() platform.CredentialBroker {
return nil
}
// StateStore returns the file-system-based state store.
func (p *DockerComposeProvider) StateStore() platform.StateStore {
return p.stateStore
}
// Healthy returns nil if Docker is reachable.
func (p *DockerComposeProvider) Healthy(ctx context.Context) error {
return p.executor.IsAvailable(ctx)
}
// Close releases any resources held by the provider.
func (p *DockerComposeProvider) Close() error {
return nil
}
// ComposeFilePath returns the path to the generated docker-compose.yml.
func (p *DockerComposeProvider) ComposeFilePath() string {
return filepath.Join(p.projectDir, "docker-compose.yml")
}
// GenerateComposeFile builds a ComposeFile from a set of resource plans and
// writes it to the project directory.
func (p *DockerComposeProvider) GenerateComposeFile(plans []platform.ResourcePlan) (*ComposeFile, error) {
cf := NewComposeFile()
for _, plan := range plans {
switch plan.ResourceType {
case "docker-compose.service":
svc := buildComposeService(plan)
cf.AddService(plan.Name, svc)
case "docker-compose.network":
net := buildComposeNetwork(plan)
cf.AddNetwork(plan.Name, net)
case "docker-compose.volume":
vol := buildComposeVolume(plan)
cf.AddVolume(plan.Name, vol)
case "docker-compose.stub":
// Stubs produce no compose output
}
}
return cf, nil
}
// WriteComposeFile writes the compose file to the project directory.
func (p *DockerComposeProvider) WriteComposeFile(cf *ComposeFile) error {
content, err := cf.MarshalYAML()
if err != nil {
return fmt.Errorf("marshal compose file: %w", err)
}
path := p.ComposeFilePath()
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
return fmt.Errorf("write compose file: %w", err)
}
return nil
}
// Up runs docker compose up for the project.
func (p *DockerComposeProvider) Up(ctx context.Context) (string, error) {
return p.executor.Up(ctx, p.projectDir, p.ComposeFilePath())
}
// Down runs docker compose down for the project.
func (p *DockerComposeProvider) Down(ctx context.Context) (string, error) {
return p.executor.Down(ctx, p.projectDir, p.ComposeFilePath())
}
// FidelityReports returns fidelity gap reports for capabilities that are not
// fully implemented by Docker Compose.
func (p *DockerComposeProvider) FidelityReports(decls []platform.CapabilityDeclaration) []platform.FidelityReport {
var reports []platform.FidelityReport
for _, decl := range decls {
switch decl.Type {
case CapKubernetesCluster:
reports = append(reports, platform.FidelityReport{
Capability: decl.Type,
Provider: ProviderName,
Fidelity: platform.FidelityStub,
Gaps: []platform.FidelityGap{
{
Property: "cluster",
Description: "Docker Compose cannot provision a Kubernetes cluster",
Workaround: "Use kind or k3d for local Kubernetes if needed",
},
},
})
case CapContainerRuntime:
var gaps []platform.FidelityGap
if _, ok := decl.Properties["ingress"]; ok {
gaps = append(gaps, platform.FidelityGap{
Property: "ingress.tls",
Description: "TLS termination uses self-signed certs in Docker Compose",
Workaround: "Production uses ACM/cert-manager; local uses plain HTTP or self-signed",
})
}
if _, ok := decl.Properties["health_check"]; ok {
gaps = append(gaps, platform.FidelityGap{
Property: "health_check.liveness",
Description: "Docker healthcheck has different semantics than Kubernetes probes",
Workaround: "Docker HEALTHCHECK is used as an approximation",
})
}
if len(gaps) > 0 {
reports = append(reports, platform.FidelityReport{
Capability: decl.Type,
Provider: ProviderName,
Fidelity: platform.FidelityPartial,
Gaps: gaps,
})
}
case CapDatabase:
if multiAZ, ok := decl.Properties["multi_az"]; ok {
if b, ok := multiAZ.(bool); ok && b {
reports = append(reports, platform.FidelityReport{
Capability: decl.Type,
Provider: ProviderName,
Fidelity: platform.FidelityPartial,
Gaps: []platform.FidelityGap{
{
Property: "multi_az",
Description: "Multi-AZ is not supported in Docker Compose",
Workaround: "Single instance used for local development",
},
},
})
}
}
case CapNetwork:
if _, ok := decl.Properties["availability_zones"]; ok {
reports = append(reports, platform.FidelityReport{
Capability: decl.Type,
Provider: ProviderName,
Fidelity: platform.FidelityPartial,
Gaps: []platform.FidelityGap{
{
Property: "availability_zones",
Description: "Docker Compose uses a single bridge network",
Workaround: "Single network used for local development",
},
},
})
}
case CapNamespace:
reports = append(reports, platform.FidelityReport{
Capability: decl.Type,
Provider: ProviderName,
Fidelity: platform.FidelityStub,
Gaps: []platform.FidelityGap{
{
Property: "resource_quotas",
Description: "Resource quotas are not enforced in Docker Compose",
Workaround: "Docker resource limits approximate quota enforcement",
},
},
})
}
}
return reports
}
// buildComposeService creates a ComposeService from a ResourcePlan.
func buildComposeService(plan platform.ResourcePlan) *ComposeService {
svc := &ComposeService{
Image: getStr(plan.Properties, "image"),
Restart: "unless-stopped",
}
if cmd := getStr(plan.Properties, "command"); cmd != "" {
svc.Command = cmd
}
// Replicas and resource limits
replicas, _ := getIntProp(plan.Properties, "replicas")
memory := getStr(plan.Properties, "memory")
cpu := getStr(plan.Properties, "cpu")
if replicas > 0 || memory != "" || cpu != "" {
svc.Deploy = &DeployConfig{}
if replicas > 0 {
svc.Deploy.Replicas = replicas
}
if memory != "" || cpu != "" {
svc.Deploy.Resources = &ResourcesConfig{
Limits: &ResourceSpec{
CPUs: cpu,
Memory: memory,
},
}
}
}
// Ports
if portsRaw, ok := plan.Properties["ports"]; ok {
svc.Ports = parsePorts(portsRaw)
}
// Environment
if envRaw, ok := plan.Properties["env"]; ok {
svc.Environment = parseEnv(envRaw)
}
// Health check
if hcRaw, ok := plan.Properties["health_check"]; ok {
svc.Healthcheck = parseHealthcheck(hcRaw)
}
// Volume mount for database services
if vol := getStr(plan.Properties, "volume"); vol != "" {
// Determine mount path based on image
mountPath := "/data"
image := svc.Image
switch {
case contains(image, "postgres"):
mountPath = "/var/lib/postgresql/data"
case contains(image, "mysql"):
mountPath = "/var/lib/mysql"
case contains(image, "redis"):
mountPath = "/data"
}
svc.Volumes = []string{vol + ":" + mountPath}
}
// DependsOn
if len(plan.DependsOn) > 0 {
svc.DependsOn = plan.DependsOn
}
return svc
}
// buildComposeNetwork creates a ComposeNetwork from a ResourcePlan.
func buildComposeNetwork(plan platform.ResourcePlan) *ComposeNetwork {
net := &ComposeNetwork{}
if driver := getStr(plan.Properties, "driver"); driver != "" {
net.Driver = driver
}
if subnet := getStr(plan.Properties, "subnet"); subnet != "" {
net.IPAM = &IPAMConfig{
Config: []IPAMPoolConfig{
{Subnet: subnet},
},
}
}
return net
}
// buildComposeVolume creates a ComposeVolume from a ResourcePlan.
func buildComposeVolume(plan platform.ResourcePlan) *ComposeVolume {
vol := &ComposeVolume{}
if driver := getStr(plan.Properties, "driver"); driver != "" {
vol.Driver = driver
}
return vol
}
func getStr(props map[string]any, key string) string {
val, ok := props[key]
if !ok {
return ""
}
s, ok := val.(string)
if !ok {
return ""
}
return s
}
func parsePorts(portsRaw any) []string {
var result []string
switch ports := portsRaw.(type) {
case []any:
for _, p := range ports {
switch pp := p.(type) {
case map[string]any:
containerPort := fmt.Sprintf("%v", pp["container_port"])
hostPort := containerPort
if hp, ok := pp["host_port"]; ok {
hostPort = fmt.Sprintf("%v", hp)
}
result = append(result, hostPort+":"+containerPort)
case string:
result = append(result, pp)
}
}
case []map[string]any:
for _, pp := range ports {
containerPort := fmt.Sprintf("%v", pp["container_port"])
hostPort := containerPort
if hp, ok := pp["host_port"]; ok {
hostPort = fmt.Sprintf("%v", hp)
}
result = append(result, hostPort+":"+containerPort)
}
}
return result
}
func parseEnv(envRaw any) map[string]string {
result := make(map[string]string)
switch env := envRaw.(type) {
case map[string]any:
for k, v := range env {
result[k] = fmt.Sprintf("%v", v)
}
case map[string]string:
return env
}
return result
}
func parseHealthcheck(hcRaw any) *HealthcheckConfig {
hcMap, ok := hcRaw.(map[string]any)
if !ok {
return nil
}
hc := &HealthcheckConfig{}
if path, ok := hcMap["path"].(string); ok {
hc.Test = []string{"CMD-SHELL", fmt.Sprintf("curl -f http://localhost%s || exit 1", path)}
}
if interval, ok := hcMap["interval"].(string); ok {
hc.Interval = interval
} else {
hc.Interval = "30s"
}
hc.Timeout = "10s"
hc.Retries = 3
hc.StartPeriod = "10s"
return hc
}
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(s) > 0 && containsImpl(s, substr))
}
func containsImpl(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}