Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I gracefully take a web app offline during Octopus deployment?

I was a bit remiss to find that Octopus, as amazing as it is, doesn't do anything cute or clever about shutting down your web app before it is upgraded.

In our solution we have two web apps (a website and a separate API web app) that rely on the same database, so while one is being upgraded the other is still live and there is potential that web or API requests are still being serviced while the database is being upgraded.

Not clean!

Clean would be for Octopus to shut down the web apps, wait until they are shut-down and then go ahead with the upgrade, bring the app pools back online once complete.

How can that be achieved?

like image 913
Jason Glover Avatar asked Nov 17 '13 22:11

Jason Glover


People also ask

What happens when octopus deploys to a package drop target?

When Octopus deploys to an offline package drop target it doesn't actually execute the deployment, but will create a folder structure complete with Packages, Scripts, Variable files, Calamari and a batch file to execute the deployment on the actual target server. { {Project Name}}. { {Environment Name}}.

How does octopus deploy work with Azure web apps?

Deploying an Azure Web App with Octopus Deploy behaves very similarly to the Visual Studio publish wizard and uses Web Deploy to synchronize the files in your package to the Azure Web App.

How do I take a web application offline during an automated deployment?

This topic describes how to take a web application offline for the duration of an automated deployment using the Internet Information Services (IIS) Web Deployment Tool (Web Deploy). Users who browse to the web application are redirected to an App_offline.htm file until the deployment is complete.

How do I add an app_offline file to the deployment package?

In the web application project, create a .wpp.targets file that ensures that an App_offline.htm file is added to the deployment package when Web Deploy is invoked. This topic will show you how to perform these procedures.


2 Answers

Selfie-answer!

It is easy to make Octopus-deploy take a little extra care with your deployments, all you need is a couple of extra Execute-Powershell steps in your deployment routine.

Add a new first step to stop the app pool:

# Settings
#---------------
$appPoolName = "PushpayApi" # Or we could set this from an Octopus environment setting.

# Installation
#---------------
Import-Module WebAdministration     
       # see http://technet.microsoft.com/en-us/library/ee790588.aspx

cd IIS:\


if ( (Get-WebAppPoolState -Name $appPoolName).Value -eq "Stopped" )
{
    Write-Host "AppPool already stopped: " + $appPoolName
}

Write-Host "Shutting down the AppPool: " + $appPoolName
Write-Host (Get-WebAppPoolState $appPoolName).Value

# Signal to stop.
Stop-WebAppPool -Name $appPoolName

do
{
    Write-Host (Get-WebAppPoolState $appPoolName).Value
    Start-Sleep -Seconds 1
}
until ( (Get-WebAppPoolState -Name $appPoolName).Value -eq "Stopped" )
# Wait for the apppool to shut down.

And then add another step at the end to restart the app pool:

# Settings
#---------------
$appPoolName = "PushpayApi"

# Installation
#---------------
Import-Module WebAdministration     
       # see http://technet.microsoft.com/en-us/library/ee790588.aspx

cd IIS:\

if ( (Get-WebAppPoolState -Name $appPoolName).Value -eq "Started" )
{
    Write-Host "AppPool already started: " + $appPoolName
}

Write-Host "Starting the AppPool: " + $appPoolName
Write-Host (Get-WebAppPoolState $appPoolName).Value

# To restart the app pool ... 
Start-WebAppPool -Name $appPoolName

Get-WebAppPoolState -Name $appPoolName
like image 63
Jason Glover Avatar answered Oct 26 '22 16:10

Jason Glover


The approach we took was to deploy an _app_offline.htm (App Offline) file with the application. That way we get a nice message explaining why the site is down.

Then when it is time for deployment we use Mircrosofts Webdeploy to rename the it to app_offline.htm. We put the code for the rename in a powershell script that runs as the first step of our Octopus Deployment.

write-host "Website: $WebSiteName"

# Take Website Offline
$path = "$WebDeployPath";
$path

$verb = "-verb:sync";
$verb

# Take root Website offline
$src = "-source:contentPath=```"$WebSiteName/_app_offline.htm```"";
$src

$dest = "-dest:contentPath=```"$WebSiteName/app_offline.htm```"";
$dest
Invoke-Expression "&'$path' $verb $src $dest"; 


# Take Sub Website 1 offline
$src = "-source:contentPath=```"$WebSiteName/WebApp1/_app_offline.htm```"";
$dest = "-dest:contentPath=```"$WebSiteName/WebApp1/app_offline.htm```"";
Invoke-Expression "&'$path' $verb $src $dest"; 

$WebSiteName is usually "Default Web Site". Also note that the ` are not single quotes but actually the backtick character (usually found with the tilda on your keyboard).

Now if octopus is deploying your web site to a new location, your web site will come back online automatically. If you don't want that, you can deploy the new website with the app_offline file allready in place. Then you can use the following script to remove it.

write-host $WebSiteName

# & "c:\Program Files (x86)\IIS\Microsoft Web Deploy V2\msdeploy.exe" -verb:delete -dest:contentPath="$WebSiteName/app_offline.htm"
# those arn't QUOTES!!!!, they are the back accent thing.  

write-host "Website: $WebSiteName"

# Put Web app Online.
$path = "$WebDeployPath";
$path

$verb = "-verb:delete";
$verb

$dest = "-dest:contentPath=```"$WebSiteName/app_offline.htm```"";
$dest
Invoke-Expression "&'$path' $verb $dest"; 

# Put Sub Website Online
$dest = "-dest:contentPath=```"$WebSiteName/WebApp1/app_offline.htm```"";
Invoke-Expression "&'$path' $verb $dest";
like image 34
RMK Avatar answered Oct 26 '22 15:10

RMK