diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..d63ab72 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +* +!/src +!/static +!/.cargo +!/Cargo.toml +!/rust-toolchain.toml +!/build.rs \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 34a38b7..af7fa39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,6 +65,7 @@ built = { version = "0.7.4", features = ["git2"] } git2 = { version = "0.19", features = ["vendored-libgit2"] } [features] -default = ["wordle", "nortverse"] +default = [] wordle = ["dep:kwordle"] nortverse = [] +docker = ["wordle", "nortverse"] diff --git a/Dockerfile b/Dockerfile index 702bce5..4094b12 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,58 @@ -FROM clux/muslrust:nightly AS chef -USER root -RUN cargo install cargo-chef -WORKDIR /app +# rust with musl development utilities & cargo chef preinstalled +FROM ghcr.io/perpetualcacophony/muslrust-chef:nightly AS chef +WORKDIR /build + + FROM chef AS planner -COPY . . + +# cargo chef prepare wants these files +COPY Cargo.toml . +COPY src/main.rs src/ + +# create the cargo chef recipe file RUN cargo +nightly chef prepare --recipe-path recipe.json + + FROM chef AS builder -COPY --link --from=planner /app/recipe.json recipe.json -RUN cargo +nightly chef cook --release --target x86_64-unknown-linux-musl --recipe-path recipe.json + +# copy recipe file to builder +COPY --link --from=planner /build/recipe.json . + +# cargo chef cook to cache dependencies from recipe file +RUN cargo +nightly chef cook \ + --release \ + --target x86_64-unknown-linux-musl \ + --recipe-path recipe.json \ + --features docker + +# copy the rest of the source code to builder COPY . . -RUN touch /app/build.rs -RUN cargo +nightly build --release --target x86_64-unknown-linux-musl +# touch the build script to ensure cargo runs it +RUN touch build.rs + +# build binary +RUN cargo +nightly build \ + --release \ + --target x86_64-unknown-linux-musl \ + --features docker + + + +# using alpine for small final image FROM alpine AS runtime -COPY --link --from=builder /app/target/x86_64-unknown-linux-musl/release/slimebot /usr/local/bin/slimebot + +# add slimebot user & group +RUN addgroup --system slimebot && \ + adduser --system slimebot --ingroup slimebot + EXPOSE 443 + +# copy binary from builder +COPY --link --from=builder --chown=slimebot:slimebot /build/target/x86_64-unknown-linux-musl/release/slimebot /usr/local/bin/slimebot + USER slimebot:slimebot -ENTRYPOINT ["slimebot"] \ No newline at end of file + +ENTRYPOINT slimebot \ No newline at end of file diff --git a/src/framework/config.rs b/src/framework/config.rs index b2527bb..5dd012b 100644 --- a/src/framework/config.rs +++ b/src/framework/config.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use poise::serenity_prelude::{ActivityData, ChannelId, GuildId, RoleId}; use rand::seq::IteratorRandom; use serde::Deserialize; @@ -190,8 +192,14 @@ pub struct DbConfig { } impl DbConfig { - pub fn url(&self) -> &str { - &self.url + pub fn url(&self) -> Cow { + #[cfg(feature = "docker")] + if let Ok(db_url) = std::env::var("SLIMEBOT_DB_URL") { + info!(db_url, "using db url override from environment"); + return db_url.into(); + } + + (&self.url).into() } pub fn username(&self) -> &str { diff --git a/src/framework/data.rs b/src/framework/data.rs index 9315b8f..4bfb368 100644 --- a/src/framework/data.rs +++ b/src/framework/data.rs @@ -33,8 +33,14 @@ impl PoiseData { info!(path, "looking for config file with SLIMEBOT_TOML..."); path } else { - warn!("SLIMEBOT_TOML env unset, using default path /usr/share/slimebot/slimebot.toml"); - "/usr/share/slimebot/slimebot.toml".to_owned() + #[cfg(not(feature = "docker"))] + let path = "./slimebot.toml".to_owned(); + + #[cfg(feature = "docker")] + let path = "/slimebot.toml".to_owned(); + + warn!(path, "SLIMEBOT_TOML env unset, using default path"); + path }; let config: super::config::Config = ::config::Config::builder()