/// <summary> /// Checks if memcache is already enabled or not for the given role startup. /// It does this by checking the role startup task. /// </summary> /// <param name="startup">The role startup</param> /// <returns>Either enabled or not</returns> private bool IsCacheEnabled(Startup startup) { if (startup.Task != null) { return(Array.Exists <Variable>(CloudRuntime.GetRuntimeStartupTask(startup).Environment, v => v.name.Equals(Resources.RuntimeTypeKey) && v.value.Contains(Resources.CacheRuntimeValue))); } return(false); }
/// <summary> /// Main entry for enabling memcache. /// </summary> /// <param name="roleName">The web role name</param> /// <param name="cacheWorkerRoleName">The cache worker role name</param> /// <param name="rootPath">The service root path</param> /// <param name="message">The resulted message</param> /// <param name="azureService">The azure service instance</param> /// <param name="webRole">The web role to enable caching one</param> private void EnableMemcache(string roleName, string cacheWorkerRoleName, ref string message, ref AzureService azureService) { // Add MemcacheShim runtime installation. azureService.AddRoleRuntime(azureService.Paths, roleName, Resources.CacheRuntimeValue, CacheRuntimeVersion); // Fetch web role information. Startup startup = azureService.Components.GetRoleStartup(roleName); // Assert that cache runtime is added to the runtime startup. Debug.Assert(Array.Exists <Variable>(CloudRuntime.GetRuntimeStartupTask(startup).Environment, v => v.name.Equals(Resources.RuntimeTypeKey) && v.value.Contains(Resources.CacheRuntimeValue))); if (azureService.Components.IsWebRole(roleName)) { WebRole webRole = azureService.Components.GetWebRole(roleName); webRole.LocalResources = General.InitializeIfNull <LocalResources>(webRole.LocalResources); DefinitionConfigurationSetting[] configurationSettings = webRole.ConfigurationSettings; CachingConfigurationFactoryMethod( azureService, roleName, true, cacheWorkerRoleName, webRole.Startup, webRole.Endpoints, webRole.LocalResources, ref configurationSettings, CacheRuntimeVersion); webRole.ConfigurationSettings = configurationSettings; } else { WorkerRole workerRole = azureService.Components.GetWorkerRole(roleName); workerRole.LocalResources = General.InitializeIfNull <LocalResources>(workerRole.LocalResources); DefinitionConfigurationSetting[] configurationSettings = workerRole.ConfigurationSettings; CachingConfigurationFactoryMethod( azureService, roleName, false, cacheWorkerRoleName, workerRole.Startup, workerRole.Endpoints, workerRole.LocalResources, ref configurationSettings, CacheRuntimeVersion); workerRole.ConfigurationSettings = configurationSettings; } // Save changes azureService.Components.Save(azureService.Paths); message = string.Format(Resources.EnableMemcacheMessage, roleName, cacheWorkerRoleName, Resources.MemcacheEndpointPort); }
/// <summary> /// Get the startup task environment settings for the given role /// </summary> /// <param name="definition">The definition containign the role</param> /// <param name="roleName">The name of the role</param> /// <returns>The environment settings for the role, or null if the role is not found</returns> private static Variable[] GetRoleRuntimeEnvironment(ServiceDefinition definition, string roleName) { WebRole webRole; if (TryGetWebRole(definition, roleName, out webRole)) { return(CloudRuntime.GetRuntimeStartupTask(webRole.Startup).Environment); } WorkerRole workerRole; if (TryGetWorkerRole(definition, roleName, out workerRole)) { return(CloudRuntime.GetRuntimeStartupTask(workerRole.Startup).Environment); } return(null); }
/// <summary> /// Apply the specified Variable values to the specified role's startup task environment /// </summary> /// <param name="definition">The service definition containing the role</param> /// <param name="roleName">The name of the role to change</param> /// <param name="environment">The Variables containing the changes</param> /// <returns>true if the variables environment is successfully changed</returns> private static bool ApplyRuntimeChanges(ServiceDefinition definition, string roleName, Variable[] environment) { WebRole webRole; if (TryGetWebRole(definition, roleName, out webRole)) { CloudRuntime.GetRuntimeStartupTask(webRole.Startup).Environment = environment; return(true); } WorkerRole workerRole; if (TryGetWorkerRole(definition, roleName, out workerRole)) { CloudRuntime.GetRuntimeStartupTask(workerRole.Startup).Environment = environment; return(true); } return(false); }