]> git.proxmox.com Git - proxmox-backup.git/blame - src/bin/proxmox-backup-api.rs
use UPID and systemd helpers from proxmox 0.13.4
[proxmox-backup.git] / src / bin / proxmox-backup-api.rs
CommitLineData
f7d4e4b5 1use anyhow::{bail, Error};
e76ac3a4 2use futures::*;
7fa9a37c
DM
3use http::request::Parts;
4use http::Response;
5use hyper::{Body, StatusCode};
6use hyper::header;
e76ac3a4 7
9ea4bce4 8use proxmox::try_block;
a2479cfa 9use proxmox::api::RpcEnvironmentType;
fd6d2438 10use proxmox::tools::fs::CreateOptions;
e76ac3a4 11
4805edc4 12use pbs_tools::auth::private_auth_key;
6fbf0acc 13use proxmox_rest_server::{ApiConfig, RestServer};
4805edc4 14
26858dba
SR
15use proxmox_backup::server::{
16 self,
17 auth::default_api_auth,
26858dba 18};
8bca935f
DM
19use proxmox_rest_server::daemon;
20
6c30068e 21use proxmox_backup::auth_helpers::*;
a8f268af 22use proxmox_backup::config;
886e5ce8 23
d973aa82 24fn main() {
ac7513e3
DM
25 proxmox_backup::tools::setup_safe_path_env();
26
d420962f 27 if let Err(err) = pbs_runtime::main(run()) {
aa5a4060
DM
28 eprintln!("Error: {}", err);
29 std::process::exit(-1);
30 }
31}
32
7fa9a37c
DM
33fn get_index(
34 _auth_id: Option<String>,
35 _language: Option<String>,
36 _api: &ApiConfig,
37 _parts: Parts,
38) -> Response<Body> {
39
40 let index = "<center><h1>Proxmox Backup API Server</h1></center>";
41
42 Response::builder()
43 .status(StatusCode::OK)
44 .header(header::CONTENT_TYPE, "text/html")
45 .body(index.into())
46 .unwrap()
47}
48
e76ac3a4 49async fn run() -> Result<(), Error> {
d96d8273
DM
50 if let Err(err) = syslog::init(
51 syslog::Facility::LOG_DAEMON,
52 log::LevelFilter::Info,
53 Some("proxmox-backup-api")) {
aa5a4060 54 bail!("unable to inititialize syslog - {}", err);
a8f268af
DM
55 }
56
57 config::create_configdir()?;
d96d8273 58
22be470d
DM
59 config::update_self_signed_cert(false)?;
60
6c76aa43 61 proxmox_backup::server::create_run_dir()?;
78bf2923 62
eaeda365 63 proxmox_backup::rrd::create_rrdb_dir()?;
1298618a 64 proxmox_backup::server::jobstate::create_jobstate_dir()?;
cafd51bf 65 proxmox_backup::tape::create_tape_status_dir()?;
cd44fb8d
DM
66 proxmox_backup::tape::create_drive_state_dir()?;
67 proxmox_backup::tape::create_changer_state_dir()?;
a0cd0f9c 68 proxmox_backup::tape::create_drive_lock_dir()?;
eaeda365 69
39a90ca6 70 if let Err(err) = generate_auth_key() {
aa5a4060 71 bail!("unable to generate auth key - {}", err);
8d04280b 72 }
d01e2420 73 let _ = private_auth_key(); // load with lazy_static
8d04280b 74
39a90ca6 75 if let Err(err) = generate_csrf_key() {
aa5a4060 76 bail!("unable to generate csrf key - {}", err);
39a90ca6 77 }
d01e2420 78 let _ = csrf_secret(); // load with lazy_static
39a90ca6 79
fd6d2438 80 let mut config = ApiConfig::new(
af06decd 81 pbs_buildcfg::JS_DIR,
26858dba
SR
82 &proxmox_backup::api2::ROUTER,
83 RpcEnvironmentType::PRIVILEGED,
84 default_api_auth(),
7fa9a37c 85 get_index,
26858dba 86 )?;
eaeda365 87
fd6d2438
DM
88 let backup_user = pbs_config::backup_user()?;
89 let mut commando_sock = proxmox_rest_server::CommandoSocket::new(crate::server::our_ctrl_sock(), backup_user.gid);
a68768cf 90
fd6d2438
DM
91 let dir_opts = CreateOptions::new().owner(backup_user.uid).group(backup_user.gid);
92 let file_opts = CreateOptions::new().owner(backup_user.uid).group(backup_user.gid);
93
94 config.enable_file_log(
95 pbs_buildcfg::API_ACCESS_LOG_FN,
36b7085e
DM
96 Some(dir_opts.clone()),
97 Some(file_opts.clone()),
98 &mut commando_sock,
99 )?;
100
101 config.enable_auth_log(
102 pbs_buildcfg::API_AUTH_LOG_FN,
0a33fba4
DM
103 Some(dir_opts.clone()),
104 Some(file_opts.clone()),
fd6d2438
DM
105 &mut commando_sock,
106 )?;
8e7e2223 107
36b7085e 108
9bc17e8d 109 let rest_server = RestServer::new(config);
0a33fba4 110 proxmox_backup::server::init_worker_tasks(pbs_buildcfg::PROXMOX_BACKUP_LOG_DIR_M!().into(), file_opts.clone())?;
886e5ce8 111
5e7bc50a 112 // http server future:
a690ecac
WB
113 let server = daemon::create_daemon(
114 ([127,0,0,1], 82).into(),
083ff3fd 115 move |listener, ready| {
db0cb9ce 116 let incoming = proxmox_backup::tools::async_io::StaticIncoming::from(listener);
083ff3fd 117 Ok(ready
db0cb9ce 118 .and_then(|_| hyper::Server::builder(incoming)
083ff3fd 119 .serve(rest_server)
fd6d2438 120 .with_graceful_shutdown(proxmox_rest_server::shutdown_future())
083ff3fd
WB
121 .map_err(Error::from)
122 )
123 .map(|e| {
124 if let Err(e) = e {
125 eprintln!("server error: {}", e);
126 }
127 })
a690ecac 128 )
5e7bc50a 129 },
d7c6ad60 130 "proxmox-backup.service",
083ff3fd 131 );
5e7bc50a 132
af06decd 133 server::write_pid(pbs_buildcfg::PROXMOX_BACKUP_API_PID_FN)?;
d98c9a7a
WB
134 daemon::systemd_notify(daemon::SystemdNotify::Ready)?;
135
e76ac3a4 136 let init_result: Result<(), Error> = try_block!({
a68768cf
TL
137 server::register_task_control_commands(&mut commando_sock)?;
138 commando_sock.spawn()?;
fd6d2438 139 proxmox_rest_server::server_state_init()?;
e76ac3a4
WB
140 Ok(())
141 });
e3f41f21 142
e76ac3a4
WB
143 if let Err(err) = init_result {
144 bail!("unable to start daemon - {}", err);
145 }
d607b886 146
083ff3fd 147 server.await?;
a546a8a0 148 log::info!("server shutting down, waiting for active workers to complete");
fd6d2438 149 proxmox_rest_server::last_worker_future().await?;
e3f41f21 150
e76ac3a4 151 log::info!("done - exit server");
eaeda365 152
aa5a4060 153 Ok(())
d8d978eb 154}