]> git.proxmox.com Git - proxmox-backup.git/blame - src/server/mod.rs
move src/server/formatter.rs to proxmox-rest-server crate
[proxmox-backup.git] / src / server / mod.rs
CommitLineData
882594c5
DM
1//! Proxmox Server/Service framework
2//!
3//! This code provides basic primitives to build our REST API
4//! services. We want async IO, so this is built on top of
5//! tokio/hyper.
6
04b053d8 7use anyhow::{format_err, Error};
a68768cf
TL
8use lazy_static::lazy_static;
9use nix::unistd::Pid;
fca1cef2 10use serde_json::Value;
a68768cf
TL
11
12use proxmox::sys::linux::procfs::PidStat;
6c76aa43 13use proxmox::tools::fs::{create_path, CreateOptions};
a68768cf 14
af06decd 15use pbs_buildcfg;
a68768cf
TL
16
17lazy_static! {
18 static ref PID: i32 = unsafe { libc::getpid() };
19 static ref PSTART: u64 = PidStat::read_from_pid(Pid::from_raw(*PID)).unwrap().starttime;
20}
21
22pub fn pid() -> i32 {
23 *PID
24}
25
26pub fn pstart() -> u64 {
27 *PSTART
28}
29
04b053d8
TL
30pub fn write_pid(pid_fn: &str) -> Result<(), Error> {
31 let pid_str = format!("{}\n", *PID);
6c76aa43 32 proxmox::tools::fs::replace_file(pid_fn, pid_str.as_bytes(), CreateOptions::new())
04b053d8
TL
33}
34
35pub fn read_pid(pid_fn: &str) -> Result<i32, Error> {
36 let pid = proxmox::tools::fs::file_get_contents(pid_fn)?;
37 let pid = std::str::from_utf8(&pid)?.trim();
38 pid.parse().map_err(|err| format_err!("could not parse pid - {}", err))
39}
40
a68768cf 41pub fn ctrl_sock_from_pid(pid: i32) -> String {
af06decd 42 format!("\0{}/control-{}.sock", pbs_buildcfg::PROXMOX_BACKUP_RUN_DIR, pid)
a68768cf
TL
43}
44
45pub fn our_ctrl_sock() -> String {
46 ctrl_sock_from_pid(*PID)
47}
48
634132fe
DM
49mod upid;
50pub use upid::*;
51
882594c5
DM
52mod worker_task;
53pub use worker_task::*;
7a630df7 54
42a87f7b
DM
55mod h2service;
56pub use h2service::*;
57
882594c5
DM
58#[macro_use]
59pub mod rest;
60
1298618a
DM
61pub mod jobstate;
62
63mod verify_job;
64pub use verify_job::*;
b9e7bcc2 65
b8d90798
HL
66mod prune_job;
67pub use prune_job::*;
68
3b707fbb
DM
69mod gc_job;
70pub use gc_job::*;
71
b9e7bcc2
DM
72mod email_notifications;
73pub use email_notifications::*;
b0ef9631
HL
74
75mod report;
76pub use report::*;
027ef213
WB
77
78pub mod ticket;
ba39ab20
SR
79
80pub mod auth;
fca1cef2 81
aa2838c2
WB
82pub mod pull;
83
fca1cef2 84pub(crate) async fn reload_proxy_certificate() -> Result<(), Error> {
af06decd 85 let proxy_pid = crate::server::read_pid(pbs_buildcfg::PROXMOX_BACKUP_PROXY_PID_FN)?;
fca1cef2 86 let sock = crate::server::ctrl_sock_from_pid(proxy_pid);
fd6d2438 87 let _: Value = proxmox_rest_server::send_raw_command(sock, "{\"command\":\"reload-certificate\"}\n")
fca1cef2
WB
88 .await?;
89 Ok(())
90}
062cf75c
DC
91
92pub(crate) async fn notify_datastore_removed() -> Result<(), Error> {
af06decd 93 let proxy_pid = crate::server::read_pid(pbs_buildcfg::PROXMOX_BACKUP_PROXY_PID_FN)?;
062cf75c 94 let sock = crate::server::ctrl_sock_from_pid(proxy_pid);
fd6d2438 95 let _: Value = proxmox_rest_server::send_raw_command(sock, "{\"command\":\"datastore-removed\"}\n")
062cf75c
DC
96 .await?;
97 Ok(())
98}
6c76aa43
WB
99
100/// Create the base run-directory.
101///
102/// This exists to fixate the permissions for the run *base* directory while allowing intermediate
103/// directories after it to have different permissions.
104pub fn create_run_dir() -> Result<(), Error> {
21211748 105 let backup_user = pbs_config::backup_user()?;
6c76aa43
WB
106 let opts = CreateOptions::new()
107 .owner(backup_user.uid)
108 .group(backup_user.gid);
109 let _: bool = create_path(pbs_buildcfg::PROXMOX_BACKUP_RUN_DIR_M!(), None, Some(opts))?;
110 Ok(())
111}