Skip to content

Commit

Permalink
update rust version & deps (#53)
Browse files Browse the repository at this point in the history
* update rust version & deps

* roll back yaml

* pest error clippy warning

* update dora to 0.2.0
  • Loading branch information
leshow committed Apr 22, 2024
1 parent cd4e175 commit a253f28
Show file tree
Hide file tree
Showing 12 changed files with 784 additions and 509 deletions.
1,179 changes: 712 additions & 467 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ trust-dns-proto = { version = "0.22.0", default-features = false, features = [
"dnssec",
"serde-config",
] }
socket2 = { version = "0.4.9", features = [
socket2 = { version = "0.5.6", features = [
"all",
] } # TODO: update when tokio sockets impl AsFd, then update unix-udp-sock
anyhow = { version = "1.0", features = ["backtrace"] }
async-trait = "0.1"
bytes = "1.1"
clap = { version = "4.1.8", features = ["derive", "env"] }
dhcproto = "0.10.0"
clap = { version = "4.5.4", features = ["derive", "env"] }
dhcproto = "0.11.0"
futures = { version = "0.3", default-features = false, features = ["std"] }
ipnet = { features = ["serde"], version = "2.4.0" }
pnet = { features = ["serde", "std"], version = "0.33.0" }
pnet = { features = ["serde", "std"], version = "0.34.0" }
prometheus = "0.13.0"
prometheus-static-metric = "0.5"
tokio = { version = "1.26.0", features = ["full"] }
tracing = "0.1.22"
tracing-futures = "0.2"
tokio = { version = "1.37.0", features = ["full"] }
tracing = "0.1.40"
tracing-futures = "0.2.5"
tracing-subscriber = { features = ["env-filter", "json"], version = "0.3" }
thiserror = "1.0"
rand = "0.8"
rand = "0.8.5"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_yaml = "0.8"
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.72.0 as builder
FROM rust:1.77.2 as builder
# set workdir
WORKDIR /usr/src/dora
COPY . .
Expand Down
2 changes: 1 addition & 1 deletion bin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dora-bin"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
default-run = "dora"
Expand Down
11 changes: 6 additions & 5 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@ async fn start(config: cli::Config) -> Result<()> {
info!(?dora_id, "using id");
// setting DORA_ID for other plugins
std::env::set_var("DORA_ID", &dora_id);
// start external api for healthchecks
let api = ExternalApi::new(config.external_api);

debug!("parsing DHCP config");
let dhcp_cfg = Arc::new(DhcpConfig::parse(&config.config_path)?);
debug!("starting database");
let ip_mgr = Arc::new(IpManager::new(SqliteDb::new(database_url).await?)?);
// start external api for healthchecks
let api = ExternalApi::new(config.external_api, Arc::clone(&ip_mgr));
// start v4 server
debug!("starting v4 server");
let mut v4: Server<v4::Message> =
Expand All @@ -82,9 +85,7 @@ async fn start(config: cli::Config) -> Result<()> {
StaticAddr::new(Arc::clone(&dhcp_cfg))?.register(&mut v4);
// leases plugin

let ip_mgr = IpManager::new(SqliteDb::new(database_url).await?)?;

Leases::new(Arc::clone(&dhcp_cfg), ip_mgr).register(&mut v4);
Leases::new(Arc::clone(&dhcp_cfg), Arc::clone(&ip_mgr)).register(&mut v4);

let v6 = if dhcp_cfg.has_v6() {
// start v6 server
Expand Down
2 changes: 1 addition & 1 deletion dora-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ rand = { workspace = true }
clap = { workspace = true }
socket2 = { workspace = true }
libc = "0.2.126"
unix-udp-sock = "0.7.0"
unix-udp-sock = "0.7.1"
pnet = { workspace = true }

[dev-dependencies]
Expand Down
8 changes: 6 additions & 2 deletions external-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ license = "MPL-2.0"

[dependencies]
dora-core = { path = "../dora-core" }
ip-manager = { path = "../libs/ip-manager" }

# libs
anyhow = { workspace = true }
axum = "0.6.10"
axum = "0.7.5"
tokio = { workspace = true }
tracing-futures = { workspace = true }
tracing = { workspace = true }
Expand All @@ -23,4 +24,7 @@ prometheus = { workspace = true }


[dev-dependencies]
reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls"] }
reqwest = { version = "0.12", default-features = false, features = [
"json",
"rustls-tls",
] }
37 changes: 24 additions & 13 deletions external-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

use anyhow::{bail, Result};
use axum::{extract::Extension, routing, Router};
use tokio::{sync::mpsc, task::JoinHandle};
use ip_manager::{IpManager, Storage};
use tokio::{net::TcpListener, sync::mpsc, task::JoinHandle};
use tracing::{error, info, trace};

use std::net::SocketAddr;
use std::{net::SocketAddr, sync::Arc};

pub use crate::models::{Health, State};

Expand All @@ -43,16 +44,17 @@ impl Drop for ExternalApiGuard {
/// Listens to relevant channels to gather information about
/// the running system and reports this data in an HTTP API
#[derive(Debug)]
pub struct ExternalApi {
pub struct ExternalApi<S> {
tx: mpsc::Sender<Health>,
rx: mpsc::Receiver<Health>,
addr: SocketAddr,
state: State,
ip_mgr: Arc<IpManager<S>>,
}

impl ExternalApi {
impl<S: Storage> ExternalApi<S> {
/// Create a new ExternalApi instance
pub fn new(addr: SocketAddr) -> Self {
pub fn new(addr: SocketAddr, ip_mgr: Arc<IpManager<S>>) -> Self {
trace!("starting external api");
let (tx, rx) = mpsc::channel(10);
let state = models::blank_health();
Expand All @@ -61,6 +63,7 @@ impl ExternalApi {
rx,
addr,
state,
ip_mgr,
}
}

Expand All @@ -87,7 +90,8 @@ impl ExternalApi {
}

/// serve the HTTP external api
async fn run(state: State, addr: SocketAddr) -> Result<()> {
async fn run(addr: SocketAddr, state: State, ip_mgr: Arc<IpManager<S>>) -> Result<()> {
let tcp = TcpListener::bind(&addr).await?;
// Provides:
// /health
// /ping
Expand All @@ -98,13 +102,12 @@ impl ExternalApi {
.route("/ping", routing::get(handlers::ping))
.route("/metrics", routing::get(handlers::metrics))
.route("/metrics-text", routing::get(handlers::metrics_text))
.layer(Extension(state));
.layer(Extension(state))
.layer(Extension(ip_mgr));

tracing::debug!("external API listening on {}", addr);

axum::Server::bind(&addr)
.serve(app.into_make_service())
.await?;
axum::serve(tcp, app).await?;
bail!("external API returned-- should not happen")
}

Expand All @@ -113,9 +116,12 @@ impl ExternalApi {
pub fn start(mut self) -> JoinHandle<()> {
let state = self.state.clone();
let addr = self.addr;
let ip_mgr = self.ip_mgr.clone();
// if tx is not cloned, health listen will never update since ExternalApi is owner

tokio::spawn(async move {
if let Err(err) = tokio::try_join!(ExternalApi::run(state, addr), self.listen_status())
if let Err(err) =
tokio::try_join!(ExternalApi::run(addr, state, ip_mgr), self.listen_status())
{
error!(?err, "health task returning, this should not happen")
}
Expand All @@ -131,6 +137,7 @@ impl ExternalApi {
}

mod handlers {

use crate::models::{Health, State};
use axum::{
body::Body,
Expand Down Expand Up @@ -235,10 +242,13 @@ pub mod models {
mod tests {
use std::time::Duration;

use ip_manager::sqlite::SqliteDb;

use super::*;
#[tokio::test]
async fn test_health() -> anyhow::Result<()> {
let api = ExternalApi::new("0.0.0.0:8889".parse().unwrap());
let mgr = Arc::new(IpManager::new(SqliteDb::new("sqlite::memory:").await?)?);
let api = ExternalApi::new("0.0.0.0:8889".parse().unwrap(), mgr);
let _handle = api.serve();
// wait for server to come up
tokio::time::sleep(Duration::from_secs(1)).await;
Expand All @@ -260,7 +270,8 @@ mod tests {
// very simple test for existence of metrics endpoint
#[tokio::test]
async fn test_metrics() -> anyhow::Result<()> {
let api = ExternalApi::new("0.0.0.0:8888".parse().unwrap());
let mgr = Arc::new(IpManager::new(SqliteDb::new("sqlite::memory:").await?)?);
let api = ExternalApi::new("0.0.0.0:8888".parse().unwrap(), mgr);
let _handle = api.serve();
// wait for server to come up
tokio::time::sleep(Duration::from_secs(1)).await;
Expand Down
13 changes: 8 additions & 5 deletions libs/client-classification/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,20 @@ pub enum ParseErr {
Bool(String),
#[error("undefined with: {0:?}")]
Undefined(Rule),
#[error("pest error {0:?}")]
PestErr(#[from] pest::error::Error<Rule>),
#[error("pest error: {0:?}")]
PestErr(#[from] Box<pest::error::Error<Rule>>),
}

impl From<pest::error::Error<Rule>> for ParseErr {
fn from(value: pest::error::Error<Rule>) -> Self {
ParseErr::PestErr(Box::new(value))
}
}

#[allow(clippy::result_large_err)]
pub fn parse<S: AsRef<str>>(expr: S) -> ParseResult<Expr> {
build_ast(PredicateParser::parse(Rule::expr, expr.as_ref())?)
}

#[allow(clippy::result_large_err)]
pub fn build_ast(pair: Pairs<Rule>) -> ParseResult<Expr> {
let climber = PrattParser::new()
.op(Op::infix(Rule::or, Assoc::Left))
Expand All @@ -115,7 +119,6 @@ pub fn build_ast(pair: Pairs<Rule>) -> ParseResult<Expr> {
parse_expr(pair, &climber)
}

#[allow(clippy::result_large_err)]
fn parse_expr(pairs: Pairs<Rule>, pratt: &PrattParser<Rule>) -> ParseResult<Expr> {
pratt
.map_primary(|primary| {
Expand Down
11 changes: 11 additions & 0 deletions libs/ip-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use tracing::{debug, error, info, trace, warn};

pub mod sqlite;

use core::fmt;
use std::{
collections::HashSet,
net::{IpAddr, Ipv4Addr},
Expand Down Expand Up @@ -161,6 +162,16 @@ pub struct IpManager<T> {
ping_cache: moka::future::Cache<IpAddr, Option<PingReply>>,
}

impl<T> fmt::Debug for IpManager<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("IpManager {{}}")
// .field("store", &self.store)
// .field("icmpv4", &self.icmpv4)
// .field("ping_cache", &self.ping_cache)
.finish()
}
}

impl<T: Clone> Clone for IpManager<T> {
fn clone(&self) -> Self {
Self {
Expand Down
10 changes: 5 additions & 5 deletions plugins/leases/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ where
{
cfg: Arc<DhcpConfig>,
ddns: DdnsUpdate,
ip_mgr: IpManager<S>,
ip_mgr: Arc<IpManager<S>>,
renew_cache: Option<RenewThreshold<Vec<u8>>>,
}

Expand All @@ -63,7 +63,7 @@ impl<S> Leases<S>
where
S: Storage,
{
pub fn new(cfg: Arc<DhcpConfig>, ip_mgr: IpManager<S>) -> Self {
pub fn new(cfg: Arc<DhcpConfig>, ip_mgr: Arc<IpManager<S>>) -> Self {
Self {
renew_cache: cfg.v4().cache_threshold().map(RenewThreshold::new),
ip_mgr,
Expand Down Expand Up @@ -473,7 +473,7 @@ mod tests {
async fn test_request() -> Result<()> {
let cfg = DhcpConfig::parse_str(SAMPLE_YAML).unwrap();
// println!("{cfg:#?}");
let mgr = IpManager::new(SqliteDb::new("sqlite::memory:").await?)?;
let mgr = Arc::new(IpManager::new(SqliteDb::new("sqlite::memory:").await?)?);
let leases = Leases::new(Arc::new(cfg.clone()), mgr);
let mut ctx = message_type::util::blank_ctx(
"192.168.0.1:67".parse()?,
Expand All @@ -496,7 +496,7 @@ mod tests {
#[traced_test]
async fn test_discover() -> Result<()> {
let cfg = DhcpConfig::parse_str(SAMPLE_YAML).unwrap();
let mgr = IpManager::new(SqliteDb::new("sqlite::memory:").await?)?;
let mgr = Arc::new(IpManager::new(SqliteDb::new("sqlite::memory:").await?)?);
let leases = Leases::new(Arc::new(cfg.clone()), mgr);
let mut ctx = message_type::util::blank_ctx(
"192.168.0.1:67".parse()?,
Expand Down Expand Up @@ -532,7 +532,7 @@ mod tests {
async fn test_release() -> Result<()> {
let cfg = DhcpConfig::parse_str(SAMPLE_YAML).unwrap();
let mgr = IpManager::new(SqliteDb::new("sqlite::memory:").await?)?;
let leases = Leases::new(Arc::new(cfg.clone()), mgr);
let leases = Leases::new(Arc::new(cfg.clone()), Arc::new(mgr));
let mut ctx = message_type::util::blank_ctx(
"192.168.0.1:67".parse()?,
"192.168.0.1".parse()?,
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.72.0
1.77.2

0 comments on commit a253f28

Please sign in to comment.