Skip to content

Commit

Permalink
Only remove Azure dependency for source builds
Browse files Browse the repository at this point in the history
  • Loading branch information
nguerrera committed Oct 18, 2017
1 parent b63fb1a commit 5a1021a
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 16 deletions.
2 changes: 2 additions & 0 deletions build/BuildDefaults.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
<NoWarn>NU1701</NoWarn>

<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

<DefineConstants Condition="'$(DotNetBuildFromSource)' == 'true'">$(DefineConstants);SOURCE_BUILD</DefineConstants>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if !SOURCE_BUILD

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
Expand Down Expand Up @@ -84,3 +86,5 @@ public override bool Execute()
}
}
}

#endif
2 changes: 2 additions & 0 deletions build_projects/dotnet-cli-build/CopyBlobsToLatest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if !SOURCE_BUILD
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.IO;
Expand Down Expand Up @@ -112,3 +113,4 @@ private void CopyBlobs(string destinationFolder)
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if !SOURCE_BUILD

using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -65,3 +67,5 @@ public override bool Execute()
}
}
}

#endif
168 changes: 152 additions & 16 deletions build_projects/shared-build-targets-utils/Publishing/AzurePublisher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if !SOURCE_BUILD
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -9,6 +10,9 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;

namespace Microsoft.DotNet.Cli.Build
{
Expand All @@ -22,77 +26,209 @@ public enum Product
Sdk,
}

public AzurePublisher(string containerName)
private const string s_dotnetBlobContainerName = "dotnet";

private string _connectionString { get; set; }
private string _containerName { get; set; }
private CloudBlobContainer _blobContainer { get; set; }

public AzurePublisher(string containerName = s_dotnetBlobContainerName)
{
throw new NotImplementedException();
_connectionString = EnvVars.EnsureVariable("CONNECTION_STRING").Trim('"');
_containerName = containerName;
_blobContainer = GetDotnetBlobContainer(_connectionString, containerName);
}

public AzurePublisher(string accountName, string accountKey, string containerName)
public AzurePublisher(string accountName, string accountKey, string containerName = s_dotnetBlobContainerName)
{
throw new NotImplementedException();
_containerName = containerName;
_blobContainer = GetDotnetBlobContainer(accountName, accountKey, containerName);
}

private CloudBlobContainer GetDotnetBlobContainer(string connectionString, string containerName)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

return GetDotnetBlobContainer(storageAccount, containerName);
}

private CloudBlobContainer GetDotnetBlobContainer(string accountName, string accountKey, string containerName)
{
var storageCredentials = new StorageCredentials(accountName, accountKey);
var storageAccount = new CloudStorageAccount(storageCredentials, true);
return GetDotnetBlobContainer(storageAccount, containerName);
}

private CloudBlobContainer GetDotnetBlobContainer(CloudStorageAccount storageAccount, string containerName)
{
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

return blobClient.GetContainerReference(containerName);
}

public string UploadFile(string file, Product product, string version)
{
throw new NotImplementedException();
string url = CalculateRelativePathForFile(file, product, version);
CloudBlockBlob blob = _blobContainer.GetBlockBlobReference(url);
blob.UploadFromFileAsync(file).Wait();
SetBlobPropertiesBasedOnFileType(blob);
return url;
}

public void PublishStringToBlob(string blob, string content)
{
throw new NotImplementedException();
CloudBlockBlob blockBlob = _blobContainer.GetBlockBlobReference(blob);
blockBlob.UploadTextAsync(content).Wait();

SetBlobPropertiesBasedOnFileType(blockBlob);
}

public void CopyBlob(string sourceBlob, string targetBlob)
{
throw new NotImplementedException();
CloudBlockBlob source = _blobContainer.GetBlockBlobReference(sourceBlob);
CloudBlockBlob target = _blobContainer.GetBlockBlobReference(targetBlob);

// Create the empty blob
using (MemoryStream ms = new MemoryStream())
{
target.UploadFromStreamAsync(ms).Wait();
}

// Copy actual blob data
target.StartCopyAsync(source).Wait();
}

public void SetBlobPropertiesBasedOnFileType(string path)
{
throw new NotImplementedException();
CloudBlockBlob blob = _blobContainer.GetBlockBlobReference(path);
SetBlobPropertiesBasedOnFileType(blob);
}

private void SetBlobPropertiesBasedOnFileType(CloudBlockBlob blockBlob)
{
if (Path.GetExtension(blockBlob.Uri.AbsolutePath.ToLower()) == ".svg")
{
blockBlob.Properties.ContentType = "image/svg+xml";
blockBlob.Properties.CacheControl = "no-cache";
blockBlob.SetPropertiesAsync().Wait();
}
else if (Path.GetExtension(blockBlob.Uri.AbsolutePath.ToLower()) == ".version")
{
blockBlob.Properties.ContentType = "text/plain";
blockBlob.Properties.CacheControl = "no-cache";
blockBlob.SetPropertiesAsync().Wait();
}
}

public IEnumerable<string> ListBlobs(Product product, string version)
{
throw new NotImplementedException();
string virtualDirectory = $"{product}/{version}";
return ListBlobs(virtualDirectory);
}

public IEnumerable<string> ListBlobs(string virtualDirectory)
{
throw new NotImplementedException();
CloudBlobDirectory blobDir = _blobContainer.GetDirectoryReference(virtualDirectory);
BlobContinuationToken continuationToken = new BlobContinuationToken();

var blobFiles = blobDir.ListBlobsSegmentedAsync(continuationToken).Result;
return blobFiles.Results.Select(bf => bf.Uri.PathAndQuery.Replace($"/{_containerName}/", string.Empty));
}

public string AcquireLeaseOnBlob(
string blob,
TimeSpan? maxWaitDefault = null,
TimeSpan? delayDefault = null)
{
throw new NotImplementedException();
TimeSpan maxWait = maxWaitDefault ?? TimeSpan.FromSeconds(120);
TimeSpan delay = delayDefault ?? TimeSpan.FromMilliseconds(500);

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();

// This will throw an exception with HTTP code 409 when we cannot acquire the lease
// But we should block until we can get this lease, with a timeout (maxWaitSeconds)
while (stopWatch.ElapsedMilliseconds < maxWait.TotalMilliseconds)
{
try
{
CloudBlockBlob cloudBlob = _blobContainer.GetBlockBlobReference(blob);
Task<string> task = cloudBlob.AcquireLeaseAsync(TimeSpan.FromMinutes(1), null);
task.Wait();
return task.Result;
}
catch (Exception e)
{
Console.WriteLine($"Retrying lease acquisition on {blob}, {e.Message}");
Thread.Sleep(delay);
}
}

throw new Exception($"Unable to acquire lease on {blob}");
}

public void ReleaseLeaseOnBlob(string blob, string leaseId)
{
throw new NotImplementedException();
CloudBlockBlob cloudBlob = _blobContainer.GetBlockBlobReference(blob);
AccessCondition ac = new AccessCondition() { LeaseId = leaseId };
cloudBlob.ReleaseLeaseAsync(ac).Wait();
}

public bool IsLatestSpecifiedVersion(string version)
{
throw new NotImplementedException();
Task<bool> task = _blobContainer.GetBlockBlobReference(version).ExistsAsync();
task.Wait();
return task.Result;
}

public void DropLatestSpecifiedVersion(string version)
{
throw new NotImplementedException();
CloudBlockBlob blob = _blobContainer.GetBlockBlobReference(version);
using (MemoryStream ms = new MemoryStream())
{
blob.UploadFromStreamAsync(ms).Wait();
}
}

public void CreateBlobIfNotExists(string path)
{
throw new NotImplementedException();
Task<bool> task = _blobContainer.GetBlockBlobReference(path).ExistsAsync();
task.Wait();
if (!task.Result)
{
CloudBlockBlob blob = _blobContainer.GetBlockBlobReference(path);
using (MemoryStream ms = new MemoryStream())
{
blob.UploadFromStreamAsync(ms).Wait();
}
}
}

public bool TryDeleteBlob(string path)
{
throw new NotImplementedException();
try
{
DeleteBlob(path);

return true;
}
catch (Exception e)
{
Console.WriteLine($"Deleting blob {path} failed with \r\n{e.Message}");

return false;
}
}

private void DeleteBlob(string path)
{
_blobContainer.GetBlockBlobReference(path).DeleteAsync().Wait();
}

private static string CalculateRelativePathForFile(string file, Product product, string version)
{
return $"{product}/{version}/{Path.GetFileName(file)}";
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.1.1" />
<PackageReference Include="System.Threading.Thread" Version="4.0.0" />
<PackageReference Include="System.Xml.XmlSerializer" Version="4.0.11" />
<PackageReference Include="WindowsAzure.Storage" Version="7.2.1" Condition="'$(DotNetBuildFromSource)' != 'true'"/>
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="2.0.0" />
</ItemGroup>

Expand Down

0 comments on commit 5a1021a

Please sign in to comment.