forked from navos/sheepd
48 lines
1.2 KiB
Rust
48 lines
1.2 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use sage::Identity;
|
|
|
|
use crate::{
|
|
api::{self, JoinResponse},
|
|
sheepd_core::config::AgentConfig,
|
|
};
|
|
|
|
use super::args::JoinCommand;
|
|
use crate::api::domain;
|
|
|
|
/// Join a herd as client
|
|
pub fn join(conf: JoinCommand) {
|
|
// TODO : check for root
|
|
// TODO : check if joined somewhere already
|
|
|
|
log::info!("Joining to {}", conf.home);
|
|
|
|
let _ = std::fs::create_dir_all("/etc/sheepd");
|
|
|
|
let i = if let Some(i) = Identity::try_load(&PathBuf::from("/etc/sheepd")) {
|
|
i
|
|
} else {
|
|
let i = Identity::new();
|
|
i.save(&PathBuf::from("/etc/sheepd"));
|
|
i
|
|
};
|
|
|
|
let url = format!("{}/join", domain(&conf.home));
|
|
let mut res = ureq::post(url)
|
|
.send_json(&api::JoinParams {
|
|
join_token: None,
|
|
machine_id: std::fs::read_to_string("/etc/machine-id").unwrap(),
|
|
hostname: std::fs::read_to_string("/etc/hostname").unwrap(),
|
|
identity: i.public(),
|
|
})
|
|
.unwrap();
|
|
let res: JoinResponse = res.body_mut().read_json().unwrap();
|
|
|
|
log::info!("Joined {} successfully", conf.home);
|
|
|
|
std::fs::write(
|
|
"/etc/sheepd/config.toml",
|
|
toml::to_string(&AgentConfig::new(&conf.home, res)).unwrap(),
|
|
)
|
|
.unwrap();
|
|
}
|