driver_utils.ps1

$TEMP_ZERTO_DRIVER_LOGS_PATH = ('{0}/zertoDriverLogs/' -f $TEMP_ZERTO_DOWNLOAD_PATH)

function Enable-HostSecurity($HostName, $DatastoreName, $BiosUuid) {
    Write-Host "Starting $($MyInvocation.MyCommand)..."
    $datastoreUuid = Get-DatastoreUUID($DatastoreName)

    $scriptPath = ('{0}/zexec_installed_only_set.sh' -f $ZERTO_FOLDER_ON_HOST)
    $fileRemoteDir = ("/vmfs/volumes/{0}/zagentid/{$BiosUuid}", $datastoreUuid);

    $Commands = ('chmod a+x {0}' -f $scriptPath),
        ('OUT=`{0} enable {1}`; EXIT_STATUS=$?; echo $OUT >> /etc/vmware/zloadmod.txt; echo $EXIT_STATUS;' -f $scriptPath, $fileRemoteDir)

    $sshResult = Invoke-SSHCommands -HostName $HostName -Commands $Commands

    Write-Host "SSH result: $($sshResult["1_output"])"
    $exitStatus = $sshResult["1_output"]
    $exitStatus = if ($exitStatus) { $exitStatus } else { "" }
    $hasErrors = ($exitStatus.Trim() -ne "0")

    if ($hasErrors) {
        throw "Failed to enable host security for $HostName"
    }
}

function Get-HostSecureBootStatus($HostName) {
    Write-Host "Starting $($MyInvocation.MyCommand)..."
    $Command =    ('esxcli system settings encryption get')
    $Res = Invoke-SSHCommands -HostName $HostName -Commands $Command
    Write-Host "Host $HostName secure boot status: $($Res["0_output"])"
}

function Copy-FilesFromDatastoreToHost {
    param(
        [Parameter(Mandatory = $true,
            HelpMessage = "Host Name to connect with SSH")]
        [string]$HostName,
        [Parameter(Mandatory = $true,
            HelpMessage = "Datastore Name")]
        [string]$DatastoreName,
        [Parameter(Mandatory = $true,
            HelpMessage = "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid")]
        [string]$BiosUuid
    )

    Write-Host "Starting $($MyInvocation.MyCommand)..."

    Initialize-ZertoTempFolder
    Get-FilesFromDatastoreToPSEngine -DatastoreName $DatastoreName -BiosUuid $BiosUuid
    Validate-AndUploadFilesFromPSEngineToHost -HostName $HostName
    Clear-ZertoTempFolderLeniently
}

function Get-FilesFromDatastoreToPSEngine {
    param(
        [Parameter(Mandatory = $true,
            HelpMessage = "Datastore Name")]
        [string]$DatastoreName,
        [Parameter(Mandatory = $true,
            HelpMessage = "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid")]
        [string]$BiosUuid
    )

    Write-Host "Starting $($MyInvocation.MyCommand)..."

    $psDriverName = "ds"
    $FullRemoteFileLocation = ('{0}:\zagentid\{1}\*' -f $psDriverName, $BiosUuid)
    $datastore = Get-Datastore $DatastoreName

    Write-Host "Attempting to download files from $datastore ($FullRemoteFileLocation) to PS engine ($TEMP_ZERTO_DOWNLOAD_PATH)."

    New-PSDrive -Location $datastore -Name $psDriverName -PSProvider VimDatastore -Root "\" -ErrorAction Stop
    Copy-DatastoreItem -Item $FullRemoteFileLocation -Destination $TEMP_ZERTO_DOWNLOAD_PATH -Force -ErrorAction Stop

    $files = (Get-ChildItem -Path $TEMP_ZERTO_DOWNLOAD_PATH -Name) -join ";"
    Write-Host "ZertoFiles: {$files} were copied from datastore $DatastoreName ($FullRemoteFileLocation) to PS engine ($TEMP_ZERTO_DOWNLOAD_PATH)"

    Remove-PSDrive -Name $psDriverName
}

function Validate-AndUploadFilesFromPSEngineToHost {
    param(
        [Parameter(Mandatory = $true,
            HelpMessage = "Host Name to connect with SSH")]
        [string]$HostName
    )

    Write-Host "Starting $($MyInvocation.MyCommand)..."

    New-ZertoFolderOnHost -HostName $HostName

    # Temp directory must not contain subdirectories
    foreach ($file in Get-ChildItem $TEMP_ZERTO_DOWNLOAD_PATH -Recurse -Include *.sh, *.o) {
        $signature = ("{0}_signature" -f $file)

        if ((Validate-FileBySignature -FilePath $file -SignatureFilePath $signature)) {
            Set-SFTPItem -SessionId ($SFTP_Sessions[$HostName]).Value.SessionId -Destination $ZERTO_FOLDER_ON_HOST -Path $file -Force
        }
        else {
            throw "Error! host $HostName failed to verify $file with $signature, openSSL output: $isVerified"
        }
    }
}

function Test-ZertoDriverLoaded {
    <#
        .DESCRIPTION
        Return true if Zerto driver is loaded, otherwise return false.
 
            .PARAMETER HostName
            Host Name to connect with SSH
 
        .EXAMPLE
 
         Test-ZertoDriverLoaded -HostName <HostName>
    #>

    param(
        [Parameter(Mandatory = $true,
            HelpMessage = "Host Name to connect with SSH")]
        [string]$HostName

    )

    process {
        Write-Host "Starting $($MyInvocation.MyCommand)..."

        $vmkload = '/tmp/vmklod_output'
        $ZertoDriver = '/tmp/zertoDriver'

        $Command =    ('vmkload_mod -l > {0}' -f $vmkload)
        $Res = Invoke-SSHCommands -HostName $HostName -Commands $Command
        $Command = ('grep "zdriver" {0} > {1}' -f $vmkload, $ZertoDriver)
        $Res = Invoke-SSHCommands -HostName $HostName -Commands $Command -ExitStatusAction "Skip"

        $ExitStatus = $Res["0_exitStatus"]
        if ($ExitStatus -eq '0') {
            Write-Host "Zerto driver is loaded"
            return $true;
        }

        if ($ExitStatus -eq '1') {
            Write-Host "Zerto driver is not loaded"
            return $false;
        }
    }
}

function Get-DriverLogsFromHost {
    <#
        .DESCRIPTION
        Copy Zerto driver logs from host to datastore
 
            .PARAMETER HostName
            Host Name to connect with SSH
 
            .PARAMETER DatastoreName
            Datastore Name
 
        .EXAMPLE
 
        Get-DriverLogsFromHost -HostName <HostName> -DatastoreName <DatastoreName>
    #>

    param(
        [Parameter(Mandatory = $true,
            HelpMessage = "Host Name to connect with SSH")]
        [string]$HostName,
        [Parameter(Mandatory = $true,
            HelpMessage = "Datastore Name")]
        [string]$DatastoreName
    )

    Write-Host "Starting $($MyInvocation.MyCommand)..."
    Copy-DriverLogFilesFromHostToPSEngine -HostName $HostName
    Copy-DriverLogFilesFromPSEngineToDatastore -DatastoreName $DatastoreName -HostName $HostName
}

function Copy-DriverLogFilesFromHostToPSEngine {
    param(
        [Parameter(Mandatory = $true,
            HelpMessage = "Host Name to connect with SSH")]
        [string]$HostName
    )

    Write-Host "Starting $($MyInvocation.MyCommand)..."

    $sftpSessionId = ($SFTP_Sessions[$HostName]).Value.SessionId

    if (!(Test-Path -path $TEMP_ZERTO_DRIVER_LOGS_PATH)) {
        Write-Host "Creating $TEMP_ZERTO_DRIVER_LOGS_PATH folder on powerShell engine"
        New-Item $TEMP_ZERTO_DRIVER_LOGS_PATH -Type Directory
    }

    $files = '/etc/vmware/zloadmod.txt',
    '/etc/vmware/zunloadmod.txt'

    foreach ($file in $files) {
        if (Test-SFTPPath -SessionId $sftpSessionId -Path $file) {
            Write-Host "$file from $HostName will be downloaded to powerShell engine ($TEMP_ZERTO_DRIVER_LOGS_PATH)"
            Get-SFTPItem -SessionId $sftpSessionId -Destination $TEMP_ZERTO_DRIVER_LOGS_PATH -Path $file -Force
            Write-Host "$file was copied from $HostName to powerShell engine"
        }
        else {
            Write-Host "File $file does not exist on $HostName"
        }
    }
}

function Copy-DriverLogFilesFromPSEngineToDatastore {
    param(
        [Parameter(Mandatory = $true,
            HelpMessage = "Datastore Name")]
        [string]$DatastoreName,
        [Parameter(Mandatory = $true,
            HelpMessage = "Host Name to connect with SSH")]
        [string]$HostName
    )

    Write-Host "Starting $($MyInvocation.MyCommand)..."

    $psDriverName = "ds"
    $zertoDriverLogsDSPath = ('{0}:\zertoDriverLogsFromHost\{1}\' -f $psDriverName, $HostName)
    $zertoDriverLogsPSPath = ('{0}*' -f $TEMP_ZERTO_DRIVER_LOGS_PATH)
    $datastore = Get-Datastore $DatastoreName

    New-PSDrive -Location $datastore -Name $psDriverName -PSProvider VimDatastore -Root "\" -ErrorAction Stop
    Copy-DatastoreItem -Item $zertoDriverLogsPSPath -Destination $zertoDriverLogsDSPath -Force -ErrorAction Stop

    $files = (Get-ChildItem -Path $zertoDriverLogsDSPath -Name) -join ";"
    Write-Host "$MyInvocation.MyCommand - ZertoDriverFiles: {$files} were copied from powerShell engine ($zertoDriverLogsPSPath) to datastore ($zertoDriverLogsDSPath)"

    Remove-PSDrive -Name $psDriverName
}

function Get-ZertoFilesListFromHost {
    <#
        .DESCRIPTION
        Return a list of all Zerto files on host under /var/zerto
 
            .PARAMETER HostName
            Host Name to connect with SSH
 
        .EXAMPLE
 
        Get-ZertoFilesListFromHost -HostName <HostName>
    #>

    param(
        [Parameter(Mandatory = $true,
            HelpMessage = "Host Name to connect with SSH")]
        [string]$HostName
    )

    process {
        Write-Host "Starting $($MyInvocation.MyCommand)..."
        $Command = ('ls -l {0}' -f $ZERTO_FOLDER_ON_HOST)
        return Invoke-SSHCommands -HostName $HostName -Commands $Command
    }
}