Create binary cache
Build nomnom / Build-NomNom (push) Failing after 4m1s Details

This commit is contained in:
Félix Baylac Jacqué 2024-01-30 12:00:39 +01:00
parent b9d09e1c99
commit 5aca7a8cdb
11 changed files with 79 additions and 77 deletions

56
Cargo.lock generated
View File

@ -8,7 +8,7 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "617a8268e3537fe1d8c9ead925fca49ef6400927ee7bc26750e90ecee14ce4b8"
dependencies = [
"bitflags 1.3.2",
"bitflags",
"bytes",
"futures-core",
"futures-sink",
@ -19,29 +19,6 @@ dependencies = [
"tracing",
]
[[package]]
name = "actix-files"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf0bdd6ff79de7c9a021f5d9ea79ce23e108d8bfc9b49b5b4a2cf6fad5a35212"
dependencies = [
"actix-http",
"actix-service",
"actix-utils",
"actix-web",
"bitflags 2.4.2",
"bytes",
"derive_more",
"futures-core",
"http-range",
"log",
"mime",
"mime_guess",
"percent-encoding",
"pin-project-lite",
"v_htmlescape",
]
[[package]]
name = "actix-http"
version = "3.3.1"
@ -54,7 +31,7 @@ dependencies = [
"actix-utils",
"ahash 0.8.3",
"base64 0.21.2",
"bitflags 1.3.2",
"bitflags",
"brotli",
"bytes",
"bytestring",
@ -842,12 +819,6 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
[[package]]
name = "block-buffer"
version = "0.10.3"
@ -952,7 +923,7 @@ version = "4.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d63b9e9c07271b9957ad22c173bae2a4d9a81127680962039296abcd2f8251d"
dependencies = [
"bitflags 1.3.2",
"bitflags",
"clap_derive",
"clap_lex",
"is-terminal",
@ -1590,12 +1561,6 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "http-range"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573"
[[package]]
name = "httparse"
version = "1.8.0"
@ -1880,7 +1845,6 @@ dependencies = [
name = "nom-nom-gc"
version = "0.1.0"
dependencies = [
"actix-files",
"actix-web",
"anyhow",
"aws-config",
@ -1975,7 +1939,7 @@ version = "0.10.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b102428fd03bc5edf97f62620f7298614c45cedf287c271e7ed450bbaf83f2e1"
dependencies = [
"bitflags 1.3.2",
"bitflags",
"cfg-if",
"foreign-types",
"libc",
@ -2292,7 +2256,7 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [
"bitflags 1.3.2",
"bitflags",
]
[[package]]
@ -2464,7 +2428,7 @@ version = "0.36.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3807b5d10909833d3e9acd1eb5fb988f79376ff10fce42937de71a449c4c588"
dependencies = [
"bitflags 1.3.2",
"bitflags",
"errno",
"io-lifetimes",
"libc",
@ -2581,7 +2545,7 @@ version = "2.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de"
dependencies = [
"bitflags 1.3.2",
"bitflags",
"core-foundation",
"core-foundation-sys",
"libc",
@ -3146,12 +3110,6 @@ dependencies = [
"serde",
]
[[package]]
name = "v_htmlescape"
version = "0.15.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e8257fbc510f0a46eb602c10215901938b5c2a7d5e70fc11483b1d3c9b5b18c"
[[package]]
name = "vcpkg"
version = "0.2.15"

View File

@ -7,7 +7,6 @@ edition = "2021"
[dependencies]
actix-web = "4"
actix-files = "*"
mime = "*"
mime_guess = "*"
rust-embed = "*"

View File

@ -10,5 +10,5 @@ pkgs.rustPlatform.buildRustPackage {
];
buildInputs = [ pkgs.openssl ];
src = pkgs.lib.cleanSource ./.;
cargoHash = "sha256-ct90VmvzUIJ8d0LO7jz5/kcC3M0rU85DbX81/67T2R4=";
cargoHash = "sha256-+fS2adHUigIgnporYk6rKvxUY2aSKbvssPnQYnzU+4Y=";
}

View File

@ -1,6 +1,6 @@
CREATE TABLE Users (
id UUID PRIMARY KEY NOT NULL,
user_name text NOT NULL
user_name text NOT NULL UNIQUE
);
CREATE TABLE Keys (
@ -19,7 +19,7 @@ CREATE TABLE PendingRegistrations (
CREATE TABLE BinaryCaches (
id SERIAL PRIMARY KEY NOT NULL,
name text NOT NULL,
name text NOT NULL UNIQUE,
access_key text NOT NULL,
secret_key text NOT NULL,
region text NOT NULL,

View File

@ -1,6 +1,6 @@
use actix_web::{web::{self, Form}, HttpResponse, http::header::{ContentType, self}, Responder, HttpRequest};
use crate::{models::{AppState, BinaryCache}, templates};
use crate::{models::{AppState, BinaryCache}, templates, s3::{check_bucket, create_client}};
use super::check_authentication;
@ -21,11 +21,18 @@ pub async fn new_binary_cache(app_state: web::Data<AppState<'_>>, req: HttpReque
pub async fn new_binary_cache_post(app_state: web::Data<AppState<'_>>, req: HttpRequest, form: Form<BinaryCache>) -> impl Responder {
if check_authentication(&app_state, req).await.is_ok() {
app_state.create_binary_cache(&form).await.unwrap();
HttpResponse::SeeOther()
.content_type(ContentType::plaintext())
.append_header((header::LOCATION, "/"))
.body("new binary cache created")
let client = create_client(form.clone());
if ! check_bucket(&client, &form.bucket).await.is_ok() {
HttpResponse::BadRequest()
.content_type(ContentType::plaintext())
.body("can't connect to the binary cache")
} else {
app_state.create_binary_cache(&form).await.unwrap();
HttpResponse::SeeOther()
.content_type(ContentType::plaintext())
.append_header((header::LOCATION, "/"))
.body("new binary cache created")
}
} else {
HttpResponse::Forbidden()
.finish()

View File

@ -100,7 +100,8 @@ pub struct BinaryCache {
pub access_key: String,
pub secret_key: String,
pub region: String,
pub endpoint_url: String
pub endpoint_url: String,
pub bucket: String
}
#[derive(Clone, Debug, Eq, PartialEq)]

View File

@ -1,10 +1,23 @@
use anyhow::{anyhow, Result};
use aws_sdk_s3::{operation::head_bucket::HeadBucketOutput, Client};
use aws_config::{Region, BehaviorVersion};
use aws_sdk_s3::{operation::head_bucket::HeadBucketOutput, Client, config::Credentials};
use aws_types::sdk_config::SharedCredentialsProvider;
use crate::models::Configuration;
use crate::models::BinaryCache;
pub async fn check_bucket(client: &Client, config: &Configuration) -> Result<HeadBucketOutput> {
println!("{}",&config.s3_bucket);
let res = client.head_bucket().bucket(&config.s3_bucket).send().await;
pub async fn check_bucket(client: &Client, bucket_name: &str) -> Result<HeadBucketOutput> {
println!("{}", bucket_name);
let res = client.head_bucket().bucket(bucket_name).send().await;
res.map_err(|e| anyhow!("Cannot access the binary cache bucket: {}", e))
}
pub fn create_client(bc: BinaryCache) -> aws_sdk_s3::Client {
let credentials = Credentials::new(bc.access_key, bc.secret_key, None, None, "nom-nom-provider");
let s3_client_config = aws_config::SdkConfig::builder()
.endpoint_url(bc.endpoint_url)
.region(Some(Region::new(bc.region)))
.credentials_provider(SharedCredentialsProvider::new(credentials))
.behavior_version(BehaviorVersion::latest())
.build();
aws_sdk_s3::Client::new(&s3_client_config)
}

View File

@ -7,7 +7,6 @@ use mime_guess::from_path;
use nom_nom_gc::handlers;
use nom_nom_gc::models::read_config;
use nom_nom_gc::models;
use nom_nom_gc::s3::check_bucket;
use rust_embed::RustEmbed;
#[derive(Parser, Debug)]
@ -42,12 +41,6 @@ async fn main() -> std::io::Result<()> {
let state = models::AppState::new(config.clone()).await;
println!("Running DB migrations");
state.run_migrations().await.unwrap_or_else(|e| panic!("Db migration error: {}", e));
println!("Checking binary cache bucket");
let bucket = check_bucket(&state.s3_client, &config).await;
match bucket {
Ok(_) => println!("Connection to the bucket successful"),
Err(e) => panic!("Cannot connect to the binary cache bucket: {}", e)
}
println!("Server listening to {}", &args.bind);
HttpServer::new(
move || {

View File

@ -52,7 +52,7 @@ form {
width: 20em;
margin: auto;
padding: 1em;
border: 3px solid;
border: 3px solid black;
border-radius: 9px;
}
@ -85,8 +85,31 @@ form > #submit {
font-size: 2em;
margin-left: 0;
text-align: center;
border-right: 2px solid black;
margin-left: .2em;
}
nav {
display: flex;
padding: 0;
}
table {
width: 30em;
margin: auto;
margin-top: 2em;
border: solid 3px black;
border-radius: 9px;
}
td {
padding-left: .5em;
}
tr:nth-child(even) {
background-color: white;
}
tr:nth-child(odd) {
background-color: #f1f3f9;
}

View File

@ -1,7 +1,7 @@
{{#> template }}
<h1>New Binary Cache</h1>
<form id="binary-cache-form" name="binary-cache" method="post" action="/binary-cache/new">
<label>Bucket Name</label>
<label>Binary Cache Name</label>
<input name="name" placeholder="Nix Cache"/>
<label>Bucket Access Key</label>
<input name="access_key" placeholder="AKIAIOSFODNN7EXAMPLE"/>
@ -11,6 +11,8 @@
<input name="region" placeholder="garage"/>
<label>Bucket Endpoint</label>
<input name="endpoint_url" placeholder="https://cache.yourdomain.com"/>
<label>Bucket</label>
<input name="bucket" placeholder="nix-cache"/>
<input id="submit" type="submit" value="Create binary cache"/>
</form>
{{ /template }}

View File

@ -55,10 +55,15 @@ async fn run_test_db(db: &TestDB) -> Result<()> {
url: "http://localhost:9000".to_string(),
db_port: Some(db.port),
db_host: Some(dbpath),
db_name: db.db_name.clone()
db_name: db.db_name.clone(),
s3_bucket: "dummy".to_string(),
s3_endpoint: "dummy".to_string(),
s3_region: "dummy".to_string(),
s3_access_key_filepath: "dummy".to_string(),
s3_secret_key_filepath: "dummy".to_string(),
};
let state = AppState::new(conf);
let state = AppState::new(conf).await;
state.run_migrations().await?;
let test_user = User { uuid: UserUuid(Uuid::new_v4()), name: "test-user".to_owned() };
@ -74,7 +79,8 @@ async fn run_test_db(db: &TestDB) -> Result<()> {
access_key: "access key".to_string(),
secret_key: "secret key".to_string(),
region: "reg-01".to_string(),
endpoint_url: "localhost:"
endpoint_url: "localhost://".to_string(),
bucket: "nix-cache".to_string()
};
state.create_binary_cache(&binary_cache).await?;
let project = Project {