blueprint-manager 0.4.0-alpha.3

Tangle Blueprint manager and Runner
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
//! Remote provider integration for Blueprint Manager
//!
//! Handles automatic cloud deployment when services are initiated

use crate::config::BlueprintManagerContext;
use crate::error::{Error, Result};
use blueprint_core::info;
use blueprint_remote_providers::deployment::DeploymentType;
use blueprint_remote_providers::deployment::manager_integration::{
    RemoteDeploymentConfig, RemoteDeploymentRegistry, TtlManager,
};
use blueprint_remote_providers::{
    CloudProvider, CloudProvisioner, DeploymentTracker, ResourceSpec,
};
use blueprint_std::collections::HashMap;
use blueprint_std::sync::Arc;
use chrono::Utc;

fn env_bool(name: &str) -> bool {
    std::env::var(name)
        .ok()
        .map(|value| {
            matches!(
                value.trim().to_ascii_lowercase().as_str(),
                "1" | "true" | "yes"
            )
        })
        .unwrap_or(false)
}

fn supports_tee(provider: &CloudProvider) -> bool {
    matches!(
        provider,
        CloudProvider::AWS | CloudProvider::GCP | CloudProvider::Azure
    )
}

/// What to deploy onto a provisioned VM.
///
/// Container images go through `docker pull` + `docker run`.
/// GitHub-release binaries go through `wget` + sha256 verify + systemd.
#[derive(Debug, Clone, Copy)]
pub enum DeployArtifact<'a> {
    /// A container image reference like `ghcr.io/tangle-network/llm-operator:v0.1.0`.
    ContainerImage(&'a str),
    /// A platform-specific binary archive from a GitHub release (or any URL).
    ///
    /// The archive is downloaded on the VM, its sha256 is verified, and the
    /// named binary is extracted + started as a systemd service.
    GithubBinary {
        archive_url: &'a str,
        sha256_hex: &'a str,
        binary_name: &'a str,
    },
}

/// Remote provider manager that handles cloud deployments
pub struct RemoteProviderManager {
    provisioner: Arc<CloudProvisioner>,
    registry: Arc<RemoteDeploymentRegistry>,
    ttl_manager: Arc<TtlManager>,
    provider_regions: HashMap<CloudProvider, String>,
    enabled: bool,
}

impl RemoteProviderManager {
    /// Initialize from Blueprint Manager config
    pub async fn new(ctx: &BlueprintManagerContext) -> Result<Option<Self>> {
        // Check if remote providers are configured
        if !ctx
            .cloud_config()
            .as_ref()
            .is_some_and(|config| config.enabled)
        {
            info!("Remote cloud providers not configured");
            return Ok(None);
        }

        // Create deployment tracker
        let tracker_path = ctx.data_dir().join("remote_deployments");
        let tracker = Arc::new(DeploymentTracker::new(&tracker_path).await?);

        // Create registry and provisioner
        let registry = Arc::new(RemoteDeploymentRegistry::new(tracker.clone()));
        let provisioner = Arc::new(CloudProvisioner::new().await?);
        let provider_regions = configured_provider_regions(ctx);

        // Create TTL manager for automatic cleanup
        let (expiry_tx, _expiry_rx) = tokio::sync::mpsc::unbounded_channel();
        let ttl_manager = Arc::new(TtlManager::new(registry.clone(), expiry_tx));

        Ok(Some(Self {
            provisioner,
            registry,
            ttl_manager,
            provider_regions,
            enabled: true,
        }))
    }

    /// Handle service initiated event. The artifact controls what gets
    /// deployed onto the provisioned VM:
    ///
    /// - `None` → VM is provisioned idle (operator deploys separately)
    /// - `Some(ContainerImage{..})` → `docker pull` + `docker run` via SSH
    /// - `Some(GithubBinary{..})` → `wget` + sha256 verify + systemd service
    ///
    /// Both artifact forms are first-class. Blueprint authors choose which
    /// to ship based on runtime needs (CUDA-heavy workloads ship containers;
    /// lighter workloads can ship plain binaries).
    pub async fn on_service_initiated(
        &self,
        blueprint_id: u64,
        service_id: u64,
        resource_requirements: Option<ResourceSpec>,
        artifact: Option<DeployArtifact<'_>>,
        extra_env: HashMap<String, String>,
    ) -> Result<()> {
        if !self.enabled {
            return Ok(());
        }

        let artifact_kind = match &artifact {
            Some(DeployArtifact::ContainerImage(_)) => "container",
            Some(DeployArtifact::GithubBinary { .. }) => "github_binary",
            None => "none",
        };
        info!(
            "Remote provider handling service initiation: blueprint={}, service={}, artifact={}",
            blueprint_id, service_id, artifact_kind
        );

        // Use provided resources or default
        let resource_spec = resource_requirements.unwrap_or_else(ResourceSpec::minimal);
        let tee_required = env_bool("BLUEPRINT_REMOTE_TEE_REQUIRED");

        // Select provider based on workload type. GPU workloads use a
        // GPU-first candidate list so operators who configured RunPod/Vast/Lambda
        // get those before falling back to GCP/AWS.
        let is_gpu = resource_spec.gpu_count.is_some();
        let preferred_provider = if tee_required {
            CloudProvider::AWS
        } else if is_gpu {
            CloudProvider::RunPod
        } else if resource_spec.cpu > 8.0 {
            CloudProvider::Vultr
        } else if resource_spec.memory_gb > 32.0 {
            CloudProvider::AWS
        } else {
            CloudProvider::DigitalOcean
        };
        let provider = self.select_configured_provider(preferred_provider, tee_required, is_gpu)?;

        // Use configured region when available.
        let region = self
            .provider_regions
            .get(&provider)
            .map(String::as_str)
            .unwrap_or_else(|| provider_default_region(&provider));

        if tee_required && !supports_tee(&provider) {
            return Err(Error::TeePrerequisiteMissing {
                prerequisite: format!("{provider} confidential-compute support"),
                hint: "Select AWS, GCP, or Azure when BLUEPRINT_REMOTE_TEE_REQUIRED=true"
                    .to_string(),
            });
        }

        let instance = self
            .provisioner
            .provision_with_requirements(provider.clone(), &resource_spec, region, tee_required)
            .await?;

        self.registry
            .register(
                blueprint_id,
                service_id,
                RemoteDeploymentConfig {
                    deployment_type: deployment_type_from_provider(&provider),
                    provider: Some(provider.clone()),
                    region: Some(region.to_string()),
                    instance_id: instance.id.clone(),
                    resource_spec: resource_spec.clone(),
                    ttl_seconds: Some(3600),
                    deployed_at: Utc::now(),
                },
            )
            .await;

        info!(
            "Remote VM provisioned on {}: instance={}",
            provider, instance.id
        );

        // Dispatch deploy based on artifact kind. Both container and native
        // binary paths are supported; blueprint authors choose at metadata time.
        match artifact {
            Some(DeployArtifact::ContainerImage(image)) => {
                let mut env_vars = extra_env;
                env_vars
                    .entry("BLUEPRINT_ID".to_string())
                    .or_insert_with(|| blueprint_id.to_string());
                env_vars
                    .entry("SERVICE_ID".to_string())
                    .or_insert_with(|| service_id.to_string());

                let deploy_result = self
                    .provisioner
                    .deploy_blueprint_to_instance(
                        &provider,
                        &instance,
                        image,
                        &resource_spec,
                        env_vars,
                    )
                    .await
                    .map_err(|e| Error::Other(format!("deploy_blueprint_to_instance: {e}")))?;
                info!(
                    blueprint_id,
                    service_id,
                    instance_id = %instance.id,
                    image,
                    deployed_id = %deploy_result.blueprint_id,
                    "Container deployed on remote VM (docker pull + run)"
                );
            }
            Some(DeployArtifact::GithubBinary {
                archive_url,
                sha256_hex,
                binary_name,
            }) => {
                let mut env_vars = extra_env;
                env_vars
                    .entry("BLUEPRINT_ID".to_string())
                    .or_insert_with(|| blueprint_id.to_string());
                env_vars
                    .entry("SERVICE_ID".to_string())
                    .or_insert_with(|| service_id.to_string());

                let deploy_result = self
                    .provisioner
                    .deploy_github_binary_to_instance(
                        &provider,
                        &instance,
                        archive_url,
                        sha256_hex,
                        binary_name,
                        &resource_spec,
                        env_vars,
                    )
                    .await
                    .map_err(|e| Error::Other(format!("deploy_github_binary_to_instance: {e}")))?;
                info!(
                    blueprint_id,
                    service_id,
                    instance_id = %instance.id,
                    archive_url,
                    binary_name,
                    deployed_id = %deploy_result.blueprint_id,
                    "Native binary deployed on remote VM (wget + sha256 + systemd)"
                );
            }
            None => {
                info!(
                    blueprint_id,
                    service_id,
                    instance_id = %instance.id,
                    "Remote VM provisioned idle (no deploy artifact supplied)"
                );
            }
        }

        self.ttl_manager
            .register_ttl(blueprint_id, service_id, 3600)
            .await; // 1 hour default

        Ok(())
    }

    /// Handle service terminated event
    pub async fn on_service_terminated(&self, blueprint_id: u64, service_id: u64) -> Result<()> {
        if !self.enabled {
            return Ok(());
        }

        info!(
            "Remote provider handling service termination: blueprint={}, service={}",
            blueprint_id, service_id
        );

        // Remove TTL registration for the terminated service
        self.ttl_manager
            .unregister_ttl(blueprint_id, service_id)
            .await;

        // Clean up deployment from registry
        self.registry.cleanup(blueprint_id, service_id).await?;

        Ok(())
    }
}

// Cloud configuration types are now imported from blueprint_remote_providers

impl RemoteProviderManager {
    fn select_configured_provider(
        &self,
        preferred: CloudProvider,
        tee_required: bool,
        is_gpu: bool,
    ) -> Result<CloudProvider> {
        let ordered_candidates = if tee_required {
            // TEE only on hyperscalers with confidential compute
            vec![
                preferred,
                CloudProvider::AWS,
                CloudProvider::GCP,
                CloudProvider::Azure,
            ]
        } else if is_gpu {
            // GPU workloads: GPU marketplaces first (cheapest), then
            // decentralized, then hyperscalers as fallback
            vec![
                preferred,
                CloudProvider::VastAi,
                CloudProvider::RunPod,
                CloudProvider::Fluidstack,
                CloudProvider::TensorDock,
                CloudProvider::LambdaLabs,
                CloudProvider::Paperspace,
                CloudProvider::CoreWeave,
                CloudProvider::Crusoe,
                CloudProvider::PrimeIntellect,
                // NOTE: Hetzner sells GPU-matrix dedicated servers, but that's
                // their Robot API / manual ordering flow — not the Cloud API
                // this adapter uses. Keep Hetzner in the CPU list only until
                // someone wires up the Robot API.
                CloudProvider::Akash,
                CloudProvider::IoNet,
                CloudProvider::Render,
                CloudProvider::BittensorLium,
                // Hyperscaler fallback (have GPUs but more expensive)
                CloudProvider::GCP,
                CloudProvider::AWS,
                CloudProvider::Azure,
            ]
        } else {
            // CPU workloads: cost-optimized first
            vec![
                preferred,
                CloudProvider::Hetzner,
                CloudProvider::Vultr,
                CloudProvider::DigitalOcean,
                CloudProvider::GCP,
                CloudProvider::AWS,
                CloudProvider::Azure,
            ]
        };

        for candidate in ordered_candidates {
            if self.provider_regions.contains_key(&candidate)
                && (!tee_required || supports_tee(&candidate))
            {
                return Ok(candidate);
            }
        }

        Err(Error::Other(
            "No configured cloud provider can satisfy deployment requirements".to_string(),
        ))
    }
}

fn deployment_type_from_provider(provider: &CloudProvider) -> DeploymentType {
    match provider {
        CloudProvider::AWS => DeploymentType::AwsEc2,
        CloudProvider::GCP => DeploymentType::GcpGce,
        CloudProvider::Azure => DeploymentType::AzureVm,
        CloudProvider::DigitalOcean => DeploymentType::DigitalOceanDroplet,
        CloudProvider::Vultr => DeploymentType::VultrInstance,
        CloudProvider::LambdaLabs => DeploymentType::LambdaLabsInstance,
        CloudProvider::RunPod => DeploymentType::RunPodInstance,
        CloudProvider::VastAi => DeploymentType::VastAiInstance,
        CloudProvider::CoreWeave => DeploymentType::CoreWeaveWorkload,
        CloudProvider::Paperspace => DeploymentType::PaperspaceMachine,
        CloudProvider::Fluidstack => DeploymentType::FluidstackServer,
        CloudProvider::TensorDock => DeploymentType::TensorDockServer,
        CloudProvider::Akash => DeploymentType::AkashLease,
        CloudProvider::IoNet => DeploymentType::IoNetCluster,
        CloudProvider::PrimeIntellect => DeploymentType::PrimeIntellectPod,
        CloudProvider::Render => DeploymentType::RenderDispersedNode,
        CloudProvider::BittensorLium => DeploymentType::BittensorLiumMiner,
        CloudProvider::Hetzner => DeploymentType::HetznerServer,
        CloudProvider::Crusoe => DeploymentType::CrusoeVm,
        _ => DeploymentType::SshRemote,
    }
}

fn configured_provider_regions(ctx: &BlueprintManagerContext) -> HashMap<CloudProvider, String> {
    let mut regions = HashMap::new();
    if let Some(config) = ctx.cloud_config() {
        if let Some(aws) = &config.aws {
            if aws.enabled {
                regions.insert(CloudProvider::AWS, aws.region.clone());
            }
        }
        if let Some(gcp) = &config.gcp {
            if gcp.enabled {
                regions.insert(CloudProvider::GCP, gcp.region.clone());
            }
        }
        if let Some(azure) = &config.azure {
            if azure.enabled {
                regions.insert(CloudProvider::Azure, azure.region.clone());
            }
        }
        if let Some(do_cfg) = &config.digital_ocean {
            if do_cfg.enabled {
                regions.insert(CloudProvider::DigitalOcean, do_cfg.region.clone());
            }
        }
        if let Some(vultr) = &config.vultr {
            if vultr.enabled {
                regions.insert(CloudProvider::Vultr, vultr.region.clone());
            }
        }
        if let Some(cfg) = &config.lambda_labs {
            if cfg.enabled {
                regions.insert(CloudProvider::LambdaLabs, cfg.region.clone());
            }
        }
        if let Some(cfg) = &config.runpod {
            if cfg.enabled {
                regions.insert(CloudProvider::RunPod, cfg.region.clone());
            }
        }
        if let Some(cfg) = &config.vast_ai {
            if cfg.enabled {
                regions.insert(CloudProvider::VastAi, "global".to_string());
            }
        }
        if let Some(cfg) = &config.coreweave {
            if cfg.enabled {
                regions.insert(CloudProvider::CoreWeave, cfg.region.clone());
            }
        }
        if let Some(cfg) = &config.paperspace {
            if cfg.enabled {
                regions.insert(CloudProvider::Paperspace, cfg.region.clone());
            }
        }
        if let Some(cfg) = &config.fluidstack {
            if cfg.enabled {
                regions.insert(CloudProvider::Fluidstack, cfg.region.clone());
            }
        }
        if let Some(cfg) = &config.tensordock {
            if cfg.enabled {
                regions.insert(CloudProvider::TensorDock, cfg.region.clone());
            }
        }
        if let Some(cfg) = &config.akash {
            if cfg.enabled {
                regions.insert(CloudProvider::Akash, "global".to_string());
            }
        }
        if let Some(cfg) = &config.io_net {
            if cfg.enabled {
                regions.insert(CloudProvider::IoNet, cfg.region.clone());
            }
        }
        if let Some(cfg) = &config.prime_intellect {
            if cfg.enabled {
                regions.insert(CloudProvider::PrimeIntellect, cfg.region.clone());
            }
        }
        if let Some(cfg) = &config.render {
            if cfg.enabled {
                regions.insert(CloudProvider::Render, cfg.region.clone());
            }
        }
        if let Some(cfg) = &config.bittensor_lium {
            if cfg.enabled {
                regions.insert(CloudProvider::BittensorLium, "global".to_string());
            }
        }
        if let Some(cfg) = &config.hetzner {
            if cfg.enabled {
                regions.insert(CloudProvider::Hetzner, cfg.region.clone());
            }
        }
        if let Some(cfg) = &config.crusoe {
            if cfg.enabled {
                regions.insert(CloudProvider::Crusoe, cfg.region.clone());
            }
        }
    }
    regions
}

fn provider_default_region(provider: &CloudProvider) -> &'static str {
    match provider {
        CloudProvider::AWS => "us-east-1",
        CloudProvider::GCP => "us-central1",
        CloudProvider::Azure => "eastus",
        CloudProvider::DigitalOcean => "nyc3",
        CloudProvider::Vultr => "ewr",
        CloudProvider::LambdaLabs => "us-west-1",
        CloudProvider::RunPod => "US",
        CloudProvider::VastAi => "global",
        CloudProvider::CoreWeave => "ORD1",
        CloudProvider::Paperspace => "NY2",
        CloudProvider::Fluidstack => "us-east",
        CloudProvider::TensorDock => "us-central",
        CloudProvider::Akash => "global",
        CloudProvider::IoNet => "us-east",
        CloudProvider::PrimeIntellect => "us-east",
        CloudProvider::Render => "oregon",
        CloudProvider::BittensorLium => "global",
        CloudProvider::Hetzner => "fsn1",
        CloudProvider::Crusoe => "us-east1",
        _ => "default",
    }
}