|
| 1 | +use crate::commands::integrations::fly::FlyManager; |
| 2 | +use crate::config::CloudConfig; |
| 3 | +use crate::docker::DockerManager; |
| 4 | +use crate::output::{Operation, Step}; |
| 5 | +use crate::project::ProjectContext; |
| 6 | +use crate::prompts; |
| 7 | +use eyre::{OptionExt, Result}; |
| 8 | + |
| 9 | +pub async fn run(instance_name: Option<String>) -> Result<()> { |
| 10 | + // Load project context |
| 11 | + let project = ProjectContext::find_and_load(None)?; |
| 12 | + |
| 13 | + // Get instance name - prompt if not provided |
| 14 | + let instance_name = match instance_name { |
| 15 | + Some(name) => name, |
| 16 | + None if prompts::is_interactive() => { |
| 17 | + let instances = project.config.list_instances_with_types(); |
| 18 | + prompts::select_instance(&instances)? |
| 19 | + } |
| 20 | + None => { |
| 21 | + let instances = project.config.list_instances(); |
| 22 | + return Err(eyre::eyre!( |
| 23 | + "No instance specified. Available instances: {}", |
| 24 | + instances |
| 25 | + .into_iter() |
| 26 | + .cloned() |
| 27 | + .collect::<Vec<_>>() |
| 28 | + .join(", ") |
| 29 | + )); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + // Get instance config |
| 34 | + let instance_config = project.config.get_instance(&instance_name)?; |
| 35 | + |
| 36 | + if instance_config.is_local() { |
| 37 | + restart_local_instance(&project, &instance_name).await |
| 38 | + } else { |
| 39 | + restart_cloud_instance(&project, &instance_name, instance_config.into()).await |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +async fn restart_local_instance(project: &ProjectContext, instance_name: &str) -> Result<()> { |
| 44 | + let op = Operation::new("Restarting", instance_name); |
| 45 | + |
| 46 | + let docker = DockerManager::new(project); |
| 47 | + |
| 48 | + // Check Docker availability |
| 49 | + DockerManager::check_runtime_available(docker.runtime)?; |
| 50 | + |
| 51 | + // Check if instance is built (has docker-compose.yml) |
| 52 | + let workspace = project.instance_workspace(instance_name); |
| 53 | + let compose_file = workspace.join("docker-compose.yml"); |
| 54 | + |
| 55 | + if !compose_file.exists() { |
| 56 | + op.failure(); |
| 57 | + let error = crate::errors::CliError::new(format!( |
| 58 | + "instance '{instance_name}' has not been built yet" |
| 59 | + )) |
| 60 | + .with_hint(format!( |
| 61 | + "run 'helix build {instance_name}' first to build the instance" |
| 62 | + )); |
| 63 | + return Err(eyre::eyre!("{}", error.render())); |
| 64 | + } |
| 65 | + |
| 66 | + // Restart the instance |
| 67 | + let mut restart_step = Step::with_messages("Restarting container", "Container restarted"); |
| 68 | + restart_step.start(); |
| 69 | + docker.restart_instance(instance_name)?; |
| 70 | + restart_step.done(); |
| 71 | + |
| 72 | + op.success(); |
| 73 | + |
| 74 | + Ok(()) |
| 75 | +} |
| 76 | + |
| 77 | +async fn restart_cloud_instance( |
| 78 | + project: &ProjectContext, |
| 79 | + instance_name: &str, |
| 80 | + instance_config: CloudConfig, |
| 81 | +) -> Result<()> { |
| 82 | + let op = Operation::new("Restarting", instance_name); |
| 83 | + |
| 84 | + let _cluster_id = instance_config.get_cluster_id().ok_or_eyre(format!( |
| 85 | + "Cloud instance '{instance_name}' must have a cluster_id" |
| 86 | + ))?; |
| 87 | + |
| 88 | + let mut restart_step = |
| 89 | + Step::with_messages("Restarting cloud instance", "Cloud instance restarted"); |
| 90 | + restart_step.start(); |
| 91 | + |
| 92 | + match instance_config { |
| 93 | + CloudConfig::FlyIo(config) => { |
| 94 | + Step::verbose_substep("Stopping Fly.io instance..."); |
| 95 | + let fly = FlyManager::new(project, config.auth_type.clone()).await?; |
| 96 | + fly.stop_instance(instance_name).await?; |
| 97 | + |
| 98 | + Step::verbose_substep("Starting Fly.io instance..."); |
| 99 | + fly.start_instance(instance_name).await?; |
| 100 | + } |
| 101 | + CloudConfig::Helix(_config) => { |
| 102 | + todo!() |
| 103 | + } |
| 104 | + CloudConfig::Ecr(_config) => { |
| 105 | + unimplemented!() |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + restart_step.done(); |
| 110 | + op.success(); |
| 111 | + |
| 112 | + Ok(()) |
| 113 | +} |
0 commit comments