From 25885b8e08252db20f2a50e64184b6a59e546a73 Mon Sep 17 00:00:00 2001 From: fx Date: Fri, 13 Oct 2023 15:26:05 +0200 Subject: stuff --- .github/workflows/test.yml | 2 +- Dockerfile.build | 16 ++++++++++++++++ src/main.rs | 4 ++-- 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 Dockerfile.build diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c1bfdf7..bf170f5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -54,4 +54,4 @@ jobs: push: true tags: | ghcr.io/fxqnlr/webol:dev-latest - ghcr.io/fxqnlr/webol:dev-${{ github.run_number }} \ No newline at end of file + ghcr.io/fxqnlr/webol:dev-${{ github.run_number }} diff --git a/Dockerfile.build b/Dockerfile.build new file mode 100644 index 0000000..3bbaf39 --- /dev/null +++ b/Dockerfile.build @@ -0,0 +1,16 @@ +FROM rust:1.73 as builder +WORKDIR /usr/src/webol +COPY . . +# RUN rustup target add armv7-unknown-linux-gnueabihf +# RUN apt update && apt install gcc-arm-linux-gnueabihf -y +# RUN CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=arm-linux-gnueabihf-gcc cargo install --path . --target armv7-unknown-linux-gnueabihf +RUN cargo install --path . + +# FROM --platform=arm64 debian:bullseye-slim +FROM debian:trixie-slim +# RUN apt-get update && apt-get install -y libc6 && rm -rf /var/lib/apt/lists/* +WORKDIR /usr/local/webol +COPY --from=builder /usr/local/cargo/bin/webol /usr/local/bin/webol + +EXPOSE 7229 +CMD ["webol"] diff --git a/src/main.rs b/src/main.rs index bb37dc2..e6c746c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,7 +51,7 @@ async fn main() { .route("/device", post(post_device)) .with_state(shared_state); - // run it with hyper on localhost:3000 + // TODO: Add to config axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) .serve(app.into_make_service()) .await @@ -76,4 +76,4 @@ async fn init_db_pool() -> PgPool { info!("dbPool successfully connected to '{}'", db_url); pool -} \ No newline at end of file +} -- cgit v1.2.3 From 6648d1da1df9a4e4e2945739fc0fd3c7f77643cb Mon Sep 17 00:00:00 2001 From: fx Date: Sat, 14 Oct 2023 22:01:33 +0200 Subject: test this fing shit on pi wohoo --- Dockerfile.arm.build | 16 ++++++++++++++++ Dockerfile.build | 24 +++++++++++++----------- README.md | 10 +++++++++- build.rs | 5 ----- docker-compose.yml | 1 + nobuild.rs.old | 5 +++++ src/main.rs | 15 ++++++++++----- src/routes/device.rs | 4 ++++ src/routes/start.rs | 2 +- 9 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 Dockerfile.arm.build delete mode 100644 build.rs create mode 100644 nobuild.rs.old diff --git a/Dockerfile.arm.build b/Dockerfile.arm.build new file mode 100644 index 0000000..1845afe --- /dev/null +++ b/Dockerfile.arm.build @@ -0,0 +1,16 @@ +FROM rust:1.73 as builder +WORKDIR /usr/src/webol +COPY . . +#RUN rustup target add armv7-unknown-linux-gnueabihf +#RUN apt update && apt install gcc-arm-linux-gnueabihf -y +#RUN CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=arm-linux-gnueabihf-gcc cargo install --path . --target armv7-unknown-linux-gnueabihf +RUN cargo install --path . + +#FROM --platform=arm64 debian:bookworm-slim +FROM debian:bookworm-slim +RUN apt update && apt install -y libc6 && rm -rf /var/lib/apt/lists/* +WORKDIR /usr/local/webol +COPY --from=builder /usr/local/cargo/bin/webol /usr/local/bin/webol + +EXPOSE 7229 +CMD ["webol"] diff --git a/Dockerfile.build b/Dockerfile.build index 3bbaf39..0158b8a 100644 --- a/Dockerfile.build +++ b/Dockerfile.build @@ -1,16 +1,18 @@ +FROM debian:bookworm AS deb_extractor +RUN cd /tmp && \ + apt-get update && apt-get download \ + libc6 && \ + mkdir /dpkg && \ + for deb in *.deb; do dpkg --extract $deb /dpkg || exit 10; done + FROM rust:1.73 as builder -WORKDIR /usr/src/webol +WORKDIR /app COPY . . -# RUN rustup target add armv7-unknown-linux-gnueabihf -# RUN apt update && apt install gcc-arm-linux-gnueabihf -y -# RUN CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=arm-linux-gnueabihf-gcc cargo install --path . --target armv7-unknown-linux-gnueabihf -RUN cargo install --path . +RUN cargo build --release -# FROM --platform=arm64 debian:bullseye-slim -FROM debian:trixie-slim -# RUN apt-get update && apt-get install -y libc6 && rm -rf /var/lib/apt/lists/* -WORKDIR /usr/local/webol -COPY --from=builder /usr/local/cargo/bin/webol /usr/local/bin/webol +FROM gcr.io/distroless/cc +COPY --from=builder /app/target/release/webol / +COPY --from=deb_extractor /dpkg / EXPOSE 7229 -CMD ["webol"] +ENTRYPOINT ["./webol"] diff --git a/README.md b/README.md index f062819..fdc7ad2 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -# webol \ No newline at end of file +# webol + +DATABASE_URL: `String` + +WEBOL_APIKEY: `String` + +WEBOL_SERVERADDR: `Option` + +WEBOL_BINDADDR: `Option` diff --git a/build.rs b/build.rs deleted file mode 100644 index 7609593..0000000 --- a/build.rs +++ /dev/null @@ -1,5 +0,0 @@ -// generated by `sqlx migrate build-script` -fn main() { - // trigger recompilation when a new migration is added - println!("cargo:rerun-if-changed=migrations"); -} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 1980356..9d73e9b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,7 @@ version: '3.1' services: db: image: postgres + container_name: webol_dev_postgres restart: no environment: POSTGRES_PASSWORD: postgres diff --git a/nobuild.rs.old b/nobuild.rs.old new file mode 100644 index 0000000..7609593 --- /dev/null +++ b/nobuild.rs.old @@ -0,0 +1,5 @@ +// generated by `sqlx migrate build-script` +fn main() { + // trigger recompilation when a new migration is added + println!("cargo:rerun-if-changed=migrations"); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e6c746c..b7306ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use sqlx::postgres::PgPoolOptions; use time::util::local_offset; use tracing::{debug, info, level_filters::LevelFilter}; use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; +use crate::config::SETTINGS; use crate::routes::device::{get_device, post_device, put_device}; use crate::routes::start::start; @@ -37,13 +38,12 @@ async fn main() { let version = env!("CARGO_PKG_VERSION"); - info!("starting webol v{}", version); + info!("start webol v{}", version); let db = init_db_pool().await; let shared_state = Arc::new(AppState { db }); - // build our application with a single route let app = Router::new() .route("/start", post(start)) .route("/device", get(get_device)) @@ -51,8 +51,9 @@ async fn main() { .route("/device", post(post_device)) .with_state(shared_state); - // TODO: Add to config - axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) + let addr = SETTINGS.get_string("serveraddr").unwrap_or("0.0.0.0:7229".to_string()); + info!("start server on {}", addr); + axum::Server::bind(&addr.parse().unwrap()) .serve(app.into_make_service()) .await .unwrap(); @@ -63,9 +64,13 @@ pub struct AppState { } async fn init_db_pool() -> PgPool { + #[cfg(not(debug_assertions))] + let db_url = SETTINGS.get_string("database.url").unwrap(); + + #[cfg(debug_assertions)] let db_url = env::var("DATABASE_URL").unwrap(); - debug!("attempting to connect dbPool to '{}'", db_url); + debug!("attempt to connect dbPool to '{}'", db_url); let pool = PgPoolOptions::new() .max_connections(5) diff --git a/src/routes/device.rs b/src/routes/device.rs index d5d7144..025c7d0 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs @@ -4,11 +4,13 @@ use axum::headers::HeaderMap; use axum::Json; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; +use tracing::info; use crate::auth::auth; use crate::db::Device; use crate::error::WebolError; pub async fn get_device(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, WebolError> { + info!("GET request"); let secret = headers.get("authorization"); if auth(secret).map_err(WebolError::Auth)? { let device = sqlx::query_as!( @@ -33,6 +35,7 @@ pub struct GetDevicePayload { } pub async fn put_device(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, WebolError> { + info!("PUT request"); let secret = headers.get("authorization"); if auth(secret).map_err(WebolError::Auth)? { sqlx::query!( @@ -64,6 +67,7 @@ pub struct PutDeviceResponse { } pub async fn post_device(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, WebolError> { + info!("POST request"); let secret = headers.get("authorization"); if auth(secret).map_err(WebolError::Auth)? { let device = sqlx::query_as!( diff --git a/src/routes/start.rs b/src/routes/start.rs index d16ea4e..2e6a648 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -28,7 +28,7 @@ pub async fn start(State(state): State>, headers: HeaderMap let bind_addr = SETTINGS .get_string("bindaddr") - .map_err(|err| WebolError::Server(Box::new(err)))?; + .unwrap_or("0.0.0.0:1111".to_string()); let _ = send_packet( &bind_addr.parse().map_err(|err| WebolError::Server(Box::new(err)))?, -- cgit v1.2.3 From d109f789b1cb75fc144a203628694dfb7ed300cb Mon Sep 17 00:00:00 2001 From: fx Date: Sat, 14 Oct 2023 23:14:09 +0200 Subject: test multiplatform build w/ actions --- .github/workflows/test.yml | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bf170f5..ed9fba6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,9 +2,9 @@ name: build on: push: - branches: [ "main" ] + branches: [ "main", "test" ] pull_request: - branches: [ "main" ] + branches: [ "main", "test" ] workflow_dispatch: env: @@ -12,7 +12,6 @@ env: RUSTC_WRAPPER: "sccache" SQLX_OFFLINE: "true" CARGO_TERM_COLOR: always - CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER: arm-linux-gnueabihf-gcc jobs: build: @@ -21,23 +20,25 @@ jobs: - name: Run sccache-cache uses: mozilla-actions/sccache-action@v0.0.3 - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: actions-rs/toolchain@v1 with: toolchain: stable - target: armv7-unknown-linux-gnueabihf components: rustfmt, clippy override: true - - name: Install arm linker - run: sudo apt update && sudo apt install gcc-arm-linux-gnueabihf -y + - name: Run cargo check + uses: actions-rs/cargo@v1 + with: + command: check - - name: Cargo build + - name: Run Clippy uses: actions-rs/cargo@v1 with: - command: build - args: --release --target armv7-unknown-linux-gnueabihf + command: clippy + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - name: Login to DockerHub @@ -50,8 +51,9 @@ jobs: id: docker_build uses: docker/build-push-action@v3 with: - file: Dockerfile + file: Dockerfile.build push: true + platforms: linux/amd64,linux/arm/v7 tags: | - ghcr.io/fxqnlr/webol:dev-latest - ghcr.io/fxqnlr/webol:dev-${{ github.run_number }} + ghcr.io/fxqnlr/webol:test-latest + ghcr.io/fxqnlr/webol:test-${{ github.run_number }} -- cgit v1.2.3 From 0948f75248a5daf8229ce1f40d1b3ec8d9aecbac Mon Sep 17 00:00:00 2001 From: fx Date: Sun, 15 Oct 2023 02:26:29 +0200 Subject: test w/ cargo chef + arm64 --- .github/workflows/test.yml | 7 +++---- Dockerfile | 27 +++++++++++++++++++++++---- Dockerfile.arm.build | 16 ---------------- Dockerfile.build | 18 ------------------ build.rs.disabled | 5 +++++ nobuild.rs.old | 5 ----- 6 files changed, 31 insertions(+), 47 deletions(-) delete mode 100644 Dockerfile.arm.build delete mode 100644 Dockerfile.build create mode 100644 build.rs.disabled delete mode 100644 nobuild.rs.old diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ed9fba6..649f295 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -51,9 +51,8 @@ jobs: id: docker_build uses: docker/build-push-action@v3 with: - file: Dockerfile.build push: true - platforms: linux/amd64,linux/arm/v7 + platforms: linux/amd64,linux/arm64 tags: | - ghcr.io/fxqnlr/webol:test-latest - ghcr.io/fxqnlr/webol:test-${{ github.run_number }} + ghcr.io/fxqnlr/webol:latest + ghcr.io/fxqnlr/webol:${{ github.run_number }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index d014c5a..5757f0d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,26 @@ -FROM --platform=amd64 debian:bullseye-slim +FROM debian:bookworm AS deb_extractor +RUN cd /tmp && \ + apt-get update && apt-get download \ + libc6 && \ + mkdir /dpkg && \ + for deb in *.deb; do dpkg --extract $deb /dpkg || exit 10; done -WORKDIR /usr/local/webol -COPY target/armv7-unknown-linux-gnueabihf/release/webol /usr/local/bin/webol +FROM lukemathwalker/cargo-chef:latest-rust-1.73.0 as chef +WORKDIR app + +FROM chef AS planner +COPY . . +RUN cargo chef prepare --recipe-path recipe.json + +FROM chef as builder +COPY --from=planner /app/recipe.json recipe.json +RUN cargo chef cook --release --recipe-path recipe.json +COPY . . +RUN cargo build --release + +FROM gcr.io/distroless/cc +COPY --from=builder /app/target/release/webol / +COPY --from=deb_extractor /dpkg / EXPOSE 7229 -CMD ["webol"] \ No newline at end of file +ENTRYPOINT ["./webol"] diff --git a/Dockerfile.arm.build b/Dockerfile.arm.build deleted file mode 100644 index 1845afe..0000000 --- a/Dockerfile.arm.build +++ /dev/null @@ -1,16 +0,0 @@ -FROM rust:1.73 as builder -WORKDIR /usr/src/webol -COPY . . -#RUN rustup target add armv7-unknown-linux-gnueabihf -#RUN apt update && apt install gcc-arm-linux-gnueabihf -y -#RUN CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=arm-linux-gnueabihf-gcc cargo install --path . --target armv7-unknown-linux-gnueabihf -RUN cargo install --path . - -#FROM --platform=arm64 debian:bookworm-slim -FROM debian:bookworm-slim -RUN apt update && apt install -y libc6 && rm -rf /var/lib/apt/lists/* -WORKDIR /usr/local/webol -COPY --from=builder /usr/local/cargo/bin/webol /usr/local/bin/webol - -EXPOSE 7229 -CMD ["webol"] diff --git a/Dockerfile.build b/Dockerfile.build deleted file mode 100644 index 0158b8a..0000000 --- a/Dockerfile.build +++ /dev/null @@ -1,18 +0,0 @@ -FROM debian:bookworm AS deb_extractor -RUN cd /tmp && \ - apt-get update && apt-get download \ - libc6 && \ - mkdir /dpkg && \ - for deb in *.deb; do dpkg --extract $deb /dpkg || exit 10; done - -FROM rust:1.73 as builder -WORKDIR /app -COPY . . -RUN cargo build --release - -FROM gcr.io/distroless/cc -COPY --from=builder /app/target/release/webol / -COPY --from=deb_extractor /dpkg / - -EXPOSE 7229 -ENTRYPOINT ["./webol"] diff --git a/build.rs.disabled b/build.rs.disabled new file mode 100644 index 0000000..7609593 --- /dev/null +++ b/build.rs.disabled @@ -0,0 +1,5 @@ +// generated by `sqlx migrate build-script` +fn main() { + // trigger recompilation when a new migration is added + println!("cargo:rerun-if-changed=migrations"); +} \ No newline at end of file diff --git a/nobuild.rs.old b/nobuild.rs.old deleted file mode 100644 index 7609593..0000000 --- a/nobuild.rs.old +++ /dev/null @@ -1,5 +0,0 @@ -// generated by `sqlx migrate build-script` -fn main() { - // trigger recompilation when a new migration is added - println!("cargo:rerun-if-changed=migrations"); -} \ No newline at end of file -- cgit v1.2.3 From 981e5c548e7098f4a391384d316ec7a52c1fa979 Mon Sep 17 00:00:00 2001 From: fx Date: Sun, 15 Oct 2023 13:02:28 +0200 Subject: auth logging changes --- src/auth.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/auth.rs b/src/auth.rs index b7693a0..81e798f 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,21 +1,26 @@ use std::error::Error; use axum::headers::HeaderValue; use axum::http::StatusCode; -use tracing::error; +use tracing::{debug, error, trace}; use crate::auth::AuthError::{MissingSecret, ServerError, WrongSecret}; use crate::config::SETTINGS; pub fn auth(secret: Option<&HeaderValue>) -> Result { + debug!("auth request with secret {:?}", secret); if let Some(value) = secret { + trace!("value exists"); let key = SETTINGS .get_string("apikey") .map_err(|err| ServerError(Box::new(err)))?; if value.to_str().map_err(|err| ServerError(Box::new(err)))? == key.as_str() { + debug!("successful auth"); Ok(true) } else { + debug!("unsuccessful auth (wrong secret)"); Err(WrongSecret) } } else { + debug!("unsuccessful auth (no secret)"); Err(MissingSecret) } } -- cgit v1.2.3 From 82463cb3c3b107dbeed2b5f2cec0751bdae4b05d Mon Sep 17 00:00:00 2001 From: fx Date: Sun, 15 Oct 2023 14:53:23 +0200 Subject: test with build.rs and cache-to and from --- .github/workflows/test.yml | 8 +++++--- build.rs | 5 +++++ build.rs.disabled | 5 ----- 3 files changed, 10 insertions(+), 8 deletions(-) create mode 100644 build.rs delete mode 100644 build.rs.disabled diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 649f295..97a0d2f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,7 +4,7 @@ on: push: branches: [ "main", "test" ] pull_request: - branches: [ "main", "test" ] + branches: [ "main" ] workflow_dispatch: env: @@ -53,6 +53,8 @@ jobs: with: push: true platforms: linux/amd64,linux/arm64 + cache-from: type=gha + cache-to: type=gha,mode=max tags: | - ghcr.io/fxqnlr/webol:latest - ghcr.io/fxqnlr/webol:${{ github.run_number }} \ No newline at end of file + ghcr.io/fxqnlr/webol:dev-latest + ghcr.io/fxqnlr/webol:dev-${{ github.run_number }} \ No newline at end of file diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..7609593 --- /dev/null +++ b/build.rs @@ -0,0 +1,5 @@ +// generated by `sqlx migrate build-script` +fn main() { + // trigger recompilation when a new migration is added + println!("cargo:rerun-if-changed=migrations"); +} \ No newline at end of file diff --git a/build.rs.disabled b/build.rs.disabled deleted file mode 100644 index 7609593..0000000 --- a/build.rs.disabled +++ /dev/null @@ -1,5 +0,0 @@ -// generated by `sqlx migrate build-script` -fn main() { - // trigger recompilation when a new migration is added - println!("cargo:rerun-if-changed=migrations"); -} \ No newline at end of file -- cgit v1.2.3 From 912323d9e42a45e05fa34778dad75ced7b0096fc Mon Sep 17 00:00:00 2001 From: fx Date: Sun, 15 Oct 2023 16:26:16 +0200 Subject: added log for build test --- src/routes/start.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/routes/start.rs b/src/routes/start.rs index 2e6a648..163d58c 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -12,6 +12,7 @@ use crate::db::Device; use crate::error::WebolError; pub async fn start(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, WebolError> { + info!("POST request"); let secret = headers.get("authorization"); if auth(secret).map_err(WebolError::Auth)? { let device = sqlx::query_as!( -- cgit v1.2.3 From bd7ec2d19da0793963a7aab355873758ae4e0601 Mon Sep 17 00:00:00 2001 From: FxQnLr <39925636+FxQnLr@users.noreply.github.com> Date: Sun, 15 Oct 2023 16:51:24 +0200 Subject: Update test.yml --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 97a0d2f..90a5fea 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,7 +2,7 @@ name: build on: push: - branches: [ "main", "test" ] + branches: [ "main" ] pull_request: branches: [ "main" ] workflow_dispatch: @@ -57,4 +57,4 @@ jobs: cache-to: type=gha,mode=max tags: | ghcr.io/fxqnlr/webol:dev-latest - ghcr.io/fxqnlr/webol:dev-${{ github.run_number }} \ No newline at end of file + ghcr.io/fxqnlr/webol:dev-${{ github.run_number }} -- cgit v1.2.3