Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save simplyadrian/1fabd81cc8e0e61fa650 to your computer and use it in GitHub Desktop.
Save simplyadrian/1fabd81cc8e0e61fa650 to your computer and use it in GitHub Desktop.
Download a zip file and deploy a web application via powershell
# Powershell 2.0
# Stop and fail script when a command fails.
$errorActionPreference = "Stop"
# load library functions
$rsLibDstDirPath = "$env:rs_sandbox_home\RightScript\lib"
. "$rsLibDstDirPath\tools\PsOutput.ps1"
. "$rsLibDstDirPath\ros\Ros.ps1"
. "$rsLibDstDirPath\tools\Archive.ps1"
. "$rsLibDstDirPath\tools\Checks.ps1"
. "$rsLibDstDirPath\tools\ResolveError.ps1"
. "$rsLibDstDirPath\tools\ExtractReturn.ps1"
try
{
$site = $env:DOWNLOAD_SITE_NAME.Replace(" ", "")
# detects if server OS is 64Bit or 32Bit
# Details http://msdn.microsoft.com/en-us/library/system.intptr.size.aspx
$s7z_cmd = Join-Path $env:programfiles "7-Zip\7z.exe"
if (!(Test-Path $s7z_cmd))
{
$s7z_cmd = Join-Path ${env:programfiles(x86)} "7-Zip\7z.exe"
}
# Check inputs
$zip_url = $env:ZIP_URL
$StorageConteinerName = $env:REMOTE_STORAGE_CONTAINER_APP
$StorageType = $env:REMOTE_STORAGE_ACCOUNT_PROVIDER_APP
$zipName = $env:ZIP_FILE_NAME
if(!$zip_url -and !$StorageConteinerName)
{
throw "ERROR: ZIP_URL and REMOTE_STORAGE_CONTAINER_APP not specified."
}
# Generate destination path and make sure it doesn't exist
$dest_path = Join-Path "Z:\$site\release" $(get-date -uformat "%Y%m%d%H%M%S")
if ($env:RS_IIS_APP_VOLUME)
{
$dest_path = Join-Path "${env:RS_IIS_APP_VOLUME}:\release" $(get-date -uformat "%Y%m%d%H%M%S")
}
# Create dir
if (Test-Path $dest_path)
{
throw "Directory $dest_path already exists."
}
$sleep_delay_sec = 1
$max_retries = 3
$retires = 0
# Proceed to download code from zip archive if specified
if ($zip_url)
{
Write-Host "Creating directory $dest_path"
if ( !(Test-path "$dest_path"))
{
mkdir "$dest_path"
}
# Download zip
$web_zip = Join-Path $dest_path "web.zip"
Write-Host "Downloading $zip_url to $web_zip"
$web = new-object System.Net.WebClient
while ($TRUE)
{
$web.DownloadFile($zip_url, $web_zip)
Write-Host "Exit " $?
if ($?)
{
# success
break
}
else
{
# failed try again
if ($retries -le $max_retries)
{
# sleep a bit before trying again
Write-Host "Sleeping for $sleep_delay_sec"
Start-Sleep -s $sleep_delay_sec
$sleep_delay_sec = $sleep_delay_sec * 2
$retries = $retries + 1
}
else
{
throw "download zip file failed."
}
}
}
cd "$dest_path"
&"$s7z_cmd" x web.zip -y
Remove-Item "$web_zip"
}
elseif ($zipName)
{
Write-Host "Creating directory $dest_path"
if ( !(Test-path "$dest_path"))
{
mkdir "$dest_path"
}
# Init ROS context (and declare inputs used by InitRosContextFromInputs)
# $env:REMOTE_STORAGE_ACCOUNT_PROVIDER_APP
# $env:REMOTE_STORAGE_ACCOUNT_ID_APP
# $env:REMOTE_STORAGE_ACCOUNT_SECRET_APP
# $env:REMOTE_STORAGE_CONTAINER_APP
# $env:REMOTE_STORAGE_BLOCK_SIZE_APP
# $env:REMOTE_STORAGE_THREAD_COUNT_APP
# $env:OBJECT_STORAGE_SERVICENET_APP
# $env:REMOTE_STORAGE_ENDPOINT_URL_APP
if ($env:REMOTE_STORAGE_ACCOUNT_PROVIDER_APP -eq 'OpenStack_Swift')
{
CheckInputNotEmpty 'REMOTE_STORAGE_ENDPOINT_URL_APP'
}
$rosCtx = InitRosContextFromInputsApp | ExtractReturn
$fullPath = Join-Path $dest_path $zipName
RosGetObject $rosCtx $zipName $fullPath
Write-Host "Unzipping ${fullPath}..."
cd "$dest_path"
&"$s7z_cmd" x "$zipName" -y
# Write-Host "Removing ${fullPath}..."
# Remove-Item $fullPath
}
else
{
throw "Location of the application code is not specified. Please check the inputs and try again."
}
$deploy_path = "C:\inetpub"
if (Test-path $deploy_path\$env:WEB_SITE_NAME)
{
#Remove previous deployment if it exists.
Remove-Item $deploy_path\$env:WEB_SITE_NAME -recurse -force
Write-Host "Application code removed successfully"
}
else
{
Write-Host "$deploy_path\$env:WEB_SITE_NAME does not exist. Continuing on."
}
#Move newly downloaded published sites into place
mv $dest_path\_PublishedWebsites\$env:WEB_SITE_NAME $deploy_path
Write-Host "Application code moved succssfully to $deploy_path"
# Set RS_IIS_LAST_RELEASE environment variable
Write-Host "Setting RS_IIS_LAST_RELEASE environment variable to '$deploy_path\$env:WEB_SITE_NAME'..."
[environment]::SetEnvironmentVariable("RS_IIS_LAST_RELEASE", "$deploy_path\$env:WEB_SITE_NAME", "Machine")
Write-Host "Application code downloaded succssfully to '$dest_path'"
}
catch
{
ResolveError
exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment