private static async Task <Agent> PrepareAgentAsync(Options options, TeamCityClient client) { Agent agent = null; while (agent == null) { Console.WriteLine("Ensuring the agent is connected and has been authorized. Fetching the list of agents..."); var agents = await client.GetAgentsAsync(); agent = agents.FirstOrDefault(x => string.Equals( x.Name, options.AgentName, StringComparison.OrdinalIgnoreCase)); if (agent == null) { Console.WriteLine($"Agent '{options.AgentName}' does not appear in the list. Waiting {WaitDuration.TotalSeconds} seconds..."); } else if (!agent.Connected) { Console.WriteLine($"Agent '{agent.Name}' is not yet connected. Waiting {WaitDuration.TotalSeconds} seconds..."); agent = null; } else if (agent.Enabled != options.AgentEnabled) { Console.WriteLine($"The agent '{agent.Name}' does not have the proper enabled status '{options.AgentEnabled}'. Changing..."); await client.SetAgentEnabledAsync(agent.Id, options.AgentEnabled); Console.WriteLine($"Waiting {WaitDuration.TotalSeconds} seconds..."); agent = null; } else if (!agent.Authorized) { Console.WriteLine($"The agent '{agent.Name}' is not yet authorized. Authorizing..."); await client.SetAgentAuthorizationAsync(agent.Id, true); Console.WriteLine($"Waiting {WaitDuration.TotalSeconds} seconds..."); agent = null; } if (agent == null) { await Task.Delay(WaitDuration); } } return(agent); }
private static async Task<Agent> PrepareAgentAsync(Options options, TeamCityClient client) { Agent agent = null; while (agent == null) { Console.WriteLine("Ensuring the agent is connected and has been authorized. Fetching the list of agents..."); var agents = await client.GetAgentsAsync(); agent = agents.FirstOrDefault(x => string.Equals( x.Name, options.AgentName, StringComparison.OrdinalIgnoreCase)); if (agent == null) { Console.WriteLine($"Agent '{options.AgentName}' does not appear in the list. Waiting {WaitDuration.TotalSeconds} seconds..."); } else if (!agent.Connected) { Console.WriteLine($"Agent '{agent.Name}' is not yet connected. Waiting {WaitDuration.TotalSeconds} seconds..."); agent = null; } else if (agent.Enabled != options.AgentEnabled) { Console.WriteLine($"The agent '{agent.Name}' does not have the proper enabled status '{options.AgentEnabled}'. Changing..."); await client.SetAgentEnabledAsync(agent.Id, options.AgentEnabled); Console.WriteLine($"Waiting {WaitDuration.TotalSeconds} seconds..."); agent = null; } else if (!agent.Authorized) { Console.WriteLine($"The agent '{agent.Name}' is not yet authorized. Authorizing..."); await client.SetAgentAuthorizationAsync(agent.Id, true); Console.WriteLine($"Waiting {WaitDuration.TotalSeconds} seconds..."); agent = null; } if (agent == null) { await Task.Delay(WaitDuration); } } return agent; }
private static async Task <ApplicationResult> RunAsync(Options options) { var client = new TeamCityClient(new Uri(options.Server)); // Prepare the agent. Agent agent = await PrepareAgentAsync(options, client); // Optionally, add the agent to the agent pool. if (options.AgentPoolName != null) { var agentPools = await client.GetAgentPoolsAsync(); var agentPool = agentPools.FirstOrDefault(x => string.Equals( x.Name, options.AgentPoolName, StringComparison.OrdinalIgnoreCase)); if (agentPool == null) { Console.WriteLine($"No agent pool '{options.AgentPoolName}' was found."); return(ApplicationResult.NoMatchingAgentPool); } var agents = await client.GetAgentPoolAgentsAsync(agentPool.Id); if (!agents.Any(x => x.Id == agent.Id)) { Console.WriteLine($"Adding agent '{agent.Name}' to agent pool '{agentPool.Name}'..."); await client.AddAgentToAgentPoolAsync(agentPool.Id, agent.Id); } else { Console.WriteLine($"The agent '{agent.Name}' is already in agent pool '{agentPool.Name}'."); } } return(ApplicationResult.Success); }
private static async Task<ApplicationResult> RunAsync(Options options) { var client = new TeamCityClient(new Uri(options.Server)); // Prepare the agent. Agent agent = await PrepareAgentAsync(options, client); // Optionally, add the agent to the agent pool. if (options.AgentPoolName != null) { var agentPools = await client.GetAgentPoolsAsync(); var agentPool = agentPools.FirstOrDefault(x => string.Equals( x.Name, options.AgentPoolName, StringComparison.OrdinalIgnoreCase)); if (agentPool == null) { Console.WriteLine($"No agent pool '{options.AgentPoolName}' was found."); return ApplicationResult.NoMatchingAgentPool; } var agents = await client.GetAgentPoolAgentsAsync(agentPool.Id); if (!agents.Any(x => x.Id == agent.Id)) { Console.WriteLine($"Adding agent '{agent.Name}' to agent pool '{agentPool.Name}'..."); await client.AddAgentToAgentPoolAsync(agentPool.Id, agent.Id); } else { Console.WriteLine($"The agent '{agent.Name}' is already in agent pool '{agentPool.Name}'."); } } return ApplicationResult.Success; }