2025-09-29 17:24:59 +02:00
|
|
|
|
// ABOUTME: CLI command for declarative deployment of EdgeConnect applications from YAML configuration
|
|
|
|
|
|
// ABOUTME: Integrates config parser, deployment planner, and resource manager for complete deployment workflow
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"fmt"
|
2025-10-01 10:49:15 +02:00
|
|
|
|
"log"
|
2025-09-29 17:24:59 +02:00
|
|
|
|
"os"
|
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
2025-10-20 13:57:57 +02:00
|
|
|
|
applyv1 "edp.buildth.ing/DevFW-CICD/edge-connect-client/internal/apply/v1"
|
|
|
|
|
|
applyv2 "edp.buildth.ing/DevFW-CICD/edge-connect-client/internal/apply/v2"
|
2025-09-29 17:35:34 +02:00
|
|
|
|
"edp.buildth.ing/DevFW-CICD/edge-connect-client/internal/config"
|
2025-09-29 17:24:59 +02:00
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
2025-10-02 14:52:40 +02:00
|
|
|
|
configFile string
|
|
|
|
|
|
dryRun bool
|
|
|
|
|
|
autoApprove bool
|
2025-09-29 17:24:59 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var applyCmd = &cobra.Command{
|
|
|
|
|
|
Use: "apply",
|
|
|
|
|
|
Short: "Deploy EdgeConnect applications from configuration files",
|
|
|
|
|
|
Long: `Deploy EdgeConnect applications and their instances from YAML configuration files.
|
|
|
|
|
|
This command reads a configuration file, analyzes the current state, and applies
|
|
|
|
|
|
the necessary changes to deploy your applications across multiple cloudlets.`,
|
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
|
if configFile == "" {
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Error: configuration file is required\n")
|
|
|
|
|
|
cmd.Usage()
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-02 14:52:40 +02:00
|
|
|
|
if err := runApply(configFile, dryRun, autoApprove); err != nil {
|
2025-09-29 17:24:59 +02:00
|
|
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-02 14:52:40 +02:00
|
|
|
|
func runApply(configPath string, isDryRun bool, autoApprove bool) error {
|
2025-09-29 17:24:59 +02:00
|
|
|
|
// Step 1: Validate and resolve config file path
|
|
|
|
|
|
absPath, err := filepath.Abs(configPath)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to resolve config file path: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if _, err := os.Stat(absPath); os.IsNotExist(err) {
|
|
|
|
|
|
return fmt.Errorf("configuration file not found: %s", absPath)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("📄 Loading configuration from: %s\n", absPath)
|
|
|
|
|
|
|
|
|
|
|
|
// Step 2: Parse and validate configuration
|
|
|
|
|
|
parser := config.NewParser()
|
2025-10-01 10:49:15 +02:00
|
|
|
|
cfg, manifestContent, err := parser.ParseFile(absPath)
|
2025-09-29 17:24:59 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to parse configuration: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := parser.Validate(cfg); err != nil {
|
|
|
|
|
|
return fmt.Errorf("configuration validation failed: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("✅ Configuration loaded successfully: %s\n", cfg.Metadata.Name)
|
|
|
|
|
|
|
2025-10-20 13:57:57 +02:00
|
|
|
|
// Step 3: Determine API version and create appropriate client
|
2025-10-20 13:49:09 +02:00
|
|
|
|
apiVersion := getAPIVersion()
|
2025-10-20 13:57:57 +02:00
|
|
|
|
|
|
|
|
|
|
// Step 4-6: Execute deployment based on API version
|
2025-10-20 13:49:09 +02:00
|
|
|
|
if apiVersion == "v1" {
|
2025-10-20 13:57:57 +02:00
|
|
|
|
return runApplyV1(cfg, manifestContent, isDryRun, autoApprove)
|
|
|
|
|
|
}
|
|
|
|
|
|
return runApplyV2(cfg, manifestContent, isDryRun, autoApprove)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func runApplyV1(cfg *config.EdgeConnectConfig, manifestContent string, isDryRun bool, autoApprove bool) error {
|
|
|
|
|
|
// Create v1 client
|
|
|
|
|
|
client := newSDKClientV1()
|
|
|
|
|
|
|
|
|
|
|
|
// Create deployment planner
|
|
|
|
|
|
planner := applyv1.NewPlanner(client)
|
|
|
|
|
|
|
|
|
|
|
|
// Generate deployment plan
|
|
|
|
|
|
fmt.Println("🔍 Analyzing current state and generating deployment plan...")
|
|
|
|
|
|
|
|
|
|
|
|
planOptions := applyv1.DefaultPlanOptions()
|
|
|
|
|
|
planOptions.DryRun = isDryRun
|
|
|
|
|
|
|
|
|
|
|
|
result, err := planner.PlanWithOptions(context.Background(), cfg, planOptions)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to generate deployment plan: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Display plan summary
|
|
|
|
|
|
fmt.Println("\n📋 Deployment Plan:")
|
|
|
|
|
|
fmt.Println(strings.Repeat("=", 50))
|
|
|
|
|
|
fmt.Println(result.Plan.Summary)
|
|
|
|
|
|
fmt.Println(strings.Repeat("=", 50))
|
|
|
|
|
|
|
|
|
|
|
|
// Display warnings if any
|
|
|
|
|
|
if len(result.Warnings) > 0 {
|
|
|
|
|
|
fmt.Println("\n⚠️ Warnings:")
|
|
|
|
|
|
for _, warning := range result.Warnings {
|
|
|
|
|
|
fmt.Printf(" • %s\n", warning)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If dry-run, stop here
|
|
|
|
|
|
if isDryRun {
|
|
|
|
|
|
fmt.Println("\n🔍 Dry-run complete. No changes were made.")
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Confirm deployment
|
|
|
|
|
|
if result.Plan.TotalActions == 0 {
|
|
|
|
|
|
fmt.Println("\n✅ No changes needed. Resources are already in desired state.")
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("\nThis will perform %d actions. Estimated time: %v\n",
|
|
|
|
|
|
result.Plan.TotalActions, result.Plan.EstimatedDuration)
|
|
|
|
|
|
|
|
|
|
|
|
if !autoApprove && !confirmDeployment() {
|
|
|
|
|
|
fmt.Println("Deployment cancelled.")
|
|
|
|
|
|
return nil
|
2025-10-20 13:49:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-20 13:57:57 +02:00
|
|
|
|
// Execute deployment
|
|
|
|
|
|
fmt.Println("\n🚀 Starting deployment...")
|
|
|
|
|
|
|
|
|
|
|
|
manager := applyv1.NewResourceManager(client, applyv1.WithLogger(log.Default()))
|
|
|
|
|
|
deployResult, err := manager.ApplyDeployment(context.Background(), result.Plan, cfg, manifestContent)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("deployment failed: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Display results
|
|
|
|
|
|
return displayDeploymentResults(deployResult)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func runApplyV2(cfg *config.EdgeConnectConfig, manifestContent string, isDryRun bool, autoApprove bool) error {
|
|
|
|
|
|
// Create v2 client
|
2025-10-20 13:41:50 +02:00
|
|
|
|
client := newSDKClientV2()
|
2025-09-29 17:24:59 +02:00
|
|
|
|
|
2025-10-20 13:57:57 +02:00
|
|
|
|
// Create deployment planner
|
|
|
|
|
|
planner := applyv2.NewPlanner(client)
|
2025-09-29 17:24:59 +02:00
|
|
|
|
|
2025-10-20 13:57:57 +02:00
|
|
|
|
// Generate deployment plan
|
2025-09-29 17:24:59 +02:00
|
|
|
|
fmt.Println("🔍 Analyzing current state and generating deployment plan...")
|
|
|
|
|
|
|
2025-10-20 13:57:57 +02:00
|
|
|
|
planOptions := applyv2.DefaultPlanOptions()
|
2025-09-29 17:24:59 +02:00
|
|
|
|
planOptions.DryRun = isDryRun
|
|
|
|
|
|
|
|
|
|
|
|
result, err := planner.PlanWithOptions(context.Background(), cfg, planOptions)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to generate deployment plan: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-20 13:57:57 +02:00
|
|
|
|
// Display plan summary
|
2025-09-29 17:24:59 +02:00
|
|
|
|
fmt.Println("\n📋 Deployment Plan:")
|
|
|
|
|
|
fmt.Println(strings.Repeat("=", 50))
|
|
|
|
|
|
fmt.Println(result.Plan.Summary)
|
|
|
|
|
|
fmt.Println(strings.Repeat("=", 50))
|
|
|
|
|
|
|
|
|
|
|
|
// Display warnings if any
|
|
|
|
|
|
if len(result.Warnings) > 0 {
|
|
|
|
|
|
fmt.Println("\n⚠️ Warnings:")
|
|
|
|
|
|
for _, warning := range result.Warnings {
|
|
|
|
|
|
fmt.Printf(" • %s\n", warning)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-20 13:57:57 +02:00
|
|
|
|
// If dry-run, stop here
|
2025-09-29 17:24:59 +02:00
|
|
|
|
if isDryRun {
|
|
|
|
|
|
fmt.Println("\n🔍 Dry-run complete. No changes were made.")
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-20 13:57:57 +02:00
|
|
|
|
// Confirm deployment
|
2025-09-29 17:24:59 +02:00
|
|
|
|
if result.Plan.TotalActions == 0 {
|
|
|
|
|
|
fmt.Println("\n✅ No changes needed. Resources are already in desired state.")
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("\nThis will perform %d actions. Estimated time: %v\n",
|
|
|
|
|
|
result.Plan.TotalActions, result.Plan.EstimatedDuration)
|
|
|
|
|
|
|
2025-10-02 14:52:40 +02:00
|
|
|
|
if !autoApprove && !confirmDeployment() {
|
2025-09-29 17:24:59 +02:00
|
|
|
|
fmt.Println("Deployment cancelled.")
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-20 13:57:57 +02:00
|
|
|
|
// Execute deployment
|
2025-09-29 17:24:59 +02:00
|
|
|
|
fmt.Println("\n🚀 Starting deployment...")
|
|
|
|
|
|
|
2025-10-20 13:57:57 +02:00
|
|
|
|
manager := applyv2.NewResourceManager(client, applyv2.WithLogger(log.Default()))
|
2025-10-01 10:49:15 +02:00
|
|
|
|
deployResult, err := manager.ApplyDeployment(context.Background(), result.Plan, cfg, manifestContent)
|
2025-09-29 17:24:59 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("deployment failed: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-20 13:57:57 +02:00
|
|
|
|
// Display results
|
|
|
|
|
|
return displayDeploymentResults(deployResult)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type deploymentResult interface {
|
|
|
|
|
|
IsSuccess() bool
|
|
|
|
|
|
GetDuration() string
|
|
|
|
|
|
GetCompletedActions() []actionResult
|
|
|
|
|
|
GetFailedActions() []actionResult
|
|
|
|
|
|
GetError() error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type actionResult interface {
|
|
|
|
|
|
GetType() string
|
|
|
|
|
|
GetTarget() string
|
|
|
|
|
|
GetError() error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func displayDeploymentResults(result interface{}) error {
|
|
|
|
|
|
// Use reflection or type assertion to handle both v1 and v2 result types
|
|
|
|
|
|
// For now, we'll use a simple approach that works with both
|
|
|
|
|
|
switch r := result.(type) {
|
|
|
|
|
|
case *applyv1.ExecutionResult:
|
|
|
|
|
|
return displayDeploymentResultsV1(r)
|
|
|
|
|
|
case *applyv2.ExecutionResult:
|
|
|
|
|
|
return displayDeploymentResultsV2(r)
|
|
|
|
|
|
default:
|
|
|
|
|
|
return fmt.Errorf("unknown deployment result type")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func displayDeploymentResultsV1(deployResult *applyv1.ExecutionResult) error {
|
2025-09-29 17:24:59 +02:00
|
|
|
|
if deployResult.Success {
|
|
|
|
|
|
fmt.Printf("\n✅ Deployment completed successfully in %v\n", deployResult.Duration)
|
|
|
|
|
|
if len(deployResult.CompletedActions) > 0 {
|
|
|
|
|
|
fmt.Println("\nCompleted actions:")
|
|
|
|
|
|
for _, action := range deployResult.CompletedActions {
|
|
|
|
|
|
fmt.Printf(" ✅ %s %s\n", action.Type, action.Target)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
fmt.Printf("\n❌ Deployment failed after %v\n", deployResult.Duration)
|
|
|
|
|
|
if deployResult.Error != nil {
|
|
|
|
|
|
fmt.Printf("Error: %v\n", deployResult.Error)
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(deployResult.FailedActions) > 0 {
|
|
|
|
|
|
fmt.Println("\nFailed actions:")
|
|
|
|
|
|
for _, action := range deployResult.FailedActions {
|
|
|
|
|
|
fmt.Printf(" ❌ %s %s: %v\n", action.Type, action.Target, action.Error)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return fmt.Errorf("deployment failed with %d failed actions", len(deployResult.FailedActions))
|
|
|
|
|
|
}
|
2025-10-20 13:57:57 +02:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-09-29 17:24:59 +02:00
|
|
|
|
|
2025-10-20 13:57:57 +02:00
|
|
|
|
func displayDeploymentResultsV2(deployResult *applyv2.ExecutionResult) error {
|
|
|
|
|
|
if deployResult.Success {
|
|
|
|
|
|
fmt.Printf("\n✅ Deployment completed successfully in %v\n", deployResult.Duration)
|
|
|
|
|
|
if len(deployResult.CompletedActions) > 0 {
|
|
|
|
|
|
fmt.Println("\nCompleted actions:")
|
|
|
|
|
|
for _, action := range deployResult.CompletedActions {
|
|
|
|
|
|
fmt.Printf(" ✅ %s %s\n", action.Type, action.Target)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
fmt.Printf("\n❌ Deployment failed after %v\n", deployResult.Duration)
|
|
|
|
|
|
if deployResult.Error != nil {
|
|
|
|
|
|
fmt.Printf("Error: %v\n", deployResult.Error)
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(deployResult.FailedActions) > 0 {
|
|
|
|
|
|
fmt.Println("\nFailed actions:")
|
|
|
|
|
|
for _, action := range deployResult.FailedActions {
|
|
|
|
|
|
fmt.Printf(" ❌ %s %s: %v\n", action.Type, action.Target, action.Error)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return fmt.Errorf("deployment failed with %d failed actions", len(deployResult.FailedActions))
|
|
|
|
|
|
}
|
2025-09-29 17:24:59 +02:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func confirmDeployment() bool {
|
|
|
|
|
|
fmt.Print("Do you want to proceed? (yes/no): ")
|
|
|
|
|
|
var response string
|
|
|
|
|
|
fmt.Scanln(&response)
|
|
|
|
|
|
|
|
|
|
|
|
switch response {
|
|
|
|
|
|
case "yes", "y", "YES", "Y":
|
|
|
|
|
|
return true
|
|
|
|
|
|
default:
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
rootCmd.AddCommand(applyCmd)
|
|
|
|
|
|
|
|
|
|
|
|
applyCmd.Flags().StringVarP(&configFile, "file", "f", "", "configuration file path (required)")
|
|
|
|
|
|
applyCmd.Flags().BoolVar(&dryRun, "dry-run", false, "preview changes without applying them")
|
2025-10-02 14:52:40 +02:00
|
|
|
|
applyCmd.Flags().BoolVar(&autoApprove, "auto-approve", false, "automatically approve the deployment plan")
|
2025-09-29 17:24:59 +02:00
|
|
|
|
|
|
|
|
|
|
applyCmd.MarkFlagRequired("file")
|
|
|
|
|
|
}
|