-
Notifications
You must be signed in to change notification settings - Fork 10
Author script to enable powershell remote for Windows Node #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bobsira
wants to merge
55
commits into
vrapolinario:main
Choose a base branch
from
bobsira:user/bosira/remote-powershell-script
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 45 commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
8cf8257
initial commit
bobsira 8a1ce2a
intial refactoring and seperation of concerns
bobsira 5b3e54a
more updates on the set up scripts
bobsira b72ec33
more updates to the automation scripts
bobsira 18efcf9
fixes to containerd installation
bobsira 226cc32
changes to VM specification
bobsira 3a48a66
response content cannot be parsed because the Internet Explorer engi…
bobsira 33456f5
fix to containerd start service command
bobsira 12c5727
logical change to replacement of toml conf file
bobsira fbd7f38
error handling additions
bobsira 3dbc715
updates to NSSM install
bobsira aec2483
uninstall containerd logic
bobsira 8c9f3d7
added stop containerd serv
bobsira 4b21c83
changes to import statement
bobsira 949cf74
restructed file imports
bobsira a3488cd
fix for Uninstall-ContainerTool
bobsira 2f71d71
removed containerd uninstalll logic
bobsira 3ba1538
script tp execute the process
bobsira 6c0e96e
fix on parent directory reference
bobsira 4936277
added the missing session declaration
bobsira 1c1900f
changes to flow of execution
9f16dde
removed unwanted definition
bobsira 85426d6
nssm service check
bobsira d50cc1e
more error handling code
c508c38
folder creation error handling
a4c14e2
fix initialize containerd logic
9ed7350
indentation fix in hosts file
f8ff45e
phase one remoting complete
29505da
containerd setup modification
bobsira af70e9a
feedback on remote work
bobsira 7f57213
intial authoring of auto install file
d291652
BIOS/MBR-Based Hard Disk answer file
7ae051c
setup file to test the configuration
58c3230
updates for testing
5306cf7
add auto-unattend iso file
iankingori c758048
flannel and kube-proxy networking config addition@
bobsira 6109743
Merge pull request #1 from iankingori/bob/iso-setup
bobsira 80d31ee
Merge branch 'vrapolinario:main' into user/bosira/autounattended-wind…
bobsira 1ffcce4
changed from yml to recommended yaml extension
bobsira b4aae2e
generic computer name
4aa93e9
computer name changes
a2e9063
configuring linux nodes
bobsira cd16c12
better logging to the console
bobsira 956beaf
fixing broken changes
bobsira aafc3c9
Merge branch 'user/bosira/autounattended-windows-install' of https://…
bobsira 936eb03
draft changes to merge remote and auto-install work
2411d0e
initial setup to combine the two work
81a2cd4
final setup to merge auto-install and remote config
59a7270
final changes, kalkikubes
e6fbbed
final polish
62e42bf
added SSH setup during auto-install
bobsira 2fa904f
bundled the new xml answer file into an iso
bobsira ed08391
fixed the path mismatch value
bobsira e3beabb
swapped the SSH configuration ordering
bobsira 8398aef
OpenSSH installation in the VM
bobsira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| Import-Module -Name "$PSScriptRoot\SetUpUtilities.psm1" -Force | ||
|
|
||
| function Get-ContainerdLatestVersion { | ||
| $latestVersion = Get-LatestToolVersion -Repository "containerd/containerd" | ||
| return $latestVersion | ||
| } | ||
|
|
||
| function Install-Containerd { | ||
| param( | ||
| [String] | ||
| [parameter(HelpMessage = "Path to install containerd. Defaults to ~\program files\containerd")] | ||
| $InstallPath = "$Env:ProgramFiles\containerd", | ||
|
|
||
| [String] | ||
| [parameter(HelpMessage = "Path to download files. Defaults to user's Downloads folder")] | ||
| $DownloadPath = "$HOME\Downloads" | ||
| ) | ||
|
|
||
| # Uninstall if tool exists at specified location. Requires user consent | ||
| Uninstall-ContainerTool -Tool "ContainerD" -Path $InstallPath | ||
|
|
||
| $Version = Get-ContainerdLatestVersion | ||
|
|
||
| $Version = $Version.TrimStart('v') | ||
| Write-Output "Downloading and installing Containerd v$version at $InstallPath" | ||
|
|
||
|
|
||
| # Download file from repo | ||
| $containerdTarFile = "containerd-${version}-windows-amd64.tar.gz" | ||
| try { | ||
| $Uri = "https://github.com/containerd/containerd/releases/download/v$version/$($containerdTarFile)" | ||
| Invoke-WebRequest -Uri $Uri -OutFile $DownloadPath\$containerdTarFile -Verbose | Out-Null | ||
| } | ||
| catch { | ||
| Throw "Containerd download failed. $_" | ||
| } | ||
|
|
||
|
|
||
| # Untar and install tool | ||
| $params = @{ | ||
| Feature = "containerd" | ||
| InstallPath = $InstallPath | ||
| DownloadPath = "$DownloadPath\$containerdTarFile" | ||
| EnvPath = "$InstallPath\bin" | ||
| cleanup = $true | ||
| } | ||
|
|
||
|
|
||
| Install-RequiredFeature @params | ||
|
|
||
| Write-Output "Containerd v$version successfully installed at $InstallPath" | ||
| containerd.exe -v | Out-Null | ||
|
|
||
| Write-Output "For containerd usage: run 'containerd -h'" | ||
| } | ||
|
|
||
| function Start-ContainerdService { | ||
| Set-Service containerd -StartupType Automatic | ||
| try { | ||
| Start-Service containerd | ||
|
|
||
| # Waiting for containerd to come to steady state | ||
| (Get-Service containerd -ErrorAction SilentlyContinue).WaitForStatus('Running', '00:00:30') | ||
| } | ||
| catch { | ||
| Throw "Couldn't start Containerd service. $_" | ||
| } | ||
|
|
||
| } | ||
|
|
||
| function Stop-ContainerdService { | ||
| $containerdStatus = Get-Service containerd -ErrorAction SilentlyContinue | ||
| if (!$containerdStatus) { | ||
| Write-Warning "Containerd service does not exist as an installed service." | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| Stop-Service containerd -NoWait | ||
|
|
||
| # Waiting for containerd to come to steady state | ||
| (Get-Service containerd -ErrorAction SilentlyContinue).WaitForStatus('Stopped', '00:00:30') | ||
| } | ||
| catch { | ||
| Throw "Couldn't stop Containerd service. $_" | ||
| } | ||
| } | ||
|
|
||
| function Initialize-ContainerdService { | ||
| param( | ||
| [string] | ||
| [parameter(HelpMessage = "Containerd path")] | ||
| $ContainerdPath = "$Env:ProgramFiles\containerd" | ||
| ) | ||
|
|
||
| Write-Output "Configuring the containerd service" | ||
|
|
||
| #Configure containerd service | ||
| $containerdConfigFile = "$ContainerdPath\config.toml" | ||
| $containerdDefault = containerd.exe config default | ||
| $containerdDefault | Out-File $ContainerdPath\config.toml -Encoding ascii | ||
| Write-Information -InformationAction Continue -MessageData "Review containerd configutations at $containerdConfigFile" | ||
|
|
||
| Add-MpPreference -ExclusionProcess "$ContainerdPath\containerd.exe" | ||
|
|
||
| # Review the configuration. Depending on setup you may want to adjust: | ||
| # - the sandbox_image (Kubernetes pause image) | ||
| # - cni bin_dir and conf_dir locations | ||
|
|
||
|
|
||
| # Setting Old value New Value | ||
| # bin_dir "C:\\Program Files\\containerd\\cni\\bin" "c:\\opt\\cni\\bin" | ||
| # conf_dir "C:\\Program Files\\containerd\\cni\\conf" "c:\\etc\\cni\\net.d\\" | ||
|
|
||
| # Read the content of the config.toml file | ||
| $containerdConfigContent = Get-Content -Path $containerdConfigFile -Raw | ||
|
|
||
| # Define the replacements | ||
| $replacements = @( | ||
| @{ | ||
| Find = 'bin_dir = "C:\\Program Files\\containerd\\cni\\bin"' | ||
| Replace = 'bin_dir = "c:\\opt\\cni\\bin"' | ||
| }, | ||
| @{ | ||
| Find = 'conf_dir = "C:\\Program Files\\containerd\\cni\\conf"' | ||
| Replace = 'conf_dir = "c:\\etc\\cni\\net.d\\"' | ||
| } | ||
| ) | ||
|
|
||
| # Perform the check and replacement in one loop | ||
| $replacementsMade = $false | ||
| foreach($replacement in $replacements) { | ||
| if ($containerdConfigContent -match [regex]::Escape($replacement.Find)) { | ||
| $containerdConfigContent = $containerdConfigContent -replace [regex]::Escape($replacement.Find), $replacement.Replace | ||
| $replacementsMade = $true | ||
| } | ||
| } | ||
|
|
||
| # Write the modified content back to the config.toml file if any replacements were made | ||
| if ($replacementsMade) { | ||
| $containerdConfigContent | Set-Content -Path $containerdConfigFile | ||
| # Output a message indicating the changes | ||
| # Write-Host "Changes applied to $containerdConfigFile" | ||
| } else { | ||
| # Write-Host "No changes needed in $containerdConfigFile" | ||
| } | ||
|
|
||
| # Create the folders if they do not exist | ||
| $binDir = "c:\opt\cni\bin" | ||
| $confDir = "c:\etc\cni\net.d" | ||
|
|
||
| if (!(Test-Path $binDir)) { | ||
| mkdir $binDir | Out-Null | ||
| # Write-Host "Created $binDir" | ||
| } | ||
|
|
||
| if (!(Test-Path $confDir)) { | ||
| mkdir $confDir | Out-Null | ||
| # Write-Host "Created $confDir" | ||
| } | ||
|
|
||
|
|
||
| $pathExists = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::Machine) -like "*$ContainerdPath\bin*" | ||
| if (-not $pathExists) { | ||
| # Register containerd service | ||
| Add-FeatureToPath -Feature "containerd" -Path "$ContainerdPath\bin" | ||
| } | ||
|
|
||
| # Check if the containerd service is already registered | ||
| $containerdServiceExists = Get-Service -Name "containerd" -ErrorAction SilentlyContinue | ||
| if (-not $containerdServiceExists) { | ||
| containerd.exe --register-service --log-level debug --service-name containerd --log-file "$env:TEMP\containerd.log" | ||
| if ($LASTEXITCODE -gt 0) { | ||
| Throw "Failed to register containerd service. $_" | ||
| } | ||
| } else { | ||
| Write-Host "Containerd service is already registered." | ||
bobsira marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| Get-Service *containerd* | Select-Object Name, DisplayName, ServiceName, ServiceType, StartupType, Status, RequiredServices, ServicesDependedOn | Out-Null | ||
| } | ||
|
|
||
| function Uninstall-Containerd { | ||
| param( | ||
| [string] | ||
| [parameter(HelpMessage = "Containerd path")] | ||
| $Path | ||
| ) | ||
| Write-Output "Uninstalling containerd" | ||
|
|
||
| if (!$Path) { | ||
| $Path = Get-DefaultInstallPath -Tool "containerd" | ||
| } | ||
|
|
||
| $pathItems = Get-ChildItem -Path $Path -ErrorAction SilentlyContinue | ||
| if (!$pathItems.Name.Length) { | ||
| Write-Warning "Containerd does not exist at $Path or the directory is empty" | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| Stop-ContainerdService | ||
| } | ||
| catch { | ||
| Write-Warning "$_" | ||
| } | ||
|
|
||
| # Unregister containerd service | ||
| Unregister-Containerd | ||
|
|
||
| # Delete the containerd key | ||
| $regkey = "HKLM:\SYSTEM\CurrentControlSet\Services\containerd" | ||
| Get-Item -path $regkey -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force | ||
|
|
||
| # Remove the folder where containerd service was installed | ||
| Get-Item -Path $Path -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force | ||
|
|
||
| # Remove from env path | ||
| Remove-FeatureFromPath -Feature "containerd" | ||
|
|
||
| Write-Output "Successfully uninstalled Containerd." | ||
| } | ||
| function Unregister-Containerd { | ||
| $scQueryResult = (sc.exe query containerd) | Select-String -Pattern "SERVICE_NAME: containerd" | ||
| if (!$scQueryResult) { | ||
| Write-Warning "Containerd service does not exist as an installed service." | ||
| return | ||
| } | ||
| # Unregister containerd service | ||
| containerd.exe --unregister-service | ||
| if ($LASTEXITCODE -gt 0) { | ||
| Write-Warning "Could not unregister containerd service. $_" | ||
| } | ||
| else { | ||
| Start-Sleep -Seconds 15 | ||
| } | ||
|
|
||
| # # Delete containerd service | ||
| # sc.exe delete containerd | ||
| # if ($LASTEXITCODE -gt 0) { | ||
| # Write-Warning "Could not delete containerd service. $_" | ||
| # } | ||
| } | ||
|
|
||
|
|
||
| Export-ModuleMember -Function Get-ContainerdLatestVersion | ||
| Export-ModuleMember -Function Install-Containerd | ||
| Export-ModuleMember -Function Start-ContainerdService | ||
| Export-ModuleMember -Function Stop-ContainerdService -Alias Stop-Containerd | ||
| Export-ModuleMember -Function Initialize-ContainerdService | ||
| Export-ModuleMember -Function Uninstall-Containerd | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| Import-Module -Name "$PSScriptRoot\ContainerdTools.psm1" -Force | ||
| Import-Module -Name "$PSScriptRoot\k8Tools.psm1" -Force | ||
| Import-Module -Name "$PSScriptRoot\MinikubeTools.psm1" -Force | ||
| Import-Module -Name "$PSScriptRoot\NSSMTools.psm1" -Force | ||
|
|
||
| Install-Containerd | ||
| Initialize-ContainerdService | ||
| Start-ContainerdService | ||
| Write-Output "* Containerd is installed and the service is started ..." | ||
| Install-NSSM | ||
| Write-Output "* NSSM is installed ..." | ||
| Install-Kubelet | ||
| Write-Output "* Kubelet is installed and the service is started ..." | ||
| Set-Port |
bobsira marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Define the paths to the ISO and the Autounattend.xml file | ||
| $isoPath = "D:\MinikubeWindowsContainers\SERVER_EVAL_x64FRE_en-us.iso" | ||
| $answerFilePath = "D:\MinikubeWindowsContainers\automation\autounattend.xml" | ||
|
|
||
| # Mount the ISO | ||
| $iso = Mount-DiskImage -ImagePath $isoPath -PassThru | ||
| $driveLetter = ($iso | Get-Volume).DriveLetter | ||
|
|
||
| # Copy the Autounattend.xml file to the root of the ISO | ||
| Copy-Item -Path $answerFilePath -Destination "$($driveLetter):\" -Force | ||
|
|
||
| # Start the installation | ||
| Start-Process -FilePath "$($driveLetter):\setup.exe" -ArgumentList "/unattend:$($driveLetter):\Autounattend.xml" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| function Start-Minikube { | ||
| param ( | ||
| [string] | ||
| [ValidateNotNullOrEmpty()] | ||
| $SwitchName = "External VM Switch" | ||
| ) | ||
|
|
||
| minikube start --driver=hyperv --hyperv-virtual-switch=$SwitchName --nodes=2 --cni=flannel --container-runtime=containerd | ||
| } | ||
|
|
||
| function Get-LinuxMasterNodeIP { | ||
| $IP = minikube ip | ||
| return $IP | ||
|
|
||
| } | ||
|
|
||
| function Set-Flannel { | ||
| param ( | ||
| [string] | ||
| $NodeName | ||
| ) | ||
|
|
||
| if ($NodeName) { | ||
| minikube ssh "sudo sysctl net.bridge.bridge-nf-call-iptables=1 && exit" | ||
| } else { | ||
| minikube ssh -n $NodeName "sudo sysctl net.bridge.bridge-nf-call-iptables=1 && exit" | ||
bobsira marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| function Get-JoinCommand { | ||
| param ( | ||
| [string] | ||
| [ValidateNotNullOrEmpty()] | ||
| $Version = "v1.27.3" | ||
| ) | ||
| $JoinCommand = (minikube ssh "cd /var/lib/minikube/binaries/v1.27.3/ && sudo ./kubeadm token create --print-join-command") | ||
| $outputString = $JoinCommand -replace 'kubeadm', '.\kubeadm' | ||
| $outputString += ' --cri-socket "npipe:////./pipe/containerd-containerd"' | ||
| $outputString += ' --v=5' | ||
| # Write-Host $outputString | ||
| # write this to a log file | ||
| return $outputString | ||
|
|
||
| } | ||
|
|
||
| function Set-MinikubeFolderError { | ||
| if (!(Test-Path -Path c:\var\lib\minikube\certs)) { | ||
| mkdir c:\var\lib\minikube\certs | Out-Null | ||
| } | ||
|
|
||
| if (Test-Path -Path C:\etc\kubernetes\pki\ca.crt) { | ||
| Copy-Item C:\etc\kubernetes\pki\ca.crt -Destination C:\var\lib\Minikube\Certs | Out-Null | ||
| Remove-Item C:\etc\kubernetes\pki\ca.crt | Out-Null | ||
| } else { | ||
| Write-Output "File C:\etc\kubernetes\pki\ca.crt does not exist." | ||
| } | ||
| } | ||
|
|
||
| function Add-Host { | ||
| param ( | ||
| [string] | ||
| [ValidateNotNullOrEmpty()] | ||
| $IP, | ||
| [string] | ||
| [ValidateNotNullOrEmpty()] | ||
| $Path = "C:\Windows\System32\drivers\etc\hosts" | ||
| ) | ||
|
|
||
| $entry = "`t$IP`tcontrol-plane.minikube.internal" | ||
|
|
||
| $hostsContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue | ||
| if ($hostsContent -notmatch [regex]::Escape($entry)) { | ||
| Add-Content -Path $Path -Value "$entry" -Force | Out-Null | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Export-ModuleMember -Function Start-Minikube | ||
| Export-ModuleMember -Function Get-LinuxMasterNodeIP | ||
| Export-ModuleMember -Function Set-Flannel | ||
| Export-ModuleMember -Function Get-JoinCommand | ||
| Export-ModuleMember -Function Invoke-RunCommand | ||
| Export-ModuleMember -Function Set-MinikubeFolderError | ||
| Export-ModuleMember -Function Add-Host | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| function Install-NSSM { | ||
| $nssmService = Get-WmiObject win32_service | Where-Object {$_.PathName -like '*nssm*'} | ||
| if ($nssmService) { | ||
| Write-Output "NSSM is already installed." | ||
| return | ||
| } | ||
|
|
||
| if (-not (Test-Path -Path "c:\k" -PathType Container)) { | ||
| mkdir "c:\k" | Out-Null | ||
| } | ||
| $arch = "win64" | ||
| curl.exe -L https://k8stestinfrabinaries.blob.core.windows.net/nssm-mirror/nssm-2.24.zip -o nssm.zip | Out-Null | ||
| tar.exe C c:\k\ -xvf .\nssm.zip --strip-components 2 */$arch/*.exe | Out-Null | ||
| } | ||
|
|
||
| Export-ModuleMember -Function Install-NSSM |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.