Example #1
0
        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;
        }
Example #2
0
        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;
        }
Example #3
0
        public static bool TryParseOptions(string[] args, out Options options)
        {
            options = null;

            if (args.Length < 4)
            {
                Console.WriteLine("At least four arguments are required.");
                Console.WriteLine();
                Console.WriteLine($"{AppDomain.CurrentDomain.FriendlyName} TC_SERVER TC_AGENT_NAME TC_AGENT_ENABLED TIMEOUT_SEC [TC_AGENT_POOL]");
                Console.WriteLine();
                Console.WriteLine("  TC_SERVER        The base HTTP URL for the TeamCity server.");
                Console.WriteLine("  TC_AGENT_NAME    The name of the TeamCity agent to authorize.");
                Console.WriteLine("  TC_AGENT_ENABLED Whether or not the agent should be enabled.");
                Console.WriteLine("  TIMEOUT_SEC      The maximum number of seconds to take before failing.");
                Console.WriteLine("  TC_AGENT_POOL    (optional) The pool to move the agent to once authorized.");
                Console.WriteLine();
                Console.WriteLine("This application blocks until an agent appears in the provided TeamCity server.");
                Console.WriteLine("Once the agent appears, the agent with be authorized and the application will");
                Console.WriteLine("then block until the agent is updated (if necessary) and in the desired pool.");
                Console.WriteLine();
                Console.WriteLine("This application uses Windows Authentication to access the TeamCity server.");

                return false;
            }

            options = new Options();
            options.Server = args[0];
            options.AgentName = args[1];
            options.AgentEnabled = bool.Parse(args[2]);
            options.Timeout = TimeSpan.FromSeconds(int.Parse(args[3]));

            if (args.Length > 4)
            {
                options.AgentPoolName = args[4];
            }

            return true;
        }