Skip to content

Commit

Permalink
Implement locale and timezone notifications
Browse files Browse the repository at this point in the history
Signed-off-by: Santtu Lakkala <[email protected]>
  • Loading branch information
slakkala committed Oct 1, 2024
1 parent c7894e9 commit 719cc37
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
4 changes: 2 additions & 2 deletions common/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ impl TryFrom<pb::QueryListItem> for QueryResult {
name: item.name,
description: item.description,
status: VMStatus::from_str(item.vm_status.as_str())
.context(format!("While parsing vm_status {}", &item.vm_status))?,
.with_context(|| format!("While parsing vm_status {}", item.vm_status))?,
trust_level: TrustLevel::from_str(item.trust_level.as_str())
.context(format!("While parsing trust_level {}", &item.trust_level))?,
.with_context(|| format!("While parsing trust_level {}", item.trust_level))?,
})
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/admin/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ impl Registry {
}
}

pub fn find_map<T, F: FnMut(&RegistryEntry) -> Option<T>>(&self, filter: F) -> Vec<T> {
let state = self.map.lock().unwrap();
state.values().filter_map(filter).collect()
}

pub fn by_type_many(&self, ty: UnitType) -> Vec<RegistryEntry> {
let state = self.map.lock().unwrap();
state.values().filter(|x| x.r#type == ty).cloned().collect()
Expand Down
60 changes: 47 additions & 13 deletions src/admin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,19 @@ impl AdminServiceImpl {
vm: VmType::Host,
service: ServiceType::Mgr,
})?;
let endpoint = host_mgr
.agent()
.with_context(|| "Resolving host agent".to_string())?;
Ok(EndpointConfig {
transport: endpoint,
tls: self.tls_config.clone(),
})
self.endpoint(host_mgr).context("Resolving host agent")
}

pub fn agent_endpoint(&self, name: &str) -> anyhow::Result<EndpointConfig> {
let endpoint = self.registry.by_name(name)?.agent()?;
pub fn endpoint(&self, reentry: RegistryEntry) -> anyhow::Result<EndpointConfig> {
Ok(EndpointConfig {
transport: endpoint,
transport: reentry.agent()?,
tls: self.tls_config.clone(),
})
}
pub fn agent_endpoint(&self, name: &str) -> anyhow::Result<EndpointConfig> {
let reentry = self.registry.by_name(name)?;
self.endpoint(reentry)
}

pub fn app_entries(&self, name: String) -> anyhow::Result<Vec<String>> {
if name.contains('@') {
Expand Down Expand Up @@ -206,7 +203,7 @@ impl AdminServiceImpl {
let name = parse_service_name(&entry.name)?;
self.start_vm(name)
.await
.with_context(|| format!("handing error, by restart VM {}", &entry.name))?;
.with_context(|| format!("handing error, by restart VM {}", entry.name))?;
Ok(()) // FIXME: should use `?` from line above, why it didn't work?
}
(x, y) => bail!(
Expand Down Expand Up @@ -245,7 +242,7 @@ impl AdminServiceImpl {
if inactive {
self.handle_error(entry)
.await
.with_context(|| "during handle error")?
.context("during handle error")?
}
}
}
Expand Down Expand Up @@ -287,7 +284,7 @@ impl AdminServiceImpl {
Err(_) => {
self.start_vm(&vm_name)
.await
.context(format!("Starting vm for {}", &name))?;
.with_context(|| format!("Starting vm for {name}"))?;
self.registry
.by_name(&systemd_agent)
.context("after starting VM")?
Expand Down Expand Up @@ -505,7 +502,26 @@ impl pb::admin_service_server::AdminService for AdminService {
bail!("Invalid locale");
}
let _ = tokio::fs::write(LOCALE_CONF, format!("LANG={}", req.locale)).await;
let managers = self.inner.registry.find_map(|re| {
(re.r#type.service == ServiceType::Mgr)
.then_some(())
.and_then(|_| self.inner.endpoint(re.clone()).ok())
});
let locale = req.locale.clone();
tokio::spawn(async move {
for ec in managers {
if let Ok(conn) = ec.connect().await {
let mut client =
pb::locale::locale_client_client::LocaleClientClient::new(conn);
let localemsg = pb::locale::LocaleMessage {
locale: locale.clone(),
};
let _ = client.locale_set(localemsg).await;
}
}
});
*self.inner.locale.lock().await = req.locale;

Ok(Empty {})
})
.await
Expand All @@ -520,6 +536,24 @@ impl pb::admin_service_server::AdminService for AdminService {
bail!("Invalid timezone");
}
let _ = tokio::fs::write(TIMEZONE_CONF, &req.timezone).await;
let managers = self.inner.registry.find_map(|re| {
(re.r#type.service == ServiceType::Mgr)
.then_some(())
.and_then(|_| self.inner.endpoint(re.clone()).ok())
});
let timezone = req.timezone.clone();
tokio::spawn(async move {
for ec in managers {
if let Ok(conn) = ec.connect().await {
let mut client =
pb::locale::locale_client_client::LocaleClientClient::new(conn);
let tzmsg = pb::locale::TimezoneMessage {
timezone: timezone.clone(),
};
let _ = client.timezone_set(tzmsg).await;
}
}
});
*self.inner.timezone.lock().await = req.timezone;
Ok(Empty {})
})
Expand Down
2 changes: 1 addition & 1 deletion src/utils/naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn parse_application_name(name: &str) -> anyhow::Result<(&str, i32)> {
if let Some((left, right)) = name_no_suffix.rsplit_once('@') {
let num = right
.parse::<i32>()
.with_context(|| format!("While parsing number part of {}", name))?;
.with_context(|| format!("While parsing number part of {name}"))?;
return Ok((left, num));
}
};
Expand Down

0 comments on commit 719cc37

Please sign in to comment.