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.

272 lines
9.3 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. [int]
  73. $RegisterApplicationTypeTimeoutSec
  74. )
  75. function Read-XmlElementAsHashtable
  76. {
  77. Param (
  78. [System.Xml.XmlElement]
  79. $Element
  80. )
  81. $hashtable = @{}
  82. if ($Element.Attributes)
  83. {
  84. $Element.Attributes |
  85. ForEach-Object {
  86. $boolVal = $null
  87. if ([bool]::TryParse($_.Value, [ref]$boolVal)) {
  88. $hashtable[$_.Name] = $boolVal
  89. }
  90. else {
  91. $hashtable[$_.Name] = $_.Value
  92. }
  93. }
  94. }
  95. return $hashtable
  96. }
  97. function Read-PublishProfile
  98. {
  99. Param (
  100. [ValidateScript({Test-Path $_ -PathType Leaf})]
  101. [String]
  102. $PublishProfileFile
  103. )
  104. $publishProfileXml = [Xml] (Get-Content $PublishProfileFile)
  105. $publishProfile = @{}
  106. $publishProfile.ClusterConnectionParameters = Read-XmlElementAsHashtable $publishProfileXml.PublishProfile.Item("ClusterConnectionParameters")
  107. $publishProfile.UpgradeDeployment = Read-XmlElementAsHashtable $publishProfileXml.PublishProfile.Item("UpgradeDeployment")
  108. $publishProfile.CopyPackageParameters = Read-XmlElementAsHashtable $publishProfileXml.PublishProfile.Item("CopyPackageParameters")
  109. $publishProfile.RegisterApplicationParameters = Read-XmlElementAsHashtable $publishProfileXml.PublishProfile.Item("RegisterApplicationParameters")
  110. if ($publishProfileXml.PublishProfile.Item("UpgradeDeployment"))
  111. {
  112. $publishProfile.UpgradeDeployment.Parameters = Read-XmlElementAsHashtable $publishProfileXml.PublishProfile.Item("UpgradeDeployment").Item("Parameters")
  113. if ($publishProfile.UpgradeDeployment["Mode"])
  114. {
  115. $publishProfile.UpgradeDeployment.Parameters[$publishProfile.UpgradeDeployment["Mode"]] = $true
  116. }
  117. }
  118. $publishProfileFolder = (Split-Path $PublishProfileFile)
  119. $publishProfile.ApplicationParameterFile = [System.IO.Path]::Combine($PublishProfileFolder, $publishProfileXml.PublishProfile.ApplicationParameterFile.Path)
  120. return $publishProfile
  121. }
  122. $LocalFolder = (Split-Path $MyInvocation.MyCommand.Path)
  123. if (!$PublishProfileFile)
  124. {
  125. $PublishProfileFile = "$LocalFolder\..\PublishProfiles\Local.xml"
  126. }
  127. if (!$ApplicationPackagePath)
  128. {
  129. $ApplicationPackagePath = "$LocalFolder\..\pkg\Release"
  130. }
  131. $ApplicationPackagePath = Resolve-Path $ApplicationPackagePath
  132. $publishProfile = Read-PublishProfile $PublishProfileFile
  133. if (-not $UseExistingClusterConnection)
  134. {
  135. $ClusterConnectionParameters = $publishProfile.ClusterConnectionParameters
  136. if ($SecurityToken)
  137. {
  138. $ClusterConnectionParameters["SecurityToken"] = $SecurityToken
  139. }
  140. try
  141. {
  142. [void](Connect-ServiceFabricCluster @ClusterConnectionParameters)
  143. }
  144. catch [System.Fabric.FabricObjectClosedException]
  145. {
  146. Write-Warning "Service Fabric cluster may not be connected."
  147. throw
  148. }
  149. }
  150. $RegKey = "HKLM:\SOFTWARE\Microsoft\Service Fabric SDK"
  151. $ModuleFolderPath = (Get-ItemProperty -Path $RegKey -Name FabricSDKPSModulePath).FabricSDKPSModulePath
  152. Import-Module "$ModuleFolderPath\ServiceFabricSDK.psm1"
  153. $IsUpgrade = ($publishProfile.UpgradeDeployment -and $publishProfile.UpgradeDeployment.Enabled -and $OverrideUpgradeBehavior -ne 'VetoUpgrade') -or $OverrideUpgradeBehavior -eq 'ForceUpgrade'
  154. $PublishParameters = @{
  155. 'ApplicationPackagePath' = $ApplicationPackagePath
  156. 'ApplicationParameterFilePath' = $publishProfile.ApplicationParameterFile
  157. 'ApplicationParameter' = $ApplicationParameter
  158. 'ErrorAction' = 'Stop'
  159. }
  160. if ($publishProfile.CopyPackageParameters.CopyPackageTimeoutSec)
  161. {
  162. $PublishParameters['CopyPackageTimeoutSec'] = $publishProfile.CopyPackageParameters.CopyPackageTimeoutSec
  163. }
  164. if ($publishProfile.CopyPackageParameters.CompressPackage)
  165. {
  166. $PublishParameters['CompressPackage'] = $publishProfile.CopyPackageParameters.CompressPackage
  167. }
  168. if ($publishProfile.RegisterApplicationParameters.RegisterApplicationTypeTimeoutSec)
  169. {
  170. $PublishParameters['RegisterApplicationTypeTimeoutSec'] = $publishProfile.RegisterApplicationParameters.RegisterApplicationTypeTimeoutSec
  171. }
  172. # CopyPackageTimeoutSec parameter overrides the value from the publish profile
  173. if ($CopyPackageTimeoutSec)
  174. {
  175. $PublishParameters['CopyPackageTimeoutSec'] = $CopyPackageTimeoutSec
  176. }
  177. # RegisterApplicationTypeTimeoutSec parameter overrides the value from the publish profile
  178. if ($RegisterApplicationTypeTimeoutSec)
  179. {
  180. $PublishParameters['RegisterApplicationTypeTimeoutSec'] = $RegisterApplicationTypeTimeoutSec
  181. }
  182. if ($IsUpgrade)
  183. {
  184. $Action = "RegisterAndUpgrade"
  185. if ($DeployOnly)
  186. {
  187. $Action = "Register"
  188. }
  189. $UpgradeParameters = $publishProfile.UpgradeDeployment.Parameters
  190. if ($OverrideUpgradeBehavior -eq 'ForceUpgrade')
  191. {
  192. # Warning: Do not alter these upgrade parameters. It will create an inconsistency with Visual Studio's behavior.
  193. $UpgradeParameters = @{ UnmonitoredAuto = $true; Force = $true }
  194. }
  195. $PublishParameters['Action'] = $Action
  196. $PublishParameters['UpgradeParameters'] = $UpgradeParameters
  197. $PublishParameters['UnregisterUnusedVersions'] = $UnregisterUnusedApplicationVersionsAfterUpgrade
  198. Publish-UpgradedServiceFabricApplication @PublishParameters
  199. }
  200. else
  201. {
  202. $Action = "RegisterAndCreate"
  203. if ($DeployOnly)
  204. {
  205. $Action = "Register"
  206. }
  207. $PublishParameters['Action'] = $Action
  208. $PublishParameters['OverwriteBehavior'] = $OverwriteBehavior
  209. $PublishParameters['SkipPackageValidation'] = $SkipPackageValidation
  210. Publish-NewServiceFabricApplication @PublishParameters
  211. }