2018-12-20 09:32:49 +00:00
|
|
|
extern crate proxmox_backup;
|
2018-11-16 12:14:11 +00:00
|
|
|
|
2019-01-29 16:21:58 +00:00
|
|
|
//use proxmox_backup::tools;
|
2019-02-17 08:59:20 +00:00
|
|
|
use proxmox_backup::api_schema::router::*;
|
|
|
|
use proxmox_backup::api_schema::config::*;
|
2018-12-20 09:32:49 +00:00
|
|
|
use proxmox_backup::server::rest::*;
|
2019-03-18 12:44:37 +00:00
|
|
|
use proxmox_backup::tools::daemon::Reloader;
|
2019-01-29 15:55:49 +00:00
|
|
|
use proxmox_backup::auth_helpers::*;
|
2019-02-16 11:19:13 +00:00
|
|
|
use proxmox_backup::config;
|
2018-10-31 09:42:14 +00:00
|
|
|
|
2019-02-11 14:11:42 +00:00
|
|
|
use failure::*;
|
2018-11-15 09:14:08 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
2018-11-15 07:18:48 +00:00
|
|
|
use futures::future::Future;
|
2019-03-18 11:27:00 +00:00
|
|
|
use tokio::prelude::*;
|
2018-11-09 07:22:54 +00:00
|
|
|
|
2018-11-15 07:18:48 +00:00
|
|
|
use hyper;
|
2018-10-31 09:42:14 +00:00
|
|
|
|
2019-03-18 11:27:00 +00:00
|
|
|
static mut QUIT_MAIN: bool = false;
|
|
|
|
|
2018-10-30 09:04:30 +00:00
|
|
|
fn main() {
|
|
|
|
|
2019-02-11 14:11:42 +00:00
|
|
|
if let Err(err) = run() {
|
|
|
|
eprintln!("Error: {}", err);
|
|
|
|
std::process::exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run() -> Result<(), Error> {
|
2019-03-18 11:27:00 +00:00
|
|
|
// This manages data for reloads:
|
2019-03-18 12:44:37 +00:00
|
|
|
let mut reloader = Reloader::new();
|
2019-02-11 14:11:42 +00:00
|
|
|
|
2019-01-26 09:56:11 +00:00
|
|
|
if let Err(err) = syslog::init(
|
|
|
|
syslog::Facility::LOG_DAEMON,
|
|
|
|
log::LevelFilter::Info,
|
|
|
|
Some("proxmox-backup-api")) {
|
2019-02-11 14:11:42 +00:00
|
|
|
bail!("unable to inititialize syslog - {}", err);
|
2019-02-16 11:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config::create_configdir()?;
|
2019-01-26 09:56:11 +00:00
|
|
|
|
2019-01-29 12:15:33 +00:00
|
|
|
if let Err(err) = generate_auth_key() {
|
2019-02-11 14:11:42 +00:00
|
|
|
bail!("unable to generate auth key - {}", err);
|
2019-01-29 11:59:07 +00:00
|
|
|
}
|
2019-01-29 16:21:58 +00:00
|
|
|
let _ = private_auth_key(); // load with lazy_static
|
2019-01-29 11:59:07 +00:00
|
|
|
|
2019-01-29 12:15:33 +00:00
|
|
|
if let Err(err) = generate_csrf_key() {
|
2019-02-11 14:11:42 +00:00
|
|
|
bail!("unable to generate csrf key - {}", err);
|
2019-01-29 12:15:33 +00:00
|
|
|
}
|
2019-01-29 16:21:58 +00:00
|
|
|
let _ = csrf_secret(); // load with lazy_static
|
2019-01-29 12:15:33 +00:00
|
|
|
|
2018-11-14 10:57:01 +00:00
|
|
|
lazy_static!{
|
2019-01-22 11:10:38 +00:00
|
|
|
static ref ROUTER: Router = proxmox_backup::api2::router();
|
2018-11-14 10:57:01 +00:00
|
|
|
}
|
2018-11-13 13:47:03 +00:00
|
|
|
|
2019-01-28 12:17:03 +00:00
|
|
|
let config = ApiConfig::new(
|
2019-02-17 10:59:18 +00:00
|
|
|
env!("PROXMOX_JSDIR"), &ROUTER, RpcEnvironmentType::PRIVILEGED);
|
2018-11-13 13:47:03 +00:00
|
|
|
|
2018-11-15 07:18:48 +00:00
|
|
|
let rest_server = RestServer::new(config);
|
2018-10-31 09:42:14 +00:00
|
|
|
|
2019-03-18 11:27:00 +00:00
|
|
|
// http server future:
|
|
|
|
|
2019-03-18 12:44:37 +00:00
|
|
|
let listener: tokio::net::TcpListener = reloader.restore(
|
2019-03-18 11:27:00 +00:00
|
|
|
"PROXMOX_BACKUP_LISTEN_FD",
|
|
|
|
|| {
|
|
|
|
let addr = ([127,0,0,1], 82).into();
|
|
|
|
Ok(tokio::net::TcpListener::bind(&addr)?)
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut http_server = hyper::Server::builder(listener.incoming())
|
2018-11-15 07:18:48 +00:00
|
|
|
.serve(rest_server)
|
2018-10-31 09:42:14 +00:00
|
|
|
.map_err(|e| eprintln!("server error: {}", e));
|
|
|
|
|
2019-03-18 11:27:00 +00:00
|
|
|
// signalfd future:
|
|
|
|
|
|
|
|
let signal_handler =
|
|
|
|
proxmox_backup::tools::daemon::default_signalfd_stream(
|
2019-03-18 12:44:37 +00:00
|
|
|
reloader,
|
2019-03-18 11:27:00 +00:00
|
|
|
|| {
|
|
|
|
unsafe { QUIT_MAIN = true; }
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
)?
|
|
|
|
.map(|si| {
|
|
|
|
// debugging...
|
|
|
|
eprintln!("received signal: {}", si.ssi_signo);
|
|
|
|
})
|
|
|
|
.map_err(|e| {
|
|
|
|
eprintln!("error from signalfd: {}, shutting down...", e);
|
|
|
|
unsafe {
|
|
|
|
QUIT_MAIN = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Combined future for signalfd & http server, we want to quit as soon as either of them ends.
|
|
|
|
// Neither of them is supposed to end unless some weird error happens, so just bail out if is
|
|
|
|
// the case...
|
|
|
|
let mut signal_handler = signal_handler.into_future();
|
|
|
|
let main = futures::future::poll_fn(move || {
|
|
|
|
// Helper for some diagnostic error messages:
|
|
|
|
fn poll_helper<S: Future>(stream: &mut S, name: &'static str) -> bool {
|
|
|
|
match stream.poll() {
|
|
|
|
Ok(Async::Ready(_)) => {
|
|
|
|
eprintln!("{} ended, shutting down", name);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
eprintln!("{} error, shutting down", name);
|
|
|
|
true
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if poll_helper(&mut http_server, "http server") ||
|
|
|
|
poll_helper(&mut signal_handler, "signalfd handler")
|
|
|
|
{
|
|
|
|
return Ok(Async::Ready(()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if unsafe { QUIT_MAIN } {
|
|
|
|
eprintln!("shutdown requested");
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
hyper::rt::run(main);
|
2019-02-11 14:11:42 +00:00
|
|
|
|
|
|
|
Ok(())
|
2018-10-30 09:04:30 +00:00
|
|
|
}
|