rise-deploy 0.15.10

A simple and powerful CLI for deploying containerized applications
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
use anyhow::{bail, Result};
use reqwest::Client;
use serde::Deserialize;
use std::io::{self, IsTerminal, Write as _};
use std::time::{Duration, Instant};
use tracing::info;

use crate::api::models::{Deployment, DeploymentStatus};
use crate::config::Config;

use super::core::{fetch_deployment, parse_duration};

// Project info for fetching project URL
#[derive(Deserialize)]
struct ProjectInfo {
    #[serde(skip_serializing_if = "Option::is_none")]
    primary_url: Option<String>,
}

// Legacy Docker controller metadata structures (for backward compatibility with old deployments)
#[derive(Deserialize, Debug, Clone, PartialEq)]
struct DockerMetadata {
    #[serde(default)]
    reconcile_phase: ReconcilePhase,
    container_id: Option<String>,
    container_name: Option<String>,
    assigned_port: Option<u16>,
}

#[derive(Deserialize, Debug, Clone, PartialEq, Default)]
enum ReconcilePhase {
    #[default]
    NotStarted,
    CreatingContainer,
    StartingContainer,
    WaitingForHealth,
    Completed,
}

// ANSI escape codes for terminal manipulation
mod ansi {
    pub const CLEAR_LINE: &str = "\x1B[2K";
    pub const HIDE_CURSOR: &str = "\x1B[?25l";
    pub const SHOW_CURSOR: &str = "\x1B[?25h";
    pub const RESET: &str = "\x1B[0m";

    /// Move cursor up n lines
    pub fn move_up(n: usize) -> String {
        format!("\x1B[{}A", n)
    }

    /// Move cursor to beginning of line
    pub const CURSOR_TO_START: &str = "\r";
}

// Spinner animation frames
const SPINNER_FRAMES: &[&str] = &["", "", "", "", "", "", "", "", "", ""];

/// State tracking between polls
struct FollowState {
    last_status: DeploymentStatus,
    last_controller_phase: Option<ReconcilePhase>,
    last_error: Option<String>,
    last_url: Option<String>,
    last_metadata: serde_json::Value,
    spinner_frame: usize,
    is_first_poll: bool,
}

impl FollowState {
    fn new() -> Self {
        Self {
            last_status: DeploymentStatus::Pending,
            last_controller_phase: None,
            last_error: None,
            last_url: None,
            last_metadata: serde_json::Value::Null,
            spinner_frame: 0,
            is_first_poll: true,
        }
    }

    fn should_log_state_change(
        &self,
        deployment: &Deployment,
        controller_phase: &Option<ReconcilePhase>,
    ) -> bool {
        self.is_first_poll
            || self.last_status != deployment.status
            || self.last_controller_phase != *controller_phase
    }

    fn update(&mut self, deployment: &Deployment, controller_phase: Option<ReconcilePhase>) {
        self.last_status = deployment.status.clone();
        self.last_controller_phase = controller_phase;
        self.last_error = deployment.error_message.clone();
        self.last_url = deployment.primary_url.clone();
        self.last_metadata = deployment.controller_metadata.clone();
        self.is_first_poll = false;
    }
}

/// Live status section that gets replaced on each poll
struct LiveStatusSection {
    pub last_line_count: usize,
}

impl LiveStatusSection {
    fn new() -> Self {
        Self { last_line_count: 0 }
    }

    fn clear_previous(&self) {
        if self.last_line_count > 0 {
            // Move cursor up and clear each line
            for _ in 0..self.last_line_count {
                print!(
                    "{}{}{}",
                    ansi::move_up(1),
                    ansi::CURSOR_TO_START,
                    ansi::CLEAR_LINE
                );
            }
            print!("{}", ansi::CURSOR_TO_START);
            io::stdout().flush().unwrap();
        }
    }

    fn render(
        &mut self,
        deployment: &Deployment,
        state: &FollowState,
        controller_phase: &Option<ReconcilePhase>,
    ) -> String {
        // Clear previous output
        self.clear_previous();

        let mut output = String::new();
        let mut line_count = 0;

        // Status line with icon and color
        let icon = status_icon(&deployment.status);
        let color = status_color(&deployment.status);
        let spinner = if is_in_progress(&deployment.status) {
            format!("{} ", spinner_frame(state.spinner_frame))
        } else {
            String::new()
        };

        // Show deployment status + controller phase if available
        let status_text = if let Some(phase) = controller_phase {
            format!("{} ({})", deployment.status, format_controller_phase(phase))
        } else {
            format!("{}", deployment.status)
        };

        output.push_str(&format!(
            "{}{} Status:    {}{}{}\n",
            spinner,
            icon,
            color,
            status_text,
            ansi::RESET
        ));
        line_count += 1;

        // URL if available
        if let Some(ref url) = deployment.primary_url {
            output.push_str(&format!("   URL:       {}\n", url));
            line_count += 1;
        }

        // Error message if present
        if let Some(ref error) = deployment.error_message {
            output.push_str(&format!(
                "   {}Error:{} {}\n",
                "\x1B[31m",
                ansi::RESET,
                error
            ));
            line_count += 1;
        }

        // Controller metadata summary (container ID if available)
        if let Some(container_id) = extract_container_id(&deployment.controller_metadata) {
            output.push_str(&format!("   Container: {}\n", container_id));
            line_count += 1;
        }

        self.last_line_count = line_count;
        output
    }
}

/// Get status color ANSI code
fn status_color(status: &DeploymentStatus) -> &'static str {
    match status {
        DeploymentStatus::Healthy => "\x1B[32m",   // Green
        DeploymentStatus::Failed => "\x1B[31m",    // Red
        DeploymentStatus::Deploying => "\x1B[33m", // Yellow
        DeploymentStatus::Building => "\x1B[36m",  // Cyan
        DeploymentStatus::Pushing => "\x1B[36m",   // Cyan
        DeploymentStatus::Unhealthy => "\x1B[31m", // Red
        DeploymentStatus::Cancelled => "\x1B[90m", // Gray
        DeploymentStatus::Stopped => "\x1B[90m",   // Gray
        _ => "\x1B[37m",                           // White
    }
}

/// Get status icon
fn status_icon(status: &DeploymentStatus) -> &'static str {
    match status {
        DeploymentStatus::Healthy => "",
        DeploymentStatus::Failed => "",
        DeploymentStatus::Deploying => "",
        DeploymentStatus::Building => "🔨",
        DeploymentStatus::Pushing => "",
        DeploymentStatus::Pushed => "",
        DeploymentStatus::Unhealthy => "",
        DeploymentStatus::Cancelled => "",
        DeploymentStatus::Cancelling => "",
        DeploymentStatus::Terminating => "",
        DeploymentStatus::Stopped => "",
        DeploymentStatus::Superseded => "",
        DeploymentStatus::Expired => "",
        DeploymentStatus::Pending => "",
    }
}

/// Get spinner frame
fn spinner_frame(frame_num: usize) -> &'static str {
    SPINNER_FRAMES[frame_num % SPINNER_FRAMES.len()]
}

/// Check if status is in-progress (should show spinner)
fn is_in_progress(status: &DeploymentStatus) -> bool {
    matches!(
        status,
        DeploymentStatus::Pending
            | DeploymentStatus::Building
            | DeploymentStatus::Pushing
            | DeploymentStatus::Pushed
            | DeploymentStatus::Deploying
            | DeploymentStatus::Cancelling
            | DeploymentStatus::Terminating
    )
}

/// Check if status is terminal
fn is_terminal_state(status: &DeploymentStatus) -> bool {
    matches!(
        status,
        DeploymentStatus::Healthy
            | DeploymentStatus::Failed
            | DeploymentStatus::Cancelled
            | DeploymentStatus::Stopped
            | DeploymentStatus::Superseded
            | DeploymentStatus::Expired
    )
}

/// Parse controller metadata to extract deployment phase info (handles legacy Docker deployments)
fn parse_controller_metadata(metadata: &serde_json::Value) -> Option<DockerMetadata> {
    if metadata.is_null() || metadata == &serde_json::json!({}) {
        return None;
    }

    // Try to parse as Docker metadata (for legacy deployments)
    // For Kubernetes deployments, this will return None, which is fine
    serde_json::from_value::<DockerMetadata>(metadata.clone()).ok()
}

/// Extract container ID from metadata for display
fn extract_container_id(metadata: &serde_json::Value) -> Option<String> {
    parse_controller_metadata(metadata)
        .and_then(|m| m.container_id.map(|id| id[..12.min(id.len())].to_string()))
}

/// Format controller phase for display
fn format_controller_phase(phase: &ReconcilePhase) -> String {
    match phase {
        ReconcilePhase::NotStarted => "not started".to_string(),
        ReconcilePhase::CreatingContainer => "creating container".to_string(),
        ReconcilePhase::StartingContainer => "starting container".to_string(),
        ReconcilePhase::WaitingForHealth => "waiting for health".to_string(),
        ReconcilePhase::Completed => "running".to_string(),
    }
}

/// Log state change to tracing (appears in history)
fn log_state_change(
    project: &str,
    deployment_id: &str,
    status: &DeploymentStatus,
    controller_phase: &Option<ReconcilePhase>,
) {
    let status_text = if let Some(phase) = controller_phase {
        format!("{} ({})", status, format_controller_phase(phase))
    } else {
        format!("{}", status)
    };

    info!("Deployment {}:{} → {}", project, deployment_id, status_text);
}

/// Check if stdout is a TTY
fn is_tty() -> bool {
    io::stdout().is_terminal()
}

/// Print deployment snapshot (for non-follow mode)
pub fn print_deployment_snapshot(deployment: &Deployment) {
    // Parse controller metadata
    let controller_phase =
        parse_controller_metadata(&deployment.controller_metadata).map(|m| m.reconcile_phase);

    // Status line with icon and color
    let icon = status_icon(&deployment.status);
    let color = status_color(&deployment.status);

    // Show deployment status + controller phase if available
    let status_text = if let Some(phase) = controller_phase {
        format!(
            "{} ({})",
            deployment.status,
            format_controller_phase(&phase)
        )
    } else {
        format!("{}", deployment.status)
    };

    println!(
        "{} Status:         {}{}{}",
        icon,
        color,
        status_text,
        ansi::RESET
    );

    // Deployment ID
    println!("   Deployment ID:  {}", deployment.deployment_id);

    // Deployment group (if not default)
    if deployment.deployment_group != "default" {
        println!("   Group:          {}", deployment.deployment_group);
    }

    // Created by
    println!("   Created by:     {}", deployment.created_by_email);

    // Created/Updated timestamps
    println!("   Created:        {}", deployment.created);
    if deployment.updated != deployment.created {
        println!("   Updated:        {}", deployment.updated);
    }

    // Expires at (if set)
    if let Some(ref expires) = deployment.expires_at {
        println!("   Expires at:     {}", expires);
    }

    // Image and digest (if available)
    if let Some(ref image) = deployment.image {
        println!("   Image:          {}", image);
    }
    if let Some(ref digest) = deployment.image_digest {
        println!("   Image digest:   {}", digest);
    }

    // URL if available
    if let Some(ref url) = deployment.primary_url {
        println!("   URL:            {}", url);
    }

    // Controller metadata summary (container ID if available)
    if let Some(container_id) = extract_container_id(&deployment.controller_metadata) {
        println!("   Container:      {}", container_id);
    }

    // Error message if present
    if let Some(ref error) = deployment.error_message {
        println!("   \x1B[31mError:{} {}", ansi::RESET, error);
    }
}

/// Fetch project info to get project URL
async fn fetch_project_info(
    http_client: &Client,
    backend_url: &str,
    token: &str,
    project: &str,
) -> Result<ProjectInfo> {
    let url = format!("{}/api/v1/projects/{}", backend_url, project);

    let response = http_client.get(&url).bearer_auth(token).send().await?;

    if !response.status().is_success() {
        bail!("Failed to fetch project info");
    }

    let project_info: ProjectInfo = response.json().await?;
    Ok(project_info)
}

/// Main follow function with enhanced UX
pub async fn follow_deployment_with_ui(
    http_client: &Client,
    backend_url: &str,
    config: &Config,
    project: &str,
    deployment_id: &str,
    timeout_str: &str,
) -> Result<Deployment> {
    let token = config
        .get_token()
        .ok_or_else(|| anyhow::anyhow!("Not authenticated"))?;

    let timeout = parse_duration(timeout_str)?;
    let start_time = Instant::now();

    // Check if we're in a TTY - if not, fall back to simple mode
    if !is_tty() {
        return follow_deployment_simple(
            http_client,
            backend_url,
            &token,
            project,
            deployment_id,
            timeout,
        )
        .await;
    }

    let mut state = FollowState::new();
    let mut live_section = LiveStatusSection::new();

    // Hide cursor for cleaner output
    print!("{}", ansi::HIDE_CURSOR);
    io::stdout().flush().unwrap();

    let result = async {
        loop {
            // Fetch deployment status
            let deployment =
                fetch_deployment(http_client, backend_url, &token, project, deployment_id).await?;

            // Parse controller metadata
            let controller_phase = parse_controller_metadata(&deployment.controller_metadata)
                .map(|m| m.reconcile_phase);

            // Check if this is a state change
            if state.should_log_state_change(&deployment, &controller_phase) {
                // Clear any existing live section before logging
                live_section.clear_previous();

                // Log state change to history
                log_state_change(
                    project,
                    deployment_id,
                    &deployment.status,
                    &controller_phase,
                );

                // Reset line count so next render works correctly
                live_section.last_line_count = 0;
            } else {
                // Only show live section when status is stable (spinner animation)
                let output = live_section.render(&deployment, &state, &controller_phase);
                print!("{}", output);
                io::stdout().flush().unwrap();
            }

            // Update state
            state.update(&deployment, controller_phase);
            state.spinner_frame = (state.spinner_frame + 1) % SPINNER_FRAMES.len();

            // Check if deployment reached terminal state
            if is_terminal_state(&deployment.status) {
                return Ok(deployment);
            }

            // Check timeout
            if start_time.elapsed() >= timeout {
                bail!(
                    "Timeout waiting for deployment to complete after {:?}",
                    timeout
                );
            }

            // Wait before next poll (1 second for 2x faster spinner)
            tokio::time::sleep(Duration::from_secs(1)).await;
        }
    }
    .await;

    // Always show cursor again before returning
    print!("{}", ansi::SHOW_CURSOR);
    io::stdout().flush().unwrap();

    // Print project URL if deployment became active (Healthy in default group)
    if let Ok(ref deployment) = result {
        if deployment.status == DeploymentStatus::Healthy
            && deployment.deployment_group == "default"
        {
            if let Ok(project_info) =
                fetch_project_info(http_client, backend_url, &token, project).await
            {
                if let Some(url) = project_info.primary_url {
                    println!();
                    println!("Project URL: {}", url);
                }
            }
        }
    }

    result
}

/// Simple fallback for non-TTY environments (pipes, redirects)
async fn follow_deployment_simple(
    http_client: &Client,
    backend_url: &str,
    token: &str,
    project: &str,
    deployment_id: &str,
    timeout: Duration,
) -> Result<Deployment> {
    let start_time = Instant::now();
    let mut state = FollowState::new();

    loop {
        let deployment =
            fetch_deployment(http_client, backend_url, token, project, deployment_id).await?;

        // Parse controller metadata
        let controller_phase =
            parse_controller_metadata(&deployment.controller_metadata).map(|m| m.reconcile_phase);

        // Log state changes only (not every poll)
        if state.should_log_state_change(&deployment, &controller_phase) {
            log_state_change(
                project,
                deployment_id,
                &deployment.status,
                &controller_phase,
            );
        }

        // Update state
        state.update(&deployment, controller_phase);

        if is_terminal_state(&deployment.status) {
            // Print project URL if deployment became active (Healthy in default group)
            if deployment.status == DeploymentStatus::Healthy
                && deployment.deployment_group == "default"
            {
                if let Ok(project_info) =
                    fetch_project_info(http_client, backend_url, token, project).await
                {
                    if let Some(url) = project_info.primary_url {
                        println!();
                        println!("Project URL: {}", url);
                    }
                }
            }
            return Ok(deployment);
        }

        if start_time.elapsed() >= timeout {
            bail!("Timeout waiting for deployment");
        }

        tokio::time::sleep(Duration::from_secs(1)).await;
    }
}