forked from navos/sheepd
🎉 init
This commit is contained in:
commit
812c4adb15
16 changed files with 4631 additions and 0 deletions
23
src/sheepd_core/args.rs
Normal file
23
src/sheepd_core/args.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
use argh::FromArgs;
|
||||
|
||||
#[derive(FromArgs)]
|
||||
/// Sheep Daemon
|
||||
pub struct SheepdArgs {
|
||||
#[argh(subcommand)]
|
||||
pub command: Option<SheepdCommand>,
|
||||
}
|
||||
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
#[argh(subcommand)]
|
||||
pub enum SheepdCommand {
|
||||
Join(JoinCommand),
|
||||
}
|
||||
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
/// Join a herd
|
||||
#[argh(subcommand, name = "join")]
|
||||
pub struct JoinCommand {
|
||||
#[argh(positional)]
|
||||
/// home server domain
|
||||
pub home: String,
|
||||
}
|
37
src/sheepd_core/cmd.rs
Normal file
37
src/sheepd_core/cmd.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
use crate::{api, sheepd_core::config::AgentConfig};
|
||||
|
||||
use super::args::JoinCommand;
|
||||
|
||||
pub fn join(conf: JoinCommand) {
|
||||
// TODO : check for root
|
||||
// TODO : check if joined somewhere already
|
||||
|
||||
log::info!("Joining to {}", conf.home);
|
||||
|
||||
let url = format!("http://{}/join", conf.home);
|
||||
println!("{url}");
|
||||
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(),
|
||||
})
|
||||
.unwrap();
|
||||
let res: serde_json::Value = res.body_mut().read_json().unwrap();
|
||||
|
||||
let token = res
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.get("ok")
|
||||
.unwrap()
|
||||
.as_str()
|
||||
.unwrap();
|
||||
|
||||
log::info!("Joined {} successfully", conf.home);
|
||||
|
||||
std::fs::write(
|
||||
"/etc/sheepd.toml",
|
||||
toml::to_string(&AgentConfig::new(&conf.home, token)).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
16
src/sheepd_core/config.rs
Normal file
16
src/sheepd_core/config.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
use owl::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct AgentConfig {
|
||||
pub home: String,
|
||||
pub token: String,
|
||||
}
|
||||
|
||||
impl AgentConfig {
|
||||
pub fn new(home_server: &str, token: &str) -> Self {
|
||||
Self {
|
||||
home: home_server.to_string(),
|
||||
token: token.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
3
src/sheepd_core/mod.rs
Normal file
3
src/sheepd_core/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub mod args;
|
||||
pub mod cmd;
|
||||
pub mod config;
|
Loading…
Add table
Add a link
Reference in a new issue