Implements full Windows Hyper-V provisioning
Rewrites setup_env_windows.ps1 to fully implement WINDOWS_PLAN.md with: - Fixed cloud-init password handling using chpasswd - Multiple ISO creation fallbacks (oscdimg/WSL/IMAPI2) - Component skip parameters for VNC, PostgreSQL, Ollama, Playwright - VNC password support via base64 encoding - BITS transfer for reliable downloads - SSH readiness checking before showing connection info Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
cc1277cd98
commit
65790ee3e2
2 changed files with 391 additions and 120 deletions
20
CHANGELOG.md
20
CHANGELOG.md
|
|
@ -5,6 +5,26 @@ All notable changes to this project will be documented in this file.
|
|||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [0.13.0] - 2025-01-25
|
||||
|
||||
### Changed
|
||||
- Rewrote `setup_env_windows.ps1` to fully implement WINDOWS_PLAN.md
|
||||
- Password handling uses cloud-init `chpasswd` with plaintext (type: text) instead of broken hash generation
|
||||
- Multiple ISO creation methods with fallback chain: oscdimg → WSL genisoimage → IMAPI2 COM
|
||||
- Downloads use BITS transfer for reliability with progress reporting
|
||||
- SSH readiness checking with timeout before displaying connection info
|
||||
|
||||
### Added
|
||||
- Component skip parameters: `-SkipVNC`, `-SkipPostgreSQL`, `-SkipOllama`, `-SkipPlaywright`
|
||||
- VNC password support via base64 encoding in cloud-init
|
||||
- Automatic hosts file cleanup when VM is deleted with `-Force`
|
||||
- Proper prerequisite checking for Hyper-V, Windows edition, and admin privileges
|
||||
|
||||
### Fixed
|
||||
- Cloud-init password configuration (was using bash syntax in PowerShell)
|
||||
- ISO creation now works without Windows ADK by using WSL or IMAPI2 fallbacks
|
||||
- Hosts file handling with proper admin privilege elevation
|
||||
|
||||
## [0.12.0] - 2025-01-25
|
||||
|
||||
### Added
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<#
|
||||
.SYNOPSIS
|
||||
OrbStack-equivalent Development Sandbox Setup for Windows using Hyper-V
|
||||
Hyper-V Development Sandbox for Windows - Maximum Security Mode
|
||||
|
||||
.DESCRIPTION
|
||||
Creates fully isolated Hyper-V VMs for running Claude Code with maximum security.
|
||||
|
|
@ -24,11 +24,23 @@
|
|||
.PARAMETER CPUs
|
||||
Number of virtual CPUs (default: 4)
|
||||
|
||||
.PARAMETER SkipVNC
|
||||
Skip VNC/XFCE desktop installation
|
||||
|
||||
.PARAMETER SkipPostgreSQL
|
||||
Skip PostgreSQL installation
|
||||
|
||||
.PARAMETER SkipOllama
|
||||
Skip Ollama LLM runner installation
|
||||
|
||||
.PARAMETER Force
|
||||
Replace existing VM with same name
|
||||
|
||||
.EXAMPLE
|
||||
.\setup_env_windows.ps1 -VMName my-project
|
||||
|
||||
.EXAMPLE
|
||||
.\setup_env_windows.ps1 -VMName elixir-dev -MemoryGB 16 -DiskGB 100
|
||||
.\setup_env_windows.ps1 -VMName elixir-dev -MemoryGB 16 -DiskGB 100 -SkipOllama
|
||||
|
||||
.NOTES
|
||||
Requirements:
|
||||
|
|
@ -54,6 +66,10 @@ param(
|
|||
[ValidateRange(1, 16)]
|
||||
[int]$CPUs = 4,
|
||||
|
||||
[switch]$SkipVNC,
|
||||
[switch]$SkipPostgreSQL,
|
||||
[switch]$SkipOllama,
|
||||
[switch]$SkipPlaywright,
|
||||
[switch]$Force
|
||||
)
|
||||
|
||||
|
|
@ -103,53 +119,22 @@ function Test-HyperVEnabled {
|
|||
}
|
||||
|
||||
function Get-VMSwitch-Default {
|
||||
# Try to find an external switch first, then default switch
|
||||
$switch = Get-VMSwitch -SwitchType External -ErrorAction SilentlyContinue | Select-Object -First 1
|
||||
# Try to find default switch first (most common), then external
|
||||
$switch = Get-VMSwitch -Name "Default Switch" -ErrorAction SilentlyContinue
|
||||
if (-not $switch) {
|
||||
$switch = Get-VMSwitch -Name "Default Switch" -ErrorAction SilentlyContinue
|
||||
$switch = Get-VMSwitch -SwitchType External -ErrorAction SilentlyContinue | Select-Object -First 1
|
||||
}
|
||||
if (-not $switch) {
|
||||
$switch = Get-VMSwitch -SwitchType Internal -ErrorAction SilentlyContinue | Select-Object -First 1
|
||||
}
|
||||
return $switch
|
||||
}
|
||||
|
||||
function New-CloudInitISO {
|
||||
param(
|
||||
[string]$OutputPath,
|
||||
[string]$UserData,
|
||||
[string]$MetaData
|
||||
)
|
||||
|
||||
# Create temp directory for cloud-init files
|
||||
$tempDir = New-TemporaryFile | ForEach-Object { Remove-Item $_; New-Item -ItemType Directory -Path $_ }
|
||||
|
||||
try {
|
||||
# Write cloud-init files
|
||||
$UserData | Out-File -FilePath "$tempDir\user-data" -Encoding UTF8 -NoNewline
|
||||
$MetaData | Out-File -FilePath "$tempDir\meta-data" -Encoding UTF8 -NoNewline
|
||||
|
||||
# Use oscdimg to create ISO (included in Windows ADK, or use alternative)
|
||||
# For simplicity, we'll use a PowerShell-based approach with .NET
|
||||
|
||||
# Check for oscdimg
|
||||
$oscdimg = Get-Command oscdimg -ErrorAction SilentlyContinue
|
||||
if ($oscdimg) {
|
||||
& oscdimg -j2 -lcidata "$tempDir" "$OutputPath" 2>&1 | Out-Null
|
||||
} else {
|
||||
# Fallback: Create a simple ISO using PowerShell
|
||||
# This requires the CDROM file system support
|
||||
Write-Warning "oscdimg not found. Installing Windows ADK tools is recommended."
|
||||
Write-Warning "Attempting alternative ISO creation..."
|
||||
|
||||
# Use a .NET approach or download a tool
|
||||
# For now, we'll use a simple approach with New-IsoFile if available
|
||||
throw "ISO creation requires Windows ADK. Install it from: https://docs.microsoft.com/en-us/windows-hardware/get-started/adk-install"
|
||||
}
|
||||
} finally {
|
||||
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
|
||||
function Get-SecureInput {
|
||||
param([string]$Prompt, [switch]$AsPlainText)
|
||||
if ($AsPlainText) {
|
||||
return Read-Host -Prompt $Prompt
|
||||
}
|
||||
}
|
||||
|
||||
function Get-SecureCredential {
|
||||
param([string]$Prompt)
|
||||
$secure = Read-Host -Prompt $Prompt -AsSecureString
|
||||
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
|
||||
try {
|
||||
|
|
@ -164,6 +149,126 @@ function ConvertTo-Base64 {
|
|||
return [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Text))
|
||||
}
|
||||
|
||||
function ConvertTo-SHA512Hash {
|
||||
# Create a SHA-512 crypt password hash compatible with Linux /etc/shadow
|
||||
# Format: $6$<salt>$<hash>
|
||||
param([string]$Password)
|
||||
|
||||
# Generate random salt (16 chars from allowed set)
|
||||
$saltChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"
|
||||
$salt = -join ((1..16) | ForEach-Object { $saltChars[(Get-Random -Maximum $saltChars.Length)] })
|
||||
|
||||
# For cloud-init, we can use the plain password with chpasswd instead
|
||||
# Return a placeholder that cloud-init will handle
|
||||
return $Password
|
||||
}
|
||||
|
||||
function New-CloudInitISO {
|
||||
param(
|
||||
[string]$OutputPath,
|
||||
[string]$UserDataContent,
|
||||
[string]$MetaDataContent
|
||||
)
|
||||
|
||||
$tempDir = Join-Path $env:TEMP "cloudinit-$(Get-Random)"
|
||||
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
|
||||
|
||||
try {
|
||||
# Write cloud-init files with Unix line endings (LF only)
|
||||
$UserDataContent -replace "`r`n", "`n" | Set-Content -Path "$tempDir\user-data" -Encoding UTF8 -NoNewline
|
||||
$MetaDataContent -replace "`r`n", "`n" | Set-Content -Path "$tempDir\meta-data" -Encoding UTF8 -NoNewline
|
||||
|
||||
# Method 1: Try oscdimg (Windows ADK)
|
||||
$oscdimg = Get-Command oscdimg -ErrorAction SilentlyContinue
|
||||
if ($oscdimg) {
|
||||
Write-Status "Creating ISO with oscdimg..." "Info"
|
||||
$result = & oscdimg -j2 -lcidata "$tempDir" "$OutputPath" 2>&1
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
return $true
|
||||
}
|
||||
}
|
||||
|
||||
# Method 2: Try WSL with genisoimage
|
||||
$wsl = Get-Command wsl -ErrorAction SilentlyContinue
|
||||
if ($wsl) {
|
||||
Write-Status "Creating ISO with WSL genisoimage..." "Info"
|
||||
# Convert Windows path to WSL path
|
||||
$wslTempDir = $tempDir -replace '\\', '/' -replace '^([A-Za-z]):', '/mnt/$1'.ToLower()
|
||||
$wslOutputPath = $OutputPath -replace '\\', '/' -replace '^([A-Za-z]):', '/mnt/$1'.ToLower()
|
||||
|
||||
# Check if genisoimage is available in WSL
|
||||
$result = wsl which genisoimage 2>&1
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
wsl genisoimage -output "$wslOutputPath" -volid cidata -joliet -rock "$wslTempDir/user-data" "$wslTempDir/meta-data" 2>&1 | Out-Null
|
||||
if ($LASTEXITCODE -eq 0 -and (Test-Path $OutputPath)) {
|
||||
return $true
|
||||
}
|
||||
}
|
||||
|
||||
# Try mkisofs as alternative
|
||||
$result = wsl which mkisofs 2>&1
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
wsl mkisofs -output "$wslOutputPath" -volid cidata -joliet -rock "$wslTempDir/user-data" "$wslTempDir/meta-data" 2>&1 | Out-Null
|
||||
if ($LASTEXITCODE -eq 0 -and (Test-Path $OutputPath)) {
|
||||
return $true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Method 3: PowerShell native ISO creation using .NET
|
||||
Write-Status "Creating ISO with PowerShell/.NET..." "Info"
|
||||
New-ISOFile -SourcePath $tempDir -DestinationPath $OutputPath -VolumeName "cidata"
|
||||
return $true
|
||||
|
||||
} finally {
|
||||
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
function New-ISOFile {
|
||||
# Pure PowerShell ISO creation using IMAPI2
|
||||
param(
|
||||
[string]$SourcePath,
|
||||
[string]$DestinationPath,
|
||||
[string]$VolumeName = "DVDROM"
|
||||
)
|
||||
|
||||
# Load IMAPI2 COM objects
|
||||
$fsi = New-Object -ComObject IMAPI2FS.MsftFileSystemImage
|
||||
$fsi.FileSystemsToCreate = 3 # FsiFileSystemISO9660 | FsiFileSystemJoliet
|
||||
$fsi.VolumeName = $VolumeName
|
||||
|
||||
# Add files from source directory
|
||||
$files = Get-ChildItem -Path $SourcePath -File
|
||||
foreach ($file in $files) {
|
||||
$stream = New-Object -ComObject ADODB.Stream
|
||||
$stream.Type = 1 # Binary
|
||||
$stream.Open()
|
||||
$stream.LoadFromFile($file.FullName)
|
||||
$fsi.Root.AddFile($file.Name, $stream)
|
||||
}
|
||||
|
||||
# Create the ISO
|
||||
$result = $fsi.CreateResultImage()
|
||||
$stream = $result.ImageStream
|
||||
|
||||
# Write to file
|
||||
$outputStream = [System.IO.File]::Create($DestinationPath)
|
||||
try {
|
||||
$buffer = New-Object byte[] 2048
|
||||
do {
|
||||
$bytesRead = $stream.Read($buffer, 0, $buffer.Length)
|
||||
if ($bytesRead -gt 0) {
|
||||
$outputStream.Write($buffer, 0, $bytesRead)
|
||||
}
|
||||
} while ($bytesRead -gt 0)
|
||||
} finally {
|
||||
$outputStream.Close()
|
||||
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($stream) | Out-Null
|
||||
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($fsi) | Out-Null
|
||||
}
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Pre-flight Checks
|
||||
# ============================================================================
|
||||
|
|
@ -199,13 +304,15 @@ if ($existingVM) {
|
|||
if (-not $Force) {
|
||||
Write-Status "VM '$VMName' already exists." "Error"
|
||||
Write-Host ""
|
||||
Write-Host "To delete it: Remove-VM -Name $VMName -Force" -ForegroundColor Yellow
|
||||
Write-Host "To connect: vmconnect localhost $VMName" -ForegroundColor Yellow
|
||||
Write-Host "Or use -Force to replace it" -ForegroundColor Yellow
|
||||
Write-Host "Options:" -ForegroundColor Yellow
|
||||
Write-Host " Connect: vmconnect localhost $VMName" -ForegroundColor White
|
||||
Write-Host " Replace: .\setup_env_windows.ps1 -VMName $VMName -Force" -ForegroundColor White
|
||||
Write-Host " Delete: Stop-VM -Name $VMName -Force; Remove-VM -Name $VMName -Force" -ForegroundColor White
|
||||
exit 1
|
||||
} else {
|
||||
Write-Status "Removing existing VM '$VMName'..." "Warning"
|
||||
Stop-VM -Name $VMName -Force -ErrorAction SilentlyContinue
|
||||
Start-Sleep -Seconds 2
|
||||
Remove-VM -Name $VMName -Force
|
||||
Remove-Item -Path "$VMS_PATH\$VMName" -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
|
@ -216,8 +323,9 @@ $vmSwitch = Get-VMSwitch-Default
|
|||
if (-not $vmSwitch) {
|
||||
Write-Status "No suitable network switch found." "Error"
|
||||
Write-Host ""
|
||||
Write-Host "Create a virtual switch in Hyper-V Manager or run:" -ForegroundColor Yellow
|
||||
Write-Host ' New-VMSwitch -Name "External Switch" -NetAdapterName (Get-NetAdapter | Where-Object Status -eq Up | Select-Object -First 1).Name' -ForegroundColor Cyan
|
||||
Write-Host "The 'Default Switch' should be available on Windows 10/11." -ForegroundColor Yellow
|
||||
Write-Host "If missing, create one in Hyper-V Manager or run:" -ForegroundColor Yellow
|
||||
Write-Host ' New-VMSwitch -Name "Default Switch" -SwitchType Internal' -ForegroundColor Cyan
|
||||
exit 1
|
||||
}
|
||||
Write-Status "Using network switch: $($vmSwitch.Name)" "Info"
|
||||
|
|
@ -242,11 +350,11 @@ if (Test-Path $CONFIG_FILE) {
|
|||
Write-Host ""
|
||||
|
||||
do {
|
||||
$GitName = Read-Host "Git commit author name"
|
||||
$GitName = Get-SecureInput "Git commit author name" -AsPlainText
|
||||
} while ([string]::IsNullOrWhiteSpace($GitName))
|
||||
|
||||
do {
|
||||
$GitEmail = Read-Host "Git commit author email"
|
||||
$GitEmail = Get-SecureInput "Git commit author email" -AsPlainText
|
||||
} while ($GitEmail -notmatch '^[^@]+@[^@]+\.[^@]+$')
|
||||
|
||||
# Save config
|
||||
|
|
@ -273,9 +381,18 @@ GIT_EMAIL="$GitEmail"
|
|||
# Prompt for VM password (not stored)
|
||||
Write-Host ""
|
||||
do {
|
||||
$VMPassword = Get-SecureCredential "VM user password (min 8 chars)"
|
||||
$VMPassword = Get-SecureInput "VM user password (min 8 chars, for 'dev' user)"
|
||||
} while ($VMPassword.Length -lt 8)
|
||||
|
||||
# Prompt for VNC password if VNC is enabled
|
||||
$VNCPassword = ""
|
||||
if (-not $SkipVNC) {
|
||||
Write-Host ""
|
||||
do {
|
||||
$VNCPassword = Get-SecureInput "VNC password (min 6 chars, for remote desktop)"
|
||||
} while ($VNCPassword.Length -lt 6)
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Download Ubuntu Cloud Image
|
||||
# ============================================================================
|
||||
|
|
@ -286,18 +403,38 @@ New-Item -ItemType Directory -Path "$VMS_PATH\$VMName" -Force | Out-Null
|
|||
$imagePath = "$IMAGES_PATH\ubuntu-$UBUNTU_VERSION-cloudimg-amd64.vhdx"
|
||||
|
||||
if (-not (Test-Path $imagePath)) {
|
||||
Write-Status "Downloading Ubuntu $UBUNTU_VERSION cloud image..." "Info"
|
||||
Write-Status "Downloading Ubuntu $UBUNTU_VERSION cloud image (~700MB)..." "Info"
|
||||
Write-Host " URL: $UBUNTU_IMAGE_URL" -ForegroundColor Gray
|
||||
Write-Host " This may take a few minutes..." -ForegroundColor Gray
|
||||
|
||||
try {
|
||||
Invoke-WebRequest -Uri $UBUNTU_IMAGE_URL -OutFile $imagePath -UseBasicParsing
|
||||
# Use BITS for better download handling
|
||||
$bitsJob = Start-BitsTransfer -Source $UBUNTU_IMAGE_URL -Destination $imagePath -Asynchronous
|
||||
while ($bitsJob.JobState -eq "Transferring" -or $bitsJob.JobState -eq "Connecting") {
|
||||
$percent = [math]::Round(($bitsJob.BytesTransferred / $bitsJob.BytesTotal) * 100, 0)
|
||||
Write-Host "`r Progress: $percent%" -NoNewline
|
||||
Start-Sleep -Seconds 2
|
||||
}
|
||||
Write-Host ""
|
||||
|
||||
if ($bitsJob.JobState -ne "Transferred") {
|
||||
throw "Download failed: $($bitsJob.JobState)"
|
||||
}
|
||||
Complete-BitsTransfer -BitsJob $bitsJob
|
||||
Write-Status "Download complete" "Success"
|
||||
} catch {
|
||||
Write-Status "Failed to download Ubuntu image: $_" "Error"
|
||||
exit 1
|
||||
# Fallback to Invoke-WebRequest
|
||||
Write-Status "BITS transfer failed, trying direct download..." "Warning"
|
||||
try {
|
||||
Invoke-WebRequest -Uri $UBUNTU_IMAGE_URL -OutFile $imagePath -UseBasicParsing
|
||||
Write-Status "Download complete" "Success"
|
||||
} catch {
|
||||
Write-Status "Failed to download Ubuntu image: $_" "Error"
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Write-Status "Using cached Ubuntu image: $imagePath" "Info"
|
||||
Write-Status "Using cached Ubuntu image" "Info"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
|
|
@ -306,74 +443,136 @@ if (-not (Test-Path $imagePath)) {
|
|||
|
||||
$vmDiskPath = "$VMS_PATH\$VMName\disk.vhdx"
|
||||
|
||||
Write-Status "Creating VM disk..." "Info"
|
||||
Write-Status "Creating VM disk ($DiskGB GB)..." "Info"
|
||||
Copy-Item -Path $imagePath -Destination $vmDiskPath -Force
|
||||
Resize-VHD -Path $vmDiskPath -SizeBytes ($DiskGB * 1GB)
|
||||
|
||||
# ============================================================================
|
||||
# Generate SSH Key
|
||||
# ============================================================================
|
||||
|
||||
$sshDir = "$env:USERPROFILE\.ssh"
|
||||
if (-not (Test-Path $sshDir)) {
|
||||
New-Item -ItemType Directory -Path $sshDir -Force | Out-Null
|
||||
}
|
||||
|
||||
$sshKeyPath = "$sshDir\id_ed25519_$VMName"
|
||||
if (-not (Test-Path $sshKeyPath)) {
|
||||
Write-Status "Generating SSH key pair..." "Info"
|
||||
# Use ssh-keygen (available on Windows 10+)
|
||||
$sshKeygen = Get-Command ssh-keygen -ErrorAction SilentlyContinue
|
||||
if ($sshKeygen) {
|
||||
& ssh-keygen -t ed25519 -f $sshKeyPath -N '""' -C "$VMName-sandbox" 2>&1 | Out-Null
|
||||
} else {
|
||||
Write-Status "ssh-keygen not found. Please install OpenSSH." "Error"
|
||||
Write-Host " Settings > Apps > Optional Features > OpenSSH Client" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
$sshPublicKey = Get-Content "$sshKeyPath.pub"
|
||||
Write-Status "SSH key ready: $sshKeyPath" "Info"
|
||||
|
||||
# ============================================================================
|
||||
# Create Cloud-Init Configuration
|
||||
# ============================================================================
|
||||
|
||||
Write-Status "Generating cloud-init configuration..." "Info"
|
||||
|
||||
# Generate SSH key for passwordless access from host
|
||||
$sshKeyPath = "$env:USERPROFILE\.ssh\id_ed25519_$VMName"
|
||||
if (-not (Test-Path $sshKeyPath)) {
|
||||
ssh-keygen -t ed25519 -f $sshKeyPath -N '""' -C "$VMName-sandbox" 2>&1 | Out-Null
|
||||
}
|
||||
$sshPublicKey = Get-Content "$sshKeyPath.pub"
|
||||
|
||||
# Encode setup script for transfer
|
||||
$setupScriptPath = "$PSScriptRoot\setup_env.sh"
|
||||
if (-not (Test-Path $setupScriptPath)) {
|
||||
Write-Status "setup_env.sh not found in script directory" "Error"
|
||||
Write-Host " Expected at: $setupScriptPath" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
$setupScriptB64 = ConvertTo-Base64 (Get-Content $setupScriptPath -Raw)
|
||||
$setupScriptContent = Get-Content $setupScriptPath -Raw
|
||||
$setupScriptB64 = ConvertTo-Base64 $setupScriptContent
|
||||
|
||||
# Build skip exports for components
|
||||
$skipExports = @()
|
||||
if ($SkipVNC) { $skipExports += "export SKIP_VNC=1" }
|
||||
if ($SkipPostgreSQL) { $skipExports += "export SKIP_POSTGRESQL=1" }
|
||||
if ($SkipOllama) { $skipExports += "export SKIP_OLLAMA=1" }
|
||||
if ($SkipPlaywright) { $skipExports += "export SKIP_PLAYWRIGHT=1; export SKIP_CHROMIUM=1" }
|
||||
$skipExportsStr = ($skipExports -join "; ")
|
||||
if ($skipExportsStr) { $skipExportsStr += "; " }
|
||||
|
||||
# VNC password handling
|
||||
$vncPasswordExport = ""
|
||||
if (-not $SkipVNC -and $VNCPassword) {
|
||||
$vncPasswordB64 = ConvertTo-Base64 $VNCPassword
|
||||
$vncPasswordExport = "export VNC_PASSWORD=`$(echo '$vncPasswordB64' | base64 -d); "
|
||||
}
|
||||
|
||||
$cloudInitUserData = @"
|
||||
#cloud-config
|
||||
hostname: $VMName
|
||||
manage_etc_hosts: true
|
||||
fqdn: $VMName.local
|
||||
|
||||
users:
|
||||
- name: dev
|
||||
gecos: Development User
|
||||
sudo: ALL=(ALL) NOPASSWD:ALL
|
||||
shell: /bin/bash
|
||||
lock_passwd: false
|
||||
passwd: $(echo "$VMPassword" | openssl passwd -6 -stdin 2>/dev/null || echo '$VMPassword')
|
||||
ssh_authorized_keys:
|
||||
- $sshPublicKey
|
||||
|
||||
chpasswd:
|
||||
expire: false
|
||||
users:
|
||||
- name: dev
|
||||
password: $VMPassword
|
||||
type: text
|
||||
|
||||
package_update: true
|
||||
package_upgrade: true
|
||||
package_upgrade: false
|
||||
|
||||
packages:
|
||||
- openssh-server
|
||||
- curl
|
||||
- wget
|
||||
- git
|
||||
- ca-certificates
|
||||
|
||||
write_files:
|
||||
- path: /tmp/setup_env.sh
|
||||
permissions: '0755'
|
||||
encoding: b64
|
||||
content: $setupScriptB64
|
||||
|
||||
- path: /tmp/provision.sh
|
||||
permissions: '0755'
|
||||
content: |
|
||||
#!/bin/bash
|
||||
set -e
|
||||
echo "=== Starting provisioning ==="
|
||||
|
||||
# Wait for apt locks to be released
|
||||
while fuser /var/lib/apt/lists/lock >/dev/null 2>&1; do
|
||||
echo "Waiting for apt lock..."
|
||||
sleep 5
|
||||
done
|
||||
|
||||
# Set environment variables
|
||||
export GIT_NAME="$GitName"
|
||||
export GIT_EMAIL="$GitEmail"
|
||||
$vncPasswordExport$skipExportsStr
|
||||
|
||||
# Run setup script as dev user
|
||||
cd /home/dev
|
||||
sudo -u dev bash /tmp/setup_env.sh --non-interactive --yes 2>&1 | tee /var/log/provision.log
|
||||
sudo -E -u dev bash /tmp/setup_env.sh --non-interactive --yes 2>&1 | tee /var/log/provision.log
|
||||
|
||||
echo "=== Provisioning complete ==="
|
||||
|
||||
runcmd:
|
||||
- systemctl enable ssh
|
||||
- systemctl start ssh
|
||||
- bash /tmp/provision.sh
|
||||
- echo "Starting provisioning in background..."
|
||||
- nohup bash /tmp/provision.sh > /var/log/provision-runner.log 2>&1 &
|
||||
|
||||
final_message: "Cloud-init provisioning complete after \$UPTIME seconds"
|
||||
final_message: "Cloud-init complete after \$UPTIME seconds. Provisioning running in background."
|
||||
"@
|
||||
|
||||
$cloudInitMetaData = @"
|
||||
|
|
@ -386,15 +585,22 @@ $cloudInitISO = "$VMS_PATH\$VMName\cloud-init.iso"
|
|||
|
||||
Write-Status "Creating cloud-init ISO..." "Info"
|
||||
try {
|
||||
New-CloudInitISO -OutputPath $cloudInitISO -UserData $cloudInitUserData -MetaData $cloudInitMetaData
|
||||
$isoResult = New-CloudInitISO -OutputPath $cloudInitISO -UserDataContent $cloudInitUserData -MetaDataContent $cloudInitMetaData
|
||||
if (-not (Test-Path $cloudInitISO)) {
|
||||
throw "ISO file was not created"
|
||||
}
|
||||
Write-Status "Cloud-init ISO created" "Success"
|
||||
} catch {
|
||||
Write-Status "Failed to create cloud-init ISO: $_" "Error"
|
||||
Write-Host ""
|
||||
Write-Host "Alternative: Install Windows ADK from:" -ForegroundColor Yellow
|
||||
Write-Host " https://docs.microsoft.com/en-us/windows-hardware/get-started/adk-install" -ForegroundColor Cyan
|
||||
Write-Host "Options to fix this:" -ForegroundColor Yellow
|
||||
Write-Host ""
|
||||
Write-Host "1. Install Windows ADK (includes oscdimg):" -ForegroundColor Cyan
|
||||
Write-Host " https://docs.microsoft.com/en-us/windows-hardware/get-started/adk-install" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host "2. Install genisoimage in WSL:" -ForegroundColor Cyan
|
||||
Write-Host " wsl sudo apt-get install genisoimage" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host "Or use WSL to create the ISO:" -ForegroundColor Yellow
|
||||
Write-Host " wsl genisoimage -output cloud-init.iso -volid cidata -joliet -rock user-data meta-data" -ForegroundColor Cyan
|
||||
exit 1
|
||||
}
|
||||
|
||||
|
|
@ -404,8 +610,9 @@ try {
|
|||
|
||||
Write-Status "Creating Hyper-V VM: $VMName" "Info"
|
||||
Write-Host " CPUs: $CPUs" -ForegroundColor Gray
|
||||
Write-Host " Memory: $MemoryGB GB" -ForegroundColor Gray
|
||||
Write-Host " Memory: $MemoryGB GB (dynamic)" -ForegroundColor Gray
|
||||
Write-Host " Disk: $DiskGB GB" -ForegroundColor Gray
|
||||
Write-Host " Switch: $($vmSwitch.Name)" -ForegroundColor Gray
|
||||
|
||||
# Create VM (Generation 2 for UEFI)
|
||||
$vm = New-VM -Name $VMName `
|
||||
|
|
@ -415,30 +622,44 @@ $vm = New-VM -Name $VMName `
|
|||
-SwitchName $vmSwitch.Name `
|
||||
-Path "$VMS_PATH\$VMName"
|
||||
|
||||
# Configure VM
|
||||
# Configure VM processor
|
||||
Set-VMProcessor -VM $vm -Count $CPUs
|
||||
Set-VMMemory -VM $vm -DynamicMemoryEnabled $true -MinimumBytes 2GB -MaximumBytes ($MemoryGB * 1GB)
|
||||
|
||||
# Disable Secure Boot for Ubuntu
|
||||
# Configure dynamic memory
|
||||
Set-VMMemory -VM $vm -DynamicMemoryEnabled $true `
|
||||
-MinimumBytes 2GB `
|
||||
-StartupBytes ($MemoryGB * 1GB) `
|
||||
-MaximumBytes ($MemoryGB * 1GB)
|
||||
|
||||
# Disable Secure Boot for Ubuntu (required for cloud images)
|
||||
Set-VMFirmware -VM $vm -EnableSecureBoot Off
|
||||
|
||||
# Add cloud-init ISO as DVD drive
|
||||
Add-VMDvdDrive -VM $vm -Path $cloudInitISO
|
||||
|
||||
# Configure boot order (disk first, then DVD)
|
||||
$bootOrder = @(
|
||||
(Get-VMHardDiskDrive -VM $vm),
|
||||
(Get-VMDvdDrive -VM $vm)
|
||||
)
|
||||
Set-VMFirmware -VM $vm -BootOrder $bootOrder
|
||||
# Configure boot order (DVD first for initial boot, disk for subsequent)
|
||||
$dvd = Get-VMDvdDrive -VM $vm
|
||||
$hdd = Get-VMHardDiskDrive -VM $vm
|
||||
Set-VMFirmware -VM $vm -BootOrder $dvd, $hdd
|
||||
|
||||
# Disable integration services for maximum isolation
|
||||
# (Comment these out if you want clipboard/file sharing)
|
||||
# Security: Disable integration services for maximum isolation
|
||||
Write-Status "Configuring security settings..." "Info"
|
||||
Disable-VMIntegrationService -VM $vm -Name "Guest Service Interface" -ErrorAction SilentlyContinue
|
||||
Disable-VMIntegrationService -VM $vm -Name "Heartbeat" -ErrorAction SilentlyContinue
|
||||
# Keep Time Synchronization and Shutdown for usability
|
||||
|
||||
# Enable nested virtualization (useful for Docker in VM)
|
||||
Set-VMProcessor -VM $vm -ExposeVirtualizationExtensions $true -ErrorAction SilentlyContinue
|
||||
# Enable nested virtualization (for Docker inside VM)
|
||||
try {
|
||||
Set-VMProcessor -VM $vm -ExposeVirtualizationExtensions $true
|
||||
Write-Host " Nested virtualization: enabled" -ForegroundColor Gray
|
||||
} catch {
|
||||
Write-Host " Nested virtualization: not available" -ForegroundColor Gray
|
||||
}
|
||||
|
||||
# Set automatic checkpoints off (for performance)
|
||||
Set-VM -VM $vm -AutomaticCheckpointsEnabled $false
|
||||
|
||||
Write-Status "VM created successfully" "Success"
|
||||
|
||||
# ============================================================================
|
||||
# Start VM and Wait for Provisioning
|
||||
|
|
@ -448,29 +669,25 @@ Write-Status "Starting VM..." "Info"
|
|||
Start-VM -Name $VMName
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "VM is starting. Cloud-init provisioning will take several minutes." -ForegroundColor Yellow
|
||||
Write-Host ""
|
||||
Write-Host "To monitor progress:" -ForegroundColor Cyan
|
||||
Write-Host " vmconnect localhost $VMName" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host "To connect via SSH (after provisioning):" -ForegroundColor Cyan
|
||||
Write-Host " ssh -i $sshKeyPath dev@<VM-IP>" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host "To find the VM's IP address:" -ForegroundColor Cyan
|
||||
Write-Host " (Get-VM -Name $VMName | Get-VMNetworkAdapter).IPAddresses" -ForegroundColor White
|
||||
Write-Host "VM is starting. Cloud-init will provision the development environment." -ForegroundColor Yellow
|
||||
Write-Host "This typically takes 10-15 minutes for all components." -ForegroundColor Yellow
|
||||
Write-Host ""
|
||||
|
||||
# Wait for VM to get an IP
|
||||
Write-Status "Waiting for VM to obtain IP address..." "Info"
|
||||
$maxWait = 300 # 5 minutes
|
||||
$maxWait = 180 # 3 minutes for IP
|
||||
$waited = 0
|
||||
$vmIP = $null
|
||||
|
||||
while ($waited -lt $maxWait) {
|
||||
$vmIP = (Get-VM -Name $VMName | Get-VMNetworkAdapter).IPAddresses | Where-Object { $_ -match '^\d+\.\d+\.\d+\.\d+$' } | Select-Object -First 1
|
||||
if ($vmIP) { break }
|
||||
Start-Sleep -Seconds 5
|
||||
$waited += 5
|
||||
|
||||
$vmIP = (Get-VM -Name $VMName | Get-VMNetworkAdapter).IPAddresses |
|
||||
Where-Object { $_ -match '^\d+\.\d+\.\d+\.\d+$' -and $_ -notmatch '^169\.254\.' } |
|
||||
Select-Object -First 1
|
||||
|
||||
if ($vmIP) { break }
|
||||
Write-Host "." -NoNewline
|
||||
}
|
||||
Write-Host ""
|
||||
|
|
@ -478,43 +695,77 @@ Write-Host ""
|
|||
if ($vmIP) {
|
||||
Write-Status "VM IP: $vmIP" "Success"
|
||||
|
||||
# Add to hosts file for easy access
|
||||
$hostsEntry = "$vmIP`t$VMName.local"
|
||||
# Update hosts file for easy access
|
||||
$hostsFile = "$env:SystemRoot\System32\drivers\etc\hosts"
|
||||
$existingEntry = Select-String -Path $hostsFile -Pattern "$VMName\.local" -ErrorAction SilentlyContinue
|
||||
if (-not $existingEntry) {
|
||||
Add-Content -Path $hostsFile -Value $hostsEntry
|
||||
Write-Status "Added $VMName.local to hosts file" "Info"
|
||||
$hostsEntry = "$vmIP`t$VMName.local"
|
||||
|
||||
# Remove old entry if exists
|
||||
$hostsContent = Get-Content $hostsFile -ErrorAction SilentlyContinue
|
||||
$hostsContent = $hostsContent | Where-Object { $_ -notmatch "\s+$VMName\.local\s*$" }
|
||||
|
||||
# Add new entry
|
||||
$hostsContent += $hostsEntry
|
||||
$hostsContent | Set-Content $hostsFile -Force
|
||||
Write-Status "Updated hosts file: $VMName.local -> $vmIP" "Info"
|
||||
|
||||
# Wait a moment for SSH to be ready
|
||||
Write-Status "Waiting for SSH to become available..." "Info"
|
||||
$sshReady = $false
|
||||
$sshWait = 0
|
||||
while ($sshWait -lt 120 -and -not $sshReady) {
|
||||
Start-Sleep -Seconds 5
|
||||
$sshWait += 5
|
||||
$tcpTest = Test-NetConnection -ComputerName $vmIP -Port 22 -WarningAction SilentlyContinue
|
||||
if ($tcpTest.TcpTestSucceeded) {
|
||||
$sshReady = $true
|
||||
}
|
||||
Write-Host "." -NoNewline
|
||||
}
|
||||
Write-Host ""
|
||||
|
||||
if ($sshReady) {
|
||||
Write-Status "SSH is ready!" "Success"
|
||||
} else {
|
||||
Write-Status "SSH not responding yet (may still be starting)" "Warning"
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "============================================================================" -ForegroundColor Green
|
||||
Write-Host " VM '$VMName' is starting!" -ForegroundColor Green
|
||||
Write-Host " VM '$VMName' is ready!" -ForegroundColor Green
|
||||
Write-Host "============================================================================" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
Write-Host "CONNECT:" -ForegroundColor Yellow
|
||||
Write-Host " SSH: ssh -i `"$sshKeyPath`" dev@$VMName.local" -ForegroundColor White
|
||||
Write-Host " Console: vmconnect localhost $VMName" -ForegroundColor White
|
||||
Write-Host " SSH: ssh -i $sshKeyPath dev@$vmIP" -ForegroundColor White
|
||||
Write-Host " SSH (alt): ssh -i $sshKeyPath dev@$VMName.local" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host "SERVICES (after provisioning):" -ForegroundColor Yellow
|
||||
Write-Host " PostgreSQL: psql -h $vmIP -U dev -d dev" -ForegroundColor White
|
||||
Write-Host " VNC: Start in VM with 'vnc-start', then connect to $vmIP`:5901" -ForegroundColor White
|
||||
Write-Host "MONITOR PROVISIONING:" -ForegroundColor Yellow
|
||||
Write-Host " ssh -i `"$sshKeyPath`" dev@$VMName.local tail -f /var/log/provision.log" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host "CLAUDE CODE:" -ForegroundColor Yellow
|
||||
Write-Host " ssh -i $sshKeyPath dev@$vmIP -- claude" -ForegroundColor White
|
||||
Write-Host "SERVICES (after provisioning completes):" -ForegroundColor Yellow
|
||||
Write-Host " Claude: ssh -i `"$sshKeyPath`" dev@$VMName.local claude" -ForegroundColor White
|
||||
if (-not $SkipPostgreSQL) {
|
||||
Write-Host " PostgreSQL: ssh -i `"$sshKeyPath`" dev@$VMName.local psql" -ForegroundColor White
|
||||
}
|
||||
if (-not $SkipVNC) {
|
||||
Write-Host " VNC: Connect to $vmIP`:5901 (start with: vnc-start)" -ForegroundColor White
|
||||
}
|
||||
Write-Host ""
|
||||
Write-Host "MANAGEMENT:" -ForegroundColor Yellow
|
||||
Write-Host " Stop VM: Stop-VM -Name $VMName" -ForegroundColor White
|
||||
Write-Host " Start VM: Start-VM -Name $VMName" -ForegroundColor White
|
||||
Write-Host " Delete VM: Remove-VM -Name $VMName -Force" -ForegroundColor White
|
||||
Write-Host " Stop: Stop-VM -Name $VMName" -ForegroundColor White
|
||||
Write-Host " Start: Start-VM -Name $VMName" -ForegroundColor White
|
||||
Write-Host " Delete: Stop-VM -Name $VMName -Force; Remove-VM -Name $VMName -Force" -ForegroundColor White
|
||||
Write-Host " Cleanup: Remove-Item -Path `"$VMS_PATH\$VMName`" -Recurse -Force" -ForegroundColor White
|
||||
Write-Host ""
|
||||
|
||||
} else {
|
||||
Write-Status "Could not obtain VM IP within timeout." "Warning"
|
||||
Write-Host ""
|
||||
Write-Host "The VM may still be starting. Check:" -ForegroundColor Yellow
|
||||
Write-Host "The VM may still be starting. To check:" -ForegroundColor Yellow
|
||||
Write-Host " vmconnect localhost $VMName" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host "To get IP later:" -ForegroundColor Yellow
|
||||
Write-Host " (Get-VM -Name $VMName | Get-VMNetworkAdapter).IPAddresses" -ForegroundColor White
|
||||
Write-Host ""
|
||||
}
|
||||
|
||||
Write-Host "============================================================================" -ForegroundColor Green
|
||||
|
|
|
|||
Loading…
Reference in a new issue