This commit is contained in:
nora 2022-09-28 20:44:42 +02:00
parent 4bcd725fb0
commit e78b5becde
8 changed files with 1614 additions and 54 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
bisect.sqlite

1194
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

18
Cargo.toml Normal file
View file

@ -0,0 +1,18 @@
[package]
name = "cargo-bisect-rustc-service"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
axum = "0.5.16"
chrono = { version = "0.4.22", features = ["serde"] }
color-eyre = "0.6.2"
rusqlite = { version = "0.28.0", features = ["bundled", "uuid"] }
serde = { version = "1.0.145", features = ["derive"] }
tempdir = "0.3.7"
tokio = { version = "1.21.2", features = ["full"] }
tracing = "0.1.36"
tracing-subscriber = "0.3.15"
uuid = { version = "1.1.2", features = ["serde", "v4"] }

30
Dockerfile Normal file
View file

@ -0,0 +1,30 @@
FROM rust as build
RUN rustup toolchain install nightly
RUN rustup default nightly
RUN rustup target add x86_64-unknown-linux-musl
RUN apt-get update
RUN apt-get install musl-tools -y
WORKDIR /app
COPY Cargo.toml Cargo.lock ./
RUN mkdir src
RUN echo "fn main() {}" > src/main.rs
RUN cargo build --release -Zsparse-registry --target x86_64-unknown-linux-musl
COPY src ./src
# now rebuild with the proper main
RUN touch src/main.rs
RUN cargo build --release -Zsparse-registry --target x86_64-unknown-linux-musl
### RUN
FROM gcr.io/distroless/static
WORKDIR /app
COPY --from=build /app/target/x86_64-unknown-linux-musl/release/cargo-bisect-rustc-service cargo-bisect-rustc-service
ENTRYPOINT ["/app/cargo-bisect-rustc-service"]

View file

@ -1,23 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<head>
<title>Bisect rustc</title>
<style>
html {
font-family: Arial, Helvetica, sans-serif;
}
.hidden {
display: none
display: none;
}
#code {
columns: 100;
}
.bisect-btn {
width: 200px;
height: 50px;
}
</style>
</head>
<body>
</head>
<body>
<h1>Bisect rustc</h1>
<textarea id="code">
// Rust code goes here...
<textarea
id="code"
rows="30"
cols="80"
placeholder="// Rust code goes here..."
>
fn uwu() {}
</textarea>
<button onclick="bisect()">Bisect!</button>
<br />
<button class="bisect-btn" onclick="bisect()">Bisect!</button>
<div id="status" class="hidden"/>
<div id="result" class="hidden"/>
<div id="status" class="hidden" />
<div id="result" class="hidden" />
<script>
let bisecting = false;
@ -33,14 +52,19 @@
status.classList.remove("hidden");
status.innerText = "Sending request...";
const fetched = await fetch("https://bisect-rustc.nilstrieb.dev/bisect", {
const fetched = await fetch(
"https://bisect-rustc.nilstrieb.dev/bisect",
{
method: "POST",
body: code.value,
});
}
);
const { id } = await fetched.json();
function tryFetch() {
const fetched = await fetch(`https://bisect-rustc.nilstrieb.dev/bisect/${id}`);
async function tryFetch() {
const fetched = await fetch(
`https://bisect-rustc.nilstrieb.dev/bisect/${id}`
);
const { done, output, time } = await fetched.json();
if (done) {
@ -57,5 +81,5 @@
status.innerHTML = `Waiting for bisection, job id=${id}`;
}
</script>
</body>
</body>
</html>

135
src/bisect.rs Normal file
View file

@ -0,0 +1,135 @@
use std::{fs, process::Command, sync::mpsc};
use color_eyre::eyre::Context;
use rusqlite::Connection;
use serde::Serialize;
use tracing::{error, info};
use uuid::Uuid;
use crate::Options;
#[derive(Debug, Serialize)]
pub enum BisectStatus {
InProgress,
Error(String),
Success(String),
}
#[derive(Debug, Serialize)]
pub struct Bisection {
pub id: Uuid,
pub code: String,
pub status: BisectStatus,
}
pub struct Job {
id: Uuid,
code: String,
options: Options,
}
enum JobState {
Failed(String),
Success(String),
}
impl JobState {
fn status(&self) -> &'static str {
match self {
Self::Failed(_) => "error",
Self::Success(_) => "success",
}
}
}
impl std::fmt::Debug for Job {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Job")
.field("id", &self.id)
.field("options", &self.options)
.finish_non_exhaustive()
}
}
impl Job {
pub fn new(id: Uuid, code: String, options: Options) -> Self {
Self { id, code, options }
}
}
pub fn bisect_worker(jobs: mpsc::Receiver<Job>, conn: Connection) {
loop {
let job = match jobs.recv() {
Ok(job) => job,
Err(_) => return,
};
info!(id = %job.id, "Starting bisection job");
bisect_job(job);
}
}
#[tracing::instrument(skip(job), fields(id = %job.id))]
fn bisect_job(job: Job) {
match run_bisect_for_file(job.code, job.options.start, job.options.end) {
Ok(state) => {
info!(state = %state.status(), "Bisection finished");
}
Err(err) => {
error!(?err, "Error during bisection");
}
}
}
fn run_bisect_for_file(
input: String,
start: Option<chrono::NaiveDate>,
end: Option<chrono::NaiveDate>,
) -> color_eyre::Result<JobState> {
let temp_dir = tempdir::TempDir::new("bisect").wrap_err("creating tempdir")?;
let mut cargo_new = Command::new("cargo");
cargo_new
.arg("new")
.arg("bisect")
.arg("--lib") // lib even works with `fn main() {}`
.current_dir(&temp_dir);
let output = cargo_new.output().wrap_err("cargo init")?;
output
.status
.exit_ok()
.wrap_err_with(|| format!("running cargo: {}", String::from_utf8_lossy(&output.stderr)))?;
let cargo_dir = temp_dir.path().join("bisect");
fs::write(cargo_dir.join("src").join("lib.rs"), input).wrap_err("writing code to lib.rs")?;
let mut bisect = Command::new("cargo-bisect-rustc");
bisect.arg("--preserve"); // preserve toolchains for future runs
bisect.arg("--access").arg("github"); // ask the github api about the commits
bisect.arg("--timeout").arg("30"); // don't hang
bisect.current_dir(&cargo_dir);
if let Some(start) = start {
bisect.arg("--start").arg(start.to_string());
}
if let Some(end) = end {
bisect.arg("--end").arg(end.to_string());
}
let output = bisect.output().wrap_err("spawning cargo-bisect-rustc")?;
if output.status.success() {
Ok(JobState::Success(
String::from_utf8(output.stdout)
.wrap_err("cargo-bisect-rustc stdout utf8 validation")?,
))
} else {
Ok(JobState::Failed(
String::from_utf8(output.stderr)
.wrap_err("cargo-bisect-rustc stderr utf8 validation")?,
))
}
}

50
src/db.rs Normal file
View file

@ -0,0 +1,50 @@
use color_eyre::eyre::Context;
use rusqlite::Connection;
use tracing::info;
use crate::bisect::{BisectStatus, Bisection};
pub fn setup(conn: &Connection) -> color_eyre::Result<()> {
conn.execute(
"CREATE TABLE IF NOT EXISTS bisect (
job_id STRING PRIMARY KEY,
code STRING NOT NULL,
status INTEGER NOT NULL DEFAULT 0,
stdout_stderr STRING -- stdout or stderr depending on the status
)",
(),
)
.wrap_err("setup sqlite table")?;
info!("Finished db setup");
Ok(())
}
pub fn add_bisection(conn: &Connection) -> color_eyre::Result<()> {
Ok(())
}
pub fn get_bisections(conn: &Connection) -> color_eyre::Result<Vec<Bisection>> {
let mut select = conn
.prepare("SELECT job_id, code, status, stdout, stderr FROM bisect")
.wrap_err("preparing select")?;
let iter = select
.query_map([], |row| {
Ok(Bisection {
id: row.get(0)?,
code: row.get(1)?,
status: match row.get(2)? {
0 => BisectStatus::InProgress,
1 => BisectStatus::Error(row.get(3)?),
2 => BisectStatus::Success(row.get(3)?),
_ => return Err(rusqlite::Error::InvalidQuery), // actually not lol
},
})
})
.wrap_err("getting bisections from db query")?;
iter.collect::<Result<Vec<_>, rusqlite::Error>>()
.wrap_err("getting bisections from db")
}

107
src/main.rs Normal file
View file

@ -0,0 +1,107 @@
#![feature(exit_status_error)]
mod bisect;
mod db;
use std::sync::{mpsc, Arc, Mutex};
use axum::{
extract::Query,
http::StatusCode,
response::{Html, IntoResponse},
routing::{get, post, Router},
Extension, Json,
};
use bisect::Job;
use color_eyre::eyre::Context;
use rusqlite::Connection;
use serde::{Deserialize, Serialize};
use std::env;
use tracing::{error, info};
use uuid::Uuid;
type SendChannel = Arc<Mutex<mpsc::Sender<Job>>>;
type Conn = Arc<Mutex<Connection>>;
#[tokio::main]
async fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
tracing_subscriber::fmt().init();
let (job_queue_send, job_queue_recv) = mpsc::channel();
let sqlite_db = env::var("SQLITE_DB").unwrap_or_else(|_| "bisect.sqlite".to_string());
let main_conn = Connection::open(&sqlite_db)
.wrap_err_with(|| format!("connect to sqlite with file path: {}", sqlite_db))?;
let main_conn = Arc::new(Mutex::new(main_conn));
let worker_conn = Connection::open(&sqlite_db)
.wrap_err_with(|| format!("connect to sqlite with file path: {}", sqlite_db))?;
db::setup(&worker_conn).wrap_err("db setup")?;
let app = Router::new()
.route("/", get(|| async { index_html() }))
// .route("/bisect/:id", get(get_bisection))
.route("/bisect", get(get_bisections))
.route("/bisect", post(do_bisection))
// this is really stupid and hacky
.layer(Extension(Arc::new(Mutex::new(job_queue_send))))
.layer(Extension(main_conn));
std::thread::spawn(|| bisect::bisect_worker(job_queue_recv, worker_conn));
info!("Starting up server");
axum::Server::bind(&"0.0.0.0:4000".parse().unwrap())
.serve(app.into_make_service())
.await
.wrap_err("failed to start server")
}
fn index_html() -> impl IntoResponse {
Html(include_str!("../index.html"))
}
async fn get_bisections(Extension(conn): Extension<Conn>) -> impl IntoResponse {
db::get_bisections(&conn.lock().unwrap())
.map(|bisections| Json(bisections))
.map_err(|err| {
error!(?err, "error getting bisections");
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string())
})
}
#[derive(Debug, Deserialize)]
pub struct Options {
start: Option<chrono::NaiveDate>,
end: Option<chrono::NaiveDate>,
}
#[derive(Debug, Serialize)]
struct JobIdReturn {
job_id: Uuid,
}
async fn do_bisection(
options: Query<Options>,
body: String,
send_channel: Extension<SendChannel>,
) -> impl IntoResponse {
let job_id = Uuid::new_v4();
let job = Job::new(job_id, body, options.0);
match send_channel.0.lock().unwrap().send(job) {
Ok(_) => {
info!(id = %job_id, "Added new job to queue");
Ok(Json(JobIdReturn { job_id }))
}
Err(_) => Err((
StatusCode::TOO_MANY_REQUESTS,
"Too many jobs in the queue already",
)),
}
}