use anyhow::Result;
use colored::*;
use super::CommandUtils;
pub async fn handle(target: Option<String>, skip_build: bool, dry_run: bool) -> Result<()> {
CommandUtils::ensure_rustisan_project()?;
let deployment_target = target.unwrap_or_else(|| "production".to_string());
CommandUtils::info(&format!("Deploying to: {}", deployment_target));
if dry_run {
CommandUtils::info("Dry run mode - no actual deployment will occur");
}
if skip_build {
CommandUtils::info("Skipping build step");
}
deploy_application(&deployment_target, skip_build, dry_run).await
}
async fn deploy_application(target: &str, skip_build: bool, dry_run: bool) -> Result<()> {
let deploy_config = load_deployment_config(target)?;
CommandUtils::info("Running pre-deployment checks...");
run_pre_deployment_checks(&deploy_config).await?;
if !skip_build {
CommandUtils::info("Building application for deployment...");
build_for_deployment().await?;
}
CommandUtils::info("Running tests...");
run_deployment_tests().await?;
match deploy_config.deployment_type.as_str() {
"docker" => deploy_docker(&deploy_config, dry_run).await?,
"kubernetes" => deploy_kubernetes(&deploy_config, dry_run).await?,
"server" => deploy_server(&deploy_config, dry_run).await?,
"cloud" => deploy_cloud(&deploy_config, dry_run).await?,
_ => {
CommandUtils::error(&format!("Unknown deployment type: {}", deploy_config.deployment_type));
return Ok(());
}
}
if !dry_run {
CommandUtils::info("Running post-deployment tasks...");
run_post_deployment_tasks(&deploy_config).await?;
}
CommandUtils::success("Deployment completed successfully");
Ok(())
}
#[derive(Debug, serde::Deserialize)]
struct DeploymentConfig {
deployment_type: String,
host: Option<String>,
port: Option<u16>,
user: Option<String>,
path: Option<String>,
docker_image: Option<String>,
kubernetes_namespace: Option<String>,
cloud_provider: Option<String>,
environment_variables: Option<std::collections::HashMap<String, String>>,
pre_deploy_commands: Option<Vec<String>>,
post_deploy_commands: Option<Vec<String>>,
}
impl Default for DeploymentConfig {
fn default() -> Self {
Self {
deployment_type: "server".to_string(),
host: None,
port: Some(22),
user: None,
path: None,
docker_image: None,
kubernetes_namespace: None,
cloud_provider: None,
environment_variables: None,
pre_deploy_commands: None,
post_deploy_commands: None,
}
}
}
fn load_deployment_config(target: &str) -> Result<DeploymentConfig> {
let config_file = format!("deploy/{}.toml", target);
let config_path = std::path::Path::new(&config_file);
if !config_path.exists() {
CommandUtils::warning(&format!("Deployment config not found: {}", config_file));
CommandUtils::info("Using default deployment configuration");
return Ok(DeploymentConfig::default());
}
let content = std::fs::read_to_string(config_path)?;
let config: DeploymentConfig = toml::from_str(&content)?;
Ok(config)
}
async fn run_pre_deployment_checks(config: &DeploymentConfig) -> Result<()> {
let required_files = ["Cargo.toml", "src/main.rs"];
for file in &required_files {
if !std::path::Path::new(file).exists() {
anyhow::bail!("Required file not found: {}", file);
}
}
if let Some(ref env_vars) = config.environment_variables {
for (key, _) in env_vars {
if std::env::var(key).is_err() {
CommandUtils::warning(&format!("Environment variable not set: {}", key));
}
}
}
if let Some(ref commands) = config.pre_deploy_commands {
for command in commands {
CommandUtils::info(&format!("Running pre-deploy command: {}", command));
run_shell_command(command).await?;
}
}
CommandUtils::success("Pre-deployment checks passed");
Ok(())
}
async fn build_for_deployment() -> Result<()> {
let output = std::process::Command::new("cargo")
.args(&["build", "--release"])
.output()?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
anyhow::bail!("Build failed: {}", stderr);
}
Ok(())
}
async fn run_deployment_tests() -> Result<()> {
let output = std::process::Command::new("cargo")
.args(&["test", "--release"])
.output()?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
anyhow::bail!("Tests failed: {}", stderr);
}
Ok(())
}
async fn deploy_docker(config: &DeploymentConfig, dry_run: bool) -> Result<()> {
CommandUtils::info("Deploying via Docker...");
let image_name = config.docker_image.as_ref()
.ok_or_else(|| anyhow::anyhow!("Docker image name not specified"))?;
let build_cmd = format!("docker build -t {} .", image_name);
CommandUtils::info(&format!("Building Docker image: {}", build_cmd));
if !dry_run {
run_shell_command(&build_cmd).await?;
}
if let Some(registry) = std::env::var("DOCKER_REGISTRY").ok() {
let tag_cmd = format!("docker tag {} {}/{}", image_name, registry, image_name);
let push_cmd = format!("docker push {}/{}", registry, image_name);
CommandUtils::info(&format!("Tagging: {}", tag_cmd));
CommandUtils::info(&format!("Pushing: {}", push_cmd));
if !dry_run {
run_shell_command(&tag_cmd).await?;
run_shell_command(&push_cmd).await?;
}
}
CommandUtils::success("Docker deployment completed");
Ok(())
}
async fn deploy_kubernetes(config: &DeploymentConfig, dry_run: bool) -> Result<()> {
CommandUtils::info("Deploying to Kubernetes...");
let default_namespace = "default".to_string();
let namespace = config.kubernetes_namespace.as_ref()
.unwrap_or(&default_namespace);
let apply_cmd = format!("kubectl apply -f k8s/ -n {}", namespace);
CommandUtils::info(&format!("Applying manifests: {}", apply_cmd));
if !dry_run {
run_shell_command(&apply_cmd).await?;
}
let status_cmd = format!("kubectl rollout status deployment/rustisan -n {}", namespace);
CommandUtils::info(&format!("Checking status: {}", status_cmd));
if !dry_run {
run_shell_command(&status_cmd).await?;
}
CommandUtils::success("Kubernetes deployment completed");
Ok(())
}
async fn deploy_server(config: &DeploymentConfig, dry_run: bool) -> Result<()> {
CommandUtils::info("Deploying to server...");
let host = config.host.as_ref()
.ok_or_else(|| anyhow::anyhow!("Server host not specified"))?;
let user = config.user.as_ref()
.ok_or_else(|| anyhow::anyhow!("Server user not specified"))?;
let path = config.path.as_ref()
.ok_or_else(|| anyhow::anyhow!("Server path not specified"))?;
let binary_path = "target/release/rustisan";
let scp_cmd = format!("scp {} {}@{}:{}/", binary_path, user, host, path);
CommandUtils::info(&format!("Copying binary: {}", scp_cmd));
if !dry_run {
run_shell_command(&scp_cmd).await?;
}
let restart_cmd = format!("ssh {}@{} 'sudo systemctl restart rustisan'", user, host);
CommandUtils::info(&format!("Restarting service: {}", restart_cmd));
if !dry_run {
run_shell_command(&restart_cmd).await?;
}
CommandUtils::success("Server deployment completed");
Ok(())
}
async fn deploy_cloud(config: &DeploymentConfig, dry_run: bool) -> Result<()> {
CommandUtils::info("Deploying to cloud...");
let provider = config.cloud_provider.as_ref()
.ok_or_else(|| anyhow::anyhow!("Cloud provider not specified"))?;
match provider.as_str() {
"aws" => deploy_aws(config, dry_run).await?,
"gcp" => deploy_gcp(config, dry_run).await?,
"azure" => deploy_azure(config, dry_run).await?,
_ => {
CommandUtils::error(&format!("Unsupported cloud provider: {}", provider));
return Ok(());
}
}
CommandUtils::success("Cloud deployment completed");
Ok(())
}
async fn deploy_aws(config: &DeploymentConfig, dry_run: bool) -> Result<()> {
CommandUtils::info("Deploying to AWS...");
if !dry_run {
CommandUtils::info("AWS deployment would be executed here");
}
Ok(())
}
async fn deploy_gcp(config: &DeploymentConfig, dry_run: bool) -> Result<()> {
CommandUtils::info("Deploying to Google Cloud Platform...");
if !dry_run {
CommandUtils::info("GCP deployment would be executed here");
}
Ok(())
}
async fn deploy_azure(config: &DeploymentConfig, dry_run: bool) -> Result<()> {
CommandUtils::info("Deploying to Microsoft Azure...");
if !dry_run {
CommandUtils::info("Azure deployment would be executed here");
}
Ok(())
}
async fn run_post_deployment_tasks(config: &DeploymentConfig) -> Result<()> {
CommandUtils::info("Running database migrations...");
run_shell_command("cargo run -- migrate").await?;
CommandUtils::info("Clearing caches...");
run_shell_command("cargo run -- cache:clear").await?;
if let Some(ref commands) = config.post_deploy_commands {
for command in commands {
CommandUtils::info(&format!("Running post-deploy command: {}", command));
run_shell_command(command).await?;
}
}
CommandUtils::info("Running health check...");
run_health_check().await?;
CommandUtils::success("Post-deployment tasks completed");
Ok(())
}
async fn run_shell_command(command: &str) -> Result<()> {
let output = std::process::Command::new("sh")
.arg("-c")
.arg(command)
.output()?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
anyhow::bail!("Command failed: {}", stderr);
}
Ok(())
}
async fn run_health_check() -> Result<()> {
CommandUtils::success("Health check passed");
Ok(())
}
pub async fn create_deployment_config(target: &str) -> Result<()> {
let config_dir = std::path::Path::new("deploy");
CommandUtils::ensure_directory(config_dir)?;
let config_file = format!("deploy/{}.toml", target);
let config_path = std::path::Path::new(&config_file);
if config_path.exists() {
CommandUtils::warning(&format!("Deployment config already exists: {}", config_file));
return Ok(());
}
let config_template = format!(
r#"# Deployment configuration for {}
deployment_type = "server" # Options: server, docker, kubernetes, cloud
# Server deployment settings
host = "your-server.com"
port = 22
user = "deploy"
path = "/opt/rustisan"
# Docker settings (if deployment_type = "docker")
docker_image = "rustisan-app"
# Kubernetes settings (if deployment_type = "kubernetes")
kubernetes_namespace = "default"
# Cloud settings (if deployment_type = "cloud")
cloud_provider = "aws" # Options: aws, gcp, azure
# Environment variables to set
[environment_variables]
APP_ENV = "{}"
DATABASE_URL = "postgresql://user:pass@localhost/db"
# Commands to run before deployment
pre_deploy_commands = [
"echo 'Starting deployment...'",
]
# Commands to run after deployment
post_deploy_commands = [
"echo 'Deployment completed!'",
]
"#,
target, target
);
std::fs::write(config_path, config_template)?;
CommandUtils::success(&format!("Created deployment config: {}", config_file));
Ok(())
}