Added support for optionally deploying to subdir (for docs later)
parent
a3cf021fc6
commit
b4c8216ebc
|
@ -1,2 +1,3 @@
|
|||
/target
|
||||
/data/
|
||||
*.tar.gz
|
||||
|
|
|
@ -791,6 +791,20 @@ name = "serde"
|
|||
version = "1.0.136"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.136"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
|
@ -843,6 +857,7 @@ dependencies = [
|
|||
"hyper",
|
||||
"metrics",
|
||||
"metrics-exporter-prometheus",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tar",
|
||||
"tokio",
|
||||
|
|
|
@ -25,3 +25,4 @@ uuid = { version = "1.0.0-alpha.1", features = ["v4"] }
|
|||
serde_json = "1.0.79"
|
||||
metrics = "0.18.1"
|
||||
metrics-exporter-prometheus = "0.9.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
curl \
|
||||
-XPOST \
|
||||
-T test.tar.gz \
|
||||
-H 'Authorization: Bearer test' \
|
||||
-v \
|
||||
http://localhost:3000/api/deploy?dir=docs
|
|
@ -1,19 +1,27 @@
|
|||
use std::{collections::HashSet, io::ErrorKind, path::Path};
|
||||
|
||||
use axum::{
|
||||
extract::{BodyStream, Extension},
|
||||
extract::{BodyStream, Extension, Query},
|
||||
http::StatusCode,
|
||||
response::IntoResponse,
|
||||
};
|
||||
use flate2::read::GzDecoder;
|
||||
use futures_util::TryStreamExt;
|
||||
use serde::Deserialize;
|
||||
use tar::Archive;
|
||||
use tokio_util::io::StreamReader;
|
||||
|
||||
use crate::STATIC_DIR_NAME;
|
||||
use crate::DEFAULT_STATIC_DIR_NAME;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct StaticDirParams
|
||||
{
|
||||
dir: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn post_deploy(
|
||||
Extension(data_dir): Extension<String>,
|
||||
Query(params): Query<StaticDirParams>,
|
||||
res: BodyStream,
|
||||
) -> impl IntoResponse
|
||||
{
|
||||
|
@ -26,46 +34,53 @@ pub async fn post_deploy(
|
|||
let mut file = tokio::fs::File::create(&file_path).await.unwrap();
|
||||
tokio::io::copy(&mut read, &mut file).await;
|
||||
|
||||
let mut static_path = Path::new(&data_dir).join(DEFAULT_STATIC_DIR_NAME);
|
||||
|
||||
if params.dir.is_some() {
|
||||
static_path = static_path.join(params.dir.unwrap());
|
||||
}
|
||||
|
||||
// Make sure the static directory exists
|
||||
tokio::fs::create_dir_all(&static_path).await;
|
||||
|
||||
let fp_clone = file_path.clone();
|
||||
// Extract the contents of the tarball synchronously
|
||||
match tokio::task::spawn_blocking(move || {
|
||||
let file = match std::fs::File::open(file_path) {
|
||||
Ok(v) => v,
|
||||
Err(_) => return StatusCode::INTERNAL_SERVER_ERROR,
|
||||
let res =
|
||||
match tokio::task::spawn_blocking(move || process_archive(&fp_clone, &static_path)).await {
|
||||
Ok(_) => StatusCode::OK,
|
||||
Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
};
|
||||
|
||||
// Remove archive file after use
|
||||
tokio::fs::remove_file(&file_path).await;
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
fn process_archive(archive_path: &Path, static_dir: &Path) -> Result<(), ()>
|
||||
{
|
||||
let file = std::fs::File::open(archive_path).map_err(|_| ())?;
|
||||
let tar = GzDecoder::new(file);
|
||||
let mut archive = Archive::new(tar);
|
||||
|
||||
let mut paths = HashSet::new();
|
||||
|
||||
let entries = match archive.entries() {
|
||||
Ok(e) => e,
|
||||
Err(_) => return StatusCode::INTERNAL_SERVER_ERROR,
|
||||
};
|
||||
|
||||
let entries = archive.entries().map_err(|_| ())?;
|
||||
// Extract each entry into the output directory
|
||||
let static_dir = Path::new(&data_dir).join(STATIC_DIR_NAME);
|
||||
for entry_res in entries {
|
||||
if let Ok(mut entry) = entry_res {
|
||||
if let Err(_) = entry.unpack_in(&static_dir) {
|
||||
return StatusCode::INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
let mut entry = entry_res.map_err(|_| ())?;
|
||||
entry.unpack_in(static_dir).map_err(|_| ())?;
|
||||
|
||||
if let Ok(path) = entry.path() {
|
||||
paths.insert(path.into_owned());
|
||||
}
|
||||
} else {
|
||||
return StatusCode::INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove any old files that weren't present in new archive
|
||||
let mut items = vec![];
|
||||
|
||||
// Start by populating the vec with the initial files
|
||||
let iter = match static_dir.read_dir() {
|
||||
Ok(v) => v,
|
||||
Err(_) => return StatusCode::INTERNAL_SERVER_ERROR,
|
||||
};
|
||||
let iter = static_dir.read_dir().map_err(|_| ())?;
|
||||
iter.filter_map(|r| r.ok())
|
||||
.for_each(|e| items.push(e.path()));
|
||||
|
||||
|
@ -86,11 +101,5 @@ pub async fn post_deploy(
|
|||
}
|
||||
}
|
||||
|
||||
StatusCode::OK
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(s) => s,
|
||||
Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ mod api;
|
|||
mod matrix;
|
||||
mod metrics;
|
||||
|
||||
const STATIC_DIR_NAME: &str = "static";
|
||||
const DEFAULT_STATIC_DIR_NAME: &str = "static";
|
||||
|
||||
#[tokio::main]
|
||||
async fn main()
|
||||
|
@ -30,7 +30,7 @@ async fn main()
|
|||
// Get required variables from env vars
|
||||
let api_key = std::env::var("API_KEY").expect("No API_KEY was provided.");
|
||||
let data_dir = std::env::var("DATA_DIR").expect("No DATA_DIR was provided.");
|
||||
let static_dir = format!("{}/{}", data_dir, STATIC_DIR_NAME);
|
||||
let static_dir = format!("{}/{}", data_dir, DEFAULT_STATIC_DIR_NAME);
|
||||
|
||||
std::fs::create_dir_all(&static_dir);
|
||||
|
||||
|
|
Loading…
Reference in New Issue