|
3 | 3 |
|
4 | 4 | using System;
|
5 | 5 | using System.IO;
|
| 6 | +using System.Threading; |
| 7 | +using Microsoft.Azure.Storage.Blob; |
6 | 8 | using Microsoft.Azure.WebJobs.Script.Configuration;
|
| 9 | +using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics; |
7 | 10 | using Microsoft.Extensions.Configuration;
|
| 11 | +using Microsoft.Extensions.Logging; |
8 | 12 | using Microsoft.Extensions.Options;
|
9 | 13 | using static Microsoft.Azure.WebJobs.Script.EnvironmentSettingNames;
|
10 | 14 |
|
@@ -65,14 +69,99 @@ public void Configure(string name, ScriptApplicationHostOptions options)
|
65 | 69 | options.IsStandbyConfiguration = true;
|
66 | 70 | }
|
67 | 71 |
|
68 |
| - options.IsFileSystemReadOnly = IsZipDeployment(); |
| 72 | + options.IsFileSystemReadOnly = IsZipDeployment(out bool isScmRunFromPackage); |
| 73 | + options.IsScmRunFromPackage = isScmRunFromPackage; |
69 | 74 | }
|
70 | 75 |
|
71 |
| - private bool IsZipDeployment() |
| 76 | + private bool IsZipDeployment(out bool isScmRunFromPackage) |
72 | 77 | {
|
73 |
| - return Utility.IsValidZipSetting(_environment.GetEnvironmentVariable(AzureWebsiteZipDeployment)) || |
| 78 | + // Check app settings for run from package. |
| 79 | + bool runFromPkgConfigured = Utility.IsValidZipSetting(_environment.GetEnvironmentVariable(AzureWebsiteZipDeployment)) || |
74 | 80 | Utility.IsValidZipSetting(_environment.GetEnvironmentVariable(AzureWebsiteAltZipDeployment)) ||
|
75 | 81 | Utility.IsValidZipSetting(_environment.GetEnvironmentVariable(AzureWebsiteRunFromPackage));
|
| 82 | + |
| 83 | + if (!_environment.IsLinuxConsumption()) |
| 84 | + { |
| 85 | + isScmRunFromPackage = false; |
| 86 | + // This check is strong enough for SKUs other than Linux Consumption. |
| 87 | + return runFromPkgConfigured; |
| 88 | + } |
| 89 | + |
| 90 | + // The following logic only applies to Linux Consumption, since currently SCM_RUN_FROM_PACKAGE is always set even if we are not using it. |
| 91 | + if (runFromPkgConfigured) |
| 92 | + { |
| 93 | + isScmRunFromPackage = false; |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + // If SCM_RUN_FROM_PACKAGE is set to a valid value and the blob exists, it's a zip deployment. |
| 98 | + var url = _environment.GetEnvironmentVariable(ScmRunFromPackage); |
| 99 | + if (string.IsNullOrEmpty(url)) |
| 100 | + { |
| 101 | + LinuxContainerEventGenerator.LogEvent(message: $"{nameof(ScmRunFromPackage)} is empty.", source: nameof(ScriptApplicationHostOptionsSetup)); |
| 102 | + isScmRunFromPackage = false; |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + if (Utility.TryCleanUrl(url, out string cleanedUrl)) |
| 107 | + { |
| 108 | + LinuxContainerEventGenerator.LogEvent(message: $"{nameof(ScmRunFromPackage)} = {cleanedUrl}", source: nameof(ScriptApplicationHostOptionsSetup)); |
| 109 | + } |
| 110 | + |
| 111 | + var isValidZipSetting = Utility.IsValidZipSetting(url); |
| 112 | + LinuxContainerEventGenerator.LogEvent(message: $"{nameof(ScmRunFromPackage)} isValidZipSetting = {isValidZipSetting}", source: nameof(ScriptApplicationHostOptionsSetup)); |
| 113 | + |
| 114 | + if (!isValidZipSetting) |
| 115 | + { |
| 116 | + isScmRunFromPackage = false; |
| 117 | + // Return early so we don't call storage if it isn't absolutely necessary. |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + var blobExists = BlobExists(url); |
| 122 | + LinuxContainerEventGenerator.LogEvent(message: $"{nameof(ScmRunFromPackage)} blobExists = {blobExists}", source: nameof(ScriptApplicationHostOptionsSetup)); |
| 123 | + |
| 124 | + bool scmRunFromPkgConfigured = isValidZipSetting && blobExists; |
| 125 | + isScmRunFromPackage = scmRunFromPkgConfigured; |
| 126 | + return scmRunFromPkgConfigured; |
| 127 | + } |
| 128 | + |
| 129 | + public virtual bool BlobExists(string url) |
| 130 | + { |
| 131 | + if (string.IsNullOrEmpty(url)) |
| 132 | + { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + try |
| 137 | + { |
| 138 | + CloudBlockBlob blob = new CloudBlockBlob(new Uri(url)); |
| 139 | + |
| 140 | + int attempt = 0; |
| 141 | + while (true) |
| 142 | + { |
| 143 | + try |
| 144 | + { |
| 145 | + return blob.Exists(); |
| 146 | + } |
| 147 | + catch (Exception ex) when (!ex.IsFatal()) |
| 148 | + { |
| 149 | + LinuxContainerEventGenerator.LogEvent(message: $"Exception when checking if {nameof(ScmRunFromPackage)} blob exists", e: ex, |
| 150 | + logLevel: LogLevel.Error, source: nameof(ScriptApplicationHostOptionsSetup)); |
| 151 | + if (++attempt > 2) |
| 152 | + { |
| 153 | + return false; |
| 154 | + } |
| 155 | + Thread.Sleep(TimeSpan.FromSeconds(0.3)); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + catch (Exception ex) |
| 160 | + { |
| 161 | + LinuxContainerEventGenerator.LogEvent(message: $"Failed to check status of {nameof(ScmRunFromPackage)}", e: ex, |
| 162 | + logLevel: LogLevel.Error, source: nameof(ScriptApplicationHostOptionsSetup)); |
| 163 | + return false; |
| 164 | + } |
76 | 165 | }
|
77 | 166 |
|
78 | 167 | public void Dispose()
|
|
0 commit comments