Skip to content

Cloud Deployment Tool Example

This example demonstrates a sophisticated cloud deployment tool using Argos with domains, fragment inheritance, environment-specific configurations, and security best practices.

Complete Implementation

import onl.ycode.argos.*
import java.io.File

enum class CloudProvider { AWS, GCP, AZURE, DIGITAL_OCEAN }
enum class Environment { DEVELOPMENT, STAGING, PRODUCTION }
enum class DeploymentStrategy { ROLLING, BLUE_GREEN, CANARY }
enum class LogLevel { DEBUG, INFO, WARN, ERROR }

class CloudDeployTool : Arguments(
    appName = "cloud-deploy",
    appDescription = "Enterprise cloud deployment tool with multi-provider support",
    aggregateErrors = true,
    maxAggregatedErrors = 15,
    didYouMean = true
) {
    // Global configuration
    val configFile by option("--config", "-c")
        .help("Deployment configuration file")
        .fromEnv("DEPLOY_CONFIG")
        .validate("Config file must exist") { File(it).exists() }

    val logLevel by option("--log-level")
        .enum<LogLevel>()
        .default(LogLevel.INFO)
        .fromEnv("LOG_LEVEL")
        .help("Logging verbosity level")

    val dryRun by option("--dry-run")
        .bool()
        .help("Show what would be deployed without making changes")

    val verboseOutput by option("--verbose", "-v")
        .bool()
        .help("Enable verbose output for all operations")

    // Authentication fragment - shared across all operations
    val authenticationFragment by domain(fragment = true)
        .exactlyOne(::cloudApiKey, ::serviceAccountKey, ::userCredentials, ::assumeRole)
        .requireIfValue(::cloudApiKey, ::region) { it != null }
        .requireIfValue(::assumeRole, ::roleArn) { it == true }
        .requireIfValue(::serviceAccountKey, ::serviceAccountFile) { it == true }

    // Environment validation fragment
    val environmentFragment by domain(fragment = true)
        .required(::targetEnvironment)
        .requireIfValue(::targetEnvironment, ::productionApproval) { it == Environment.PRODUCTION }
        .requireIfValue(::targetEnvironment, ::stagingValidation) { it == Environment.STAGING }

    // Security compliance fragment
    val securityFragment by domain(fragment = true)
        .required(::complianceLevel)
        .requireIfValue(::complianceLevel, ::encryptionKey) { it in listOf("high", "critical") }
        .requireIfValue(::complianceLevel, ::auditLog) { it in listOf("medium", "high", "critical") }
        .conflicts(::skipSecurityScan, ::bypassCompliance)

    // Resource management fragment
    val resourceFragment by domain(fragment = true)
        .required(::resourceGroup)
        .atMostOne(::createResources, ::useExistingResources)
        .requireIfAnyPresent(::createResources, ::resourceTemplate)
        .requireIfValue(::targetEnvironment, ::resourceQuota) { it == Environment.PRODUCTION }

    // Deploy domain - primary deployment functionality
    val deployDomain by domain("deploy")
        .label("Deploy Application")
        .help("Deploy application to cloud infrastructure")
        .aliases("up", "apply")
        .inherits(::authenticationFragment, ::environmentFragment, ::securityFragment, ::resourceFragment)
        .required(::applicationManifest)
        .exactlyOne(::deploymentStrategy)
        .requireIfValue(::deploymentStrategy, ::canaryConfig) { it == DeploymentStrategy.CANARY }
        .requireIfValue(::deploymentStrategy, ::blueGreenConfig) { it == DeploymentStrategy.BLUE_GREEN }
        .conflicts(::dryRun, ::autoApprove)
        .conflicts(::forceDeployment, ::safetyChecks)

    // Rollback domain
    val rollbackDomain by domain("rollback")
        .label("Rollback Deployment")
        .help("Rollback to a previous deployment version")
        .aliases("revert")
        .inherits(::authenticationFragment, ::environmentFragment, ::securityFragment)
        .required(::rollbackTarget)
        .exactlyOne(::rollbackVersion, ::rollbackToLatest, ::rollbackToPrevious)
        .requireIfValue(::targetEnvironment, ::emergencyApproval) { it == Environment.PRODUCTION }
        .conflicts(::dryRun, ::skipRollbackValidation)

    // Scale domain
    val scaleDomain by domain("scale")
        .label("Scale Application")
        .help("Scale application instances up or down")
        .inherits(::authenticationFragment, ::environmentFragment, ::resourceFragment)
        .required(::serviceName)
        .exactlyOne(::targetReplicas, ::autoscalingConfig)
        .requireIfAnyPresent(::autoscalingConfig, ::minReplicas, ::maxReplicas)
        .conflicts(::manualScaling, ::autoscalingConfig)

    // Monitor domain
    val monitorDomain by domain("monitor")
        .label("Monitor Deployment")
        .help("Monitor deployment status and health")
        .aliases("watch", "status")
        .inherits(::authenticationFragment, ::environmentFragment)
        .atLeastOne(::monitorHealth, ::monitorMetrics, ::monitorLogs)
        .requireIfAnyPresent(::monitorMetrics, ::metricsConfig)
        .requireIfAnyPresent(::monitorLogs, ::logAggregator)

    // Destroy domain
    val destroyDomain by domain("destroy")
        .label("Destroy Infrastructure")
        .help("Destroy cloud infrastructure and resources")
        .aliases("down", "delete")
        .inherits(::authenticationFragment, ::environmentFragment, ::securityFragment)
        .required(::confirmDestroy)
        .requireIfValue(::targetEnvironment, ::destructionApproval) { it == Environment.PRODUCTION }
        .conflicts(::dryRun, ::keepData)
        .conflicts(::forceDestroy, ::safeDestroy)

    // Authentication options
    val cloudApiKey by option("--api-key")
        .fromEnv("CLOUD_API_KEY")
        .hidden()
        .help("Cloud provider API key")

    val serviceAccountKey by option("--service-account")
        .bool()
        .help("Use service account authentication")

    val serviceAccountFile by option("--service-account-file")
        .fromEnv("SERVICE_ACCOUNT_FILE")
        .validate("Service account file must exist") { File(it).exists() }
        .help("Service account key file path")

    val userCredentials by option("--user-credentials")
        .bool()
        .help("Use interactive user credentials")

    val assumeRole by option("--assume-role")
        .bool()
        .help("Assume IAM role for deployment")

    val roleArn by option("--role-arn")
        .fromEnv("ASSUME_ROLE_ARN")
        .help("ARN of the role to assume")

    // Cloud provider configuration
    val cloudProvider by option("--provider")
        .enum<CloudProvider>()
        .default(CloudProvider.AWS)
        .fromEnv("CLOUD_PROVIDER")
        .help("Cloud provider platform")

    val region by option("--region")
        .fromEnv("CLOUD_REGION")
        .help("Cloud provider region")

    val availabilityZones by option("--availability-zone", "--az")
        .list()
        .fromEnv("AVAILABILITY_ZONES")
        .help("Specific availability zones for deployment")

    // Environment configuration
    val targetEnvironment by option("--environment", "--env")
        .enum<Environment>()
        .fromEnv("TARGET_ENVIRONMENT")
        .help("Target deployment environment")

    val productionApproval by option("--production-approval")
        .fromEnv("PRODUCTION_APPROVAL_TOKEN")
        .hidden()
        .help("Approval token required for production deployments")

    val stagingValidation by option("--staging-validation")
        .bool()
        .default(true)
        .help("Require validation checks for staging deployments")

    val emergencyApproval by option("--emergency-approval")
        .fromEnv("EMERGENCY_APPROVAL_TOKEN")
        .hidden()
        .help("Emergency approval token for production rollbacks")

    val destructionApproval by option("--destruction-approval")
        .fromEnv("DESTRUCTION_APPROVAL_TOKEN")
        .hidden()
        .help("Approval token required for production infrastructure destruction")

    // Security and compliance
    val complianceLevel by option("--compliance-level")
        .oneOf("basic", "medium", "high", "critical")
        .default("medium")
        .fromEnv("COMPLIANCE_LEVEL")
        .help("Security compliance level")

    val encryptionKey by option("--encryption-key")
        .fromEnv("ENCRYPTION_KEY")
        .password()
        .hidden()
        .help("Encryption key for sensitive data")

    val auditLog by option("--audit-log")
        .fromEnv("AUDIT_LOG_PATH")
        .help("Path for audit log file")

    val skipSecurityScan by option("--skip-security-scan")
        .bool()
        .help("Skip security vulnerability scanning")

    val bypassCompliance by option("--bypass-compliance")
        .bool()
        .help("Bypass compliance checks (dangerous)")

    // Resource management
    val resourceGroup by option("--resource-group")
        .fromEnv("RESOURCE_GROUP")
        .help("Target resource group for deployment")

    val createResources by option("--create-resources")
        .bool()
        .help("Create new resources as needed")

    val useExistingResources by option("--use-existing")
        .bool()
        .help("Use only existing resources")

    val resourceTemplate by option("--resource-template")
        .validate("Template file must exist") { File(it).exists() }
        .help("Resource template file for infrastructure creation")

    val resourceQuota by option("--resource-quota")
        .help("Resource quota configuration for production")

    // Application deployment
    val applicationManifest by option("--manifest", "--app-manifest")
        .validate("Manifest file must exist") { File(it).exists() }
        .help("Application deployment manifest")

    val deploymentStrategy by option("--strategy")
        .enum<DeploymentStrategy>()
        .default(DeploymentStrategy.ROLLING)
        .help("Deployment strategy to use")

    val canaryConfig by option("--canary-config")
        .validate("Canary config must exist") { File(it).exists() }
        .help("Canary deployment configuration file")

    val blueGreenConfig by option("--blue-green-config")
        .validate("Blue-green config must exist") { File(it).exists() }
        .help("Blue-green deployment configuration file")

    val autoApprove by option("--auto-approve")
        .bool()
        .help("Automatically approve deployment steps")

    val forceDeployment by option("--force")
        .bool()
        .help("Force deployment even with warnings")

    val safetyChecks by option("--safety-checks")
        .bool()
        .default(true)
        .help("Enable comprehensive safety checks")

    // Rollback configuration
    val rollbackTarget by option("--rollback-target")
        .help("Target service/application for rollback")

    val rollbackVersion by option("--rollback-version")
        .help("Specific version to rollback to")

    val rollbackToLatest by option("--rollback-to-latest")
        .bool()
        .help("Rollback to the latest stable version")

    val rollbackToPrevious by option("--rollback-to-previous")
        .bool()
        .help("Rollback to the previous version")

    val skipRollbackValidation by option("--skip-rollback-validation")
        .bool()
        .help("Skip validation during rollback")

    // Scaling configuration
    val serviceName by option("--service-name")
        .help("Name of the service to scale")

    val targetReplicas by option("--replicas")
        .int()
        .validate("Replicas must be positive") { it > 0 }
        .validate("Too many replicas") { it <= 1000 }
        .help("Target number of replicas")

    val autoscalingConfig by option("--autoscaling-config")
        .validate("Autoscaling config must exist") { File(it).exists() }
        .help("Autoscaling configuration file")

    val minReplicas by option("--min-replicas")
        .int()
        .validate("Min replicas must be positive") { it > 0 }
        .help("Minimum number of replicas for autoscaling")

    val maxReplicas by option("--max-replicas")
        .int()
        .validate("Max replicas must be positive") { it > 0 }
        .help("Maximum number of replicas for autoscaling")

    val manualScaling by option("--manual-scaling")
        .bool()
        .help("Use manual scaling mode")

    // Monitoring configuration
    val monitorHealth by option("--monitor-health")
        .bool()
        .help("Monitor application health")

    val monitorMetrics by option("--monitor-metrics")
        .bool()
        .help("Monitor application metrics")

    val monitorLogs by option("--monitor-logs")
        .bool()
        .help("Monitor application logs")

    val metricsConfig by option("--metrics-config")
        .validate("Metrics config must exist") { File(it).exists() }
        .help("Metrics monitoring configuration")

    val logAggregator by option("--log-aggregator")
        .oneOf("cloudwatch", "stackdriver", "elk", "splunk")
        .help("Log aggregation service")

    val monitoringDuration by option("--monitoring-duration")
        .int()
        .default(300)
        .validate("Duration must be positive") { it > 0 }
        .help("Monitoring duration in seconds")

    // Destruction configuration
    val confirmDestroy by option("--confirm-destroy")
        .help("Confirmation phrase for infrastructure destruction")
        .validate("Must type exact confirmation") { it == "DELETE_ALL_RESOURCES" }

    val keepData by option("--keep-data")
        .bool()
        .help("Preserve data volumes during destruction")

    val forceDestroy by option("--force-destroy")
        .bool()
        .help("Force destruction without safety checks")

    val safeDestroy by option("--safe-destroy")
        .bool()
        .default(true)
        .help("Use safe destruction with backups")

    val backupBeforeDestroy by option("--backup-before-destroy")
        .bool()
        .default(true)
        .help("Create backup before destroying resources")

    // Built-in options
    val help by help()
    val version by version("3.2.1")
}

fun main(args: Array<String>) {
    val deployTool = CloudDeployTool()
    deployTool.parse(args,
        onError = { error, _ ->
            System.err.println("Error: ${error.message}")
            deployTool.printUsage()
        }
    ) ?: return

    // Configure logging
    configureLogging(deployTool.logLevel, deployTool.verboseOutput)

    // Validate cloud provider configuration
    validateCloudConfiguration(deployTool)

    // Execute the appropriate domain
    when {
        deployTool.deployDomain -> executeDeploy(deployTool)
        deployTool.rollbackDomain -> executeRollback(deployTool)
        deployTool.scaleDomain -> executeScale(deployTool)
        deployTool.monitorDomain -> executeMonitor(deployTool)
        deployTool.destroyDomain -> executeDestroy(deployTool)
        else -> {
            println("No command specified. Use --help for available commands.")
            showQuickStart()
        }
    }
}

fun configureLogging(level: LogLevel, verbose: Boolean) {
    val effectiveLevel = if (verbose && level == LogLevel.INFO) LogLevel.DEBUG else level
    Logger.configure(effectiveLevel)
    Logger.info("Cloud deployment tool starting...")
}

fun validateCloudConfiguration(config: CloudDeployTool) {
    // Validate cloud provider specific requirements
    when (config.cloudProvider) {
        CloudProvider.AWS -> {
            if (config.region == null) {
                throw IllegalArgumentException("AWS deployments require --region")
            }
        }
        CloudProvider.GCP -> {
            if (config.serviceAccountKey && config.serviceAccountFile == null) {
                throw IllegalArgumentException("GCP service account requires --service-account-file")
            }
        }
        CloudProvider.AZURE -> {
            if (config.resourceGroup == null) {
                throw IllegalArgumentException("Azure deployments require --resource-group")
            }
        }
        CloudProvider.DIGITAL_OCEAN -> {
            if (config.cloudApiKey == null) {
                throw IllegalArgumentException("DigitalOcean deployments require --api-key")
            }
        }
    }

    Logger.debug("Cloud configuration validated for ${config.cloudProvider}")
}

fun executeDeploy(config: CloudDeployTool) {
    Logger.info("Starting deployment to ${config.targetEnvironment} environment...")

    val deploymentContext = DeploymentContext(
        provider = config.cloudProvider,
        environment = config.targetEnvironment,
        region = config.region,
        resourceGroup = config.resourceGroup,
        manifest = config.applicationManifest!!,
        strategy = config.deploymentStrategy,
        dryRun = config.dryRun,
        autoApprove = config.autoApprove,
        safetyChecks = config.safetyChecks
    )

    // Pre-deployment validation
    Logger.info("Running pre-deployment validation...")
    val validationResult = validateDeployment(deploymentContext)
    if (!validationResult.success) {
        Logger.error("Pre-deployment validation failed: ${validationResult.errors.joinToString()}")
        kotlin.system.exitProcess(1)
    }

    // Security scanning
    if (!config.skipSecurityScan) {
        Logger.info("Running security scan...")
        val securityResult = runSecurityScan(deploymentContext)
        if (!securityResult.passed) {
            Logger.error("Security scan failed: ${securityResult.issues.joinToString()}")
            if (config.complianceLevel in listOf("high", "critical")) {
                kotlin.system.exitProcess(1)
            }
        }
    }

    // Execute deployment
    if (config.dryRun) {
        Logger.info("DRY RUN: Would deploy using ${config.deploymentStrategy} strategy")
        simulateDeployment(deploymentContext)
    } else {
        Logger.info("Executing deployment...")
        val deployResult = performDeployment(deploymentContext)

        if (deployResult.success) {
            Logger.info("Deployment completed successfully!")
            Logger.info("Application URL: ${deployResult.applicationUrl}")
            Logger.info("Deployment ID: ${deployResult.deploymentId}")

            // Post-deployment monitoring
            if (config.monitorHealth) {
                Logger.info("Starting post-deployment health monitoring...")
                monitorPostDeployment(deployResult.deploymentId!!)
            }
        } else {
            Logger.error("Deployment failed: ${deployResult.error}")

            // Automatic rollback on failure
            if (config.targetEnvironment == Environment.PRODUCTION) {
                Logger.info("Initiating automatic rollback...")
                performAutomaticRollback(deploymentContext)
            }

            kotlin.system.exitProcess(1)
        }
    }
}

fun executeRollback(config: CloudDeployTool) {
    Logger.info("Starting rollback process...")

    val rollbackContext = RollbackContext(
        provider = config.cloudProvider,
        environment = config.targetEnvironment,
        target = config.rollbackTarget!!,
        version = config.rollbackVersion,
        toLatest = config.rollbackToLatest,
        toPrevious = config.rollbackToPrevious,
        skipValidation = config.skipRollbackValidation
    )

    // Rollback validation
    if (!config.skipRollbackValidation) {
        Logger.info("Validating rollback target...")
        val validationResult = validateRollbackTarget(rollbackContext)
        if (!validationResult.isValid) {
            Logger.error("Rollback validation failed: ${validationResult.error}")
            kotlin.system.exitProcess(1)
        }
    }

    // Execute rollback
    if (config.dryRun) {
        Logger.info("DRY RUN: Would rollback ${config.rollbackTarget}")
        simulateRollback(rollbackContext)
    } else {
        Logger.info("Executing rollback...")
        val rollbackResult = performRollback(rollbackContext)

        if (rollbackResult.success) {
            Logger.info("Rollback completed successfully!")
            Logger.info("Restored to version: ${rollbackResult.restoredVersion}")
        } else {
            Logger.error("Rollback failed: ${rollbackResult.error}")
            kotlin.system.exitProcess(1)
        }
    }
}

fun executeScale(config: CloudDeployTool) {
    Logger.info("Starting scaling operation...")

    val scaleContext = ScaleContext(
        provider = config.cloudProvider,
        environment = config.targetEnvironment,
        serviceName = config.serviceName!!,
        targetReplicas = config.targetReplicas,
        autoscalingConfig = config.autoscalingConfig,
        minReplicas = config.minReplicas,
        maxReplicas = config.maxReplicas
    )

    // Validate scaling parameters
    if (config.autoscalingConfig != null) {
        if (config.minReplicas != null && config.maxReplicas != null) {
            if (config.minReplicas!! > config.maxReplicas!!) {
                Logger.error("Min replicas cannot be greater than max replicas")
                kotlin.system.exitProcess(1)
            }
        }
    }

    // Execute scaling
    if (config.dryRun) {
        Logger.info("DRY RUN: Would scale ${config.serviceName}")
        simulateScaling(scaleContext)
    } else {
        Logger.info("Executing scaling operation...")
        val scaleResult = performScaling(scaleContext)

        if (scaleResult.success) {
            Logger.info("Scaling completed successfully!")
            Logger.info("Current replicas: ${scaleResult.currentReplicas}")
        } else {
            Logger.error("Scaling failed: ${scaleResult.error}")
            kotlin.system.exitProcess(1)
        }
    }
}

fun executeMonitor(config: CloudDeployTool) {
    Logger.info("Starting monitoring session...")

    val monitorContext = MonitorContext(
        provider = config.cloudProvider,
        environment = config.targetEnvironment,
        health = config.monitorHealth,
        metrics = config.monitorMetrics,
        logs = config.monitorLogs,
        duration = config.monitoringDuration,
        metricsConfig = config.metricsConfig,
        logAggregator = config.logAggregator
    )

    Logger.info("Monitoring for ${config.monitoringDuration} seconds...")
    val monitorResult = startMonitoring(monitorContext)

    Logger.info("Monitoring session completed")
    Logger.info("Health status: ${monitorResult.healthStatus}")
    if (config.monitorMetrics) {
        Logger.info("Average response time: ${monitorResult.avgResponseTime}ms")
        Logger.info("Error rate: ${monitorResult.errorRate}%")
    }
}

fun executeDestroy(config: CloudDeployTool) {
    Logger.info("Starting infrastructure destruction...")

    val destroyContext = DestroyContext(
        provider = config.cloudProvider,
        environment = config.targetEnvironment,
        resourceGroup = config.resourceGroup,
        keepData = config.keepData,
        forceDestroy = config.forceDestroy,
        safeDestroy = config.safeDestroy,
        backupBeforeDestroy = config.backupBeforeDestroy
    )

    // Create backup before destruction
    if (config.backupBeforeDestroy && !config.keepData) {
        Logger.info("Creating backup before destruction...")
        val backupResult = createPreDestructionBackup(destroyContext)
        if (!backupResult.success) {
            Logger.error("Backup failed: ${backupResult.error}")
            if (!config.forceDestroy) {
                kotlin.system.exitProcess(1)
            }
        }
    }

    // Execute destruction
    if (config.dryRun) {
        Logger.info("DRY RUN: Would destroy infrastructure in ${config.targetEnvironment}")
        simulateDestruction(destroyContext)
    } else {
        Logger.warning("DESTRUCTIVE OPERATION: This will permanently delete resources!")
        if (config.targetEnvironment == Environment.PRODUCTION) {
            Logger.warning("PRODUCTION ENVIRONMENT: All data will be lost!")
        }

        Logger.info("Executing destruction...")
        val destroyResult = performDestruction(destroyContext)

        if (destroyResult.success) {
            Logger.info("Infrastructure destruction completed")
            Logger.info("Destroyed resources: ${destroyResult.destroyedResources.joinToString()}")
        } else {
            Logger.error("Destruction failed: ${destroyResult.error}")
            kotlin.system.exitProcess(1)
        }
    }
}


fun showQuickStart() {
    println("""
Quick Start Examples:
  Deploy application:
    cloud-deploy deploy --env staging --manifest app.yml --strategy rolling

  Scale service:
    cloud-deploy scale --service-name web-app --replicas 5

  Monitor deployment:
    cloud-deploy monitor --monitor-health --monitor-metrics

  Rollback deployment:
    cloud-deploy rollback --rollback-target web-app --rollback-to-previous

For detailed documentation, visit: https://docs.example.com/cloud-deploy
    """.trimIndent())
}

// Data classes and stubs for the example
data class DeploymentContext(
    val provider: CloudProvider,
    val environment: Environment,
    val region: String?,
    val resourceGroup: String?,
    val manifest: String,
    val strategy: DeploymentStrategy,
    val dryRun: Boolean,
    val autoApprove: Boolean,
    val safetyChecks: Boolean
)

data class ValidationResult(val success: Boolean, val errors: List<String> = emptyList())
data class SecurityScanResult(val passed: Boolean, val issues: List<String> = emptyList())
data class DeployResult(
    val success: Boolean,
    val error: String? = null,
    val applicationUrl: String? = null,
    val deploymentId: String? = null
)

data class RollbackContext(
    val provider: CloudProvider,
    val environment: Environment,
    val target: String,
    val version: String?,
    val toLatest: Boolean,
    val toPrevious: Boolean,
    val skipValidation: Boolean
)

data class RollbackValidationResult(val isValid: Boolean, val error: String? = null)
data class RollbackResult(val success: Boolean, val error: String? = null, val restoredVersion: String? = null)

data class ScaleContext(
    val provider: CloudProvider,
    val environment: Environment,
    val serviceName: String,
    val targetReplicas: Int?,
    val autoscalingConfig: String?,
    val minReplicas: Int?,
    val maxReplicas: Int?
)

data class ScaleResult(val success: Boolean, val error: String? = null, val currentReplicas: Int? = null)

data class MonitorContext(
    val provider: CloudProvider,
    val environment: Environment,
    val health: Boolean,
    val metrics: Boolean,
    val logs: Boolean,
    val duration: Int,
    val metricsConfig: String?,
    val logAggregator: String?
)

data class MonitorResult(
    val healthStatus: String,
    val avgResponseTime: Int? = null,
    val errorRate: Double? = null
)

data class DestroyContext(
    val provider: CloudProvider,
    val environment: Environment,
    val resourceGroup: String?,
    val keepData: Boolean,
    val forceDestroy: Boolean,
    val safeDestroy: Boolean,
    val backupBeforeDestroy: Boolean
)

data class BackupResult(val success: Boolean, val error: String? = null)
data class DestroyResult(val success: Boolean, val error: String? = null, val destroyedResources: List<String> = emptyList())

object Logger {
    private var level = LogLevel.INFO

    fun configure(newLevel: LogLevel) { level = newLevel }
    fun info(message: String) { if (level <= LogLevel.INFO) println("[INFO] $message") }
    fun debug(message: String) { if (level <= LogLevel.DEBUG) println("[DEBUG] $message") }
    fun warning(message: String) { if (level <= LogLevel.WARN) println("[WARN] $message") }
    fun error(message: String) { if (level <= LogLevel.ERROR) System.err.println("[ERROR] $message") }
}

// Stub implementations
fun validateDeployment(context: DeploymentContext): ValidationResult = ValidationResult(true)
fun runSecurityScan(context: DeploymentContext): SecurityScanResult = SecurityScanResult(true)
fun simulateDeployment(context: DeploymentContext) { /* Implementation */ }
fun performDeployment(context: DeploymentContext): DeployResult =
    DeployResult(true, applicationUrl = "https://app.example.com", deploymentId = "deploy-123")
fun monitorPostDeployment(deploymentId: String) { /* Implementation */ }
fun performAutomaticRollback(context: DeploymentContext) { /* Implementation */ }

fun validateRollbackTarget(context: RollbackContext): RollbackValidationResult = RollbackValidationResult(true)
fun simulateRollback(context: RollbackContext) { /* Implementation */ }
fun performRollback(context: RollbackContext): RollbackResult = RollbackResult(true, restoredVersion = "v1.2.3")

fun simulateScaling(context: ScaleContext) { /* Implementation */ }
fun performScaling(context: ScaleContext): ScaleResult = ScaleResult(true, currentReplicas = 5)

fun startMonitoring(context: MonitorContext): MonitorResult =
    MonitorResult("healthy", avgResponseTime = 250, errorRate = 0.1)

fun createPreDestructionBackup(context: DestroyContext): BackupResult = BackupResult(true)
fun simulateDestruction(context: DestroyContext) { /* Implementation */ }
fun performDestruction(context: DestroyContext): DestroyResult =
    DestroyResult(true, destroyedResources = listOf("web-app", "database", "load-balancer"))

Usage Examples

Basic Deployment

# Deploy to staging with service account
cloud-deploy deploy --env staging --manifest app.yml --provider aws --region us-east-1 --service-account --service-account-file creds.json

# Production deployment with approvals
cloud-deploy deploy --env production --manifest prod-app.yml --strategy blue-green \
  --production-approval $PROD_TOKEN --compliance-level high

Scaling Operations

# Manual scaling
cloud-deploy scale --service-name web-app --replicas 10 --env production

# Autoscaling configuration
cloud-deploy scale --service-name api --autoscaling-config autoscale.yml \
  --min-replicas 2 --max-replicas 20

Monitoring and Rollbacks

# Monitor deployment health
cloud-deploy monitor --monitor-health --monitor-metrics --monitoring-duration 600

# Emergency rollback
cloud-deploy rollback --rollback-target web-app --rollback-to-previous \
  --env production --emergency-approval $EMERGENCY_TOKEN

Infrastructure Management

# Destroy development environment
cloud-deploy destroy --env development --confirm-destroy DELETE_ALL_RESOURCES \
  --keep-data --safe-destroy

# Dry run destruction
cloud-deploy destroy --env staging --dry-run --confirm-destroy DELETE_ALL_RESOURCES

Key Features Demonstrated

  1. Fragment Inheritance: Reusable authentication, security, and resource management patterns
  2. Environment-Specific Constraints: Different requirements for dev/staging/production
  3. Security Integration: Compliance levels, approvals, audit logging
  4. Multi-Provider Support: AWS, GCP, Azure, DigitalOcean configurations
  5. Comprehensive Validation: Pre-flight checks, security scanning, rollback validation
  6. Production Safety: Confirmation tokens, approval workflows, backup strategies
  7. Complex Domain Relationships: Deploy, rollback, scale, monitor, destroy operations
  8. Real-World Integration: Environment variables, configuration files, logging

This example demonstrates how Argos enables building sophisticated enterprise-grade tools with complex business logic while maintaining clean, maintainable code.