Files
o11y-foundry/internal/casting/dockercomposecasting/casting.go
T

171 lines
6.2 KiB
Go

package dockercomposecasting
import (
"bytes"
"context"
"errors"
"fmt"
"log/slog"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/hanzoai/o11y-foundry/api/v1alpha1/installation"
rootcasting "github.com/hanzoai/o11y-foundry/internal/casting"
"github.com/hanzoai/o11y-foundry/internal/domain"
foundryerrors "github.com/hanzoai/o11y-foundry/internal/errors"
"github.com/hanzoai/o11y-foundry/internal/molding"
)
var _ rootcasting.Casting = (*dockerComposeCasting)(nil)
type dockerComposeCasting struct {
logger *slog.Logger
castings []*domain.Template
}
func New(logger *slog.Logger) *dockerComposeCasting {
return &dockerComposeCasting{
logger: logger,
castings: []*domain.Template{
composeYAMLTemplate,
},
}
}
func (casting *dockerComposeCasting) Enricher(ctx context.Context, config *installation.Casting) (molding.MoldingEnricher, error) {
return newDockerComposeMoldingEnricher(config)
}
func (casting *dockerComposeCasting) Forge(ctx context.Context, config installation.Casting, poursPath string) ([]domain.Material, error) {
buf := bytes.NewBuffer(nil)
err := composeYAMLTemplate.Execute(buf, config)
if err != nil {
return nil, foundryerrors.Wrapf(err, foundryerrors.TypeInternal, "failed to execute compose yaml template")
}
composeMaterial, err := domain.NewYAMLMaterial(buf.Bytes(), filepath.Join(rootcasting.DeploymentDir, "compose.yaml"))
if err != nil {
return nil, foundryerrors.Wrapf(err, foundryerrors.TypeInternal, "failed to create compose yaml material")
}
materials := []domain.Material{composeMaterial}
// Add telemetrykeeper config files
for filename, content := range config.Spec.TelemetryKeeper.Spec.Config.Data {
material, err := domain.NewYAMLMaterial([]byte(content), filepath.Join(rootcasting.DeploymentDir, "telemetrykeeper", config.Spec.TelemetryKeeper.Kind.String(), filename))
if err != nil {
return nil, foundryerrors.Wrapf(err, foundryerrors.TypeInternal, "failed to create telemetrykeeper config material")
}
materials = append(materials, material)
}
// Add telemetrystore config files
for filename, content := range config.Spec.TelemetryStore.Spec.Config.Data {
material, err := domain.NewYAMLMaterial([]byte(content), filepath.Join(rootcasting.DeploymentDir, "telemetrystore", config.Spec.TelemetryStore.Kind.String(), filename))
if err != nil {
return nil, foundryerrors.Wrapf(err, foundryerrors.TypeInternal, "failed to create telemetrystore config material")
}
materials = append(materials, material)
}
// Add metastore config files
for filename, content := range config.Spec.MetaStore.Spec.Config.Data {
material, err := domain.NewYAMLMaterial([]byte(content), filepath.Join(rootcasting.DeploymentDir, "metastore", config.Spec.MetaStore.Kind.String(), filename))
if err != nil {
return nil, foundryerrors.Wrapf(err, foundryerrors.TypeInternal, "failed to create metastore config material")
}
materials = append(materials, material)
}
// Add o11y config files
for filename, content := range config.Spec.O11y.Spec.Config.Data {
material, err := domain.NewYAMLMaterial([]byte(content), filepath.Join(rootcasting.DeploymentDir, "configs", "o11y", filename))
if err != nil {
return nil, fmt.Errorf("failed to create o11y config material: %w", err)
}
materials = append(materials, material)
}
// Add ingester config files
for filename, content := range config.Spec.Ingester.Spec.Config.Data {
material, err := domain.NewYAMLMaterial([]byte(content), filepath.Join(rootcasting.DeploymentDir, "ingester", filename))
if err != nil {
return nil, foundryerrors.Wrapf(err, foundryerrors.TypeInternal, "failed to create ingester config material")
}
materials = append(materials, material)
}
return materials, nil
}
func (casting *dockerComposeCasting) Cast(ctx context.Context, config installation.Casting, outputPath string) error {
casting.logger.InfoContext(ctx, "Executing commands for platform")
// Check if compose file exists
composeFile := filepath.Join(outputPath, rootcasting.DeploymentDir, "compose.yaml")
if _, err := os.Stat(composeFile); os.IsNotExist(err) {
return foundryerrors.Newf(foundryerrors.TypeNotFound, "compose file does not exist at path: %s", composeFile)
}
// Create a context with 5-minute timeout
runctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
// Get the available docker compose command
composeCmd, err := getComposeCommand(runctx)
if err != nil {
casting.logger.ErrorContext(runctx, "Docker compose not available", slog.String("error", err.Error()))
return foundryerrors.Wrapf(err, foundryerrors.TypeNotFound, "docker compose not available")
}
args := append(composeCmd[1:], "-f", composeFile, "up", "-d")
casting.logger.DebugContext(runctx, "Running command", slog.String("command", strings.Join(append([]string{composeCmd[0]}, args...), " ")))
cmd := exec.CommandContext(runctx, composeCmd[0], args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
casting.logger.ErrorContext(runctx, "Command execution failed", slog.String("error", err.Error()))
return err
}
casting.logger.InfoContext(runctx, "Command executed successfully")
return nil
}
func getComposeMaterial(config *installation.Casting, path string) (domain.StructuredMaterial, error) {
buf := bytes.NewBuffer(nil)
err := composeYAMLTemplate.Execute(buf, config)
if err != nil {
return nil, foundryerrors.Wrapf(err, foundryerrors.TypeInternal, "failed to execute compose yaml template")
}
return domain.NewYAMLMaterial(buf.Bytes(), path)
}
// getComposeCommand detects the available docker compose command.
// It checks for "docker compose" (newer, preferred) first, then falls back to "docker-compose" (legacy).
func getComposeCommand(ctx context.Context) ([]string, error) {
// Check "docker compose" first (newer, preferred)
if _, err := exec.LookPath("docker"); err == nil {
cmd := exec.CommandContext(ctx, "docker", "compose", "version")
if err := cmd.Run(); err == nil {
return []string{"docker", "compose"}, nil
}
}
// Fallback to "docker-compose" (legacy)
if _, err := exec.LookPath("docker-compose"); err == nil {
return []string{"docker-compose"}, nil
}
return nil, errors.New("neither 'docker compose' nor 'docker-compose' is available")
}