-
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
base: main
Are you sure you want to change the base?
Changes from 28 commits
8cf8257
8a1ce2a
5b3e54a
b72ec33
18efcf9
226cc32
3a48a66
33456f5
12c5727
fbd7f38
3dbc715
aec2483
8c9f3d7
4b21c83
949cf74
a3488cd
2f71d71
3ba1538
6c0e96e
4936277
1c1900f
9f16dde
85426d6
d50cc1e
c508c38
a4c14e2
9ed7350
f8ff45e
29505da
af70e9a
7f57213
d291652
7ae051c
58c3230
5306cf7
c758048
6109743
80d31ee
1ffcce4
b4aae2e
4aa93e9
a2e9063
cd16c12
956beaf
aafc3c9
936eb03
2411d0e
81a2cd4
59a7270
e6fbbed
62e42bf
2fa904f
ed08391
e3beabb
8398aef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,264 @@ | ||
| Import-Module -Name "$PSScriptRoot\SetUpUtilities.psm1" -Force | ||
|
|
||
| function Get-ContainerdLatestVersion { | ||
| $latestVersion = Get-LatestToolVersion -Repository "containerd/containerd" | ||
| return $latestVersion | ||
| } | ||
|
|
||
| function Install-Containerd { | ||
| param( | ||
| [string] | ||
| [ValidateNotNullOrEmpty()] | ||
| [parameter(HelpMessage = "ContainerD version to use. Default 1.7.6")] | ||
bobsira marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $Version, | ||
|
|
||
| [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 | ||
|
|
||
| if(!$Version) { | ||
| # Get default version | ||
| $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 | ||
| } | ||
| catch { | ||
| if ($_.ErrorDetails.Message -eq "Not found") { | ||
bobsira marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Throw "Containerd download failed. Invalid URL: $uri" | ||
| } | ||
|
|
||
| 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 | ||
|
|
||
| Write-Output "For containerd usage: run 'containerd -h'" | ||
| } | ||
|
|
||
| function Start-ContainerdService { | ||
| Set-Service containerd -StartupType Automatic | ||
bobsira marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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. $_" | ||
| } | ||
|
|
||
| Get-Service *containerd* | Select-Object Name, DisplayName, ServiceName, ServiceType, StartupType, Status, RequiredServices, ServicesDependedOn | ||
|
||
| } | ||
|
|
||
| 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 | ||
|
||
| Write-Host "Created $binDir" | ||
| } | ||
|
|
||
| if (!(Test-Path $confDir)) { | ||
| mkdir $confDir | ||
|
||
| 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 | ||
bobsira marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| 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 | ||
| Install-NSSM | ||
| Install-Kubelet | ||
| Set-Port | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
bobsira marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
| 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] | ||
| [ValidateNotNullOrEmpty()] | ||
| $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 | ||
| return $outputString | ||
|
|
||
| } | ||
|
|
||
| function Set-MinikubeFolderError { | ||
| if (!(Test-Path -Path c:\var\lib\minikube\certs)) { | ||
| mkdir c:\var\lib\minikube\certs | ||
| } | ||
|
|
||
| if (Test-Path -Path C:\etc\kubernetes\pki\ca.crt) { | ||
| Copy-Item C:\etc\kubernetes\pki\ca.crt -Destination C:\var\lib\Minikube\Certs | ||
| Remove-Item C:\etc\kubernetes\pki\ca.crt | ||
| } 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 | ||
| if ($hostsContent -notmatch [regex]::Escape($entry)) { | ||
| Add-Content -Path $Path -Value "$entry" -Force | ||
| } | ||
| } | ||
|
|
||
|
|
||
| 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 | ||
| 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" | ||
bobsira marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| $arch = "win64" | ||
| curl.exe -L https://k8stestinfrabinaries.blob.core.windows.net/nssm-mirror/nssm-2.24.zip -o nssm.zip | ||
| tar.exe C c:\k\ -xvf .\nssm.zip --strip-components 2 */$arch/*.exe | ||
| } | ||
|
|
||
| Export-ModuleMember -Function Install-NSSM | ||
Uh oh!
There was an error while loading. Please reload this page.