Version Differences for Agent Filter

(Select Agents from Multi-Select Property)
(AHPSCRIPTS-30)
Line 223:
  }     }  
  };</pre>    };</pre> 
       
    + = Determine if Agents Skipped During Deployment And Run ReDeploy on Skipped Agents =  
    + *This process involves two scripts.  
    + **'''Agent Filter Script''' - Determines if it is the first time a particular buildLife is being deployed to a specific environment. If it is the first deployment, the filter determines what agents are online and returns those agents for deployment. If it is a redeployment, the filter only returns those agents marked for redeployment.  
    + ** '''Evaluate Script Step''' - This script determines what agents were offline (if any) and marks them for redeployment.  
       
    + ==== AHPSCRIPTS-102 ====  
    + ''Agent Filter'':  
    + <pre>import com.urbancode.anthill3.domain.agent.Agent;  
    + import com.urbancode.anthill3.services.agent.AgentManager;  
    + import com.urbancode.anthill3.runtime.scripting.*;  
    + import com.urbancode.anthill3.runtime.scripting.helpers.*;  
    + import com.urbancode.commons.util.CollectionUtil;  
    + import com.urbancode.anthill3.domain.status.*;  
    + import com.urbancode.anthill3.domain.agent.*;  
    + import com.urbancode.anthill3.domain.buildlife.*;  
    + return new Where() {  
       
    + public Agent[] filter(Agent[] agents) {  
    + agentList = new LinkedList();  
    + BuildLife bl = BuildLifeLookup.getCurrent();  
    + String environment_name = EnvironmentLookup.getCurrent().getName();  
    + String offline_agents = "offline_agents_";  
    + String isRedeploy = bl.getProperty("agents_offline");  
    +  
    + //Determine if we are re-deploying  
    + if("true".equals(isRedeploy)){  
    + String[] agent_names = PropertyLookup.getValues("Agents_not_deployed_too");  
    + for(int x=0; x<agent_names.length; x++){  
    + Agent name = AgentFactory.getInstance().restoreForName(agent_names[x]);  
    + agentList.add(name);  
    + }//end for  
       
    + } else {  
    + // If we are not redeploying - return all agents  
    + for (int i=0; i<agents.length; i++) {  
    + status = AgentManager.getInstance().getAgentStatus(agents[i]);  
    + if (status != null && status.isOnline()) {  
    + agentList.add(agents[i]);  
    + }//end if  
    + }//end for  
    + }//end else  
    +  
    + bl.setProperty("agents_offline", "false");  
    + return (Agent[]) agentList.toArray(new Agent[agentList.size()]);  
    + }  
    + }</pre>  
       
    + ''Evaluate Script Step'':  
    + <pre>import com.urbancode.anthill3.runtime.scripting.helpers;  
    + import com.urbancode.anthill3.domain.servergroup.*;  
    + import com.urbancode.anthill3.domain.agent.*;  
    + import com.urbancode.anthill3.domain.jobtrace.*;  
    + import com.urbancode.devilfish.client.ServiceEndpoint;  
    + import com.urbancode.anthill3.domain.buildlife.*;  
    + import java.util.*;  
       
    + org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("script");  
       
    + //Get the name of the Environment  
    + String environment_name = EnvironmentLookup.getCurrent().getName();  
       
    + //Get the ServerGroup (environment) obj.  
    + ServerGroup environment = EnvironmentLookup.getCurrent();  
       
    + //Get an array of all the agent endpoints in an env.  
    + ServiceEndpoint[] endpoints = environment.getServerArray();  
       
    + //turn the array into a set of agent endpoints  
    + Set eps = new HashSet();  
    + for(int v = 0; v<endpoints.length; v++){  
    + String id = endpoints[v].getEndpointId();  
    + eps.add(id);  
    + }  
       
       
    + //Determine what jobs ran in the workflow.  
    + JobTrace[] jobTrace = WorkflowLookup.getCurrentCase().getJobTraceArray();  
       
    + //loop through the jobTraces, and find the agent the jobs used. Then determine the agents endpoint ID.  
    + for(int i = 0; i<jobTrace.length; i++){  
    + String epId = jobTrace[i].getAgent().getEndpointId();  
    + //For every agent that was used, remove its endpoint from the list of possible endpoints  
    + eps.remove(epId);  
    + }  
       
    + //the Set eps now only contains agents that were not deployed too. Here we will determine the names of those agents and save it as a string  
       
    + Iterator myIterator = eps.iterator(); //iterate over the set  
    + String list_of_agents = ""; //string of agent names that were not deployed too.  
       
       
    + while(myIterator.hasNext()){  
    + String ep = myIterator.next();  
    + Agent agent = AgentFactory.getInstance().restoreByEndpointId(ep);  
    + String tempName = agent.getName();  
    + list_of_agents = list_of_agents.concat(tempName+", ");  
    + }  
       
    + //trim the string to remove the extra ","  
    + int length = list_of_agents.length();  
    + if(length > 2){  
    + list_of_agents = list_of_agents.substring(0, length-2);  
    + }  
       
    + //set the string as a property on the build life.  
    + BuildLife bl = BuildLifeLookup.getCurrent();  
    + String offline_env = "offline_agents_"+environment_name;  
    + if(!eps.isEmpty()){  
    + bl.setProperty("agents_offline", "true");  
    + bl.setProperty("Agents_not_deployed_too", list_of_agents);  
    + } else {  
    + bl.setProperty("agents_offline", "false");  
    + bl.setProperty("Agents_not_deployed_too", null);  
    + }</pre>