Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.IO;
using System.Threading.Tasks;
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using SharpCompress.Common;

namespace Calamari.AzureAppService;

public class FlexConsumptionZipPackageProvider : IPackageProvider
{
public string UploadUrlPath => "/api/publish";
public bool SupportsAsynchronousDeployment => true;

public async Task<FileInfo> PackageArchive(string sourceDirectory, string targetDirectory)
{
await Task.Run(() =>
{
using var archive = ZipArchive.Create();
archive.AddAllFromDirectory(
$"{sourceDirectory}");
archive.SaveTo($"{targetDirectory}/app.zip", CompressionType.Deflate);
});
return new FileInfo($"{targetDirectory}/app.zip");
}

public async Task<FileInfo> ConvertToAzureSupportedFile(FileInfo sourceFile) => await Task.Run(() => sourceFile);
public string ContentType => "application/zip";
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public interface IPackageProvider
Task<FileInfo> PackageArchive(string sourceDirectory, string targetDirectory);

Task<FileInfo> ConvertToAzureSupportedFile(FileInfo sourceFile);

string ContentType { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Calamari.Common.Features.Packages;
using Calamari.Common.Features.Packages.Java;
using Calamari.Common.Features.Processes;
using Calamari.Common.Plumbing.Extensions;
using Calamari.Common.Plumbing.FileSystem;
using Calamari.Common.Plumbing.Logging;
using Calamari.Common.Plumbing.Variables;
Expand Down Expand Up @@ -56,6 +55,6 @@ await Task.Run(() =>
}

public async Task<FileInfo> ConvertToAzureSupportedFile(FileInfo sourceFile) => await Task.Run(() => sourceFile);

public string ContentType => "application/octet-stream";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ public async Task<FileInfo> ConvertToAzureSupportedFile(FileInfo sourceFile)
await Task.Run(() => File.Copy(sourceFile.FullName, newFilePath));
return new FileInfo(newFilePath);
}

public string ContentType => "application/octet-stream";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Calamari.AzureAppService
{
public class ZipPackageProvider : IPackageProvider
{
public string UploadUrlPath => @"/api/zipdeploy";
public string UploadUrlPath => "/api/zipdeploy";
public bool SupportsAsynchronousDeployment => true;

public async Task<FileInfo> PackageArchive(string sourceDirectory, string targetDirectory)
Expand All @@ -24,5 +24,6 @@ await Task.Run(() =>
}

public async Task<FileInfo> ConvertToAzureSupportedFile(FileInfo sourceFile) => await Task.Run(() => sourceFile);
public string ContentType => "application/octet-stream";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
using Calamari.AzureAppService.Azure;
using Calamari.CloudAccounts;
using Calamari.Common.Commands;
using Calamari.Common.FeatureToggles;
using Calamari.Common.Plumbing.Extensions;
using Calamari.Common.Plumbing.FileSystem;
using Calamari.Common.Plumbing.Logging;
using Calamari.Common.Plumbing.Pipeline;
using Calamari.Common.Plumbing.Variables;
using Octopus.CoreUtilities.Extensions;
using Polly;
using Polly.Timeout;
using AccountVariables = Calamari.AzureAppService.Azure.AccountVariables;

namespace Calamari.AzureAppService.Behaviors
{
Expand Down Expand Up @@ -92,12 +89,19 @@ public async Task Execute(RunningDeployment context)

var webSiteResource = armClient.GetWebSiteResource(targetSite.CreateWebSiteResourceIdentifier());
Log.Verbose($"App Service '{targetSite.Site}' found, with Azure Resource Manager Id '{webSiteResource.Id.ToString()}'.");
var siteInfo = webSiteResource.Get();
WebSiteResource? webSiteInforResource = null;
if (siteInfo != null && siteInfo.HasValue && siteInfo.Value.HasData)
{
Log.Verbose($"App Service Plan running on '{siteInfo.Value.Data.Sku}' tier");
webSiteInforResource = siteInfo.Value;
}

var packageFileInfo = new FileInfo(variables.Get(TentacleVariables.CurrentDeployment.PackageFilePath)!);

IPackageProvider packageProvider = packageFileInfo.Extension switch
{
".zip" => new ZipPackageProvider(),
".zip" => GetZipPackageProvider(webSiteInforResource),
".nupkg" => new NugetPackageProvider(),
".war" => new JavaPackageProvider(Log, fileSystem, variables, context, "/api/wardeploy"),
".jar" => new JavaPackageProvider(Log, fileSystem, variables, context, "/api/publish?type=jar"),
Expand Down Expand Up @@ -150,7 +154,7 @@ public async Task Execute(RunningDeployment context)
Log.Verbose($"Retrieving publishing profile for App Service to determine correct deployment endpoint.");
using var publishingProfileXmlStream = await armClient.GetPublishingProfileXmlWithSecrets(targetSite);
var publishingProfile = await PublishingProfile.ParseXml(publishingProfileXmlStream);

Log.Verbose($"Using deployment endpoint '{publishingProfile.PublishUrl}' from publishing profile.");

Log.Info($"Uploading package to {targetSite.SiteAndSlot}");
Expand All @@ -176,6 +180,16 @@ public async Task Execute(RunningDeployment context)
}
}

static IPackageProvider GetZipPackageProvider(WebSiteResource? webSiteResource)
{
if (webSiteResource != null && webSiteResource.Data.Sku.Equals("FlexConsumption", StringComparison.OrdinalIgnoreCase))
{
return new FlexConsumptionZipPackageProvider();
}

return new ZipPackageProvider();
}

private async Task<WebSiteSlotResource> FindOrCreateSlot(ArmClient armClient, WebSiteResource webSiteResource, AzureTargetSite site)
{
Log.Verbose($"Checking if deployment slot '{site.Slot}' exists.");
Expand Down Expand Up @@ -233,7 +247,7 @@ private async Task UploadZipAsync(IAzureAccount azureAccount,
{
await using var fileStream = new FileStream(uploadZipPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var streamContent = new StreamContent(fileStream);
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
streamContent.Headers.ContentType = new MediaTypeHeaderValue(packageProvider.ContentType);

//we have to create a new request message each time
var request = new HttpRequestMessage(HttpMethod.Post, zipUploadUrl)
Expand Down Expand Up @@ -287,7 +301,7 @@ private async Task UploadZipAndPollAsync(IAzureAccount azureAccount,
if (!new FileInfo(uploadZipPath).Exists)
throw new FileNotFoundException(uploadZipPath);

var zipUploadUrl = $"{publishingProfile.PublishUrl}{packageProvider.UploadUrlPath}?isAsync=true";
var zipUploadUrl = $"{publishingProfile.PublishUrl}{packageProvider.UploadUrlPath}?remoteBuild=false&deployer=Octopus";
Log.Verbose($"Publishing {uploadZipPath} to {zipUploadUrl} and checking for deployment");

using var httpClient = new HttpClient(new HttpClientHandler
Expand All @@ -311,7 +325,7 @@ private async Task UploadZipAndPollAsync(IAzureAccount azureAccount,
//we have to create a new request message each time
await using var fileStream = new FileStream(uploadZipPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var streamContent = new StreamContent(fileStream);
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
streamContent.Headers.ContentType = new MediaTypeHeaderValue(packageProvider.ContentType);

var uploadRequest = new HttpRequestMessage(HttpMethod.Post, zipUploadUrl)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.ResourceManager.AppService" Version="1.0.2" />
<PackageReference Include="Azure.ResourceManager.ResourceGraph" Version="1.0.1" />
<PackageReference Include="Azure.ResourceManager.AppService" Version="1.4.1" />
<PackageReference Include="Azure.ResourceManager.ResourceGraph" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Polly" Version="8.3.1" />
Expand Down