Skip to content

Instantly share code, notes, and snippets.

@Dalmirog-zz
Created March 6, 2015 22:03
Show Gist options
  • Select an option

  • Save Dalmirog-zz/2dc9f64c70066d6eaa70 to your computer and use it in GitHub Desktop.

Select an option

Save Dalmirog-zz/2dc9f64c70066d6eaa70 to your computer and use it in GitHub Desktop.
tweaked step template to create web app
{
"Id": "ActionTemplates-67",
"Name": "IIS Application - Create",
"Description": "Create an IIS virtual application (a virtual directory with an application pool)",
"ActionType": "Octopus.Script",
"Version": 2,
"Properties": {
"Octopus.Action.Script.ScriptBody": "## --------------------------------------------------------------------------------------\r\n## Input\r\n## --------------------------------------------------------------------------------------\r\n\r\n$virtualPath = $OctopusParameters['VirtualPath']\r\n$physicalPath = $OctopusParameters['PhysicalPath']\r\n$applicationPoolName = $OctopusParameters['ApplicationPoolName']\r\n$parentSite = $OctopusParameters['ParentSite']\r\n\r\n## --------------------------------------------------------------------------------------\r\n## Helpers\r\n## --------------------------------------------------------------------------------------\r\n# Helper for validating input parameters\r\nfunction Validate-Parameter([string]$foo, [string[]]$validInput, $parameterName) {\r\n Write-Host \"${parameterName}: $foo\"\r\n if (! $foo) {\r\n throw \"No value was set for $parameterName, and it cannot be empty\"\r\n }\r\n \r\n if ($validInput) {\r\n if (! $validInput -contains $foo) {\r\n throw \"'$input' is not a valid input for '$parameterName'\"\r\n }\r\n }\r\n \r\n}\r\n\r\n# Helper to run a block with a retry if things go wrong\r\n$maxFailures = 5\r\n$sleepBetweenFailures = Get-Random -minimum 1 -maximum 4\r\nfunction Execute-WithRetry([ScriptBlock] $command) {\r\n\t$attemptCount = 0\r\n\t$operationIncomplete = $true\r\n\r\n\twhile ($operationIncomplete -and $attemptCount -lt $maxFailures) {\r\n\t\t$attemptCount = ($attemptCount + 1)\r\n\r\n\t\tif ($attemptCount -ge 2) {\r\n\t\t\tWrite-Output \"Waiting for $sleepBetweenFailures seconds before retrying...\"\r\n\t\t\tStart-Sleep -s $sleepBetweenFailures\r\n\t\t\tWrite-Output \"Retrying...\"\r\n\t\t}\r\n\r\n\t\ttry {\r\n\t\t\t& $command\r\n\r\n\t\t\t$operationIncomplete = $false\r\n\t\t} catch [System.Exception] {\r\n\t\t\tif ($attemptCount -lt ($maxFailures)) {\r\n\t\t\t\tWrite-Output (\"Attempt $attemptCount of $maxFailures failed: \" + $_.Exception.Message)\r\n\t\t\t\r\n\t\t\t}\r\n\t\t\telse {\r\n\t\t\t throw \"Failed to execute command\"\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n\r\n## --------------------------------------------------------------------------------------\r\n## Configuration\r\n## --------------------------------------------------------------------------------------\r\nValidate-Parameter $virtualPath -parameterName \"Virtual path\"\r\nValidate-Parameter $physicalPath -parameterName \"Physical path\"\r\nValidate-Parameter $applicationPoolName -parameterName \"Application pool\"\r\nValidate-Parameter $parentSite -parameterName \"Parent site\"\r\n\r\nAdd-PSSnapin WebAdministration -ErrorAction SilentlyContinue\r\nImport-Module WebAdministration -ErrorAction SilentlyContinue\r\n\r\n\r\n## --------------------------------------------------------------------------------------\r\n## Run\r\n## --------------------------------------------------------------------------------------\r\n\r\nWrite-Host \"Getting web site $parentSite\"\r\n$site = Get-Website -name $parentSite\r\nif (!$site) {\r\n throw \"The web site '$parentSite' does not exist. Please create the site first.\"\r\n}\r\n\r\n$name = $virtualPath\r\n\r\n$existing = Get-WebApplication -site $parentSite -Name $name\r\n\r\n\r\nExecute-WithRetry { \r\n if (!$existing) {\r\n Write-Host \"Creating web application '$name'\"\r\n New-WebApplication -Site $parentSite -Name $name -ApplicationPool $applicationPoolName -PhysicalPath $physicalPath\r\n Write-Host \"Web application created\"\r\n } else {\r\n Write-Host \"The web application '$name' already exists. Updating physical path:\"\r\n\r\n Set-ItemProperty IIS:\\Sites\\$parentSite\\$name -name physicalPath -value $physicalPath\r\n\r\n Write-Host \"Physical path changed to: $physicalPath\"\r\n }\r\n}"
},
"SensitiveProperties": {},
"Parameters": [
{
"Name": "VirtualPath",
"Label": "Virtual path",
"HelpText": "The name of the application to create. For example, to serve an application that will be available at `/myapp`, enter `myapp`. To create an application under a parent virtual directory or application, separate with slashes - for example: `/applications/myapp`",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "PhysicalPath",
"Label": "Physical path",
"HelpText": "Physical folder that the application will serve files from. Example: `C:\\MyApp`.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "ApplicationPoolName",
"Label": "Application pool",
"HelpText": "The name of the application pool that the application will run under. The application pool must already exist.",
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "ParentSite",
"Label": "Parent site",
"HelpText": "The name of the IIS web site to attach the application to. For example, to put the application under the default web site, enter:\n\n Default Web Site",
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
}
],
"LastModifiedOn": "2015-03-06T21:32:25.936+00:00",
"LastModifiedBy": "dalmiro",
"$Meta": {
"ExportedAt": "2015-03-06T22:02:05.895Z",
"OctopusVersion": "2.6.2.845",
"Type": "ActionTemplate"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment