Fazer upgrade de um aplicativo do Service Fabric
Esse exemplo de script faz upgrade de uma instância de aplicativo do Service Fabric em execução para a versão 1.3.0. O script copia o novo pacote de aplicativos para o repositório de imagens do cluster, registra o tipo de aplicativo e remove o pacote de aplicativos desnecessários. O script inicia uma atualização monitorada e verifica continuamente o status da atualização até sua conclusão ou reversão. Personalize os parâmetros conforme necessário.
Se necessário, instale o módulo Service Fabric do PowerShell com o SDK do Service Fabric.
Exemplo de script
## Variables
$ApplicationPackagePath = "C:\Users\sfuser\documents\visual studio 2017\Projects\Voting\Voting\pkg\Debug"
$ApplicationName = "fabric:/Voting"
$ApplicationTypeName = "VotingType"
$ApplicationTypeVersion = "1.3.0"
$imageStoreConnectionString = "fabric:ImageStore"
$CopyPackageTimeoutSec = 600
$CompressPackage = $false
## Check existence of the application
$oldApplication = Get-ServiceFabricApplication -ApplicationName $ApplicationName
if (!$oldApplication)
{
$errMsg = "Application '$ApplicationName' doesn't exist in cluster."
throw $errMsg
}
else
{
## Check upgrade status
$upgradeStatus = Get-ServiceFabricApplicationUpgrade -ApplicationName $ApplicationName
if ($upgradeStatus.UpgradeState -ne "RollingBackCompleted" -and $upgradeStatus.UpgradeState -ne "RollingForwardCompleted" -and $upgradeStatus.UpgradeState -ne "Failed")
{
$errMsg = "An upgrade for the application '$ApplicationTypeName' is already in progress."
throw $errMsg
}
$reg = Get-ServiceFabricApplicationType -ApplicationTypeName $ApplicationTypeName | Where-Object { $_.ApplicationTypeVersion -eq $ApplicationTypeVersion }
if ($reg)
{
Write-Host 'Application Type '$ApplicationTypeName' and Version '$ApplicationTypeVersion' was already registered with Cluster, unregistering it...'
$reg | Unregister-ServiceFabricApplicationType -Force
}
## Copy application package to image store
$applicationPackagePathInImageStore = $ApplicationTypeName
Write-Host "Copying application package to image store..."
Copy-ServiceFabricApplicationPackage -ApplicationPackagePath $ApplicationPackagePath -ImageStoreConnectionString $imageStoreConnectionString -ApplicationPackagePathInImageStore $applicationPackagePathInImageStore -TimeOutSec $CopyPackageTimeoutSec -CompressPackage:$CompressPackage
if(!$?)
{
throw "Copying of application package to image store failed. Cannot continue with registering the application."
}
## Register application type
Write-Host "Registering application type..."
Register-ServiceFabricApplicationType -ApplicationPathInImageStore $applicationPackagePathInImageStore
if(!$?)
{
throw "Registration of application type failed."
}
# Remove the application package to free system resources.
Remove-ServiceFabricApplicationPackage -ImageStoreConnectionString $imageStoreConnectionString -ApplicationPackagePathInImageStore $applicationPackagePathInImageStore
if(!$?)
{
Write-Host "Removing the application package failed."
}
## Start monitored application upgrade
try
{
Write-Host "Start upgrading application..."
Start-ServiceFabricApplicationUpgrade -ApplicationName $ApplicationName -ApplicationTypeVersion $ApplicationTypeVersion -HealthCheckStableDurationSec 60 -UpgradeDomainTimeoutSec 1200 -UpgradeTimeout 3000 -FailureAction Rollback -Monitored
}
catch
{
Write-Host ("Error starting upgrade. " + $_)
Write-Host "Unregister application type '$ApplicationTypeName' and version '$ApplicationTypeVersion' ..."
Unregister-ServiceFabricApplicationType -ApplicationTypeName $ApplicationTypeName -ApplicationTypeVersion $ApplicationTypeVersion -Force
throw
}
do
{
Write-Host "Waiting for upgrade..."
Start-Sleep -Seconds 3
$upgradeStatus = Get-ServiceFabricApplicationUpgrade -ApplicationName $ApplicationName
} while ($upgradeStatus.UpgradeState -ne "RollingBackCompleted" -and $upgradeStatus.UpgradeState -ne "RollingForwardCompleted" -and $upgradeStatus.UpgradeState -ne "Failed")
if($upgradeStatus.UpgradeState -eq "RollingForwardCompleted")
{
Write-Host "Upgrade completed successfully."
}
elseif($upgradeStatus.UpgradeState -eq "RollingBackCompleted")
{
Write-Error "Upgrade was Rolled back."
}
elseif($upgradeStatus.UpgradeState -eq "Failed")
{
Write-Error "Upgrade Failed."
}
}
Explicação sobre o script
Este script usa os comandos a seguir. Cada comando da tabela é vinculado à documentação específica do comando.
Comando | Observações |
---|---|
Get-ServiceFabricApplication | Obtém todos os aplicativos no cluster do Service Fabric ou um aplicativo específico. |
Get-ServiceFabricApplicationUpgrade | Obtém o status do upgrade de um aplicativo do Service Fabric. |
Get-ServiceFabricApplicationType | Obtém os tipos de aplicativo do Service Fabric registrados no cluster do Service Fabric. |
Unregister-ServiceFabricApplicationType | Cancela o registro de um tipo de aplicativo do Service Fabric. |
Copy-ServiceFabricApplicationPackage | Copia um pacote de aplicativos do Service Fabric para o repositório de imagens. |
Register-ServiceFabricApplicationType | Registra um tipo de aplicativo do Service Fabric. |
Start-ServiceFabricApplicationUpgrade | Faz upgrade de um aplicativo do Service Fabric para a versão do tipo de aplicativo especificado. |
Remove-ServiceFabricApplicationPackage | Remove do repositório de imagens um pacote de aplicativos do Service Fabric. |
Próximas etapas
Para obter mais informações sobre o módulo do PowerShell do Service Fabric, confira Documentação do Azure PowerShell.
Mais exemplos do PowerShell para o Azure Service Fabric podem ser encontrados nos exemplos do Azure PowerShell.