HLK-Driver.psm1

Import-Module .\TurnKeySdn\TurnKeySdn.psd1 -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 -TotalVMCount 20 -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)][int] $TotalVMCount,
        [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
        
    ) 

        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"

            $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_VIP = "$($t[0]).$($t[1]).3.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_VIP = "$($t[0]).$($t[1]).3.20"
            $ipPoolEnd_VIP = "$($t[0]).$($t[1]).3.100"

            $managementPrefix = "$($t[0]).$($t[1]).1.0/24"
            $paPrefix = "$($t[0]).$($t[1]).2.0/24"
            $vipPrefix = "$($t[0]).$($t[1]).3.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
            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
            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
            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 Occured during validtion: $_"
        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", "ILB", "VNET", "LB-LBRULE","LIVEMIGRATION")]
        [string] $TestType
    )

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

    try {
        
        Start-HLKTracingSession -CSVFolderPath $CSVFolderPath

        Start-SdnHlkTrafficTests -WorkloadType $TestType
    
    } catch {

        Write-Host "Error Occured during validtion: $_"
        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 Occured during stop-transcript: $_"
        }
    }
}