After working through three good sized projects with nant - there are some things that should have been more clear, but weren't for me. So heres some experience, hoping it saves you time.
Expressions
I got off to the wrong foot thinking ${} was part of how you referenced a property itself. This made it confusing when trying to reference properties within an expression. Cluu Phone!!! ${ } is about an expression, not just a property. Evaluating ${propertyname} is just an expression that just looks up the name's value, nothing else.
So when looking at the following
logdir in the first line does not have ${} around it because its inside an existing expression (which contains a function which is referning the name logdir...). Seeing the name without quotes, it is automatically assuming the text is the name of a property. If you were trying to put a string in there instead of an existing property, the string would be in quotes ' '.
In the second line, the expression is only evaluating the value of the property named 'logdir' so its just ${logdir}.
You'd be amazed how many tasks you can write before you recognize this :) However, when you start getting into functions to manipulate things.. you will fall down quickly with errors if you don't reference names or strings properly. Here is an example of a property definition that uses multiple nested functions, property references and strings.
This disaster is actually.. getting the current directory, then getting the parent of that directory, twice (to move up two levels), then using directory combine to append the string 'trendData' to that result. Note, only the string trendData has any quotes or other extra material.
Avoiding extra warning messages when moving, deleting, creating files
Well this one is just simple best practice coding. Ask before you do it. Use the directory:: and file:: functions to check for the existance of things before you go and try to delete or create new ones. Example:
Here's another example that includes using strings properly inside a function
A simple check before you delete...
Rename a group of files
Filesets/include sets can be a bit confusing. I had a task where I needed to rename all the files in a directory with a prefix. Here's the example
A Blog simply to put ideas and thoughts back out onto the net for others to find and gain from
Showing posts with label nant. Show all posts
Showing posts with label nant. Show all posts
Wednesday, October 15, 2008
nant tips and tricks - part #3 remote shutdown and remote execute
More nant examples to hopefully save you the effort!
Nant reboot remote computer
psshutdown.exe is part of the PStools kit from sysinternals (now Microsoft). The static timer at the end was an addition because it takes some time for the reboot request to take the machine down, even with the force option. I put the sleep in this task so my next task can immediately start checking for the server to be up and wait for it.
Nant launch a process remotely using psexec
In my last post, I mentioned the possibility of using psexec to launch remote programs. Here's a simple task example that does just that by launching ping on the remote computer.
Nant reboot remote computer
psshutdown.exe is part of the PStools kit from sysinternals (now Microsoft). The static timer at the end was an addition because it takes some time for the reboot request to take the machine down, even with the force option. I put the sleep in this task so my next task can immediately start checking for the server to be up and wait for it.
Nant launch a process remotely using psexec
In my last post, I mentioned the possibility of using psexec to launch remote programs. Here's a simple task example that does just that by launching ping on the remote computer.
nant tips and tricks - part #2 Windows Shares and Visual Basic
Here are some more nant examples I've used in my projects.
Nant Map a Windows Share
These simple tasks will map a Windows network share as a local drive letter using the NET USE command
The properties in the examples are pretty self-explainitory. The ones of interest were defined as follows in my scenario where I was mounting the c: of the remote machine defined in the property named 'server':
Nant reading the result code of an Installshield Install
When you run an installshield installer with silent mode, it outputs a text file. Sound practice says to read that file and ensure the installer did not fail. This reads the setup.log file from the path defined in deployLocalPath
UPDATE: The above is pretty hack - now that I recognized you can simply use a regex task.Here's a simplier example that does the same thing and loads the Result code into setupResultCode
Nant and visual basic scripts (vbs)
Sometimes doing this is easier with visual basic and if you don't have VisualStudio or can't code.. visual basic scripting is enough to access things like WMI so you can manipulate Windows machines. Visual Basic scripts are not intended to be console applications anymore.. so first problem is running them in a command line so output can be viewed and captured by nant. Solution.. run the script using cscript.exe. The following example calls the vbs script remoteprocUP.vbs and passes a few arguements to the vbs file
The second problem with visual basic scripts is getting some debugging or informational messages out to console so they can be seen or logged by nant. When using cscript.exe to launch the program, in the vbs file you can use WScript.StdOut.WriteLine(string) or WScript.Echo. Example:
The last challenge is handling error codes from visual basic apps. Again, when using cscript, you can simply use WScript.Quit 0 to return 0 (success) or WScript.Quit 1 to return 1 (failure). nant will see the failure code and automatically stop your script.
Here is a complete example. nant calls a target who's overall job is wait until a website is online. It calls a visual basic script using cscript and relies on error codes to know if things failed or not.
And here is the visual basic script
Nant Map a Windows Share
These simple tasks will map a Windows network share as a local drive letter using the NET USE command
The properties in the examples are pretty self-explainitory. The ones of interest were defined as follows in my scenario where I was mounting the c: of the remote machine defined in the property named 'server':
Nant reading the result code of an Installshield Install
When you run an installshield installer with silent mode, it outputs a text file. Sound practice says to read that file and ensure the installer did not fail. This reads the setup.log file from the path defined in deployLocalPath
UPDATE: The above is pretty hack - now that I recognized you can simply use a regex task.Here's a simplier example that does the same thing and loads the Result code into setupResultCode
Nant and visual basic scripts (vbs)
Sometimes doing this is easier with visual basic and if you don't have VisualStudio or can't code.. visual basic scripting is enough to access things like WMI so you can manipulate Windows machines. Visual Basic scripts are not intended to be console applications anymore.. so first problem is running them in a command line so output can be viewed and captured by nant. Solution.. run the script using cscript.exe. The following example calls the vbs script remoteprocUP.vbs and passes a few arguements to the vbs file
The second problem with visual basic scripts is getting some debugging or informational messages out to console so they can be seen or logged by nant. When using cscript.exe to launch the program, in the vbs file you can use WScript.StdOut.WriteLine(string) or WScript.Echo. Example:
The last challenge is handling error codes from visual basic apps. Again, when using cscript, you can simply use WScript.Quit 0 to return 0 (success) or WScript.Quit 1 to return 1 (failure). nant will see the failure code and automatically stop your script.
Here is a complete example. nant calls a target who's overall job is wait until a website is online. It calls a visual basic script using cscript and relies on error codes to know if things failed or not.
And here is the visual basic script
nant tips and tricks - part #1
Here are some of the simple tasks and example syntax I've used to help clean up or perform certain tasks in nant
Setting a property up to have a default value or override it via environment variable.
Since nant doesn't have an else to go with if, here's a simple alternative
I used to do this by specifying the property on the command line when launching nant - but using environment variables was much cleaner when the list of variables got long because our TeamCity Build Agent can define environment variables on the fly
Deleting and recreating a directory without generating error messages
Here's a simple target that plays nice when deleting an existing directory so you can tell what happened in the logs without erroneous error messages. Note how the variable name in the directory:exists does not have the ${} because inside the expression already
Starting a process on a remote computer
This execute will launch notepad.exe on the remote computer by logging in with the supplied credentials and using WMI via wmic.exe
Psexec.exe from sysinternals (now Microsoft) is another alternative to launch programs remotely, but is known to have problems in some scripted setups. Both psexec.exe and wmic.exe worked for me in nant, but neither worked when the nant script was called by my TeamCity build agent (java) :(
Setting a property up to have a default value or override it via environment variable.
Since nant doesn't have an else to go with if, here's a simple alternative
I used to do this by specifying the property on the command line when launching nant - but using environment variables was much cleaner when the list of variables got long because our TeamCity Build Agent can define environment variables on the fly
Deleting and recreating a directory without generating error messages
Here's a simple target that plays nice when deleting an existing directory so you can tell what happened in the logs without erroneous error messages. Note how the variable name in the directory:exists does not have the ${} because inside the expression already
Starting a process on a remote computer
This execute will launch notepad.exe on the remote computer by logging in with the supplied credentials and using WMI via wmic.exe
Psexec.exe from sysinternals (now Microsoft) is another alternative to launch programs remotely, but is known to have problems in some scripted setups. Both psexec.exe and wmic.exe worked for me in nant, but neither worked when the nant script was called by my TeamCity build agent (java) :(
Subscribe to:
Posts (Atom)