change uuid to vm_id and app_id
This commit is contained in:
parent
40db6710b4
commit
0ff4db99f0
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -396,7 +396,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "detee-shared"
|
||||
version = "0.1.0"
|
||||
source = "git+ssh://git@gitea.detee.cloud/testnet/proto?branch=offers#4753a17fa29393b3f99b6dfcdcec48d935e6ebd9"
|
||||
source = "git+ssh://git@gitea.detee.cloud/testnet/proto?branch=remove_uuid#6765cafd68664dcfa269dd776039532eb4253e8a"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"prost",
|
||||
|
@ -27,7 +27,7 @@ bs58 = "0.5.1"
|
||||
chrono = "0.4.39"
|
||||
|
||||
# TODO: switch this back to main after the upgrade
|
||||
detee-shared = { git = "ssh://git@gitea.detee.cloud/testnet/proto", branch = "offers" }
|
||||
detee-shared = { git = "ssh://git@gitea.detee.cloud/testnet/proto", branch = "remove_uuid" }
|
||||
# detee-shared = { path = "../detee-shared" }
|
||||
|
||||
[build-dependencies]
|
||||
|
36
src/main.rs
36
src/main.rs
@ -52,22 +52,22 @@ impl VMHandler {
|
||||
|
||||
async fn handle_new_vm_req(&mut self, new_vm_req: snp_proto::NewVmReq) {
|
||||
debug!("Processing new vm request: {new_vm_req:?}");
|
||||
let uuid = new_vm_req.uuid.clone();
|
||||
let vm_id = new_vm_req.vm_id.clone();
|
||||
match self.state.new_vm(new_vm_req.into()) {
|
||||
Ok(vm) => match vm.start() {
|
||||
Ok(_) => {
|
||||
info!("Succesfully started VM {uuid}");
|
||||
info!("Succesfully started VM {vm_id}");
|
||||
let vm: snp_proto::NewVmResp = vm.into();
|
||||
let _ = self.sender.send(vm.into()).await;
|
||||
self.send_node_resources().await;
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Could not start VM {uuid}: {e:?}");
|
||||
log::error!("Could not start VM {vm_id}: {e:?}");
|
||||
let _ = self
|
||||
.sender
|
||||
.send(
|
||||
snp_proto::NewVmResp {
|
||||
uuid,
|
||||
vm_id,
|
||||
error: "This node has an internal error. Choose another node."
|
||||
.to_string(),
|
||||
..Default::default()
|
||||
@ -81,18 +81,18 @@ impl VMHandler {
|
||||
crate::state::VMCreationErrors::VMAlreadyExists(vm) => {
|
||||
log::info!(
|
||||
"Got NewVmReq for VM {}, that already exist. Will send NewVmResp.",
|
||||
vm.uuid
|
||||
vm.vm_id
|
||||
);
|
||||
let vm: snp_proto::NewVmResp = vm.into();
|
||||
let _ = self.sender.send(vm.into()).await;
|
||||
}
|
||||
_ => {
|
||||
warn!("Refusing to service vm {uuid} due to error: {e:?}");
|
||||
warn!("Refusing to service vm {vm_id} due to error: {e:?}");
|
||||
let _ = self
|
||||
.sender
|
||||
.send(
|
||||
snp_proto::NewVmResp {
|
||||
uuid,
|
||||
vm_id,
|
||||
error: format!("{e:?}"),
|
||||
..Default::default()
|
||||
}
|
||||
@ -106,7 +106,7 @@ impl VMHandler {
|
||||
|
||||
async fn handle_update_vm_req(&mut self, update_vm_req: snp_proto::UpdateVmReq) -> Result<()> {
|
||||
debug!("Processing update vm request: {update_vm_req:?}");
|
||||
let vm_id = update_vm_req.uuid.clone();
|
||||
let vm_id = update_vm_req.vm_id.clone();
|
||||
let content = std::fs::read_to_string(VM_CONFIG_DIR.to_string() + &vm_id + ".yaml")?;
|
||||
let mut vm: state::VM = serde_yaml::from_str(&content)?;
|
||||
match vm.update(update_vm_req.into(), &mut self.state) {
|
||||
@ -122,7 +122,7 @@ impl VMHandler {
|
||||
.sender
|
||||
.send(
|
||||
snp_proto::UpdateVmResp {
|
||||
uuid: vm_id,
|
||||
vm_id: vm_id,
|
||||
error: format!("{e:?}"),
|
||||
..Default::default()
|
||||
}
|
||||
@ -135,7 +135,7 @@ impl VMHandler {
|
||||
}
|
||||
|
||||
fn handle_delete_vm(&mut self, delete_vm_req: snp_proto::DeleteVmReq) -> Result<()> {
|
||||
let vm_id = delete_vm_req.uuid;
|
||||
let vm_id = delete_vm_req.vm_id;
|
||||
let content = std::fs::read_to_string(VM_CONFIG_DIR.to_string() + &vm_id + ".yaml")?;
|
||||
let vm: state::VM = serde_yaml::from_str(&content)?;
|
||||
vm.delete(&mut self.state.res)?;
|
||||
@ -156,9 +156,9 @@ impl VMHandler {
|
||||
}
|
||||
}
|
||||
Some(snp_proto::brain_vm_message::Msg::DeleteVm(delete_vm_req)) => {
|
||||
let uuid = delete_vm_req.uuid.clone();
|
||||
let vm_id = delete_vm_req.vm_id.clone();
|
||||
if let Err(e) = self.handle_delete_vm(delete_vm_req) {
|
||||
log::error!("Could not delete vm {uuid}: {e:?}");
|
||||
log::error!("Could not delete vm {vm_id}: {e:?}");
|
||||
} else {
|
||||
self.send_node_resources().await;
|
||||
}
|
||||
@ -170,25 +170,25 @@ impl VMHandler {
|
||||
|
||||
fn clear_deleted_contracts(&mut self, deleted_vms: Vec<snp_proto::DeleteVmReq>) {
|
||||
for deleted_vm in deleted_vms {
|
||||
let uuid = deleted_vm.uuid;
|
||||
let content = match std::fs::read_to_string(VM_CONFIG_DIR.to_string() + &uuid + ".yaml")
|
||||
let vm_id = deleted_vm.vm_id;
|
||||
let content = match std::fs::read_to_string(VM_CONFIG_DIR.to_string() + &vm_id + ".yaml")
|
||||
{
|
||||
Ok(content) => content,
|
||||
Err(e) => {
|
||||
log::debug!("Could not find VM config for {uuid}. Maybe it already got deleted? Error: {e:?}");
|
||||
log::debug!("Could not find VM config for {vm_id}. Maybe it already got deleted? Error: {e:?}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let vm: crate::state::VM = match serde_yaml::from_str(&content) {
|
||||
Ok(vm) => vm,
|
||||
Err(e) => {
|
||||
log::error!("VM config corrupted for {uuid}. Cannot delete VM: {e:?}");
|
||||
log::error!("VM config corrupted for {vm_id}. Cannot delete VM: {e:?}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
match vm.delete(&mut self.state.res) {
|
||||
Ok(()) => info!("Successfully deleted VM {uuid}"),
|
||||
Err(e) => log::error!("Deletion failed for VM {uuid}: {e:?}"),
|
||||
Ok(()) => info!("Successfully deleted VM {vm_id}"),
|
||||
Err(e) => log::error!("Deletion failed for VM {vm_id}: {e:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
82
src/state.rs
82
src/state.rs
@ -190,8 +190,8 @@ impl State {
|
||||
pub fn new_vm(&mut self, req: NewVMRequest) -> Result<VM, VMCreationErrors> {
|
||||
let offer_path = self.get_offer_path(&req)?;
|
||||
|
||||
if self.res.existing_vms.contains(&req.uuid) {
|
||||
let content = std::fs::read_to_string(VM_CONFIG_DIR.to_string() + &req.uuid + ".yaml")
|
||||
if self.res.existing_vms.contains(&req.vm_id) {
|
||||
let content = std::fs::read_to_string(VM_CONFIG_DIR.to_string() + &req.vm_id + ".yaml")
|
||||
.map_err(|e| VMCreationErrors::ServerDiskError(e.to_string()))?;
|
||||
let vm: crate::state::VM = serde_yaml::from_str(&content)
|
||||
.map_err(|e| VMCreationErrors::ServerDiskError(e.to_string()))?;
|
||||
@ -270,7 +270,7 @@ impl State {
|
||||
}
|
||||
|
||||
let vm = VM {
|
||||
uuid: req.uuid,
|
||||
vm_id: req.vm_id,
|
||||
admin_key: req.admin_key,
|
||||
nics: vm_nics,
|
||||
vcpus: req.vcpus,
|
||||
@ -322,7 +322,7 @@ impl Resources {
|
||||
log::info!("Found VM config: {:?}", path.to_str());
|
||||
let content = std::fs::read_to_string(path)?;
|
||||
let vm: VM = serde_yaml::from_str(&content)?;
|
||||
res.existing_vms.insert(vm.uuid);
|
||||
res.existing_vms.insert(vm.vm_id);
|
||||
res.reserved_hw
|
||||
.entry(vm.storage_dir)
|
||||
.and_modify(|utilization| {
|
||||
@ -477,7 +477,7 @@ impl Resources {
|
||||
}
|
||||
|
||||
fn reserve_vm_resources(&mut self, vm: &VM) {
|
||||
self.existing_vms.insert(vm.uuid.clone());
|
||||
self.existing_vms.insert(vm.vm_id.clone());
|
||||
for nic in vm.nics.iter() {
|
||||
if let Some(vtap) = nic.if_config.vtap_name() {
|
||||
self.reserved_if_names.insert(vtap);
|
||||
@ -513,7 +513,7 @@ impl Resources {
|
||||
}
|
||||
|
||||
fn free_vm_resources(&mut self, vm: &VM) {
|
||||
if !self.existing_vms.remove(&vm.uuid) {
|
||||
if !self.existing_vms.remove(&vm.vm_id) {
|
||||
return;
|
||||
}
|
||||
for nic in vm.nics.iter() {
|
||||
@ -546,14 +546,6 @@ impl Resources {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct StoragePool {
|
||||
path: String,
|
||||
available_gb: usize,
|
||||
// add mechanic to detect storage tier
|
||||
// tier: StorageTier,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum InterfaceConfig {
|
||||
// TODO: instead of QEMU userspace NAT, use iptables kernelspace NAT
|
||||
@ -616,7 +608,7 @@ pub struct VMNIC {
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct VM {
|
||||
pub uuid: String,
|
||||
pub vm_id: String,
|
||||
admin_key: String,
|
||||
fw_ports: Vec<(u16, u16)>,
|
||||
nics: Vec<VMNIC>,
|
||||
@ -669,21 +661,21 @@ impl From<VM> for snp_proto::MeasurementArgs {
|
||||
|
||||
impl From<VM> for snp_proto::NewVmResp {
|
||||
fn from(vm: VM) -> Self {
|
||||
let uuid = vm.uuid.clone();
|
||||
snp_proto::NewVmResp { uuid, args: Some(vm.into()), error: "".to_string() }
|
||||
let vm_id = vm.vm_id.clone();
|
||||
snp_proto::NewVmResp { vm_id, args: Some(vm.into()), error: "".to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<VM> for snp_proto::UpdateVmResp {
|
||||
fn from(vm: VM) -> Self {
|
||||
let uuid = vm.uuid.clone();
|
||||
snp_proto::UpdateVmResp { uuid, args: Some(vm.into()), error: "".to_string() }
|
||||
let vm_id = vm.vm_id.clone();
|
||||
snp_proto::UpdateVmResp { vm_id, args: Some(vm.into()), error: "".to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct NewVMRequest {
|
||||
uuid: String,
|
||||
vm_id: String,
|
||||
admin_key: String,
|
||||
extra_ports: Vec<u16>,
|
||||
public_ipv4: bool,
|
||||
@ -701,7 +693,7 @@ pub struct NewVMRequest {
|
||||
impl From<snp_proto::NewVmReq> for NewVMRequest {
|
||||
fn from(req: snp_proto::NewVmReq) -> Self {
|
||||
Self {
|
||||
uuid: req.uuid,
|
||||
vm_id: req.vm_id,
|
||||
admin_key: req.admin_pubkey,
|
||||
extra_ports: req.extra_ports.iter().map(|&port| port as u16).collect(),
|
||||
public_ipv4: req.public_ipv4,
|
||||
@ -720,7 +712,7 @@ impl From<snp_proto::NewVmReq> for NewVMRequest {
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct UpdateVMReq {
|
||||
pub uuid: String,
|
||||
pub vm_id: String,
|
||||
vcpus: usize,
|
||||
memory_mib: usize,
|
||||
disk_size_mib: usize,
|
||||
@ -734,7 +726,7 @@ pub struct UpdateVMReq {
|
||||
impl From<snp_proto::UpdateVmReq> for UpdateVMReq {
|
||||
fn from(req: snp_proto::UpdateVmReq) -> Self {
|
||||
Self {
|
||||
uuid: req.uuid,
|
||||
vm_id: req.vm_id,
|
||||
vcpus: req.vcpus as usize,
|
||||
memory_mib: req.memory_mib as usize,
|
||||
disk_size_mib: req.disk_size_mib as usize,
|
||||
@ -812,7 +804,7 @@ impl VM {
|
||||
};
|
||||
info!(
|
||||
"Kernel and DTRFS updated for VM: {}, kernel {}, dtrfs: {}",
|
||||
self.uuid, req.kernel_sha, req.dtrfs_sha
|
||||
self.vm_id, req.kernel_sha, req.dtrfs_sha
|
||||
);
|
||||
self.kernel_sha = req.kernel_sha;
|
||||
self.dtrfs_sha = req.dtrfs_sha;
|
||||
@ -839,7 +831,7 @@ impl VM {
|
||||
self.disk_size_mib = req.disk_size_mib;
|
||||
}
|
||||
|
||||
if let Err(e) = systemctl_stop_and_disable(&self.uuid) {
|
||||
if let Err(e) = systemctl_stop_and_disable(&self.vm_id) {
|
||||
return Err(VMCreationErrors::HypervizorError(e.to_string()));
|
||||
}
|
||||
|
||||
@ -855,7 +847,7 @@ impl VM {
|
||||
return Err(VMCreationErrors::HypervizorError(e.to_string()));
|
||||
}
|
||||
|
||||
if let Err(e) = systemctl_start_and_enable(&self.uuid) {
|
||||
if let Err(e) = systemctl_start_and_enable(&self.vm_id) {
|
||||
return Err(VMCreationErrors::HypervizorError(e.to_string()));
|
||||
}
|
||||
|
||||
@ -867,12 +859,12 @@ impl VM {
|
||||
self.write_sh_exports()?;
|
||||
self.write_systemd_unit_file()?;
|
||||
systemctl_reload()?;
|
||||
systemctl_start_and_enable(&self.uuid)?;
|
||||
systemctl_start_and_enable(&self.vm_id)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete(&self, res: &mut Resources) -> Result<()> {
|
||||
let _ = systemctl_stop_and_disable(&self.uuid);
|
||||
let _ = systemctl_stop_and_disable(&self.vm_id);
|
||||
let _ = self.delete_systemd_unit_file();
|
||||
let _ = systemctl_reload();
|
||||
let _ = self.delete_disk();
|
||||
@ -891,7 +883,7 @@ impl VM {
|
||||
true => self.storage_dir.clone(),
|
||||
false => self.storage_dir.clone() + "/",
|
||||
};
|
||||
dir + &self.uuid + ".qcow2"
|
||||
dir + &self.vm_id + ".qcow2"
|
||||
}
|
||||
|
||||
// If you change this here, you also have to change it in the CLI.
|
||||
@ -911,18 +903,18 @@ impl VM {
|
||||
i += 1;
|
||||
}
|
||||
let admin_key = format!("detee_admin={} ", self.admin_key);
|
||||
let uuid = format!("detee_uuid={}", self.uuid);
|
||||
format!("{}{}{}", ip_string, admin_key, uuid)
|
||||
let vm_id = format!("detee_vm_id={}", self.vm_id);
|
||||
format!("{}{}{}", ip_string, admin_key, vm_id)
|
||||
}
|
||||
|
||||
fn write_config(&self) -> Result<()> {
|
||||
let mut file = File::create(VM_CONFIG_DIR.to_string() + &self.uuid + ".yaml")?;
|
||||
let mut file = File::create(VM_CONFIG_DIR.to_string() + &self.vm_id + ".yaml")?;
|
||||
file.write_all(serde_yaml::to_string(self)?.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete_config(&self) -> Result<()> {
|
||||
remove_file(VM_CONFIG_DIR.to_string() + &self.uuid + ".yaml")?;
|
||||
remove_file(VM_CONFIG_DIR.to_string() + &self.vm_id + ".yaml")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -970,13 +962,13 @@ impl VM {
|
||||
vars += &format!(r#"export DISK="{}""#, self.disk_path());
|
||||
vars += "\n";
|
||||
|
||||
let mut file = File::create(VM_CONFIG_DIR.to_string() + &self.uuid + ".sh")?;
|
||||
let mut file = File::create(VM_CONFIG_DIR.to_string() + &self.vm_id + ".sh")?;
|
||||
file.write_all(vars.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete_sh_exports(&self) -> Result<()> {
|
||||
remove_file(VM_CONFIG_DIR.to_string() + &self.uuid + ".sh")?;
|
||||
remove_file(VM_CONFIG_DIR.to_string() + &self.vm_id + ".sh")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -1000,12 +992,12 @@ impl VM {
|
||||
fn write_systemd_unit_file(&self) -> Result<()> {
|
||||
let mut contents = String::new();
|
||||
contents += &format!("[Unit]\n");
|
||||
contents += &format!("Description=DeTEE {}\n", self.uuid);
|
||||
contents += &format!("Description=DeTEE {}\n", self.vm_id);
|
||||
contents += &format!("After=network.target\n");
|
||||
contents += &format!("\n");
|
||||
contents += &format!("[Service]\n");
|
||||
contents += &format!("Type=simple\n");
|
||||
contents += &format!("Environment=VM_UUID={}\n", self.uuid);
|
||||
contents += &format!("Environment=VM_UUID={}\n", self.vm_id);
|
||||
contents += &format!("ExecStart={}\n", START_VM_SCRIPT);
|
||||
contents += &format!("ExecStop=/bin/kill -s SIGINT $MAINPID\n");
|
||||
contents += &format!("Restart=always\n");
|
||||
@ -1014,13 +1006,13 @@ impl VM {
|
||||
contents += &format!("WantedBy=multi-user.target\n");
|
||||
|
||||
let mut file =
|
||||
File::create_new("/etc/systemd/system/".to_string() + &self.uuid + ".service")?;
|
||||
File::create_new("/etc/systemd/system/".to_string() + &self.vm_id + ".service")?;
|
||||
file.write_all(contents.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete_systemd_unit_file(&self) -> Result<()> {
|
||||
remove_file("/etc/systemd/system/".to_string() + &self.uuid + ".service")?;
|
||||
remove_file("/etc/systemd/system/".to_string() + &self.vm_id + ".service")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -1072,9 +1064,9 @@ impl VM {
|
||||
}
|
||||
}
|
||||
|
||||
fn systemctl_start_and_enable(vm_uuid: &str) -> Result<()> {
|
||||
fn systemctl_start_and_enable(vm_id: &str) -> Result<()> {
|
||||
let result =
|
||||
Command::new("systemctl").arg("start").arg(vm_uuid.to_string() + ".service").output()?;
|
||||
Command::new("systemctl").arg("start").arg(vm_id.to_string() + ".service").output()?;
|
||||
if !result.status.success() {
|
||||
return Err(anyhow!(
|
||||
"Could not reload systemctl daemon:\n{:?}\n{:?}",
|
||||
@ -1085,7 +1077,7 @@ fn systemctl_start_and_enable(vm_uuid: &str) -> Result<()> {
|
||||
));
|
||||
}
|
||||
let result =
|
||||
Command::new("systemctl").arg("enable").arg(vm_uuid.to_string() + ".service").output()?;
|
||||
Command::new("systemctl").arg("enable").arg(vm_id.to_string() + ".service").output()?;
|
||||
if !result.status.success() {
|
||||
return Err(anyhow!(
|
||||
"Could not reload systemctl daemon:\n{:?}\n{:?}",
|
||||
@ -1098,9 +1090,9 @@ fn systemctl_start_and_enable(vm_uuid: &str) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn systemctl_stop_and_disable(vm_uuid: &str) -> Result<()> {
|
||||
fn systemctl_stop_and_disable(vm_id: &str) -> Result<()> {
|
||||
let result =
|
||||
Command::new("systemctl").arg("stop").arg(vm_uuid.to_string() + ".service").output()?;
|
||||
Command::new("systemctl").arg("stop").arg(vm_id.to_string() + ".service").output()?;
|
||||
if !result.status.success() {
|
||||
return Err(anyhow!(
|
||||
"Could not reload systemctl daemon:\n{:?}\n{:?}",
|
||||
@ -1111,7 +1103,7 @@ fn systemctl_stop_and_disable(vm_uuid: &str) -> Result<()> {
|
||||
));
|
||||
}
|
||||
let result =
|
||||
Command::new("systemctl").arg("disable").arg(vm_uuid.to_string() + ".service").output()?;
|
||||
Command::new("systemctl").arg("disable").arg(vm_id.to_string() + ".service").output()?;
|
||||
if !result.status.success() {
|
||||
return Err(anyhow!(
|
||||
"Could not reload systemctl daemon:\n{:?}\n{:?}",
|
||||
|
Loading…
Reference in New Issue
Block a user