Added files

This commit is contained in:
absurdo 2023-11-24 23:02:05 +01:00
commit fdc8852fcd
5 changed files with 219 additions and 0 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
*~
composer.lock
vendor/*
!vendor/phangoapp
settings/

16
composer.json Normal file
View file

@ -0,0 +1,16 @@
{
"repositories": [
{
"type": "path",
"url": "./vendor/phangoapp/leviathanutils"
}
],
"require": {
"phangoapp/leviathanutils": "dev-main",
"symfony/process": "^6.3"
},
"require-dev": {
"phpunit/phpunit": "^10.4"
}
}

View file

@ -0,0 +1,20 @@
{
"name": "phangoapp/leviathanutils",
"description": "A set of classes and methods for make shell operations in linux",
"require-dev": {
"phpunit/phpunit": "~4.8@dev"
},
"license": "GPL3",
"authors": [
{
"name": "Antonio de la Rosa",
"email": "antonio.delarosa@cuchulu.com"
}
],
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"PhangoApp\\LeviathanUtils\\": "src"
}
}
}

View file

@ -0,0 +1,157 @@
<?php
namespace PhangoApp\LeviathanUtils;
use Symfony\Component\Process\Process;
class Linux {
static public function shell_command($arr_command, $exit_if_error=0) {
$error=0;
/*$process=new Process($arr_command);
$process->run(function ($type, $buffer): bool {
if (Process::ERR === $type) {
echo $buffer;
$error=1;
} else {
echo $buffer;
}
});
if($error && $exit_if_error) {
exit(1);
}
echo 'Error'.$error;
return $error;*/
$cmd=implode(' ', $arr_command);
$a = popen($cmd, 'r');
while($b = fgets($a, 2048)) {
echo $b."\n";
//ob_flush();flush();
}
if(pclose($a)>0) {
echo 'Error executing command '.$cmd."\n";
exit(1);
}
}
static public function get_linux_distro() {
$file_distro=__DIR__.'/../../../../settings/distro.php';
$dir_distro=__DIR__.'/../../../../settings/';
if(!is_file($file_distro)) {
ob_start();
Linux::shell_command(['python', '-c', '"import distro;print(distro.id())"']);
$distro=trim(ob_get_contents());
ob_end_clean();
$distro_data="<?php\n\n\define('DISTRO_LINUX', '".$distro."');";
if(!is_dir($dir_distro)) {
if(!mkdir($dir_distro)) {
throw new \Exception('Sorry, cannot create settings directory');
}
}
if(!file_put_contents($file_distro, $distro_data)) {
throw new \Exception('Sorry, cannot create settings config file');
}
}
else {
if(!defined('DISTRO_LINUX')) {
include_once($file_distro);
}
$distro=DISTRO_LINUX;
}
return $distro;
}
static public function add_new_user() {
}
static public function delete_user() {
}
static public function install_package($package) {
$distro=Linux::get_linux_distro();
if(!isset($package[$distro])) {
echo 'Sorry, you don\'t have a package for this distro';
return false;
}
if($distro=='debian' || $distro=='ubuntu') {
Linux::shell_command('sudo DEBIAN_FRONTEND="noninteractive" apt-get install -y '.$package[$distro]);
}
else if($distro=='fedora' || $distro=='almalinux' || $distro=='rocky') {
//if call("sudo dnf install -y {}".format(package[linux_distro]), shell=True) > 0:
Linux::shell_command('sudo dnf install -y '.$package[$distro]);
}
else if($distro=='arch') {
//sudo pacman -S --noconfirm
Linux::shell_command('sudo pacman -S --noconfirm '.$package[$distro]);
}
}
static public function patch_file() {
}
}

View file

@ -0,0 +1,21 @@
<?php
use PHPUnit\Framework\TestCase;
include("vendor/autoload.php");
final class LinuxTest extends TestCase {
public function testDistro() {
$distro=PhangoApp\LeviathanUtils\Linux::get_linux_distro();
$this->assertEquals($distro, 'manjaro');
unlink('./settings/distro.php');
rmdir('./settings');
}
}