git.delta.rocks / jrsonnet / refs/commits / 35b93190d1f0

difftreelog

feat use files for host keys

Yaroslav Bolyukin2021-03-09parent: #f89d040.patch.diff
in: trunk

1 file changed

modifiedsrc/cmds/fetch_keys.rsdiffbeforeafterboth
1use crate::db::{1use crate::host::FleetOpts;
2 keys::{list_hosts, KeyDb},
3 Db, DbData,
4};
5use anyhow::Result;2use anyhow::Result;
6use clap::Clap;3use clap::Clap;
7use log::info;4use log::{info, warn};
85
9#[derive(Clap)]6#[derive(Clap)]
10pub struct FetchKeys {7pub struct FetchKeys {
11 /// Fetch if already exists the following hosts
12 #[clap(short = 'f', long)]8 #[clap(flatten)]
13 force_hosts: Vec<String>,9 fleet_opts: FleetOpts,
10
14 /// If true - remove orphaned keys11 /// If true - remove orphaned keys
15 #[clap(long)]12 #[clap(long)]
16 cleanup: bool,13 cleanup: bool,
17}14}
1815
19impl FetchKeys {16impl FetchKeys {
20 pub fn run(self) -> Result<()> {17 pub fn run(self) -> Result<()> {
21 let db = Db::new(".fleet")?;18 let fleet = self.fleet_opts.build()?;
22 let hosts = list_hosts()?;19 let hosts = fleet.list_hosts()?;
23 let mut keys = KeyDb::open(&db)?;
24 for host in hosts.iter() {20 for host in hosts.iter() {
25 let force = self.force_hosts.contains(&host);21 if host.skip() {
22 warn!("Skipped host {}", host.hostname);
23 continue;
24 }
26 keys.ensure_key_loaded(host, force)?;25 host.key()?;
27 }26 }
28 let orphans: Vec<_> = hosts.iter().filter(|h| !keys.has_key(h)).cloned().collect();27 let orphans: Vec<_> = fleet.list_orphaned_keys()?;
29 if !orphans.is_empty() {28 if !orphans.is_empty() {
30 if self.cleanup {29 if self.cleanup {
31 info!("Removed orphan host keys:");30 info!("Removed orphan host keys:");
32 } else {31 } else {
33 info!("Orphan host keys found, run with --cleanup to remove them from db:");32 info!("Orphan host keys found, run with --cleanup to remove them from db:");
34 }33 }
35 for key in orphans {34 for (name, path) in orphans {
36 info!("- {}", key);35 info!("- {}", name);
37 if self.cleanup {36 if self.cleanup {
38 keys.remove_key(&key)37 std::fs::remove_file(path)?;
39 }38 }
40 }39 }
41 }40 }