feat dingtalk and restart nginx
This commit is contained in:
@@ -55,6 +55,8 @@ pub struct CertConfigItem {
|
||||
pub struct CertConfig {
|
||||
pub port: Option<u16>,
|
||||
pub cert_items: Vec<CertConfigItem>,
|
||||
pub trigger_after_update: Option<Vec<String>>,
|
||||
pub notify_token: Option<String>,
|
||||
}
|
||||
|
||||
impl CertConfig {
|
||||
@@ -98,7 +100,12 @@ impl CertConfig {
|
||||
}
|
||||
}
|
||||
|
||||
Self { port: self.port, cert_items: filtered_cert_items }
|
||||
Self {
|
||||
port: self.port,
|
||||
cert_items: filtered_cert_items,
|
||||
trigger_after_update: self.trigger_after_update,
|
||||
notify_token: self.notify_token,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load(config_fn: &str) -> XResult<Self> {
|
||||
|
||||
69
src/main.rs
69
src/main.rs
@@ -7,6 +7,7 @@ mod config;
|
||||
mod x509;
|
||||
mod network;
|
||||
mod statics;
|
||||
mod dingtalk;
|
||||
// mod simple_thread_pool;
|
||||
|
||||
use std::env;
|
||||
@@ -20,7 +21,7 @@ use std::str::FromStr;
|
||||
use std::sync::RwLock;
|
||||
use std::collections::BTreeMap;
|
||||
use tide::Request;
|
||||
use std::process::exit;
|
||||
use std::process::{Command, exit};
|
||||
use std::time::{Duration, SystemTime};
|
||||
use async_std::task;
|
||||
use async_std::channel;
|
||||
@@ -29,6 +30,8 @@ use config::AcmeMode;
|
||||
use crate::config::{CertConfig, CERT_NAME, KEY_NAME};
|
||||
use crate::x509::{X509PublicKeyAlgo, X509EcPublicKeyAlgo};
|
||||
use std::path::PathBuf;
|
||||
use rust_util::util_cmd::run_command_and_wait;
|
||||
use crate::dingtalk::send_dingtalk_message;
|
||||
use crate::network::{get_local_public_ip, get_resolver, resolve_first_ipv4};
|
||||
use crate::statics::{AcmeStatics, AcmeStatus};
|
||||
|
||||
@@ -75,7 +78,7 @@ async fn main() -> tide::Result<()> {
|
||||
.arg(Arg::with_name("config").short("c").long("config").takes_value(true).help("Cert config"))
|
||||
.arg(Arg::with_name("check").long("check").help("Check cert config"))
|
||||
.arg(Arg::with_name("hide-logo").long("hide-logo").help("Hide logo"))
|
||||
.arg(Arg::with_name("skip-verify-ip").long("skip-verify-ip").help("Skip verify public ip"))
|
||||
.arg(Arg::with_name("skip-verify-ip").short("k").long("skip-verify-ip").help("Skip verify public ip"))
|
||||
.get_matches();
|
||||
|
||||
if matches.is_present("verbose") {
|
||||
@@ -285,7 +288,69 @@ async fn main() -> tide::Result<()> {
|
||||
}
|
||||
}
|
||||
acme_statics.end();
|
||||
|
||||
let mut success_count = 0;
|
||||
for acme_static in &acme_statics.items {
|
||||
if let AcmeStatus::Success = acme_static.status {
|
||||
success_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
success!("Statics: \n{}", acme_statics);
|
||||
|
||||
let mut dingtalk_message = format!("Statics: \n{}", acme_statics);
|
||||
if success_count > 0 {
|
||||
if let Some(trigger_after_update) = &filtered_cert_config.trigger_after_update {
|
||||
if trigger_after_update.len() > 0 {
|
||||
let mut cmd = Command::new(&trigger_after_update[0]);
|
||||
for i in 1..trigger_after_update.len() {
|
||||
cmd.arg(&trigger_after_update[i]);
|
||||
}
|
||||
match run_command_and_wait(&mut cmd) {
|
||||
Ok(_) => {
|
||||
success!("Restart nginx success");
|
||||
dingtalk_message.push_str("\n\nrestart nginx success");
|
||||
}
|
||||
Err(err) => {
|
||||
failure!("Restart nginx failed: {:?}", err);
|
||||
dingtalk_message.push_str(&format!("\n\nrestart nginx failed: {:?}", err));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
warning!("No trigger after update is configed but is empty");
|
||||
}
|
||||
} else {
|
||||
warning!("No trigger after update configed");
|
||||
}
|
||||
}
|
||||
|
||||
let mut success_domains = vec![];
|
||||
let mut failed_domains = vec![];
|
||||
for acme_static in &acme_statics.items {
|
||||
if let AcmeStatus::Success = acme_static.status {
|
||||
success_domains.push(format!("* {}", acme_static.domains.join(", ")));
|
||||
}
|
||||
if let AcmeStatus::Fail(_) = acme_static.status {
|
||||
failed_domains.push(format!("* {}", &acme_static.domains.join(", ")));
|
||||
}
|
||||
}
|
||||
|
||||
if !success_domains.is_empty() {
|
||||
dingtalk_message.push_str("\nsuccess domains:\n");
|
||||
dingtalk_message.push_str(&success_domains.join("\n"));
|
||||
}
|
||||
if !failed_domains.is_empty() {
|
||||
dingtalk_message.push_str("\nfailed domains:\n");
|
||||
dingtalk_message.push_str(&failed_domains.join("\n"));
|
||||
}
|
||||
|
||||
if !acme_statics.items.is_empty() && filtered_cert_config.notify_token.is_some() {
|
||||
if let Err(err) = send_dingtalk_message(&filtered_cert_config, &dingtalk_message) {
|
||||
failure!("Send notification message failed: {:?}", err);
|
||||
}
|
||||
} else {
|
||||
information!("No notification message need to send, or not configed notification token");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user