init config?

This commit is contained in:
lda
2026-04-11 16:23:31 +07:00 Unverified
parent b4823eeb1a
commit fec10a61d4
3 changed files with 61 additions and 1 deletions
+25
View File
@@ -24,6 +24,8 @@ pub enum Command {
Serve(ServeArgs),
/// Enroll this router with a control plane and write agent config.
Enroll(EnrollArgs),
/// Create a local config scaffold for manual bootstrap.
InitConfig(InitConfigArgs),
/// Reload a running agent daemon by sending SIGHUP.
Reload(ReloadArgs),
}
@@ -52,6 +54,29 @@ pub struct EnrollArgs {
pub config: PathBuf,
}
#[derive(Args)]
pub struct InitConfigArgs {
/// Path to the agent config file to write.
#[arg(long, default_value = config::DEFAULT_CONFIG_PATH)]
pub config: PathBuf,
/// Base HTTPS URL of the control plane.
#[arg(long)]
pub server_url: Option<String>,
/// Persistent agent id obtained from enroll flow.
#[arg(long)]
pub agent_id: Option<String>,
/// Persistent agent token obtained from enroll flow.
#[arg(long)]
pub agent_token: Option<String>,
/// Replace an existing config file.
#[arg(long)]
pub force: bool,
}
#[derive(Args)]
pub struct ReloadArgs {
/// Path to pid file for reload signaling.
+28 -1
View File
@@ -9,7 +9,7 @@ mod tracing;
use anyhow::Result;
use clap::Parser;
use cli::{Cli, Command};
use cli::{Cli, Command, InitConfigArgs};
#[tokio::main]
async fn main() -> Result<()> {
@@ -23,8 +23,35 @@ async fn main() -> Result<()> {
println!("agent_id={}", config.agent_id);
println!("config={}", args.config.display());
}
Command::InitConfig(args) => init_config(args)?,
Command::Reload(args) => serve::reload_daemon(&args.pid_file)?,
}
Ok(())
}
fn init_config(args: InitConfigArgs) -> Result<()> {
if args.config.exists() && !args.force {
anyhow::bail!(
"config {} already exists; re-run with --force to overwrite",
args.config.display()
);
}
let cfg = config::AgentConfig {
server_url: args
.server_url
.unwrap_or_else(|| "https://control-plane.example.com".to_string()),
agent_id: args.agent_id.unwrap_or_else(|| "REPLACE_ME_AGENT_ID".to_string()),
agent_token: args
.agent_token
.unwrap_or_else(|| "REPLACE_ME_AGENT_TOKEN".to_string()),
reconnect_base_ms: 1_000,
reconnect_max_ms: 30_000,
};
config::save_config(&args.config, &cfg)?;
println!("config={}", args.config.display());
println!("next=wakey-agent serve --config {}", args.config.display());
Ok(())
}
+8
View File
@@ -7,6 +7,14 @@ use crate::cli::ServeArgs;
use crate::{config, session};
pub async fn serve(args: ServeArgs) -> Result<()> {
if !args.config.exists() {
anyhow::bail!(
"agent config {} not found. Run `wakey-agent enroll --server-url <url> --enroll-token <token>` or `wakey-agent init-config --config {}` first",
args.config.display(),
args.config.display()
);
}
write_pid_file(&args.pid_file)?;
let mut cfg = config::load_config(&args.config)?;