Skip to content

Commit

Permalink
Merge pull request #52 from wobcom/fix/fairy/nix-not-installed
Browse files Browse the repository at this point in the history
don't run fairy if nix isn't installed
  • Loading branch information
Kek5chen authored Jun 18, 2024
2 parents bd6078e + 954365e commit 11524cd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion fairy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ serde_yaml = "0.9.25"
tokio = { version = "1.32.0", features = ["rt", "macros", "process"] }
tokio-util = { version = "0.7.9", features = ["codec"] }
uuid = { version = "1.4.1", features = ["serde"] }
rocket = { version="0.5.0", features = ["json", "secrets"] }
rocket = { version="0.5.0", features = ["json", "secrets"] }
which = "6.0.1"

[features]
nixless-test-mode = []
42 changes: 30 additions & 12 deletions fairy/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::process::{exit, Stdio};
use std::sync::Arc;

use anyhow::anyhow;
use futures_util::{Sink, StreamExt, TryStreamExt};
use hyper::{Body, Client, Method, Request};
use serde::de::DeserializeOwned;
use rocket::figment::{Figment, Profile};
use rocket::figment::providers::{Env, Format, Toml};
use serde::{Deserialize, Serialize};
use std::process::Stdio;
use std::sync::Arc;
use serde::de::DeserializeOwned;
use tokio::process::Command;
use tokio_util::codec::{FramedRead, LinesCodec};
use uuid::Uuid;

use rocket::figment::providers::{Env, Format, Toml};
use rocket::figment::{Figment, Profile};
use which::which;

#[derive(Deserialize)]
pub(crate) struct AppConfig {
Expand All @@ -20,11 +21,23 @@ pub(crate) struct AppConfig {
pub(crate) features: Vec<String>,
}

const CODE_NIX_NOT_INSTALLED: i32 = 1;

fn ensure_nix() {
if which("nix").is_err() {
log::error!("\"nix\" binary not found. Please install nix or run on a nix-os host.");
exit(CODE_NIX_NOT_INSTALLED);
}
}

fn main() -> anyhow::Result<()> {
env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.init();

#[cfg(not(feature = "nixless-test-mode"))]
ensure_nix();

log::info!("Fairy starting up.");

// Took from rocket source code and added .split("__") to be able to add keys in nested structures.
Expand Down Expand Up @@ -170,13 +183,18 @@ async fn try_run_task(cfg: Arc<AppConfig>, task: &Task) -> anyhow::Result<()> {
}

async fn run_task(cfg: Arc<AppConfig>, task: Task) {
#[cfg(not(feature = "nixless-test-mode"))]
let result = match try_run_task(cfg.clone(), &task).await {
Err(e) => {
log::info!("task failed: {} {} {:?}", task.id, task.display_name, e);
TaskResult::Error
}
Ok(_) => TaskResult::Success,
};
Err(e) => {
log::info!("task failed: {} {} {:?}", task.id, task.display_name, e);
TaskResult::Error
}
Ok(_) => TaskResult::Success,
};

#[cfg(feature = "nixless-test-mode")]
let result = TaskResult::Success;

tokio::time::sleep(std::time::Duration::from_secs(1)).await;
let _ = api::<_, ()>(
&cfg,
Expand Down

0 comments on commit 11524cd

Please sign in to comment.