|
# manager-devbox.ps1 |
|
# Script to check if WSL (Windows Subsystem for Linux) is installed |
|
|
|
param( |
|
[Parameter(Mandatory=$false)] |
|
[ValidateSet("help", "check", "install", "uninstall", |
|
"start", "stop", "update", "export", "import", |
|
"set", "kubectl", "run", |
|
"install-apps", |
|
"interactive")] |
|
[string]$Command = "help", |
|
|
|
[Parameter(Mandatory=$false)] |
|
[Alias("n")] |
|
[string]$Name = "devbox", |
|
|
|
[Parameter(Mandatory=$false)] |
|
[Alias("d")] |
|
[string]$Distro = "Debian", |
|
|
|
[Parameter(Mandatory=$false)] |
|
[Alias("f")] |
|
[string]$FilePath = "$env:USERPROFILE\wsl\$Name.tar", |
|
|
|
[Parameter(Mandatory=$false)] |
|
[Alias("il")] |
|
[string]$InstallLocation = "$env:USERPROFILE\wsl\$Name", |
|
|
|
[Parameter(Mandatory=$false)] |
|
[Alias("v")] |
|
[string]$VerboseOutput = $false |
|
) |
|
|
|
$DistroName = "devbox" |
|
$exitCommands = @('exit', 'return', 'quit', 'cancel') |
|
$USERNAME="devuser" |
|
$PASSWORD="devuser" # Change this |
|
$CODESERVER_PORT=8080 |
|
$SSH_PORT=2222 |
|
|
|
function Show-Help { |
|
$sriptName =".\manager-devbox.ps1" |
|
Write-Host "WSL Manager DevBox Script" -ForegroundColor Cyan |
|
Write-Host "=========================" -ForegroundColor Cyan |
|
Write-Host "" |
|
Write-Host "Usage: $sriptName -Command <command> [-Name <container_name>]" -ForegroundColor White |
|
Write-Host "" |
|
Write-Host "Parameters:" -ForegroundColor Yellow |
|
Write-Host " -Command Command to execute (check, help)" -ForegroundColor White |
|
Write-Host " -Name/-n Container name to check (default: devbox)" -ForegroundColor White |
|
Write-Host " -Distro/-d WSL distribution name (default: Debian)" -ForegroundColor White |
|
Write-Host "" |
|
Write-Host "Commands:" -ForegroundColor Yellow |
|
Write-Host " help Show this help message" -ForegroundColor White |
|
Write-Host " check Check WSL installation and devbox container status" -ForegroundColor White |
|
Write-Host " install Install and set up the devbox container (if not exists)" -ForegroundColor White |
|
Write-Host " uninstall Uninstall the devbox container" -ForegroundColor White |
|
Write-Host " start Start the devbox container" -ForegroundColor White |
|
Write-Host " stop Stop the devbox container" -ForegroundColor White |
|
Write-Host " update Update packages inside the devbox container" -ForegroundColor White |
|
Write-Host " export Export the devbox container to a tar file" -ForegroundColor White |
|
Write-Host " import Import the devbox container from a tar file" -ForegroundColor White |
|
Write-Host " install-apps Install K3s, podman, tools inside the devbox container" -ForegroundColor White |
|
Write-Host " run Run commands like kubectl, VSCode inside the devbox container" -ForegroundColor White |
|
Write-Host " Set Set up kubeconfig inside the devbox container" -ForegroundColor White |
|
Write-Host " interactive Run an interactive script in the devbox container" -ForegroundColor White |
|
|
|
Write-Host "" |
|
Write-Host "Examples:" -ForegroundColor Yellow |
|
Write-Host " $sriptName -Command check" -ForegroundColor Gray |
|
Write-Host " $sriptName check -Name mydevbox" -ForegroundColor Gray |
|
Write-Host " $sriptName check -n ubuntu-dev" -ForegroundColor Gray |
|
Write-Host " $sriptName -Command install" -ForegroundColor Gray |
|
Write-Host " $sriptName install -n mydevbox -d Ubuntu" -ForegroundColor Gray |
|
Write-Host " $sriptName -Command uninstall -n mydevbox" -ForegroundColor Gray |
|
Write-Host " $sriptName export -n mydevbox -f ./mydevbox.tar" -ForegroundColor Gray |
|
Write-Host " $sriptName export -n mydevbox --filePath ./mydevbox.tar" -ForegroundColor Gray |
|
Write-Host " $sriptName import -n mydevbox -f ./mydevbox.tar --installLocation c:/tmp" -ForegroundColor Gray |
|
Write-Host " $sriptName import -n mydevbox --filePath ./mydevbox.tar -il c:/tmp" -ForegroundColor Gray |
|
Write-Host " $sriptName interactive -Name mydevbox -v `$true" -ForegroundColor Gray |
|
} |
|
|
|
function Read-HostAtBottom { |
|
param([string]$Prompt = "Input") |
|
|
|
[Console]::SetCursorPosition(0, $Host.UI.RawUI.WindowSize.Height - 1) |
|
|
|
Write-Host (" " * $Host.UI.RawUI.WindowSize.Width) -NoNewline |
|
[Console]::SetCursorPosition(0, $Host.UI.RawUI.WindowSize.Height - 1) |
|
Write-Host "$Prompt`: " -NoNewline |
|
|
|
return [Console]::ReadLine() |
|
} |
|
|
|
function Read-HostBottomSafe { |
|
param( |
|
[string]$Prompt = "Input", |
|
[ConsoleColor]$PromptColor = [ConsoleColor]::White |
|
) |
|
|
|
try { |
|
$originalPosition = $Host.UI.RawUI.CursorPosition |
|
$windowHeight = $Host.UI.RawUI.WindowSize.Height |
|
$windowWidth = $Host.UI.RawUI.WindowSize.Width |
|
|
|
[Console]::SetCursorPosition(0, $windowHeight - 1) |
|
|
|
Write-Host (" " * $windowWidth) -NoNewline |
|
[Console]::SetCursorPosition(0, $windowHeight - 1) |
|
|
|
$Host.UI.RawUI.ForegroundColor = $PromptColor |
|
Write-Host "$Prompt`: " -NoNewline |
|
$Host.UI.RawUI.ForegroundColor = [ConsoleColor]::White |
|
|
|
$input = [Console]::ReadLine() |
|
|
|
[Console]::SetCursorPosition(0, $windowHeight - 1) |
|
Write-Host (" " * $windowWidth) -NoNewline |
|
|
|
$Host.UI.RawUI.CursorPosition = $originalPosition |
|
|
|
return $input |
|
} |
|
catch { |
|
# Fallback to regular Read-Host |
|
return Read-Host $Prompt |
|
} |
|
} |
|
|
|
# Simple Read-Host with validation |
|
function Get-YesNoInput { |
|
param([string]$Message = "Do you want to continue?") |
|
|
|
do { |
|
#$response = Read-Host "$Message (y/n)" |
|
$response = Read-HostBottomSafe -Prompt "$Message (y/n)" |
|
$response = $response.ToLower().Trim() |
|
} while ($response -notin @('y', 'n', 'yes', 'no')) |
|
|
|
return $response -in @('y', 'yes') |
|
} |
|
# One-liner with validation |
|
function Confirm-Action { |
|
param([string]$Message) |
|
|
|
$response = Read-Host "$Message (y/N)" |
|
return $response.ToLower().Trim() -in @('y', 'yes') |
|
} |
|
|
|
function Exec-BashScript{ |
|
param([string]$name, [Parameter(Mandatory=$true)][Alias("c")][string] $bashScript) |
|
if ($VerboseOutput) { |
|
Write-Host "Executing wsl -d $name -- bash -c `"$bashScript`"" -ForegroundColor Cyan |
|
Write-Host $bashScript -ForegroundColor Gray |
|
} |
|
if(!(Test-Container -name $name).Found){ |
|
Write-Host "✗ Container '$Name' is not set up" -ForegroundColor Red |
|
$global:LASTEXITCODE = -1 |
|
return |
|
} |
|
# Convert to Unix line endings |
|
$bashScript = $bashScript -replace "`r`n", "`n" |
|
wsl -d $name -- bash -c "$bashScript" |
|
} |
|
|
|
function Test-Container { |
|
param([string]$name = $DistroName, [bool]$print = $false) |
|
$found = $false |
|
$running = $false |
|
$anyDistributions = $false |
|
$distributions = wsl -l -v | ForEach-Object { $_.Trim() } | where {$_ -ne "" } 2>$null |
|
if ($distributions) { |
|
$anyDistributions = $true |
|
|
|
if($print){ |
|
foreach ($line in $distributions) { |
|
Write-Host $line -ForegroundColor White |
|
} |
|
} |
|
|
|
foreach ($line in $distributions) { |
|
$printColor = "White" |
|
if ($line.Split(" ") -contains $name) { |
|
$found = $true |
|
$printColor = "Yellow" |
|
if ($line -match "Running") { |
|
$running = $true |
|
$printColor = "Green" |
|
} |
|
break |
|
} |
|
} |
|
} |
|
@{ |
|
Found = $found |
|
Running = $running |
|
Name = $name |
|
Distributions = $anyDistributions |
|
} |
|
} |
|
|
|
function Test-WSL { |
|
param([string]$name = $DistroName) |
|
Write-Host "Checking WSL installation status..." -ForegroundColor Yellow |
|
Write-Host "" |
|
|
|
try { |
|
$found = $false |
|
$running = $false |
|
|
|
$wslVersion = wsl --version 2>$null |
|
|
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "✓ WSL is installed" -ForegroundColor Green |
|
Write-Host "" |
|
Write-Host "WSL Version Information:" -ForegroundColor Cyan |
|
Write-Host $wslVersion |
|
|
|
Write-Host "" |
|
Write-Host "Installed WSL Distributions:" -ForegroundColor Cyan |
|
$status = Test-Container -name $Name -print $true |
|
|
|
if ($status.Distributions) { |
|
if ($status.Found) { |
|
Write-Host "✓ Container '$Name' is set up" -ForegroundColor Green |
|
if ($status.Running) { |
|
Write-Host "✓ Container '$Name' is currently running" -ForegroundColor Green |
|
Write-Host "✓ To access: wsl -d $Name" -ForegroundColor Green |
|
}else { |
|
Write-Host "✗ Container '$Name' is not running" -ForegroundColor Yellow |
|
Write-Host "✓ To start: wsl -d $Name" -ForegroundColor Green |
|
} |
|
} else { |
|
Write-Host "✗ Container '$Name' is not set up" -ForegroundColor Red |
|
} |
|
} else { |
|
Write-Host "No WSL distributions found" -ForegroundColor Yellow |
|
} |
|
} else { |
|
Write-Host "✗ WSL is not installed or not available" -ForegroundColor Red |
|
Write-Host "" |
|
Write-Host "To install WSL, run the following command as Administrator:" -ForegroundColor Yellow |
|
Write-Host "wsl --install" -ForegroundColor White |
|
} |
|
} catch { |
|
Write-Host "✗ WSL is not installed or not available" -ForegroundColor Red |
|
Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red |
|
Write-Host "" |
|
Write-Host "To install WSL, run the following command as Administrator:" -ForegroundColor Yellow |
|
Write-Host "wsl --install" -ForegroundColor White |
|
} |
|
|
|
Write-Host "" |
|
|
|
@{ |
|
Found = $status.Found |
|
Running = $status.Running |
|
Name = $Name |
|
} |
|
} |
|
|
|
function Install-Container{ |
|
param([string]$name = $DistroName) |
|
|
|
if(!(Get-YesNoInput "Do you want to Install '$name' WSL?")){ |
|
Write-Host "✗ Installation cancelled by user." -ForegroundColor Yellow |
|
return |
|
} |
|
|
|
if ((Test-Container -name $name).Found) { |
|
Write-Host "Container '$name' already exists." -ForegroundColor Yellow |
|
} else { |
|
Write-Host "Creating and setting up container '$name' with distribution '$Distro'..." -ForegroundColor Yellow |
|
wsl --install -d $Distro --name $name |
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "Container '$name' created successfully." -ForegroundColor Green |
|
Write-Host "Update container using Update Parameter" -ForegroundColor Yellow |
|
} else { |
|
Write-Host "Failed to create container '$name'." -ForegroundColor Red |
|
} |
|
} |
|
} |
|
|
|
function Uninstall-Container{ |
|
param([string]$name = $DistroName) |
|
|
|
if(!(Get-YesNoInput "Do you want to Uninstall '$name' WSL?")){ |
|
Write-Host "✗ Uninstallation cancelled by user." -ForegroundColor Yellow |
|
return |
|
} |
|
|
|
if(!(Test-Container -name $name).Found){ |
|
Write-Host "✗ Container '$Name' is not set up" -ForegroundColor Red |
|
return |
|
} |
|
if (!(Confirm-Action "Are you sure you want to uninstall the container '$name'? This action cannot be undone.")) { |
|
Write-Host "✗ Uninstallation cancelled by user." -ForegroundColor Yellow |
|
return |
|
} |
|
Write-Host "Uninstalling container '$name'..." -ForegroundColor Yellow |
|
wsl --unregister $name |
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "✓ Container '$name' uninstalled successfully." -ForegroundColor Green |
|
} else { |
|
Write-Host "✗ Failed to uninstall container '$name'." -ForegroundColor Red |
|
} |
|
} |
|
|
|
function Start-Container{ |
|
param([string]$name = $DistroName) |
|
Write-Host "🚀 Starting WSL container '$name'..." -ForegroundColor Yellow |
|
$bashScript = "echo '✓ WSL container '$name' is running.'; exit 0" |
|
Exec-BashScript -name $name -c $bashScript |
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "✓ Container '$name' started successfully." -ForegroundColor Green |
|
} else { |
|
Write-Host "✗ Failed to start container '$name'." -ForegroundColor Red |
|
} |
|
} |
|
|
|
function Stop-Container{ |
|
param([string]$name = $DistroName) |
|
|
|
if(!(Test-Container -name $name).Found){ |
|
Write-Host "✗ Container '$Name' is not set up" -ForegroundColor Red |
|
return |
|
} |
|
|
|
Write-Host "🛑 Stopping WSL container '$name'..." -ForegroundColor Yellow |
|
wsl --terminate $name |
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "✓ Container '$name' stopped successfully." -ForegroundColor Green |
|
} else { |
|
Write-Host "✗ Failed to stop container '$name'." -ForegroundColor Red |
|
} |
|
} |
|
|
|
function Update-Container{ |
|
param([string]$name = $DistroName) |
|
|
|
Write-Host "🚀 Updating container '$name'..." -ForegroundColor Yellow |
|
# Update dependencies |
|
Exec-BashScript -name $name -c "sudo apt update && sudo apt upgrade -y" |
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "✓ Container '$name' updated successfully." -ForegroundColor Green |
|
} else { |
|
Write-Host "✗ Failed to update container '$name'." -ForegroundColor Red |
|
} |
|
} |
|
|
|
function Export-Container{ |
|
param([string]$name = $DistroName, [string]$file = "$name.tar") |
|
|
|
if(!(Test-Container -name $name).Found){ |
|
Write-Host "✗ Container '$Name' is not set up" -ForegroundColor Red |
|
return |
|
} |
|
Write-Host "📦 Exporting container '$name' to file '$file'..." -ForegroundColor Yellow |
|
wsl --export $name $file |
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "✓ Container '$name' exported successfully to '$file'." -ForegroundColor Green |
|
} else { |
|
Write-Host "✗ Failed to export container '$name'." -ForegroundColor Red |
|
} |
|
} |
|
|
|
function Import-Container{ |
|
param([string]$name = $DistroName, [string]$location, [string]$file = "$name.tar") |
|
|
|
if((Test-Container -name $name).Found){ |
|
Write-Host "✗ Container '$Name' already exists" -ForegroundColor Red |
|
return |
|
} |
|
if(!(Test-Path $file)){ |
|
Write-Host "✗ File '$file' does not exist" -ForegroundColor Red |
|
return |
|
} |
|
if(!(Test-Path $location)){ |
|
Write-Host "✗ InstallLocation '$location' does not exist" -ForegroundColor Red |
|
return |
|
} |
|
Write-Host "📦 Importing container '$name' from file '$file'..." -ForegroundColor Yellow |
|
wsl --import $name "$location\$name" $file --version 2 |
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "✓ Container '$name' imported successfully from '$file'." -ForegroundColor Green |
|
} else { |
|
Write-Host "✗ Failed to import container '$name'." -ForegroundColor Red |
|
} |
|
} |
|
|
|
function Install-Tools{ |
|
param([string]$name = $DistroName) |
|
|
|
Write-Host "🚀 Installing tools in container '$name'..." -ForegroundColor Yellow |
|
# install dependencies |
|
Exec-BashScript -name $name -c "sudo apt install curl wget git unzip gnupg ca-certificates lsb-release -y" |
|
Exec-BashScript -name $name -c "sudo apt install software-properties-common -y" |
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "✓ Container '$name' updated successfully." -ForegroundColor Green |
|
} else { |
|
Write-Host "✗ Failed to update container '$name'." -ForegroundColor Red |
|
} |
|
} |
|
|
|
function Install-Podman { |
|
param([string]$name = $DistroName) |
|
|
|
# --- Install Podman --- |
|
Write-Host "🍶 Installing Podman in container '$name'..." |
|
|
|
# --- Install podman-compose --- |
|
Write-Host "📦 Installing podman-compose in container '$name'..." |
|
|
|
# --- Install Buildah --- |
|
Write-Host "🔨 Installing Buildah in container '$name'..." |
|
|
|
Exec-BashScript -name $name -c "sudo apt install -y podman podman-compose buildah" |
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "✓ Podman, podman-compose, and Buildah installed successfully in container '$name'." -ForegroundColor Green |
|
} else { |
|
Write-Host "✗ Failed to install Podman, podman-compose, or Buildah in container '$name'." -ForegroundColor Red |
|
} |
|
} |
|
|
|
function Install-K3s{ |
|
param([string]$name = $DistroName) |
|
|
|
Write-Host "🐳 Installing K3s in container '$name'..." -ForegroundColor Yellow |
|
Exec-BashScript -name $name -c "curl -sfL https://get.k3s.io | sh -" |
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "✓ K3s installed successfully in container '$name'." -ForegroundColor Green |
|
if((Get-YesNoInput "Do you wanna Enable systemd in WSL?(optional but useful for k3s)")){ |
|
# Enable systemd in WSL (optional but useful for k3s) |
|
Write-Host "🔧 Enabling systemd..." |
|
Exec-BashScript -name $name -c "echo -e '[boot]\nsystemd=true' | sudo tee /etc/wsl.conf" |
|
Write-Host "🔧 Restarting WSL..." |
|
wsl --shutdown |
|
Start-Container -name $name |
|
Write-Host "✓ systemd enabled and WSL restarted. Please start the container again." -ForegroundColor Green |
|
} |
|
} else { |
|
Write-Host "✗ Failed to install K3s in container '$name'." -ForegroundColor Red |
|
} |
|
} |
|
function Install-VSCode{ |
|
param([string]$name = $DistroName) |
|
|
|
Write-Host "🖥️ Installing code-server (VS Code in browser) in container '$name'..." -ForegroundColor Yellow |
|
Exec-BashScript -name $name -c "curl -fsSL https://code-server.dev/install.sh | sh" |
|
if ($LASTEXITCODE -eq 0){ |
|
Write-Host "✓ code-server installed successfully in container '$name'." -ForegroundColor Green |
|
} else { |
|
Write-Host "✗ Failed to install code-server in container '$name'." -ForegroundColor Red |
|
} |
|
} |
|
|
|
function Install-Apps{ |
|
param([string]$name = $DistroName) |
|
|
|
$Message = "type K3s, Podman, VSCode or tools" |
|
|
|
do { |
|
$cmd = Read-Host "$Message" |
|
$cmd = $cmd.ToLower().Trim() |
|
if ($cmd -in $exitCommands) { |
|
Write-Host "✗ Exiting Install Apps command prompt." -ForegroundColor Yellow |
|
break |
|
} |
|
switch ($cmd) { |
|
"k3s" { |
|
Install-K3s -name $name |
|
} |
|
"podman" { |
|
Install-Podman -name $name |
|
} |
|
"tools" { |
|
Install-Tools -name $name |
|
} |
|
"vscode" { |
|
Install-VSCode -name $name |
|
} |
|
default { |
|
Write-Host "✗ Invalid command: $cmd" -ForegroundColor Red |
|
Write-Host "Available commands: K3s, Podman, tools" -ForegroundColor Yellow} |
|
} |
|
} while ($response -notin $exitCommands) |
|
} |
|
|
|
function Set-User{ |
|
param([string]$name = $DistroName) |
|
|
|
$bashScript = @" |
|
#!/bin/bash |
|
if id -u $USERNAME >/dev/null 2>&1; then |
|
echo "User $USERNAME already exists" |
|
else |
|
echo "Creating user $USERNAME" |
|
sudo adduser --disabled-password --gecos "" $USERNAME |
|
echo "${USERNAME}:${PASSWORD}" | sudo chpasswd |
|
sudo usermod -aG sudo $USERNAME |
|
echo "User $USERNAME created and added to sudo group" |
|
fi |
|
"@ |
|
|
|
Exec-BashScript -name $name -c $bashScript |
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "✓ User '$USERNAME' set up successfully in container '$name'." -ForegroundColor Green |
|
} else { |
|
Write-Host "✗ Failed to set up user '$USERNAME' in container '$name'." -ForegroundColor Red |
|
} |
|
} |
|
|
|
function Set-Kubeconfig{ |
|
param([string]$name = $DistroName) |
|
|
|
$bashScript = @" |
|
mkdir -p ~/.kube |
|
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config |
|
sudo chown `$USER:`$USER ~/.kube/config |
|
export KUBECONFIG=`$HOME/.kube/config |
|
echo 'export KUBECONFIG=`$HOME/.kube/config' >> ~/.bashrc |
|
source ~/.bashrc |
|
"@ |
|
|
|
Exec-BashScript -name $name -c $bashScript |
|
|
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "✓ Kubeconfig set up successfully in container '$name'." -ForegroundColor Green |
|
} else { |
|
Write-Host "✗ Failed to set up Kubeconfig in container '$name'." -ForegroundColor Red |
|
} |
|
} |
|
|
|
function Set-Commands{ |
|
param([string]$name = $DistroName) |
|
|
|
$Message = "type kubeconfig or user" |
|
|
|
do { |
|
$cmd = Read-Host "$Message" |
|
$cmd = $cmd.ToLower().Trim() |
|
if ($cmd -in $exitCommands) { |
|
Write-Host "✗ Exiting Install Apps command prompt." -ForegroundColor Yellow |
|
break |
|
} |
|
switch ($cmd) { |
|
"kubeconfig" { |
|
Set-Kubeconfig -name $name |
|
} |
|
"user" { |
|
Set-User -name $name |
|
} |
|
default { |
|
Write-Host "✗ Invalid command: $cmd" -ForegroundColor Red |
|
Write-Host "Available commands: kubeconfig, user" -ForegroundColor Yellow} |
|
} |
|
} while ($response -notin $exitCommands) |
|
} |
|
|
|
function Run-kubectl{ |
|
param([string]$name = $DistroName) |
|
if(!(Test-Container -name $name).Found){ |
|
Write-Host "✗ Container '$Name' is not set up" -ForegroundColor Red |
|
return |
|
} |
|
$Message = "type a kubectl get nods, get pods, get scv command" |
|
|
|
do { |
|
$cmd = Read-Host "$Message" |
|
$cmd = $cmd.ToLower().Trim() |
|
if ($cmd -in $exitCommands) { |
|
Write-Host "✗ Exiting kubectl command prompt." -ForegroundColor Yellow |
|
break |
|
} |
|
wsl -d "$Name" -- bash -c "kubectl $cmd" |
|
} while ($response -notin $exitCommands) |
|
|
|
|
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "✓ kubectl command executed successfully in container '$name'." -ForegroundColor Green |
|
} else { |
|
Write-Host "✗ Failed to execute kubectl command in container '$name'." -ForegroundColor Red |
|
} |
|
} |
|
|
|
function Run-VSCode{ |
|
param([string]$name = $DistroName) |
|
if(!(Test-Container -name $name).Found){ |
|
Write-Host "✗ Container '$Name' is not set up" -ForegroundColor Red |
|
return |
|
} |
|
Write-Host "🚀 Starting code-server (VS Code in browser) in container '$name'..." -ForegroundColor Yellow |
|
wsl -d "$Name" -- bash -c "code-server --bind-addr 0.0.0.0:$CODESERVER_PORT" |
|
if ($LASTEXITCODE -eq 0) { |
|
Write-Host "✓ code-server started successfully in container '$name'." -ForegroundColor Green |
|
Write-Host "✓ Access it at http://localhost:8080" -ForegroundColor Green |
|
} else { |
|
Write-Host "✗ Failed to start code-server in container '$name'." -ForegroundColor Red |
|
} |
|
# To have systemd start code-server now and restart on boot: |
|
# sudo systemctl enable --now code-server@$USER |
|
# Or, if you don't want/need a background service you can run: |
|
# code-server |
|
} |
|
|
|
function Run-Interactive (){ |
|
param([string]$name, [string]$distro, [string]$filePath, [string]$installLocation ) |
|
$Message = "type a help, check, install, uninstall, start, update, export, import, stop, install-apps, kubectl or set-Kubeconfig command" |
|
|
|
do { |
|
$cmd = Read-Host "$Message" |
|
$cmd = $cmd.ToLower().Trim() |
|
if ($cmd -in $exitCommands) { |
|
Write-Host "✗ Exiting Run-Interactive command prompt." -ForegroundColor Yellow |
|
break |
|
} |
|
Run-Main -command $cmd -name $name -distro $distro -filePath $filePath -installLocation $installLocation |
|
} while ($response -notin $exitCommands) |
|
} |
|
|
|
|
|
function Run-Commands{ |
|
param([string]$name = $DistroName) |
|
|
|
if(!(Test-Container -name $name).Found){ |
|
Write-Host "✗ Container '$Name' is not set up" -ForegroundColor Red |
|
return |
|
} |
|
|
|
$Message = "type VSCode or kubectl" |
|
|
|
do { |
|
$cmd = Read-Host "$Message" |
|
$cmd = $cmd.ToLower().Trim() |
|
if ($cmd -in $exitCommands) { |
|
Write-Host "✗ Exiting Install Apps command prompt." -ForegroundColor Yellow |
|
break |
|
} |
|
switch ($cmd) { |
|
"kubectl" { |
|
Run-kubectl -name $name |
|
} |
|
"vscode" { |
|
Run-VSCode -name $name |
|
} |
|
default { |
|
Write-Host "✗ Invalid command: $cmd" -ForegroundColor Red |
|
Write-Host "Available commands: kubectl, VSCode" -ForegroundColor Yellow} |
|
} |
|
} while ($response -notin $exitCommands) |
|
} |
|
|
|
function Run-Main(){ |
|
param([string]$command, [string]$name, [string]$distro, [string]$filePath, [string]$installLocation ) |
|
# Main script logic |
|
$status |
|
switch ($command) { |
|
"check" { |
|
$status = Test-WSL -name $name |
|
} |
|
"help" { |
|
Show-Help |
|
} |
|
"install" { |
|
Install-Container -name $name |
|
} |
|
"uninstall"{ |
|
Uninstall-Container -name $name |
|
} |
|
"start" { |
|
Start-Container -name $name |
|
} |
|
"update" { |
|
Update-Container -name $name |
|
} |
|
"export" { |
|
Export-Container -name $name -file $filePath |
|
} |
|
"import" { |
|
Import-Container -name $name -location $installLocation -file $filePath |
|
} |
|
"stop" { |
|
Stop-Container -name $name |
|
} |
|
"install-apps"{ |
|
Install-Apps -name $name |
|
} |
|
"set"{ |
|
Set-Commands -name $name |
|
} |
|
"run"{ |
|
Run-Commands -name $name |
|
} |
|
default { |
|
Show-Help |
|
} |
|
} |
|
} |
|
|
|
if ($Command -eq "help" -or $Command -eq "") { |
|
Show-Help |
|
exit |
|
} elseif ($Command -notin @("check", "install", "uninstall", "start", "stop", "update", "export", "import", "install-apps", "set-Kubeconfig", "kubectl", "interactive")) { |
|
Write-Host "✗ Invalid command: $Command" -ForegroundColor Red |
|
Show-Help |
|
exit |
|
} elseif ($Command -in @("export", "import")) { |
|
if (-not $FilePath) { |
|
Write-Host "✗ FilePath is required for command '$Command'" -ForegroundColor Red |
|
Show-Help |
|
exit |
|
} elseif ($Command -eq "import" -and -not $InstallLocation) { |
|
Write-Host "✗ InstallLocation is required for command 'import'" -ForegroundColor Red |
|
Show-Help |
|
exit |
|
} |
|
} elseif($Command -eq "interactive") { |
|
Run-Interactive -name $Name -distro $Distro -filePath $FilePath -installLocation $InstallLocation |
|
exit |
|
} elseif($Command -eq "kubectl") { |
|
Run-kubectl -name $Name |
|
exit |
|
} |
|
# Run the main function with parsed parameters |
|
Run-Main -command $Command -name $Name -distro $Distro -filePath $FilePath -installLocation $InstallLocation |
|
|