provisioning/scripts/docker-generate-compose.nu

131 lines
3.6 KiB
Text
Raw Normal View History

#!/usr/bin/env nu
# Docker Compose Build File Generator
# Generates docker-compose.build.yml from Nickel template for parallel BuildKit builds
#
# Usage:
# docker-generate-compose.nu # Generate with default registry (localhost:5000)
# docker-generate-compose.nu --registry ghcr.io # Generate with custom registry
#
# Environment:
# PROVISIONING_ROOT - Optional: override project root detection
#
# Patterns:
# - Result pattern (NO try-catch)
# - Pipeline let binding (Nushell 0.110.0+)
# - Guards for validation
# Search up directory tree for provisioning root (helper)
def search-up-for-provisioning [dir: string]: nothing -> string {
if ($"($dir)/schemas/platform" | path exists) {
$dir
} else {
let parent = ($dir | path dirname)
if $parent == $dir {
""
} else {
search-up-for-provisioning $parent
}
}
}
# Detect provisioning project root
# Follows same pattern as platform-generate-manifests.nu
def get-provisioning-root []: nothing -> string {
# 1. Check PROVISIONING environment variable (standard)
if ($env.PROVISIONING? != null) {
return $env.PROVISIONING
}
# 2. Check PROVISIONING_ROOT (alternative)
if ($env.PROVISIONING_ROOT? != null) {
return $env.PROVISIONING_ROOT
}
# 3. Check if we're in provisioning/ directory
let cwd = (pwd)
if ($cwd | path basename) == "provisioning" {
# We're inside provisioning/, use current dir
if ("schemas/platform" | path exists) {
return "."
}
}
# 4. Check if provisioning/ exists as subdirectory
if ("provisioning/schemas/platform" | path exists) {
return "provisioning"
}
# 5. Search up the directory tree
let found = (search-up-for-provisioning $cwd)
if $found != "" {
return $found
}
# 6. Fallback to current directory
pwd
}
# Generate docker-compose.build.yml from Nickel template
def main [
--registry: string = "localhost:5000", # Docker registry for image tags
--output: string = "", # Output file path (default: docker-compose.build.yml)
]: nothing -> record<ok: bool, err: string, path: string> {
# Detect provisioning root
let prov_root = (get-provisioning-root)
# Construct paths
let template_file = $"($prov_root)/schemas/platform/templates/docker/docker-compose.build.yml.ncl"
let output_file = if ($output | is-empty) {
$"($prov_root)/docker-compose.build.yml"
} else {
$output
}
# Guard: Check template file exists
if not ($template_file | path exists) {
return {
ok: false,
err: $"Template file not found: ($template_file)",
path: ""
}
}
# Execute nickel export
# Note: Registry customization must be done by editing the template or the generated file
let result = (
nickel export
--format yaml
$template_file
| complete
)
if $result.exit_code != 0 {
return {
ok: false,
err: $"Nickel export failed: ($result.stderr)",
path: ""
}
}
let compose_yaml = $result.stdout
# Check if generation succeeded
if ($compose_yaml | is-empty) {
return {
ok: false,
err: "Generated docker-compose.yml is empty",
path: ""
}
}
# Write docker-compose.build.yml
$compose_yaml | save --force $output_file
# Return success
{
ok: true,
err: "",
path: $output_file
}
}