From aefd7d5ea48c9c9f860073b5fa18301f688d88f9 Mon Sep 17 00:00:00 2001 From: keryax Date: Sun, 29 Dec 2024 03:01:51 +0300 Subject: [PATCH] initial commit --- composer.json | 5 ++ config.json | 5 ++ pending-bot.php | 94 ++++++++++++++++++++++++++++++++++++ rules/DEB.php | 18 +++++++ rules/Firmwares.php | 19 ++++++++ rules/LinuxArchives.php | 22 +++++++++ rules/MVN.php | 60 +++++++++++++++++++++++ rules/RPM.php | 54 +++++++++++++++++++++ rules/WindowsExecutables.php | 20 ++++++++ 9 files changed, 297 insertions(+) create mode 100644 composer.json create mode 100644 config.json create mode 100755 pending-bot.php create mode 100644 rules/DEB.php create mode 100644 rules/Firmwares.php create mode 100644 rules/LinuxArchives.php create mode 100644 rules/MVN.php create mode 100644 rules/RPM.php create mode 100644 rules/WindowsExecutables.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ad815ec --- /dev/null +++ b/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "ext-simplexml": "*" + } +} \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..d254356 --- /dev/null +++ b/config.json @@ -0,0 +1,5 @@ +{ + "pending": "/home/jenkins/cdn-pub-pending", + "dest": "/srv/repo", + "key": "/root/RPM-GPG-KEY-kirillius" +} \ No newline at end of file diff --git a/pending-bot.php b/pending-bot.php new file mode 100755 index 0000000..969e4d1 --- /dev/null +++ b/pending-bot.php @@ -0,0 +1,94 @@ +#!/usr/bin/php +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); diff --git a/rules/DEB.php b/rules/DEB.php new file mode 100644 index 0000000..a63034f --- /dev/null +++ b/rules/DEB.php @@ -0,0 +1,18 @@ +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); + } + } + + +} \ No newline at end of file diff --git a/rules/RPM.php b/rules/RPM.php new file mode 100644 index 0000000..35fec35 --- /dev/null +++ b/rules/RPM.php @@ -0,0 +1,54 @@ +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"); + } + } +} \ No newline at end of file diff --git a/rules/WindowsExecutables.php b/rules/WindowsExecutables.php new file mode 100644 index 0000000..30baa99 --- /dev/null +++ b/rules/WindowsExecutables.php @@ -0,0 +1,20 @@ +