From 0ac7a0447716cdaccc03b6782f2149da4c03b3da Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 12 Dec 2024 03:44:08 +0100 Subject: [PATCH 1/8] `rdserver`: Add `.healthcheck` route for monitoring purposes --- acrate_rdserver/src/main.rs | 1 + acrate_rdserver/src/route.rs | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/acrate_rdserver/src/main.rs b/acrate_rdserver/src/main.rs index 3752cbf..ac14b4a 100644 --- a/acrate_rdserver/src/main.rs +++ b/acrate_rdserver/src/main.rs @@ -21,6 +21,7 @@ async fn main() -> anyhow::Result { log::trace!("Creating Axum router..."); let app = axum::Router::new() .route("/*path", axum::routing::get(route::webfinger_handler)) + .route("/.healthcheck", axum::routing::get(route::healthcheck_handler)) .layer(Extension(Arc::new(mj))); log::trace!("Axum router created successfully!"); diff --git a/acrate_rdserver/src/route.rs b/acrate_rdserver/src/route.rs index 61ba271..25bfbaa 100644 --- a/acrate_rdserver/src/route.rs +++ b/acrate_rdserver/src/route.rs @@ -13,6 +13,17 @@ use acrate_rd::jrd::ResourceDescriptorLinkJRD; use acrate_rd::xrd::{ResourceDescriptorLinkXRD, ResourceDescriptorPropertyXRD, ResourceDescriptorTitleXRD}; use crate::config; +pub async fn healthcheck_handler() -> Result { + log::info!("Handling an healthcheck request!"); + + log::trace!("Making sure the database is up..."); + let _conn = AsyncPgConnection::establish(config::ACRATE_WEBFINGER_DATABASE_URL()) + .await + .map_err(|_| StatusCode::BAD_GATEWAY)?; + + Ok(StatusCode::NO_CONTENT) +} + #[derive(Debug, Clone, Deserialize)] pub struct WebfingerQuery { pub resource: Option, -- 2.45.2 From b9bd59dc134b0a0ae874deb64da1e16f5a4e2dfe Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 12 Dec 2024 03:46:36 +0100 Subject: [PATCH 2/8] Configure `log` crate features --- acrate_database/Cargo.toml | 2 +- acrate_nodeinfo/Cargo.toml | 2 +- acrate_rdserver/Cargo.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/acrate_database/Cargo.toml b/acrate_database/Cargo.toml index ba63f83..d2afc81 100644 --- a/acrate_database/Cargo.toml +++ b/acrate_database/Cargo.toml @@ -13,7 +13,7 @@ categories = ["database"] diesel = { version = "2.2.4", features = ["postgres", "uuid"] } diesel-async = { version = "0.5.1", features = ["postgres"] } diesel_migrations = { version = "2.2.0", optional = true } -log = "0.4.22" +log = { version = "0.4.22", features = ["std", "max_level_trace", "release_max_level_debug"] } mediatype = "0.19.18" micronfig = { version = "0.3.0", optional = true } mime = "0.3.17" diff --git a/acrate_nodeinfo/Cargo.toml b/acrate_nodeinfo/Cargo.toml index 5e7693e..b137bc0 100644 --- a/acrate_nodeinfo/Cargo.toml +++ b/acrate_nodeinfo/Cargo.toml @@ -11,7 +11,7 @@ categories = ["web-programming"] [dependencies] acrate_rd = { path = "../acrate_rd" } -log = "0.4.22" +log = { version = "0.4.22", features = ["std", "max_level_trace", "release_max_level_debug"] } mediatype = { version = "0.19.18", features = ["serde"] } reqwest = { version = "0.12.9", features = ["json", "stream"] } serde = { version = "1.0.214", features = ["derive"] } diff --git a/acrate_rdserver/Cargo.toml b/acrate_rdserver/Cargo.toml index 4752f69..43b0148 100644 --- a/acrate_rdserver/Cargo.toml +++ b/acrate_rdserver/Cargo.toml @@ -15,7 +15,7 @@ acrate_rd = { path = "../acrate_rd" } anyhow = "1.0.93" axum = { version = "0.7.7", features = ["macros"] } axum-extra = { version = "0.9.4", features = ["query"] } -log = "0.4.22" +log = { version = "0.4.22", features = ["std", "max_level_trace", "release_max_level_debug"] } micronfig = "0.3.0" minijinja = "2.5.0" pretty_env_logger = "0.5.0" -- 2.45.2 From 79fa482b5b3c9f932485f34445d58c9cc8ead72c Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 12 Dec 2024 04:00:04 +0100 Subject: [PATCH 3/8] `rdserver`: Upgrade "starting server" log to info --- acrate_rdserver/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acrate_rdserver/src/main.rs b/acrate_rdserver/src/main.rs index ac14b4a..60dffed 100644 --- a/acrate_rdserver/src/main.rs +++ b/acrate_rdserver/src/main.rs @@ -32,7 +32,7 @@ async fn main() -> anyhow::Result { .context("failed to bind listener to address")?; log::trace!("Tokio listener bound to: {bind_address}"); - log::debug!("Starting server..."); + log::info!("Starting server..."); axum::serve(listener, app) .await .context("server exited with error")?; -- 2.45.2 From 9622d3e18842c0803bdd2ac70b9093e978832d20 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 12 Dec 2024 04:12:07 +0100 Subject: [PATCH 4/8] `docker`: Set up docker staging environment --- .idea/jsonSchemas.xml | 25 ++++++++ acrate_docker/Dockerfile | 32 ++++++++++ acrate_docker/compose.yml | 89 ++++++++++++++++++++++++++++ acrate_docker/config/caddy/Caddyfile | 16 +++++ 4 files changed, 162 insertions(+) create mode 100644 .idea/jsonSchemas.xml create mode 100644 acrate_docker/Dockerfile create mode 100644 acrate_docker/compose.yml create mode 100644 acrate_docker/config/caddy/Caddyfile diff --git a/.idea/jsonSchemas.xml b/.idea/jsonSchemas.xml new file mode 100644 index 0000000..da97271 --- /dev/null +++ b/.idea/jsonSchemas.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/acrate_docker/Dockerfile b/acrate_docker/Dockerfile new file mode 100644 index 0000000..7af2e9e --- /dev/null +++ b/acrate_docker/Dockerfile @@ -0,0 +1,32 @@ +FROM rust AS base_builder +WORKDIR /usr/src/acrate +COPY --from=source ./acrate_database ./acrate_database +COPY --from=source ./acrate_nodeinfo ./acrate_nodeinfo +COPY --from=source ./acrate_rd ./acrate_rd +COPY --from=source ./acrate_rdserver ./acrate_rdserver +COPY --from=source ./Cargo.toml ./Cargo.toml +COPY --from=source ./Cargo.lock ./Cargo.lock + +FROM rust:slim AS base_runner +RUN apt-get update +RUN apt-get upgrade --assume-yes +RUN apt-get install --assume-yes libpq5 +WORKDIR /usr/local/bin +ENV RUST_LOG="warn" + +FROM base_builder AS migrate_build +RUN cargo build --release --package=acrate_database --features=bin --bin=acrate_database_migrate + +FROM base_runner AS migrate +COPY --from=migrate_build /usr/src/acrate/target/release/acrate_database_migrate /usr/local/bin/acrate_database_migrate +ENTRYPOINT ["acrate_database_migrate"] +ENV RUST_LOG="warn,acrate_database_migrate=info" + +FROM base_builder AS rdserver_build +RUN cargo build --release --package=acrate_rdserver --bin=acrate_rdserver + +FROM base_runner AS rdserver +COPY --from=rdserver_build /usr/src/acrate/target/release/acrate_rdserver /usr/local/bin/acrate_rdserver +ENTRYPOINT ["acrate_rdserver"] +HEALTHCHECK CMD ["curl", "http://127.0.0.1/.healthcheck"] +ENV RUST_LOG="warn,acrate_rdserver=info" diff --git a/acrate_docker/compose.yml b/acrate_docker/compose.yml new file mode 100644 index 0000000..9da9246 --- /dev/null +++ b/acrate_docker/compose.yml @@ -0,0 +1,89 @@ +# Full acrate stack, running on a single machine for experimental purposes + +x-config: + ingress_config_dir: &ingress_config_dir "./config/caddy" + +name: "acrate" + +volumes: + ingress_data: + ingress_config: + postgres_data: + +services: + # Public ingress node + ingress: + image: "caddy" + restart: "unless-stopped" + cap_add: + - "NET_ADMIN" + ports: + - protocol: "tcp" + target: 80 + published: 80 + - protocol: "tcp" + target: 443 + published: 443 + - protocol: "udp" + target: 443 + published: 443 + volumes: + - type: "volume" + source: "ingress_data" + target: "/data" + - type: "volume" + source: "ingress_config" + target: "/config" + - type: "bind" + source: *ingress_config_dir + target: "/etc/caddy" + + # Main database + database: + image: "postgres" + restart: "unless-stopped" + environment: + # Make sure the password is the same for both client and server tools + POSTGRES_PASSWORD: &postgres_password "acrate" + PGUSER: "postgres" + PGPASS: *postgres_password + volumes: + - type: "volume" + source: "postgres_data" + target: "/var/lib/postgresql/data" + expose: + - 5432 + healthcheck: + test: ["CMD-SHELL", "pg_isready"] + + # Migrations + migrate: + build: + dockerfile: "./Dockerfile" + additional_contexts: + - "source=.." + target: "migrate" + environment: + ACRATE_DATABASE_DATABASE_URL: &database_url "postgres:///postgres?host=database&user=postgres&password=acrate" # TODO: Split parameters off to their own envvars + depends_on: + database: + condition: "service_healthy" + + # Resource descriptor server + rdserver: + build: + dockerfile: "./Dockerfile" + additional_contexts: + - "source=.." + target: "rdserver" + restart: "unless-stopped" + environment: + ACRATE_WEBFINGER_BIND_ADDRESS: "0.0.0.0:80" + ACRATE_WEBFINGER_DATABASE_URL: *database_url + expose: + - 80 + depends_on: + database: + condition: "service_healthy" + migrate: + condition: "service_completed_successfully" diff --git a/acrate_docker/config/caddy/Caddyfile b/acrate_docker/config/caddy/Caddyfile new file mode 100644 index 0000000..6514746 --- /dev/null +++ b/acrate_docker/config/caddy/Caddyfile @@ -0,0 +1,16 @@ +# replace with your HTTPS domain +:80 { + @rdserver { + path "/.well-known/webfinger" + path "/.well-known/host-meta" + path "/.well-known/host-meta.xml" + path "/.well-known/host-meta.json" + } + + reverse_proxy @rdserver { + to "http://rdserver" + + health_uri "/.healthcheck" + health_status "204" + } +} -- 2.45.2 From 257ed5475c838f33640b262132c3e0f06fc60b77 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 12 Dec 2024 04:14:52 +0100 Subject: [PATCH 5/8] `rdserver`: Slightly improve logging --- acrate_rdserver/src/route.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/acrate_rdserver/src/route.rs b/acrate_rdserver/src/route.rs index 25bfbaa..9821182 100644 --- a/acrate_rdserver/src/route.rs +++ b/acrate_rdserver/src/route.rs @@ -14,13 +14,14 @@ use acrate_rd::xrd::{ResourceDescriptorLinkXRD, ResourceDescriptorPropertyXRD, R use crate::config; pub async fn healthcheck_handler() -> Result { - log::info!("Handling an healthcheck request!"); + log::debug!("Handling an healthcheck request!"); log::trace!("Making sure the database is up..."); let _conn = AsyncPgConnection::establish(config::ACRATE_WEBFINGER_DATABASE_URL()) .await .map_err(|_| StatusCode::BAD_GATEWAY)?; + log::trace!("Healthcheck successful! Everything's fine!"); Ok(StatusCode::NO_CONTENT) } @@ -38,7 +39,7 @@ pub async fn webfinger_handler( headers: HeaderMap, Extension(mj): Extension>>, ) -> Result, StatusCode> { - log::info!("Handling a WebFinger request!"); + log::debug!("Handling a WebFinger request!"); let resource = resource.unwrap_or_else(|| "".to_string()); log::debug!("Resource is: {resource:#?}"); -- 2.45.2 From ef9671562dabb1cfaef0db2be1a71c35a486ca93 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 12 Dec 2024 04:23:55 +0100 Subject: [PATCH 6/8] `docker`: Expose database port to the host system --- acrate_docker/compose.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/acrate_docker/compose.yml b/acrate_docker/compose.yml index 9da9246..03e4a32 100644 --- a/acrate_docker/compose.yml +++ b/acrate_docker/compose.yml @@ -51,6 +51,12 @@ services: - type: "volume" source: "postgres_data" target: "/var/lib/postgresql/data" + ports: + # FIXME: for development purposes only + - protocol: "tcp" + host_ip: "127.0.0.1" + target: 5432 + published: 5432 expose: - 5432 healthcheck: -- 2.45.2 From aaae52df8e3972acd9d6df6838d5e59614818887 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 12 Dec 2024 04:32:54 +0100 Subject: [PATCH 7/8] IDEA: Add docker data source --- .idea/dataSources.xml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index 6cfc387..318244b 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -1,12 +1,19 @@ - + postgresql true org.postgresql.Driver jdbc:postgresql:///acrate $ProjectFileDir$ + + postgresql + true + org.postgresql.Driver + jdbc:postgresql://localhost:5432/postgres + $ProjectFileDir$ + \ No newline at end of file -- 2.45.2 From 6cd7e2d425c842a720cdbd9d83828a41ba85495d Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 12 Dec 2024 04:33:11 +0100 Subject: [PATCH 8/8] IDEA: Add `cargo check` run configuration --- .idea/runConfigurations/Check.xml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .idea/runConfigurations/Check.xml diff --git a/.idea/runConfigurations/Check.xml b/.idea/runConfigurations/Check.xml new file mode 100644 index 0000000..048d405 --- /dev/null +++ b/.idea/runConfigurations/Check.xml @@ -0,0 +1,20 @@ + + + + \ No newline at end of file -- 2.45.2