Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to choose VM for start app #9

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ impl AdminClient {
Ok(response.into_inner().cmd_status)
}

pub async fn start(&self, app: String) -> anyhow::Result<()> {
let request = pb::admin::ApplicationRequest { app_name: app };
pub async fn start(&self, app: String, vm: Option<String>) -> anyhow::Result<()> {
let app_name = match vm {
Some(vm_name) => format!("{app}:{vm_name}"),
None => app,
};
let request = pb::admin::ApplicationRequest { app_name };
let response = self.connect_to().await?.start_application(request).await?;
// Ok(response.into_inner().cmd_status)
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions nixos/tests/admin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ in {
admin = adminSettings;
tls = mkTls "chromium-vm";
applications = lib.mkForce (builtins.toJSON {
"foot" = "run-waypipe foot";
"foot" = "/run/current-system/sw/bin/run-waypipe ${pkgs.foot}/bin/foot";
});
};
};
Expand Down Expand Up @@ -260,7 +260,7 @@ in {

return any(pattern in name for name in nodes)

retry(func)
retry(func, timeout=30)
# End of borrowed code

import time
Expand Down
18 changes: 14 additions & 4 deletions src/admin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,11 @@ impl AdminServiceImpl {
Ok(())
}

pub async fn start_vm(&self, name: String) -> anyhow::Result<()> {
pub async fn start_vm(&self, name: &str) -> anyhow::Result<()> {
let endpoint = self.host_endpoint()?;
let client = SystemDClient::new(endpoint);
let vm_name = format_vm_name(&name);
let (name, vm_name) = Self::parse_app_vm_pair(name);
let vm_name = format_vm_name(&name, vm_name);
let status = client
.get_remote_status(vm_name.clone())
.await
Expand Down Expand Up @@ -162,7 +163,7 @@ impl AdminServiceImpl {
}
(VmType::AppVM, ServiceType::Mgr) | (VmType::SysVM, ServiceType::Mgr) => {
let name = parse_service_name(&entry.name)?;
self.start_vm(name.to_string())
self.start_vm(name)
.await
.with_context(|| format!("handing error, by restart VM {}", &entry.name))?;
Ok(()) // FIXME: should use `?` from line above, why it didn't work?
Expand Down Expand Up @@ -232,17 +233,26 @@ impl AdminServiceImpl {
self.registry.register(entry)
}

fn parse_app_vm_pair(app_and_vm: &str) -> (&str, Option<&str>) {
if let Some((app, vm)) = app_and_vm.split_once(":") {
(app, Some(vm))
} else {
(app_and_vm, None)
}
}

pub async fn start_app(&self, req: ApplicationRequest) -> anyhow::Result<()> {
if self.state != State::VmsRegistered {
info!("not all required system-vms are registered")
}

let systemd_agent = format_service_name(&req.app_name);

// Entry unused in "go" code
match self.registry.by_name(&systemd_agent) {
std::result::Result::Ok(e) => e,
Err(_) => {
self.start_vm(req.app_name.clone())
self.start_vm(&req.app_name)
.await
.context(format!("Starting vm for {}", &req.app_name))?;
self.registry
Expand Down
1 change: 1 addition & 0 deletions src/bin/givc-agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {

let addr = SocketAddr::new(cli.addr.parse()?, cli.port);

// FIXME: Totally wrong,
let agent_service_name = format_service_name(&cli.name);

let mut builder = Server::builder();
Expand Down
4 changes: 3 additions & 1 deletion src/bin/givc-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ struct Cli {
#[derive(Debug, Subcommand)]
enum Commands {
Start {
#[arg(long)]
vm: Option<String>,
app: String,
},
Stop {
Expand Down Expand Up @@ -128,7 +130,7 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {

match cli.command {
Commands::Test { test } => test_subcommands(test, admin).await?,
Commands::Start { app } => admin.start(app).await?,
Commands::Start { app, vm } => admin.start(app, vm).await?,
Commands::Stop { app } => admin.stop(app).await?,

Commands::Pause { app } => admin.pause(app).await?,
Expand Down
29 changes: 15 additions & 14 deletions src/utils/naming.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
use anyhow::*;

pub fn format_vm_name(name: &String) -> String {
format!("microvm@{name}-vm.service")
pub fn format_vm_name(name: &str, vm: Option<&str>) -> String {
if let Some(vm_name) = vm {
format!("microvm@{vm_name}.service")
} else {
format!("microvm@{name}-vm.service")
}
}

pub fn format_service_name(name: &String) -> String {
pub fn format_service_name(name: &str) -> String {
format!("givc-{}-vm.service", name)
}

// FIXME: rewrite as `pub fn parse_service_name(name: &str) -> anyhow::Result<&str>`
pub fn parse_service_name(name: &String) -> anyhow::Result<String> {
pub fn parse_service_name(name: &str) -> anyhow::Result<&str> {
name.strip_suffix("-vm.service")
.and_then(|name| name.strip_prefix("givc-"))
.map(str::to_string)
.ok_or_else(|| anyhow!("Doesn't know how to parse VM name: {name}"))
}

// From `agent` code, ported for future
// FIXME: rewrite as `parse_application_name(name: &str) -> anyhow::Result<(&str, i32)>`
pub fn parse_application_name(name: &String) -> anyhow::Result<(String, i32)> {
pub fn parse_application_name(name: &str) -> anyhow::Result<(&str, i32)> {
if let Some(name_no_suffix) = name.strip_suffix(".service") {
if let Some((left, right)) = name_no_suffix.rsplit_once("@") {
let num = right
.parse::<i32>()
.with_context(|| format!("While parsing number part of {}", name))?;
return Ok((left.to_string(), num));
return Ok((left, num));
}
};
bail!("App name {} not it app@<number>.service format", name)
Expand All @@ -36,10 +37,10 @@ mod tests {

#[test]
fn test_parse_service_name() -> Result<()> {
let good = parse_service_name(&String::from("givc-good-vm.service"))?;
let good = parse_service_name("givc-good-vm.service")?;
assert_eq!(good, "good");

let bad = parse_service_name(&String::from("just-a.service"));
let bad = parse_service_name("just-a.service");
let err = bad.unwrap_err();
assert_eq!(
format!("{}", err.root_cause()),
Expand All @@ -50,10 +51,10 @@ mod tests {

#[test]
fn test_parse_application_name() -> Result<()> {
let good = parse_application_name(&String::from("[email protected]"))?;
assert_eq!(good, (String::from("good-app"), 42));
let good = parse_application_name("[email protected]")?;
assert_eq!(good, ("good-app", 42));

let bad = parse_application_name(&String::from("just-a.service"));
let bad = parse_application_name("just-a.service");
let err = bad.unwrap_err();
assert_eq!(
format!("{}", err.root_cause()),
Expand Down