Skip to content

Instantly share code, notes, and snippets.

@matejskubic
Created September 25, 2015 01:01
Show Gist options
  • Save matejskubic/b9e2aac8db5cc2949709 to your computer and use it in GitHub Desktop.
Save matejskubic/b9e2aac8db5cc2949709 to your computer and use it in GitHub Desktop.
Publish Build output to azure
[CmdletBinding(DefaultParameterSetName="UseContextVariables")]
Param(
# folder to upload
[parameter(Position=0, ParameterSetName="UseArgs", Mandatory=$true)]
[string]$dropLocation = (Get-Variable $dropLocation -ValueOnly -ErrorAction SilentlyContinue)
,
# upload location - sugned Azure blob url with upload permission
[uri]$blobFileUri="http://$_StorageAccount.blob.core.windows.net/$_Company/Appl.zip?sr=b&sv=2015-02-21&si=deploy$Company&sig=$_Signature"
,
# build version - added to Metadata.AxModelVersion
[parameter(ParameterSetName="UseArgs")]
[string]$CurrentVersion = (Get-Variable CurrentVersion -ValueOnly -ErrorAction SilentlyContinue)
,
# addition metadata key-value pair
[System.Collections.IDictionary]$Metadata = @{}
,
# use context (environment) variables instead args (used by build script)
[parameter(ParameterSetName="UseContextVariables")]
[switch]$UseContextVariables
)
Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)"
if ($PSVersionTable.CLRVersion.Major -lt 4)
{
# we need .NET 4 or newer to use System.IO.Compression - restart in powershell 3
if (!$CurrentVersion)
{
$CurrentVersion = "0"
}
Write-Verbose -Verbose "Starting powershell v3"
$newCommand = (Get-Item $MyInvocation.MyCommand.Path).FullName
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Version 3 -NonInteractive -NoLogo -NoProfile -ExecutionPolicy Unrestricted -File $newCommand $dropLocation -CurrentVersion $CurrentVersion
return
}
try
{
Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem
if ($CurrentVersion)
{
$Metadata."AxModelVersion" = $CurrentVersion
}
$tmpZipPath=[System.IO.Path]::Combine([System.IO.Path]::GetTempPath(),[System.IO.Path]::GetRandomFileName())
md $tmpZipPath
$tmpZipFilename=[System.IO.Path]::Combine($tmpZipPath, 'Appl.zip')
[System.IO.Compression.ZipFile]::CreateFromDirectory("$dropLocation\Application\Appl", $tmpZipFilename)
$httpHeaders = @{}
$httpHeaders."x-ms-version" = "2015-02-21"
#$httpHeaders."x-ms-blob-type" = "BlockBlob"
if ($Metadata -ne $null)
{
$Metadata.GetEnumerator() | % {$httpHeaders."x-ms-meta-$([System.Xml.XmlConvert]::EncodeLocalName($_.Key))" = [System.Net.WebUtility]::UrlEncode($_.Value)}
}
$fileInfo = Get-Item $tmpZipFilename
$fileStream = $fileInfo.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read)
$blockSize = 4*1024*1024
$blockBytes = New-Object byte[] $blockSize
try
{
function AzureAppend()
{
$httpHeaders."x-ms-blob-type" = "AppendBlob"
$httpHeaders."x-ms-blob-condition-maxsize" = $fileInfo.Length.ToString()
$response = Invoke-RestMethod -Uri $blobFileUri -Method Put -Headers $httpHeaders -Proxy http://localhost:8888
$blobFileUriAppend = "$blobFileUri&comp=appendblock"
$blobBytesUploaded = 0
while ($blockBytesRead = $fileStream.Read($blockBytes, 0, $blockSize))
{
$httpHeaders."x-ms-blob-condition-appendpos" = $blobBytesUploaded.ToString()
if ($blockBytesRead -ne $blockSize)
{
[byte[]]$blockBytesToWrite = $blockBytes[0..($blockBytesRead-1)]
}
else
{
$blockBytesToWrite = $blockBytes
}
Write-Verbose -Verbose "Putting $blockBytesRead bytes at offset $blobBytesUploaded"
$response = Invoke-RestMethod -Uri $blobFileUriAppend -Method Put -Headers $httpHeaders -Body $blockBytesToWrite -Proxy http://localhost:8888
$blobBytesUploaded+= $blockBytesRead
}
}
function AzureBlockList()
{
$blockIdListExisting = New-Object System.Collections.ArrayList
$blockIdListNew = New-Object System.Collections.ArrayList
$checksumProvider = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
Invoke-RestMethod -Uri "$blobFileUri&comp=blocklist&blocklisttype=all" -Method Get -Headers $httpHeaders -OutFile tmp-AzureBlockList.xml -Proxy http://localhost:8888
$existingBlocksXml = New-Object xml
$existingBlocksXml.Load("tmp-AzureBlockList.xml")
$existingBlocksXml.SelectNodes("//Name") | % {[void]$blockIdListExisting.Add($_.InnerText)}
$blobBytesProcessed = 0
$blobBytesUploaded = 0
$blockCount = 0
while ($blockBytesRead = $fileStream.Read($blockBytes, 0, $blockSize))
{
$blobBytesProcessed+= $blockBytesRead
$blockCount++
if ($blockBytesRead -ne $blockSize)
{
[byte[]]$blockBytesToWrite = $blockBytes[0..($blockBytesRead-1)]
}
else
{
$blockBytesToWrite = $blockBytes
}
$blockChecksum = $checksumProvider.ComputeHash($blockBytesToWrite)
$curBlockId = [convert]::ToBase64String($blockChecksum)
[void]$blockIdListNew.Add($curBlockId)
if (!$blockIdListExisting.Contains($curBlockId))
{
$blobFileUriPutBlock = "$blobFileUri&comp=block&blockid=$([System.Net.WebUtility]::UrlEncode($curBlockId))"
Write-Verbose -Verbose "Putting $blockBytesRead bytes at offset $blobBytesUploaded"
$response = Invoke-RestMethod -Uri $blobFileUriPutBlock -Method Put -Headers $httpHeaders -Body $blockBytesToWrite -Proxy http://localhost:8888
$blobBytesUploaded+= $blockBytesRead
[void]$blockIdListExisting.Add($curBlockId)
}
}
$response = Invoke-RestMethod -Uri "$blobFileUri&comp=blocklist&blocklisttype=all" -Method Get -Headers $httpHeaders -Proxy http://localhost:8888
[xml]$blockList = "<BlockList />"
$blockIdListNew | % {
$blockList.DocumentElement.AppendChild($blockList.CreateElement("Latest")).InnerText = $_
}
$blobFileUriBlocklist = "$blobFileUri&comp=blocklist"
$response = Invoke-RestMethod -Uri $blobFileUriBlocklist -Method Put -Headers $httpHeaders -Body $blockList -Proxy http://localhost:8888
}
AzureBlockList
$blobFileUriSnapshot = "$blobFileUri&comp=snapshot"
$response = Invoke-RestMethod -Uri $blobFileUriSnapshot -Method Put -Headers $httpHeaders
}
finally
{
if ($fileStream)
{
$fileStream.Dispose()
}
}
rmdir $tmpZipPath -Recurse -Force
}
catch
{
rmdir $tmpZipPath -Recurse -Force -ErrorAction Continue
Write-Output $_
if ($_.Exception -is [System.Exception])
{
try
{
[System.Exception]$ex = $_.Exception
while ($ex)
{
Write-Output $ex.ToString()
$ex = $ex.InnerException
}
}
catch
{
Write-Output $_
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment