Powershell Deploy Service Fabric Application To Local Cluster

之前写过一篇用 Powershell 部署 Service Fabric Application 到本地集群的随笔,感觉过程有点复杂,这次将流程简化,只需要将应用程序打包,加上配置文件就可以了。

 

# 该脚本用于部署到本机集群,若有错误,欢迎指正。

# Variables
# 自行修改下面参数

## 应用打包后的文件夹路径
$PackagePath="C:\pkg\Release"

## 应用配置文件路径,对应 Service Fabric App/ApplicationParameters/*.xml
$ApplicationParameterFilePath = "C:\ApplicationParameters\Local.1Node.xml"

######################### 以下内容请不要轻易修改 #########################

$Endpoint = 'localhost:19000'
$CopyPackageTimeoutSec = 6000
$CompressPackage = $false

#references
try
{
  ## 该脚本是安装 Service Fabric SDK 后生成的,以下是默认路径,如安装时修改了安装路径,请自行修改。
  . "C:\Program Files\Microsoft SDKs\Service Fabric\Tools\PSModule\ServiceFabricSDK\Utilities.ps1"
}
catch
{
  Write-Warning 'Can not reference files.'
  throw
}

# Connect to the cluster using a client certificate.
Connect-ServiceFabricCluster -ConnectionEndpoint $Endpoint

# Get Application Type Name, Application Name and Application Parameters.
try
{
  $Names = Get-NamesFromApplicationManifest -ApplicationManifestPath "$PackagePath\ApplicationManifest.xml"
  $ApplicationTypeName = $Names.ApplicationTypeName
  $ApplicationTypeVersion = $Names.ApplicationTypeVersion

  $ApplicationName = ([xml] (Get-Content $ApplicationParameterFilePath)).Application.Name

  $ApplicationParameter = Get-ApplicationParametersFromApplicationParameterFile $ApplicationParameterFilePath
}
catch
{
  Write-Warning 'Unable to read Application Type Name, Application Name and Application Parameters.'
  throw
}

try
{
  # Copy the application package to the cluster image store.
  Write-Host 'Copying application to image store...'
  Copy-ServiceFabricApplicationPackage -ApplicationPackagePath $PackagePath -ApplicationPackagePathInImageStore $ApplicationTypeName -TimeOutSec $CopyPackageTimeoutSec -CompressPackage:$CompressPackage
}
catch
{
  Write-Error 'Copying application to image store failed.'
  throw
}

try
{
  # Register the application type.
  Write-Host 'Registering application type...'
  Register-ServiceFabricApplicationType -ApplicationPathInImageStore $ApplicationTypeName -TimeoutSec 6000
}
catch
{
  Write-Error 'Registering application type failed.'
  throw
}

try
{
  # Remove the application package to free system resources.
  Write-Host 'Removing application package from image store...'
  Remove-ServiceFabricApplicationPackage -ApplicationPackagePathInImageStore $ApplicationTypeName -TimeoutSec 6000
}
catch
{
  Write-Error 'Removing application package from image store failed.'
  throw
}

# Create the application instance.
Write-Host 'Creating application...'
New-ServiceFabricApplication -ApplicationName $ApplicationName -ApplicationTypeName $ApplicationTypeName -ApplicationTypeVersion $ApplicationTypeVersion -ApplicationParameter $ApplicationParameter -TimeoutSec 6000
if(!$?)
{
  throw "Creation of application failed."
}

Write-Host 'Create application succeeded.'

posted @ 2018-08-30 10:42  AI柚子  阅读(287)  评论(0编辑  收藏  举报