blueprint-remote-providers 0.2.0-alpha.2

Remote service providers for Tangle Blueprints
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
//! Cloud provider configuration types

use serde::{Deserialize, Serialize};

/// Cloud provider configuration for all supported providers
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CloudConfig {
    pub enabled: bool,
    pub aws: Option<AwsConfig>,
    pub gcp: Option<GcpConfig>,
    pub azure: Option<AzureConfig>,
    pub digital_ocean: Option<DigitalOceanConfig>,
    pub vultr: Option<VultrConfig>,
    pub lambda_labs: Option<LambdaLabsConfig>,
    pub runpod: Option<RunPodConfig>,
    pub vast_ai: Option<VastAiConfig>,
    pub coreweave: Option<CoreWeaveConfig>,
    pub paperspace: Option<PaperspaceConfig>,
    pub fluidstack: Option<FluidstackConfig>,
    pub tensordock: Option<TensorDockConfig>,
    pub akash: Option<AkashConfig>,
    pub io_net: Option<IoNetConfig>,
    pub prime_intellect: Option<PrimeIntellectConfig>,
    pub render: Option<RenderConfig>,
    pub bittensor_lium: Option<BittensorLiumConfig>,
}

/// AWS configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AwsConfig {
    pub enabled: bool,
    pub region: String,
    pub access_key: String,
    pub secret_key: String,
    pub priority: Option<u8>,
}

/// GCP configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GcpConfig {
    pub enabled: bool,
    pub region: String,
    pub project_id: String,
    pub service_account_path: String,
    pub priority: Option<u8>,
}

/// Azure configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AzureConfig {
    pub enabled: bool,
    pub region: String,
    pub client_id: String,
    pub client_secret: String,
    pub tenant_id: String,
    pub priority: Option<u8>,
}

/// DigitalOcean configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DigitalOceanConfig {
    pub enabled: bool,
    pub region: String,
    pub api_token: String,
    pub priority: Option<u8>,
}

/// Vultr configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VultrConfig {
    pub enabled: bool,
    pub region: String,
    pub api_key: String,
    pub priority: Option<u8>,
}

/// Lambda Labs configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LambdaLabsConfig {
    pub enabled: bool,
    pub region: String,
    pub api_key: String,
    pub ssh_key_name: Option<String>,
    pub priority: Option<u8>,
}

/// RunPod configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunPodConfig {
    pub enabled: bool,
    pub region: String,
    pub api_key: String,
    pub cloud_type: Option<String>,
    pub priority: Option<u8>,
}

/// Vast.ai configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VastAiConfig {
    pub enabled: bool,
    pub api_key: String,
    pub max_price_per_hour: Option<f64>,
    pub min_reliability: Option<f64>,
    pub priority: Option<u8>,
}

/// CoreWeave configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoreWeaveConfig {
    pub enabled: bool,
    pub region: String,
    pub token: String,
    pub namespace: Option<String>,
    pub priority: Option<u8>,
}

/// Paperspace configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaperspaceConfig {
    pub enabled: bool,
    pub region: String,
    pub api_key: String,
    pub priority: Option<u8>,
}

/// Fluidstack configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FluidstackConfig {
    pub enabled: bool,
    pub region: String,
    pub api_key: String,
    pub priority: Option<u8>,
}

/// TensorDock configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TensorDockConfig {
    pub enabled: bool,
    pub region: String,
    pub api_key: String,
    pub api_token: String,
    pub priority: Option<u8>,
}

/// Akash configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AkashConfig {
    pub enabled: bool,
    pub rpc_url: String,
    pub wallet_mnemonic: String,
    pub chain_id: Option<String>,
    pub priority: Option<u8>,
}

/// io.net configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IoNetConfig {
    pub enabled: bool,
    pub region: String,
    pub api_key: String,
    pub cluster_type: Option<String>,
    pub priority: Option<u8>,
}

/// Prime Intellect configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrimeIntellectConfig {
    pub enabled: bool,
    pub region: String,
    pub api_key: String,
    pub priority: Option<u8>,
}

/// Render configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RenderConfig {
    pub enabled: bool,
    pub region: String,
    pub api_key: String,
    pub priority: Option<u8>,
}

/// Bittensor/Lium configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BittensorLiumConfig {
    pub enabled: bool,
    pub api_key: String,
    pub wallet_hotkey: Option<String>,
    pub wallet_coldkey: Option<String>,
    pub priority: Option<u8>,
}

/// Try to load a provider config from env. If `primary_env` is set, calls `builder`
/// with the value, assigns the result to `field`, and sets `any_enabled`.
fn load_provider<T>(
    field: &mut Option<T>,
    primary_env: &str,
    any_enabled: &mut bool,
    builder: impl FnOnce(String) -> T,
) {
    if let Ok(val) = std::env::var(primary_env) {
        *field = Some(builder(val));
        *any_enabled = true;
    }
}

/// Same as `load_provider` but requires two env vars to both be present.
fn load_provider2<T>(
    field: &mut Option<T>,
    env_a: &str,
    env_b: &str,
    any_enabled: &mut bool,
    builder: impl FnOnce(String, String) -> T,
) {
    if let (Ok(a), Ok(b)) = (std::env::var(env_a), std::env::var(env_b)) {
        *field = Some(builder(a, b));
        *any_enabled = true;
    }
}

/// Shorthand: read an env var with a default fallback.
fn env_or(var: &str, default: &str) -> String {
    std::env::var(var).unwrap_or_else(|_| default.to_string())
}

impl CloudConfig {
    /// Load configuration from environment variables
    pub fn from_env() -> Option<Self> {
        use std::env;

        let mut c = CloudConfig::default();
        let mut any = false;

        load_provider2(
            &mut c.aws,
            "AWS_ACCESS_KEY_ID",
            "AWS_SECRET_ACCESS_KEY",
            &mut any,
            |key, secret| AwsConfig {
                enabled: true,
                region: env_or("AWS_DEFAULT_REGION", "us-east-1"),
                access_key: key,
                secret_key: secret,
                priority: Some(10),
            },
        );

        load_provider(&mut c.gcp, "GCP_PROJECT_ID", &mut any, |project_id| {
            GcpConfig {
                enabled: true,
                region: env_or("GCP_DEFAULT_REGION", "us-central1"),
                project_id,
                service_account_path: env_or(
                    "GOOGLE_APPLICATION_CREDENTIALS",
                    "/etc/gcp/service-account.json",
                ),
                priority: Some(8),
            }
        });

        if let (Ok(client_id), Ok(client_secret), Ok(tenant_id)) = (
            env::var("AZURE_CLIENT_ID"),
            env::var("AZURE_CLIENT_SECRET"),
            env::var("AZURE_TENANT_ID"),
        ) {
            c.azure = Some(AzureConfig {
                enabled: true,
                region: env_or("AZURE_DEFAULT_REGION", "East US"),
                client_id,
                client_secret,
                tenant_id,
                priority: Some(7),
            });
            any = true;
        }

        load_provider(&mut c.digital_ocean, "DO_API_TOKEN", &mut any, |token| {
            DigitalOceanConfig {
                enabled: true,
                region: env_or("DO_DEFAULT_REGION", "nyc3"),
                api_token: token,
                priority: Some(5),
            }
        });

        load_provider(&mut c.vultr, "VULTR_API_KEY", &mut any, |key| VultrConfig {
            enabled: true,
            region: env_or("VULTR_DEFAULT_REGION", "ewr"),
            api_key: key,
            priority: Some(3),
        });

        load_provider(&mut c.lambda_labs, "LAMBDA_LABS_API_KEY", &mut any, |key| {
            LambdaLabsConfig {
                enabled: true,
                region: env_or("LAMBDA_LABS_REGION", "us-west-1"),
                api_key: key,
                ssh_key_name: env::var("LAMBDA_LABS_SSH_KEY_NAME").ok(),
                priority: Some(6),
            }
        });

        load_provider(&mut c.runpod, "RUNPOD_API_KEY", &mut any, |key| {
            RunPodConfig {
                enabled: true,
                region: env_or("RUNPOD_REGION", "US"),
                api_key: key,
                cloud_type: env::var("RUNPOD_CLOUD_TYPE").ok(),
                priority: Some(6),
            }
        });

        load_provider(&mut c.vast_ai, "VAST_AI_API_KEY", &mut any, |key| {
            VastAiConfig {
                enabled: true,
                api_key: key,
                max_price_per_hour: env::var("VAST_AI_MAX_PRICE_PER_HOUR")
                    .ok()
                    .and_then(|v| v.parse().ok()),
                min_reliability: env::var("VAST_AI_MIN_RELIABILITY")
                    .ok()
                    .and_then(|v| v.parse().ok()),
                priority: Some(4),
            }
        });

        load_provider(&mut c.coreweave, "COREWEAVE_TOKEN", &mut any, |token| {
            CoreWeaveConfig {
                enabled: true,
                region: env_or("COREWEAVE_REGION", "ORD1"),
                token,
                namespace: env::var("COREWEAVE_NAMESPACE").ok(),
                priority: Some(7),
            }
        });

        load_provider(&mut c.paperspace, "PAPERSPACE_API_KEY", &mut any, |key| {
            PaperspaceConfig {
                enabled: true,
                region: env_or("PAPERSPACE_REGION", "NY2"),
                api_key: key,
                priority: Some(5),
            }
        });

        load_provider(&mut c.fluidstack, "FLUIDSTACK_API_KEY", &mut any, |key| {
            FluidstackConfig {
                enabled: true,
                region: env_or("FLUIDSTACK_REGION", "us-east"),
                api_key: key,
                priority: Some(4),
            }
        });

        load_provider2(
            &mut c.tensordock,
            "TENSORDOCK_API_KEY",
            "TENSORDOCK_API_TOKEN",
            &mut any,
            |key, token| TensorDockConfig {
                enabled: true,
                region: env_or("TENSORDOCK_REGION", "us-central"),
                api_key: key,
                api_token: token,
                priority: Some(4),
            },
        );

        load_provider2(
            &mut c.akash,
            "AKASH_RPC_URL",
            "AKASH_WALLET_MNEMONIC",
            &mut any,
            |rpc_url, mnemonic| AkashConfig {
                enabled: true,
                rpc_url,
                wallet_mnemonic: mnemonic,
                chain_id: env::var("AKASH_CHAIN_ID").ok(),
                priority: Some(3),
            },
        );

        load_provider(&mut c.io_net, "IO_NET_API_KEY", &mut any, |key| {
            IoNetConfig {
                enabled: true,
                region: env_or("IO_NET_REGION", "us-east"),
                api_key: key,
                cluster_type: env::var("IO_NET_CLUSTER_TYPE").ok(),
                priority: Some(4),
            }
        });

        load_provider(
            &mut c.prime_intellect,
            "PRIME_INTELLECT_API_KEY",
            &mut any,
            |key| PrimeIntellectConfig {
                enabled: true,
                region: env_or("PRIME_INTELLECT_REGION", "us-east"),
                api_key: key,
                priority: Some(5),
            },
        );

        load_provider(&mut c.render, "RENDER_API_KEY", &mut any, |key| {
            RenderConfig {
                enabled: true,
                region: env_or("RENDER_REGION", "oregon"),
                api_key: key,
                priority: Some(3),
            }
        });

        load_provider(&mut c.bittensor_lium, "LIUM_API_KEY", &mut any, |key| {
            BittensorLiumConfig {
                enabled: true,
                api_key: key,
                wallet_hotkey: env::var("LIUM_WALLET_HOTKEY").ok(),
                wallet_coldkey: env::var("LIUM_WALLET_COLDKEY").ok(),
                priority: Some(2),
            }
        });

        if any {
            c.enabled = true;
            Some(c)
        } else {
            None
        }
    }
}