internal override void AddRoleToDefinition(ServiceDefinition def, object template) { WebRole webRole = template as WebRole; var toAdd = new WebRole[] { webRole }; if (def.WebRole != null) { def.WebRole = def.WebRole.Concat(toAdd).ToArray(); } else { def.WebRole = toAdd; } }
/// <summary> /// Set the runtime properties for a role /// </summary> /// <param name="definition">The service containing the role</param> /// <param name="roleName">The name of the role to change</param> /// <param name="path">The path to the service</param> /// <param name="version">The version of the runtime to be installed</param> /// <param name="overrideUrl">The value of the override url, if the user wants to opt out of the system</param> /// <returns>true if the settings were successfully changed</returns> public static bool SetRoleRuntime(ServiceDefinition definition, string roleName, ServicePathInfo path, string version = null, string overrideUrl = null) { bool changed = false; Variable[] environment = GetRoleRuntimeEnvironment(definition, roleName); if (version != null) { string filePath = Path.Combine(path.RootPath, roleName, "package.json"); File.WriteAllText(filePath, string.Format(TestResources.ValidPackageJson, "testapp", version)); changed = true; } if (overrideUrl != null) { environment = SetRuntimeEnvironment(environment, Resources.RuntimeOverrideKey, overrideUrl); changed = true; } return changed && ApplyRuntimeChanges(definition, roleName, environment); }
public void CreatePackage(ServiceDefinition definition, string rootPath, DevEnv type, out string standardOutput, out string standardError) { if (definition == null) { throw new ArgumentNullException( "definition", string.Format(Resources.InvalidOrEmptyArgumentMessage, "Service definition")); } if (string.IsNullOrEmpty(rootPath) || File.Exists(rootPath)) { throw new ArgumentException(Resources.InvalidRootNameMessage, "rootPath"); } // Track the directories that are created by GetOrCreateCleanPath // to avoid publishing iisnode log files so we can delete the temp // copies when we're finished packaging Dictionary<string, string> tempDirectories = new Dictionary<string, string>(); try { string roles = // Get the names of all web and worker roles Enumerable.Concat( definition.WebRole.NonNull().Select(role => role.name), definition.WorkerRole.NonNull().Select(role => role.name)) // Get the name and safe path for each role (i.e., if the // role has files that shouldn't be packaged, it'll be // copied to a temp location without those files) .Select(name => GetOrCreateCleanPath(rootPath, name, tempDirectories, type)) // Format the role name and path as a role argument .Select(nameAndPath => string.Format(Resources.RoleArgTemplate, nameAndPath.Key, nameAndPath.Value)) // Join all the role arguments together into one .DefaultIfEmpty(string.Empty) .Aggregate(string.Concat); string sites = // Get all of the web roles definition.WebRole.NonNull() // Get all the sites in each role and format them all as // site arguments .SelectMany(role => // Format each site as a site argument role.Sites.Site.Select(site => string.Format( Resources.SitesArgTemplate, role.name, site.name, tempDirectories.GetValueOrDefault(role.name, rootPath)))) // Join all the site arguments together into one .DefaultIfEmpty(string.Empty) .Aggregate(string.Concat); string args = string.Format( type == DevEnv.Local ? Resources.CsPackLocalArg : Resources.CsPackCloudArg, rootPath, roles, sites); // Run CsPack to generate the package ProcessHelper.StartAndWaitForProcess( new ProcessStartInfo( Path.Combine(AzureSdkBinDirectory, Resources.CsPackExe), args), out standardOutput, out standardError); } finally { // Cleanup any temp directories tempDirectories.Values.ForEach(dir => Directory.Delete(dir, true)); } }
internal virtual void AddRoleToDefinition(ServiceDefinition serviceDefinition, object template) { Validate.ValidateNullArgument(template, string.Format(Resources.NullRoleSettingsMessage, "service definition")); Validate.ValidateNullArgument(serviceDefinition, Resources.NullServiceDefinitionMessage); }
/// <summary> /// Validates that given service definition is valid against list of web/worker roles. Validation steps: /// 1. Make sure that name element /// </summary> /// <param name="actual">Service definition to be checked</param> /// <param name="serviceName">New created service name</param> public static void IsValidServiceDefinition(ServiceDefinition actual, string serviceName) { Assert.AreEqual<string>(serviceName, actual.name); Assert.IsNull(actual.WebRole); Assert.IsNull(actual.WorkerRole); }
/// <summary> /// Validates that given service definition is valid for a service. Validation steps: /// 1. Validates name element matches serviceName /// 2. Validates web role element has all webRoles with same configuration. /// 3. Validates worker role element has all workerRoles with same configuration. /// </summary> /// <param name="actual">Service definition to be checked</param> /// <param name="serviceName">New created service name</param> public static void IsValidServiceDefinition(ServiceDefinition actual, string serviceName, WebRoleInfo[] webRoles = null, WorkerRoleInfo[] workerRoles = null) { Assert.AreEqual<string>(serviceName, actual.name); if (webRoles != null) { Assert.AreEqual<int>(webRoles.Length, actual.WebRole.Length); int length = webRoles.Length; for (int i = 0; i < length; i++) { Assert.IsTrue(webRoles[i].Equals(actual.WebRole[i])); } } else { Assert.IsNull(actual.WebRole); } if (workerRoles != null) { Assert.AreEqual<int>(workerRoles.Length, actual.WorkerRole.Length); int length = workerRoles.Length; for (int i = 0; i < length; i++) { Assert.IsTrue(workerRoles[i].Equals(actual.WorkerRole[i])); } } else { Assert.IsNull(actual.WorkerRole); } }
public static void AreEqualServiceDefinition(ServiceDefinition expected, ServiceDefinition actual) { throw new NotImplementedException(); }
/// <summary> /// Get the resolved runtime url for the runtime that will be installed on the given role /// </summary> /// <param name="definition">The service definition containing the role</param> /// <param name="roleName">The name of the role</param> /// <returns>The resolved runtime url for the runtime package to be installed on the role</returns> public static string GetRoleRuntimeUrl(ServiceDefinition definition, string roleName) { Variable v = GetRoleRuntimeEnvironment(definition, roleName).FirstOrDefault<Variable>( variable => string.Equals(variable.name, Resources.RuntimeUrlKey)); return (null == v ? null : v.value); }
/// <summary> /// Try to get the specified worker role from the given definiiton /// </summary> /// <param name="definition">The service definiiton</param> /// <param name="roleName">The name of the role</param> /// <param name="role">output variable where the worker role is returned</param> /// <returns>true if the web role is found in the given definition</returns> private static bool TryGetWorkerRole(ServiceDefinition definition, string roleName, out WorkerRole role) { role = definition.WorkerRole.FirstOrDefault<WorkerRole>(r => string.Equals(r.name, roleName, StringComparison.OrdinalIgnoreCase)); return role != null; }
/// <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; }
/// <summary> /// Validate that the actual role runtime values for the given role match the given expected values /// </summary> /// <param name="definition">The service definition containing the role to validate</param> /// <param name="roleName">The name of the role to validate</param> /// <param name="runtimeUrl">The resolved runtime url for the role</param> /// <param name="overrideUrl">The override url for the role runtime</param> public static void ValidateRoleRuntime(ServiceDefinition definition, string roleName, string runtimeUrl, string overrideUrl) { string actualRuntimeUrl = GetRoleRuntimeUrl(definition, roleName); string actualOverrideUrl = GetRoleRuntimeOverrideUrl(definition, roleName); Assert.IsTrue(VerifySetting(runtimeUrl, actualRuntimeUrl), string.Format( "Actual runtime URL '{0}' does not match expected runtime URL '{1}'", actualRuntimeUrl, runtimeUrl)); Assert.IsTrue(VerifySetting(overrideUrl, actualOverrideUrl), string.Format( "Actual override URL '{0}' does not match expected override URL '{1}'", actualOverrideUrl, overrideUrl)); }