-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13b3b44
commit 3694bfc
Showing
10 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- Remove the constraint for job kinds | ||
ALTER TABLE jobs DROP CONSTRAINT chk_job_kind; | ||
|
||
-- Add back the original constraint without the prune job type | ||
ALTER TABLE jobs ADD CONSTRAINT chk_job_kind CHECK (kind IN (0, 1, 2, 3, 4)); -- Excluding 5 (Prune) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-- Migration to add the "prune" job type to the jobs table | ||
|
||
ALTER TABLE jobs | ||
ADD CONSTRAINT chk_job_kind CHECK (kind IN (0, 1, 2, 3, 4)); -- Assuming 0-3 are existing job types, 4 is for "prune" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod build; | ||
pub mod delta; | ||
pub mod prune; | ||
pub mod repo; | ||
pub mod status; | ||
pub mod tokens; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use actix_web::{web, HttpRequest, HttpResponse}; | ||
use futures3::TryFutureExt; | ||
use serde::Deserialize; | ||
|
||
use crate::db::Db; | ||
use crate::tokens::{ClaimsScope, ClaimsValidator}; | ||
use crate::errors::ApiError; | ||
|
||
#[derive(Deserialize)] | ||
pub struct PruneArgs { | ||
repo: String, | ||
} | ||
|
||
pub fn handle_prune( | ||
args: web::Json<PruneArgs>, | ||
db: web::Data<Db>, | ||
req: HttpRequest, | ||
) -> impl futures::Future<Item = HttpResponse, Error = ApiError> { | ||
Box::pin(handle_prune_async(args, db, req)).compat() | ||
} | ||
|
||
async fn handle_prune_async( | ||
args: web::Json<PruneArgs>, | ||
db: web::Data<Db>, | ||
req: HttpRequest, | ||
) -> Result<HttpResponse, ApiError> { | ||
// Verify token has TokenManagement scope | ||
req.has_token_claims("", ClaimsScope::TokenManagement)?; | ||
|
||
// Create a new prune job | ||
let job = db.get_ref().start_prune_job(args.repo.clone()).await?; | ||
|
||
Ok(HttpResponse::Ok().json(job)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use crate::errors::{JobResult, JobError}; | ||
use crate::jobs::job_executor::JobExecutor; | ||
use crate::jobs::job_instance::JobInstance; | ||
use crate::models::Job; | ||
use diesel::pg::PgConnection; | ||
use serde::{Deserialize, Serialize}; | ||
use std::process::Command; | ||
use log::info; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct PruneJob {} | ||
|
||
pub struct PruneJobInstance { | ||
job: Job, | ||
} | ||
|
||
impl PruneJobInstance { | ||
pub fn new(job: Job) -> Box<dyn JobInstance> { | ||
match serde_json::from_str::<PruneJob>(&job.contents) { | ||
Ok(_) => Box::new(PruneJobInstance { job }), | ||
Err(e) => super::job_instance::InvalidJobInstance::new( | ||
job, | ||
JobError::new(&format!("Invalid prune job contents: {}", e)) | ||
), | ||
} | ||
} | ||
} | ||
|
||
impl JobInstance for PruneJobInstance { | ||
fn get_job_id(&self) -> i32 { | ||
self.job.id | ||
} | ||
|
||
fn handle_job( | ||
&mut self, | ||
executor: &JobExecutor, | ||
conn: &mut PgConnection, | ||
) -> JobResult<serde_json::Value> { | ||
info!("#{}: Handling Job Prune", &self.job.id); | ||
|
||
// Get repo config | ||
let config = &executor.config; | ||
let repo = self.job.repo.as_ref().ok_or_else(|| JobError::new("No repo specified"))?; | ||
let repoconfig = config | ||
.get_repoconfig(repo) | ||
.map_err(|_e| JobError::new(&format!("Can't find repo {}", repo)))?; | ||
|
||
let repo_path = repoconfig.get_abs_repo_path(); | ||
|
||
// First do a dry run | ||
job_log_and_info!(self.job.id, conn, "Running prune dry-run"); | ||
let mut cmd = Command::new("flatpak"); | ||
cmd.arg("build-update-repo") | ||
.arg("-v") | ||
.arg("--no-update-summary") | ||
.arg("--no-update-appstream") | ||
.arg("--prune-dry-run") | ||
.arg("--prune-depth=3") | ||
.arg(&repo_path); | ||
|
||
super::utils::do_command(cmd)?; | ||
|
||
// Then do the actual prune | ||
job_log_and_info!(self.job.id, conn, "Running actual prune"); | ||
let mut cmd = Command::new("flatpak"); | ||
cmd.arg("build-update-repo") | ||
.arg("-v") | ||
.arg("--no-update-summary") | ||
.arg("--no-update-appstream") | ||
.arg("--prune") | ||
.arg("--prune-depth=3") | ||
.arg(&repo_path); | ||
|
||
super::utils::do_command(cmd)?; | ||
|
||
Ok(serde_json::json!({})) | ||
} | ||
|
||
// Higher order than Publish/UpdateRepo to prevent them from running while prune is in queue | ||
fn order(&self) -> i32 { | ||
100 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters