uds subscription wip
This commit is contained in:
parent
2506ee0164
commit
e5d5118022
|
@ -31,11 +31,16 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use ::attic::nix_store::NixStore;
|
use ::attic::nix_store::NixStore;
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
|
use axum::body::Body;
|
||||||
use axum::{extract::Extension, routing::get, Router};
|
use axum::{extract::Extension, routing::get, Router};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use http_body_util::BodyExt;
|
||||||
|
use hyper_util::rt::{TokioExecutor, TokioIo};
|
||||||
|
use futures::StreamExt;
|
||||||
use tempfile::NamedTempFile;
|
use tempfile::NamedTempFile;
|
||||||
use tokio::fs::File;
|
use tokio::fs::File;
|
||||||
use tokio::io::AsyncWriteExt;
|
use tokio::io::AsyncWriteExt;
|
||||||
|
use tokio::net::UnixStream;
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
use tokio::sync::{oneshot, Mutex, RwLock};
|
use tokio::sync::{oneshot, Mutex, RwLock};
|
||||||
use tracing_subscriber::filter::EnvFilter;
|
use tracing_subscriber::filter::EnvFilter;
|
||||||
|
@ -44,6 +49,9 @@ use tracing_subscriber::util::SubscriberInitExt;
|
||||||
|
|
||||||
use gha_cache::Credentials;
|
use gha_cache::Credentials;
|
||||||
|
|
||||||
|
const DETERMINATE_STATE_DIR: &str = "/nix/var/determinate";
|
||||||
|
const DETERMINATE_NIXD_SOCKET_NAME: &str = "determinate-nixd.socket";
|
||||||
|
|
||||||
type State = Arc<StateInner>;
|
type State = Arc<StateInner>;
|
||||||
|
|
||||||
/// GitHub Actions-powered Nix binary cache
|
/// GitHub Actions-powered Nix binary cache
|
||||||
|
@ -292,65 +300,116 @@ async fn main_cli() -> Result<()> {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Write the post-build hook script. Note that the shell script
|
let dnixd_uds_socket_dir: &Path = Path::new(&DETERMINATE_STATE_DIR);
|
||||||
* ignores errors, to avoid the Nix build from failing. */
|
let dnixd_uds_socket_path = dnixd_uds_socket_dir.join(DETERMINATE_NIXD_SOCKET_NAME);
|
||||||
let post_build_hook_script = {
|
|
||||||
let mut file = NamedTempFile::with_prefix("magic-nix-cache-build-hook-")
|
|
||||||
.with_context(|| "Creating a temporary file for the post-build hook")?;
|
|
||||||
file.write_all(
|
|
||||||
format!(
|
|
||||||
// NOTE(cole-h): We want to exit 0 even if the hook failed, otherwise it'll fail the
|
|
||||||
// build itself
|
|
||||||
"#! /bin/sh\nRUST_LOG=trace RUST_BACKTRACE=full {} --server {} || :\n",
|
|
||||||
std::env::current_exe()
|
|
||||||
.with_context(|| "Getting the path of magic-nix-cache")?
|
|
||||||
.display(),
|
|
||||||
args.listen
|
|
||||||
)
|
|
||||||
.as_bytes(),
|
|
||||||
)
|
|
||||||
.with_context(|| "Writing the post-build hook")?;
|
|
||||||
let path = file
|
|
||||||
.keep()
|
|
||||||
.with_context(|| "Keeping the post-build hook")?
|
|
||||||
.1;
|
|
||||||
|
|
||||||
fs::set_permissions(&path, fs::Permissions::from_mode(0o755))
|
if dnixd_uds_socket_path.exists() {
|
||||||
.with_context(|| "Setting permissions on the post-build hook")?;
|
let stream = TokioIo::new(UnixStream::connect(dnixd_uds_socket_path).await?);
|
||||||
|
// let (mut sender, conn): (SendRequest<Body>, _) =
|
||||||
|
// hyper::client::conn::http1::handshake(stream).await?;
|
||||||
|
|
||||||
/* Copy the script to the Nix store so we know for sure that
|
let executor = TokioExecutor::new();
|
||||||
* it's accessible to the Nix daemon, which might have a
|
let (mut sender, conn): (hyper::client::conn::http2::SendRequest<Body>, _) =
|
||||||
* different /tmp from us. */
|
hyper::client::conn::http2::handshake(executor, stream)
|
||||||
let res = Command::new("nix")
|
.await
|
||||||
.args([
|
.unwrap();
|
||||||
"--extra-experimental-features",
|
|
||||||
"nix-command",
|
// NOTE(colemickens): for now we just drop the joinhandle and let it keep running
|
||||||
"store",
|
let _join_handle = tokio::task::spawn(async move {
|
||||||
"add-path",
|
if let Err(err) = conn.await {
|
||||||
&path.display().to_string(),
|
tracing::error!("Connection failed: {:?}", err);
|
||||||
])
|
}
|
||||||
.output()
|
});
|
||||||
.await?;
|
|
||||||
if res.status.success() {
|
let request = http::Request::builder()
|
||||||
tokio::fs::remove_file(path).await?;
|
.method(http::Method::GET)
|
||||||
PathBuf::from(String::from_utf8_lossy(&res.stdout).trim())
|
.uri("http://localhost/built-paths")
|
||||||
} else {
|
.body(axum::body::Body::empty())?;
|
||||||
path
|
|
||||||
|
let response = sender.send_request(request).await?;
|
||||||
|
|
||||||
|
// NOTE(to self): there was a note somewhere to use this _IF_ you wanted all data, including trialers etc
|
||||||
|
// so ... we'll see..
|
||||||
|
let mut data = response.into_data_stream();
|
||||||
|
|
||||||
|
while let Some(foo) = data.next().await {
|
||||||
|
tracing::info!("got {:?}", foo);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
// while let Some(frame_result) = response.frame().await {
|
||||||
|
// let frame = frame_result?;
|
||||||
|
|
||||||
/* Update nix.conf. */
|
// if let Some(segment) = frame.data_ref() {
|
||||||
nix_conf
|
// tokio::io::stdout()
|
||||||
.write_all(
|
// .write_all(segment.iter().as_slice())
|
||||||
format!(
|
// .await?;
|
||||||
"fallback = true\npost-build-hook = {}\n",
|
// }
|
||||||
post_build_hook_script.display()
|
// }
|
||||||
|
|
||||||
|
// TODO: do something with these paths
|
||||||
|
} else {
|
||||||
|
// TODO: split into own function(s)
|
||||||
|
|
||||||
|
/* Write the post-build hook script. Note that the shell script
|
||||||
|
* ignores errors, to avoid the Nix build from failing. */
|
||||||
|
let post_build_hook_script = {
|
||||||
|
let mut file = NamedTempFile::with_prefix("magic-nix-cache-build-hook-")
|
||||||
|
.with_context(|| "Creating a temporary file for the post-build hook")?;
|
||||||
|
file.write_all(
|
||||||
|
format!(
|
||||||
|
// NOTE(cole-h): We want to exit 0 even if the hook failed, otherwise it'll fail the
|
||||||
|
// build itself
|
||||||
|
"#! /bin/sh\nRUST_LOG=trace RUST_BACKTRACE=full {} --server {} || :\n",
|
||||||
|
std::env::current_exe()
|
||||||
|
.with_context(|| "Getting the path of magic-nix-cache")?
|
||||||
|
.display(),
|
||||||
|
args.listen
|
||||||
|
)
|
||||||
|
.as_bytes(),
|
||||||
)
|
)
|
||||||
.as_bytes(),
|
.with_context(|| "Writing the post-build hook")?;
|
||||||
)
|
let path = file
|
||||||
.with_context(|| "Writing to nix.conf")?;
|
.keep()
|
||||||
|
.with_context(|| "Keeping the post-build hook")?
|
||||||
|
.1;
|
||||||
|
|
||||||
drop(nix_conf);
|
fs::set_permissions(&path, fs::Permissions::from_mode(0o755))
|
||||||
|
.with_context(|| "Setting permissions on the post-build hook")?;
|
||||||
|
|
||||||
|
/* Copy the script to the Nix store so we know for sure that
|
||||||
|
* it's accessible to the Nix daemon, which might have a
|
||||||
|
* different /tmp from us. */
|
||||||
|
let res = Command::new("nix")
|
||||||
|
.args([
|
||||||
|
"--extra-experimental-features",
|
||||||
|
"nix-command",
|
||||||
|
"store",
|
||||||
|
"add-path",
|
||||||
|
&path.display().to_string(),
|
||||||
|
])
|
||||||
|
.output()
|
||||||
|
.await?;
|
||||||
|
if res.status.success() {
|
||||||
|
tokio::fs::remove_file(path).await?;
|
||||||
|
PathBuf::from(String::from_utf8_lossy(&res.stdout).trim())
|
||||||
|
} else {
|
||||||
|
path
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Update nix.conf. */
|
||||||
|
nix_conf
|
||||||
|
.write_all(
|
||||||
|
format!(
|
||||||
|
"fallback = true\npost-build-hook = {}\n",
|
||||||
|
post_build_hook_script.display()
|
||||||
|
)
|
||||||
|
.as_bytes(),
|
||||||
|
)
|
||||||
|
.with_context(|| "Writing to nix.conf")?;
|
||||||
|
|
||||||
|
drop(nix_conf);
|
||||||
|
}
|
||||||
|
|
||||||
let diagnostic_endpoint = match args.diagnostic_endpoint.as_str() {
|
let diagnostic_endpoint = match args.diagnostic_endpoint.as_str() {
|
||||||
"" => {
|
"" => {
|
||||||
|
|
Loading…
Reference in a new issue