initial commit

This commit is contained in:
keryax 2024-12-29 03:01:51 +03:00
commit aefd7d5ea4
9 changed files with 297 additions and 0 deletions

5
composer.json Normal file
View File

@ -0,0 +1,5 @@
{
"require": {
"ext-simplexml": "*"
}
}

5
config.json Normal file
View File

@ -0,0 +1,5 @@
{
"pending": "/home/jenkins/cdn-pub-pending",
"dest": "/srv/repo",
"key": "/root/RPM-GPG-KEY-kirillius"
}

94
pending-bot.php Executable file
View File

@ -0,0 +1,94 @@
#!/usr/bin/php
<?php
$runfile = "/var/run/cdn-pending-bot.run";
if(file_exists($runfile)){
die("Bot is running now!\n");
}
file_put_contents($runfile, time());
class Config{
public static $pending;
public static $dest;
public static $key;
public static function load(){
$config = @json_decode(@file_get_contents(__DIR__ . "/config.json"), true);
if ($config === null) throw new RuntimeException("Failed to read config");
$ref = new ReflectionClass(self::class);
foreach($ref->getStaticProperties() as $key=>$value){
if(!isset($config[$key])) throw new RuntimeException("Config doesnt contain field ".$key);
self::$$key = $config[$key];
}
}
}
Config::load();
interface IRule
{
public function getMatchingExtensions(): array;
public function processFile(string $file);
}
/**
* @var $rules IRule[]
*/
$rules = [];
/**
* @param string $ext
* @return IRule|null
*/
function get_rule_by_ext(string $ext)
{
global $rules;
foreach ($rules as $rule) {
foreach ($rule->getMatchingExtensions() as $re) {
if ($re === $ext) return $rule;
}
}
return null;
}
foreach (new IteratorIterator(new DirectoryIterator(__DIR__ . "/rules")) as $file) {
/**
* @var $file SplFileInfo
*/
if ($file->getExtension() == "php") {
include $file->getPathname();
}
}
foreach (get_declared_classes() as $class) {
try {
$ref = new ReflectionClass($class);
if (in_array(IRule::class, $ref->getInterfaceNames())) $rules[] = new $class;
} catch (ReflectionException $e) {
continue;
}
}
//List files
foreach (new IteratorIterator(new DirectoryIterator(Config::$pending)) as $file) {
clearstatcache();
if (!file_exists($file->getPathname())) continue;
/**
* @var $file SplFileInfo
*/
$ext = strtolower($file->getExtension());
if($ext == "part") continue;
$rule = get_rule_by_ext($ext);
if ($rule !== null) {
echo "Applying rule ".get_class($rule)." to file ".$file->getBasename()."\r\n";
$rule->processFile($file->getPathname());
}
if (file_exists($file->getPathname())) @unlink($file->getPathname());
}
unlink($runfile);

18
rules/DEB.php Normal file
View File

@ -0,0 +1,18 @@
<?php
class DEB implements IRule
{
public function getMatchingExtensions(): array
{
return ["deb"];
}
public function processFile(string $file)
{
echo shell_exec("reprepro -b ".escapeshellarg(Config::$dest . "/linux/deb")." includedeb yabba ".escapeshellarg($file));
}
}

19
rules/Firmwares.php Normal file
View File

@ -0,0 +1,19 @@
<?php
class Firmwares implements IRule
{
public function getMatchingExtensions(): array
{
return ["elf", "hex", "bin", "frmpkg"];
}
public function processFile(string $file)
{
$dest = Config::$dest."/firmware/" . basename($file);
if (file_exists($dest)) @unlink($dest);
echo "Copying to repository...";
echo (copy($file, $dest) ? "OK" : "FAIL") . "\r\n";
}
}

22
rules/LinuxArchives.php Normal file
View File

@ -0,0 +1,22 @@
<?php
class LinuxArchives implements IRule
{
public function getMatchingExtensions(): array
{
return ["tar", "gz", "tgz", "gzip", "bz2", "bzip2", "tbz2", "tb2", "tbz", "z", "taz", "lzma", "xz", "txz", "lzo", "tzo", "lz", "tlz", "zst", "tzst"];
}
public function processFile(string $file)
{
$dest = Config::$dest."/linux/general/" . basename($file);
if (file_exists($dest)) @unlink($dest);
echo "Copying to repository...";
echo (copy($file, $dest) ? "OK" : "FAIL") . "\r\n";
}
}

60
rules/MVN.php Normal file
View File

@ -0,0 +1,60 @@
<?php
class MVN implements IRule
{
public function getMatchingExtensions(): array
{
return ["pom", "jar"];
}
public function processFile(string $file)
{
$pom = "";
$jar = "";
$base = "";
$ext = substr($file, strrpos($file, ".") + 1);
if ($ext == "jar") {
$jar = $file;
$pom = str_replace(".jar", ".pom", $file);
$base = basename($file, ".jar");
} else {
$pom = $file;
$jar = str_replace(".pom", ".jar", $file);
$base = basename($file, ".pom");
}
if (!file_exists($pom)) {
echo "File not found " . $pom . "\n";
return;
}
if (!file_exists($jar)) {
echo "File not found " . $jar . "\n";
return;
}
$xml = simplexml_load_file($pom);
$groupId = $xml->groupId;
$artifactId = $xml->artifactId;
$version = $xml->version;
$path = str_replace(".", "/", $groupId) . "/" . $artifactId . "/" . $version;
$dest = Config::$dest . "/maven/" . $path;
if (!file_exists($dest)) mkdir($dest, 0777, true);
if (file_exists($dest . "/" . $base . ".jar")) unlink($dest . "/" . $base . ".jar");
if (file_exists($dest . "/" . $base . ".pom")) unlink($dest . "/" . $base . ".pom");
foreach ([$jar, $pom] as $f) {
copy($f, $dest . "/" . basename($f));
file_put_contents($dest . "/" . basename($f) . ".md5", md5_file($f));
file_put_contents($dest . "/" . basename($f) . ".sha1", sha1_file($f));
unlink($f);
}
}
}

54
rules/RPM.php Normal file
View File

@ -0,0 +1,54 @@
<?php
class RPM implements IRule
{
private $rebuildRepodata = false;
private $imported = false;
public function getMatchingExtensions(): array
{
return ["rpm"];
}
public function processFile(string $file)
{
/*
if(!$this->imported){
$this->imported = true;
echo "Importing signature...\r\n";
echo shell_exec("rpm --import " . escapeshellarg(Config::$key));
}
*/
$this->rebuildRepodata = true;
/*echo "Getting package arch...\r\n";
$arch = shell_exec("rpm -qip " . escapeshellarg($file) . " | grep Architecture:");
if (strpos($arch, "Architecture:") === false) {
echo "Failed to get package arch\r\n";
return;
} else {
$arch = trim(explode(":", $arch)[1]);
}*/
echo "Signing package...\r\n";
echo shell_exec("rpm --addsign " . escapeshellarg($file));
$dest = Config::$dest . "/linux/rpm/" . basename($file);
//if (!file_exists(dirname($dest))) mkdir(dirname($dest));
if (file_exists($dest)) @unlink($dest);
echo "Copying to repository...";
echo (copy($file, $dest) ? "OK" : "FAIL") . "\r\n";
}
public function __destruct()
{
if ($this->rebuildRepodata) {
echo "Rebuilding rpm repodata...\r\n";
$repoPath = Config::$dest . "/linux/rpm";
echo shell_exec("rm -rf " . escapeshellarg($repoPath . "/repodata"));
echo shell_exec("createrepo " . escapeshellarg($repoPath));
echo "Signing repository...\r\n";
echo shell_exec("gpg --detach-sign --armor " . escapeshellarg($repoPath . "/repodata/repomd.xml"));
copy(Config::$key, $repoPath . "/repodata/gpg-key");
}
}
}

View File

@ -0,0 +1,20 @@
<?php
class WindowsExecutables implements IRule
{
public function getMatchingExtensions(): array
{
return ["exe", "cmd", "bat", "msi", "zip"];
}
public function processFile(string $file)
{
$dest = Config::$dest."/windows/" . basename($file);
if (file_exists($dest)) @unlink($dest);
echo "Copying to repository...";
echo (copy($file, $dest) ? "OK" : "FAIL") . "\r\n";
}
}