142 lines
6.5 KiB
PowerShell
142 lines
6.5 KiB
PowerShell
# ============================================================
|
|
# IAAI scraper -- one-shot installer for Windows Server 2022 (4 vCPU / 4 GB)
|
|
#
|
|
# RUN AS ADMINISTRATOR in PowerShell:
|
|
# Set-ExecutionPolicy -Scope Process Bypass -Force
|
|
# iwr -useb https://gitea.millionmiles.ru/qananasikq/iaai-parser/raw/branch/main/scripts/vps_bootstrap.ps1 -OutFile $env:TEMP\b.ps1
|
|
# & $env:TEMP\b.ps1
|
|
#
|
|
# .env nado polozhit' v C:\iaai\.env DO zapuska (cherez RDP clipboard / notepad).
|
|
# ============================================================
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
function Step($m) { Write-Host "`n=== $m ===" -ForegroundColor Cyan }
|
|
|
|
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
|
throw "Run as Administrator (Start menu -> PowerShell -> Run as Administrator)"
|
|
}
|
|
|
|
# --- .env presence -------------------------------------------
|
|
$envSrc = "C:\iaai\.env"
|
|
if (-not (Test-Path $envSrc)) {
|
|
Write-Host ""
|
|
Write-Host "ERROR: $envSrc not found." -ForegroundColor Red
|
|
Write-Host "Create it via RDP clipboard:" -ForegroundColor Yellow
|
|
Write-Host " mkdir C:\iaai" -ForegroundColor Yellow
|
|
Write-Host " notepad C:\iaai\.env # paste contents, save as UTF-8 (no BOM)" -ForegroundColor Yellow
|
|
Write-Host "Then re-run this script." -ForegroundColor Yellow
|
|
exit 2
|
|
}
|
|
|
|
# --- 1. WSL2 + Ubuntu 22.04 ----------------------------------
|
|
Step "WSL state"
|
|
# Probe properly: 'wsl -l -q' on a system without any distro PRINTS HELP and exits non-zero.
|
|
# Use -v (verbose) and check exit code; only valid distro lines are accepted.
|
|
$ubuntu = $null
|
|
$probe = & wsl.exe -l -q 2>&1
|
|
$probeExit = $LASTEXITCODE
|
|
if ($probeExit -eq 0 -and $probe) {
|
|
$candidates = ($probe -join "`n") -split "`n" | ForEach-Object { ($_ -replace "[`0`r]","").Trim() } | Where-Object { $_ -and ($_ -notmatch '^[-= ]+$') -and ($_ -notmatch '(?i)usage|help|wsl\.exe|--install|argument|distribution') }
|
|
$ubuntu = $candidates | Where-Object { $_ -match '(?i)^Ubuntu' } | Select-Object -First 1
|
|
}
|
|
|
|
if (-not $ubuntu) {
|
|
Step "Installing WSL2 + Ubuntu-22.04 (REBOOT REQUIRED)"
|
|
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart | Out-Null
|
|
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart | Out-Null
|
|
wsl --set-default-version 2 2>$null | Out-Null
|
|
wsl --install -d Ubuntu-22.04 --no-launch
|
|
Write-Host ""
|
|
Write-Host "REBOOT NOW. After reboot:" -ForegroundColor Yellow
|
|
Write-Host " 1) Launch 'Ubuntu 22.04' from Start, set username 'iaai' and a password" -ForegroundColor Yellow
|
|
Write-Host " 2) Re-run this script (it will continue from here)" -ForegroundColor Yellow
|
|
Read-Host "Press Enter to reboot now (or Ctrl+C to abort)"
|
|
Restart-Computer -Force
|
|
exit 0
|
|
}
|
|
Write-Host "Ubuntu distro: $ubuntu"
|
|
|
|
# --- 2. .wslconfig (limit memory for 4 GB host) --------------
|
|
$wslConfig = "$env:USERPROFILE\.wslconfig"
|
|
$wslConfigDesired = "[wsl2]`nmemory=2500MB`nprocessors=3`nswap=1500MB`nlocalhostForwarding=true`n"
|
|
$existing = if (Test-Path $wslConfig) { Get-Content $wslConfig -Raw } else { "" }
|
|
if ($existing -ne $wslConfigDesired) {
|
|
Step "Writing $wslConfig"
|
|
Set-Content -Path $wslConfig -Value $wslConfigDesired -Encoding ASCII -NoNewline
|
|
Step "Restarting WSL"
|
|
wsl --shutdown
|
|
Start-Sleep -Seconds 5
|
|
}
|
|
|
|
# --- 3. Bootstrap script inside WSL --------------------------
|
|
$bash = @'
|
|
set -e
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
|
|
if ! sudo -n true 2>/dev/null; then
|
|
echo "[bootstrap] need passwordless sudo. run inside WSL once:"
|
|
echo " echo \"$USER ALL=(ALL) NOPASSWD:ALL\" | sudo tee /etc/sudoers.d/99-$USER"
|
|
exit 5
|
|
fi
|
|
|
|
if ! command -v docker >/dev/null 2>&1; then
|
|
echo "[bootstrap] installing docker..."
|
|
sudo apt-get update -y
|
|
sudo apt-get install -y ca-certificates curl gnupg git lsb-release
|
|
sudo install -m 0755 -d /etc/apt/keyrings
|
|
if [ ! -f /etc/apt/keyrings/docker.gpg ]; then
|
|
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
|
sudo chmod a+r /etc/apt/keyrings/docker.gpg
|
|
fi
|
|
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
|
|
sudo apt-get update -y
|
|
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
|
|
sudo usermod -aG docker "$USER"
|
|
fi
|
|
|
|
if ! sudo service docker status >/dev/null 2>&1; then
|
|
sudo service docker start
|
|
sleep 3
|
|
fi
|
|
|
|
if [ ! -d /opt/iaai-parser/.git ]; then
|
|
sudo mkdir -p /opt/iaai-parser
|
|
sudo chown -R "$USER:$USER" /opt/iaai-parser
|
|
git clone https://gitea.millionmiles.ru/qananasikq/iaai-parser.git /opt/iaai-parser
|
|
else
|
|
cd /opt/iaai-parser && git fetch --all --prune && git reset --hard origin/main
|
|
fi
|
|
|
|
cd /opt/iaai-parser
|
|
git log -1 --oneline
|
|
|
|
# Copy .env from Windows
|
|
cp /mnt/c/iaai/.env .env
|
|
sed -i '1s/^\xEF\xBB\xBF//' .env || true
|
|
sed -i 's/\r$//' .env || true
|
|
chmod 600 .env
|
|
echo "[bootstrap] .env size: $(wc -c < .env) bytes"
|
|
|
|
echo "[bootstrap] docker compose build (5-10 min)..."
|
|
sudo docker compose -f docker-compose.yml -f docker-compose.vps.yml build
|
|
echo "[bootstrap] docker compose up -d..."
|
|
sudo docker compose -f docker-compose.yml -f docker-compose.vps.yml up -d
|
|
sleep 5
|
|
sudo docker compose -f docker-compose.yml -f docker-compose.vps.yml ps
|
|
'@
|
|
|
|
$tmp = "$env:TEMP\wsl_bootstrap.sh"
|
|
[System.IO.File]::WriteAllText($tmp, ($bash -replace "`r`n","`n"), [System.Text.UTF8Encoding]::new($false))
|
|
|
|
Step "Running bootstrap inside WSL (5-15 min on first run)"
|
|
$wslPath = (wsl -d $ubuntu -- wslpath -u "$tmp").Trim()
|
|
wsl -d $ubuntu -- bash -c "cp '$wslPath' ~/bootstrap.sh && chmod +x ~/bootstrap.sh && ~/bootstrap.sh"
|
|
|
|
Step "DONE"
|
|
Write-Host ""
|
|
Write-Host "Useful commands:" -ForegroundColor Green
|
|
Write-Host " Status: wsl -d $ubuntu -- bash -c 'cd /opt/iaai-parser && sudo docker compose -f docker-compose.yml -f docker-compose.vps.yml ps'"
|
|
Write-Host " Logs: wsl -d $ubuntu -- bash -c 'cd /opt/iaai-parser && sudo docker compose -f docker-compose.yml -f docker-compose.vps.yml logs -f --tail=80 worker'"
|
|
Write-Host " Restart: wsl -d $ubuntu -- bash -c 'cd /opt/iaai-parser && sudo docker compose -f docker-compose.yml -f docker-compose.vps.yml restart'"
|
|
Write-Host " Update: wsl -d $ubuntu -- bash -c 'cd /opt/iaai-parser && git pull && sudo docker compose -f docker-compose.yml -f docker-compose.vps.yml up -d --build'"
|