Compare commits

...

6 commits

Author SHA1 Message Date
Luc Perkins e07b0fdbff
Merge remote-tracking branch 'origin/main' into optional-nix-conf 2024-07-22 09:09:59 -07:00
Luc Perkins 0d3959f304
Fix merge conflicts 2024-07-15 12:06:47 -07:00
Luc Perkins 6084fad5f6
Merge remote-tracking branch 'origin/main' into optional-nix-conf 2024-05-28 18:47:02 -03:00
Luc Perkins fabff97e46
Standardize on tokio::fs 2024-05-28 18:29:55 -03:00
Luc Perkins 50c47c34eb
Merge remote-tracking branch 'origin/main' into optional-nix-conf 2024-05-28 18:17:25 -03:00
Luc Perkins 094956a787
Make Nix configuration path optional 2024-05-26 16:47:14 -03:00
4 changed files with 66 additions and 41 deletions

View file

@ -8,3 +8,6 @@ end_of_line = lf
charset = utf-8 charset = utf-8
trim_trailing_whitespace = true trim_trailing_whitespace = true
insert_final_newline = true insert_final_newline = true
[*.rs]
indent_size = 4

View file

@ -30,7 +30,7 @@ jobs:
run: nix develop --command cargo fmt --check run: nix develop --command cargo fmt --check
- name: Clippy - name: Clippy
run: nix develop --command cargo clippy run: nix develop --command cargo clippy --all --all-targets --all-features -- -Dwarnings
build: build:
name: Build artifacts name: Build artifacts

View file

@ -6,6 +6,7 @@ use attic::nix_store::StorePath;
use axum::{extract::Extension, routing::post, Json, Router}; use axum::{extract::Extension, routing::post, Json, Router};
use axum_macros::debug_handler; use axum_macros::debug_handler;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::fs::read_to_string;
use super::State; use super::State;
use crate::error::{Error, Result}; use crate::error::{Error, Result};
@ -114,7 +115,7 @@ async fn workflow_finish(
// NOTE(cole-h): see `init_logging` // NOTE(cole-h): see `init_logging`
if let Some(logfile) = &state.logfile { if let Some(logfile) = &state.logfile {
let logfile_contents = std::fs::read_to_string(logfile)?; let logfile_contents = read_to_string(logfile).await?;
println!("Every log line throughout the lifetime of the program:"); println!("Every log line throughout the lifetime of the program:");
println!("\n{logfile_contents}\n"); println!("\n{logfile_contents}\n");
} }

View file

@ -21,8 +21,9 @@ mod gha;
mod telemetry; mod telemetry;
mod util; mod util;
use std::borrow::BorrowMut;
use std::collections::HashSet; use std::collections::HashSet;
use std::fs::{self, create_dir_all}; use std::fs::Permissions;
use std::io::Write; use std::io::Write;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
@ -34,7 +35,7 @@ use anyhow::{anyhow, Context, Result};
use axum::{extract::Extension, routing::get, Router}; use axum::{extract::Extension, routing::get, Router};
use clap::Parser; use clap::Parser;
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
use tokio::fs::File; use tokio::fs::{self, create_dir_all, File, OpenOptions};
use tokio::io::AsyncWriteExt; use tokio::io::AsyncWriteExt;
use tokio::process::Command; use tokio::process::Command;
use tokio::sync::{oneshot, Mutex, RwLock}; use tokio::sync::{oneshot, Mutex, RwLock};
@ -103,7 +104,7 @@ struct Args {
/// The location of `nix.conf`. /// The location of `nix.conf`.
#[arg(long)] #[arg(long)]
nix_conf: PathBuf, nix_conf: Option<PathBuf>,
/// Whether to use the GHA cache. /// Whether to use the GHA cache.
#[arg(long)] #[arg(long)]
@ -175,7 +176,7 @@ struct StateInner {
} }
async fn main_cli() -> Result<()> { async fn main_cli() -> Result<()> {
let guard = init_logging()?; let guard = init_logging().await?;
let _tracing_guard = guard.appender_guard; let _tracing_guard = guard.appender_guard;
let args = Args::parse(); let args = Args::parse();
@ -185,15 +186,24 @@ async fn main_cli() -> Result<()> {
let metrics = Arc::new(telemetry::TelemetryReport::new()); let metrics = Arc::new(telemetry::TelemetryReport::new());
if let Some(parent) = Path::new(&args.nix_conf).parent() { let mut nix_conf_file: Option<File> = if let Some(nix_conf_path) = &args.nix_conf {
create_dir_all(parent).with_context(|| "Creating parent directories of nix.conf")?; if let Some(parent) = Path::new(&nix_conf_path).parent() {
} create_dir_all(parent)
.await
.with_context(|| "Creating parent directories of nix.conf")?;
}
let mut nix_conf = std::fs::OpenOptions::new() Some(
.create(true) OpenOptions::new()
.append(true) .create(true)
.open(&args.nix_conf) .append(true)
.with_context(|| "Creating nix.conf")?; .open(nix_conf_path)
.await
.with_context(|| "Creating nix.conf")?,
)
} else {
None
};
let store = Arc::new(NixStore::connect()?); let store = Arc::new(NixStore::connect()?);
@ -221,16 +231,19 @@ async fn main_cli() -> Result<()> {
.await .await
{ {
Ok(state) => { Ok(state) => {
nix_conf if let Some(ref mut nix_conf_file) = nix_conf_file {
.write_all( nix_conf_file
format!( .write_all(
"extra-substituters = {}?trusted=1\nnetrc-file = {}\n", format!(
&flakehub_cache_server, "extra-substituters = {}?trusted=1\nnetrc-file = {}\n",
flakehub_api_server_netrc.display() &flakehub_cache_server,
flakehub_api_server_netrc.display()
)
.as_bytes(),
) )
.as_bytes(), .await
) .with_context(|| "Writing to nix.conf")?;
.with_context(|| "Writing to nix.conf")?; }
tracing::info!("FlakeHub cache is enabled."); tracing::info!("FlakeHub cache is enabled.");
Some(state) Some(state)
@ -250,7 +263,7 @@ async fn main_cli() -> Result<()> {
let gha_cache = if args.use_gha_cache { let gha_cache = if args.use_gha_cache {
let credentials = if let Some(credentials_file) = &args.credentials_file { let credentials = if let Some(credentials_file) = &args.credentials_file {
tracing::info!("Loading credentials from {:?}", credentials_file); tracing::info!("Loading credentials from {:?}", credentials_file);
let bytes = fs::read(credentials_file).with_context(|| { let bytes = fs::read(credentials_file).await.with_context(|| {
format!( format!(
"Failed to read credentials file '{}'", "Failed to read credentials file '{}'",
credentials_file.display() credentials_file.display()
@ -278,9 +291,12 @@ async fn main_cli() -> Result<()> {
) )
.with_context(|| "Failed to initialize GitHub Actions Cache API")?; .with_context(|| "Failed to initialize GitHub Actions Cache API")?;
nix_conf if let Some(ref mut nix_conf_file) = nix_conf_file {
.write_all(format!("extra-substituters = http://{}?trusted=1&compression=zstd&parallel-compression=true&priority=1\n", args.listen).as_bytes()) nix_conf_file
.with_context(|| "Writing to nix.conf")?; .write_all(format!("extra-substituters = http://{}?trusted=1&compression=zstd&parallel-compression=true&priority=1\n", args.listen).as_bytes())
.await
.with_context(|| "Writing to nix.conf")?;
}
tracing::info!("Native GitHub Action cache is enabled."); tracing::info!("Native GitHub Action cache is enabled.");
Some(gha_cache) Some(gha_cache)
@ -315,7 +331,8 @@ async fn main_cli() -> Result<()> {
.with_context(|| "Keeping the post-build hook")? .with_context(|| "Keeping the post-build hook")?
.1; .1;
fs::set_permissions(&path, fs::Permissions::from_mode(0o755)) fs::set_permissions(&path, Permissions::from_mode(0o755))
.await
.with_context(|| "Setting permissions on the post-build hook")?; .with_context(|| "Setting permissions on the post-build hook")?;
/* Copy the script to the Nix store so we know for sure that /* Copy the script to the Nix store so we know for sure that
@ -340,17 +357,20 @@ async fn main_cli() -> Result<()> {
}; };
/* Update nix.conf. */ /* Update nix.conf. */
nix_conf if let Some(ref mut nix_conf_file) = nix_conf_file.borrow_mut() {
.write_all( nix_conf_file
format!( .write_all(
"fallback = true\npost-build-hook = {}\n", format!(
post_build_hook_script.display() "fallback = true\npost-build-hook = {}\n",
post_build_hook_script.display()
)
.as_bytes(),
) )
.as_bytes(), .await
) .with_context(|| "Writing to nix.conf")?;
.with_context(|| "Writing to nix.conf")?; }
drop(nix_conf); drop(nix_conf_file);
let diagnostic_endpoint = match args.diagnostic_endpoint.as_str() { let diagnostic_endpoint = match args.diagnostic_endpoint.as_str() {
"" => { "" => {
@ -513,7 +533,7 @@ pub struct LogGuard {
logfile: Option<PathBuf>, logfile: Option<PathBuf>,
} }
fn init_logging() -> Result<LogGuard> { async fn init_logging() -> Result<LogGuard> {
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| { let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
return EnvFilter::new("info") return EnvFilter::new("info")
@ -531,12 +551,13 @@ fn init_logging() -> Result<LogGuard> {
let (guard, file_layer) = match std::env::var("RUNNER_DEBUG") { let (guard, file_layer) = match std::env::var("RUNNER_DEBUG") {
Ok(val) if val == "1" => { Ok(val) if val == "1" => {
let logfile = debug_logfile(); let logfile = debug_logfile();
let file = std::fs::OpenOptions::new() let file = OpenOptions::new()
.create(true) .create(true)
.write(true) .write(true)
.truncate(true) .truncate(true)
.open(&logfile)?; .open(&logfile)
let (nonblocking, guard) = tracing_appender::non_blocking(file); .await?;
let (nonblocking, guard) = tracing_appender::non_blocking(file.into_std().await);
let file_layer = tracing_subscriber::fmt::layer() let file_layer = tracing_subscriber::fmt::layer()
.with_writer(nonblocking) .with_writer(nonblocking)
.pretty(); .pretty();