ZertoAVSModule.psm1

using module Microsoft.AVS.Management


$PUBLIC_KEY = ('{0}\ZertoPublicKey.pem' -f $psScriptRoot)
$ZERTO_FOLDER_ON_HOST = "/var/zerto"
$LOCAL_TEMP_FOLDER_FOR_DOWNLOADED_FILES = ('{0}\filesFromDatastore\' -f $psScriptRoot)

Function TestConnection {
    return "TestConnection"
}

Function CreateZertoFolderOnHost {
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname 
    )
    
    process {
        $Command = "mkdir -p $ZERTO_FOLDER_ON_HOST"
        $Res = RunSSHCommands -Hostname $Hostname -Commands $Command

        if ( $Res.ExitStatus -ne '0' ) {
            throw "failed to create $ZERTO_FOLDER_ON_HOST on host"
        }
    }
}

Function VerifyAndUploadFilesFromPSEngineToHost {
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname
    )
    
    CreateZertoFolderOnHost -HostName $HostName
    
    foreach ($file in Get-ChildItem $LOCAL_TEMP_FOLDER_FOR_DOWNLOADED_FILES* -Include *.sh, *.o) {
        $signature = ("{0}_signature" -f $file)

        $isVerified = openssl dgst -sha256 -verify $PUBLIC_KEY -signature $signature $file
        
        if ($isVerified -eq "Verified OK") {
            Set-SFTPItem -SessionId ($SFTP_Sessions[$Hostname]).Value -Destination $ZERTO_FOLDER_ON_HOST -Path $file -Force
        }
        else {
            throw "Error! failed to verify $file, openSSL output: $isVerified"
        }
    }
}

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

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

    New-PSDrive -Location $datastore -Name $psDriverName -PSProvider VimDatastore -Root "\"
    Copy-DatastoreItem -Item $FullRemoteFileLocation -Destination $LOCAL_TEMP_FOLDER_FOR_DOWNLOADED_FILES -Force
    Remove-PSDrive -Name $psDriverName
}

Function CopyFilesFromDatastoreToHost {
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Datastore Uuid")]
        [string]$DatastoreUuid,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid")]
        [string]$BiosUuid
    )
    
    DownloadFilesFromDatastoreToPSEngine -DatastoreUuid $DatastoreUuid -BiosUuid $BiosUuid
    VerifyAndUploadFilesFromPSEngineToHost -HostName $HostName
}

Function RunSSHCommands {
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Commands to execute")]
        [String[]]$Commands
    )
    
    process {
        $Result = @()

        foreach ($Command in $Commands) {
            $SSH = Invoke-SSHCommand -SSHSession $SSH_Sessions[$Hostname].Value -Command $Command
            $Result += New-Object PSObject -Property @{
                Cmd        = $Command
                ExitStatus = $SSH.ExitStatus
                Output     = $SSH.Output
            }    
        }

        return $Result
    }
}

<#
.DESCRIPTION
 
This Cmdlet displays information about the available disk space
 
    .PARAMETER HostName
    Host Name to connect with ssh
 
.EXAMPLE
 
Get-HostTempFolderInfo -HostName xxx.xxx.xxx.xxx
#>

Function Get-HostTempFolderInfo {
    [CmdletBinding()]
    [AVSAttribute(5, UpdatesSDDC = $false)]
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname 
    )
    
    process {
        $Command = "vdf"
        return RunSSHCommands -Hostname $Hostname -Commands $Command
    }
}

<#
.DESCRIPTION
 
This Cmdlet checks if the host is up and running
 
    .PARAMETER HostName
    Host Name to connect with ssh
 
.EXAMPLE
 
EnsureConnectivity -HostName xxx.xxx.xxx.xxx
#>

Function EnsureConnectivity {    
    [CmdletBinding()]
    [AVSAttribute(5, UpdatesSDDC = $false)]
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname 
    )
    
    process {
        $Command = "echo testing123"
        return RunSSHCommands -Hostname $Hostname -Commands $Command
    }
}

<#
.DESCRIPTION
 
This Cmdlet retrieves the ESXi version
 
    .PARAMETER HostName
    Host Name to connect with ssh
 
.EXAMPLE
 
Get-HostEsxiVersion -HostName xxx.xxx.xxx.xxx
#>

Function Get-HostEsxiVersion {
    [CmdletBinding()]
    [AVSAttribute(5, UpdatesSDDC = $false)]
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname 
    )
    
    process {
        $Command = "vmware -l"
        return RunSSHCommands -Hostname $Hostname -Commands $Command
    }
}

<#
.DESCRIPTION
 
This Cmdlet is responsible for loading the driver when the host is booting.
Rc.local file is executed after all the normal system services are started
 
    .PARAMETER HostName
    Host Name to connect with ssh
 
    .PARAMETER DatastoreUuid
    Datastore Uuid
 
    .PARAMETER BiosUuid
    "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid"
 
.EXAMPLE
 
ChangeRcLocal -HostName xxx.xxx.xxx.xxx -DatastoreUuid xxx -BiosUuid xxx
#>

Function ChangeRcLocal {
    [CmdletBinding()]
    [AVSAttribute(30, UpdatesSDDC = $false)]
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Datastore Uuid")]
        [string]$DatastoreUuid,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid")]    
        [string]$BiosUuid
    )

    Process {

        $zloadmod = ('{0}/zloadmod.sh' -f $ZERTO_FOLDER_ON_HOST)

        CopyFilesFromDatastoreToHost -Hostname  $HostName -DatastoreUuid $DatastoreUuid -BiosUuid $BiosUuid

        $rcLocal = ('{0}/rc.local' -f $ZERTO_FOLDER_ON_HOST)
    
        $Commands =    ('grep -v ZeRTO /etc/rc.local > {0}' -f $rcLocal),
        ('echo \#ZeRTO\ >> {0}' -f $rcLocal),
        ('echo sh {0} load {1} {2} \"\" \"\" 1 > /etc/vmware/zloadmod.txt \2\>\&\1 \#ZeRTO\ >> {3}' -f $zloadmod, $DatastoreUuid, $BiosUuid, $rcLocal),
        ('echo \#ZeRTO\ >> {0}' -f $rcLocal),
        ('cp -f {0} /etc/rc.local' -f $rcLocal)
                    
        return RunSSHCommands -Hostname $Hostname -Commands $Commands
    }
}

<#
.DESCRIPTION
 
This Cmdlet installs the driver
 
    .PARAMETER HostName
    Host Name to connect with SSH
     
    .PARAMETER DatastoreUuid
    Datastore Uuid
 
    .PARAMETER BiosUuid
    Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid
 
    .PARAMETER IsInit
    Init or load the driver
 
    .PARAMETER EsxiVersion
    Esxi version
 
.EXAMPLE
InstallDriver -HostName xxx.xxx.xxx.xxx -DatastoreUuid <UUID> -BiosUuid <UUID> -IsInit <init/load> -EsxiVersion xx
#>

Function InstallDriver {
    [CmdletBinding()]
    [AVSAttribute(30, UpdatesSDDC = $false)]
    param(
        [Parameter(Mandatory = $true, 
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname,
        [Parameter(Mandatory = $true, 
            HelpMessage = "Datastore Uuid")]
        [string]$DatastoreUuid,
        [Parameter(Mandatory = $true,
            HelpMessage = "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid")]    
        [string]$BiosUuid,
        [Parameter(Mandatory = $true,    
            HelpMessage = "Init or load the driver")]    
        [string]$isInit,
        [Parameter(Mandatory = $true,    
            HelpMessage = "Esxi version")]    
        [string]$EsxiVersion
    )

    Process {

        $zloadmod = ('{0}/zloadmod.sh' -f $ZERTO_FOLDER_ON_HOST)
        
        CopyFilesFromDatastoreToHost -Hostname  $HostName -DatastoreUuid $DatastoreUuid -BiosUuid $BiosUuid
        
        $Commands = ('chmod a+x {0}' -f $zloadmod),
            ('{0} {1} {2} {3} 1 {4} 1 > /etc/vmware/zloadmod.txt' -f $zloadmod, $IsInit, $DatastoreUuid, $BiosUuid, $EsxiVersion)

        return RunSSHCommands -Hostname $Hostname -Commands $Commands
    }
}

<#
.DESCRIPTION
 
This Cmdlet used to uninstall the driver
 
    .PARAMETER HostName
    Host Name to connect with SSH
     
    .PARAMETER DatastoreUuid
    Datastore Uuid
 
    .PARAMETER BiosUuid
    Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid
 
 
.EXAMPLE
UninstallDriver -HostName xxx.xxx.xxx.xxx -DatastoreUuid <UUID> -BiosUuid <UUID>
#>

Function UninstallDriver {
    [CmdletBinding()]
    [AVSAttribute(30, UpdatesSDDC = $false)]
    param(
        [Parameter(Mandatory = $true,
            HelpMessage = "Host Name to connect with SSH")]
        [string]$Hostname,
        [Parameter(Mandatory = $true,
            HelpMessage = "Datastore Uuid")]
        [string]$DatastoreUuid,
        [Parameter(Mandatory = $true,
            HelpMessage = "Host Bios Uuid || mob-> Property Path: host.hardware.systemInfo.uuid")]
        [string]$BiosUuid
    )
    
    process {

        $zunloadmod = ('{0}/zunloadmod.sh' -f $ZERTO_FOLDER_ON_HOST)
        
        CopyFilesFromDatastoreToHost -Hostname  $HostName -DatastoreUuid $DatastoreUuid -BiosUuid $BiosUuid

        $Commands = ('chmod a+x {0}' -f $zunloadmod),
                    ('{0} cleanup > /etc/vmware/zunloadmod.txt' -f $zunloadmod)

        return RunSSHCommands -Hostname $Hostname -Commands $Commands
    }
}