2

我知道可以在 Bamboo 代理页面上手动禁用或删除代理。我正在编写一个 Bamboo 插件,它应该在构建完成后删除 Bamboo 代理。使用 Bamboo REST API 或 SDK 库似乎没有直接的方法来做到这一点。

4

2 回答 2

3

我们(Atlassian Build Engineering)正在我们最近在https://bitbucket.org/atlassian/per-build-container开源的插件中做类似的事情

我们按需创建基于 Docker 的远程代理,它们构建一个作业,然后被删除。

于 2017-03-22T10:47:18.303 回答
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);
         }
     }
 }
}
于 2017-09-18T14:43:38.227 回答