From 5c7c54a17c336d3d8af02991ca4178d39e073aff Mon Sep 17 00:00:00 2001 From: Mathew Charles Date: Thu, 15 Feb 2018 15:22:59 -0800 Subject: [PATCH] Run-From-Zip readonly file mode checks (#2230) --- .../App_Start/WebHostResolver.cs | 26 ++++++++++++------- .../Config/ScriptSettingsManager.cs | 2 ++ src/WebJobs.Script/Host/ScriptHost.cs | 4 +-- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/WebJobs.Script.WebHost/App_Start/WebHostResolver.cs b/src/WebJobs.Script.WebHost/App_Start/WebHostResolver.cs index f72a37dadf..809f175adb 100644 --- a/src/WebJobs.Script.WebHost/App_Start/WebHostResolver.cs +++ b/src/WebJobs.Script.WebHost/App_Start/WebHostResolver.cs @@ -130,7 +130,7 @@ internal void EnsureInitialized(WebHostSettings settings) _activeScriptHostConfig = CreateScriptHostConfiguration(settings); _activeHostManager = new WebScriptHostManager(_activeScriptHostConfig, _secretManagerFactory, _eventManager, _settingsManager, settings); _activeReceiverManager = new WebHookReceiverManager(_activeHostManager.SecretManager); - InitializeFileSystem(); + InitializeFileSystem(_settingsManager.FileSystemIsReadOnly); if (_standbyHostManager != null) { @@ -165,7 +165,7 @@ internal void EnsureInitialized(WebHostSettings settings) _standbyHostManager = new WebScriptHostManager(_standbyScriptHostConfig, _secretManagerFactory, _eventManager, _settingsManager, standbySettings); _standbyReceiverManager = new WebHookReceiverManager(_standbyHostManager.SecretManager); - InitializeFileSystem(); + InitializeFileSystem(_settingsManager.FileSystemIsReadOnly); StandbyManager.Initialize(_standbyScriptHostConfig); // start a background timer to identify when specialization happens @@ -285,7 +285,7 @@ private void OnSpecializationTimerTick(object state) _activeHostManager?.EnsureInitialized(); } - private static void InitializeFileSystem() + private static void InitializeFileSystem(bool readOnlyFileSystem) { if (ScriptSettingsManager.Instance.IsAzureEnvironment) { @@ -294,17 +294,23 @@ private static void InitializeFileSystem() string home = ScriptSettingsManager.Instance.GetSetting(EnvironmentSettingNames.AzureWebsiteHomePath); if (!string.IsNullOrEmpty(home)) { - // Delete hostingstart.html if any. Azure creates that in all sites by default - string siteRootPath = Path.Combine(home, @"site\wwwroot"); - string hostingStart = Path.Combine(siteRootPath, "hostingstart.html"); - if (File.Exists(hostingStart)) + if (!readOnlyFileSystem) { - File.Delete(hostingStart); + // Delete hostingstart.html if any. Azure creates that in all sites by default + string siteRootPath = Path.Combine(home, @"site\wwwroot"); + string hostingStart = Path.Combine(siteRootPath, "hostingstart.html"); + if (File.Exists(hostingStart)) + { + File.Delete(hostingStart); + } } - // Create the tools folder if it doesn't exist string toolsPath = Path.Combine(home, @"site\tools"); - Directory.CreateDirectory(toolsPath); + if (!readOnlyFileSystem) + { + // Create the tools folder if it doesn't exist + Directory.CreateDirectory(toolsPath); + } var folders = new List(); folders.Add(Path.Combine(home, @"site\tools")); diff --git a/src/WebJobs.Script/Config/ScriptSettingsManager.cs b/src/WebJobs.Script/Config/ScriptSettingsManager.cs index 80635ec3b7..42ca0132cc 100644 --- a/src/WebJobs.Script/Config/ScriptSettingsManager.cs +++ b/src/WebJobs.Script/Config/ScriptSettingsManager.cs @@ -32,6 +32,8 @@ public static ScriptSettingsManager Instance public bool IsDynamicSku => WebsiteSku == ScriptConstants.DynamicSku; + public bool FileSystemIsReadOnly => IsZipDeployment; + public virtual string AzureWebsiteDefaultSubdomain { get diff --git a/src/WebJobs.Script/Host/ScriptHost.cs b/src/WebJobs.Script/Host/ScriptHost.cs index 11a9e6cadc..62539693e9 100644 --- a/src/WebJobs.Script/Host/ScriptHost.cs +++ b/src/WebJobs.Script/Host/ScriptHost.cs @@ -288,9 +288,9 @@ public void Initialize() // read host.json and apply to JobHostConfiguration string hostConfigFilePath = Path.Combine(ScriptConfig.RootScriptPath, ScriptConstants.HostMetadataFileName); - // If it doesn't exist, create an empty JSON file - if (!File.Exists(hostConfigFilePath)) + if (!_settingsManager.FileSystemIsReadOnly && !File.Exists(hostConfigFilePath)) { + // If it doesn't exist, create an empty JSON file File.WriteAllText(hostConfigFilePath, "{}"); }