Created
May 14, 2015 14:21
-
-
Save simplyadrian/678970700c85062785a1 to your computer and use it in GitHub Desktop.
Download and deploy database DDL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Powershell 2.0 | |
if($env:RS_REBOOT){exit 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" | |
# add PowerShell snap-in | |
Import-Module 'SQLPS' | |
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_DB_VOLUME) | |
{ | |
$dest_path = Join-Path "${env:RS_DB_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 database code is not specified. Please check the inputs and try again." | |
} | |
foreach ($f in Get-ChildItem -path $dest_path -Filter *.sql | sort-object) | |
{ | |
$out = "Z:\DEP\" + $f.name.split(".")[0] + ".txt" ; | |
invoke-sqlcmd -ServerInstance $env:DB_SERVERNAME -InputFile $f.fullname | format-table | out-file -filePath $out | |
} | |
} | |
catch | |
{ | |
ResolveError | |
exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment