2 Commits
4 changed files with 84 additions and 1 deletions

No files matched your search

+23
View File
@@ -10,11 +10,13 @@ param(
[string]$User = "root",
[string]$RemotePath = "/root/.bin/wakey",
[string]$AgentRemotePath = "/root/.bin/wakey-agent",
[string]$RemoteInitPath = "/etc/init.d/wakey",
[string]$Target = "armv7-unknown-linux-musleabihf",
[string]$BinName = "wakey",
[string]$AgentBinName = "wakey-agent",
[string]$HostKey,
[switch]$ForcePassword,
[switch]$SkipInitScript,
[switch]$Restart = $true,
[switch]$Quiet = $true
)
@@ -62,7 +64,10 @@ try {
$agentRemoteTmp = "$AgentRemotePath.tmp"
$destTmp = "$User@${HostName}:$remoteTmp"
$agentDestTmp = "$User@${HostName}:$agentRemoteTmp"
$initTmp = '/var/tmp/wakey.init.tmp'
$initDestTmp = "$User@${HostName}:$initTmp"
$localDeploy = Join-Path $repoRoot 'scripts/remote_deploy_wakey.sh'
$localInit = Join-Path $repoRoot 'scripts/init/openwrt/wakey'
$deployTmp = '/var/tmp/remote_deploy_wakey.sh'
$deployPreferred = '/root/.bin/remote_deploy_wakey.sh'
@@ -70,6 +75,11 @@ try {
Invoke-Scp -Local $localBin -Dest $destTmp -Pass $Pass -HostKey $HostKey -Port $Port -Quiet:$Quiet -ForcePassword:$ForcePassword
Invoke-Scp -Local $localAgentBin -Dest $agentDestTmp -Pass $Pass -HostKey $HostKey -Port $Port -Quiet:$Quiet -ForcePassword:$ForcePassword
# Push OpenWrt init script unless explicitly skipped
if (-not $SkipInitScript -and (Test-Path $localInit)) {
Invoke-Scp -Local $localInit -Dest $initDestTmp -Pass $Pass -HostKey $HostKey -Port $Port -Quiet:$Quiet -ForcePassword:$ForcePassword
}
# Push deploy helper if exists
if (Test-Path $localDeploy) {
Invoke-Scp -Local $localDeploy -Dest "$User@${HostName}:$deployTmp" -Pass $Pass -HostKey $HostKey -Port $Port -Quiet:$Quiet -ForcePassword:$ForcePassword
@@ -80,6 +90,19 @@ try {
$script = @"
$(Get-DeployScript $deployPreferred $deployTmp $remoteTmp $RemotePath 0)
$(Get-DeployScript $deployPreferred $deployTmp $agentRemoteTmp $AgentRemotePath $restartFlag)
$(if (-not $SkipInitScript) {
@"
if [ -f $initTmp ]; then
sed -i "s/\r$//" $initTmp 2>/dev/null || true
if command -v install >/dev/null 2>&1; then
install -m 0755 $initTmp $RemoteInitPath
else
cp -f $initTmp $RemoteInitPath
chmod 0755 $RemoteInitPath
fi
fi
"@
})
"@
$remoteCmd = "sh -lc '$($script -replace "`r",'')'"
if ($Quiet) { $remoteCmd += " >/dev/null 2>&1" }
+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)?;