2012-01-05 95 views
7

道歉,這幾乎可以肯定是this question的重複,但是如果沒有人回答,我會再試一次。TFS 2010 API,確定構建在哪個構建服務器上運行。

我正在嘗試構建一個工具,該工具將允許我查看所有在TFS上排隊或運行的構建。

其中一個要求是能夠看到正在運行構建的構建服務器。 IQueuedBuildsView中的所有「BuildAgent」屬性和方法都被棄用,並且未實現異常。有很多方法可以查詢代理商,但是您需要代理商的姓名或名稱,然後才能做到這一點,並且我感覺自己處於雞和雞蛋的狀況。

有誰知道如何找到正在運行的構建的構建服務器名稱?我的下面的代碼片段可能會有所幫助。

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 
using System.Net; 
using System.Text; 
using Microsoft.TeamFoundation.Server; 
using Microsoft.TeamFoundation.Build.Client; 
using Microsoft.TeamFoundation.VersionControl.Client; 
using Microsoft.TeamFoundation.Framework.Client; 
using Microsoft.TeamFoundation.Framework.Common; 
using Microsoft.TeamFoundation.Client; 


namespace TeamFoundationServerTools 
{ 
    public static class TeamBuildData 
    { 

     public static void Main() 
     { 

      Uri teamFoundationServerUri = new Uri("http://tfs:8080/tfs"); 
      Uri teamFoudationServerProjectCollectionUri = new Uri("http://tfs:8080/tfs/collection"); 
      string teamFoundationServerName = "tfs"; 
      string teamFoundationServerProjectCollectionName = string.Empty; 
      string teamFoundationServerProjectName = string.Empty; 

      try 
      { 

       Dictionary<string, Uri> collections = new Dictionary<string, Uri>(); 

       if (string.IsNullOrEmpty(teamFoundationServerProjectCollectionName)) 
       { 
        DetermineCollections(teamFoundationServerUri, collections); 
       } 
       else 
       { 
        collections.Add(teamFoundationServerName, teamFoudationServerProjectCollectionUri); 
       } 

       QueryCollections(teamFoundationServerName, teamFoundationServerProjectName, collections); 

      } 
      catch (Exception ex) 
      { 
       Console.Write(ex.ToString()); 
      } 
     } 

     /// <summary> 
     /// Queries the Team project collection for team builds 
     /// </summary> 
     /// <param name="teamFoundationServerName">the name of the TFS server</param> 
     /// <param name="teamFoundationServerProjectName">the name of the Team Project</param> 
     /// <param name="collections">the Team Project Collections to be queried</param> 
     private static void QueryCollections(string teamFoundationServerName, string teamFoundationServerProjectName, Dictionary<string, Uri> collections) 
     { 
      foreach (KeyValuePair<string, Uri> collection in collections) 
      { 
       // connect to the collection 
       using (TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(collection.Value, CredentialCache.DefaultCredentials)) 
       { 
        Console.WriteLine(teamProjectCollection.Name); 

        IBuildServer buildServer = (IBuildServer)teamProjectCollection.GetService(typeof(IBuildServer)); 

        // get ICommonStructureService (later to be used to list all team projects) 
        ICommonStructureService commonStructureService = (ICommonStructureService)teamProjectCollection.GetService(typeof(ICommonStructureService)); 

        // I need to list all the TFS Team Projects that exist on a server 
        ProjectInfo[] allTeamProjects; 

        if (!String.IsNullOrEmpty(teamFoundationServerProjectName)) 
        { 
         allTeamProjects = new ProjectInfo[1]; 
         allTeamProjects[0] = new ProjectInfo(); 
         allTeamProjects[0] = commonStructureService.GetProjectFromName(teamFoundationServerProjectName); 
        } 
        else 
        { 
         allTeamProjects = commonStructureService.ListProjects(); 
        } 

        // iterate thru the team project list 
        foreach (ProjectInfo teamProjectInfo in allTeamProjects) 
        { 
         Console.WriteLine(teamProjectInfo.Name); 

         // skip this team project if it is not WellFormed. 
         if (teamProjectInfo.Status != ProjectState.WellFormed) 
         { 
          continue; 
         } 

         IQueuedBuildsView queuedBuildsView = buildServer.CreateQueuedBuildsView(teamProjectInfo.Name); 
         queuedBuildsView.StatusFilter = QueueStatus.Queued | QueueStatus.InProgress | QueueStatus.Postponed; 

         queuedBuildsView.QueryOptions = QueryOptions.All; 

         queuedBuildsView.Refresh(false); 
         foreach (IQueuedBuild queuedBuild in queuedBuildsView.QueuedBuilds) 
         { 
          Console.WriteLine(queuedBuild.BuildDefinition.Name); 
          Console.WriteLine(queuedBuild.BuildController.Name); 
          Console.WriteLine(queuedBuild); 
          Console.WriteLine(queuedBuild.Status); 
          Console.WriteLine(queuedBuild.RequestedBy); 
          Console.WriteLine(queuedBuild.QueuePosition); 
          Console.WriteLine(queuedBuild.QueueTime); 
          Console.WriteLine(queuedBuild.Priority); 
          Console.WriteLine(); 

          if (queuedBuild.Status == QueueStatus.InProgress) 
          { 


          } 

          Console.WriteLine("***********************"); 

         } 
        } 
       } 
      } 

      Console.ReadLine(); 
     } 

     /// <summary> 
     /// Determins the team project collections for a given TFS instance 
     /// </summary> 
     /// <param name="teamFoundationServerUri">the uri of the Team Foundation Server</param> 
     /// <param name="collections">a dictionary of collections to be added to</param> 
     private static void DetermineCollections(Uri teamFoundationServerUri, Dictionary<string, Uri> collections) 
     { 
      // get a list of Team Project Collections and their URI's 
      using (TfsConfigurationServer tfsConfigurationServer = new TfsConfigurationServer(teamFoundationServerUri)) 
      { 
       CatalogNode configurationServerNode = tfsConfigurationServer.CatalogNode; 

       // Query the children of the configuration server node for all of the team project collection nodes 
       ReadOnlyCollection<CatalogNode> tpcNodes = configurationServerNode.QueryChildren(
         new Guid[] { CatalogResourceTypes.ProjectCollection }, 
         false, 
         CatalogQueryOptions.None); 

       foreach (CatalogNode tpcNode in tpcNodes) 
       { 
        ServiceDefinition tpcServiceDefinition = tpcNode.Resource.ServiceReferences["Location"]; 

        ILocationService configLocationService = tfsConfigurationServer.GetService<ILocationService>(); 
        Uri tpcUri = new Uri(configLocationService.LocationForCurrentConnection(tpcServiceDefinition)); 

        collections.Add(tpcNode.Resource.DisplayName, tpcUri); 
       } 
      } 
     } 
    } 
} 

回答

4

我創建了一個LinqPad腳本來執行此操作。通過查詢控制器上的所有代理,您可以看到針對每個代理運行的構建。腳本中還有一些額外的東西是我自己的偏好添加的。

TFS Build Agents我的SkyDrive

+0

這做的工作,這是一個有點笨重,因爲我還是要查詢的團隊項目生成控制器,看看有什麼版本是排隊,然後查詢正在構建的構建的單個服務器。 – 2012-01-10 15:08:24

1

這裏有一個PowerShell腳本做同樣的。請注意,您需要替換tfs服務器並構建控制器名稱字符串。

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client") | Out-Null 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.WorkItemTracking.Client") | Out-Null 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client") | Out-Null 

$teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection("http://tfsserver:8080/tfs") 
$bs = $teamProjectCollection.GetService([type]"Microsoft.TeamFoundation.Build.Client.IBuildServer") 
$tfsapps_controller=$bs.QueryBuildControllers('true') | where {$_.Name -like '*YOURBUILDCONTROLLER*'} 
$agents=$tfsapps_controller.Agents 

foreach ($agent in $agents){ 
    if ($agent.IsReserved){ 
    $build=$bs.QueryBuildsByUri($agent.ReservedForBuild,'*','All') 
    Write-Host $build[0].BuildDefinition.Name, ' : ', $agent.MachineName 
    } 
} 
2

從我看到的,你有90%的代碼;這裏要說的是要完成任務中的最後10%:

if (queuedBuild.Status == QueueStatus.InProgress) 
    { 
    //search agent associated to running build 
    foreach (IBuildAgent agent in queuedBuild.BuildController.Agents) 
    { 
     if (queuedBuild.Build.Uri.Equals(agent.ReservedForBuild)) 
     { 
      Console.WriteLine(" associated Build Agent =" + agent.Name); 
     } 
    } 
}