outsource package logic into package

This commit is contained in:
lukas-heiligenbrunner 2024-02-15 20:33:42 +01:00
parent d344ac2d73
commit b641cc3c96
8 changed files with 208 additions and 173 deletions

View File

@ -5,14 +5,13 @@ use crate::api::list::search;
use crate::api::list::{get_build, okapi_add_operation_for_list_builds_}; use crate::api::list::{get_build, okapi_add_operation_for_list_builds_};
use crate::api::list::{list_builds, okapi_add_operation_for_search_}; use crate::api::list::{list_builds, okapi_add_operation_for_search_};
use crate::api::list::{okapi_add_operation_for_build_output_, stats}; use crate::api::list::{okapi_add_operation_for_build_output_, stats};
use crate::api::package::okapi_add_operation_for_get_package_; use crate::api::package::okapi_add_operation_for_package_add_endpoint_;
use crate::api::package::okapi_add_operation_for_package_del_; use crate::api::package::okapi_add_operation_for_package_del_;
use crate::api::package::okapi_add_operation_for_package_list_; use crate::api::package::okapi_add_operation_for_package_list_;
use crate::api::package::okapi_add_operation_for_package_update_; use crate::api::package::okapi_add_operation_for_package_update_endpoint_;
use crate::api::package::package_add; use crate::api::package::package_add_endpoint;
use crate::api::package::{ use crate::api::package::{get_package, package_del, package_list};
get_package, okapi_add_operation_for_package_add_, package_del, package_list, package_update, use crate::api::package::{okapi_add_operation_for_get_package_, package_update_endpoint};
};
use crate::api::remove::okapi_add_operation_for_version_del_; use crate::api::remove::okapi_add_operation_for_version_del_;
use crate::api::remove::version_del; use crate::api::remove::version_del;
use rocket::Route; use rocket::Route;
@ -22,7 +21,7 @@ pub fn build_api() -> Vec<Route> {
openapi_get_routes![ openapi_get_routes![
search, search,
package_list, package_list,
package_add, package_add_endpoint,
package_del, package_del,
version_del, version_del,
build_output, build_output,
@ -30,6 +29,6 @@ pub fn build_api() -> Vec<Route> {
stats, stats,
get_build, get_build,
get_package, get_package,
package_update package_update_endpoint
] ]
} }

View File

@ -1,21 +1,19 @@
use crate::api::list::ListPackageModel; use crate::api::list::ListPackageModel;
use crate::aur::aur::get_info_by_name;
use crate::builder::types::Action; use crate::builder::types::Action;
use crate::db::migration::{JoinType, Order}; use crate::db::migration::{JoinType, Order};
use crate::db::prelude::{Packages, Versions}; use crate::db::prelude::Packages;
use crate::db::{packages, versions}; use crate::db::{packages, versions};
use crate::repo::repo::remove_pkg; use crate::package::add::package_add;
use crate::package::delete::package_delete;
use crate::package::update::package_update;
use rocket::response::status::{BadRequest, NotFound}; use rocket::response::status::{BadRequest, NotFound};
use rocket::serde::json::Json; use rocket::serde::json::Json;
use rocket::serde::Deserialize; use rocket::serde::Deserialize;
use rocket::{get, post, State}; use rocket::{get, post, State};
use rocket_okapi::okapi::schemars; use rocket_okapi::okapi::schemars;
use rocket_okapi::{openapi, JsonSchema}; use rocket_okapi::{openapi, JsonSchema};
use sea_orm::{ use sea_orm::DatabaseConnection;
ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, QueryOrder, QuerySelect, use sea_orm::{ColumnTrait, EntityTrait, QueryFilter, QueryOrder, QuerySelect, RelationTrait};
RelationTrait, TransactionTrait,
};
use sea_orm::{DatabaseConnection, Set};
use tokio::sync::broadcast::Sender; use tokio::sync::broadcast::Sender;
#[derive(Deserialize, JsonSchema)] #[derive(Deserialize, JsonSchema)]
@ -26,75 +24,14 @@ pub struct AddBody {
#[openapi(tag = "Packages")] #[openapi(tag = "Packages")]
#[post("/packages/add", data = "<input>")] #[post("/packages/add", data = "<input>")]
pub async fn package_add( pub async fn package_add_endpoint(
db: &State<DatabaseConnection>, db: &State<DatabaseConnection>,
input: Json<AddBody>, input: Json<AddBody>,
tx: &State<Sender<Action>>, tx: &State<Sender<Action>>,
) -> Result<(), BadRequest<String>> { ) -> Result<(), BadRequest<String>> {
let txn = db package_add(db, input.name.clone(), tx)
.begin()
.await .await
.map_err(|e| BadRequest(Some(e.to_string())))?; .map_err(|e| BadRequest(Some(e.to_string())))
// remove leading and trailing whitespaces
let pkg_name = input.name.trim();
if let Some(..) = Packages::find()
.filter(packages::Column::Name.eq(pkg_name))
.one(&txn)
.await
.map_err(|e| BadRequest(Some(e.to_string())))?
{
return Err(BadRequest(Some("Package already exists".to_string())));
}
let pkg = get_info_by_name(pkg_name)
.await
.map_err(|_| BadRequest(Some("couldn't download package metadata".to_string())))?;
let mut new_package = packages::ActiveModel {
name: Set(pkg_name.to_string()),
status: Set(3),
latest_aur_version: Set(pkg.version.clone()),
..Default::default()
};
let mut new_package = new_package
.clone()
.save(&txn)
.await
.map_err(|e| BadRequest(Some(e.to_string())))?;
let new_version = versions::ActiveModel {
version: Set(pkg.version.clone()),
package_id: new_package.id.clone(),
..Default::default()
};
let new_version = new_version
.clone()
.save(&txn)
.await
.map_err(|e| BadRequest(Some(e.to_string())))?;
new_package.status = Set(3);
new_package.latest_version_id = Set(Some(new_version.id.clone().unwrap()));
new_package
.save(&txn)
.await
.map_err(|e| BadRequest(Some(e.to_string())))?;
let _ = tx.send(Action::Build(
pkg.name,
pkg.version,
pkg.url_path.unwrap(),
new_version,
));
txn.commit()
.await
.map_err(|e| BadRequest(Some(e.to_string())))?;
Ok(())
} }
#[derive(Deserialize, JsonSchema)] #[derive(Deserialize, JsonSchema)]
@ -105,64 +42,15 @@ pub struct UpdateBody {
#[openapi(tag = "Packages")] #[openapi(tag = "Packages")]
#[post("/packages/<id>/update", data = "<input>")] #[post("/packages/<id>/update", data = "<input>")]
pub async fn package_update( pub async fn package_update_endpoint(
db: &State<DatabaseConnection>, db: &State<DatabaseConnection>,
id: i32, id: i32,
input: Json<UpdateBody>, input: Json<UpdateBody>,
tx: &State<Sender<Action>>, tx: &State<Sender<Action>>,
) -> Result<(), BadRequest<String>> { ) -> Result<(), BadRequest<String>> {
let db = db as &DatabaseConnection; package_update(db, id, input.force, tx)
let mut pkg_model: packages::ActiveModel = Packages::find_by_id(id)
.one(db)
.await .await
.map_err(|e| BadRequest(Some(e.to_string())))? .map_err(|e| BadRequest(Some(e.to_string())))
.ok_or(BadRequest(Some("id not found".to_string())))?
.into();
let pkg = get_info_by_name(pkg_model.name.clone().unwrap().as_str())
.await
.map_err(|_| BadRequest(Some("couldn't download package metadata".to_string())))?;
let version_model = match Versions::find()
.filter(versions::Column::Version.eq(pkg.version.clone()))
.filter(versions::Column::PackageId.eq(pkg.id.clone()))
.one(db)
.await
.map_err(|e| BadRequest(Some(e.to_string())))?
{
None => {
let new_version = versions::ActiveModel {
version: Set(pkg.version.clone()),
package_id: Set(pkg_model.id.clone().unwrap()),
..Default::default()
};
new_version.save(db).await.expect("TODO: panic message")
}
Some(p) => {
// todo add check if this version was successfully built
// if not allow build
if input.force {
p.into()
} else {
return Err(BadRequest(Some("Version already existing".to_string())));
}
}
};
pkg_model.status = Set(3);
pkg_model.latest_version_id = Set(Some(version_model.id.clone().unwrap()));
pkg_model.save(db).await.expect("todo error message");
let _ = tx.send(Action::Build(
pkg.name,
pkg.version,
pkg.url_path.unwrap(),
version_model,
));
Ok(())
} }
#[openapi(tag = "Packages")] #[openapi(tag = "Packages")]
@ -170,7 +58,7 @@ pub async fn package_update(
pub async fn package_del(db: &State<DatabaseConnection>, id: i32) -> Result<(), String> { pub async fn package_del(db: &State<DatabaseConnection>, id: i32) -> Result<(), String> {
let db = db as &DatabaseConnection; let db = db as &DatabaseConnection;
remove_pkg(db, id).await.map_err(|e| e.to_string())?; package_delete(db, id).await.map_err(|e| e.to_string())?;
Ok(()) Ok(())
} }

View File

@ -2,6 +2,7 @@ mod api;
mod aur; mod aur;
mod builder; mod builder;
mod db; mod db;
mod package;
mod pkgbuild; mod pkgbuild;
mod repo; mod repo;
mod scheduler; mod scheduler;

View File

@ -0,0 +1,63 @@
use crate::aur::aur::get_info_by_name;
use crate::builder::types::Action;
use crate::db::prelude::Packages;
use crate::db::{packages, versions};
use anyhow::anyhow;
use rocket::State;
use sea_orm::ColumnTrait;
use sea_orm::QueryFilter;
use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, Set, TransactionTrait};
use tokio::sync::broadcast::Sender;
pub async fn package_add(
db: &DatabaseConnection,
pkg_name: String,
tx: &State<Sender<Action>>,
) -> anyhow::Result<()> {
let txn = db.begin().await?;
// remove leading and trailing whitespaces
let pkg_name = pkg_name.trim();
if let Some(..) = Packages::find()
.filter(packages::Column::Name.eq(pkg_name))
.one(&txn)
.await?
{
return Err(anyhow!("Package already exists"));
}
let pkg = get_info_by_name(pkg_name).await?;
let new_package = packages::ActiveModel {
name: Set(pkg_name.to_string()),
status: Set(3),
latest_aur_version: Set(pkg.version.clone()),
..Default::default()
};
let mut new_package = new_package.clone().save(&txn).await?;
let new_version = versions::ActiveModel {
version: Set(pkg.version.clone()),
package_id: new_package.id.clone(),
..Default::default()
};
let new_version = new_version.clone().save(&txn).await?;
new_package.status = Set(3);
new_package.latest_version_id = Set(Some(new_version.id.clone().unwrap()));
new_package.save(&txn).await?;
let _ = tx.send(Action::Build(
pkg.name,
pkg.version,
pkg.url_path.unwrap(),
new_version,
));
txn.commit().await?;
Ok(())
}

View File

@ -0,0 +1,45 @@
use crate::db::prelude::{Builds, Packages, Versions};
use crate::db::{builds, versions};
use crate::repo::repo::rem_ver;
use anyhow::anyhow;
use sea_orm::ColumnTrait;
use sea_orm::QueryFilter;
use sea_orm::{DatabaseConnection, EntityTrait, ModelTrait, TransactionTrait};
use std::fs;
pub async fn package_delete(db: &DatabaseConnection, pkg_id: i32) -> anyhow::Result<()> {
let txn = db.begin().await?;
let pkg = Packages::find_by_id(pkg_id)
.one(&txn)
.await?
.ok_or(anyhow!("id not found"))?;
// remove build dir if available
let _ = fs::remove_dir_all(format!("./builds/{}", pkg.name));
// remove package db entry
pkg.clone().delete(&txn).await?;
let versions = Versions::find()
.filter(versions::Column::PackageId.eq(pkg.id))
.all(&txn)
.await?;
for v in versions {
rem_ver(&txn, v).await?;
}
// remove corresponding builds
let builds = Builds::find()
.filter(builds::Column::PkgId.eq(pkg.id))
.all(&txn)
.await?;
for b in builds {
b.delete(&txn).await?;
}
txn.commit().await?;
Ok(())
}

View File

@ -0,0 +1,3 @@
pub mod add;
pub mod delete;
pub mod update;

View File

@ -0,0 +1,70 @@
use crate::aur::aur::get_info_by_name;
use crate::builder::types::Action;
use crate::db::prelude::{Packages, Versions};
use crate::db::{packages, versions};
use anyhow::anyhow;
use rocket::State;
use sea_orm::ColumnTrait;
use sea_orm::QueryFilter;
use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, Set, TransactionTrait};
use tokio::sync::broadcast::Sender;
pub async fn package_update(
db: &DatabaseConnection,
pkg_id: i32,
force: bool,
tx: &State<Sender<Action>>,
) -> anyhow::Result<()> {
let txn = db.begin().await?;
let mut pkg_model: packages::ActiveModel = Packages::find_by_id(pkg_id)
.one(&txn)
.await?
.ok_or(anyhow!("id not found"))?
.into();
let pkg = get_info_by_name(pkg_model.name.clone().unwrap().as_str())
.await
.map_err(|_| anyhow!("couldn't download package metadata".to_string()))?;
let version_model = match Versions::find()
.filter(versions::Column::Version.eq(pkg.version.clone()))
.filter(versions::Column::PackageId.eq(pkg.id.clone()))
.one(&txn)
.await?
{
None => {
let new_version = versions::ActiveModel {
version: Set(pkg.version.clone()),
package_id: Set(pkg_model.id.clone().unwrap()),
..Default::default()
};
new_version.save(&txn).await.expect("TODO: panic message")
}
Some(p) => {
// todo add check if this version was successfully built
// if not allow build
if force {
p.into()
} else {
return Err(anyhow!("Version already existing"));
}
}
};
pkg_model.status = Set(3);
pkg_model.latest_version_id = Set(Some(version_model.id.clone().unwrap()));
pkg_model.save(&txn).await.expect("todo error message");
let _ = tx.send(Action::Build(
pkg.name,
pkg.version,
pkg.url_path.unwrap(),
version_model,
));
txn.commit().await?;
Ok(())
}

View File

@ -1,7 +1,7 @@
use crate::aur::aur::download_pkgbuild; use crate::aur::aur::download_pkgbuild;
use crate::db::prelude::Packages;
use crate::db::prelude::Versions; use crate::db::prelude::Versions;
use crate::db::prelude::{Builds, Packages}; use crate::db::versions;
use crate::db::{builds, versions};
use crate::pkgbuild::build::build_pkgbuild; use crate::pkgbuild::build::build_pkgbuild;
use anyhow::anyhow; use anyhow::anyhow;
use sea_orm::{ use sea_orm::{
@ -48,43 +48,6 @@ pub async fn add_pkg(
Ok(firstpkgname) Ok(firstpkgname)
} }
pub async fn remove_pkg(db: &DatabaseConnection, pkg_id: i32) -> anyhow::Result<()> {
let txn = db.begin().await?;
let pkg = Packages::find_by_id(pkg_id)
.one(&txn)
.await?
.ok_or(anyhow!("id not found"))?;
// remove build dir if available
let _ = fs::remove_dir_all(format!("./builds/{}", pkg.name));
// remove package db entry
pkg.clone().delete(&txn).await?;
let versions = Versions::find()
.filter(versions::Column::PackageId.eq(pkg.id))
.all(&txn)
.await?;
for v in versions {
rem_ver(&txn, v).await?;
}
// remove corresponding builds
let builds = Builds::find()
.filter(builds::Column::PkgId.eq(pkg.id))
.all(&txn)
.await?;
for b in builds {
b.delete(&txn).await?;
}
txn.commit().await?;
Ok(())
}
pub async fn remove_version(db: &DatabaseConnection, version_id: i32) -> anyhow::Result<()> { pub async fn remove_version(db: &DatabaseConnection, version_id: i32) -> anyhow::Result<()> {
let txn = db.begin().await?; let txn = db.begin().await?;
@ -141,7 +104,10 @@ fn repo_remove(pkg_file_name: String) -> anyhow::Result<()> {
Ok(()) Ok(())
} }
async fn rem_ver(db: &DatabaseTransaction, version: versions::Model) -> anyhow::Result<()> { pub(crate) async fn rem_ver(
db: &DatabaseTransaction,
version: versions::Model,
) -> anyhow::Result<()> {
if let Some(filename) = version.file_name.clone() { if let Some(filename) = version.file_name.clone() {
// so repo-remove only supports passing a package name and removing the whole package // so repo-remove only supports passing a package name and removing the whole package
// it seems that repo-add removes an older version when called // it seems that repo-add removes an older version when called