-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathAzureAppServiceContainerDeployBehaviour.cs
104 lines (85 loc) · 5.32 KB
/
AzureAppServiceContainerDeployBehaviour.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Threading.Tasks;
using Azure.ResourceManager;
using Azure.ResourceManager.AppService;
using Calamari.Azure;
using Calamari.Azure.AppServices;
using Calamari.AzureAppService.Azure;
using Calamari.CloudAccounts;
using Calamari.Common.Commands;
using Calamari.Common.Plumbing.Logging;
using Calamari.Common.Plumbing.Pipeline;
using Octopus.CoreUtilities.Extensions;
using AccountVariables = Calamari.AzureAppService.Azure.AccountVariables;
namespace Calamari.AzureAppService.Behaviors
{
class AzureAppServiceContainerDeployBehaviour : IDeployBehaviour
{
private ILog Log { get; }
public AzureAppServiceContainerDeployBehaviour(ILog log)
{
Log = log;
}
public bool IsEnabled(RunningDeployment context) => true;
public async Task Execute(RunningDeployment context)
{
var variables = context.Variables;
var hasAccessToken = !variables.Get(AccountVariables.Jwt).IsNullOrEmpty();
var account = hasAccessToken ? (IAzureAccount)new AzureOidcAccount(variables) : new AzureServicePrincipalAccount(variables);
var webAppName = variables.Get(SpecialVariables.Action.Azure.WebAppName);
var slotName = variables.Get(SpecialVariables.Action.Azure.WebAppSlot);
var resourceGroupName = variables.Get(SpecialVariables.Action.Azure.ResourceGroupName);
var targetSite = new AzureTargetSite(account.SubscriptionNumber, resourceGroupName, webAppName, slotName);
var image = variables.Get(SpecialVariables.Action.Package.Image);
var registryHost = variables.Get(SpecialVariables.Action.Package.Registry);
var regUsername = variables.Get(SpecialVariables.Action.Package.Feed.Username);
var regPwd = variables.Get(SpecialVariables.Action.Package.Feed.Password);
var armClient = account.CreateArmClient();
Log.Info($"Updating web app to use image {image} from registry {registryHost}");
Log.Verbose("Retrieving app service to determine operating system");
var isLinuxWebApp = await IsLinuxWebApp(armClient, targetSite);
Log.Verbose("Retrieving config (this is required to update image)");
var config = await armClient.GetSiteConfigDataAsync(targetSite);
var newVersion = $"DOCKER|{image}";
if (isLinuxWebApp)
{
Log.Verbose($"Updating LinuxFxVersion to {newVersion}");
config.LinuxFxVersion = newVersion;
}
else
{
Log.Verbose($"Updating WindowsFxVersion to {newVersion}");
config.WindowsFxVersion = newVersion;
}
Log.Verbose("Retrieving app settings");
var appSettings = await armClient.GetAppSettingsAsync(targetSite);
appSettings.Properties["DOCKER_REGISTRY_SERVER_URL"] = "https://" + registryHost;
appSettings.Properties["DOCKER_REGISTRY_SERVER_USERNAME"] = regUsername;
appSettings.Properties["DOCKER_REGISTRY_SERVER_PASSWORD"] = regPwd;
Log.Info("Updating app settings with container registry");
await armClient.UpdateAppSettingsAsync(targetSite, appSettings);
Log.Info("Updating configuration with container image");
await armClient.UpdateSiteConfigDataAsync(targetSite, config);
}
static async Task<bool> IsLinuxWebApp(ArmClient armClient, AzureTargetSite targetSite)
{
var webSiteData = targetSite.HasSlot switch
{
true => (await armClient.GetWebSiteSlotResource(WebSiteSlotResource.CreateResourceIdentifier(
targetSite.SubscriptionId,
targetSite.ResourceGroupName,
targetSite.Site,
targetSite.Slot))
.GetAsync()).Value.Data,
false => (await armClient.GetWebSiteResource(WebSiteResource.CreateResourceIdentifier(
targetSite.SubscriptionId,
targetSite.ResourceGroupName,
targetSite.Site))
.GetAsync()).Value.Data
};
//If the app service is a linux, it will contain linux in the kind string
//possible values are found here: https://github.com/Azure/app-service-linux-docs/blob/master/Things_You_Should_Know/kind_property.md
return webSiteData.Kind.ToLowerInvariant().Contains("linux");
}
}
}