Allow enter username and password for WSL account

This commit is contained in:
2026-11-16 22:59:54 +07:00
parent 62100913ae
commit e74d2eff3c
5 changed files with 129 additions and 88 deletions
-79
View File
@@ -1,79 +0,0 @@
param(
[string]$GiteaUrl = "https://gitea.digitoolsolutions.synology.me",
[string]$Owner = "lamtruong28",
[string]$Repo = "mini-pc-setup",
[object]$ReleaseJson = $null,
[pscredential]$Credential = $null
)
$ErrorActionPreference = "Stop"
$GiteaUrl = $GiteaUrl.TrimEnd('/')
$TargetDir = "C:\Apps\mini-pc-setup"
$TempZip = "$env:TEMP\mini-pc-setup-release.zip"
$TempExtract = "$env:TEMP\mini-pc-release-extract"
Write-Host "========================================================" -ForegroundColor Cyan
Write-Host " DTS TORQUE VAULT - DOWNLOADING LATEST RELEASE" -ForegroundColor Cyan
Write-Host "========================================================" -ForegroundColor Cyan
if ($null -eq $ReleaseJson) {
$apiUrl = "$GiteaUrl/api/v1/repos/$Owner/$Repo/releases/latest"
try {
$ReleaseJson = Invoke-RestMethod -Uri $apiUrl -UseBasicParsing
} catch {
Write-Host "[AUTH] Private repository detected. Please sign in to Gitea:" -ForegroundColor Yellow
$Credential = Get-Credential -Message "Enter Gitea Credentials"
$ReleaseJson = Invoke-RestMethod -Uri $apiUrl -Credential $Credential -UseBasicParsing
}
}
$TagName = $ReleaseJson.tag_name
Write-Host "[INFO] Latest Release Tag: $TagName ($($ReleaseJson.name))" -ForegroundColor Green
$ZipAsset = $ReleaseJson.assets | Where-Object { $_.name -like "*.zip" } | Select-Object -First 1
if ($null -ne $ZipAsset) {
$DownloadUrl = $ZipAsset.browser_download_url
Write-Host "[1/3] Downloading attached release asset: $($ZipAsset.name)..."
} else {
$DownloadUrl = $ReleaseJson.zipball_url
Write-Host "[1/3] Downloading release archive ($TagName)..."
}
if (-not (Test-Path $TargetDir)) {
New-Item -ItemType Directory -Path $TargetDir -Force | Out-Null
}
if ($null -ne $Credential) {
Invoke-WebRequest -Uri $DownloadUrl -OutFile $TempZip -Credential $Credential -UseBasicParsing
} else {
Invoke-WebRequest -Uri $DownloadUrl -OutFile $TempZip -UseBasicParsing
}
Write-Host "[2/3] Extracting files to $TargetDir..."
if (Test-Path $TempExtract) { Remove-Item $TempExtract -Recurse -Force }
Expand-Archive -Path $TempZip -DestinationPath $TempExtract -Force
$TopDirs = Get-ChildItem -Path $TempExtract -Directory
$TopFiles = Get-ChildItem -Path $TempExtract -File
if ($TopFiles.Count -eq 0 -and $TopDirs.Count -eq 1) {
Copy-Item -Path "$($TopDirs[0].FullName)\*" -Destination $TargetDir -Recurse -Force
} else {
Copy-Item -Path "$TempExtract\*" -Destination $TargetDir -Recurse -Force
}
Remove-Item $TempZip -Force -ErrorAction SilentlyContinue
Remove-Item $TempExtract -Recurse -Force -ErrorAction SilentlyContinue
# Auto-create .env from .env.template if .env does not exist yet
if (-not (Test-Path "$TargetDir\.env") -and (Test-Path "$TargetDir\.env.template")) {
Copy-Item "$TargetDir\.env.template" "$TargetDir\.env"
Write-Host "[ACTION REQUIRED] Please configure '.env' in Notepad, save, and close it to continue..." -ForegroundColor Yellow
Start-Process notepad.exe "$TargetDir\.env" -Wait
}
Write-Host "[3/3] Launching scripts\install.bat..." -ForegroundColor Green
$InstallScript = "$TargetDir\scripts\install.bat"
if (-not (Test-Path $InstallScript)) {
throw "Cannot find $InstallScript after extraction!"
}
Start-Process -FilePath "cmd.exe" -ArgumentList "/c `"$InstallScript`"" -WorkingDirectory "$TargetDir\scripts"
+43 -10
View File
@@ -10,7 +10,7 @@ set "CLEAN_ROOT=%ROOT_DIR:~0,-1%"
:: WSL2 & Docker Engine Settings
set "WSL_DISTRO=Ubuntu-22.04"
set "WSL_USER=dtsadmin"
set "DEFAULT_WSL_USER=gateway"
set "DOCKER_VERSION=5:28.3.2-1~ubuntu.22.04~jammy"
:: Project & Health-Check Polling Settings
@@ -136,20 +136,53 @@ if %errorLevel% neq 0 (
)
:: ============================================================================
:: PHASE 2: INITIALIZE UBUNTU USER, SYSTEMD & VMIDLETIMEOUT IN WSL2
:: PHASE 2: INITIALIZE UBUNTU USER (WITH PASSWORD), SYSTEMD & VMIDLETIMEOUT
:: ============================================================================
echo [1/7] Initializing %WSL_DISTRO%, enabling systemd and disabling VM idle timeout...
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"$wslCfg = Join-Path $env:USERPROFILE '.wslconfig';" ^
"Set-Content -Path $wslCfg -Value \"[wsl2]`r`nvmIdleTimeout=-1\" -Encoding Ascii"
echo [1/7] Initializing %WSL_DISTRO%, configuring Linux user, systemd and VM idle timeout...
wsl -d %WSL_DISTRO% -u root bash -c ^
"id -u %WSL_USER% >/dev/null 2>&1 || (useradd -m -s /bin/bash -G sudo %WSL_USER% && echo '%WSL_USER% ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/%WSL_USER%)"
wsl -d %WSL_DISTRO% -u root bash -c ^
"printf '[user]\ndefault=%WSL_USER%\n[boot]\nsystemd=true\n' > /etc/wsl.conf"
set "WSL_USER="
set /p WSL_USER="Enter Linux username for WSL [Default: %DEFAULT_WSL_USER%]: "
if "%WSL_USER%"=="" set "WSL_USER=%DEFAULT_WSL_USER%"
if /i "%WSL_USER%"=="root" goto :ERR_WSL_ROOT
set "WSL_PASS="
set /p WSL_PASS="Enter Linux sudo password for '%WSL_USER%' [Default: Dts@2026]: "
if "%WSL_PASS%"=="" set "WSL_PASS=Dts@2026"
powershell -NoProfile -ExecutionPolicy Bypass -Command "$wslCfg = Join-Path $env:USERPROFILE '.wslconfig'; Set-Content -Path $wslCfg -Value \"[wsl2]`r`nvmIdleTimeout=-1\" -Encoding Ascii"
set "STEP1_SH=%TEMP%\dts_step1.sh"
> "%STEP1_SH%" echo set -e
>> "%STEP1_SH%" echo WSL_USR="$1"
>> "%STEP1_SH%" echo WSL_PWD="$2"
>> "%STEP1_SH%" echo id -u "$WSL_USR" ^>/dev/null 2^>^&1 ^|^| useradd -m -s /bin/bash -G sudo "$WSL_USR"
>> "%STEP1_SH%" echo echo "${WSL_USR}:${WSL_PWD}" ^| chpasswd
>> "%STEP1_SH%" echo rm -f "/etc/sudoers.d/${WSL_USR}"
>> "%STEP1_SH%" echo printf '[user]\ndefault=%%s\n[boot]\nsystemd=true\n' "$WSL_USR" ^> /etc/wsl.conf
for /f "delims=" %%i in ('wsl -d %WSL_DISTRO% -u root wslpath -a "%STEP1_SH%"') do set "WSL_STEP1_SH=%%i"
wsl -d %WSL_DISTRO% -u root bash -c "tr -d '\r' < '%WSL_STEP1_SH%' > /tmp/dts_step1.sh && bash /tmp/dts_step1.sh '%WSL_USER%' '%WSL_PASS%'; RC=$?; rm -f /tmp/dts_step1.sh; exit $RC"
set "STEP1_ERR=%errorLevel%"
del /f /q "%STEP1_SH%" >nul 2>&1
if %STEP1_ERR% neq 0 goto :ERR_WSL_USER
wsl --terminate %WSL_DISTRO% >nul 2>&1
timeout /t 2 /nobreak >nul
goto :PHASE2_DONE
:ERR_WSL_ROOT
echo [ERROR] Cannot use root as the standard WSL user!
pause
exit /b 1
:ERR_WSL_USER
echo [ERROR] Failed to create or configure Linux user "%WSL_USER%".
pause
exit /b 1
:PHASE2_DONE
:: ============================================================================
:: PHASE 3: INSTALL & PIN DOCKER ENGINE + COMPOSE V2 (DIRECT .SH + DYNAMIC PARAMS)
-69
View File
@@ -1,69 +0,0 @@
@echo off
setlocal EnableDelayedExpansion
:: ============================================================================
:: CONFIGURATION
:: ============================================================================
set "GITEA_URL=https://gitea.digitoolsolutions.synology.me"
set "REPO_OWNER=lamtruong28"
set "REPO_NAME=mini-pc-setup"
set "EXTRACT_DIR=C:\Apps\%REPO_NAME%"
:: ============================================================================
:: 0. AUTO-ELEVATE TO ADMINISTRATOR
:: ============================================================================
net session >nul 2>&1
if %errorLevel% neq 0 (
echo Requesting Administrator privileges...
powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
exit /b
)
echo ============================================================================
echo DTS TORQUE VAULT - ONE-CLICK LATEST RELEASE BOOTSTRAPPER
echo ============================================================================
echo Repository : %GITEA_URL%/%REPO_OWNER%/%REPO_NAME%
echo Destination : %EXTRACT_DIR%
echo.
:: ============================================================================
:: 1. PRE-CHECK DNS & INTERNET CONNECTIVITY
:: ============================================================================
echo [1/3] Checking network and DNS resolution for %GITEA_URL%...
powershell -NoProfile -ExecutionPolicy Bypass -Command "$h=([System.Uri]'%GITEA_URL%').Host; try { [System.Net.Dns]::GetHostAddresses($h) | Out-Null; exit 0 } catch { Write-Host '[ERROR] Cannot resolve DNS for' $h '- Check Internet.' -ForegroundColor Red; exit 1 }"
if %errorLevel% neq 0 (
pause
exit /b 1
)
:: ============================================================================
:: 2. FETCH LATEST RELEASE (PUBLIC FIRST, AUTH ONLY ON HTTP 401/403/404)
:: ============================================================================
echo [2/3] Fetching latest release package...
set "ZIP_FILE=%TEMP%\%REPO_NAME%-latest.zip"
if exist "%ZIP_FILE%" del /f /q "%ZIP_FILE%" >nul 2>&1
powershell -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $apiUrl = '%GITEA_URL%/api/v1/repos/%REPO_OWNER%/%REPO_NAME%/releases/latest'; $outZip = '%ZIP_FILE%'; $headers = @{ 'User-Agent' = 'DTS-Bootstrapper' }; $needAuth = $false; try { $rel = Invoke-RestMethod -Uri $apiUrl -Headers $headers -ErrorAction Stop } catch { $status = $null; if ($_.Exception.Response) { $status = [int]$_.Exception.Response.StatusCode }; if ($status -in @(401, 403, 404)) { $needAuth = $true } else { Write-Host ('[ERROR] Network/SSL error connecting to Gitea: ' + $_.Exception.Message) -ForegroundColor Red; exit 1 } }; if ($needAuth) { Write-Host '[AUTH] Private repository or restricted release. Please sign in to Gitea:'; $cred = Get-Credential -UserName '%REPO_OWNER%' -Message 'Enter Gitea credentials'; if (-not $cred) { exit 1 }; $pair = '{0}:{1}' -f $cred.UserName, $cred.GetNetworkCredential().Password; $headers['Authorization'] = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($pair)); try { $rel = Invoke-RestMethod -Uri $apiUrl -Headers $headers -ErrorAction Stop } catch { Write-Host ('[ERROR] Gitea authentication or release lookup failed: ' + $_.Exception.Message) -ForegroundColor Red; exit 1 } }; $zipUrl = $rel.zipball_url; if (-not $zipUrl) { Write-Host '[ERROR] No release zipball found. Ensure at least one Release is published on Gitea.' -ForegroundColor Red; exit 1 }; Write-Host (' - Downloading release ' + $rel.tag_name + '...'); Invoke-WebRequest -Uri $zipUrl -Headers $headers -OutFile $outZip -UseBasicParsing -ErrorAction Stop"
if %errorLevel% neq 0 (
echo [ERROR] Bootstrap failed.
pause
exit /b 1
)
:: ============================================================================
:: 3. EXTRACT & LAUNCH INSTALLER
:: ============================================================================
echo [3/3] Extracting to %EXTRACT_DIR% and launching install.bat...
if exist "%EXTRACT_DIR%" rmdir /s /q "%EXTRACT_DIR%" >nul 2>&1
mkdir "%EXTRACT_DIR%" >nul 2>&1
powershell -NoProfile -ExecutionPolicy Bypass -Command "$tmp = Join-Path $env:TEMP 'dts_release_extract'; if (Test-Path $tmp) { Remove-Item $tmp -Recurse -Force }; Expand-Archive -Path '%ZIP_FILE%' -DestinationPath $tmp -Force; $inner = Get-ChildItem -Path $tmp -Directory | Select-Object -First 1; if ($inner) { Get-ChildItem -Path $inner.FullName -Force | Copy-Item -Destination '%EXTRACT_DIR%' -Recurse -Force } else { Get-ChildItem -Path $tmp -Force | Copy-Item -Destination '%EXTRACT_DIR%' -Recurse -Force }; Remove-Item $tmp -Recurse -Force; Remove-Item '%ZIP_FILE%' -Force"
if not exist "%EXTRACT_DIR%\scripts\install.bat" (
echo [ERROR] scripts\install.bat not found inside %EXTRACT_DIR%!
pause
exit /b 1
)
call "%EXTRACT_DIR%\scripts\install.bat"