v0.38.0
This commit is contained in:
1 parent
df0e39b899
commit
21aef61dd5
35 files changed
+1360
-100
No files matched your search
@@ -0,0 +1,169 @@
|
||||
const HEADLESS_PID_FILE: &str = "rustproxy.pid";
|
||||
|
||||
struct HeadlessPidFile(PathBuf);
|
||||
|
||||
impl HeadlessPidFile {
|
||||
fn create(data_dir: &Path) -> Result<Self> {
|
||||
let path = data_dir.join(HEADLESS_PID_FILE);
|
||||
let mut options = std::fs::OpenOptions::new();
|
||||
options.write(true).create(true).truncate(true);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
options.mode(0o600).custom_flags(libc::O_NOFOLLOW);
|
||||
}
|
||||
let mut file = options
|
||||
.open(&path)
|
||||
.with_context(|| format!("cannot write {}", path.display()))?;
|
||||
writeln!(file, "{}", std::process::id())?;
|
||||
Ok(Self(path))
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for HeadlessPidFile {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_file(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
async fn headless_reload_loop(
|
||||
state: Arc<App>,
|
||||
shutdown: tokio_util::sync::CancellationToken,
|
||||
) -> Result<()> {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let mut hangup = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::hangup())?;
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = shutdown.cancelled() => return Ok(()),
|
||||
_ = hangup.recv() => {
|
||||
let revision = state.config.load().envelope.revision;
|
||||
match state.reload_from_file(revision).await {
|
||||
Ok(envelope) => {
|
||||
rust_proxy::runtime::record_headless_reload(None);
|
||||
tracing::info!(
|
||||
revision = envelope.revision,
|
||||
"headless configuration reloaded; listener changes need a restart"
|
||||
)
|
||||
}
|
||||
Err(error) => {
|
||||
rust_proxy::runtime::record_headless_reload(Some(error.1.clone()));
|
||||
tracing::error!(
|
||||
error = %error.1,
|
||||
"headless configuration reload rejected; the running configuration is unchanged"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
let _ = state;
|
||||
shutdown.cancelled().await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn signal_headless_reload(data_dir: &Path) -> Result<()> {
|
||||
let path = data_dir.join(HEADLESS_PID_FILE);
|
||||
let text = std::fs::read_to_string(&path).with_context(|| {
|
||||
format!(
|
||||
"{} not found; is rust-proxy serve --headless running with --data-dir {}?",
|
||||
path.display(),
|
||||
data_dir.display()
|
||||
)
|
||||
})?;
|
||||
let pid: i32 = text
|
||||
.trim()
|
||||
.parse()
|
||||
.with_context(|| format!("invalid process id in {}", path.display()))?;
|
||||
ensure!(pid > 1, "invalid process id in {}", path.display());
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
let command = std::fs::read(format!("/proc/{pid}/cmdline")).unwrap_or_default();
|
||||
ensure!(
|
||||
String::from_utf8_lossy(&command).contains("rust-proxy"),
|
||||
"process {pid} from {} is not a running RustProxy; remove the stale file",
|
||||
path.display()
|
||||
);
|
||||
}
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let result = unsafe { libc::kill(pid, libc::SIGHUP) };
|
||||
ensure!(
|
||||
result == 0,
|
||||
"cannot signal RustProxy process {pid}: {}",
|
||||
io::Error::last_os_error()
|
||||
);
|
||||
println!(
|
||||
"Reload requested (SIGHUP to process {pid}); the result is written to the service log."
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
let _ = pid;
|
||||
bail!("headless reload by signal is only available on Unix")
|
||||
}
|
||||
}
|
||||
|
||||
async fn validate_headless_file(
|
||||
data_dir: PathBuf,
|
||||
admin_addr: SocketAddr,
|
||||
file: PathBuf,
|
||||
) -> Result<()> {
|
||||
let proxy_addr: SocketAddr = std::env::var("RUST_PROXY_PROXY_LISTEN")
|
||||
.unwrap_or_else(|_| "127.0.0.1:8080".into())
|
||||
.parse()?;
|
||||
let tls_addr: Option<SocketAddr> = std::env::var("RUST_PROXY_TLS_LISTEN")
|
||||
.ok()
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(|value| value.parse())
|
||||
.transpose()?;
|
||||
let listeners = rust_proxy::settings::Listeners::from_startup(proxy_addr, tls_addr);
|
||||
let file = if file.is_absolute() {
|
||||
file
|
||||
} else {
|
||||
std::env::current_dir()?.join(file)
|
||||
};
|
||||
let (config, _) = tokio::task::spawn_blocking(move || {
|
||||
rust_proxy::runtime::external_config::read_headless(&file, &listeners)
|
||||
})
|
||||
.await??;
|
||||
rust_proxy::runtime::ensure_headless_supported(&config)?;
|
||||
let cert_path = data_dir.join("certs");
|
||||
let scratch = if cert_path.is_dir() {
|
||||
None
|
||||
} else {
|
||||
let scratch = std::env::temp_dir().join(format!(
|
||||
"rustproxy-validate-{}",
|
||||
rust_proxy::auth::random_secret()
|
||||
));
|
||||
let directory = scratch.clone();
|
||||
tokio::task::spawn_blocking(move || rust_proxy::tls::initialize_default(&directory))
|
||||
.await??;
|
||||
Some(scratch)
|
||||
};
|
||||
let policy = ValidationPolicy {
|
||||
proxy_addr,
|
||||
admin_addr,
|
||||
tls_addr,
|
||||
cert_dir: Some(scratch.clone().unwrap_or(cert_path)),
|
||||
data_dir: Some(data_dir),
|
||||
};
|
||||
let compiled = tokio::task::spawn_blocking(move || config::compile(config, 0, &policy)).await?;
|
||||
if let Some(scratch) = scratch {
|
||||
let _ = std::fs::remove_dir_all(scratch);
|
||||
}
|
||||
let snapshot = compiled?;
|
||||
let config = &snapshot.envelope.config;
|
||||
println!(
|
||||
"Headless configuration valid: {} listener(s), {} vhost(s), {} backend(s). Nothing was changed.",
|
||||
config.listeners.endpoints.len(),
|
||||
config.vhosts.len(),
|
||||
config.vhosts.iter().map(|vhost| vhost.backends.len()).sum::<usize>()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user