conductor/env/nginx_bind_le/src/lib.rs

103 lines
2.9 KiB
Rust

/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use async_trait::async_trait;
use tokio::process::Command;
use libconductor::*;
mod nginx;
mod templates;
use nginx::Nginx;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NginxBindLEConductor;
const CONDUCTOR_NAME: &str = "NGINX_BIND_LE_CONDUCTOR";
#[async_trait]
impl Conductor for NginxBindLEConductor {
async fn process(&self, event: EventType) {
match event {
EventType::NewSite {
hostname,
path,
branch: _branch,
} => {
Nginx::new_site(&hostname, &path, None).await.unwrap();
}
EventType::Config { hostname, data } => {
unimplemented!();
// Nginx::new_site(&hostname, &path, Some(data)).await.unwrap();
}
EventType::DeleteSite { hostname } => {
Nginx::rm_site(&hostname).await.unwrap();
}
};
}
fn name(&self) -> &'static str {
CONDUCTOR_NAME
}
async fn health(&self) -> bool {
nginx::Nginx::env_exists() && nginx::Nginx::status().await
}
}
#[cfg(test)]
mod tests {
use std::process::Stdio;
use super::*;
#[tokio::test]
async fn all_good() {
const HOSTNAME: &str = "lab.batsense.net";
let c = NginxBindLEConductor {};
assert_eq!(c.name(), CONDUCTOR_NAME);
assert!(c.health().await);
if Nginx::site_exists(HOSTNAME) {
c.process(EventType::DeleteSite {
hostname: HOSTNAME.into(),
})
.await;
}
c.process(EventType::NewSite {
hostname: HOSTNAME.into(),
branch: "librepages".into(),
path: "/var/www/website/".into(),
})
.await;
let out = tokio::process::Command::new("sudo")
.arg("nginx")
.arg("-T")
.stdout(Stdio::piped())
.output()
.await
.unwrap();
let expected = format!("server_name {HOSTNAME}");
let out = String::from_utf8(out.stdout).unwrap();
assert!(out.contains(&expected));
c.process(EventType::DeleteSite {
hostname: HOSTNAME.into(),
})
.await;
}
}