added mechanic to get random ports
This commit is contained in:
parent
b905c96261
commit
57801c725d
37
src/state.rs
37
src/state.rs
@ -38,6 +38,27 @@ pub struct Resources {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Resources {
|
impl Resources {
|
||||||
|
// TODO: improve this to error out if ports are not available
|
||||||
|
// be careful with loops
|
||||||
|
// server must provide number of ports in the contract or fail
|
||||||
|
fn reserve_ports(&mut self, mut nr: u16, config: &Config) -> Vec<u16> {
|
||||||
|
use rand::Rng;
|
||||||
|
if nr > config.max_ports_per_vm {
|
||||||
|
nr = config.max_ports_per_vm;
|
||||||
|
}
|
||||||
|
let mut published_ports = Vec::new();
|
||||||
|
for _ in 0..nr {
|
||||||
|
for _ in 0..5 {
|
||||||
|
let port = rand::thread_rng().gen_range(config.public_port_range.clone());
|
||||||
|
if self.used_ports.insert(port) {
|
||||||
|
published_ports.push(port);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
|
||||||
fn reserve_vm_if(&mut self) -> String {
|
fn reserve_vm_if(&mut self) -> String {
|
||||||
use rand::{distributions::Alphanumeric, Rng};
|
use rand::{distributions::Alphanumeric, Rng};
|
||||||
loop {
|
loop {
|
||||||
@ -74,8 +95,8 @@ impl Resources {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
let mut ips = Vec::new();
|
let mut ips = Vec::new();
|
||||||
let mask = range
|
let mask = ip
|
||||||
.subnet
|
.network()
|
||||||
.to_string()
|
.to_string()
|
||||||
.split('/')
|
.split('/')
|
||||||
.next()
|
.next()
|
||||||
@ -115,8 +136,8 @@ impl Resources {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
let mut ips = Vec::new();
|
let mut ips = Vec::new();
|
||||||
let mask = range
|
let mask = ip
|
||||||
.subnet
|
.network()
|
||||||
.to_string()
|
.to_string()
|
||||||
.split('/')
|
.split('/')
|
||||||
.next()
|
.next()
|
||||||
@ -183,6 +204,12 @@ pub struct VMNIC {
|
|||||||
ips: Vec<IPConfig>,
|
ips: Vec<IPConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl VMNIC {
|
||||||
|
fn new() -> VMNIC {
|
||||||
|
todo!("implement this here to improve code elegance of resource reservation");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct VM {
|
pub struct VM {
|
||||||
uuid: String,
|
uuid: String,
|
||||||
hostname: String,
|
hostname: String,
|
||||||
@ -267,7 +294,7 @@ impl VM {
|
|||||||
} else {
|
} else {
|
||||||
vm_nics.push(vmnic);
|
vm_nics.push(vmnic);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
None => return Err(VMCreationErrors::IPv4NotAvailable),
|
None => return Err(VMCreationErrors::IPv4NotAvailable),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user