Skip to content

Commit 7213efa

Browse files
committed
rust 1.0.4
1 parent 28e35bc commit 7213efa

File tree

4 files changed

+86
-79
lines changed

4 files changed

+86
-79
lines changed

rust/Cargo.lock

Lines changed: 28 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "xnode-deployer"
33
description = "Deploy new Xnodes on several hardware providers"
4-
version = "1.0.3"
4+
version = "1.0.4"
55
edition = "2024"
66
repository = "https://github.com/Openmesh-Network/xnode-deployer"
77
license = "MIT"

rust/src/hivelocity/mod.rs

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use std::{fmt::Display, time::Duration};
1+
use std::{fmt::Display, net::Ipv4Addr, str::FromStr};
22

33
use reqwest::Client;
44
use serde_json::json;
5-
use tokio::time::sleep;
65

76
use crate::{
8-
DeployInput, DeployOutput, Error, XnodeDeployer, XnodeDeployerError,
7+
DeployInput, Error,
8+
OptionalSupport::{self, Supported},
9+
XnodeDeployer, XnodeDeployerError,
910
utils::XnodeDeployerErrorInner,
1011
};
1112

@@ -60,15 +61,12 @@ impl HivelocityDeployer {
6061
impl XnodeDeployer for HivelocityDeployer {
6162
type ProviderOutput = HivelocityOutput;
6263

63-
async fn deploy(
64-
&self,
65-
input: DeployInput,
66-
) -> Result<DeployOutput<Self::ProviderOutput>, Error> {
64+
async fn deploy(&self, input: DeployInput) -> Result<Self::ProviderOutput, Error> {
6765
log::info!(
6866
"Hivelocity deployment of {input:?} on {hardware:?} started",
6967
hardware = self.hardware
7068
);
71-
let mut response = match &self.hardware {
69+
let response = match &self.hardware {
7270
HivelocityHardware::BareMetal {
7371
location_name,
7472
period,
@@ -147,45 +145,14 @@ impl XnodeDeployer for HivelocityDeployer {
147145
Err(e) => return Err(e),
148146
};
149147

150-
let mut ip = "0.0.0.0".to_string();
151-
while ip == "0.0.0.0" {
152-
log::info!("Getting ip address of hivelocity device {device_id}",);
153-
if let serde_json::Value::Object(map) = &response {
154-
if let Some(serde_json::Value::String(primary_ip)) = map.get("primaryIp") {
155-
ip = primary_ip.clone();
156-
}
157-
};
158-
159-
sleep(Duration::from_secs(1)).await;
160-
let scope = match self.hardware {
161-
HivelocityHardware::BareMetal { .. } => "bare-metal-devices",
162-
HivelocityHardware::Compute { .. } => "compute",
163-
};
164-
response = self
165-
.client
166-
.get(format!(
167-
"https://core.hivelocity.net/api/v2/{scope}/{device_id}"
168-
))
169-
.header("X-API-KEY", self.api_key.clone())
170-
.send()
171-
.await
172-
.map_err(Error::ReqwestError)?
173-
.json::<serde_json::Value>()
174-
.await
175-
.map_err(Error::ReqwestError)?;
176-
}
177-
178-
let output = DeployOutput::<Self::ProviderOutput> {
179-
ip,
180-
provider: HivelocityOutput { device_id },
181-
};
148+
let output = Self::ProviderOutput { device_id };
182149
log::info!("Hivelocity deployment succeeded: {output:?}");
183150
Ok(output)
184151
}
185152

186-
async fn undeploy(&self, xnode: DeployOutput<Self::ProviderOutput>) -> Option<Error> {
187-
let device_id = xnode.provider.device_id;
188-
log::info!("Undeploying hivelocity device {device_id} started",);
153+
async fn undeploy(&self, xnode: Self::ProviderOutput) -> Option<Error> {
154+
let device_id = xnode.device_id;
155+
log::info!("Undeploying hivelocity device {device_id} started");
189156
let scope = match self.hardware {
190157
HivelocityHardware::BareMetal { .. } => "bare-metal-devices",
191158
HivelocityHardware::Compute { .. } => "compute",
@@ -206,6 +173,40 @@ impl XnodeDeployer for HivelocityDeployer {
206173
log::info!("Undeploying hivelocity device {device_id} succeeded");
207174
None
208175
}
176+
177+
async fn ipv4(
178+
&self,
179+
xnode: Self::ProviderOutput,
180+
) -> Result<OptionalSupport<Option<Ipv4Addr>>, Error> {
181+
let device_id = xnode.device_id;
182+
let scope = match self.hardware {
183+
HivelocityHardware::BareMetal { .. } => "bare-metal-devices",
184+
HivelocityHardware::Compute { .. } => "compute",
185+
};
186+
let response = self
187+
.client
188+
.get(format!(
189+
"https://core.hivelocity.net/api/v2/{scope}/{device_id}"
190+
))
191+
.header("X-API-KEY", self.api_key.clone())
192+
.send()
193+
.await
194+
.and_then(|response| response.error_for_status())
195+
.map_err(Error::ReqwestError)?
196+
.json::<serde_json::Value>()
197+
.await
198+
.map_err(Error::ReqwestError)?;
199+
200+
if let serde_json::Value::Object(map) = &response {
201+
if let Some(serde_json::Value::String(primary_ip)) = map.get("primaryIp") {
202+
if let Ok(ip) = Ipv4Addr::from_str(primary_ip) {
203+
return Ok(Supported(Some(ip)));
204+
}
205+
}
206+
};
207+
208+
Ok(Supported(None))
209+
}
209210
}
210211

211212
#[derive(Debug)]

rust/src/lib.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::net::Ipv4Addr;
2+
13
use serde::{Deserialize, Serialize};
24

35
mod utils;
@@ -15,10 +17,10 @@ pub struct DeployInput {
1517
pub encrypted: Option<String>,
1618
pub initial_config: Option<String>,
1719
}
18-
#[derive(Serialize, Deserialize, Debug)]
19-
pub struct DeployOutput<ProviderOutput> {
20-
pub ip: String,
21-
pub provider: ProviderOutput,
20+
21+
pub enum OptionalSupport<T> {
22+
NotSupported,
23+
Supported(T),
2224
}
2325

2426
pub trait XnodeDeployer: Send + Sync {
@@ -28,13 +30,16 @@ pub trait XnodeDeployer: Send + Sync {
2830
fn deploy(
2931
&self,
3032
input: DeployInput,
31-
) -> impl Future<Output = Result<DeployOutput<Self::ProviderOutput>, Error>> + Send;
33+
) -> impl Future<Output = Result<Self::ProviderOutput, Error>> + Send;
3234

3335
/// Cancel renting of hardware
34-
fn undeploy(
36+
fn undeploy(&self, xnode: Self::ProviderOutput) -> impl Future<Output = Option<Error>> + Send;
37+
38+
/// Get ipv4 address of deployed hardware
39+
fn ipv4(
3540
&self,
36-
xnode: DeployOutput<Self::ProviderOutput>,
37-
) -> impl Future<Output = Option<Error>> + Send;
41+
xnode: Self::ProviderOutput,
42+
) -> impl Future<Output = Result<OptionalSupport<Option<Ipv4Addr>>, Error>> + Send;
3843
}
3944

4045
impl DeployInput {

0 commit comments

Comments
 (0)