The Teamaton tool belt: a collection of small but useful tools for every day development and administration

by Oliver Wed, January 26 2011 20:30

Today: a simple hosts file editor

Today I set up a new project on GitHub: the Teamaton tool belt! It shall serve us as a central store for small tools, probably mostly command-line, built for a single purpose. The first tool in our new tool belt is: HostsEditor.

I wrote this small command line utility after reading this blog post: http://apdubey.blogspot.com/2008/09/edit-host-file-by-batch-file.html. We were looking for an easy way to edit the Windows hosts file from a script for a larger development environment setup that we wanted to fully automate including IIS website, bindings and new entries in the said hosts file. Of course, for a one-timer echo does the job, but don’t try to run the setup script twice on the same maching – you’ll get duplicate entries very quickly.

Reading about how other people are asking the author of the above post how to avoid the duplicates or how to erase entries from the hosts file, I set out to simply write such a utility myself and share it with the rest of the world. If there is one other person out there to whom it will be useful I will be happy.

So, HostsEditor does the following:

  • Adds and removes entries to and from the Windows hosts file.
  • Automatically backs up the original hosts file to hosts.bck.
  • Does not add duplicate entries; instead prints a warning.
  • For usage in automation scripts there is /q switch to suppress all info messages.

hostseditor-usage

That’s all there is to it for now. But there’s more to come!

Stay tuned, Oliver

Tags:

Build | Productivity | Automation

Automatic deployment of an ASP.NET Web Application Project with TeamCity and MSBuild

by Oliver Fri, January 21 2011 20:19

We recently updated one our largest project to use ASP.NET 4.0, and for this matter the new Package/Publish feature including sub-web.configs which is meant to supersede the Web Deployment Project. For a manual deployment there’s a good write-up on the msdn library titled ASP.NET Web Application Project Deployment Overview which shows how and where to set this up.

In our case this was not satisfactory because our deployment process is a bit more complicated. We push our changes to a central repository and use JetBrains’ continuous integration server (CIS) TeamCity Professional, which is totally free for our project size, for a continuous integration process. Once TeamCity has pulled and tested the current version, it is supposed to deploy this version to our staging server where we further test the complete site.

The key point in an automatic deployment was the management of the different web.config files for the different environments our project is running on. Unfortunately, until yesterday every deployment that included changes to the web.config file – even to the staging server - required a manual step of editing the web.config that live on our staging system (outside of source control!). What we used to do: after a successful build on our CIS we simply copied the web application (files) to our staging server! But as Scott Hanselman wrote: If You're Using XCopy, You're Doing It Wrong! This post inspired us to move along and take advantage of the new possibilities that we were given.

In the meanwhile, before switching to .NET 4.0 actually, we also took a shot at the Web Deployment Project way of doing things but never actually got that far as to fully automate the deployment – somehow the setup was not as easy as we hoped. Anyway, we wanted web.config Transforms!

So what does our setup look like and what did we want to do?

setup 

During local development and testing I use a web.config file that talks to a local DB instance and has some more specific settings. To run the web application on our staging server we need to replace certain values or whole sections in the web.config. For this transformation we use the sub-web.config files, one for each build configuration:

web-configs

Now, with all of these web.config files the simple XCOPY deployment we used to use does not work any longer. We need to trigger the web.config transformation on the build server and then deploy the whole application. As easy as this looks using the built-in menus and dialogs in Visual Studio – it took me quite a while to find how to do this in an automated build, more concretely from the command line.

After unsuccessfulle skimming stackoverflow.com for a solution I finally tripped over this very informative blog post on publishing a VS2010 ASP.NET web application using MSBuild. Admittedly, the author focuses on how to publish on the local machine as it’s yet a different process but towards the end he posts the solution I was looking for:

   1: msbuild Website.csproj "/p:Platform=AnyCPU;Configuration=Release;DesktopBuildPackageLocation=c:\_Publish\stage\Website.zip" /t:Package

This was it! After running this on my machine with my own settings I looked into the folder with the zip file and found the following 5 files:

package-folder

At first I just wanted to take the zip file, copy it to the staging server, unpack it – done! But then I peaked into it… and deeper… and deeper… and… still deeper… until I finally saw our application files underneath this directory:

zip-path

This has got to be one of the longest paths I’ve ever seen and used! How would I automate the extraction of web application files from the zip with such a path? I was already seeing myself hacking away on the command line…

But wait: what about those files that appeared next to the zip file? A ci-stage.deploy.cmd and a readme.txt caught my attention – of course, I opened the cmd file first :-D

deploy-cmd

Well… maybe the readme file gives me a shortcut to understanding this and the rest of the 190 lines:

readme

Looks promising! I convinced myself to give it a shot. So we set up a new configuration in TeamCity with the following settings:

tc-config

These settings reflect the command line from above with a few minor changes (removed the DesktopBuildPackageLocation and set the /v[erbose] switch to m[inimal]):

msbuild Website.csproj "/p:Platform=AnyCPU;Configuration=Release" /t:Package /v:m

The second step is to use the generated script, ci-stage.deploy.cmd. I recommend to run the script by hand once using the /T switch just to make sure everything looks alright. In our case we found out that the deployment of the package would have deleted a lot of files, most of all images, from our website. This was not what we wanted! After a quick search I found this question on stackoverflow.com: MSDeploy: “Leave extra files on destination” from command line? So I added this switch to the parameters list in the build step configuration as follows:

web-deploy

That’s it! This is all we need on the command line to generate a package that is ready for deployment on the staging server. There are a few more necessary settings, including the DesktopBuildPackageLocation, that can be found in the Package settings window inside the project properties of the web application project:

  • the DesktopBuildPackageLocation can be set here instead of on the command line
  • the website and application name on the destination IIS server that this package should be installed to
  • some more options like excluding debug symbols etc.

package-settings

These settings are saved in the project file and will be used during generation and deployment of the package.

That’s all I have to say right now.

Happy Coding, Oliver

Tags:

ASP.NET | Automation | Productivity | Software development | Web Applikationen