stdout
This commit is contained in:
@@ -64,9 +64,13 @@ pub struct EnrollArgs {
|
|||||||
|
|
||||||
#[derive(Args)]
|
#[derive(Args)]
|
||||||
pub struct InitConfigArgs {
|
pub struct InitConfigArgs {
|
||||||
/// Path to the agent config file to write.
|
/// Path to the agent config file to write. If omitted, config is printed to stdout.
|
||||||
#[arg(long, default_value = config::DEFAULT_CONFIG_PATH)]
|
#[arg(long)]
|
||||||
pub config: PathBuf,
|
pub config: Option<PathBuf>,
|
||||||
|
|
||||||
|
/// Print config to stdout.
|
||||||
|
#[arg(long, conflicts_with = "config")]
|
||||||
|
pub stdout: bool,
|
||||||
|
|
||||||
/// Base HTTPS URL of the control plane.
|
/// Base HTTPS URL of the control plane.
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
|
|||||||
+16
-6
@@ -36,7 +36,6 @@ async fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Command::InitConfig(args) => {
|
Command::InitConfig(args) => {
|
||||||
::tracing::info!(config = %args.config.display(), force = args.force, "wakey-agent command: init-config");
|
|
||||||
init_config(args)?
|
init_config(args)?
|
||||||
}
|
}
|
||||||
Command::Reload(args) => {
|
Command::Reload(args) => {
|
||||||
@@ -49,12 +48,18 @@ async fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn init_config(args: InitConfigArgs) -> Result<()> {
|
fn init_config(args: InitConfigArgs) -> Result<()> {
|
||||||
if args.config.exists() && !args.force {
|
if args.stdout && args.config.is_some() {
|
||||||
|
anyhow::bail!("--stdout cannot be used with --config");
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(config) = &args.config {
|
||||||
|
if config.exists() && !args.force {
|
||||||
anyhow::bail!(
|
anyhow::bail!(
|
||||||
"config {} already exists; re-run with --force to overwrite",
|
"config {} already exists; re-run with --force to overwrite",
|
||||||
args.config.display()
|
config.display()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let cfg = config::AgentConfig {
|
let cfg = config::AgentConfig {
|
||||||
server_url: args
|
server_url: args
|
||||||
@@ -70,8 +75,13 @@ fn init_config(args: InitConfigArgs) -> Result<()> {
|
|||||||
reconnect_max_ms: 30_000,
|
reconnect_max_ms: 30_000,
|
||||||
};
|
};
|
||||||
|
|
||||||
config::save_config(&args.config, &cfg)?;
|
if let Some(path) = &args.config {
|
||||||
println!("config={}", args.config.display());
|
config::save_config(path, &cfg)?;
|
||||||
println!("next=wakey-agent serve --config {}", args.config.display());
|
println!("config={}", path.display());
|
||||||
|
println!("next=wakey-agent serve --config {}", path.display());
|
||||||
|
} else {
|
||||||
|
let rendered = toml::to_string_pretty(&cfg)?;
|
||||||
|
print!("{}", rendered);
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ pub struct Cli {
|
|||||||
pub enum Command {
|
pub enum Command {
|
||||||
/// Run the control-plane daemon.
|
/// Run the control-plane daemon.
|
||||||
Serve(ServeArgs),
|
Serve(ServeArgs),
|
||||||
/// Write a control-plane config scaffold.
|
/// Render a control-plane config scaffold.
|
||||||
InitConfig(InitConfigArgs),
|
InitConfig(InitConfigArgs),
|
||||||
/// Create a new enroll token for provisioning a router.
|
/// Create a new enroll token for provisioning a router.
|
||||||
IssueEnrollToken(IssueEnrollTokenArgs),
|
IssueEnrollToken(IssueEnrollTokenArgs),
|
||||||
@@ -80,8 +80,11 @@ pub struct ServeArgs {
|
|||||||
|
|
||||||
#[derive(Args, Clone)]
|
#[derive(Args, Clone)]
|
||||||
pub struct InitConfigArgs {
|
pub struct InitConfigArgs {
|
||||||
#[arg(long, default_value = DEFAULT_CONFIG_FILE)]
|
#[arg(long = "config-file", alias = "config")]
|
||||||
pub config_file: PathBuf,
|
pub config_file: Option<PathBuf>,
|
||||||
|
|
||||||
|
#[arg(long, conflicts_with = "config_file")]
|
||||||
|
pub stdout: bool,
|
||||||
|
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
pub data_dir: Option<PathBuf>,
|
pub data_dir: Option<PathBuf>,
|
||||||
|
|||||||
@@ -6,13 +6,19 @@ use crate::cli::{InitConfigArgs, ServeArgs};
|
|||||||
use crate::config::resolve::{normalize_public_url, resolve_path};
|
use crate::config::resolve::{normalize_public_url, resolve_path};
|
||||||
use crate::config::types::{WritableConfig, WritableTelemetry};
|
use crate::config::types::{WritableConfig, WritableTelemetry};
|
||||||
|
|
||||||
pub fn write_init_config(args: &InitConfigArgs) -> Result<()> {
|
pub fn write_init_config(args: &InitConfigArgs) -> Result<Option<PathBuf>> {
|
||||||
if args.config_file.exists() && !args.force {
|
if args.stdout && args.config_file.is_some() {
|
||||||
|
anyhow::bail!("--stdout cannot be used with --config-file");
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(config_file) = &args.config_file {
|
||||||
|
if config_file.exists() && !args.force {
|
||||||
anyhow::bail!(
|
anyhow::bail!(
|
||||||
"config {} already exists; re-run with --force to overwrite",
|
"config {} already exists; re-run with --force to overwrite",
|
||||||
args.config_file.display()
|
config_file.display()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let bind = args.bind.unwrap_or_else(|| {
|
let bind = args.bind.unwrap_or_else(|| {
|
||||||
"0.0.0.0:8080"
|
"0.0.0.0:8080"
|
||||||
@@ -61,15 +67,21 @@ pub fn write_init_config(args: &InitConfigArgs) -> Result<()> {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(parent) = args.config_file.parent() {
|
let rendered = toml::to_string_pretty(&body).context("failed to render config template")?;
|
||||||
|
|
||||||
|
if let Some(config_file) = &args.config_file {
|
||||||
|
if let Some(parent) = config_file.parent() {
|
||||||
std::fs::create_dir_all(parent)
|
std::fs::create_dir_all(parent)
|
||||||
.with_context(|| format!("failed to create config dir {}", parent.display()))?;
|
.with_context(|| format!("failed to create config dir {}", parent.display()))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let rendered = toml::to_string_pretty(&body).context("failed to render config template")?;
|
std::fs::write(config_file, rendered)
|
||||||
std::fs::write(&args.config_file, rendered)
|
.with_context(|| format!("failed to write config {}", config_file.display()))?;
|
||||||
.with_context(|| format!("failed to write config {}", args.config_file.display()))?;
|
return Ok(Some(config_file.clone()));
|
||||||
Ok(())
|
}
|
||||||
|
|
||||||
|
print!("{}", rendered);
|
||||||
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bootstrap_config_if_missing(args: &ServeArgs) -> Result<bool> {
|
pub fn bootstrap_config_if_missing(args: &ServeArgs) -> Result<bool> {
|
||||||
@@ -78,7 +90,8 @@ pub fn bootstrap_config_if_missing(args: &ServeArgs) -> Result<bool> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let init = InitConfigArgs {
|
let init = InitConfigArgs {
|
||||||
config_file: args.config_file.clone(),
|
config_file: Some(args.config_file.clone()),
|
||||||
|
stdout: false,
|
||||||
data_dir: args.data_dir.clone(),
|
data_dir: args.data_dir.clone(),
|
||||||
bind: args.bind,
|
bind: args.bind,
|
||||||
public_url: args.public_url.clone(),
|
public_url: args.public_url.clone(),
|
||||||
|
|||||||
@@ -30,13 +30,10 @@ async fn main() -> Result<()> {
|
|||||||
runtime::serve(daemon).await
|
runtime::serve(daemon).await
|
||||||
}
|
}
|
||||||
Command::InitConfig(args) => {
|
Command::InitConfig(args) => {
|
||||||
tracing::init(cli.verbose, &config::TelemetryConfig::default())?;
|
if let Some(path) = config::write_init_config(&args)? {
|
||||||
config::write_init_config(&args)?;
|
println!("config={}", path.display());
|
||||||
println!("config={}", args.config_file.display());
|
println!("next=wakey-control-plane serve --config-file {}", path.display());
|
||||||
println!(
|
}
|
||||||
"next=wakey-control-plane serve --config-file {}",
|
|
||||||
args.config_file.display()
|
|
||||||
);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
Command::IssueEnrollToken(args) => {
|
Command::IssueEnrollToken(args) => {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ pub async fn issue_enroll_token(args: IssueEnrollTokenArgs) -> Result<()> {
|
|||||||
let ttl_seconds = settings.ttl.as_secs().max(1);
|
let ttl_seconds = settings.ttl.as_secs().max(1);
|
||||||
let endpoint = format!(
|
let endpoint = format!(
|
||||||
"{}?ttl_seconds={ttl_seconds}",
|
"{}?ttl_seconds={ttl_seconds}",
|
||||||
config::issue_token_endpoint(&base)
|
config::issue_token_endpoint(base)
|
||||||
);
|
);
|
||||||
tracing::info!(endpoint = %endpoint, "requesting live enroll token from running control-plane daemon");
|
tracing::info!(endpoint = %endpoint, "requesting live enroll token from running control-plane daemon");
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|||||||
Reference in New Issue
Block a user