Skip to main content

BuildUtilities.NET - NAntService

NAntService - Description
This utility allows NAnt build scripts to be run automatically once created. To do this a Windows Service monitors for a .build file to be created in a specifiedfolder. Once the build file has been executed by NAnt it (and any other files in the folder) can be either archived to a datetime stamped folder or deleted.

Once the service detects the .build has been created it will wait for 10 seconds(configurable) before trying to execute the script - this allows for any files the script depends upon to be copied to the folder. You can also schedule when NAnt actually executes the script once it has been copied to the folder. Instead of it being instantly executed you can set a time for NAnt to execute it.

You can download the source code from ProjectDistributor.NET

If you find a use for this utility it would be great to hear from you! Suggestions, bug reports etc are welcome via the ProjectDistributor feedback pages or comments on this blog


What problem does this solve?
In order to deploy builds to multiple servers we wanted a simple mechanism to do these repetitive tasks - NAnt and NAntContrib provide easily enough tasks to cover almost any deployment requirement - all we need was something to execute a build (ie deployment) script on each machine.

Our specific problem was updating IIS with a new build to a web application
The new code base sits in a version numbered folder, something like,

C:\inetpub\wwwroot\ourapplication_1.0.0.7
Our deployment NAnt script is used to...

1. Unzip the new code base (creates the new application folder)
2. Update the IIS application folder to point to the new code base folder
3. Email the devs that the script has run successfully

All we have to do is drop the deploy.build script and the build zip file into a folder on each server and it will be executed - bingo, one touch deployment :)

Application Design
The application uses "Triggers" and "Processors". Triggers detect the build file has been created and Processors process the build file once the trigger has been tripped.

At the moment the service can only use a single trigger and processor. The trigger and processor combination are configured in the app.config and they are dynamically loaded when the service starts. More triggers and processors can be created in a seperate assembly if required and loaded into the service as replacements - they just need to implement an interface (ITrigger & IFileProcessor respectively).

I have supplied 2 triggers and 1 processor.

NAntProcessor - launches NAnt.exe for the build file detected by the trigger.
FileTrigger - detects the build file and immediately invokes NAntProcessor.
ScheduledFileTrigger - detects the build file and invokes NAntProcessor at the time specified. If this time is in the past it will invoke it the next day at the time required.

To switch between the normal and scheduled filetrigger have a look in the app.config "Config" section - just uncomment/comment the one you want (remember to restart the service or console app though).

The service is designed to be fairly generic. The ITrigger interface specifies Setup(), Test(), Reset() and TearDown() methods. Setup() and TearDown() are called once when the service starts and stops respectively. Test() is called every 1 seconds and if it returns true (eg it did something and triggered) then Reset() is called to reset the trigger properties. By having the implementing Assemblies dynamically loaded when the service starts its possible for you to create your own Triggers and Processors - they don't have to have anything to do with NAnt!

Programs/Installation
NAntService
- A windows service application. Install this using installutil on the .exe
InstallUtil can be found in the C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322 folder

To install: installutil NAntService.exe
To un-install: installutil NAntService /u

Once installed it will show up in your service list as 'NAntService'.

NOTE: The user the service is running under will impact on some NAnt tasks - be sure the .build script tasks you want run are possible with the security permissions of NAntService account you have configured it with.

NAntServiceConsole - a console application. I used this to debug the application as it provides lots of Console.WriteLine diagnostic info. Once executed it will open a cmd window and sit there waiting for a .build file to be created. To terminate it just press Enter. This application does not require and special installation - just update the app.config (NAntServiceConsole.exe.config) with your values and then run the .exe

NOTE: Config file changes to NAntService.exe.config & NAntServiceConsole.exe.config will only be picked up once the service or console application have been restarted

** IMPORTANT ** You must have the NAnt bin folder in your system PATH otherwise NAnt will not execute correctly.

TODO
* Redirect the Console output to a log file when running as a Service to provide a history of events
* Enable multiple triggers to be configured


Disclaimer
This software is provided 'As is' and no warranty is implied by its use. By using this software or parts of the supplied software the user understands it is entirely at their own risk and the author shall not be liable in any way whatsoever.


Version History
02-Nov-05 v1.0.0.0 Initial Release

Comments

Popular posts from this blog

Walk-Thru: Using Wolfpack to automatically deploy and smoke test your system

First, some history... The advent of NuGet has revolutionised many many aspects of the .Net ecosystem; MyGet, Chocolatey & OctopusDeploy to name a few solutions building upon its success bring even more features to the table.

I also spotted that NuGet could solve a problem I was having with my OSS System Monitoring software Wolfpack; essentially this is a core application framework that uses plugins for extension (Wolfpack Contrib) but how to unify, standardise and streamline how these plugins are made available? NuGet to the rescue again - I wrapped the NuGet infrastructure (I deem NuGet to be so ubiquitous and stable that is has transcended into the software "infrastrucuture" hall of fame) with a new OSS project called Sidewinder. Sidewinder allows me to wrap all my little extension and plugins in NuGet packages and deploy them directly from the Wolfpack application - it even allows me to issue a new version of Wolfpack and have Wolfpack update itself, sweet huh?

Abou…

Resharper add-in idea - highlight IDisposable vars

[Update 18th July 2013]
@RobGibbens and Greg Hurlman picked up on this - Rob pointed out that there is an FxCop rule that can do this and Greg suggested a Visual Studio extension. I've had a quick look at the Visual Studio options and it looks like an "Editor Extension" is a good fit....hmmm, Project New, click...doh...dammit I really don't have time for this but it looks a fun little diversion! I'll update here if I get anything working.

[Original Post]
Had an interesting idea for a Visual Studio Resharper add-in the other day but don't have the time to implement it so thought I would put it out there...it might already exist (and hopefully someone can point me in the right direction) or someone will build it (Darren Voisey where are you?!).

The idea is very simple really - when you have a variable for an object that implements IDisposable it gets highlighted or a tell-tale is displayed to let you know it should be disposed or should be wrapped in a using sta…

Geckoboard Countdown Widget v2

v2 is here, now with added colours! This time using the RAG Numbers widget to display your date countdown - as you get nearer the date the number will change from green to amber then red.

The new url is: http://geckocountdown.appspot.com/rag/?date=2010-12-25&tz=0&msg=Days+to+Xmas!
Note 1: notice the new /rag/ path and the msg querystring param.
Note 2: the original v1 url still works


To use this on your Geckoboard add a new "Custom Widget/RAG Numbers" widget. By default the number will turn amber at 10 or less days to go and red at 3 or less days to go however you can change these with the querystring (see below).


Required querystring params
date: "yyyy-mm-dd" format, this is the target date to countdown totz: timezone offset in hours from GMT that you are in. Can be negative if you are behind GMT
Optional querystring params
msg: the label that appears next to the number of days (remember to encode spaces as +). The default is "Days Remaining" if you…