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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
//! `RunPod` API client implementation.
//!
//! This module provides the HTTP client for interacting with the `RunPod` GraphQL API.

use reqwest::{header, Client};
use serde::{Deserialize, Serialize};
use std::time::Duration;
use tracing::{debug, trace};

use crate::error::{HalldyllError, Result, RunPodError};

use super::types::{CreatePodRequest, GpuType, Pod, UpdatePodRequest};

/// `RunPod` API base URL.
const RUNPOD_API_URL: &str = "https://api.runpod.io/graphql";

/// Default request timeout in seconds.
const DEFAULT_TIMEOUT_SECS: u64 = 30;

/// Maximum number of retries for transient failures.
const MAX_RETRIES: u32 = 3;

/// Delay between retries in milliseconds.
const RETRY_DELAY_MS: u64 = 1000;

/// `RunPod` API client.
#[derive(Debug, Clone)]
pub struct RunPodClient {
    /// HTTP client.
    client: Client,
    /// API key.
    api_key: String,
}

/// GraphQL request structure.
#[derive(Debug, Serialize)]
struct GraphQLRequest {
    query: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    variables: Option<serde_json::Value>,
}

/// GraphQL response structure.
#[derive(Debug, Deserialize)]
struct GraphQLResponse<T> {
    data: Option<T>,
    errors: Option<Vec<GraphQLError>>,
}

/// GraphQL error structure.
#[derive(Debug, Deserialize)]
struct GraphQLError {
    message: String,
}

impl RunPodClient {
    /// Creates a new `RunPod` API client.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP client cannot be created.
    pub fn new(api_key: &str) -> Result<Self> {
        let client = Client::builder()
            .timeout(Duration::from_secs(DEFAULT_TIMEOUT_SECS))
            .build()
            .map_err(|e| RunPodError::network(format!("Failed to create HTTP client: {e}")))?;

        Ok(Self {
            client,
            api_key: api_key.to_string(),
        })
    }

    /// Creates a client with a custom timeout.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP client cannot be created.
    pub fn with_timeout(api_key: &str, timeout_secs: u64) -> Result<Self> {
        let client = Client::builder()
            .timeout(Duration::from_secs(timeout_secs))
            .build()
            .map_err(|e| RunPodError::network(format!("Failed to create HTTP client: {e}")))?;

        Ok(Self {
            client,
            api_key: api_key.to_string(),
        })
    }

    /// Executes a GraphQL query.
    async fn execute<T: for<'de> Deserialize<'de>>(
        &self,
        query: &str,
        variables: Option<serde_json::Value>,
    ) -> Result<T> {
        let request = GraphQLRequest {
            query: query.to_string(),
            variables,
        };

        let mut last_error = None;

        for attempt in 0..MAX_RETRIES {
            if attempt > 0 {
                debug!("Retry attempt {attempt} of {MAX_RETRIES}");
                tokio::time::sleep(Duration::from_millis(RETRY_DELAY_MS * u64::from(attempt)))
                    .await;
            }

            match self.execute_once::<T>(&request).await {
                Ok(result) => return Ok(result),
                Err(e) => {
                    if e.is_retryable() {
                        last_error = Some(e);
                        continue;
                    }
                    return Err(e);
                }
            }
        }

        Err(last_error.unwrap_or_else(|| {
            HalldyllError::RunPod(RunPodError::NetworkError {
                message: String::from("Max retries exceeded"),
            })
        }))
    }

    /// Executes a single GraphQL request.
    async fn execute_once<T: for<'de> Deserialize<'de>>(
        &self,
        request: &GraphQLRequest,
    ) -> Result<T> {
        trace!("Executing GraphQL query: {}", request.query);

        let response = self
            .client
            .post(RUNPOD_API_URL)
            .header(header::CONTENT_TYPE, "application/json")
            .header(header::AUTHORIZATION, format!("Bearer {}", self.api_key))
            .json(request)
            .send()
            .await
            .map_err(|e| {
                HalldyllError::RunPod(RunPodError::NetworkError {
                    message: format!("Request failed: {e}"),
                })
            })?;

        let status = response.status();

        if status.as_u16() == 429 {
            let retry_after = response
                .headers()
                .get("retry-after")
                .and_then(|v| v.to_str().ok())
                .and_then(|s| s.parse().ok())
                .unwrap_or_default();
            let retry_after = if retry_after == 0 { 60 } else { retry_after };

            return Err(HalldyllError::RunPod(RunPodError::RateLimited {
                retry_after_secs: retry_after,
            }));
        }

        if status.as_u16() == 401 || status.as_u16() == 403 {
            return Err(HalldyllError::RunPod(RunPodError::AuthenticationFailed {
                message: String::from("Invalid API key"),
            }));
        }

        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(HalldyllError::RunPod(RunPodError::api_error(
                status.as_u16(),
                body,
            )));
        }

        let gql_response: GraphQLResponse<T> = response.json().await.map_err(|e| {
            HalldyllError::RunPod(RunPodError::InvalidResponse {
                message: format!("Failed to parse response: {e}"),
            })
        })?;

        if let Some(errors) = gql_response.errors.filter(|e| !e.is_empty()) {
            let message = errors
                .iter()
                .map(|e| e.message.as_str())
                .collect::<Vec<_>>()
                .join("; ");
            return Err(HalldyllError::RunPod(RunPodError::api_error(400, message)));
        }

        gql_response.data.ok_or_else(|| {
            HalldyllError::RunPod(RunPodError::InvalidResponse {
                message: String::from("No data in response"),
            })
        })
    }

    /// Lists all pods.
    ///
    /// # Errors
    ///
    /// Returns an error if the API call fails.
    pub async fn list_pods(&self) -> Result<Vec<Pod>> {
        #[derive(Deserialize)]
        struct Response {
            myself: MyselfResponse,
        }
        #[derive(Deserialize)]
        struct MyselfResponse {
            pods: Vec<Pod>,
        }

        let query = r"
            query {
                myself {
                    pods {
                        id
                        name
                        desiredStatus
                        imageName
                        gpuCount
                        volumeInGb
                        containerDiskInGb
                        memoryInGb
                        vcpuCount
                        ports
                        machine {
                            gpuTypeId
                        }
                        runtime {
                            ports {
                                ip
                                privatePort
                                publicPort
                                type
                            }
                            gpus {
                                id
                                gpuUtilizationPercent
                                memoryUtilizationPercent
                            }
                            uptimeInSeconds
                        }
                        env {
                            key
                            value
                        }
                    }
                }
            }
        ";

        let response: Response = self.execute(query, None).await?;
        Ok(response.myself.pods)
    }

    /// Gets a pod by ID.
    ///
    /// # Errors
    ///
    /// Returns an error if the pod is not found or the API call fails.
    pub async fn get_pod(&self, pod_id: &str) -> Result<Pod> {
        #[derive(Deserialize)]
        struct Response {
            pod: Option<Pod>,
        }

        let query = r"
            query Pod($podId: String!) {
                pod(input: { podId: $podId }) {
                    id
                    name
                    desiredStatus
                    imageName
                    gpuCount
                    volumeInGb
                    containerDiskInGb
                    memoryInGb
                    vcpuCount
                    ports
                    machine {
                        gpuTypeId
                    }
                    runtime {
                        ports {
                            ip
                            privatePort
                            publicPort
                            type
                        }
                        gpus {
                            id
                            gpuUtilizationPercent
                            memoryUtilizationPercent
                        }
                        uptimeInSeconds
                    }
                    env {
                        key
                        value
                    }
                }
            }
        ";

        let variables = serde_json::json!({ "podId": pod_id });
        let response: Response = self.execute(query, Some(variables)).await?;

        response.pod.ok_or_else(|| {
            HalldyllError::RunPod(RunPodError::PodNotFound {
                pod_id: pod_id.to_string(),
            })
        })
    }

    /// Creates a new pod.
    ///
    /// # Errors
    ///
    /// Returns an error if the pod cannot be created.
    pub async fn create_pod(&self, request: &CreatePodRequest) -> Result<Pod> {
        #[derive(Deserialize)]
        struct Response {
            #[serde(rename = "podFindAndDeployOnDemand")]
            pod: Pod,
        }

        let query = r"
            mutation CreatePod($input: PodFindAndDeployOnDemandInput!) {
                podFindAndDeployOnDemand(input: $input) {
                    id
                    name
                    desiredStatus
                    imageName
                    gpuCount
                    volumeInGb
                    containerDiskInGb
                    memoryInGb
                    vcpuCount
                    ports
                    machine {
                        gpuTypeId
                    }
                    env {
                        key
                        value
                    }
                }
            }
        ";

        let input = serde_json::json!({
            "cloudType": request.cloud_type,
            "gpuTypeId": request.gpu_type_id,
            "gpuCount": request.gpu_count,
            "name": request.name,
            "imageName": request.image_name,
            "volumeInGb": request.volume_in_gb,
            "containerDiskInGb": request.container_disk_in_gb,
            "volumeMountPath": request.volume_mount_path,
            "ports": request.ports,
            "env": request.env.iter().map(|e| {
                serde_json::json!({ "key": e.key, "value": e.value })
            }).collect::<Vec<_>>(),
            "dockerArgs": request.docker_args,
            "dataCenterId": request.data_center_id,
        });

        let variables = serde_json::json!({ "input": input });
        let response: Response = self.execute(query, Some(variables)).await?;

        Ok(response.pod)
    }

    /// Stops a pod.
    ///
    /// # Errors
    ///
    /// Returns an error if the pod cannot be stopped.
    pub async fn stop_pod(&self, pod_id: &str) -> Result<()> {
        #[derive(Deserialize)]
        struct Response {
            #[serde(rename = "podStop")]
            _pod: Option<serde_json::Value>,
        }

        let query = r"
            mutation StopPod($podId: String!) {
                podStop(input: { podId: $podId }) {
                    id
                }
            }
        ";

        let variables = serde_json::json!({ "podId": pod_id });
        let _: Response = self.execute(query, Some(variables)).await?;

        Ok(())
    }

    /// Resumes a stopped pod.
    ///
    /// # Errors
    ///
    /// Returns an error if the pod cannot be resumed.
    pub async fn resume_pod(&self, pod_id: &str) -> Result<Pod> {
        #[derive(Deserialize)]
        struct Response {
            #[serde(rename = "podResume")]
            pod: Pod,
        }

        let query = r"
            mutation ResumePod($podId: String!) {
                podResume(input: { podId: $podId }) {
                    id
                    name
                    desiredStatus
                    imageName
                    gpuCount
                }
            }
        ";

        let variables = serde_json::json!({ "podId": pod_id });
        let response: Response = self.execute(query, Some(variables)).await?;

        Ok(response.pod)
    }

    /// Terminates (deletes) a pod.
    ///
    /// # Errors
    ///
    /// Returns an error if the pod cannot be terminated.
    pub async fn terminate_pod(&self, pod_id: &str) -> Result<()> {
        #[derive(Deserialize)]
        struct Response {
            #[serde(rename = "podTerminate")]
            _result: Option<serde_json::Value>,
        }

        let query = r"
            mutation TerminatePod($podId: String!) {
                podTerminate(input: { podId: $podId })
            }
        ";

        let variables = serde_json::json!({ "podId": pod_id });
        let _: Response = self.execute(query, Some(variables)).await?;

        Ok(())
    }

    /// Updates a pod's configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the pod cannot be updated.
    pub async fn update_pod(&self, request: &UpdatePodRequest) -> Result<Pod> {
        #[derive(Deserialize)]
        struct Response {
            #[serde(rename = "podEditJob")]
            pod: Pod,
        }

        let query = r"
            mutation UpdatePod($input: PodEditJobInput!) {
                podEditJob(input: $input) {
                    id
                    name
                    desiredStatus
                    imageName
                }
            }
        ";

        let mut input = serde_json::json!({ "podId": request.pod_id });

        if let Some(image) = &request.image_name {
            input["imageName"] = serde_json::json!(image);
        }

        if let Some(env) = &request.env {
            input["env"] = serde_json::json!(env
                .iter()
                .map(|e| serde_json::json!({ "key": e.key, "value": e.value }))
                .collect::<Vec<_>>());
        }

        let variables = serde_json::json!({ "input": input });
        let response: Response = self.execute(query, Some(variables)).await?;

        Ok(response.pod)
    }

    /// Gets available GPU types.
    ///
    /// # Errors
    ///
    /// Returns an error if the API call fails.
    pub async fn list_gpu_types(&self) -> Result<Vec<GpuType>> {
        #[derive(Deserialize)]
        struct Response {
            #[serde(rename = "gpuTypes")]
            gpu_types: Vec<GpuType>,
        }

        let query = r"
            query {
                gpuTypes {
                    id
                    displayName
                    memoryInGb
                    secureCloud
                    communityCloud
                    securePrice
                    communityPrice
                }
            }
        ";

        let response: Response = self.execute(query, None).await?;
        Ok(response.gpu_types)
    }

    /// Checks if a GPU type is available.
    ///
    /// # Errors
    ///
    /// Returns an error if the API call fails.
    pub async fn is_gpu_available(&self, gpu_type_id: &str, cloud_type: &str) -> Result<bool> {
        let gpu_types = self.list_gpu_types().await?;

        for gpu in gpu_types {
            if gpu.id == gpu_type_id || gpu.display_name == gpu_type_id {
                return Ok(match cloud_type {
                    "SECURE" => gpu.secure_cloud,
                    "COMMUNITY" => gpu.community_cloud,
                    _ => false,
                });
            }
        }

        Ok(false)
    }

    /// Validates the API key by making a test request.
    ///
    /// # Errors
    ///
    /// Returns an error if the API key is invalid.
    pub async fn validate_api_key(&self) -> Result<bool> {
        #[derive(Deserialize)]
        struct Response {
            #[serde(rename = "myself")]
            _myself: MyselfResponse,
        }
        #[derive(Deserialize)]
        struct MyselfResponse {
            #[serde(rename = "id")]
            _id: String,
        }

        let query = r"
            query {
                myself {
                    id
                }
            }
        ";

        match self.execute::<Response>(query, None).await {
            Ok(_) => Ok(true),
            Err(HalldyllError::RunPod(RunPodError::AuthenticationFailed { .. })) => Ok(false),
            Err(e) => Err(e),
        }
    }

    /// Executes a command on a running pod using RunPod's exec API.
    ///
    /// # Errors
    ///
    /// Returns an error if the command cannot be executed.
    pub async fn exec_command(
        &self,
        pod_id: &str,
        command: &str,
        timeout_secs: u64,
    ) -> Result<super::executor::CommandResult> {
        use super::executor::CommandResult;

        // RunPod uses a REST API for pod exec, not GraphQL
        let _exec_url = format!(
            "https://api.runpod.ai/v2/{}/run",
            pod_id
        );

        #[derive(Serialize)]
        struct ExecRequest {
            input: ExecInput,
        }

        #[derive(Serialize)]
        struct ExecInput {
            command: String,
        }

        #[derive(Deserialize)]
        struct ExecResponse {
            id: Option<String>,
            #[allow(dead_code)]
            status: Option<String>,
            output: Option<ExecOutput>,
            error: Option<String>,
        }

        #[derive(Deserialize)]
        struct ExecOutput {
            stdout: Option<String>,
            stderr: Option<String>,
            exit_code: Option<i32>,
        }

        // First, try using the runsync endpoint for immediate execution
        let runsync_url = format!(
            "https://api.runpod.ai/v2/{}/runsync",
            pod_id
        );

        let request = ExecRequest {
            input: ExecInput {
                command: command.to_string(),
            },
        };

        let response = self
            .client
            .post(&runsync_url)
            .header(header::CONTENT_TYPE, "application/json")
            .header(header::AUTHORIZATION, format!("Bearer {}", self.api_key))
            .timeout(Duration::from_secs(timeout_secs))
            .json(&request)
            .send()
            .await
            .map_err(|e| {
                HalldyllError::RunPod(RunPodError::NetworkError {
                    message: format!("Exec request failed: {e}"),
                })
            })?;

        if !response.status().is_success() {
            // Fallback: try SSH-style exec via the pod's SSH port
            // This requires the pod to have SSH enabled and accessible
            return self.exec_via_ssh(pod_id, command).await;
        }

        let exec_response: ExecResponse = response.json().await.map_err(|e| {
            HalldyllError::RunPod(RunPodError::InvalidResponse {
                message: format!("Failed to parse exec response: {e}"),
            })
        })?;

        if let Some(error) = exec_response.error {
            return Ok(CommandResult {
                success: false,
                stdout: String::new(),
                stderr: error,
                exit_code: Some(1),
            });
        }

        if let Some(output) = exec_response.output {
            let exit_code = output.exit_code.unwrap_or(0);
            return Ok(CommandResult {
                success: exit_code == 0,
                stdout: output.stdout.unwrap_or_default(),
                stderr: output.stderr.unwrap_or_default(),
                exit_code: Some(exit_code),
            });
        }

        // If we got a job ID, we need to poll for results
        if let Some(_job_id) = exec_response.id {
            // For now, assume it succeeded if we got this far
            Ok(CommandResult {
                success: true,
                stdout: String::new(),
                stderr: String::new(),
                exit_code: None,
            })
        } else {
            Ok(CommandResult {
                success: false,
                stdout: String::new(),
                stderr: "No output or job ID received".to_string(),
                exit_code: None,
            })
        }
    }

    /// Executes a command via SSH on a pod.
    /// This is a fallback when the RunPod exec API is not available.
    async fn exec_via_ssh(
        &self,
        pod_id: &str,
        command: &str,
    ) -> Result<super::executor::CommandResult> {
        use super::executor::CommandResult;

        // Get pod details to find SSH endpoint
        let pod = self.get_pod(pod_id).await?;

        // For RunPod pods, commands can be executed through the web terminal API
        // or by connecting to the pod's public IP if SSH is enabled
        
        // Check if pod has SSH port exposed
        let ssh_available = pod.ports.as_ref()
            .map(|p| p.contains("22"))
            .unwrap_or(false);

        if !ssh_available {
            return Ok(CommandResult {
                success: false,
                stdout: String::new(),
                stderr: "SSH not available on this pod. Enable port 22/tcp in your config.".to_string(),
                exit_code: Some(1),
            });
        }

        // Note: Actual SSH execution would require an SSH library like thrussh or openssh
        // For now, we'll use RunPod's web-based execution method
        
        // RunPod provides a way to execute commands via their internal API
        // This endpoint may vary based on pod type
        let _internal_exec_url = format!(
            "https://api.runpod.io/graphql"
        );

        #[derive(Deserialize)]
        struct ExecResponse {
            #[serde(rename = "podExec")]
            pod_exec: Option<PodExecResult>,
        }

        #[derive(Deserialize)]
        struct PodExecResult {
            stdout: Option<String>,
            stderr: Option<String>,
            #[serde(rename = "exitCode")]
            exit_code: Option<i32>,
        }

        let query = r#"
            mutation PodExec($podId: String!, $command: String!) {
                podExec(input: { podId: $podId, command: $command }) {
                    stdout
                    stderr
                    exitCode
                }
            }
        "#;

        let variables = serde_json::json!({
            "podId": pod_id,
            "command": command
        });

        match self.execute::<ExecResponse>(query, Some(variables)).await {
            Ok(response) => {
                if let Some(result) = response.pod_exec {
                    let exit_code = result.exit_code.unwrap_or(0);
                    Ok(CommandResult {
                        success: exit_code == 0,
                        stdout: result.stdout.unwrap_or_default(),
                        stderr: result.stderr.unwrap_or_default(),
                        exit_code: Some(exit_code),
                    })
                } else {
                    Ok(CommandResult {
                        success: false,
                        stdout: String::new(),
                        stderr: "No exec result returned".to_string(),
                        exit_code: None,
                    })
                }
            }
            Err(_) => {
                // If GraphQL exec fails, the pod might not support it
                // Return a helpful message
                Ok(CommandResult {
                    success: false,
                    stdout: String::new(),
                    stderr: format!(
                        "Command execution not available. Ensure the pod is running and supports exec. \
                        Command attempted: {}",
                        command
                    ),
                    exit_code: Some(1),
                })
            }
        }
    }
}