2 using System.Collections.Generic;
3 using System.Diagnostics;
7 using System.Threading.Tasks;
10 using System.Xml.XPath;
12 namespace FUSE.AzureConfig.Tasks
16 public const string DefaultEnvName =
"__DEFAULT__";
18 public const string CsdefName =
"ServiceDefinition.csdef";
19 public const string CscfgNameFmt =
"ServiceConfiguration.{0}cscfg";
23 if (!Directory.Exists(csdefPath))
24 throw new ArgumentException(csdefPath +
" must exist");
26 outPath = outPath ?? csdefPath;
27 if (!Directory.Exists(outPath))
28 throw new ArgumentException(outPath +
" must exist");
30 var doc = XDocument.Load(Path.Combine(csdefPath, CsdefName));
31 var rootNS = GetRootNamespace(doc);
32 foreach (var role
in config.Roles)
34 var roleName = role.Value.RoleName;
35 var roleNode = (from e in doc.Descendants()
36 where (
string)e.Attribute(
"name") == roleName
37 select e).FirstOrDefault();
39 throw new ArgumentException(
"Unable to find role " + roleName);
42 var settings = roleNode.Element(rootNS +
"ConfigurationSettings");
45 Trace.TraceWarning(
"Adding ConfigurationSettings node for {0}", roleName);
46 settings =
new XElement(rootNS +
"ConfigurationSettings");
47 roleNode.Add(settings);
49 foreach (var setting
in role.Value.ConfigurationSettingKeys.OrderBy(x => x))
51 AddOrUpdateSetting(settings, rootNS, setting);
55 using (var writer = XmlWriter.Create(Path.Combine(outPath, CsdefName),
new XmlWriterSettings() { Indent =
true }))
61 if (!Directory.Exists(cscfgPath))
62 throw new ArgumentException(cscfgPath +
" must exist");
64 outPath = outPath ?? cscfgPath;
65 if (!Directory.Exists(outPath))
66 throw new ArgumentException(outPath +
" must exist");
68 foreach (var env
in config.Environments)
70 var envName = env.Value.EnvName;
72 var doc = XDocument.Load(Path.Combine(cscfgPath, string.Format(CscfgNameFmt,
"")));
73 var rootNS = GetRootNamespace(doc);
74 foreach (var role
in env.Value.Roles)
76 var roleName = role.Value.RoleName;
77 var roleNode = (from e in doc.Descendants()
78 where (
string)e.Attribute(
"name") == roleName
79 select e).FirstOrDefault();
81 throw new ArgumentException(
"Unable to find role " + roleName);
84 var instances = roleNode.Element(rootNS +
"Instances");
85 if (instances == null)
87 Trace.TraceWarning(
"Adding Instances node for {0}", roleName);
88 instances =
new XElement(rootNS +
"Instances");
89 roleNode.AddFirst(instances);
91 var countAttr = instances.Attribute(
"count");
92 if (countAttr == null)
94 countAttr =
new XAttribute(
"count", role.Value.Instances);
95 instances.Add(countAttr);
99 countAttr.Value = role.Value.Instances.ToString();
102 var settings = roleNode.Element(rootNS +
"ConfigurationSettings");
103 if (settings == null)
105 Trace.TraceWarning(
"Adding ConfigurationSettings node for {0}", roleName);
106 settings =
new XElement(rootNS +
"ConfigurationSettings");
107 roleNode.Add(settings);
109 foreach (var setting
in role.Value.ConfigSettings.Settings.OrderBy(x => x.Key))
111 AddOrUpdateSetting(settings, rootNS, setting.Key, setting.Value);
113 foreach (var setting
in role.Value.ConfigSettings.MissingSettings.OrderBy(x => x))
115 AddOrUpdateSetting(settings, rootNS, setting,
string.Empty);
119 var cscfgName = string.Format(CscfgNameFmt, envName == DefaultEnvName ?
"" : envName +
".");
120 using (var writer = XmlWriter.Create(Path.Combine(outPath, cscfgName),
new XmlWriterSettings() { Indent =
true }))
127 var namespaces = doc.Root.Attributes().Where(x => x.IsNamespaceDeclaration)
128 .GroupBy(x => x.Name.Namespace == XNamespace.None ? string.Empty : x.Name.LocalName)
129 .ToDictionary(x => x.Key, x => x.First());
130 return namespaces[
""].Value;
133 private void AddOrUpdateSetting(XElement settingsElt, XNamespace ns,
string name,
string value = null)
135 var existing = (from e in settingsElt.Elements()
136 where (
string)e.Attribute("name") == name
137 select e).FirstOrDefault();
140 if (existing == null)
141 settingsElt.Add(
new XElement(ns +
"Setting",
new XAttribute(
"name", name)));
145 if (existing == null)
146 settingsElt.Add(
new XElement(ns +
"Setting",
new XAttribute(
"name", name),
new XAttribute(
"value", value)));
149 var valueAttr = existing.Attribute(ns +
"value");
150 if (valueAttr == null)
151 throw new ArgumentException(
"Passed in value, found Setting entry for " + name +
", but it had no value attribute");
152 valueAttr.Value = value;
Processed Azure configuration
static XNamespace GetRootNamespace(XDocument doc)
void AlterCscfgs(AzureConfig config, string cscfgPath, string outPath=null)
void AlterCsdef(AzureConfig config, string csdefPath, string outPath=null)