Some code to remove old build agent and test environment references
Disclaimer: This is just a blog post, and this isn’t supported code by any stretch of the imagination. It hasn’t been certified by anybody. I can’t say that customer support will help you if you’ve used it. It’s just me using the TFS API. |
I have an unusual situation. Since I demonstrate Visual Studio, TFS, and Lab Management a lot, and I’m a field employee, I have everything running on my laptop. I’ve set up my laptop to run Server 2008 R2 SP1, and TFS is on a VM, as is a domain controller, and some lab environments.
Over the past year, I’ve actually created a new TFS VM, detached the old TFS database, and moved it to the new instance. I’ve also decommissioned an old lab environment and created a new one with nearly the same name. That’s left a few state references in my TFS database. I wanted those old references gone—there are some problems in having services listed that don’t really exist any more.
So, for my build server, I used the TFS API and wrote a console application to find the now-departed build agents and unregister them properly. In my case, I’m attaching to “mycollection” on “myserver”, and deleting agents on build servers on a machine named “stalehost”.
Delete stale build servers
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.TeamFoundation.Client;
- using Microsoft.TeamFoundation.Build.Client;
- using System.Diagnostics;
- namespace UnregisterBuildServiceHost
- {
- class Program
- {
- static void Main(string[] args)
- {
- var tfs = TeamFoundationServerFactory.GetServer("myserver:8080/tfs/mycollection");
- IBuildServer buildServer = (IBuildServer)tfs.GetService(typeof(IBuildServer));
- var buildServiceHosts = (IEnumerable<IBuildServiceHost>) buildServer.QueryBuildServiceHosts("stalehost*");
- var buildControllers = (IEnumerable<IBuildController>)buildServer.QueryBuildControllers(true);
- foreach (IBuildServiceHost buildHost in buildServiceHosts)
- {
- Debug.WriteLine("{0} URI:({1}) Controller:({2}) Number of agents: {3}", buildHost.Name, buildHost.Uri, buildHost.Controller, buildHost.Agents.Count);
- foreach (var agent in buildHost.Agents)
- {
- agent.Delete();
- }
- if (buildHost.Controller != null) buildHost.Controller.Delete();
- buildHost.Delete();
- }
- foreach (IBuildController controller in buildControllers)
- {
- Debug.WriteLine("{0} ({1})", controller.Name, controller.ServiceHost.Name);
- foreach (IBuildAgent agent in controller.Agents)
- {
- Debug.WriteLine("\t{0} ({1}) {2}", agent.Name, agent.ServiceHost.Name, agent.Uri);
- }
- }
- }
- }
- }
The build server installation will also allow you to replace an existing “stale” agent with the one you’re installing. So this is only in the instance that you have a build server that you didn’t unregister and you’re not replacing. Again, I’m connecting to “mycollection” on “myserver”, to unregister the stale environment “Departed Env”.
Delete stale test environments
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using Microsoft.TeamFoundation.Client;
- using Microsoft.TeamFoundation.TestManagement.Client;
- namespace UnregisterBuildServiceHost
- {
- class Program
- {
- static void Main(string[] args)
- {
- TeamFoundationServer tfs;
- ITestManagementService testManagementService;
- tfs = new TeamFoundationServer("myserver:8080/tfs/mycollection");
- testManagementService = tfs.GetService<Microsoft.TeamFoundation.TestManagement.Client.ITestManagementService>();
- var testControllerHelper = testManagementService.TestControllers;
- var testControllers = testControllerHelper.Query();
- foreach (var testController in testControllers)
- {
- Debug.WriteLine("Display Name'{0}' :Group ID({1}) Name:({2})", testController.DisplayName, testController.GroupId, testController.Name);
- var testenvs = testController.TestEnvironments.Query();
- foreach (var testenv in testenvs)
- {
- Debug.WriteLine("\tName: {0}, Desc:({1}) Date Created: ({2}) Environment Type:({3}) Error({4})",
- testenv.DisplayName, testenv.Description, testenv.DateCreated, testenv.EnvironmentType, testenv.Error);
- if (testenv.DisplayName.Equals("Departed Env"))
- {
- testenv.Unregister();
- }
- }
- }
- }
- }
- }
Now, both of these are single-purpose monolithic console applications. This isn’t elegant code—it’s just a hack to solve a temporary condition. I just thought it was a simple demonstration of the TFS API.
Technorati Tags: TFS API,test agent,test environment,Visual Studio,Test Management,UnregisterBuildServiceHost,TeamFoundationServerFactory,IBuildServer,IBuildServiceHost,QueryBuildServiceHosts,IBuildController,QueryBuildControllers,IBuildAgent,TestManagement,TeamFoundationServer,ITestManagementService,TestControllers
Windows Live Tags: TFS API,test agent,test environment,Visual Studio,Test Management,UnregisterBuildServiceHost,TeamFoundationServerFactory,IBuildServer,IBuildServiceHost,QueryBuildServiceHosts,IBuildController,QueryBuildControllers,IBuildAgent,TestManagement,TeamFoundationServer,ITestManagementService,TestControllers