zvmRemoteScripts_utils.ps1

function Assert-ZertoInitialized {
    Write-Host "Starting $($MyInvocation.MyCommand)..."
    $action = {
        $ZVM = Get-VM -Name $ZVM_VM_NAME
        if ($null -eq $ZVM) {
            Write-Error "$ZVM_VM_NAME doesn't exists" -ErrorAction Stop
        }
        $res = Invoke-VMScript -VM $ZVM -ScriptText "whoami" -GuestUser $ZAPPLIANCE_USER -GuestPassword $PersistentSecrets.ZappliancePassword -ErrorAction SilentlyContinue
        if ($res.ScriptOutput.Trim() -ne $ZAPPLIANCE_USER) {
            throw "ZVM failed to initialize"
        }
    }
    Invoke-Retry -Action $action -ActionName "TestZertoInitialized" -RetryCount 10 -RetryIntervalSeconds 60
}

function Set-ZertoConfiguration {
    Write-Host "Starting $($MyInvocation.MyCommand)..."
    try {
        $startTime = Get-Date
        Write-Host "Wating for Zerto to start, this might take a while..."
        Assert-ZertoInitialized
        Write-Host "Zerto initialization took: $((Get-Date).Subtract($startTime).TotalSeconds) second(s)"

        Write-Host "Configuring Zerto, this might take a while..."
        $startTime = Get-Date
        #TODO: we need to rename key that holds VC user password. It sound confusing
        $scriptLocation = "/opt/zerto/zlinux/avs/configure_zerto.py"
        $commandToExecute = "sudo python3 $scriptLocation --vcPassword '$($PersistentSecrets.ZertoPassword)' --avsClientSecret '$($PersistentSecrets.AvsClientSecret)'"
        $result = Invoke-ZVMScriptWithTimeout -ScriptText $commandToExecute -ActionName "Configure ZVM"
        Write-Host "Zerto configuration took: $((Get-Date).Subtract($startTime).TotalSeconds) second(s)"

        if ($result.ScriptOutput.Contains("Success")) {
            Write-Host "Zerto configured successfully"
        }
        else {
            if ($result.ScriptOutput.Contains("Error:")) {
                Write-Error "Failed to configure ZVM. $result.ScriptOutput" -ErrorAction Stop
            }
            if ($result.ScriptOutput.Contains("Warning:")) {
                $message = "$result.ScriptOutput"
                Write-Host $message
                Write-Warning $message
            }
            else {
                Write-Error "An error occurred while configuring Zerto. Please reinstall Zerto." -ErrorAction Stop
            }
        }
    }
    catch {
        Write-Error "Failed to configure Zerto. Error: $_" -ErrorAction Stop
    }
}

function Test-ZertoPassword {
    process {
        Write-Host "Starting $($MyInvocation.MyCommand)..."
        $scriptLocation = "/opt/zerto/zlinux/avs/try_zerto_login.py"
        $commandToExecute = "sudo python3 $scriptLocation --zertoAdminPassword '$($PersistentSecrets.ZertoAdminPassword)'"
        try {
            $result = Invoke-ZVMScriptWithTimeout -ScriptText $commandToExecute -ActionName "Validate Zerto password" -TimeoutMinutes 5
            if ($result.ScriptOutput.Contains("Success")) {
                Write-Host "Zerto password is valid"
            }
            else {
                throw "Please, provide valid Zerto password."
            }
        }
        catch {
            Write-Error "An error happende during Zerto password validataion. Exception = $_" -ErrorAction Stop
        }
    }
}

function Update-VcPasswordInZvm {
    process {
        Write-Host "Starting $($MyInvocation.MyCommand)..."
        $startTime = Get-Date
        $scriptLocation = "/opt/zerto/zlinux/avs/change_vc_password.py"
        $commandToExecute = "sudo python3 $scriptLocation --vcPassword '$($PersistentSecrets.ZertoPassword)' --zertoAdminPassword '$($PersistentSecrets.ZertoAdminPassword)' --avsClientSecret '$($PersistentSecrets.AvsClientSecret)'"
        try {
            $result = Invoke-ZVMScriptWithTimeout -ScriptText $commandToExecute -ActionName "Update VC password in ZVM" -TimeoutMinutes 20
            Write-Host "Zerto reconfiguration took: $((Get-Date).Subtract($startTime).TotalSeconds) second(s)"

            if ($result.ScriptOutput.Contains("Success")) {
                Write-Host "New VC password set succesfully."
            }
            else {
                if ($result.ScriptOutput.Contains("Error:")) {
                    throw $result.ScriptOutput -replace "Error: ", ""
                }
                throw "An error occurred while updating VC password. Please try again."
            }
        }
        catch {
            Write-Error "Failed update VC password, please try again. Exception = $_" -ErrorAction Stop
        }
    }
}