简体   繁体   中英

Merge external .config files back into web.config that have used the configSource extraction

I use external config files for various dynamic sections in my web.config files. Essentially all the dynamic variables between environments (Prod, UAT, Test, Dev etc.) have been externalised out to generic named .config files.

My web.config is generic as well so all environments utilise the same web.config (as things like connection strings etc. are in the external files). All the files are named the same across the environments so when we do a release we just need to keep the web.config and \\Configs* directory in tact during a release.

What I want to do (via something like XSLT or PowerShell) is to merge all of the information contained within these external *.config files back into the web.config so that I can compare the (now merged) web.config with the version provided by developers.

So our present web.config may say:

<sessionState configSource="Configs\System.Web.SessionStateConnectionString.config" />

And in the System.Web.SessionStateConnectionString.config file for the Prod environment has:

<sessionState mode="StateServer" cookieless="true" timeout="20"/>

And for the Test environment has:

<sessionState mode="InProc" cookieless="true" timeout="20"/>

I'd like to merge in all the external *.config files back into a web.config so that the file is essentially completed as if configSource had never been used. This will allow me to perform a diff or WinMerge on the web.config file (after it's been merged back) and what the developer gives us, showing which things have been changed such as new keys added or new modules utilised.

What would be the best way of going about this?

I don't have a simple approach. I do hope there is one exist and it would help both of us.

I found the only reliable way of modifying a large amount of config file during deployment without mistake is do the actual XML editing. Which means follow the XML structure.

With that, the task you are trying to archive, is not only changing a XML value

From

<sessionState configSource="Configs\System.Web.SessionStateConnectionString.config" />

To

<sessionState configSource="abc.config" />

but also changing XML parent's name

From

<sessionState configSource="abc.config" />

To

<sessionState mode="stateServer" />

Then, you need attach more child to make it

<sessionState mode="StateServer" cookieless="true" timeout="20"/>

Thus, I saved all environmental variables I need in a deployment tool, instead of using another XML for comparsion. During deployment, my deployment script (powershell) directly load the config file $config = [xml](get-content $webconfig) and load the variables from deployment tool

For a simple change I would do

$node = $config.configuration.sessionState | where {$_.configSource -eq 'Configs\System.Web.SessionStateConnectionString.config'}
$node.configSource = $Prod_session_config #"abc.config"

For the change you need, I will detach configSource from sessionState and reattach 3 new child mode , cookieless , timeout with their desired value.

With that said, this is a very lengthy deployment script. You can create your own helpers to make it look much better, something like replace-config $config_keyname $config_keyvalue . Even with helpers to reduce common operations you need, this is still easily thousands line of code depending on how many moving parts your application have.

This would probly take weeks to accomplish. Once this is done, there is unlikely any massive changes to it but still require you to have very good communication with developers, you need them to update you whenever a new environment dependent variable is introduced.

This task can also be done with configuration management tools like Puppet Augeas, but I don't think that's any easier if you write down a few good helpers in powershell. I looked into XSLT as well but couldn't find a convenient way to use it with my deployment tool.

This is my solution with limited devops experience and I desperately hope there would be a better one same as you do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM