add removal of failed downloads, add sh1 check of downloaded packages

This commit is contained in:
dmacias72 2016-09-09 23:19:56 -06:00
parent 0cf21cd263
commit e32eb33734

View File

@ -20,24 +20,24 @@ EOF;
$shortopts = "drquv"; $shortopts = "drquv";
$longopts = [ $longopts = [
"delete", "delete",
"download", "download",
"help", "help",
"quiet", "quiet",
"uninstall", "uninstall",
"verbose", "verbose",
"version" "version"
]; ];
$args = getopt($shortopts, $longopts); $args = getopt($shortopts, $longopts);
if (array_key_exists("help", $args)) { if (array_key_exists("help", $args)) {
echo $usage; echo $usage;
exit(1); exit(1);
} }
if (array_key_exists("version", $args)) { if (array_key_exists("version", $args)) {
echo "Package Manager Version: 1.2\n"; echo "Package Manager Version: 1.2\n";
exit(1); exit(1);
} }
$argd = (array_key_exists("d", $args) || array_key_exists("download", $args)); $argd = (array_key_exists("d", $args) || array_key_exists("download", $args));
@ -52,77 +52,84 @@ require_once '/usr/local/emhttp/plugins/NerdPack/include/DownloadHelpers.php';
logger('Processing Packages...'); logger('Processing Packages...');
foreach ($pkg_cfg as $pkg_name => $pkg_pref) { // get preferences for each package foreach ($pkg_cfg as $pkg_name => $pkg_pref) { // get preferences for each package
$pkg_cmd = ''; $pkg_cmd = '';
$pkg_name = str_replace('_', '.', $pkg_name); // replace "_" with "." in package names $pkg_name = str_replace('_', '.', $pkg_name); // replace "_" with "." in package names
$pkg_pattern = '/^'.$pkg_name.'.*/'; // search pattern for packages $pkg_pattern = '/^'.$pkg_name.'.*/'; // search pattern for packages
$pkg_install_status = preg_grep($pkg_pattern, $pkgs_installed); // check install status $pkg_install_status = preg_grep($pkg_pattern, $pkgs_installed); // check install status
$pkg_download_status = preg_grep($pkg_pattern, $pkgs_downloaded); // check package download status $pkg_download_status = preg_grep($pkg_pattern, $pkgs_downloaded); // check package download status
$pkg_online_status = preg_grep($pkg_pattern, $pkgs_github_array);
//check if plugin is dependent on package $pkg_matches = array_filter($pkgs_github_array, function($a) use ($pkg_pattern) {
$plugins = []; return preg_grep($pkg_pattern, $a);
exec("cd /boot/config/plugins ; find *.plg | xargs grep '$pkg_name' -sl",$plugins); });
if ($plugins){
$plg_msg = "$pkg_name used by plugin: ";
foreach ($plugins as $plugin){
$plg_msg .= pathinfo($plugin, PATHINFO_FILENAME).", ";
}
logger(substr($plg_msg, 0, -2));
}
if ($pkg_pref == 'yes') { $pkg_gitname = array_values($pkg_matches)[0]['name'];
// if executing from the wegui check status and download if necessary $pkg_file = $pkg_path.$pkg_gitname;
if ($argd) { $pkg_url = array_values($pkg_matches)[0]['download_url'];
if (!$pkg_download_status) { $pkg_sha1 = array_values($pkg_matches)[0]['sha'];
$pkg_online_status = preg_grep($pkg_pattern, $pkgs_github_array); //check if plugin is dependent on package
$plugins = [];
exec("cd /boot/config/plugins ; find *.plg | xargs grep '$pkg_name' -sl",$plugins);
if ($plugins){
$plg_msg = "$pkg_name used by plugin: ";
foreach ($plugins as $plugin){
$plg_msg .= pathinfo($plugin, PATHINFO_FILENAME).", ";
}
logger(substr($plg_msg, 0, -2));
}
$pkg_matches = array_filter($pkgs_github_array, function($a) use ($pkg_pattern) { if ($pkg_pref == 'yes') { // if package is selected to be installed
return preg_grep($pkg_pattern, $a); // if executing from the wegui check status and download if necessary
}); if ($argd) {
if (!$pkg_download_status) { //if package is not downloaded
$pkg_gitname = array_values($pkg_matches)[0]['name']; logger('Downloading '.$pkg_gitname.' package...', $argq);
$pkg_file = $pkg_path.$pkg_gitname;
$pkg_url = array_values($pkg_matches)[0]['download_url'];
$pkg_sha1 = array_values($pkg_matches)[0]['sha'];
logger('Downloading '.$pkg_gitname.' package...', $argq); get_file_from_url($pkg_file, $pkg_url);
get_file_from_url($pkg_file, $pkg_url); if (file_check_sha1($pkg_file, $pkg_sha1)){
logger($pkg_gitname.' package download sucessful!', $argq);
if (file_check_sha1($pkg_file, $pkg_sha1)) }else{
logger($pkg_gitname.' package downloaded sucessfully!', $argq); unlink($pkg_file);
else{ logger($pkg_gitname.' package download failed!', $argq);
if(is_file($pkg_file)) }
unlink($pkg_file); }
logger($pkg_gitname.' package download failed!', $argq); }
} if (!$pkg_install_status) { // if package is not installed
} $pkg_msg = 'Installing';
} $pkg_cmd = 'upgradepkg --install-new '.$pkg_path.$pkg_name.'* 2>&1';
if (!$pkg_install_status) { }
$pkg_msg = 'Installing'; } else if ($pkg_pref == 'no' && $pkg_install_status && $argu) {
$pkg_cmd = 'upgradepkg --install-new '.$pkg_path.$pkg_name.'* 2>&1'; if($plugins){
} logger("$pkg_name in use by another plugin, not uninstalled.",$argq);
} else if ($pkg_pref == 'no' && $pkg_install_status && $argu) { }else{
if($plugins){ $pkg_msg = 'Uninstalling';
logger("Unable to uninstall $pkg_name",$argq); $pkg_cmd = 'removepkg '.$pkg_path.$pkg_name.'* 2>&1';
}else{ if ($argr) {
$pkg_msg = 'Uninstalling'; $pkg_cmd .= '; rm '.$pkg_path.$pkg_name.'* 2>&1';
$pkg_cmd = 'removepkg '.$pkg_path.$pkg_name.'* 2>&1'; $pkg_msg .= ' and deleting';
if ($argr) { }
$pkg_cmd .= '; rm '.$pkg_path.$pkg_name.'* 2>&1'; }
$pkg_msg .= ' and deleting'; }
} if (!empty($pkg_cmd)) {
} logger("$pkg_msg $pkg_name package...");
} shell_exec($pkg_cmd);
if (!empty($pkg_cmd)) { //$output = shell_exec($pkg_cmd);
logger("$pkg_msg $pkg_name package..."); //logger($output);
shell_exec($pkg_cmd); }else{
//$output = shell_exec($pkg_cmd); if ($pkg_pref == 'yes'){
//logger($output); if (file_exists($pkg_file)){
} else { if (file_check_sha1($pkg_file, $pkg_sha1))
if ($pkg_pref == 'yes') logger($pkg_gitname.' package up to date, checksum ok.', $argq);
logger("$pkg_name package up to date", $argq); else{
} unlink($pkg_file);
logger($pkg_gitname.' package checksum failed! package deleted.', $argq);
}
}else
logger($pkg_gitname.' package missing!', $argq);
}
}
} }
logger('All packages processed...'); logger('All packages processed...');