From 2a1d80d82094c0490e453a8d126203e5bb0fa62e Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 3 Jul 2020 21:03:43 -0400 Subject: [PATCH] build docker image --- .gitignore | 1 + Cargo.lock | 3 ++- Cargo.toml | 2 +- default.nix | 24 ++++++++++++++++++++++++ docker.nix | 22 ++++++++++++++++++++++ nix/sources.json | 12 ++++++++++++ src/main.rs | 11 ++++++++++- 7 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 default.nix create mode 100644 docker.nix diff --git a/.gitignore b/.gitignore index fedaa2b..4be8a58 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target +/result* .env diff --git a/Cargo.lock b/Cargo.lock index 4858ee7..0bb2a70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -336,7 +336,8 @@ checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" [[package]] name = "furbooru" version = "0.3.0" -source = "git+https://github.com/Xe/furbooru#8fac90975d480116851575b3d706bc48dc26a46f" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ef1f8a2e91bead257f057491c9581bfee0e9d7eef219ffafd88408fd4a3c818" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index 0014f47..37d67a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ edition = "2018" anyhow = "1" async-trait = "0" envy = "0.4" +furbooru = "0.3" kankyo = "*" log = "0.4" pretty_env_logger = "0.4" @@ -20,4 +21,3 @@ tokio = { version = "0.2", features = ["macros"] } # git deps discord_webhook = { git = "https://github.com/Xe/withinbot", branch = "main"} -furbooru = { git = "https://github.com/Xe/furbooru" } diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..0aadfad --- /dev/null +++ b/default.nix @@ -0,0 +1,24 @@ +{ sources ? import ./nix/sources.nix, pkgs ? import sources.nixpkgs { } +, naersk ? pkgs.callPackage sources.naersk { } }: + +let + campaigns = pkgs.callPackage ./campaigns { }; + tron = naersk.buildPackage { + src = builtins.filterSource + (path: type: type != "directory" || builtins.baseNameOf path != "target") + ./.; + buildInputs = [ pkgs.openssl pkgs.pkg-config pkgs.sqlite ]; + doCheck = false; + }; + +in pkgs.stdenv.mkDerivation { + inherit (tron) name version; + + phases = "installPhase"; + + installPhase = '' + mkdir -p $out/bin + ln -s ${tron}/bin/tron $out/bin/tron + ln -s ${./regexes.dhall} $out/regexes.dhall + ''; +} diff --git a/docker.nix b/docker.nix new file mode 100644 index 0000000..d13ef28 --- /dev/null +++ b/docker.nix @@ -0,0 +1,22 @@ +{ system ? builtins.currentSystem }: + +let + sources = import ./nix/sources.nix; + pkgs = import sources.nixpkgs { }; + callPackage = pkgs.lib.callPackageWith pkgs; + tron = callPackage ./default.nix { }; + + dockerImage = pkg: + pkgs.dockerTools.buildLayeredImage { + name = "xena/tron"; + tag = pkg.version; + + contents = [ pkgs.cacert pkg ]; + + config = { + Cmd = [ "/bin/tron" ]; + WorkingDir = "/"; + }; + }; + +in dockerImage tron diff --git a/nix/sources.json b/nix/sources.json index a92283a..c59a5f9 100644 --- a/nix/sources.json +++ b/nix/sources.json @@ -11,6 +11,18 @@ "url": "https://github.com/justinwoo/easy-dhall-nix/archive/288ee825c326f352a5db194a024bd3e1f2f735b2.tar.gz", "url_template": "https://github.com///archive/.tar.gz" }, + "naersk": { + "branch": "master", + "description": "Build rust crates in Nix. No configuration, no code generation, no IFD. Sandbox friendly.", + "homepage": "", + "owner": "nmattia", + "repo": "naersk", + "rev": "a82fd7dc31a58c462b6dfa9d9d886fa2cc75dfd4", + "sha256": "00bjwir52y6jbf0b22qy9qxramw35k5fc7kp9hymr1zgpmw9kbwg", + "type": "tarball", + "url": "https://github.com/nmattia/naersk/archive/a82fd7dc31a58c462b6dfa9d9d886fa2cc75dfd4.tar.gz", + "url_template": "https://github.com///archive/.tar.gz" + }, "niv": { "branch": "master", "description": "Easy dependency management for Nix projects", diff --git a/src/main.rs b/src/main.rs index e491f75..ac0a4df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,7 @@ impl FirehoseAdaptor for Rules { for rule in &self.0 { if rule.regex.is_match(&img.description.to_lowercase()) { + log::debug!("{:?} matches {}", img.description, rule.raw); found = true; buf.push_str(&format!("\n- match on rule `{}` ({})", rule.raw, rule.why)); } @@ -58,6 +59,7 @@ impl FirehoseAdaptor for Rules { Body::new(format!("matches found on <{}>:{}", img.view_url, buf)), ) .await?; + log::info!("the description of {} has naughty words", img.view_url); } Ok(()) @@ -69,6 +71,7 @@ impl FirehoseAdaptor for Rules { for rule in &self.0 { if rule.regex.is_match(&cmt.body.to_lowercase()) { + log::debug!("{:?} matches {}", cmt.body, rule.raw); found = true; buf.push_str(&format!("\n- match on rule `{}` ({})", rule.raw, rule.why)); } @@ -83,6 +86,11 @@ impl FirehoseAdaptor for Rules { )), ) .await?; + log::info!( + "comment https://furbooru.org/{}#comment_{} has naughty words", + cmt.image_id, + cmt.id + ); } Ok(()) @@ -98,7 +106,7 @@ async fn main() -> Result<()> { let mut compiled_rules: Vec = Vec::new(); for rule in rexes { - println!("{} -> {}", rule.regex, rule.why); + log::debug!("{} -> {}", rule.regex, rule.why); compiled_rules.push(CompiledRule { raw: rule.regex.clone(), regex: Regex::new(rule.regex.as_str())?, @@ -110,6 +118,7 @@ async fn main() -> Result<()> { user_agent(cfg.bot_owner_furbooru_account.clone()), cfg.furbooru_api_key.clone(), )?; + log::info!("listening on the firehose"); cli.firehose(Rules(compiled_rules, cfg)).await?; Ok(())