2023-04-30 12:42:19 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\doctor\item;
|
|
|
|
|
|
|
|
|
|
use SPC\builder\linux\SystemUtil;
|
|
|
|
|
use SPC\doctor\AsCheckItem;
|
|
|
|
|
use SPC\doctor\AsFixItem;
|
|
|
|
|
use SPC\doctor\CheckResult;
|
|
|
|
|
use SPC\exception\RuntimeException;
|
|
|
|
|
|
|
|
|
|
class LinuxMuslCheck
|
|
|
|
|
{
|
2023-08-20 19:51:45 +08:00
|
|
|
/** @noinspection PhpUnused */
|
2023-04-30 12:42:19 +08:00
|
|
|
#[AsCheckItem('if musl-libc is installed', limit_os: 'Linux')]
|
|
|
|
|
public function checkMusl(): ?CheckResult
|
|
|
|
|
{
|
2023-08-21 12:13:25 +02:00
|
|
|
$file = sprintf('/lib/ld-musl-%s.so.1', php_uname('m'));
|
2023-04-30 12:42:19 +08:00
|
|
|
if (file_exists($file)) {
|
|
|
|
|
return CheckResult::ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// non-exist, need to recognize distro
|
|
|
|
|
$distro = SystemUtil::getOSRelease();
|
|
|
|
|
return match ($distro['dist']) {
|
2023-09-18 12:52:12 +02:00
|
|
|
'ubuntu', 'alpine', 'debian', 'rhel', 'almalinux' => CheckResult::fail('musl-libc is not installed on your system', 'fix-musl', [$distro]),
|
2023-04-30 12:42:19 +08:00
|
|
|
default => CheckResult::fail('musl-libc is not installed on your system'),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
/**
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
* @noinspection PhpUnused
|
|
|
|
|
*/
|
2023-04-30 12:42:19 +08:00
|
|
|
#[AsFixItem('fix-musl')]
|
|
|
|
|
public function fixMusl(array $distro): bool
|
|
|
|
|
{
|
2023-09-30 17:50:37 +02:00
|
|
|
$rhel_install = 'wget https://musl.libc.org/releases/musl-1.2.4.tar.gz && tar -zxvf musl-1.2.4.tar.gz && \
|
2023-09-18 13:43:58 +02:00
|
|
|
rm -f musl-1.2.4.tar.gz && cd musl-1.2.4 &&
|
2023-09-30 17:50:37 +02:00
|
|
|
if [[ ! "$PATH" =~ (^|:)"/usr/local/musl/bin"(:|$) ]]; then echo "export PATH=/usr/local/musl/bin:$PATH" >> ~/.bash_profile
|
2023-09-18 13:43:58 +02:00
|
|
|
fi && \
|
2023-09-30 17:50:37 +02:00
|
|
|
./configure --enable-wrapper=gcc && \
|
|
|
|
|
make -j && make install && cd .. && rm -rf musl-1.2.4';
|
2023-04-30 12:42:19 +08:00
|
|
|
$install_cmd = match ($distro['dist']) {
|
2023-08-21 21:47:35 +02:00
|
|
|
'ubuntu', 'debian' => 'apt-get install musl musl-tools -y',
|
2023-04-30 12:42:19 +08:00
|
|
|
'alpine' => 'apk add musl musl-utils musl-dev',
|
2023-09-18 13:43:58 +02:00
|
|
|
'rhel' => $rhel_install,
|
|
|
|
|
'almalinux' => $rhel_install,
|
2023-09-18 12:52:12 +02:00
|
|
|
default => throw new RuntimeException('Current linux distro does not have an auto-install script for musl packages yet.'),
|
2023-04-30 12:42:19 +08:00
|
|
|
};
|
|
|
|
|
$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);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (RuntimeException) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|