Parse command and configuration file options for the server

This allows Janky to start setting up projects and agents
This commit is contained in:
R Tyler Croy 2023-01-28 19:36:20 -08:00
parent eea8fbf8a9
commit 05fb2d5889
No known key found for this signature in database
GPG Key ID: E5C92681BEF6CEA2
5 changed files with 92 additions and 19 deletions

View File

@ -13,17 +13,20 @@ path = "src/agent/main.rs"
[dependencies]
async-std = { version = "1", features = ["attributes"] }
chrono = "0.4.15"
serde = { version = "1.0", features = ["derive"] }
chrono = "0.4"
dotenv = "~0.15"
driftwood = "0"
handlebars = { version = "~3.4.0", features = ["dir_source"] }
html-escape = "~0.2.6"
# Command line parsing
gumdrop = "0.8"
handlebars = { version = "3", features = ["dir_source"] }
html-escape = "0.2"
log = "~0.4.8"
os_pipe = "1"
pretty_env_logger = "~0.3.1"
serde_json = "~1.0.0"
sqlx = { version = "~0.5.1", features = ["chrono", "json", "migrate", "offline", "sqlite", "uuid", "runtime-async-std-rustls"] }
pretty_env_logger = "~0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
serde_yaml = "0.9"
sqlx = { version = "~0.6", features = ["chrono", "json", "migrate", "offline", "sqlite", "uuid", "runtime-async-std-rustls"] }
tide = "0"
uuid = { version = "1", features = ["v4", "serde"]}
url = "2"

View File

@ -3,8 +3,6 @@
Janky is a simple CI system built in Rust. This is performative coding and not
intended to be a production system you can actually use.
* Two binaries:
* `janky-server`
* Listens HTTP

13
examples/server.yml Normal file
View File

@ -0,0 +1,13 @@
---
agents:
- 'http://localhost:9000'
projects:
- type: 'git'
url: 'https://github.com/rtyler/janky'
ref: 'main'
filename: 'Jankyfile'
- type: 'git'
url: 'https://github.com/rtyler/jdp'
ref: 'main'
filename: 'ci/Jankyfile'

View File

@ -1,10 +1,9 @@
CREATE TABLE agents (
id SERIAL PRIMARY KEY,
uuid UUID NOT NULL UNIQUE,
name TEXT NOT NULL,
capabilities TEXT,
url TEXT NOT NUll,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
id INTEGER PRIMARY KEY,
uuid TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
capabilities TEXT,
url TEXT NOT NUll,
created_at TEXT NOT NULL
);
CREATE UNIQUE INDEX uuid_idx ON agents(uuid);

View File

@ -5,11 +5,16 @@
#[macro_use]
extern crate serde_json;
use std::path::PathBuf;
use async_std::sync::{Arc, RwLock};
use dotenv::dotenv;
use gumdrop::Options;
use handlebars::Handlebars;
use log::*;
use serde::Deserialize;
use sqlx::SqlitePool;
use url::Url;
#[derive(Clone, Debug)]
pub struct AppState<'a> {
@ -76,16 +81,71 @@ mod routes {
pub mod api {}
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all="lowercase")]
enum Scm {
Git,
}
#[derive(Clone, Debug, Deserialize)]
struct Project {
#[serde(rename="type")]
scm_type: Scm,
url: Url,
#[serde(rename="ref")]
scm_ref: String,
filename: PathBuf,
}
#[derive(Clone, Debug, Deserialize)]
struct ServerConfig {
agents: Vec<Url>,
projects: Vec<Project>,
}
impl Default for ServerConfig {
fn default() -> Self {
Self {
agents: vec![],
projects: vec![],
}
}
}
#[derive(Debug,Options)]
struct ServerOptions {
#[options(help = "print help message")]
help: bool,
#[options(help="host:port to bind the server to", default="0.0.0.0:8000")]
listen: String,
#[options(help="Path to the configuration file")]
config: Option<PathBuf>,
#[options(help="Comma separated list of URLs for agents")]
agents: Vec<Url>,
}
#[async_std::main]
async fn main() -> Result<(), tide::Error> {
pretty_env_logger::init();
dotenv().ok();
let opts = ServerOptions::parse_args_default_or_exit();
debug!("Starting with options: {:?}", opts);
let config = match opts.config {
Some(path) => {
let config_file = std::fs::File::open(path).expect("Failed to open config file");
serde_yaml::from_reader(config_file).expect("Failed to read config file")
},
None => ServerConfig::default(),
};
debug!("Starting with config: {:?}", config);
let database_url = std::env::var("DATABASE_URL").unwrap_or(":memory:".to_string());
let pool = SqlitePool::connect(&database_url).await?;
/* If janky-server is running in memory, make sure the database is set up properly */
if database_url == ":memory:" {
// TODO: Figure out why failing
//sqlx::migrate!().run(&pool).await?;
sqlx::migrate!().run(&pool).await?;
}
let state = AppState::new(pool);
@ -121,6 +181,6 @@ async fn main() -> Result<(), tide::Error> {
app.at("/static").serve_dir("static/")?;
debug!("Configuring routes");
app.at("/").get(routes::index);
app.listen("0.0.0.0:8000").await?;
app.listen(opts.listen).await?;
Ok(())
}