2020-10-31 20:27:06 +00:00
|
|
|
use anyhow::Error;
|
2021-09-23 09:12:27 +00:00
|
|
|
use serde_json::json;
|
2020-10-31 20:27:06 +00:00
|
|
|
|
2022-02-21 14:44:06 +00:00
|
|
|
use proxmox_router::{cli::*, ApiHandler, RpcEnvironment};
|
2021-11-23 16:57:00 +00:00
|
|
|
use proxmox_sys::fs::CreateOptions;
|
2020-10-31 20:27:06 +00:00
|
|
|
|
|
|
|
use proxmox_backup::api2;
|
|
|
|
use proxmox_backup::tools::subscription;
|
|
|
|
|
|
|
|
async fn wait_for_local_worker(upid_str: &str) -> Result<(), Error> {
|
2021-09-10 10:25:32 +00:00
|
|
|
let upid: pbs_api_types::UPID = upid_str.parse()?;
|
2022-02-22 09:25:40 +00:00
|
|
|
let poll_delay = core::time::Duration::from_millis(100);
|
2020-10-31 20:27:06 +00:00
|
|
|
|
|
|
|
loop {
|
2021-09-23 08:09:19 +00:00
|
|
|
if !proxmox_rest_server::worker_is_active_local(&upid) {
|
2020-10-31 20:27:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-22 09:25:40 +00:00
|
|
|
tokio::time::sleep(poll_delay).await;
|
2020-10-31 20:27:06 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Daily update
|
2022-02-21 14:44:06 +00:00
|
|
|
async fn do_update(rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
|
2020-10-31 20:27:06 +00:00
|
|
|
let param = json!({});
|
|
|
|
|
|
|
|
let method = &api2::node::subscription::API_METHOD_CHECK_SUBSCRIPTION;
|
2022-02-22 09:26:59 +00:00
|
|
|
match method.handler {
|
|
|
|
ApiHandler::Sync(handler) => {
|
|
|
|
if let Err(err) = (handler)(param, method, rpcenv) {
|
2022-02-22 09:58:44 +00:00
|
|
|
log::error!("Error checking subscription - {}", err);
|
2022-02-22 09:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-31 20:27:06 +00:00
|
|
|
_ => unreachable!(),
|
2022-02-22 09:26:59 +00:00
|
|
|
}
|
2020-10-31 20:27:06 +00:00
|
|
|
|
|
|
|
let notify = match subscription::read_subscription() {
|
|
|
|
Ok(Some(subscription)) => subscription.status == subscription::SubscriptionStatus::ACTIVE,
|
|
|
|
Ok(None) => false,
|
|
|
|
Err(err) => {
|
2022-02-22 09:58:44 +00:00
|
|
|
log::error!("Error reading subscription - {}", err);
|
2020-10-31 20:27:06 +00:00
|
|
|
false
|
2022-02-21 14:44:06 +00:00
|
|
|
}
|
2020-10-31 20:27:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let param = json!({
|
|
|
|
"notify": notify,
|
|
|
|
});
|
|
|
|
let method = &api2::node::apt::API_METHOD_APT_UPDATE_DATABASE;
|
2022-02-22 09:26:59 +00:00
|
|
|
match method.handler {
|
|
|
|
ApiHandler::Sync(handler) => match (handler)(param, method, rpcenv) {
|
|
|
|
Err(err) => {
|
2022-02-22 09:58:44 +00:00
|
|
|
log::error!("Error triggering apt database update - {}", err);
|
2022-02-22 09:26:59 +00:00
|
|
|
}
|
|
|
|
Ok(upid) => wait_for_local_worker(upid.as_str().unwrap()).await?,
|
|
|
|
},
|
2020-10-31 20:27:06 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2021-05-03 09:39:59 +00:00
|
|
|
match check_acme_certificates(rpcenv).await {
|
|
|
|
Ok(()) => (),
|
|
|
|
Err(err) => {
|
2022-02-22 09:58:44 +00:00
|
|
|
log::error!("error checking certificates: {}", err);
|
2021-05-03 09:39:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-31 20:27:06 +00:00
|
|
|
|
|
|
|
// TODO: cleanup tasks like in PVE?
|
|
|
|
|
2021-09-23 09:12:27 +00:00
|
|
|
Ok(())
|
2020-10-31 20:27:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:39:59 +00:00
|
|
|
async fn check_acme_certificates(rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
|
|
|
|
let (config, _) = proxmox_backup::config::node::config()?;
|
|
|
|
|
|
|
|
// do we even have any acme domains configures?
|
|
|
|
if config.acme_domains().next().is_none() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if !api2::node::certificates::cert_expires_soon()? {
|
2022-02-22 09:58:44 +00:00
|
|
|
log::info!("Certificate does not expire within the next 30 days, not renewing.");
|
2021-05-03 09:39:59 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let info = &api2::node::certificates::API_METHOD_RENEW_ACME_CERT;
|
|
|
|
let result = match info.handler {
|
|
|
|
ApiHandler::Sync(handler) => (handler)(json!({}), info, rpcenv)?,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
wait_for_local_worker(result.as_str().unwrap()).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-23 09:12:27 +00:00
|
|
|
async fn run(rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
|
|
|
|
let backup_user = pbs_config::backup_user()?;
|
2022-02-21 14:44:06 +00:00
|
|
|
let file_opts = CreateOptions::new()
|
|
|
|
.owner(backup_user.uid)
|
|
|
|
.group(backup_user.gid);
|
|
|
|
proxmox_rest_server::init_worker_tasks(
|
|
|
|
pbs_buildcfg::PROXMOX_BACKUP_LOG_DIR_M!().into(),
|
|
|
|
file_opts.clone(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut commando_sock = proxmox_rest_server::CommandSocket::new(
|
|
|
|
proxmox_rest_server::our_ctrl_sock(),
|
|
|
|
backup_user.gid,
|
|
|
|
);
|
2021-09-23 09:12:27 +00:00
|
|
|
proxmox_rest_server::register_task_control_commands(&mut commando_sock)?;
|
|
|
|
commando_sock.spawn()?;
|
|
|
|
|
|
|
|
do_update(rpcenv).await
|
|
|
|
}
|
|
|
|
|
2020-10-31 20:27:06 +00:00
|
|
|
fn main() {
|
|
|
|
proxmox_backup::tools::setup_safe_path_env();
|
|
|
|
|
2022-02-22 09:58:44 +00:00
|
|
|
if let Err(err) = syslog::init(
|
|
|
|
syslog::Facility::LOG_DAEMON,
|
|
|
|
log::LevelFilter::Info,
|
|
|
|
Some("proxmox-daily-update"),
|
|
|
|
) {
|
|
|
|
eprintln!("unable to inititialize syslog - {}", err);
|
|
|
|
}
|
|
|
|
|
2020-10-31 20:27:06 +00:00
|
|
|
let mut rpcenv = CliEnvironment::new();
|
|
|
|
rpcenv.set_auth_id(Some(String::from("root@pam")));
|
|
|
|
|
2021-11-19 16:36:06 +00:00
|
|
|
if let Err(err) = proxmox_async::runtime::main(run(&mut rpcenv)) {
|
2022-02-22 09:58:44 +00:00
|
|
|
log::error!("error during update: {}", err);
|
2021-01-25 13:43:00 +00:00
|
|
|
std::process::exit(1);
|
2020-10-31 20:27:06 +00:00
|
|
|
}
|
|
|
|
}
|