♻️ refactor
This commit is contained in:
parent
e5135dc9e4
commit
caf4165f13
4 changed files with 70 additions and 61 deletions
|
@ -22,6 +22,6 @@ Include = /etc/pacman.d/mirrorlist
|
|||
Server = https://pac.hydrar.de/pkg/$repo/$arch
|
||||
SigLevel = Optional TrustAll
|
||||
|
||||
#[our]
|
||||
#SigLevel = Never
|
||||
#Server = https://pac.hydrar.de/pkg/$repo/$arch
|
||||
[our]
|
||||
SigLevel = Never
|
||||
Server = https://pac.hydrar.de/pkg/$repo/$arch
|
||||
|
|
|
@ -5,7 +5,7 @@ use based::get_pg;
|
|||
use crate::{
|
||||
git::get_commit_hash,
|
||||
pacco_push,
|
||||
package::{PackageIndex, find_pkg, get_pkgver},
|
||||
package::{PackageIndex, find_pkgs, get_pkgver},
|
||||
};
|
||||
|
||||
pub struct BuildEnv(String, bool);
|
||||
|
@ -85,7 +85,14 @@ impl BuildEnv {
|
|||
.expect("Failed to run systemd-nspawn container");
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
||||
(output.status.success(), stdout)
|
||||
let success = output.status.success();
|
||||
|
||||
if !success {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
||||
log::error!("Error running command: {stderr}\nStdout: {stdout}");
|
||||
}
|
||||
|
||||
(success, stdout)
|
||||
}
|
||||
|
||||
pub fn cleanup(&self) {
|
||||
|
@ -106,7 +113,7 @@ impl Drop for BuildEnv {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn build(pkg: &PackageIndex) -> (String, Option<(String, Vec<u8>)>) {
|
||||
pub async fn build(pkg: &PackageIndex) -> (String, Vec<(String, Vec<u8>)>) {
|
||||
let base_env = BuildEnv::new();
|
||||
|
||||
let commit = get_commit_hash(pkg.path().to_str().unwrap());
|
||||
|
@ -127,7 +134,7 @@ pub async fn build(pkg: &PackageIndex) -> (String, Option<(String, Vec<u8>)>) {
|
|||
);
|
||||
|
||||
let (success, stdout) = base_env.run_command(
|
||||
r#"cd /build;pacman -Syu --noconfirm;useradd build;echo "ALL ALL=(ALL) NOPASSWD: ALL"|tee -a /etc/sudoers;chown -R build /build;su -c "makepkg -c -C -s --noconfirm" build"#);
|
||||
r#"cd /build;pacman -Syu --noconfirm;useradd -m build;echo "ALL ALL=(ALL) NOPASSWD: ALL"|tee -a /etc/sudoers;chown -R build /build;su -c "makepkg -c -C -s --noconfirm --skippgpcheck" build"#);
|
||||
|
||||
if success {
|
||||
sqlx::query(
|
||||
|
@ -141,40 +148,42 @@ pub async fn build(pkg: &PackageIndex) -> (String, Option<(String, Vec<u8>)>) {
|
|||
.await
|
||||
.unwrap();
|
||||
|
||||
let package = find_pkg(&base_env.build_dir()).unwrap();
|
||||
let packages = find_pkgs(&base_env.build_dir());
|
||||
|
||||
log::info!(
|
||||
"Successfully built {} / {} @ {}{}",
|
||||
pkg.repo,
|
||||
pkg.pkg,
|
||||
pkgver.as_ref().unwrap_or(&String::new()),
|
||||
if let Some(c) = &commit {
|
||||
format!(" [#{c}]")
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
);
|
||||
for package in &packages {
|
||||
log::info!(
|
||||
"Successfully built {} / {} @ {}{}",
|
||||
pkg.repo,
|
||||
pkg.pkg,
|
||||
pkgver.as_ref().unwrap_or(&String::new()),
|
||||
if let Some(c) = &commit {
|
||||
format!(" [#{c}]")
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
);
|
||||
|
||||
std::fs::create_dir_all(std::path::Path::new("./packages").join(&pkg.repo)).unwrap();
|
||||
std::fs::write(
|
||||
std::path::Path::new("./packages")
|
||||
.join(&pkg.repo)
|
||||
.join(&package.0),
|
||||
&package.1,
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::create_dir_all(std::path::Path::new("./packages").join(&pkg.repo)).unwrap();
|
||||
std::fs::write(
|
||||
std::path::Path::new("./packages")
|
||||
.join(&pkg.repo)
|
||||
.join(&package.0),
|
||||
&package.1,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
pacco_push(
|
||||
std::path::Path::new("./packages")
|
||||
.join(&pkg.repo)
|
||||
.join(&package.0)
|
||||
.to_str()
|
||||
.unwrap(),
|
||||
&pkg.repo,
|
||||
)
|
||||
.await;
|
||||
pacco_push(
|
||||
std::path::Path::new("./packages")
|
||||
.join(&pkg.repo)
|
||||
.join(&package.0)
|
||||
.to_str()
|
||||
.unwrap(),
|
||||
&pkg.repo,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
(stdout, Some(package))
|
||||
(stdout, packages)
|
||||
} else {
|
||||
log::error!(
|
||||
"Error building {} / {} @ {}{}",
|
||||
|
@ -187,6 +196,6 @@ pub async fn build(pkg: &PackageIndex) -> (String, Option<(String, Vec<u8>)>) {
|
|||
String::new()
|
||||
}
|
||||
);
|
||||
(stdout, None)
|
||||
(stdout, Vec::new())
|
||||
}
|
||||
}
|
||||
|
|
32
src/main.rs
32
src/main.rs
|
@ -2,9 +2,7 @@ use based::get_pg;
|
|||
use comrade::defer;
|
||||
use comrade::service::ServiceManager;
|
||||
use package::{rebuild_pkgs, reindex_pkg};
|
||||
use reqwest::Client;
|
||||
use reqwest::multipart::{Form, Part};
|
||||
use std::io::Read;
|
||||
use std::process::Command;
|
||||
|
||||
pub mod builder;
|
||||
pub mod git;
|
||||
|
@ -30,23 +28,21 @@ async fn pacco_push(pkg_path: &str, repo: &str) {
|
|||
log::info!("Pushing package to pacco at {pacco}");
|
||||
let upload_url = format!("{}/pkg/{repo}/upload", pacco);
|
||||
|
||||
let client = Client::new();
|
||||
let token = std::env::var("PACCO_TOKEN").unwrap();
|
||||
|
||||
let mut pkg_file = std::fs::File::open(pkg_path).unwrap();
|
||||
let mut pkg_buffer = Vec::new();
|
||||
pkg_file.read_to_end(&mut pkg_buffer).unwrap();
|
||||
let pkg_part = Part::bytes(pkg_buffer).file_name(pkg_path.to_string());
|
||||
let output = Command::new("curl")
|
||||
.arg("-X")
|
||||
.arg("POST")
|
||||
.arg("-F")
|
||||
.arg(format!("pkg=@{}", pkg_path))
|
||||
.arg("-H")
|
||||
.arg(format!("Token: {token}"))
|
||||
.arg(upload_url)
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
let form = Form::new().part("pkg", pkg_part);
|
||||
|
||||
if let Ok(_) = client
|
||||
.post(upload_url)
|
||||
.multipart(form)
|
||||
.header("Token", std::env::var("PACCO_TOKEN").unwrap())
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
log::info!("Successfully pushed {} to pacco", pkg_path);
|
||||
if !output.status.success() {
|
||||
log::error!("Failed to push to pacco");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -96,12 +96,13 @@ pub async fn rebuild_pkgs() {
|
|||
}
|
||||
|
||||
pub fn get_pkgver(repo: &str) -> Option<String> {
|
||||
let base_env = BuildEnv::new_from("./build/srcinfo");
|
||||
// let base_env = BuildEnv::new_from("./build/srcinfo");
|
||||
let base_env = BuildEnv::new();
|
||||
|
||||
base_env.copy_build_env_dir(repo);
|
||||
|
||||
let (success, out) = base_env.run_command(
|
||||
r#"cd /build;useradd build;echo "ALL ALL=(ALL) NOPASSWD: ALL"|tee -a /etc/sudoers;chown -R build /build;su -c "makepkg --printsrcinfo" build"#);
|
||||
r#"cd /build;useradd -m build;echo "ALL ALL=(ALL) NOPASSWD: ALL"|tee -a /etc/sudoers;chown -R build /build;su -c "makepkg --printsrcinfo" build"#);
|
||||
|
||||
if success {
|
||||
for line in out.lines() {
|
||||
|
@ -115,7 +116,9 @@ pub fn get_pkgver(repo: &str) -> Option<String> {
|
|||
None
|
||||
}
|
||||
|
||||
pub fn find_pkg(dir: &str) -> Option<(String, Vec<u8>)> {
|
||||
pub fn find_pkgs(dir: &str) -> Vec<(String, Vec<u8>)> {
|
||||
let mut pkgs = Vec::new();
|
||||
|
||||
let path = Path::new(dir);
|
||||
|
||||
if let Ok(entries) = fs::read_dir(path) {
|
||||
|
@ -129,7 +132,7 @@ pub fn find_pkg(dir: &str) -> Option<(String, Vec<u8>)> {
|
|||
Ok(mut file) => {
|
||||
let mut content = Vec::new();
|
||||
if file.read_to_end(&mut content).is_ok() {
|
||||
return Some((file_name.to_string(), content));
|
||||
pkgs.push((file_name.to_string(), content));
|
||||
}
|
||||
}
|
||||
Err(_) => continue,
|
||||
|
@ -140,7 +143,8 @@ pub fn find_pkg(dir: &str) -> Option<(String, Vec<u8>)> {
|
|||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
|
||||
pkgs
|
||||
}
|
||||
|
||||
pub async fn reindex_pkg(repo_dir: &Path) {
|
||||
|
|
Loading…
Add table
Reference in a new issue