2012-01-05 23 views
7

क्षमा याचना, यह लगभग निश्चित रूप से this question का डुप्लिकेट है लेकिन यह है कि एक के रूप में मैं फिर से कोशिश करने जा रहा हूँ जवाब नहीं किया गया है।टीएफएस 2010 एपीआई, यह निर्धारित करता है कि कौन सा निर्माण सर्वर एक बिल्ड चल रहा है।

मैं एक उपकरण जो मुझे बनाता है या तो कतारबद्ध या 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

यही काम किया है, यह थोड़ा भद्दा है के रूप में मैं अब भी टीम परियोजनाओं पर निर्माण नियंत्रक क्वेरी को देखने के लिए बनाता है क्या पंक्तिबद्ध कर रहे हैं और उसके बाद क्वेरी करने के लिए है निर्माण के लिए व्यक्तिगत सर्वर बनाया जा रहा है। –

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); 
     } 
    } 
}