dockerization #1

Merged
bizcochito merged 4 commits from dockerization into develop 2024-02-17 11:15:49 +00:00
6 changed files with 331 additions and 90 deletions

105
Cargo.lock generated
View File

@ -133,17 +133,6 @@ version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1181e1e0d1fce796a03db1ae795d67167da795f9cf4a39c37589e85ef57f26d3" checksum = "1181e1e0d1fce796a03db1ae795d67167da795f9cf4a39c37589e85ef57f26d3"
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi 0.1.19",
"libc",
"winapi",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@ -566,15 +555,6 @@ version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.2.6" version = "0.2.6"
@ -740,6 +720,17 @@ version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146" checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146"
[[package]]
name = "is-terminal"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b"
dependencies = [
"hermit-abi 0.3.2",
"libc",
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "isolang" name = "isolang"
version = "2.2.0" version = "2.2.0"
@ -850,7 +841,7 @@ dependencies = [
[[package]] [[package]]
name = "mastodon-image-uploader-bot" name = "mastodon-image-uploader-bot"
version = "0.3.0" version = "0.3.1"
dependencies = [ dependencies = [
"async-std", "async-std",
"log", "log",
@ -1354,12 +1345,12 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]] [[package]]
name = "stderrlog" name = "stderrlog"
version = "0.5.4" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69a26bbf6de627d389164afa9783739b56746c6c72c4ed16539f4ff54170327b" checksum = "61c910772f992ab17d32d6760e167d2353f4130ed50e796752689556af07dc6b"
dependencies = [ dependencies = [
"atty",
"chrono", "chrono",
"is-terminal",
"log", "log",
"termcolor", "termcolor",
"thread_local", "thread_local",
@ -1977,6 +1968,15 @@ dependencies = [
"windows-targets 0.48.1", "windows-targets 0.48.1",
] ]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.0",
]
[[package]] [[package]]
name = "windows-targets" name = "windows-targets"
version = "0.42.1" version = "0.42.1"
@ -2007,6 +2007,21 @@ dependencies = [
"windows_x86_64_msvc 0.48.0", "windows_x86_64_msvc 0.48.0",
] ]
[[package]]
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm 0.52.0",
"windows_aarch64_msvc 0.52.0",
"windows_i686_gnu 0.52.0",
"windows_i686_msvc 0.52.0",
"windows_x86_64_gnu 0.52.0",
"windows_x86_64_gnullvm 0.52.0",
"windows_x86_64_msvc 0.52.0",
]
[[package]] [[package]]
name = "windows_aarch64_gnullvm" name = "windows_aarch64_gnullvm"
version = "0.42.1" version = "0.42.1"
@ -2019,6 +2034,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.42.1" version = "0.42.1"
@ -2031,6 +2052,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.42.1" version = "0.42.1"
@ -2043,6 +2070,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.42.1" version = "0.42.1"
@ -2055,6 +2088,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.42.1" version = "0.42.1"
@ -2067,6 +2106,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.42.1" version = "0.42.1"
@ -2079,6 +2124,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.42.1" version = "0.42.1"
@ -2091,6 +2142,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"
[[package]] [[package]]
name = "winnow" name = "winnow"
version = "0.5.0" version = "0.5.0"

View File

@ -11,7 +11,7 @@ reqwest = "0.11.14"
serde = "1.0.171" serde = "1.0.171"
toml = "0.8.8" toml = "0.8.8"
log = "0.4.19" log = "0.4.19"
stderrlog = "0.5.4" stderrlog = "0.6.0"
async-std = "1.12.0" async-std = "1.12.0"
[dependencies.mastodon-async] [dependencies.mastodon-async]

41
compose.yaml Normal file
View File

@ -0,0 +1,41 @@
version: "3"
services:
mongodb:
image: mongo:bionic
container_name: mongodb
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: bot
volumes:
- ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
- mongodb_data:/data/db
bot-api:
image: git.fai.st/fedi-image-bot/bot-api:v1.0.0
container_name: bot-api
ports:
- "8080:8080"
depends_on:
- mongodb
environment:
MONGODB_URI: "mongodb://mongodb:27017/bot"
MONGODB_USER: "root"
MONGODB_PASS: "password"
JWTSECRET: "cooljwtsecret"
bot:
image: rust
container_name: bot
working_dir: /app
entrypoint: ["cargo"]
depends_on:
- bot-api
volumes:
- ./:/app:rw
volumes:
mongodb_data:

View File

@ -4,10 +4,13 @@ instance = "https://awoo.fai.st"
bio = "Bot who posts images of sleeping girls every 6 hours." bio = "Bot who posts images of sleeping girls every 6 hours."
[files] [files]
urls = "./urls.csv"
posted = "./posted.csv"
tempfile = "/tmp/botimage.png" tempfile = "/tmp/botimage.png"
[backend]
url = "http://bot-api:8080"
app = "tester"
secret = "test"
[errors] [errors]
maintainers = "@Sugui@awoo.fai.st @MeDueleLaTeta@awoo.fai.st" maintainers = "@Sugui@awoo.fai.st @MeDueleLaTeta@awoo.fai.st"
out_of_images = "me quedé sin chicas" out_of_images = "me quedé sin chicas"

10
init-mongo.js Normal file
View File

@ -0,0 +1,10 @@
db = new Mongo().getDB("bot");
db.createCollection('authorizations');
db.authorizations.insert([
{
app: "tester",
secret: "test",
}
]);

View File

@ -2,12 +2,28 @@ use mastodon_async::entities::visibility::Visibility;
use mastodon_async::helpers::{cli, toml as masto_toml}; use mastodon_async::helpers::{cli, toml as masto_toml};
use mastodon_async::prelude::*; use mastodon_async::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashSet; use std::io::Cursor;
use std::io::{Cursor, Write};
use std::process::exit; use std::process::exit;
use std::time::Duration; use std::time::Duration;
#[derive(Debug, Serialize)] #[derive(Deserialize)]
struct ImagesWrap {
images: Vec<Image>,
}
#[derive(Deserialize, Clone, Eq, PartialEq, Debug)]
struct Image {
url: String,
_id: String,
status: String,
}
#[derive(Serialize)]
struct ImageStatus {
status: String,
}
#[derive(Serialize)]
struct AccountUpdate { struct AccountUpdate {
note: String, note: String,
} }
@ -16,28 +32,34 @@ const CONFIG_FILENAME: &str = "config.toml";
type DynResult<T> = Result<T, Box<dyn std::error::Error>>; type DynResult<T> = Result<T, Box<dyn std::error::Error>>;
#[derive(Deserialize)] #[derive(Deserialize, Serialize, Default)]
struct Config { struct Config {
bot: Bot, bot: Bot,
files: Files, backend: Backend,
errors: Errors, errors: Errors,
files: Files,
} }
#[derive(Deserialize)] #[derive(Deserialize, Serialize, Default)]
struct Files {
tempfile: String,
}
#[derive(Deserialize, Serialize, Default)]
struct Bot { struct Bot {
name: String, name: String,
instance: String, instance: String,
bio: String, bio: String,
} }
#[derive(Deserialize)] #[derive(Deserialize, Serialize, Default)]
struct Files { struct Backend {
posted: String, url: String,
urls: String, app: String,
tempfile: String, secret: String,
} }
#[derive(Deserialize)] #[derive(Deserialize, Serialize, Default)]
struct Errors { struct Errors {
maintainers: String, maintainers: String,
out_of_images: String, out_of_images: String,
@ -56,11 +78,12 @@ async fn main() -> DynResult<()> {
let config: Config = get_config(); let config: Config = get_config();
let mastodon = get_account(&config).await; let mastodon = get_account(&config).await;
match get_next_url(&config) { match get_next_url(&config).await {
Ok(url) => match url { Ok(image) => match image {
Some(url) => { Some(image) => {
let mut retry: u8 = 0; let mut retry: u8 = 0;
while let Err(err) = post_image(&mastodon, &url, &config, Visibility::Unlisted).await { while let Err(err) = post_image(&mastodon, &image.url, &config, Visibility::Unlisted).await
{
log::warn!("Cannot post image, retry: {}, {}", retry, err); log::warn!("Cannot post image, retry: {}, {}", retry, err);
async_std::task::sleep(Duration::new(1, 0)).await; async_std::task::sleep(Duration::new(1, 0)).await;
retry += 1; retry += 1;
@ -70,7 +93,7 @@ async fn main() -> DynResult<()> {
exit(1); exit(1);
} }
} }
set_url_as_posted(&config, &url)?; set_url_as_posted(&config, &image).await?;
let mut retry: u8 = 0; let mut retry: u8 = 0;
while let Err(err) = update_bio(&mastodon, &config).await { while let Err(err) = update_bio(&mastodon, &config).await {
log::warn!("Cannot update bio, retry: {}, {}", retry, err); log::warn!("Cannot update bio, retry: {}, {}", retry, err);
@ -129,11 +152,26 @@ fn get_config() -> Config {
Ok(config) => config, Ok(config) => config,
Err(err) => { Err(err) => {
log::error!("Config file parsing unsuccesful: {}", err); log::error!("Config file parsing unsuccesful: {}", err);
generate_config().unwrap();
log::info!("Please check the working directory to find a new config file");
exit(1); exit(1);
} }
} }
} }
fn generate_config() -> DynResult<()> {
let config = Config::default();
let config = toml::to_string(&config)?;
std::fs::write(CONFIG_FILENAME, config)?;
Ok(())
}
/// Parses the given filename to a config struct
fn parse_config(filename: &str) -> DynResult<Config> {
let toml_file = std::fs::read_to_string(filename)?; //.expect("No config file, consider getting the original one and modifing it");
Ok(toml::from_str(&toml_file)?) //("Malformed config file, check the original one for reference")
}
async fn get_account(config: &Config) -> Mastodon { async fn get_account(config: &Config) -> Mastodon {
if let Ok(data) = masto_toml::from_file("mastodon-data.toml") { if let Ok(data) = masto_toml::from_file("mastodon-data.toml") {
Mastodon::from(data) Mastodon::from(data)
@ -148,33 +186,67 @@ async fn get_account(config: &Config) -> Mastodon {
} }
} }
/// Parses the given filename to a config struct async fn get_next_url(config: &Config) -> DynResult<Option<Image>> {
fn parse_config(filename: &str) -> DynResult<Config> { let images: ImagesWrap = reqwest::get(format!(
let toml_file = std::fs::read_to_string(filename)?; //.expect("No config file, consider getting the original one and modifing it"); "{}/images?status=available&limit=1",
Ok(toml::from_str(&toml_file)?) //("Malformed config file, check the original one for reference") config.backend.url
} ))
.await?
.json()
.await?;
fn get_next_url(config: &Config) -> DynResult<Option<String>> { let images = images.images;
let binding = std::fs::read_to_string(&config.files.posted)?; //.expect("Posted file not found");
let posted = binding.lines().collect::<HashSet<&str>>(); if images.is_empty() {
let binding = std::fs::read_to_string(&config.files.urls)?; //.expect("Urls file not found");
let urls = binding.lines().collect::<HashSet<&str>>();
let urls = urls.difference(&posted).collect::<Vec<_>>();
if urls.is_empty() {
Ok(None) Ok(None)
} else { } else {
Ok(Some(urls[0].to_string().clone())) Ok(Some(images[0].clone()))
} }
} }
fn set_url_as_posted(config: &Config, url: &String) -> DynResult<()> { #[derive(Serialize)]
let mut file = std::fs::OpenOptions::new() struct Auth {
.write(true) app: String,
.append(true) // This is needed to append to file secret: String,
.open(&config.files.posted)?; //.expect("Cannot open posted file"); // Maybe we should retry just in case }
writeln!(file, "{}", url)?; //.expect("Cannot write to posted file"); // maybe we should retry tbh
log::info!("Set url {} as posted", url); #[derive(Deserialize)]
Ok(()) struct Token {
token: String,
}
async fn set_url_as_posted(config: &Config, image: &Image) -> DynResult<()> {
let client = reqwest::Client::new();
let auth = &Auth {
app: config.backend.app.to_string(),
secret: config.backend.secret.to_string(),
};
let token: Token = client
.post(&format!("{}/login", config.backend.url))
.json(auth)
.send()
.await?
.json()
.await?;
let response = client
.put(&format!("{}/images/{}", config.backend.url, image._id))
.bearer_auth(token.token)
.json(&ImageStatus {
status: "consumed".to_string(),
})
.send()
.await?;
if response.status().is_success() {
log::info!("Set url {} as posted", image.url);
Ok(())
} else {
let error = response.text().await?;
log::error!("Set url response: {}", error);
Err(error.into())
}
} }
async fn post_image( async fn post_image(
@ -201,12 +273,12 @@ async fn post_image(
} }
async fn update_bio(account: &Mastodon, config: &Config) -> DynResult<()> { async fn update_bio(account: &Mastodon, config: &Config) -> DynResult<()> {
let binding = std::fs::read_to_string(&config.files.posted)?; //.expect("Posted file not found"); let images: ImagesWrap = reqwest::get(format!("{}/images?status=available", config.backend.url))
let posted = binding.lines().collect::<HashSet<&str>>(); .await?
let binding = std::fs::read_to_string(&config.files.urls)?; //.expect("Url file not found"); .json()
let urls = binding.lines().collect::<HashSet<&str>>(); .await?;
let remaining = urls.difference(&posted).count(); let remaining = images.images.len();
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let account_update = AccountUpdate { let account_update = AccountUpdate {
@ -323,35 +395,93 @@ mod tests {
assert_eq!(response.status(), StatusCode::OK) assert_eq!(response.status(), StatusCode::OK)
} }
#[test] #[tokio::test]
fn set_as_posted_works() { async fn set_as_posted_works() {
let config = get_config(); let config = get_config();
const IMAGE: &str = "https://picsum.photos/id/2";
let expected = insert_image(&config, IMAGE).await.unwrap();
std::fs::write(&config.files.posted, "").unwrap(); set_url_as_posted(&config, &expected).await.unwrap();
let image: ImageWrap = reqwest::get(format!("{}/images/{}", config.backend.url, expected._id))
set_url_as_posted(&config, &TEST_URL.to_string()).unwrap(); .await
let file = std::fs::read_to_string(&config.files.posted).unwrap(); .unwrap()
let url = file.lines().next_back().unwrap(); .json()
assert_eq!(url, TEST_URL) .await
.unwrap();
let image = image.image;
assert_eq!(image.status, "consumed");
} }
#[test] #[tokio::test]
fn get_next_url_works() { async fn get_next_url_works() {
let config = get_config(); let config = get_config();
// Reset file
std::fs::write(&config.files.posted, "").unwrap(); const IMAGE: &str = "https://picsum.photos/id/1";
let expected = insert_image(&config, IMAGE).await.unwrap();
// Get test url // Get test url
let url = get_next_url(&config).unwrap().unwrap(); let image = get_next_url(&config).await.unwrap().unwrap();
assert_eq!(url, TEST_URL); assert_eq!(image.url, IMAGE);
set_url_as_posted(&config, &TEST_URL.to_string()).unwrap(); set_url_as_posted(&config, &expected).await.unwrap();
let file = std::fs::read_to_string(&config.files.posted).unwrap(); let image: ImageWrap = reqwest::get(format!("{}/images/{}", config.backend.url, expected._id))
let url = file.lines().next_back().unwrap(); .await
assert_eq!(url, TEST_URL); .unwrap()
.json()
.await
.unwrap();
let image = image.image;
assert_eq!(image.status, "consumed");
// Test that now it does not get it // Test that now it does not get it
let url = get_next_url(&config).unwrap(); let image = get_next_url(&config).await.unwrap();
assert_eq!(url, None); assert_eq!(image, None);
}
async fn insert_image(config: &Config, url: &str) -> DynResult<Image> {
let client = reqwest::Client::new();
let auth = &Auth {
app: config.backend.app.to_string(),
secret: config.backend.secret.to_string(),
};
let token: Token = client
.post(&format!("{}/login", config.backend.url))
.json(auth)
.send()
.await?
.json()
.await?;
let response = client
.post(&format!("{}/images", config.backend.url))
.bearer_auth(&token.token)
.json(&Insert {
url: url.to_string(),
status: "available".to_string(),
tags: vec![],
})
.send()
.await?;
if response.status().is_success() {
let response: ImageWrap = response.json().await?;
return Ok(response.image);
}
dbg!(response.text().await?);
panic!();
}
#[derive(Serialize)]
struct Insert {
url: String,
status: String,
tags: Vec<String>,
}
#[derive(Deserialize)]
struct ImageWrap {
image: Image,
} }
} }