2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, Error};
|
2019-08-28 14:11:01 +00:00
|
|
|
use futures::*;
|
|
|
|
|
2020-01-21 11:28:01 +00:00
|
|
|
use proxmox::try_block;
|
2019-11-21 13:36:28 +00:00
|
|
|
use proxmox::api::RpcEnvironmentType;
|
2019-08-28 14:11:01 +00:00
|
|
|
|
2019-01-29 16:21:58 +00:00
|
|
|
//use proxmox_backup::tools;
|
2019-11-22 08:23:03 +00:00
|
|
|
//use proxmox_backup::api_schema::config::*;
|
2021-03-31 10:21:51 +00:00
|
|
|
use proxmox_backup::server::{
|
|
|
|
self,
|
|
|
|
auth::default_api_auth,
|
|
|
|
rest::*,
|
|
|
|
};
|
2019-03-18 13:13:44 +00:00
|
|
|
use proxmox_backup::tools::daemon;
|
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
|
|
|
|
2020-01-20 11:52:22 +00:00
|
|
|
fn main() {
|
2020-06-15 08:38:30 +00:00
|
|
|
proxmox_backup::tools::setup_safe_path_env();
|
|
|
|
|
2021-07-06 10:08:44 +00:00
|
|
|
if let Err(err) = pbs_runtime::main(run()) {
|
2019-02-11 14:11:42 +00:00
|
|
|
eprintln!("Error: {}", err);
|
|
|
|
std::process::exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 14:11:01 +00:00
|
|
|
async fn run() -> Result<(), Error> {
|
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
|
|
|
}
|
|
|
|
|
2019-04-09 10:15:06 +00:00
|
|
|
server::create_task_log_dirs()?;
|
2019-04-06 15:53:12 +00:00
|
|
|
|
2019-02-16 11:19:13 +00:00
|
|
|
config::create_configdir()?;
|
2019-01-26 09:56:11 +00:00
|
|
|
|
2019-12-27 12:44:21 +00:00
|
|
|
config::update_self_signed_cert(false)?;
|
|
|
|
|
2021-05-06 08:23:54 +00:00
|
|
|
proxmox_backup::tools::create_run_dir()?;
|
|
|
|
|
2020-05-23 08:43:08 +00:00
|
|
|
proxmox_backup::rrd::create_rrdb_dir()?;
|
2020-10-28 06:33:05 +00:00
|
|
|
proxmox_backup::server::jobstate::create_jobstate_dir()?;
|
2020-12-09 09:16:01 +00:00
|
|
|
proxmox_backup::tape::create_tape_status_dir()?;
|
2021-02-19 16:49:50 +00:00
|
|
|
proxmox_backup::tape::create_drive_state_dir()?;
|
|
|
|
proxmox_backup::tape::create_changer_state_dir()?;
|
2020-05-23 08:43:08 +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
|
|
|
|
2020-10-16 09:06:46 +00:00
|
|
|
let mut config = server::ApiConfig::new(
|
2021-07-06 09:56:35 +00:00
|
|
|
pbs_buildcfg::JS_DIR,
|
2021-03-31 10:21:51 +00:00
|
|
|
&proxmox_backup::api2::ROUTER,
|
|
|
|
RpcEnvironmentType::PRIVILEGED,
|
|
|
|
default_api_auth(),
|
|
|
|
)?;
|
2020-05-23 08:43:08 +00:00
|
|
|
|
2020-11-02 18:13:36 +00:00
|
|
|
let mut commando_sock = server::CommandoSocket::new(server::our_ctrl_sock());
|
|
|
|
|
2021-07-06 09:56:35 +00:00
|
|
|
config.enable_file_log(pbs_buildcfg::API_ACCESS_LOG_FN, &mut commando_sock)?;
|
2020-10-16 09:06:46 +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 13:13:44 +00:00
|
|
|
let server = daemon::create_daemon(
|
|
|
|
([127,0,0,1], 82).into(),
|
2019-09-02 13:16:21 +00:00
|
|
|
move |listener, ready| {
|
2019-12-12 14:27:07 +00:00
|
|
|
let incoming = proxmox_backup::tools::async_io::StaticIncoming::from(listener);
|
2019-09-02 13:16:21 +00:00
|
|
|
Ok(ready
|
2019-12-12 14:27:07 +00:00
|
|
|
.and_then(|_| hyper::Server::builder(incoming)
|
2019-09-02 13:16:21 +00:00
|
|
|
.serve(rest_server)
|
|
|
|
.with_graceful_shutdown(server::shutdown_future())
|
|
|
|
.map_err(Error::from)
|
|
|
|
)
|
|
|
|
.map(|e| {
|
|
|
|
if let Err(e) = e {
|
|
|
|
eprintln!("server error: {}", e);
|
|
|
|
}
|
|
|
|
})
|
2019-03-18 13:13:44 +00:00
|
|
|
)
|
2019-03-18 11:27:00 +00:00
|
|
|
},
|
2020-11-11 08:27:51 +00:00
|
|
|
"proxmox-backup.service",
|
2019-09-02 13:16:21 +00:00
|
|
|
);
|
2019-03-18 11:27:00 +00:00
|
|
|
|
2021-07-06 09:56:35 +00:00
|
|
|
server::write_pid(pbs_buildcfg::PROXMOX_BACKUP_API_PID_FN)?;
|
2019-04-25 08:38:26 +00:00
|
|
|
daemon::systemd_notify(daemon::SystemdNotify::Ready)?;
|
|
|
|
|
2019-08-28 14:11:01 +00:00
|
|
|
let init_result: Result<(), Error> = try_block!({
|
2020-11-02 18:13:36 +00:00
|
|
|
server::register_task_control_commands(&mut commando_sock)?;
|
|
|
|
commando_sock.spawn()?;
|
2019-08-28 14:11:01 +00:00
|
|
|
server::server_state_init()?;
|
|
|
|
Ok(())
|
|
|
|
});
|
2019-04-08 12:00:23 +00:00
|
|
|
|
2019-08-28 14:11:01 +00:00
|
|
|
if let Err(err) = init_result {
|
|
|
|
bail!("unable to start daemon - {}", err);
|
|
|
|
}
|
2019-04-09 10:15:06 +00:00
|
|
|
|
2019-09-02 13:16:21 +00:00
|
|
|
server.await?;
|
2019-12-27 11:50:27 +00:00
|
|
|
log::info!("server shutting down, waiting for active workers to complete");
|
|
|
|
proxmox_backup::server::last_worker_future().await?;
|
2019-04-08 12:00:23 +00:00
|
|
|
|
2019-08-28 14:11:01 +00:00
|
|
|
log::info!("done - exit server");
|
2020-05-23 08:43:08 +00:00
|
|
|
|
2019-02-11 14:11:42 +00:00
|
|
|
Ok(())
|
2018-10-30 09:04:30 +00:00
|
|
|
}
|