Skip to content

Commit

Permalink
Merge pull request #134 from szymonos/rfr/wsl-setup-scripts
Browse files Browse the repository at this point in the history
refactor(ps): wsl setup scripts
  • Loading branch information
szymonos authored Oct 9, 2023
2 parents 05a2c99 + c3509cf commit 728a1ac
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 164 deletions.
111 changes: 0 additions & 111 deletions wsl/wsl_distro_get.ps1

This file was deleted.

31 changes: 8 additions & 23 deletions wsl/wsl_network_fix.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,11 @@ begin {

# set location to workspace folder
Push-Location "$PSScriptRoot/.."
# check if the required functions are available, otherwise import SetupUtils module
try {
Get-Command ConvertFrom-Cfg -CommandType Function | Out-Null
Get-Command ConvertTo-Cfg -CommandType Function | Out-Null
} catch {
Import-Module (Resolve-Path './modules/SetupUtils')
}
# import SetupUtils module
Import-Module (Resolve-Path './modules/SetupUtils')

# check if distro exist
$distros = wsl/wsl_distro_get.ps1 -FromRegistry
$distros = Get-WslDistro -FromRegistry
if ($Distro -notin $distros.Name) {
Write-Warning "The specified distro does not exist ($Distro)."
exit 1
Expand All @@ -81,25 +76,15 @@ begin {
process {
# *replace wsl.conf
Write-Host 'replacing wsl.conf' -ForegroundColor DarkGreen
$wslConf = wsl.exe -d $Distro --exec cat /etc/wsl.conf 2>$null | ConvertFrom-Cfg
if ($wslConf) {
$wslConf.network = [ordered]@{ generateResolvConf = $genResolv }
} else {
$wslConf = [ordered]@{
automount = [ordered]@{
enabled = 'true'
options = '"metadata"'
mountFsTab = 'true'
}
network = [ordered]@{
$param = @{
Distro = $Distro
ConfDict = [ordered]@{
network = [ordered]@{
generateResolvConf = $genResolv
}
}
}
$wslConfStr = ConvertTo-Cfg -OrderedDict $wslConf -LineFeed
# save wsl.conf file
$cmd = "rm -f /etc/wsl.conf || true && echo '$wslConfStr' >/etc/wsl.conf"
wsl.exe -d $Distro --user root --exec bash -c $cmd
Set-WslConf @param

# *recreate resolv.conf
if (-not $Revert) {
Expand Down
28 changes: 19 additions & 9 deletions wsl/wsl_setup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ begin {
Push-Location "$PSScriptRoot/.."
# import InstallUtils for the Invoke-GhRepoClone function
Import-Module (Resolve-Path './modules/InstallUtils')
# import SetupUtils for the Set-WslConf function
Import-Module (Resolve-Path './modules/SetupUtils')

# check if repository is up to date
Write-Host "`nchecking if the repository is up to date..." -ForegroundColor Cyan
Expand All @@ -129,12 +131,10 @@ begin {
}

# *get list of distros
$lxss = wsl/wsl_distro_get.ps1 | Where-Object Name -NotMatch '^docker-desktop'
$lxss = Get-WslDistro | Where-Object Name -NotMatch '^docker-desktop'
if ($PsCmdlet.ParameterSetName -ne 'Update') {
if ($Distro -in $lxss.Name) {
$lxss = $lxss | Where-Object Name -EQ $Distro
} else {
$onlineDistros = wsl/wsl_distro_get.ps1 -Online
if ($Distro -notin $lxss.Name) {
$onlineDistros = Get-WslDistro -Online
# install online distro
if ($Distro -in $onlineDistros.Name) {
Write-Host "`nspecified distribution not found ($Distro), proceeding to install..." -ForegroundColor Cyan
Expand All @@ -143,7 +143,6 @@ begin {
Get-Service LxssManagerUser*, WSLService | Out-Null
Write-Host "`nSetting up user profile in WSL distro. Type 'exit' when finished to proceed with WSL setup!`n" -ForegroundColor Yellow
Invoke-Expression $cmd
$lxss = wsl/wsl_distro_get.ps1 -FromRegistry | Where-Object Name -EQ $Distro
} catch {
if (Test-IsAdmin) {
Invoke-Expression $cmd
Expand All @@ -160,8 +159,20 @@ begin {
exit 1
}
}
# *perform initial distro setup
# enable automount in wsl.conf
$param = @{
Distro = $Distro
ConfDict = [ordered]@{
automount = [ordered]@{
enabled = 'true'
mountFsTab = 'true'
}
}
}
Set-WslConf @param
# disable appending Windows path inside distro to fix mounting issues
$lxss = wsl/wsl_distro_get.ps1 -FromRegistry | Where-Object Name -EQ $Distro
$lxss = Get-WslDistro -FromRegistry | Where-Object Name -EQ $Distro
if ($lxss -and $lxss.Flags -ne 13) {
Set-ItemProperty -Path $lxss.PSPath -Name 'Flags' -Value 13
Write-Host "`nrestarting WSL to apply changes..." -ForegroundColor Cyan
Expand Down Expand Up @@ -450,8 +461,7 @@ process {
$builder.AppendLine('git config --global push.autoSetupRemote true') | Out-Null
if ($AddCertificate) {
# a guess, that if certs are being installed, you're behind MITM proxy without chunked transfer encoding
$builder.AppendLine('git config --global http.postBuffer 512M') | Out-Null
$builder.AppendLine('git config --global http.maxRequestBuffer 128M') | Out-Null
$builder.AppendLine('git config --global http.postBuffer 524288000') | Out-Null
}
Write-Host 'configuring git...' -ForegroundColor Cyan
wsl.exe --distribution $Distro --exec bash -c $builder.ToString().Trim()
Expand Down
32 changes: 11 additions & 21 deletions wsl/wsl_systemd.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,44 +43,34 @@ begin {

# set location to workspace folder
Push-Location "$PSScriptRoot/.."
# check if the required functions are available, otherwise import SetupUtils module
try {
Get-Command ConvertFrom-Cfg -CommandType Function | Out-Null
Get-Command ConvertTo-Cfg -CommandType Function | Out-Null
} catch {
Import-Module (Resolve-Path './modules/SetupUtils')
}
# import SetupUtils module
Import-Module (Resolve-Path './modules/SetupUtils')

# check if distro exist
$distros = wsl/wsl_distro_get.ps1 -FromRegistry
$distros = Get-WslDistro -FromRegistry
if ($Distro -notin $distros.Name) {
Write-Warning "The specified distro does not exist ($Distro)."
exit 1
}
}

process {
$wslConf = wsl.exe -d $Distro --exec cat /etc/wsl.conf 2>$null | ConvertFrom-Cfg
if ($wslConf) {
$wslConf.boot = [ordered]@{ systemd = $Systemd }
} else {
$wslConf = [ordered]@{
# *set wsl.conf
Write-Host 'replacing wsl.conf' -ForegroundColor DarkGreen
$param = @{
Distro = $Distro
ConfDict = [ordered]@{
boot = [ordered]@{
systemd = $Systemd
}
}
ShowConf = $ShowConf ? $true : $false
}
$wslConfStr = ConvertTo-Cfg -OrderedDict $wslConf -LineFeed
# save wsl.conf file
$cmd = "rm -f /etc/wsl.conf || true && echo '$wslConfStr' >/etc/wsl.conf"
wsl.exe -d $Distro --user root --exec bash -c $cmd
Set-WslConf @param
}

end {
if ($ShowConf) {
Write-Host 'wsl.conf' -ForegroundColor Magenta
wsl.exe -d $Distro --exec cat /etc/wsl.conf | Write-Host
} else {
if (-not $ShowConf) {
Write-Host "systemd $($Systemd -eq 'true' ? 'enabled' : 'disabled')"
}
}

0 comments on commit 728a1ac

Please sign in to comment.