2023-07-17 20:26:48 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\doctor\item;
|
|
|
|
|
|
|
|
|
|
use SPC\builder\linux\SystemUtil;
|
|
|
|
|
use SPC\builder\traits\UnixSystemUtilTrait;
|
|
|
|
|
use SPC\doctor\AsCheckItem;
|
|
|
|
|
use SPC\doctor\AsFixItem;
|
|
|
|
|
use SPC\doctor\CheckResult;
|
|
|
|
|
use SPC\exception\RuntimeException;
|
|
|
|
|
|
|
|
|
|
class LinuxToolCheckList
|
|
|
|
|
{
|
|
|
|
|
use UnixSystemUtilTrait;
|
|
|
|
|
|
2023-07-17 21:11:06 +08:00
|
|
|
public const TOOLS_ALPINE = [
|
|
|
|
|
'make', 'bison', 'flex',
|
|
|
|
|
'git', 'autoconf', 'automake',
|
|
|
|
|
'tar', 'unzip', 'gzip',
|
|
|
|
|
'bzip2', 'cmake', 'gcc',
|
|
|
|
|
'g++', 'patch',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
public const TOOLS_DEBIAN = [
|
|
|
|
|
'make', 'bison', 'flex',
|
|
|
|
|
'git', 'autoconf', 'automake',
|
|
|
|
|
'tar', 'unzip', 'gzip',
|
|
|
|
|
'bzip2', 'cmake', 'patch',
|
|
|
|
|
];
|
|
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
/** @noinspection PhpUnused */
|
2023-07-17 20:26:48 +08:00
|
|
|
#[AsCheckItem('if necessary tools are installed', limit_os: 'Linux')]
|
|
|
|
|
public function checkCliTools(): ?CheckResult
|
|
|
|
|
{
|
|
|
|
|
$distro = SystemUtil::getOSRelease();
|
|
|
|
|
|
|
|
|
|
$required = match ($distro['dist']) {
|
2023-07-17 21:11:06 +08:00
|
|
|
'alpine' => self::TOOLS_ALPINE,
|
|
|
|
|
default => self::TOOLS_DEBIAN,
|
2023-07-17 20:26:48 +08:00
|
|
|
};
|
|
|
|
|
$missing = [];
|
|
|
|
|
foreach ($required as $cmd) {
|
|
|
|
|
if ($this->findCommand($cmd) === null) {
|
|
|
|
|
$missing[] = $cmd;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!empty($missing)) {
|
|
|
|
|
return match ($distro['dist']) {
|
|
|
|
|
'ubuntu', 'alpine', 'debian' => CheckResult::fail(implode(', ', $missing) . ' not installed on your system', 'install-linux-tools', [$distro, $missing]),
|
|
|
|
|
default => CheckResult::fail(implode(', ', $missing) . ' not installed on your system'),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return CheckResult::ok();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
/** @noinspection PhpUnused */
|
2023-07-17 20:58:48 +08:00
|
|
|
#[AsCheckItem('if necessary packages are installed', limit_os: 'Linux')]
|
|
|
|
|
public function checkSystemOSPackages(): ?CheckResult
|
|
|
|
|
{
|
|
|
|
|
$distro = SystemUtil::getOSRelease();
|
|
|
|
|
if ($distro['dist'] === 'alpine') {
|
|
|
|
|
// check linux-headers installation
|
|
|
|
|
if (!file_exists('/usr/include/linux/mman.h')) {
|
2023-07-17 20:59:59 +08:00
|
|
|
return CheckResult::fail('linux-headers not installed on your system', 'install-linux-tools', [$distro, ['linux-headers']]);
|
2023-07-17 20:58:48 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return CheckResult::ok();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
/**
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
* @noinspection PhpUnused
|
|
|
|
|
*/
|
2023-07-17 20:26:48 +08:00
|
|
|
#[AsFixItem('install-linux-tools')]
|
|
|
|
|
public function fixBuildTools(array $distro, array $missing): bool
|
|
|
|
|
{
|
|
|
|
|
$install_cmd = match ($distro['dist']) {
|
2023-08-21 21:47:35 +02:00
|
|
|
'ubuntu', 'debian' => 'apt-get install -y',
|
2023-07-17 20:26:48 +08:00
|
|
|
'alpine' => 'apk add',
|
|
|
|
|
default => throw new RuntimeException('Current linux distro is not supported for auto-install musl packages'),
|
|
|
|
|
};
|
|
|
|
|
$prefix = '';
|
|
|
|
|
if (get_current_user() !== 'root') {
|
|
|
|
|
$prefix = 'sudo ';
|
|
|
|
|
logger()->warning('Current user is not root, using sudo for running command');
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
shell(true)->exec($prefix . $install_cmd . ' ' . implode(' ', $missing));
|
|
|
|
|
} catch (RuntimeException) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|