Reputation: 1004
I know that it is possible to manually disable or delete an agent on the Bamboo agents page. I am writing a Bamboo plugin that should delete Bamboo agents when the build is completed. There does not seem to be a straight forward way to do that either using the Bamboo REST API or SDK library.
Upvotes: 2
Views: 1395
Reputation: 1004
This is pretty much a modified version of what I found in https://bitbucket.org/atlassian/per-build-container shared by @mkleint. I added a query to get the list of agents based on job requirements.
import com.atlassian.bamboo.plan.ExecutableAgentsHelper;
import com.atlassian.bamboo.buildqueue.manager.AgentManager;
import com.atlassian.bamboo.v2.build.agent.AgentCommandSender;
import com.atlassian.bamboo.v2.build.agent.BuildAgent;
import com.atlassian.bamboo.v2.build.agent.messages.StopAgentNicelyMessage;
class DeleteAgent{
private AgentManager agentManager;
private AgentCommandSender agentCommandSender;
//Atlassian managed object and will be auto injected when this setter is found
public void setAgentManager(AgentManager agentManager) {
this.agentManager = agentManager;
}
//Atlassian managed object and will be auto injected when this setter is found
public void setAgentCommandSender(AgentCommandSender agentCommandSender) {
this.agentCommandSender = agentCommandSender;
}
public static void main(String[] args) {
RequirementSetImpl reqs = new RequirementSetImpl();
reqs.addRequirement(new RequirementImpl("requirement-key","requirement-value");
reqs.addRequirement(new RequirementImpl("requirement-key","requirement-value");
Collection<BuildAgent> agentsToBeDeleted = executableAgentsHelper.getExecutableAgents(ExecutableAgentsHelper.ExecutorQuery.newQuery(reqs));
Iterator<BuildAgent> agentsToBeDeletedIterator = agentsToBeDeleted.iterator();
while(agentsToBeDeletedIterator.hasNext()) {
this.deleteSingleAgentOnBambooServer(agentsToBeDeletedIterator.next());
}
}
private void deleteSingleAgentOnBambooServer(BuildAgent agent) {
if(agent!=null) {
this.stopAgentRemotely(agent.getId());
this.removeAgent(agent);
}
}
public void stopAgentRemotely(BuildAgent buildAgent) {
Long agentId = buildAgent.getId();
String agentName = buildAgent.getName();
buildAgent.setRequestedToBeStopped(true);
agentCommandSender.send(new StopAgentNicelyMessage(), agentId);
}
public void stopAgentRemotely(long agentId) {
BuildAgent ba = agentManager.getAgent(agentId);
if (ba != null) {
stopAgentRemotely(ba);
}
}
public void removeAgent(BuildAgent agent) {
if (agent != null) {
removeAgent(agent.getId());
}
}
public synchronized void removeAgent(long agentId) {
BuildAgent ba = agentManager.getAgent(agentId);
if (ba != null) {
String agentName = ba.getName();
try {
agentManager.removeAgent(agentId);
} catch (TimeoutException e) {
System.out.println(String.format("timeout on removing agent %s (id: %s)", agentName, agentId), e);
}
}
}
}
Upvotes: 0
Reputation: 2331
We (Atlassian Build Engineering) are doing something similar in our plugins that we recently open sourced at https://bitbucket.org/atlassian/per-build-container
We create Docker based remote agents on demand, they build a job and then get removed.
Upvotes: 3