Skip to content

Instantly share code, notes, and snippets.

@Dalmirog-zz
Created March 25, 2015 21:50
Show Gist options
  • Save Dalmirog-zz/ee761808edaf9536f675 to your computer and use it in GitHub Desktop.
Save Dalmirog-zz/ee761808edaf9536f675 to your computer and use it in GitHub Desktop.
Updated Octopus Step template to create IIS virtual directory
{
"Id": "ActionTemplates-129",
"Name": "IIS Virtual Directory - Create",
"Description": "Create an IIS virtual directory.",
"ActionType": "Octopus.Script",
"Version": 2,
"Properties": {
"Octopus.Action.Script.ScriptBody": "## --------------------------------------------------------------------------------------\n## Input\n## --------------------------------------------------------------------------------------\n\n$virtualPath = $OctopusParameters['VirtualDirectoryVirtualPath']\n$physicalPath = $OctopusParameters['PhysicalPath']\n$parentSite = $OctopusParameters['ParentSite']\n\n## --------------------------------------------------------------------------------------\n## Helpers\n## --------------------------------------------------------------------------------------\n# Helper for validating input parameters\nfunction Validate-Parameter([string]$foo, [string[]]$validInput, $parameterName) {\n Write-Host \"${parameterName}: $foo\"\n if (! $foo) {\n throw \"No value was set for $parameterName, and it cannot be empty\"\n }\n \n if ($validInput) {\n if (! $validInput -contains $foo) {\n throw \"'$input' is not a valid input for '$parameterName'\"\n }\n }\n \n}\n\n# Helper to run a block with a retry if things go wrong\n$maxFailures = 5\n$sleepBetweenFailures = Get-Random -minimum 1 -maximum 4\nfunction Execute-WithRetry([ScriptBlock] $command) {\n\t$attemptCount = 0\n\t$operationIncomplete = $true\n\n\twhile ($operationIncomplete -and $attemptCount -lt $maxFailures) {\n\t\t$attemptCount = ($attemptCount + 1)\n\n\t\tif ($attemptCount -ge 2) {\n\t\t\tWrite-Output \"Waiting for $sleepBetweenFailures seconds before retrying...\"\n\t\t\tStart-Sleep -s $sleepBetweenFailures\n\t\t\tWrite-Output \"Retrying...\"\n\t\t}\n\n\t\ttry {\n\t\t\t& $command\n\n\t\t\t$operationIncomplete = $false\n\t\t} catch [System.Exception] {\n\t\t\tif ($attemptCount -lt ($maxFailures)) {\n\t\t\t\tWrite-Output (\"Attempt $attemptCount of $maxFailures failed: \" + $_.Exception.Message)\n\t\t\t\n\t\t\t}\n\t\t\telse {\n\t\t\t throw \"Failed to execute command\"\n\t\t\t}\n\t\t}\n\t}\n}\n\n## --------------------------------------------------------------------------------------\n## Configuration\n## --------------------------------------------------------------------------------------\nValidate-Parameter $virtualPath -parameterName \"Virtual path\"\nValidate-Parameter $physicalPath -parameterName \"Physical path\"\nValidate-Parameter $parentSite -parameterName \"Parent site\"\n\nAdd-PSSnapin WebAdministration -ErrorAction SilentlyContinue\nImport-Module WebAdministration -ErrorAction SilentlyContinue\n\n\n## --------------------------------------------------------------------------------------\n## Run\n## --------------------------------------------------------------------------------------\n\nWrite-Host \"Getting web site $parentSite\"\n$site = Get-Website -name $parentSite\nif (!$site) {\n throw \"The web site '$parentSite' does not exist. Please create the site first.\"\n}\n\n$parts = $virtualPath -split \"[/\\\\]\"\n$name = \"\"\n\nfor ($i = 0; $i -lt $parts.Length; $i++) {\n $name = $name + \"/\" + $parts[$i]\n $name = $name.TrimStart('/').TrimEnd('/')\n if ($i -eq $parts.Length - 1) {\n \n }\n elseif ([string]::IsNullOrEmpty($name) -eq $false -and $name -ne \"/\") {\n Write-Host \"Ensuring parent exists: $name\"\n\n $app = Get-WebApplication -Name $name -Site $parentSite\n\n if (!$app) {\n $vdir = Get-WebVirtualDirectory -Name $name -site $parentSite\n if (!$vdir) {\n throw \"The application or virtual directory '$name' does not exist\"\n }\n }\n }\n}\n\n$existing = Get-WebVirtualDirectory -site $parentSite -Name $name\n\n\nExecute-WithRetry { \n if (!$existing) {\n Write-Host \"Creating virtual directory '$name'\"\n New-WebVirtualDirectory -Site $parentSite -Name $name -PhysicalPath $physicalPath\n Write-Host \"Virtual directory created\"\n } else {\n Write-Host \"The virtual directory '$name' already exists. Updating physical path:\"\n\n Set-ItemProperty IIS:\\Sites\\$parentSite\\$name -name physicalPath -value $physicalPath\n\n Write-Host \"Physical path changed to: $physicalPath\"\n }\n}"
},
"SensitiveProperties": {},
"Parameters": [
{
"Name": "VirtualDirectoryVirtualPath", #Just changed this variable name to something more specific
"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": "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-25T21:44:12.797+00:00",
"LastModifiedBy": "dalmiro",
"$Meta": {
"ExportedAt": "2015-03-25T21:47:48.954Z",
"OctopusVersion": "2.6.4.951",
"Type": "ActionTemplate"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment