xtask: implement xtask run subcommand

This new subcommand provides a convenient way to run your aya application.

Features include:
    - automatic compilation of userspace + bpf code
    - support for user-defined runners, defaulting to `sudo -E` for convenience
    - no need to run `sudo cargo run` (gross) or `sudo target/debug/myapp` (also gross),
      it just works
    - an optional `-p` flag which automatically supplies the correct --path flag to the
      application
    - support for trailing args after a final -- which are passed to the application,
      cargo run-style
pull/20/head
William Findlay 3 years ago
parent 509073c454
commit d590d3a1ea
No known key found for this signature in database
GPG Key ID: 7162B44E9E560373

@ -33,12 +33,12 @@ impl std::fmt::Display for Architecture {
#[derive(StructOpt)] #[derive(StructOpt)]
pub struct Options { pub struct Options {
#[structopt(default_value = "bpfel-unknown-none", long)] #[structopt(default_value = "bpfel-unknown-none", long)]
target: Architecture, pub target: Architecture,
#[structopt(long)] #[structopt(long)]
release: bool, pub release: bool,
} }
pub fn build(opts: Options) -> Result<(), anyhow::Error> { pub fn build_ebpf(opts: Options) -> Result<(), anyhow::Error> {
let dir = PathBuf::from("{{project-name}}-ebpf"); let dir = PathBuf::from("{{project-name}}-ebpf");
let target = format!("--target={}", opts.target); let target = format!("--target={}", opts.target);
let mut args = vec![ let mut args = vec![

@ -1,4 +1,5 @@
mod build_ebpf; mod build_ebpf;
mod run;
use std::process::exit; use std::process::exit;
@ -12,6 +13,7 @@ pub struct Options {
#[derive(StructOpt)] #[derive(StructOpt)]
enum Command { enum Command {
BuildEbpf(build_ebpf::Options), BuildEbpf(build_ebpf::Options),
Run(run::Options),
} }
fn main() { fn main() {
@ -19,7 +21,8 @@ fn main() {
use Command::*; use Command::*;
let ret = match opts.command { let ret = match opts.command {
BuildEbpf(opts) => build_ebpf::build(opts), BuildEbpf(opts) => build_ebpf::build_ebpf(opts),
Run(opts) => run::run(opts),
}; };
if let Err(e) = ret { if let Err(e) = ret {

@ -0,0 +1,75 @@
use std::{os::unix::process::CommandExt, process::Command};
use anyhow::Context as _;
use structopt::StructOpt;
use crate::build_ebpf::{build_ebpf, Architecture, Options as BuildOptions};
#[derive(StructOpt)]
pub struct Options {
/// Set the endianness of the BPF target
#[structopt(default_value = "bpfel-unknown-none", long)]
pub bpf_target: Architecture,
/// Build and run the release target
#[structopt(long)]
pub release: bool,
/// The command used to wrap your application
#[structopt(short, long, default_value = "sudo -E")]
pub runner: String,
/// A convenience flag that will supply `--path /path/to/bpf/object` to your application
#[structopt(short = "p", long)]
pub supply_path: bool,
/// Arguments to pass to your application
#[structopt(name = "args", last = true)]
pub run_args: Vec<String>,
}
/// Build the project
fn build(opts: &Options) -> Result<(), anyhow::Error> {
let mut args = vec!["build"];
if opts.release {
args.push("--release")
}
let status = Command::new("cargo")
.args(&args)
.status()
.expect("failed to build userspace");
assert!(status.success());
Ok(())
}
/// Build and run the project
pub fn run(opts: Options) -> Result<(), anyhow::Error> {
// build our ebpf program followed by our application
build_ebpf(BuildOptions {
target: opts.bpf_target,
release: opts.release,
})
.context("Error while building eBPF program")?;
build(&opts).context("Error while building userspace application")?;
// profile we are building (release or debug)
let profile = if opts.release { "release" } else { "debug" };
let bin_path = format!("target/{}/{{project-name}}", profile);
let bpf_path = format!("target/{}/{}/{{project-name}}", opts.bpf_target, profile);
// arguments to pass to the application
let mut run_args: Vec<_> = opts.run_args.iter().map(String::as_str).collect();
if opts.supply_path {
run_args.push("--path");
run_args.push(bpf_path.as_str());
};
// configure args
let mut args: Vec<_> = opts.runner.trim().split_terminator(" ").collect();
args.push(bin_path.as_str());
args.append(&mut run_args);
// spawn the command
let err = Command::new(args.get(0).expect("No first argument"))
.args(args.iter().skip(1))
.exec();
// we shouldn't get here unless the command failed to spawn
Err(anyhow::Error::from(err).context(format!("Failed to run `{}`", args.join(" "))))
}
Loading…
Cancel
Save