Skip to content

Commit

Permalink
Fix #35: reformat bash commands to work with pwsh 7.3.*
Browse files Browse the repository at this point in the history
  • Loading branch information
wikiped committed Feb 14, 2023
1 parent 59c078d commit a697cb6
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 89 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

[Overview](#overview)

[Prerequisites](#prerequisites)

[How to get this module?](#how-to-get-this-module)

[Where the module is installed?](#where-the-module-is-installed)
Expand Down Expand Up @@ -81,22 +83,33 @@ In other words what WSL should have been doing out-of-the-box.

---

## How to get this module
## Prerequisites

1. Windows 10.

### Prerequisites
> Please be aware that the module is not being tested to work on Windows 11. Which does not mean it will not work, but since Windows 11 introduced new WSL features this might affect how the module operates.
1. PowerShell 7.1+.

[How to install Powershell Core.](https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.1)

1. [WSL 2](https://github.com/microsoft/WSL) (This module has not been tested with WSL 1)
1. [WSL 2](https://github.com/microsoft/WSL) enabled / installed.

> This module is not being tested to work with WSL 1
1. Administrative access on the windows machine, where the module will be used.

1. Ubuntu or Fedora family OS as WSL instance.

> Other versions might work, but the module is being tested only with the above two.
1. Unicode UTF-8 support [enabled](#how-to-enable-unicode-utf-8-support-on-windows) in Windows Settings.

---

## How to get this module


To download and copy the module to Modules folder of Powershell profile for Current User run the following commands from Powershell prompt:

### Universal web installer
Expand Down Expand Up @@ -134,7 +147,7 @@ Rename-Item -Path 'Wsl-IpHandler-master' -NewName 'Wsl-IpHandler'

After executing above commands in [How to get this module](#how-to-get-this-module) the module is installed in a Powershell profile directory for the current user.

Run `Split-Path $Profile` to see location of this profile directory.
Run `Split-Path $Profile` to see location of the profile directory.

When `Import-Module SomeModule` command is executed, Powershell looks for `SomeModule` in this directory (among others).

Expand Down Expand Up @@ -275,7 +288,7 @@ In all other cases when `wsl` is executed to open a terminal (i.e. interactive s
> A workaround to ensure execution of profile script in non-interactive Bash session is to run command like this (from Powershell):
```powershell
wsl.exe env BASH_ENV=/etc/profile bash -c `"ping windows.host`"
wsl.exe -e env BASH_ENV=/etc/profile bash -c 'ping windows.host'
```

---
Expand Down
14 changes: 11 additions & 3 deletions Scripts/Bash/install-wsl-iphandler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ echo_verbose "Bash Installing Wsl-IpHandler..."

# Prcess Incoming Arguments
echo_debug "Starting '$0' with User ID: $EUID" ${LINENO}
echo_debug "$0 Processing Incoming Arguments:" ${LINENO}
echo_debug "Incoming Arguments:" ${LINENO}
echo_debug "$*" ${LINENO}
echo_debug "Current Directory: '$(pwd)'" ${LINENO}
echo_debug "DEBUG=${DEBUG:-}" ${LINENO}
echo_debug "VERBOSE=${VERBOSE:-}" ${LINENO}
echo_debug "arg1=${1}" ${LINENO}
echo_debug "arg2=${2}" ${LINENO}
echo_debug "arg3=${3}" ${LINENO}
echo_debug "arg4=${4}" ${LINENO}
echo_debug "arg5=${5}" ${LINENO}
echo_debug "arg6=${6}" ${LINENO}
echo_debug "arg7=${7}" ${LINENO}

script_source=$(wslpath -u "$1" 2>/dev/null)
echo_debug "script_source: $script_source" ${LINENO}

script_source="$(wslpath "$1" 2>/dev/null)"
test -f "${script_source}" || error ${LINENO} "File Not Found: $script_source"

readonly script_target="${2%/}/${script_source##*/}" # Use file name only: ##*/ -> removes path
Expand All @@ -40,7 +49,6 @@ readonly windows_host=$5
readonly wsl_host=$6
readonly wsl_static_ip_or_offset=$7

echo_debug "script_source: $script_source" ${LINENO}
echo_debug "script_target: $script_target" ${LINENO}
echo_debug "config: $config" ${LINENO}
echo_debug "windows_host: $windows_host" ${LINENO}
Expand Down
16 changes: 8 additions & 8 deletions Scripts/Powershell/MigrateConfigFiles.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,22 @@ function Invoke-MigrateWslInstanceConfig {
$WslInstanceName | ForEach-Object {
try {
$wslInstanceWasRunning = Test-WslInstanceIsRunning $_
$modConfExists = (wsl.exe -d $_ test -f $modConf && 1) -eq 1
$modConfExists = (wsl.exe -d $_ -e test -f $modConf && 1) -eq 1
Write-Debug "Module Config '$modConf' exists: $modConfExists"
if (-not $modConfExists) {
Write-Debug "$wslConf before:`n$(wsl.exe -d $_ cat $wslConf)"
Write-Debug "$wslConf before:`n$(wsl.exe -d $_ -e cat $wslConf)"
foreach ($option in @('static_ip', 'ip_offset', 'windows_host', 'wsl_host')) {
Write-Debug "Migrating option: $option"
$line = wsl.exe -d $_ grep -P "^${option}\\s*=.*$" $wslConf
$line = wsl.exe -d $_ -e grep -P "^${option}\\s*=.*$" $wslConf
if ($line) {
Write-Debug "Line with ${option}: '$line'"
wsl.exe -d $_ echo $line `>> $modConf
wsl.exe -d $_ sed -i "/${line}/d" $wslConf
wsl.exe -d $_ -e echo $line `>> $modConf
wsl.exe -d $_ -e sed -i "/${line}/d" $wslConf
}
}
Write-Debug "$wslConf after:`n$(wsl.exe -d $_ cat $wslConf)"
if ((wsl.exe -d $_ test -f $modConf && 1) -eq 1) {
Write-Debug "$modConf after:`n$(wsl.exe -d $_ cat $modConf)"
Write-Debug "$wslConf after:`n$(wsl.exe -d $_ -e cat $wslConf)"
if ((wsl.exe -d $_ -e test -f $modConf && 1) -eq 1) {
Write-Debug "$modConf after:`n$(wsl.exe -d $_ -e cat $modConf)"
}
else {
Write-Error "Error migrating config file: '$modConf' - file was not created."
Expand Down
36 changes: 18 additions & 18 deletions Tests/Install-WslIpHandler-Static.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,52 +36,52 @@ Describe 'Install & Uninstall WSL IP Handler' -ForEach @(
$actualPrefix | Should -Be $PrefixLength
}
It ' Assigned Static IP address matches <WslInstanceIpAddress>' {
$command = '"hostname -I"'
$actualWslIpAddress = Invoke-WslExe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'hostname -I'
$actualWslIpAddress = Invoke-WslExe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$actualWslIpAddress.Trim() | Should -Be $WslInstanceIpAddress
}
It ' Gateway IP address matches <GatewayIpAddress>' {
$command = "`"grep -oP 'nameserver \K.*' /etc/resolv.conf`""
$actualGateway = Invoke-WslExe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = "grep -oP 'nameserver \K.*' /etc/resolv.conf"
$actualGateway = Invoke-WslExe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$actualGateway | Should -Be $GatewayIpAddress
}
Context ' Uninstall-WslIpHandler' {
BeforeAll {
Uninstall-WslIpHandler -WslInstanceName $WslInstanceName
}
It ' Removed wsl-iphandler.sh from <WslInstanceName>' {
$command = '"test -f /usr/local/bin/wsl-iphandler.sh && echo true || echo false"'
$fileExists = wsl.exe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'test -f /usr/local/bin/wsl-iphandler.sh && echo true || echo false'
$fileExists = wsl.exe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$fileExists | Should -Be 'false'
}
It ' Removed run-wsl-iphandler.sh from <WslInstanceName>' {
$command = '"test -f /etc/profile.d/run-wsl-iphandler.sh && echo true || echo false"'
$fileExists = wsl.exe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'test -f /etc/profile.d/run-wsl-iphandler.sh && echo true || echo false'
$fileExists = wsl.exe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$fileExists | Should -Be 'false'
}
It ' Removed /etc/sudoers.d/wsl-iphandler from <WslInstanceName>' {
$command = '"test -f /etc/sudoers.d/wsl-iphandler && echo true || echo false"'
$fileExists = wsl.exe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'test -f /etc/sudoers.d/wsl-iphandler && echo true || echo false'
$fileExists = wsl.exe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$fileExists | Should -Be 'false'
}
It ' Removed windows_host from /etc/wsl.conf @ <WslInstanceName>' {
$command = '"grep -Pqs "^\s*windows_host\s*=*" "/etc/wsl.conf" &>/dev/null && echo true || echo false"'
$fileExists = wsl.exe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'grep -Pqs "^\s*windows_host\s*=*" /etc/wsl.conf &>/dev/null && echo true || echo false'
$fileExists = wsl.exe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$fileExists | Should -Be 'false'
}
It ' Removed wsl_host from /etc/wsl.conf @ <WslInstanceName>' {
$command = '"grep -Pqs "^\s*wsl_host\s*=*" "/etc/wsl.conf" &>/dev/null && echo true || echo false"'
$fileExists = wsl.exe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'grep -Pqs "^\s*wsl_host\s*=*" /etc/wsl.conf &>/dev/null && echo true || echo false'
$fileExists = wsl.exe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$fileExists | Should -Be 'false'
}
It ' Removed static_ip from /etc/wsl.conf @ <WslInstanceName>' {
$command = '"grep -Pqs "^\s*static_ip\s*=*" "/etc/wsl.conf" &>/dev/null && echo true || echo false"'
$fileExists = wsl.exe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'grep -Pqs "^\s*static_ip\s*=*" /etc/wsl.conf &>/dev/null && echo true || echo false'
$fileExists = wsl.exe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$fileExists | Should -Be 'false'
}
It ' Removed ip_offset from /etc/wsl.conf @ <WslInstanceName>' {
$command = '"grep -Pqs "^\s*ip_offset\s*=*" "/etc/wsl.conf" &>/dev/null && echo true || echo false"'
$fileExists = wsl.exe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'grep -Pqs "^\s*ip_offset\s*=*" /etc/wsl.conf &>/dev/null && echo true || echo false'
$fileExists = wsl.exe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$fileExists | Should -Be 'false'
}
}
Expand Down
28 changes: 14 additions & 14 deletions Tests/Uninstall-WslIpHandler-Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,38 @@ Describe 'Uninstall WSL IP Handler' -ForEach @(
Uninstall-WslIpHandler -WslInstanceName $WslInstanceName
}
It ' Removed wsl-iphandler.sh from <WslInstanceName>' {
$command = '"test -f /usr/local/bin/wsl-iphandler.sh && echo true || echo false"'
$fileExists = wsl.exe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'test -f /usr/local/bin/wsl-iphandler.sh && echo true || echo false'
$fileExists = wsl.exe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$fileExists | Should -Be 'false'
}
It ' Removed run-wsl-iphandler.sh from <WslInstanceName>' {
$command = '"test -f /etc/profile.d/run-wsl-iphandler.sh && echo true || echo false"'
$fileExists = wsl.exe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'test -f /etc/profile.d/run-wsl-iphandler.sh && echo true || echo false'
$fileExists = wsl.exe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$fileExists | Should -Be 'false'
}
It ' Removed /etc/sudoers.d/wsl-iphandler from <WslInstanceName>' {
$command = '"test -f /etc/sudoers.d/wsl-iphandler && echo true || echo false"'
$fileExists = wsl.exe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'test -f /etc/sudoers.d/wsl-iphandler && echo true || echo false'
$fileExists = wsl.exe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$fileExists | Should -Be 'false'
}
It ' Removed windows_host from /etc/wsl.conf @ <WslInstanceName>' {
$command = '"grep -Pqs "^\s*windows_host\s*=*" "/etc/wsl.conf" &>/dev/null && echo true || echo false"'
$fileExists = wsl.exe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'grep -Pqs "^\s*windows_host\s*=*" /etc/wsl.conf &>/dev/null && echo true || echo false'
$fileExists = wsl.exe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$fileExists | Should -Be 'false'
}
It ' Removed wsl_host from /etc/wsl.conf @ <WslInstanceName>' {
$command = '"grep -Pqs "^\s*wsl_host\s*=*" "/etc/wsl.conf" &>/dev/null && echo true || echo false"'
$fileExists = wsl.exe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'grep -Pqs "^\s*wsl_host\s*=*" /etc/wsl.conf &>/dev/null && echo true || echo false'
$fileExists = wsl.exe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$fileExists | Should -Be 'false'
}
It ' Removed static_ip from /etc/wsl.conf @ <WslInstanceName>' {
$command = '"grep -Pqs "^\s*static_ip\s*=*" "/etc/wsl.conf" &>/dev/null && echo true || echo false"'
$fileExists = wsl.exe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'grep -Pqs "^\s*static_ip\s*=*" /etc/wsl.conf &>/dev/null && echo true || echo false'
$fileExists = wsl.exe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$fileExists | Should -Be 'false'
}
It ' Removed ip_offset from /etc/wsl.conf @ <WslInstanceName>' {
$command = '"grep -Pqs "^\s*ip_offset\s*=*" "/etc/wsl.conf" &>/dev/null && echo true || echo false"'
$fileExists = wsl.exe -d $WslInstanceName env BASH_ENV=/etc/profile bash -c $command
$command = 'grep -Pqs "^\s*ip_offset\s*=*" /etc/wsl.conf &>/dev/null && echo true || echo false'
$fileExists = wsl.exe -d $WslInstanceName -e env BASH_ENV=/etc/profile bash -c $command
$fileExists | Should -Be 'false'
}
}
Expand Down
Loading

0 comments on commit a697cb6

Please sign in to comment.