我知道可以在Bamboo代理页面上手动禁用或删除代理。我正在编写一个Bamboo插件,它应该在构建完成时删除Bamboo代理。使用Bamboo REST API或SDK库似乎没有直接的方法。
答案 0 :(得分:3)
我们(Atlassian Build Engineering)在我们最近在https://bitbucket.org/atlassian/per-build-container开源的插件中做了类似的事情
我们根据需要创建基于Docker的远程代理,他们构建一个工作然后被删除。
答案 1 :(得分:0)
这几乎是我在@mkleint共享的https://bitbucket.org/atlassian/per-build-container中找到的修改版本。我添加了一个查询,以根据工作要求获取代理列表。
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);
}
}
}
}