initial commit of nerpack plugin
This commit is contained in:
111
source/NerdPack/usr/local/emhttp/plugins/NerdPack/scripts/packagemanager
Executable file
111
source/NerdPack/usr/local/emhttp/plugins/NerdPack/scripts/packagemanager
Executable file
@ -0,0 +1,111 @@
|
||||
#!/usr/bin/php -q
|
||||
<?php
|
||||
include("/usr/local/emhttp/plugins/NerdPack/include/DownloadHelpers.php");
|
||||
|
||||
function logger($message) {
|
||||
echo "$message\n";
|
||||
//shell_exec( "echo $message | logger -tnerdpack");
|
||||
}
|
||||
|
||||
$config_file = "/boot/config/plugins/NerdPack/NerdPack.cfg";
|
||||
|
||||
$pkg_path ="/boot/config/plugins/NerdPack/packages/";
|
||||
|
||||
$pkg_cfg = (is_file($config_file)) ? parse_ini_file($config_file) : array(); // get package configs
|
||||
|
||||
$pkgs_downloaded = array_diff( scandir($pkg_path, 1), array(".", "..") ); // get array of packages
|
||||
|
||||
$pkgs_installed = array_diff( scandir("/var/log/packages", 1), array(".", "..") );
|
||||
|
||||
$pkgs_json = "/boot/config/plugins/NerdPack/packages.json";
|
||||
$pkgs_file = (is_file($pkgs_json)) ? file_get_contents($pkgs_json) : array();
|
||||
|
||||
$pkgs_github = json_decode($pkgs_file, true);
|
||||
|
||||
logger("Processing Packages...");
|
||||
|
||||
foreach($pkg_cfg as $pkg_name => $pkg_pref) { //get preferences for each package
|
||||
|
||||
$pkg_cmd = '';
|
||||
|
||||
$pkg_name = str_replace("_", ".", $pkg_name); // replace "_" with "." in package names
|
||||
|
||||
$pkg_pattern = "/^".$pkg_name.".*/"; // search patter for packages
|
||||
|
||||
$pkg_install_status = preg_grep($pkg_pattern, $pkgs_installed); // check install status
|
||||
|
||||
if($pkg_pref == "yes" && !$pkg_install_status){
|
||||
|
||||
//if executing from the wegui check status and download if necessary
|
||||
if ($argv[1] == "webgui"){
|
||||
|
||||
if(!preg_grep($pkg_pattern , $pkgs_downloaded)) {
|
||||
|
||||
$pkg_online_status = preg_grep($pkg_pattern , $pkgs_github);
|
||||
|
||||
$pkg_matches = array_filter($pkgs_github, function($a) use($pkg_pattern ) {
|
||||
return preg_grep($pkg_pattern , $a);
|
||||
});
|
||||
|
||||
$pkg_gitname = array_values($pkg_matches)[0]["name"];
|
||||
|
||||
$pkg_file = $pkg_path.$pkg_gitname;
|
||||
|
||||
$pkg_url = array_values($pkg_matches)[0]["download_url"];
|
||||
|
||||
$pkg_sha = array_values($pkg_matches)[0]["sha"];
|
||||
|
||||
$pkg_size = array_values($pkg_matches)[0]["size"];
|
||||
|
||||
logger("Downloading $pkg_gitname package...");
|
||||
get_file_from_url($pkg_file, $pkg_url);
|
||||
|
||||
/*$pkg_msg = "$pkg_name package download failed.";
|
||||
if (file_put_contents($pkg_new, fopen($pkg_git.$pkg_name, 'r')));{
|
||||
$pkg_size = filesize($pkg_new);
|
||||
$pkg_handle = fopen($pkg_new, "rb");
|
||||
$pkg_contents = fread($pkg_handle, $pkg_size);
|
||||
fclose($pkg_handle);
|
||||
$pkg_str = "blob ".$pkg_size."\0".$pkg_contents;
|
||||
$pkg_sha = sha1($pkg_str);
|
||||
if($pkg_sha == $_POST["pkgsha"]) {
|
||||
$pkg_msg = "$pkg_name package downloaded.";
|
||||
if (is_file($pkg_old))
|
||||
unlink($pkg_old);
|
||||
if($pkg_pref == "yes") {
|
||||
$pkg_msg = "$pkg_name package updated.";
|
||||
}
|
||||
}else {
|
||||
if (is_file($pkg_new))
|
||||
unlink($pkg_new);
|
||||
}
|
||||
}
|
||||
echo json_encode($pkg_msg);
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
$pkg_msg = "Installing";
|
||||
$pkg_cmd = "upgradepkg --install-new ";
|
||||
|
||||
}elseif($pkg_pref == "no" && $pkg_install_status && $argv[2] == "remove"){
|
||||
|
||||
$pkg_msg = "Removing";
|
||||
$pkg_cmd = "removepkg ";
|
||||
|
||||
}
|
||||
if(!empty($pkg_cmd)){
|
||||
logger("$pkg_msg $pkg_name package...");
|
||||
$pkg_cmd = $pkg_cmd.$pkg_path.$pkg_name."*";
|
||||
|
||||
exec($pkg_cmd, $output, $return_var);
|
||||
|
||||
for ($i = 0; $i < sizeof($output); $i++) {
|
||||
logger($output[$i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger("All packages processed");
|
||||
?>
|
Reference in New Issue
Block a user