Skip to content

Commit 1455cc2

Browse files
committed
Enable mesh init server
1 parent ebec14e commit 1455cc2

File tree

4 files changed

+37
-48
lines changed

4 files changed

+37
-48
lines changed

src/WebJobs.Script.WebHost/Management/InstanceManager.cs

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -270,24 +270,15 @@ private async Task ApplyContext(HostAssignmentContext assignmentContext)
270270
}
271271
else if (!string.IsNullOrEmpty(assignmentContext.AzureFilesConnectionString))
272272
{
273-
ApplyAzureFilesContext(assignmentContext.AzureFilesConnectionString, assignmentContext.AzureFilesContentShare, "/home");
273+
await MountCifs(assignmentContext.AzureFilesConnectionString, assignmentContext.AzureFilesContentShare, "/home");
274274
}
275275
}
276276

277-
private void ApplyAzureFilesContext(string connectionString, string contentShare, string targetPath)
278-
{
279-
var sa = CloudStorageAccount.Parse(connectionString);
280-
var key = Convert.ToBase64String(sa.Credentials.ExportKey());
281-
282-
var mountCommand = $"mount -t cifs //{sa.FileEndpoint.Host}/{contentShare} {targetPath} -o vers=3.0,username={sa.Credentials.AccountName},password={key},dir_mode=0777,file_mode=0777,serverino";
283-
RunBashCommand($"(mkdir -p {targetPath} || true) && ({mountCommand}) && (mkdir -p /home/site/wwwroot || true)", MetricEventNames.LinuxContainerSpecializationAzureFilesMount);
284-
}
285-
286277
private async Task ApplyBlobPackageContext(RunFromPackageContext pkgContext, string targetPath)
287278
{
288279
// download zip and extract
289280
var filePath = await Download(pkgContext);
290-
UnpackPackage(filePath, targetPath, pkgContext);
281+
await UnpackPackage(filePath, targetPath, pkgContext);
291282

292283
string bundlePath = Path.Combine(targetPath, "worker-bundle");
293284
if (Directory.Exists(bundlePath))
@@ -368,7 +359,7 @@ private void AriaDownload(string directory, string fileName, Uri zipUri)
368359
_logger.LogInformation($"{fileInfo.Length} bytes downloaded");
369360
}
370361

371-
private void UnpackPackage(string filePath, string scriptPath, RunFromPackageContext pkgContext)
362+
private async Task UnpackPackage(string filePath, string scriptPath, RunFromPackageContext pkgContext)
372363
{
373364
CodePackageType packageType;
374365
using (_metricsLogger.LatencyEvent(MetricEventNames.LinuxContainerSpecializationGetPackageType))
@@ -385,15 +376,15 @@ private void UnpackPackage(string filePath, string scriptPath, RunFromPackageCon
385376
}
386377
else
387378
{
388-
MountFsImage(filePath, scriptPath);
379+
await MountFuse("squashfs", filePath, scriptPath);
389380
}
390381
}
391382
else if (packageType == CodePackageType.Zip)
392383
{
393384
// default to unzip for zip packages
394385
if (_environment.IsMountEnabled())
395386
{
396-
MountZipFile(filePath, scriptPath);
387+
await MountFuse("zip", filePath, scriptPath);
397388
}
398389
else
399390
{
@@ -451,40 +442,35 @@ private void UnzipPackage(string filePath, string scriptPath)
451442
}
452443

453444
private void UnsquashImage(string filePath, string scriptPath)
454-
=> RunBashCommand($"(mkdir -p '{scriptPath}' || true) && unsquashfs -f -d '{scriptPath}' '{filePath}'", MetricEventNames.LinuxContainerSpecializationUnsquash);
445+
=> RunBashCommand($"unsquashfs -f -d '{scriptPath}' '{filePath}'", MetricEventNames.LinuxContainerSpecializationUnsquash);
455446

456-
private void MountFsImage(string filePath, string scriptPath)
457-
=> RunFuseMount($"squashfuse_ll -o nonempty '{filePath}' '{scriptPath}'", scriptPath);
447+
private async Task MountFuse(string type, string filePath, string scriptPath)
448+
=> await Mount(new[]
449+
{
450+
new KeyValuePair<string, string>("operation", type),
451+
new KeyValuePair<string, string>("filePath", filePath),
452+
new KeyValuePair<string, string>("targetPath", scriptPath),
453+
});
458454

459-
private void MountZipFile(string filePath, string scriptPath)
460-
=> RunFuseMount($"fuse-zip -o nonempty -r '{filePath}' '{scriptPath}'", scriptPath);
455+
private async Task MountCifs(string connectionString, string contentShare, string targetPath)
456+
{
457+
var sa = CloudStorageAccount.Parse(connectionString);
458+
var key = Convert.ToBase64String(sa.Credentials.ExportKey());
459+
await Mount(new[]
460+
{
461+
new KeyValuePair<string, string>("operation", "cifs"),
462+
new KeyValuePair<string, string>("host", sa.FileEndpoint.Host),
463+
new KeyValuePair<string, string>("accountName", sa.Credentials.AccountName),
464+
new KeyValuePair<string, string>("accountKey", key),
465+
new KeyValuePair<string, string>("contentShare", contentShare),
466+
new KeyValuePair<string, string>("targetPath", targetPath),
467+
});
468+
}
461469

462-
private void RunFuseMount(string mountCommand, string targetPath)
470+
private async Task Mount(IEnumerable<KeyValuePair<string, string>> formData)
463471
{
464-
using (_metricsLogger.LatencyEvent(MetricEventNames.LinuxContainerSpecializationFuseMount))
465-
{
466-
var bashCommand = $"(mknod /dev/fuse c 10 229 || true) && (mkdir -p '{targetPath}' || true) && ({mountCommand})";
467-
var process = new Process
468-
{
469-
StartInfo = new ProcessStartInfo
470-
{
471-
FileName = "bash",
472-
Arguments = $"-c \"{bashCommand}\"",
473-
RedirectStandardOutput = true,
474-
RedirectStandardError = true,
475-
UseShellExecute = false,
476-
CreateNoWindow = true
477-
}
478-
};
479-
_logger.LogInformation($"Running: {process.StartInfo.FileName} {process.StartInfo.Arguments}");
480-
process.Start();
481-
var output = process.StandardOutput.ReadToEnd();
482-
var error = process.StandardError.ReadToEnd();
483-
process.WaitForExit();
484-
_logger.LogInformation($"Output: {output}");
485-
_logger.LogInformation($"error: {output}");
486-
_logger.LogInformation($"exitCode: {process.ExitCode}");
487-
}
472+
var res = await _client.PostAsync(_environment.GetEnvironmentVariable(EnvironmentSettingNames.MeshInitURI), new FormUrlEncodedContent(formData));
473+
_logger.LogInformation("Response {res} from init", res);
488474
}
489475

490476
private (string, string, int) RunBashCommand(string command, string metricName)

src/WebJobs.Script/Environment/EnvironmentExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,11 @@ public static string GetKubernetesApiServerUrl(this IEnvironment environment)
208208
}
209209

210210
public static bool IsMountEnabled(this IEnvironment environment)
211-
=> string.Equals(environment.GetEnvironmentVariable(MountEnabled), "1");
211+
=> string.Equals(environment.GetEnvironmentVariable(MountEnabled), "1") &&
212+
!string.IsNullOrEmpty(environment.GetEnvironmentVariable(MeshInitURI));
212213

213214
public static bool IsMountDisabled(this IEnvironment environment)
214-
=> string.Equals(environment.GetEnvironmentVariable(MountEnabled), "0");
215+
=> string.Equals(environment.GetEnvironmentVariable(MountEnabled), "0") ||
216+
string.IsNullOrEmpty(environment.GetEnvironmentVariable(MeshInitURI));
215217
}
216218
}

src/WebJobs.Script/Environment/EnvironmentSettingNames.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,6 @@ public static class EnvironmentSettingNames
7777
public const string FunctionsLogPath = "FUNCTIONS_LOG_PATH";
7878
public const string FunctionsSecretsPath = "FUNCTIONS_SECRETS_PATH";
7979
public const string FunctionsTestDataPath = "FUNCTIONS_TEST_DATA_PATH";
80+
public const string MeshInitURI = "MESH_INIT_URI";
8081
}
8182
}

test/WebJobs.Script.Tests.Integration/Management/InstanceManagerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ public async void StartAssignment_Succeeds_With_NonEmpty_ScmRunFromPackage_Blob(
172172
p => Assert.StartsWith("Downloading zip contents from", p),
173173
p => Assert.EndsWith(" bytes downloaded", p),
174174
p => Assert.EndsWith(" bytes written", p),
175-
p => Assert.StartsWith("Running: bash ", p),
175+
p => Assert.StartsWith("Running: ", p),
176176
p => Assert.StartsWith("Output:", p),
177-
p => Assert.StartsWith("error:", p),
177+
p => Assert.StartsWith("bash:", p),
178178
p => Assert.StartsWith("exitCode:", p),
179179
p => Assert.StartsWith("Triggering specialization", p));
180180
}

0 commit comments

Comments
 (0)