HLK-Driver.psm1

Import-Module $PSScriptRoot\TurnKeySdn.psd1 -Force
Import-Module $PSScriptRoot\ConfigManager.psm1 -Force

# Driver Module for HLK + TurnKey Installation

# addressPrefix should be a /16 prefix
# 192.168.1.0/24 > management network
# 192.168.2.0/24 > HNV PA network
# 192.168.3.0/24 > VIP network

$script:LogFileName = "HLK-Deployment.log"
$script

# Init-HLKSDNWorkload -NetworkPrefix "173.20.0.1/16" -VLANID 8 -NCNodeCount 1 -CSVFolderPath C:\ClusterStorage\SdnSpace\ -VHDPath C:\ClusterStorage\SdnSpace\VHDs\ -VHDFileName 23h2.vhdx -SdnSwitchname sdnswitch -InternetSwitchName "corp" -DomainName CFDEV.NTTEST.MICROSOFT.COM -DomainUserName wolfpack -DomainPassword wolfpack -WorkloadUserName administrator -WorkloadPassword administrator
function Init-HLKSDNWorkload {
    
    param(
        [parameter(Mandatory=$true)][string] $NetworkPrefix,
        [parameter(Mandatory=$true)][int] $VLANID,
        [parameter(Mandatory=$true)][int] $NCNodeCount,
        [parameter(Mandatory=$true)][string] $CSVFolderPath,
        [parameter(Mandatory=$true)][string] $VHDPath,
        [parameter(Mandatory=$true)][string] $VHDFileName,
        [parameter(Mandatory=$true)][string] $SdnSwitchname,
        [parameter(Mandatory=$true)][string] $InternetSwitchName,

        [parameter(Mandatory=$true)][string] $DomainName,
        [parameter(Mandatory=$true)][string] $DomainUserName,
        [parameter(Mandatory=$true)][string] $DomainPassword,

        [parameter(Mandatory=$false)][string] $WorkloadUserName,
        [parameter(Mandatory=$false)][string] $WorkloadPassword,

        [parameter(Mandatory=$false)][string[]] $ManagementDNSIpAddresses = @()

    ) 

        try {

            $logFolder = Join-Path $CSVFolderPath "\logs\"
            $logFileFullPath = Join-Path $logFolder  $script:LogFileName
            if( (test-Path -path $logFolder) -eq $false) {
                mkdir $logFolder -force
            }

            try {
                Start-Transcript -Path  $logFileFullPath  -Force -Append -IncludeInvocationHeader
            } catch {
                # continue, even if transcripting fails
            }

            Write-Host "Starting Init-HLKSDNWorkload"
            #test cluster path
            if( -not (Test-Path $CSVFolderPath -Verbose)) {
                throw "CSVFolderPath does not exist"
            }
            Write-Host "Validated path $CSVFolderPath"


            # setup workload VM credentials
            if(-not [string]::IsNullOREmpty($WorkloadUserName) -and -not [string]::IsNullOrEmpty($WorkloadPassword)) {

                $Env:TURNKEY_WORKLOAD_USERNAME = $WorkloadUserName
                $Env:TURNKEY_WORKLOAD_PASSWORD = $WorkloadPassword

            } else {

                $WorkloadUserName = "administrator"

                $Env:TURNKEY_WORKLOAD_USERNAME = $WorkloadUserName
                $Env:TURNKEY_WORKLOAD_PASSWORD = $WorkloadUserName

            }

            [Environment]::SetEnvironmentVariable("TURNKEY_WORKLOAD_USERNAME", $WorkloadUserName, "Machine")
            [Environment]::SetEnvironmentVariable("TURNKEY_WORKLOAD_PASSWORD", $WorkloadPassword, "Machine")
            
            Write-Host "Workload VM credentials setup complete"
            
            # check and setup domain name
            if([string]::IsNullOREmpty($DomainName)) {

                throw "DomainName cannot be null"

            } else {

                $Env:TEST_DOMAIN = $DomainName
                [Environment]::SetEnvironmentVariable("TEST_DOMAIN", $DomainName, "Machine")

            }
            Write-Host "Domain name setup complete"


            # initialize domain credentials
            if(-not [string]::IsNullOREmpty($DomainUserName) -and -not [string]::IsNullOrEmpty($DomainPassword)) {

                $env:TEST_USERNAME = $DomainUserName
                [Environment]::SetEnvironmentVariable("TEST_USERNAME", $DomainUserName, "Machine")
                
                $env:TEST_PASSWORD = $DomainPassword
                [Environment]::SetEnvironmentVariable("TEST_PASSWORD", $DomainPassword, "Machine")
                
            } else {

                throw "DomainUserName or DomainPassword cannot be null"
            }
            Write-Host "Domain credentials setup complete"
            

            # this deployment will be similar to CTL, where management and PA networks are carved from the same range
            if($NetworkPrefix -notmatch "\/16") {
                throw "NetworkPrefix must be a /16 prefix"
            }
            Write-Host "Validated NetworkPrefix $NetworkPrefix"
            
            $configOverrideFile = Join-Path -Path $CSVFolderPath -ChildPath "SDNHLKConfig.json"
            if ($(Test-Path $configOverrideFile)) {
                $configOverride = Get-Content $configOverrideFile | ConvertFrom-Json
            }

            if ($configOverride.reuseTopology -eq 'true' -and $(Test-IsTurnKeySdnInstalled)) {
                Write-Host "Reusing existing topology, return"
                return
            }

            try {
                Uninstall-TurnkeySdn -Force
            } catch {
                Write-Host "Uninstall failed with $_"
            }

            # Restore sdnexp path
            Set-SdnExpressPath -SdnExpressPath $PSScriptRoot\..\SdnExpress

            $ipTokens = $NetworkPrefix -split "/"
            #just to ensure that IP address is valid
            $ip_Addr = [ipaddress]::Parse($ipTokens[0])

            $t = $ipTokens[0].Split(".")
            # todo: ask for different networks in future, for now we carve out subnets from a larger pool of prefix
            
            $defaultGatway_management = "$($t[0]).$($t[1]).1.1"
            $defaultGatway_PA = "$($t[0]).$($t[1]).2.1"
            $defaultGatway_PublicVIP = "$($t[0]).$($t[1]).3.1"
            $defaultGatway_PrivateVIP = "$($t[0]).$($t[1]).4.1"

            $ipPoolStart_management = "$($t[0]).$($t[1]).1.20"
            $ipPoolEnd_management = "$($t[0]).$($t[1]).1.100"

            $ipPoolStart_PA = "$($t[0]).$($t[1]).2.20"
            $ipPoolEnd_PA = "$($t[0]).$($t[1]).2.100"

            $ipPoolStart_PublicVIP = "$($t[0]).$($t[1]).3.20"
            $ipPoolEnd_PublicVIP = "$($t[0]).$($t[1]).3.100"

            $ipPoolStart_PrivateVIP = "$($t[0]).$($t[1]).4.20"
            $ipPoolEnd_PrivateVIP = "$($t[0]).$($t[1]).4.100"

            $managementPrefix = "$($t[0]).$($t[1]).1.0/24"
            $paPrefix = "$($t[0]).$($t[1]).2.0/24"
            $publicVipPrefix = "$($t[0]).$($t[1]).3.0/24"
            $privateVipPrefix = "$($t[0]).$($t[1]).4.0/24"


            # fix config (management network)
            Write-Host "Setting up management network with VLAN $VLANID and default gateway $defaultGatway_management"
            $mgmtNetwork = Get-TurnKeySdnManagementNetwork 
            $mgmtNetwork.properties.subnets[0].properties.vlanID = $VLANID
            $mgmtNetwork.properties.subnets[0].properties.defaultGateways = @($defaultGatway_management)
            $mgmtNetwork.properties.subnets[0].properties.ipPools[0].properties.startIpAddress = $ipPoolStart_management
            $mgmtNetwork.properties.subnets[0].properties.ipPools[0].properties.endIpAddress = $ipPoolEnd_management
            $mgmtNetwork.properties.subnets[0].properties.addressPrefix = $managementPrefix

            if($null -ne $ManagementDNSIpAddresses -and $ManagementDNSIpAddresses.Count -gt 0) {
                Write-Host "Assigning management DNS IP addresse(s) $ManagementDNSIpAddresses"
                $mgmtNetwork.properties.subnets[0].properties.dnsServers = $ManagementDNSIpAddresses
            }
            Set-TurnKeySdnManagementNetwork -Network $mgmtNetwork
            Write-Host "Management network setup complete"

            # fix config (pa network)
            $paNetwork = Get-TurnKeySdnHNVPANetwork
            Write-Host "Setting up PA network with VLAN $VLANID and default gateway $defaultGatway_PA and ip pool $ipPoolStart_PA - $ipPoolEnd_PA"
            $paNetwork.properties.subnets[0].properties.vlanID = $VLANID
            $paNetwork.properties.subnets[0].properties.defaultGateways = @($defaultGatway_PA)
            $paNetwork.properties.subnets[0].properties.ipPools[0].properties.startIpAddress = $ipPoolStart_PA
            $paNetwork.properties.subnets[0].properties.ipPools[0].properties.endIpAddress = $ipPoolEnd_PA
            $paNetwork.properties.subnets[0].properties.addressPrefix = $paPrefix

            Set-TurnKeySdnHNVPANetwork -Network $paNetwork

            $publicVipNetwork = Get-TurnKeySdnPublicVIPNetwork
            Write-Host "Setting up Public network with default gateway $defaultGatway_PublicVIP and ip pool $ipPoolStart_PublicVIP - $ipPoolEnd_PublicVIP"
            $publicVipNetwork.properties.subnets[0].properties.defaultGateways = @($defaultGatway_PublicVIP)
            $publicVipNetwork.properties.subnets[0].properties.ipPools[0].properties.startIpAddress = $ipPoolStart_PublicVIP
            $publicVipNetwork.properties.subnets[0].properties.ipPools[0].properties.endIpAddress = $ipPoolEnd_PublicVIP
            $publicVipNetwork.properties.subnets[0].properties.addressPrefix = $publicVipPrefix

            Set-TurnKeySdnPublicVIPNetwork -Network $publicVipNetwork

            $privateVipNetwork = Get-TurnKeySdnPrivateVIPNetwork
            Write-Host "Setting up private network with default gateway $defaultGatway_PrivateVIP and ip pool $ipPoolStart_PrivateVIP - $ipPoolEnd_PrivateVIP"
            $privateVipNetwork.properties.subnets[0].properties.defaultGateways = @($defaultGatway_PrivateVIP)
            $privateVipNetwork.properties.subnets[0].properties.ipPools[0].properties.startIpAddress = $ipPoolStart_PrivateVIP
            $privateVipNetwork.properties.subnets[0].properties.ipPools[0].properties.endIpAddress = $ipPoolEnd_PrivateVIP
            $privateVipNetwork.properties.subnets[0].properties.addressPrefix = $privateVipPrefix

            Set-TurnKeySdnPrivateVIPNetwork -Network $privateVipNetwork

            Write-Host "PA network setup complete"

            Set-TurnKeySdnGatewayConfig -NodeCount 0
            Write-Host "Gateway node count set to 0"

            Set-TurnKeySdnNCConfig -UseSF -nodecount 1
            Write-Host "NC node count set to 1"
            
            $depconfig = Get-TurnKeySdnDeploymentConfig
            # set SDN switchname
            $depconfig.sdnSwitchName = $SdnSwitchname
            $depconfig.internetSwitchName = $InternetSwitchName
            # disable address randomization
            $depconfig.randomizeAddresses = $false

            if (-not [String]::IsNullOrEmpty($configOverride.windowsServerProductKey)) {
                $depconfig.productKey = $configOverride.windowsServerProductKey
            } else {
                $productKeyFile = Join-Path $VHDPath "productkey.txt"
                if ($(Test-Path $productKeyFile)) {
                    [array]$content = Get-Content $productKeyFile
                    if ($content.Count -eq 0) {
                        Write-Host "Product key file is empty"
                    } else {
                        $productKey = $content[0].Trim()
                        $depconfig.productKey = $productKey
                    }
                }
            }

            if (-not [String]::IsNullOrEmpty($configOverride.torVMConfig.IPAddress)) {
                $depconfig.torVMConfig = $configOverride.torVMConfig
            }

            $depConfig.bypassTORForLBDataPath = $true
            Set-TurnKeySdnDeploymentConfig -Config $depconfig
            $sdnVmPath = (Join-Path -Path $CSVFolderPath -ChildPath "SDNVMs")
            Write-Host "Setting SDN VM path to $sdnVmPath"

            Set-TurnKeySdnDeploymentPath -DeploymentPath $sdnVmPath
            
            Set-TurnKeySdnDeploymentVhd -vhdPath $VHDPath -vhdFile $VHDFileName -setDefaultForWorkload
            
            Set-TurnKeySdnMuxConfig -NodeCount 1

            # force reload of properties
            Initialize-TurnKeySdnDeployment
            Install-TurnKeySdn

    } catch {

        Write-Host "Error Occurred during validation: $_"
        Write-Host "ExceptionDetails: $($_.ToString())"

        if($null -ne $_.Exception ) {
            Write-Host "`t InnerException: $($_.Exception)"
            Write-Host "`t InnerException: $($_.Exception.ToString())"

            if( $null -ne $_.Exception.InnerException) {
                Write-Host "`t `t InnerException: $($_.Exception.InnerException)"
                Write-Host "`t `t InnerException: $($_.Exception.InnerException.ToString())"
            }
        }
        throw $_
    } finally {
        Write-Host "Init-HLKSDNWorkload: exit & stopping transcript"
        Stop-Transcript
    }
}


function Start-HLKTracingSession {
    param(
        [string] $CSVFolderPath
    )

    Write-Host "Start-HLKTracingSession: enter, folder path: $CSVFolderPath"
    if([string]::IsNullOrEmpty($CSVFolderPath)) {
        throw "CSVFolderPath cannot be null"
    }


    $logFolder = Join-Path $CSVFolderPath "\logs\"
    $logFileFullPath = Join-Path $logFolder  $script:LogFileName

    if( (test-Path -path $logFolder) -eq $false) {
        mkdir $logFolder -force
    }

    try {
        Start-Transcript -Path  $logFileFullPath  -Force -Append -IncludeInvocationHeader
    } catch {
        # continue, even if transcripting fails
    }

    Write-Host "Start-HLKTracingSession: exit"
}

function Start-HlkValidationTraffic {

    param(
        [string] $CSVFolderPath,

        [ValidateSet("LB-INBOUND", "LB-OUTBOUND", "ALL", "VNET", "LB-LBRULE","LIVEMIGRATION")]
        [string] $TestType
    )

    if([string]::IsNullOrEmpty($CSVFolderPath)) {
        throw "Start-HlkValidationTraffic: CSVFolderPath cannot be null"
    }

    try {
        
        Start-HLKTracingSession -CSVFolderPath $CSVFolderPath

        Start-TurnKeySdnTrafficTests -WorkloadType $TestType
    
    } catch {

        Write-Host "Error Occurred during validation: $_"
        Write-Host "ExceptionDetails: $($_.ToString())"

        if($null -ne $_.Exception ) {
            Write-Host "`t InnerException: $($_.Exception)"
            Write-Host "`t InnerException: $($_.Exception.ToString())"

            if( $null -ne $_.Exception.InnerException) {
                Write-Host "`t `t InnerException: $($_.Exception.InnerException)"
                Write-Host "`t `t InnerException: $($_.Exception.InnerException.ToString())"
            }
        }
        throw $_
    }
    finally {
        Write-Host "Start-HlkValidationTraffic: exit & stopping transcript"
        try {
            stop-transcript -ErrorAction SilentlyContinue | Out-Null
        } catch {
            Write-Host "Error Occurred during stop-transcript: $_"
        }
    }
}


function Get-SdnExpressFork {
    param([string] $turnKeyRoot = "C:\tools\deployment\")

    mkdir "$turnKeyRoot\SdnExpress" -Force -ErrorAction SilentlyContinue | Out-Null
    mkdir "$Env:Temp\SdnExpress\" -ErrorAction SilentlyContinue | Out-Null

    Start-BitsTransfer `
        -Source https://github.com/sbgms/SDN/archive/refs/heads/master.zip `
        -Destination "$turnKeyRoot\sdnexpress.zip" -ErrorAction Stop -TransferType Download -Dynamic
    Expand-Archive "$turnKeyRoot\sdnexpress.zip" -DestinationPath $Env:Temp\SdnExpress\ -ErrorAction Stop

    copy-item -Path "$Env:Temp\SdnExpress\SDN-master\SdnExpress\*" -Destination "$turnKeyRoot\SdnExpress" -Recurse -Force -ErrorAction Stop
    
    Remove-Item "$turnKeyRoot\sdnexpress.zip" -Force -ErrorAction SilentlyContinue | Out-Null
    Push-Location $turnKeyRoot
    
    Remove-Item "$turnKeyRoot\sdnexpress.zip" -Force -ErrorAction SilentlyContinue | Out-Null
    Remove-Item "$Env:Temp\SdnExpress\"  -Recurse -Force -ErrorAction SilentlyContinue | Out-Null
    Push-Location $turnKeyRoot
}

function Get-TurnKeyFork {
    param([string] $turnKeyRoot = "C:\tools\deployment\")

    Install-Module -Name "PowerShellGet" -Force -ErrorAction SilentlyContinue | Out-Null
    $mod = Install-module TurnKeySdn -Force
    Import-Module TurnKeySdn -Force
    $mod = Get-Module TurnKeySdn
    
    mkdir "$turnKeyRoot\TurnKeySdn" -Force -ErrorAction SilentlyContinue | Out-Null
    copy-item -Path "$($mod.ModuleBase)\*" -Destination "$turnKeyRoot\TurnKeySdn\" -Recurse -Force -ErrorAction Stop

    Get-SdnExpressFork -turnKeyRoot $turnKeyRoot
}

function Get-SdnDiagnosticsOfflinePath {

    $sdnDiag = Join-Path $PSScriptRoot "..\SdnDiagnostics"
    $sdnDiagPsd1 = Join-Path $sdnDiag "SdnDiagnostics.psd1"
    if ($(Test-Path $sdnDiagPsd1)) {
        return $sdnDiag
    }
    return $null
}

function Get-SdnDiagnostics {
    
    param(
        [string] $storageRoot = "C:\clusterstorage\sdnspace"
    )

    $creds = Get-TurnKeySdnCred

    # get NC Name using a hack
    $ncName = ((get-clustergroup -name *NC*) | select -First 1).Name

    $logLocation  = Join-Path $storageRoot "SdnDataCollection"
    $logLocation = Join-Path $logLocation (Get-Date -Format "MM-dd-HHmmss")

    $modulePath = Get-SdnDiagnosticsOfflinePath
    if ($null -eq $modulePath) {
        Install-Module -Name SdnDiagnostics -Force -Confirm:$false
        Update-Module -Name SdnDiagnostics -Force -Confirm:$false
    } else {
        Write-Verbose "Importing SdnDiagnostics module from $modulePath"
        Import-Module "$modulePath\SdnDiagnostics.psd1" -Force
    }
    
    Start-SdnDataCollection -NetworkController $ncName  -Credential $creds -OutputDirectory $logLocation -Role NetworkController,Server,LoadBalancerMux -IncludeNetView -IncludeLogs
}