fix: Install script variable interpolation in heredoc
Problem: PHP was interpolating bash variables like $INSTALL_DIR
as empty PHP variables. Fixed by using PHP variables in single
quotes: '{$installDir}' instead of "$INSTALL_DIR"
This commit is contained in:
parent
bce4c2e2d0
commit
3afd6f8366
|
|
@ -34,6 +34,7 @@ class AgentController extends Model
|
||||||
|
|
||||||
$apiUrl = 'https://mon.mirv.top/api/v1/metrics';
|
$apiUrl = 'https://mon.mirv.top/api/v1/metrics';
|
||||||
$agentDownloadUrl = 'https://mon.mirv.top/agent/agent.py?token=' . $token;
|
$agentDownloadUrl = 'https://mon.mirv.top/agent/agent.py?token=' . $token;
|
||||||
|
$installDir = '/opt/server-monitor-agent';
|
||||||
|
|
||||||
$script = <<<BASH
|
$script = <<<BASH
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
@ -48,7 +49,7 @@ set -e
|
||||||
TOKEN='{$token}'
|
TOKEN='{$token}'
|
||||||
API_URL='{$apiUrl}'
|
API_URL='{$apiUrl}'
|
||||||
AGENT_URL='{$agentDownloadUrl}'
|
AGENT_URL='{$agentDownloadUrl}'
|
||||||
INSTALL_DIR='/opt/server-monitor-agent'
|
INSTALL_DIR='{$installDir}'
|
||||||
|
|
||||||
echo '=============================================='
|
echo '=============================================='
|
||||||
echo ' Установка агента мониторинга серверов'
|
echo ' Установка агента мониторинга серверов'
|
||||||
|
|
@ -71,35 +72,35 @@ apt-get install -y -qq lm-sensors smartmontools 2>/dev/null || true
|
||||||
|
|
||||||
# Создаем директорию для агента
|
# Создаем директорию для агента
|
||||||
echo '[3/6] Создание директории агента...'
|
echo '[3/6] Создание директории агента...'
|
||||||
mkdir -p "$INSTALL_DIR"
|
mkdir -p '{$installDir}'
|
||||||
|
|
||||||
# Скачиваем агента
|
# Скачиваем агента
|
||||||
echo '[4/6] Скачивание агента...'
|
echo '[4/6] Скачивание агента...'
|
||||||
if ! curl -fsSL "$AGENT_URL" -o "$INSTALL_DIR/agent.py" 2>/dev/null; then
|
if ! curl -fsSL '{$agentDownloadUrl}' -o '{$installDir}/agent.py' 2>/dev/null; then
|
||||||
echo 'ERROR: Не удалось скачать агента. Проверьте токен и подключение к серверу.'
|
echo 'ERROR: Не удалось скачать агента. Проверьте токен и подключение к серверу.'
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! grep -q 'psutil' "$INSTALL_DIR/agent.py"; then
|
if ! grep -q 'psutil' '{$installDir}/agent.py'; then
|
||||||
echo 'ERROR: Скачанный файл не является агентом мониторинга.'
|
echo 'ERROR: Скачанный файл не является агентом мониторинга.'
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
chmod +x "$INSTALL_DIR/agent.py"
|
chmod +x '{$installDir}/agent.py'
|
||||||
|
|
||||||
# Создаем конфигурационный файл
|
# Создаем конфигурационный файл
|
||||||
echo '[5/6] Создание конфигурации...'
|
echo '[5/6] Создание конфигурации...'
|
||||||
cat > "$INSTALL_DIR/config.json" << CONFIG_EOF
|
cat > '{$installDir}/config.json' <<'CONFIG_EOF'
|
||||||
{
|
{
|
||||||
"token": "$TOKEN",
|
"token": "{$token}",
|
||||||
"api_url": "$API_URL",
|
"api_url": "{$apiUrl}",
|
||||||
"interval_seconds": 60
|
"interval_seconds": 60
|
||||||
}
|
}
|
||||||
CONFIG_EOF
|
CONFIG_EOF
|
||||||
|
|
||||||
# Создаем systemd сервис
|
# Создаем systemd сервис
|
||||||
echo '[6/6] Регистрация системной службы...'
|
echo '[6/6] Регистрация системной службы...'
|
||||||
cat > /etc/systemd/system/server-monitor-agent.service << SERVICE_EOF
|
cat > /etc/systemd/system/server-monitor-agent.service <<'SERVICE_EOF'
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=Server Monitor Agent
|
Description=Server Monitor Agent
|
||||||
After=network.target
|
After=network.target
|
||||||
|
|
@ -107,8 +108,8 @@ After=network.target
|
||||||
[Service]
|
[Service]
|
||||||
Type=simple
|
Type=simple
|
||||||
User=root
|
User=root
|
||||||
WorkingDirectory=$INSTALL_DIR
|
WorkingDirectory={$installDir}
|
||||||
ExecStart=/usr/bin/python3 $INSTALL_DIR/agent.py
|
ExecStart=/usr/bin/python3 {$installDir}/agent.py
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=10
|
RestartSec=10
|
||||||
|
|
||||||
|
|
@ -127,7 +128,7 @@ echo '=============================================='
|
||||||
echo ' Агент мониторинга успешно установлен!'
|
echo ' Агент мониторинга успешно установлен!'
|
||||||
echo '=============================================='
|
echo '=============================================='
|
||||||
echo ''
|
echo ''
|
||||||
echo 'Директория: $INSTALL_DIR'
|
echo "Директория: {$installDir}"
|
||||||
echo 'Логи: journalctl -u server-monitor-agent -f'
|
echo 'Логи: journalctl -u server-monitor-agent -f'
|
||||||
echo 'Статус: systemctl status server-monitor-agent'
|
echo 'Статус: systemctl status server-monitor-agent'
|
||||||
echo ''
|
echo ''
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue