]> git.proxmox.com Git - proxmox-backup.git/blob - proxmox-backup-banner/src/main.rs
add proxmox-backup-banner binary crate
[proxmox-backup.git] / proxmox-backup-banner / src / main.rs
1 use std::fmt::Write;
2 use std::fs;
3 use std::net::ToSocketAddrs;
4
5 use proxmox::tools;
6
7 fn main() {
8 let nodename = tools::nodename();
9 let addr = format!("{}:8007", nodename);
10
11 let mut banner = format!(
12 "
13 {:-<78}
14
15 Welcome to the Proxmox Backup Server. Please use your web browser to
16 configure this server - connect to:
17
18 ",
19 ""
20 );
21
22 let msg = match addr.to_socket_addrs() {
23 Ok(saddrs) => {
24 let saddrs: Vec<_> = saddrs
25 .filter_map(|s| match !s.ip().is_loopback() {
26 true => Some(format!(" https://{}/", s)),
27 false => None,
28 })
29 .collect();
30
31 if !saddrs.is_empty() {
32 saddrs.join("\n")
33 } else {
34 format!(
35 "hostname '{}' does not resolves to any non-loopback address",
36 nodename
37 )
38 }
39 }
40 Err(e) => format!("could not resolve hostname '{}': {}", nodename, e),
41 };
42 banner += &msg;
43
44 // unwrap will never fail for write!:
45 // https://github.com/rust-lang/rust/blob/1.39.0/src/liballoc/string.rs#L2318-L2331
46 write!(&mut banner, "\n\n{:-<78}\n\n", "").unwrap();
47
48 fs::write("/etc/issue", banner.as_bytes()).expect("Unable to write banner to issue file");
49 }