use std::path::Path;
use uuid::Uuid;
use crate::utils::get_web_nginx_config_file;
use crate::{NGINX_WEB_CONFIG_PATH, SSL_CERTIFICATE_KEY_PATH, SSL_CERTIFICATE_PATH, WEB_FOLDER};
use crate::session::RumiSession;
use crate::error::Result;
pub fn install_command(session: &RumiSession, domain: &str, dist_path: &str) -> Result<()> {
session.execute_command_checked("sudo apt update")?;
session.execute_command_checked("sudo apt-get -y install ufw")?;
session.execute_command_checked("sudo apt install -y nginx certbot")?;
session.execute_command_checked("sudo ufw allow 'Nginx HTTP'")?;
let certbot_instruction = format!(
"sudo certbot certonly -y --standalone -d {} -d www.{} --agree-tos --email pondonda@gmail.com",
domain, domain
);
session.execute_command_checked(&certbot_instruction)?;
let certificate_path = format!("{}/{}/fullchain.pem", SSL_CERTIFICATE_PATH, domain);
let certificate_key_path = format!("{}/{}/privkey.pem", SSL_CERTIFICATE_KEY_PATH, domain);
let random_uuid = Uuid::new_v4().to_string();
let web_folder_path = format!("{}/{}_{}", WEB_FOLDER, domain, random_uuid);
session.execute_command_checked(
"sudo chmod 777 /var/www/ && sudo chmod 777 /etc/nginx/sites-available/ && sudo chmod 777 /etc/nginx/sites-enabled/"
)?;
let dist_path = Path::new(dist_path);
session.upload_directory(dist_path, &web_folder_path)?;
session.execute_command_checked("sudo rm /etc/nginx/sites-enabled/default")?;
let nginx_config = get_web_nginx_config_file(
domain,
&certificate_path,
&certificate_key_path,
&web_folder_path,
);
let config_file_path = format!("{}/{}", NGINX_WEB_CONFIG_PATH, domain);
session.create_remote_file(&config_file_path, &nginx_config)?;
let enable_command = format!(
"sudo ln -s {} /etc/nginx/sites-enabled/ && ls -a /etc/nginx/sites-enabled",
config_file_path
);
session.execute_command_checked(&enable_command)?;
session.execute_command_checked(
"sudo ufw allow 80 && sudo ufw allow 443 && sudo systemctl restart nginx"
)?;
Ok(())
}
pub fn update_command(session: &RumiSession, domain: &str, dist_path: &str) -> Result<()> {
let certificate_path = format!("{}/{}/fullchain.pem", SSL_CERTIFICATE_PATH, domain);
let certificate_key_path = format!("{}/{}/privkey.pem", SSL_CERTIFICATE_KEY_PATH, domain);
let random_uuid = Uuid::new_v4().to_string();
let web_folder_path = format!("{}/{}_{}", WEB_FOLDER, domain, random_uuid);
let dist_path = Path::new(dist_path);
session.upload_directory(dist_path, &web_folder_path)?;
let nginx_config = get_web_nginx_config_file(
domain,
&certificate_path,
&certificate_key_path,
&web_folder_path,
);
let config_file_path = format!("{}/{}", NGINX_WEB_CONFIG_PATH, domain);
session.create_remote_file(&config_file_path, &nginx_config)?;
let enable_command = format!(
"sudo ln -s {} /etc/nginx/sites-enabled/ && ls -a /etc/nginx/sites-enabled",
config_file_path
);
session.execute_command_checked(&enable_command)?;
session.execute_command_checked("sudo systemctl reload nginx")?;
Ok(())
}
pub fn rollback_command(session: &RumiSession, domain: &str, version_name: &str) -> Result<()> {
let certificate_path = format!("{}/{}/fullchain.pem", SSL_CERTIFICATE_PATH, domain);
let certificate_key_path = format!("{}/{}/privkey.pem", SSL_CERTIFICATE_KEY_PATH, domain);
let web_folder_path = format!("{}/{}", WEB_FOLDER, version_name);
let nginx_config = get_web_nginx_config_file(
domain,
&certificate_path,
&certificate_key_path,
&web_folder_path,
);
let config_file_path = format!("{}/{}", NGINX_WEB_CONFIG_PATH, domain);
session.create_remote_file(&config_file_path, &nginx_config)?;
let enable_command = format!(
"sudo ln -s {} /etc/nginx/sites-enabled/ && ls -a /etc/nginx/sites-enabled",
config_file_path
);
session.execute_command_checked(&enable_command)?;
session.execute_command_checked("sudo systemctl reload nginx")?;
Ok(())
}