You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
8.7 KiB

  1. <#
  2. .SYNOPSIS
  3. Deploys a Service Fabric application type to a cluster.
  4. .DESCRIPTION
  5. This script deploys a Service Fabric application type to a cluster. It is invoked by Visual Studio when deploying a Service Fabric Application project.
  6. .NOTES
  7. WARNING: This script file is invoked by Visual Studio. Its parameters must not be altered but its logic can be customized as necessary.
  8. .PARAMETER PublishProfileFile
  9. Path to the file containing the publish profile.
  10. .PARAMETER ApplicationPackagePath
  11. Path to the folder of the packaged Service Fabric application.
  12. .PARAMETER DeployOnly
  13. Indicates that the Service Fabric application should not be created or upgraded after registering the application type.
  14. .PARAMETER ApplicationParameter
  15. Hashtable of the Service Fabric application parameters to be used for the application.
  16. .PARAMETER UnregisterUnusedApplicationVersionsAfterUpgrade
  17. Indicates whether to unregister any unused application versions that exist after an upgrade is finished.
  18. .PARAMETER OverrideUpgradeBehavior
  19. Indicates the behavior used to override the upgrade settings specified by the publish profile.
  20. 'None' indicates that the upgrade settings will not be overridden.
  21. 'ForceUpgrade' indicates that an upgrade will occur with default settings, regardless of what is specified in the publish profile.
  22. 'VetoUpgrade' indicates that an upgrade will not occur, regardless of what is specified in the publish profile.
  23. .PARAMETER UseExistingClusterConnection
  24. Indicates that the script should make use of an existing cluster connection that has already been established in the PowerShell session. The cluster connection parameters configured in the publish profile are ignored.
  25. .PARAMETER OverwriteBehavior
  26. Overwrite Behavior if an application exists in the cluster with the same name. Available Options are Never, Always, SameAppTypeAndVersion. This setting is not applicable when upgrading an application.
  27. 'Never' will not remove the existing application. This is the default behavior.
  28. 'Always' will remove the existing application even if its Application type and Version is different from the application being created.
  29. 'SameAppTypeAndVersion' will remove the existing application only if its Application type and Version is same as the application being created.
  30. .PARAMETER SkipPackageValidation
  31. Switch signaling whether the package should be validated or not before deployment.
  32. .PARAMETER SecurityToken
  33. A security token for authentication to cluster management endpoints. Used for silent authentication to clusters that are protected by Azure Active Directory.
  34. .PARAMETER CopyPackageTimeoutSec
  35. Timeout in seconds for copying application package to image store.
  36. .EXAMPLE
  37. . Scripts\Deploy-FabricApplication.ps1 -ApplicationPackagePath 'pkg\Debug'
  38. Deploy the application using the default package location for a Debug build.
  39. .EXAMPLE
  40. . Scripts\Deploy-FabricApplication.ps1 -ApplicationPackagePath 'pkg\Debug' -DoNotCreateApplication
  41. Deploy the application but do not create the application instance.
  42. .EXAMPLE
  43. . Scripts\Deploy-FabricApplication.ps1 -ApplicationPackagePath 'pkg\Debug' -ApplicationParameter @{CustomParameter1='MyValue'; CustomParameter2='MyValue'}
  44. Deploy the application by providing values for parameters that are defined in the application manifest.
  45. #>
  46. Param
  47. (
  48. [String]
  49. $PublishProfileFile,
  50. [String]
  51. $ApplicationPackagePath,
  52. [Switch]
  53. $DeployOnly,
  54. [Hashtable]
  55. $ApplicationParameter,
  56. [Boolean]
  57. $UnregisterUnusedApplicationVersionsAfterUpgrade,
  58. [String]
  59. [ValidateSet('None', 'ForceUpgrade', 'VetoUpgrade')]
  60. $OverrideUpgradeBehavior = 'None',
  61. [Switch]
  62. $UseExistingClusterConnection,
  63. [String]
  64. [ValidateSet('Never','Always','SameAppTypeAndVersion')]
  65. $OverwriteBehavior = 'Never',
  66. [Switch]
  67. $SkipPackageValidation,
  68. [String]
  69. $SecurityToken,
  70. [int]
  71. $CopyPackageTimeoutSec
  72. )
  73. function Read-XmlElementAsHashtable
  74. {
  75. Param (
  76. [System.Xml.XmlElement]
  77. $Element
  78. )
  79. $hashtable = @{}
  80. if ($Element.Attributes)
  81. {
  82. $Element.Attributes |
  83. ForEach-Object {
  84. $boolVal = $null
  85. if ([bool]::TryParse($_.Value, [ref]$boolVal)) {
  86. $hashtable[$_.Name] = $boolVal
  87. }
  88. else {
  89. $hashtable[$_.Name] = $_.Value
  90. }
  91. }
  92. }
  93. return $hashtable
  94. }
  95. function Read-PublishProfile
  96. {
  97. Param (
  98. [ValidateScript({Test-Path $_ -PathType Leaf})]
  99. [String]
  100. $PublishProfileFile
  101. )
  102. $publishProfileXml = [Xml] (Get-Content $PublishProfileFile)
  103. $publishProfile = @{}
  104. $publishProfile.ClusterConnectionParameters = Read-XmlElementAsHashtable $publishProfileXml.PublishProfile.Item("ClusterConnectionParameters")
  105. $publishProfile.UpgradeDeployment = Read-XmlElementAsHashtable $publishProfileXml.PublishProfile.Item("UpgradeDeployment")
  106. $publishProfile.CopyPackageParameters = Read-XmlElementAsHashtable $publishProfileXml.PublishProfile.Item("CopyPackageParameters")
  107. if ($publishProfileXml.PublishProfile.Item("UpgradeDeployment"))
  108. {
  109. $publishProfile.UpgradeDeployment.Parameters = Read-XmlElementAsHashtable $publishProfileXml.PublishProfile.Item("UpgradeDeployment").Item("Parameters")
  110. if ($publishProfile.UpgradeDeployment["Mode"])
  111. {
  112. $publishProfile.UpgradeDeployment.Parameters[$publishProfile.UpgradeDeployment["Mode"]] = $true
  113. }
  114. }
  115. $publishProfileFolder = (Split-Path $PublishProfileFile)
  116. $publishProfile.ApplicationParameterFile = [System.IO.Path]::Combine($PublishProfileFolder, $publishProfileXml.PublishProfile.ApplicationParameterFile.Path)
  117. return $publishProfile
  118. }
  119. $LocalFolder = (Split-Path $MyInvocation.MyCommand.Path)
  120. if (!$PublishProfileFile)
  121. {
  122. $PublishProfileFile = "$LocalFolder\..\PublishProfiles\Local.xml"
  123. }
  124. if (!$ApplicationPackagePath)
  125. {
  126. $ApplicationPackagePath = "$LocalFolder\..\pkg\Release"
  127. }
  128. $ApplicationPackagePath = Resolve-Path $ApplicationPackagePath
  129. $publishProfile = Read-PublishProfile $PublishProfileFile
  130. if (-not $UseExistingClusterConnection)
  131. {
  132. $ClusterConnectionParameters = $publishProfile.ClusterConnectionParameters
  133. if ($SecurityToken)
  134. {
  135. $ClusterConnectionParameters["SecurityToken"] = $SecurityToken
  136. }
  137. try
  138. {
  139. [void](Connect-ServiceFabricCluster @ClusterConnectionParameters)
  140. }
  141. catch [System.Fabric.FabricObjectClosedException]
  142. {
  143. Write-Warning "Service Fabric cluster may not be connected."
  144. throw
  145. }
  146. }
  147. $RegKey = "HKLM:\SOFTWARE\Microsoft\Service Fabric SDK"
  148. $ModuleFolderPath = (Get-ItemProperty -Path $RegKey -Name FabricSDKPSModulePath).FabricSDKPSModulePath
  149. Import-Module "$ModuleFolderPath\ServiceFabricSDK.psm1"
  150. $IsUpgrade = ($publishProfile.UpgradeDeployment -and $publishProfile.UpgradeDeployment.Enabled -and $OverrideUpgradeBehavior -ne 'VetoUpgrade') -or $OverrideUpgradeBehavior -eq 'ForceUpgrade'
  151. $PublishParameters = @{
  152. 'ApplicationPackagePath' = $ApplicationPackagePath
  153. 'ApplicationParameterFilePath' = $publishProfile.ApplicationParameterFile
  154. 'ApplicationParameter' = $ApplicationParameter
  155. 'ErrorAction' = 'Stop'
  156. }
  157. if ($publishProfile.CopyPackageParameters.CopyPackageTimeoutSec)
  158. {
  159. $PublishParameters['CopyPackageTimeoutSec'] = $publishProfile.CopyPackageParameters.CopyPackageTimeoutSec
  160. }
  161. if ($publishProfile.CopyPackageParameters.CompressPackage)
  162. {
  163. $PublishParameters['CompressPackage'] = $publishProfile.CopyPackageParameters.CompressPackage
  164. }
  165. # CopyPackageTimeoutSec parameter overrides the value from the publish profile
  166. if ($CopyPackageTimeoutSec)
  167. {
  168. $PublishParameters['CopyPackageTimeoutSec'] = $CopyPackageTimeoutSec
  169. }
  170. if ($IsUpgrade)
  171. {
  172. $Action = "RegisterAndUpgrade"
  173. if ($DeployOnly)
  174. {
  175. $Action = "Register"
  176. }
  177. $UpgradeParameters = $publishProfile.UpgradeDeployment.Parameters
  178. if ($OverrideUpgradeBehavior -eq 'ForceUpgrade')
  179. {
  180. # Warning: Do not alter these upgrade parameters. It will create an inconsistency with Visual Studio's behavior.
  181. $UpgradeParameters = @{ UnmonitoredAuto = $true; Force = $true }
  182. }
  183. $PublishParameters['Action'] = $Action
  184. $PublishParameters['UpgradeParameters'] = $UpgradeParameters
  185. $PublishParameters['UnregisterUnusedVersions'] = $UnregisterUnusedApplicationVersionsAfterUpgrade
  186. Publish-UpgradedServiceFabricApplication @PublishParameters
  187. }
  188. else
  189. {
  190. $Action = "RegisterAndCreate"
  191. if ($DeployOnly)
  192. {
  193. $Action = "Register"
  194. }
  195. $PublishParameters['Action'] = $Action
  196. $PublishParameters['OverwriteBehavior'] = $OverwriteBehavior
  197. $PublishParameters['SkipPackageValidation'] = $SkipPackageValidation
  198. Publish-NewServiceFabricApplication @PublishParameters
  199. }