halldyll_deploy_pods 0.1.0

Declarative, idempotent, and reconcilable deployment system for RunPod GPU pods
Documentation
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! Pod command executor for post-provisioning tasks.
//!
//! This module handles executing commands on running pods via the RunPod API,
//! including model downloads and inference engine startup.

use std::time::Duration;
use tracing::{debug, error, info, warn};

use crate::config::{LoadConfig, ModelConfig, ModelProvider, PodConfig};
use crate::error::{HalldyllError, Result, RunPodError};

use super::client::RunPodClient;
use super::types::PodStatus;

/// Default timeout for command execution in seconds.
const DEFAULT_COMMAND_TIMEOUT_SECS: u64 = 600;

/// Default timeout for model download in seconds.
const MODEL_DOWNLOAD_TIMEOUT_SECS: u64 = 1800;

/// Polling interval for command status checks.
const POLL_INTERVAL_SECS: u64 = 5;

/// Pod command executor for post-provisioning tasks.
#[derive(Debug)]
pub struct PodExecutor {
    /// `RunPod` API client.
    client: RunPodClient,
}

/// Result of a command execution.
#[derive(Debug, Clone)]
pub struct CommandResult {
    /// Whether the command succeeded.
    pub success: bool,
    /// Command output (stdout).
    pub stdout: String,
    /// Command error output (stderr).
    pub stderr: String,
    /// Exit code if available.
    pub exit_code: Option<i32>,
}

/// Model setup result.
#[derive(Debug, Clone)]
pub struct ModelSetupResult {
    /// Model ID.
    pub model_id: String,
    /// Whether setup succeeded.
    pub success: bool,
    /// Model path on the pod.
    pub model_path: Option<String>,
    /// Error message if failed.
    pub error: Option<String>,
}

/// Engine startup result.
#[derive(Debug, Clone)]
pub struct EngineStartResult {
    /// Engine name.
    pub engine: String,
    /// Whether startup succeeded.
    pub success: bool,
    /// Service endpoint if available.
    pub endpoint: Option<String>,
    /// Error message if failed.
    pub error: Option<String>,
}

impl PodExecutor {
    /// Creates a new pod executor.
    #[must_use]
    pub const fn new(client: RunPodClient) -> Self {
        Self { client }
    }

    /// Executes a command on a running pod.
    ///
    /// # Errors
    ///
    /// Returns an error if the command cannot be executed.
    pub async fn execute_command(
        &self,
        pod_id: &str,
        command: &str,
        timeout_secs: Option<u64>,
    ) -> Result<CommandResult> {
        let timeout = timeout_secs.unwrap_or(DEFAULT_COMMAND_TIMEOUT_SECS);
        
        debug!("Executing command on pod {}: {}", pod_id, command);

        // Use RunPod's exec endpoint
        let result = self.client.exec_command(pod_id, command, timeout).await?;

        Ok(result)
    }

    /// Waits for a pod to be ready for command execution.
    ///
    /// # Errors
    ///
    /// Returns an error if the pod doesn't become ready within the timeout.
    pub async fn wait_for_ready(&self, pod_id: &str, timeout_secs: u64) -> Result<()> {
        let start = std::time::Instant::now();
        let timeout = Duration::from_secs(timeout_secs);

        info!("Waiting for pod {} to be ready for commands", pod_id);

        loop {
            let pod = self.client.get_pod(pod_id).await?;

            // Check if pod is running
            if pod.desired_status == PodStatus::Running && pod.runtime.is_some() {
                // Try a simple command to verify SSH/exec is working
                match self.execute_command(pod_id, "echo ready", Some(30)).await {
                    Ok(result) if result.success => {
                        info!("Pod {} is ready for commands", pod_id);
                        return Ok(());
                    }
                    Ok(_) => {
                        debug!("Pod {} not ready yet, retrying...", pod_id);
                    }
                    Err(e) => {
                        debug!("Pod {} exec not ready: {}", pod_id, e);
                    }
                }
            }

            if start.elapsed() > timeout {
                return Err(HalldyllError::RunPod(RunPodError::Timeout {
                    pod_id: pod_id.to_string(),
                    expected_state: "ready for commands".to_string(),
                }));
            }

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

    /// Downloads and sets up models on a pod.
    ///
    /// # Errors
    ///
    /// Returns an error if model setup fails.
    pub async fn setup_models(
        &self,
        pod_id: &str,
        models: &[ModelConfig],
    ) -> Result<Vec<ModelSetupResult>> {
        if models.is_empty() {
            return Ok(Vec::new());
        }

        info!("Setting up {} model(s) on pod {}", models.len(), pod_id);

        let mut results = Vec::with_capacity(models.len());

        for model in models {
            let result = self.setup_single_model(pod_id, model).await;
            results.push(result);
        }

        // Check if any critical models failed
        let failed: Vec<_> = results.iter().filter(|r| !r.success).collect();
        if !failed.is_empty() {
            warn!(
                "{} model(s) failed to setup on pod {}",
                failed.len(),
                pod_id
            );
        }

        Ok(results)
    }

    /// Sets up a single model on a pod.
    async fn setup_single_model(&self, pod_id: &str, model: &ModelConfig) -> ModelSetupResult {
        info!("Setting up model '{}' on pod {}", model.id, pod_id);

        match model.provider {
            ModelProvider::Huggingface => {
                self.setup_huggingface_model(pod_id, model).await
            }
            ModelProvider::Bundle => {
                self.setup_bundle_model(pod_id, model).await
            }
            ModelProvider::Custom => {
                // Custom models are expected to be already available
                ModelSetupResult {
                    model_id: model.id.clone(),
                    success: true,
                    model_path: None,
                    error: None,
                }
            }
        }
    }

    /// Downloads a HuggingFace model.
    async fn setup_huggingface_model(
        &self,
        pod_id: &str,
        model: &ModelConfig,
    ) -> ModelSetupResult {
        let repo = match &model.repo {
            Some(r) => r,
            None => {
                return ModelSetupResult {
                    model_id: model.id.clone(),
                    success: false,
                    model_path: None,
                    error: Some("Missing 'repo' field for HuggingFace model".to_string()),
                };
            }
        };

        let model_path = format!("/root/.cache/huggingface/hub/models--{}", repo.replace('/', "--"));

        // Check if model already exists
        let check_cmd = format!("test -d '{}' && echo 'exists' || echo 'missing'", model_path);
        match self.execute_command(pod_id, &check_cmd, Some(30)).await {
            Ok(result) if result.stdout.trim() == "exists" => {
                info!("Model '{}' already exists on pod {}", model.id, pod_id);
                return ModelSetupResult {
                    model_id: model.id.clone(),
                    success: true,
                    model_path: Some(model_path),
                    error: None,
                };
            }
            _ => {}
        }

        // Download the model using huggingface-cli
        info!("Downloading model '{}' ({}) on pod {}", model.id, repo, pod_id);

        let download_cmd = format!(
            "huggingface-cli download {} --local-dir /models/{} 2>&1 || \
             python -c \"from huggingface_hub import snapshot_download; snapshot_download('{}')\" 2>&1",
            repo, model.id, repo
        );

        match self.execute_command(pod_id, &download_cmd, Some(MODEL_DOWNLOAD_TIMEOUT_SECS)).await {
            Ok(result) if result.success => {
                info!("Successfully downloaded model '{}' on pod {}", model.id, pod_id);
                ModelSetupResult {
                    model_id: model.id.clone(),
                    success: true,
                    model_path: Some(format!("/models/{}", model.id)),
                    error: None,
                }
            }
            Ok(result) => {
                error!("Failed to download model '{}': {}", model.id, result.stderr);
                ModelSetupResult {
                    model_id: model.id.clone(),
                    success: false,
                    model_path: None,
                    error: Some(result.stderr),
                }
            }
            Err(e) => {
                error!("Error downloading model '{}': {}", model.id, e);
                ModelSetupResult {
                    model_id: model.id.clone(),
                    success: false,
                    model_path: None,
                    error: Some(e.to_string()),
                }
            }
        }
    }

    /// Sets up a bundle of models/components.
    async fn setup_bundle_model(&self, pod_id: &str, model: &ModelConfig) -> ModelSetupResult {
        let components = match &model.components {
            Some(c) if !c.is_empty() => c,
            _ => {
                return ModelSetupResult {
                    model_id: model.id.clone(),
                    success: false,
                    model_path: None,
                    error: Some("Missing 'components' field for bundle model".to_string()),
                };
            }
        };

        info!("Setting up bundle '{}' with {} components", model.id, components.len());

        // Download each component
        for component in components {
            let cmd = format!(
                "huggingface-cli download {} 2>&1 || echo 'Failed to download {}'",
                component, component
            );
            
            if let Err(e) = self.execute_command(pod_id, &cmd, Some(MODEL_DOWNLOAD_TIMEOUT_SECS)).await {
                warn!("Failed to download component '{}': {}", component, e);
            }
        }

        ModelSetupResult {
            model_id: model.id.clone(),
            success: true,
            model_path: Some("/root/.cache/huggingface".to_string()),
            error: None,
        }
    }

    /// Starts an inference engine on the pod.
    ///
    /// # Errors
    ///
    /// Returns an error if the engine cannot be started.
    pub async fn start_inference_engine(
        &self,
        pod_id: &str,
        model: &ModelConfig,
        port: u16,
    ) -> Result<EngineStartResult> {
        let load_config = match &model.load {
            Some(c) => c,
            None => {
                return Ok(EngineStartResult {
                    engine: "none".to_string(),
                    success: true,
                    endpoint: None,
                    error: None,
                });
            }
        };

        let engine = load_config.engine.to_lowercase();
        info!("Starting {} engine for model '{}' on pod {}", engine, model.id, pod_id);

        match engine.as_str() {
            "vllm" => self.start_vllm(pod_id, model, load_config, port).await,
            "tgi" | "text-generation-inference" => {
                self.start_tgi(pod_id, model, load_config, port).await
            }
            "ollama" => self.start_ollama(pod_id, model, port).await,
            "transformers" => {
                // No server to start, just verify the model is loadable
                Ok(EngineStartResult {
                    engine: engine.clone(),
                    success: true,
                    endpoint: None,
                    error: None,
                })
            }
            other => {
                warn!("Unknown engine '{}', skipping auto-start", other);
                Ok(EngineStartResult {
                    engine: other.to_string(),
                    success: true,
                    endpoint: None,
                    error: Some(format!("Unknown engine '{}', manual start required", other)),
                })
            }
        }
    }

    /// Starts vLLM server.
    async fn start_vllm(
        &self,
        pod_id: &str,
        model: &ModelConfig,
        load_config: &LoadConfig,
        port: u16,
    ) -> Result<EngineStartResult> {
        let repo = model.repo.as_deref().unwrap_or(&model.id);
        
        let mut cmd_parts = vec![
            "nohup python -m vllm.entrypoints.openai.api_server".to_string(),
            format!("--model {}", repo),
            format!("--port {}", port),
            "--host 0.0.0.0".to_string(),
        ];

        // Add quantization if specified
        if let Some(quant) = &load_config.quant {
            let quant_arg = match quant.to_lowercase().as_str() {
                "awq" => "--quantization awq",
                "gptq" => "--quantization gptq",
                "squeezellm" => "--quantization squeezellm",
                "fp8" => "--quantization fp8",
                _ => "",
            };
            if !quant_arg.is_empty() {
                cmd_parts.push(quant_arg.to_string());
            }
        }

        // Add max sequence length
        if let Some(max_len) = load_config.max_seq_len {
            cmd_parts.push(format!("--max-model-len {}", max_len));
        }

        // Add any extra options
        for (key, value) in &load_config.options {
            if let Some(v) = value.as_str() {
                cmd_parts.push(format!("--{} {}", key, v));
            } else if let Some(v) = value.as_bool() {
                if v {
                    cmd_parts.push(format!("--{}", key));
                }
            } else if let Some(v) = value.as_i64() {
                cmd_parts.push(format!("--{} {}", key, v));
            }
        }

        cmd_parts.push("> /var/log/vllm.log 2>&1 &".to_string());

        let cmd = cmd_parts.join(" ");
        info!("Starting vLLM: {}", cmd);

        match self.execute_command(pod_id, &cmd, Some(60)).await {
            Ok(_) => {
                // Wait a bit for the server to start
                tokio::time::sleep(Duration::from_secs(10)).await;

                // Verify it's running
                let check_cmd = "pgrep -f 'vllm.entrypoints' || echo 'not running'";
                match self.execute_command(pod_id, check_cmd, Some(30)).await {
                    Ok(result) if !result.stdout.contains("not running") => {
                        info!("vLLM started successfully on pod {}", pod_id);
                        Ok(EngineStartResult {
                            engine: "vllm".to_string(),
                            success: true,
                            endpoint: Some(format!("http://localhost:{}", port)),
                            error: None,
                        })
                    }
                    _ => {
                        // Check logs for errors
                        let log_cmd = "tail -50 /var/log/vllm.log 2>/dev/null || echo 'No logs'";
                        let logs = self.execute_command(pod_id, log_cmd, Some(30)).await
                            .map(|r| r.stdout)
                            .unwrap_or_default();
                        
                        Ok(EngineStartResult {
                            engine: "vllm".to_string(),
                            success: false,
                            endpoint: None,
                            error: Some(format!("vLLM failed to start. Logs: {}", logs)),
                        })
                    }
                }
            }
            Err(e) => Ok(EngineStartResult {
                engine: "vllm".to_string(),
                success: false,
                endpoint: None,
                error: Some(e.to_string()),
            }),
        }
    }

    /// Starts Text Generation Inference server.
    async fn start_tgi(
        &self,
        pod_id: &str,
        model: &ModelConfig,
        load_config: &LoadConfig,
        port: u16,
    ) -> Result<EngineStartResult> {
        let repo = model.repo.as_deref().unwrap_or(&model.id);

        let mut cmd_parts = vec![
            "nohup text-generation-launcher".to_string(),
            format!("--model-id {}", repo),
            format!("--port {}", port),
            "--hostname 0.0.0.0".to_string(),
        ];

        // Add quantization
        if let Some(quant) = &load_config.quant {
            cmd_parts.push(format!("--quantize {}", quant));
        }

        // Add max sequence length
        if let Some(max_len) = load_config.max_seq_len {
            cmd_parts.push(format!("--max-input-length {}", max_len));
        }

        cmd_parts.push("> /var/log/tgi.log 2>&1 &".to_string());

        let cmd = cmd_parts.join(" ");
        info!("Starting TGI: {}", cmd);

        match self.execute_command(pod_id, &cmd, Some(60)).await {
            Ok(_) => {
                tokio::time::sleep(Duration::from_secs(10)).await;

                let check_cmd = "pgrep -f 'text-generation-launcher' || echo 'not running'";
                match self.execute_command(pod_id, check_cmd, Some(30)).await {
                    Ok(result) if !result.stdout.contains("not running") => {
                        Ok(EngineStartResult {
                            engine: "tgi".to_string(),
                            success: true,
                            endpoint: Some(format!("http://localhost:{}", port)),
                            error: None,
                        })
                    }
                    _ => Ok(EngineStartResult {
                        engine: "tgi".to_string(),
                        success: false,
                        endpoint: None,
                        error: Some("TGI failed to start".to_string()),
                    }),
                }
            }
            Err(e) => Ok(EngineStartResult {
                engine: "tgi".to_string(),
                success: false,
                endpoint: None,
                error: Some(e.to_string()),
            }),
        }
    }

    /// Starts Ollama server.
    async fn start_ollama(
        &self,
        pod_id: &str,
        model: &ModelConfig,
        port: u16,
    ) -> Result<EngineStartResult> {
        // Start Ollama server
        let start_cmd = format!(
            "nohup ollama serve > /var/log/ollama.log 2>&1 & sleep 5 && ollama pull {}",
            model.id
        );

        match self.execute_command(pod_id, &start_cmd, Some(300)).await {
            Ok(result) if result.success => {
                Ok(EngineStartResult {
                    engine: "ollama".to_string(),
                    success: true,
                    endpoint: Some(format!("http://localhost:{}", port)),
                    error: None,
                })
            }
            Ok(result) => Ok(EngineStartResult {
                engine: "ollama".to_string(),
                success: false,
                endpoint: None,
                error: Some(result.stderr),
            }),
            Err(e) => Ok(EngineStartResult {
                engine: "ollama".to_string(),
                success: false,
                endpoint: None,
                error: Some(e.to_string()),
            }),
        }
    }

    /// Performs full post-provisioning setup for a pod.
    ///
    /// This includes:
    /// 1. Waiting for the pod to be ready
    /// 2. Downloading and setting up models
    /// 3. Starting inference engines
    ///
    /// # Errors
    ///
    /// Returns an error if setup fails critically.
    pub async fn post_provision_setup(
        &self,
        pod_id: &str,
        pod_config: &PodConfig,
    ) -> Result<PostProvisionResult> {
        info!("Starting post-provisioning setup for pod {}", pod_id);

        // Wait for pod to be ready
        self.wait_for_ready(pod_id, 300).await?;

        // Setup models
        let model_results = self.setup_models(pod_id, &pod_config.models).await?;

        // Start engines for each model that has a load config
        let mut engine_results = Vec::new();
        
        // Get the primary HTTP port
        let port = pod_config
            .http_ports()
            .first()
            .copied()
            .unwrap_or(8000);

        for model in &pod_config.models {
            if model.load.is_some() {
                let result = self.start_inference_engine(pod_id, model, port).await?;
                engine_results.push(result);
            }
        }

        let success = model_results.iter().all(|r| r.success)
            && engine_results.iter().all(|r| r.success);

        Ok(PostProvisionResult {
            pod_id: pod_id.to_string(),
            success,
            model_results,
            engine_results,
        })
    }
}

/// Result of post-provisioning setup.
#[derive(Debug, Clone)]
pub struct PostProvisionResult {
    /// Pod ID.
    pub pod_id: String,
    /// Overall success status.
    pub success: bool,
    /// Individual model setup results.
    pub model_results: Vec<ModelSetupResult>,
    /// Individual engine startup results.
    pub engine_results: Vec<EngineStartResult>,
}

impl PostProvisionResult {
    /// Returns a summary of the setup.
    #[must_use]
    pub fn summary(&self) -> String {
        let models_ok = self.model_results.iter().filter(|r| r.success).count();
        let models_total = self.model_results.len();
        let engines_ok = self.engine_results.iter().filter(|r| r.success).count();
        let engines_total = self.engine_results.len();

        format!(
            "Pod {}: Models {}/{} OK, Engines {}/{} OK",
            self.pod_id, models_ok, models_total, engines_ok, engines_total
        )
    }
}