diff --git a/ofborg/src/bin/pastebin-web.rs b/ofborg/src/bin/pastebin-web.rs new file mode 100644 index 0000000..8116856 --- /dev/null +++ b/ofborg/src/bin/pastebin-web.rs @@ -0,0 +1,72 @@ +use std::env; +use std::error::Error; + +use async_std::task; +use ofborg::config; +use ofborg::easyamqp; +use ofborg::easyamqp::ChannelExt; +use ofborg::easyamqp::ConsumerExt; +use ofborg::easylapin; +use ofborg::tasks; +use tracing::info; + +fn main() -> Result<(), Box> { + ofborg::setup_log(); + + let arg = env::args().nth(1).expect("usage: pastebin-web "); + let cfg = config::load(arg.as_ref()); + + let conn = easylapin::from_config(&cfg.rabbitmq)?; + let mut chan = task::block_on(conn.create_channel())?; + + let queue_name = "pastebin-log".to_owned(); + + chan.declare_exchange(easyamqp::ExchangeConfig { + exchange: "pastebin-log".to_owned(), + exchange_type: easyamqp::ExchangeType::Topic, + passive: false, + durable: true, + auto_delete: false, + no_wait: false, + internal: false, + })?; + + chan.declare_queue(easyamqp::QueueConfig { + queue: queue_name.clone(), + passive: false, + durable: true, + exclusive: false, + auto_delete: false, + no_wait: false, + })?; + + chan.bind_queue(easyamqp::BindQueueConfig { + queue: queue_name.clone(), + exchange: "pastebin-log".to_owned(), + routing_key: None, + no_wait: false, + })?; + + let handle = easylapin::WorkerChannel(chan).consume( + tasks::pastebin_collector::PastebinCollector::new( + cfg.pastebin.clone().path + ), + easyamqp::ConsumeConfig { + queue: queue_name.clone(), + consumer_tag: format!("{}-pastebin-web", cfg.whoami()), + no_local: false, + no_ack: false, + no_wait: false, + exclusive: false, + }, + )?; + + // Boot an HTTP server to serve pastebin logs as well. + + info!("Fetching jobs from {}", &queue_name); + task::block_on(handle); + + drop(conn); // Close connection. + info!("Closed the session... EOF"); + Ok(()) +} diff --git a/ofborg/src/config.rs b/ofborg/src/config.rs index 3898993..0556865 100644 --- a/ofborg/src/config.rs +++ b/ofborg/src/config.rs @@ -21,6 +21,7 @@ pub struct Config { pub rabbitmq: RabbitMqConfig, pub github: Option, pub github_app: Option, + pub pastebin: PastebinConfig, pub log_storage: Option, } @@ -29,6 +30,13 @@ pub struct FeedbackConfig { pub full_logs: bool, } +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct PastebinConfig { + pub path: PathBuf, + // max_disk_size? + // auto_expiry? +} + #[derive(Serialize, Deserialize, Debug, Clone)] pub struct RabbitMqConfig { pub ssl: bool, diff --git a/ofborg/src/lib.rs b/ofborg/src/lib.rs index 1c392e6..73a66eb 100644 --- a/ofborg/src/lib.rs +++ b/ofborg/src/lib.rs @@ -41,6 +41,7 @@ pub mod systems; pub mod tagger; pub mod tasks; pub mod test_scratch; +pub mod utils; pub mod vcs; pub mod worker; pub mod writetoline; diff --git a/ofborg/src/tasks/mod.rs b/ofborg/src/tasks/mod.rs index 5aab0fa..c290c36 100644 --- a/ofborg/src/tasks/mod.rs +++ b/ofborg/src/tasks/mod.rs @@ -4,5 +4,6 @@ pub mod evaluate; pub mod evaluationfilter; pub mod githubcommentfilter; pub mod githubcommentposter; +pub mod pastebin_collector; pub mod log_message_collector; pub mod statscollector; diff --git a/ofborg/src/tasks/pastebin_collector.rs b/ofborg/src/tasks/pastebin_collector.rs new file mode 100644 index 0000000..f14f7b1 --- /dev/null +++ b/ofborg/src/tasks/pastebin_collector.rs @@ -0,0 +1,46 @@ +use std::path::PathBuf; + +use crate::{utils::pastebin::Pastebin, worker}; + +enum CompressionMethod { + //None, + Zstd, +} + +#[allow(dead_code)] +pub struct PastebinCollector { + pastebin_root: PathBuf, + compression_method: CompressionMethod, +} + +impl PastebinCollector { + pub fn new(pastebin_root: PathBuf) -> Self { + Self { + pastebin_root, + compression_method: CompressionMethod::Zstd, + } + } +} + +impl worker::SimpleWorker for PastebinCollector { + type J = Pastebin; + + fn msg_to_job( + &mut self, + _routing_key: &str, + _: &Option, + _body: &[u8], + ) -> Result { + Ok(Pastebin { + contents: "".to_owned(), + }) + } + + fn consumer(&mut self, _job: &Self::J) -> worker::Actions { + // Persist the pastebin on disk compressed in the pastebin_root. + vec![worker::Action::Ack] + } +} + +#[cfg(test)] +mod tests {} diff --git a/ofborg/src/utils/mod.rs b/ofborg/src/utils/mod.rs new file mode 100644 index 0000000..cc0cdf8 --- /dev/null +++ b/ofborg/src/utils/mod.rs @@ -0,0 +1 @@ +pub mod pastebin; diff --git a/ofborg/src/utils/pastebin.rs b/ofborg/src/utils/pastebin.rs new file mode 100644 index 0000000..791eb82 --- /dev/null +++ b/ofborg/src/utils/pastebin.rs @@ -0,0 +1,4 @@ +// A micro-pastebin service. +pub struct Pastebin { + pub contents: String +}