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

Configuration in .Net 2.0

11-Dec-2007 Update I've updated this post to fix the broken images and replaced them with inline text for the example xml and accompanying C# code. This post has been by far the most hit on this blog and along with the comments about the missing images I thought it was time to update it! Whilst recreating the examples below I zipped up the working source code and xml file and loaded this onto my Project Distributor site - please download it to get a full working custom configuration to play with! Just click on the CustomConfigExampleSource link on the right hand side, then the "Source" link to get the zip. We are in the process of converting our codebase to .Net 2.0. We've used Enterprise Library to great effect so decided that we should continue with this in the form of the Jan 2006 release which targets 2.0 and I've got the job of porting our Logging, Data Access etc wrappers to EntLib 2.0. ...And so far so good - the EntLib docs aren't bad and the migrati...

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...

Announcing FluentGeoApi - a C# wrapper to GeoAPI.com

I'm pleased to make public the fruits of my late nights.... FluentGeoApi ! I previously mentioned that I am working on a private/personal project - well it's got an element of geolocation to it and after a bit of internet research I found GeoAPI.com . In order to interact with GeoAPI I decided to write a fluent style wrapper over the top of it and I've just released v1.0.0.0, a .Net 3.5 C# library to take the pain out of making REST calls and dealing with the GeoJson wire format used by GeoAPI. It's not 100% coverage of the API but I plan on getting there ASAP...however I've implemented Create/Modify/Delete a user entity, Simple and Keyword Search which is enough to release it. If you are working with geolocation data/features in your .Net application I would check out GeoAPI.com - I've been really impressed with what if offers (and if you hit the api < 20,000 times a day it won't cost you a penny!) - hopefully you'll also consider using FluentGe...