Search in sources :

Example 11 with Agent

use of com.thoughtworks.go.config.Agent in project gocd by gocd.

the class TriStateSelectionTest method shouldHaveActionAddIfAllAgentsHaveThatResource.

@Test
public void shouldHaveActionAddIfAllAgentsHaveThatResource() {
    resourceConfigs.add(new ResourceConfig("all"));
    agents.add(new Agent("uuid1", "host1", "127.0.0.1", singletonList("all")));
    agents.add(new Agent("uuid2", "host2", "127.0.0.2", singletonList("all")));
    List<TriStateSelection> selections = TriStateSelection.forAgentsResources(resourceConfigs, agents);
    assertThat(selections, hasItem(new TriStateSelection("all", TriStateSelection.Action.add)));
}
Also used : Agent(com.thoughtworks.go.config.Agent) ResourceConfig(com.thoughtworks.go.config.ResourceConfig) Test(org.junit.jupiter.api.Test)

Example 12 with Agent

use of com.thoughtworks.go.config.Agent in project gocd by gocd.

the class JobAssignmentIntegrationTest method setupRemoteAgent.

private AgentInstance setupRemoteAgent() {
    Agent agent = AgentMother.remoteAgent();
    agentService.saveOrUpdate(agent);
    AgentInstance instance = AgentInstance.createFromAgent(agent, systemEnvironment, agentStatusChangeListener());
    instance.enable();
    return instance;
}
Also used : AgentInstance(com.thoughtworks.go.domain.AgentInstance) Agent(com.thoughtworks.go.config.Agent)

Example 13 with Agent

use of com.thoughtworks.go.config.Agent in project gocd by gocd.

the class AgentRegistrationController method agentRequest.

@RequestMapping(value = "/admin/agent", method = RequestMethod.POST)
public ResponseEntity agentRequest(@RequestParam("hostname") String hostname, @RequestParam("uuid") String uuid, @RequestParam("location") String location, @RequestParam("usablespace") String usableSpaceStr, @RequestParam("operatingSystem") String os, @RequestParam("agentAutoRegisterKey") String agentAutoRegisterKey, @RequestParam("agentAutoRegisterResources") String agentAutoRegisterResources, @RequestParam("agentAutoRegisterEnvironments") String agentAutoRegisterEnvs, @RequestParam("agentAutoRegisterHostname") String agentAutoRegisterHostname, @RequestParam("elasticAgentId") String elasticAgentId, @RequestParam("elasticPluginId") String elasticPluginId, @RequestParam("token") String token, HttpServletRequest request) {
    final String ipAddress = request.getRemoteAddr();
    LOG.debug("Processing registration request from agent [{}/{}]", hostname, ipAddress);
    boolean keyEntry;
    String preferredHostname = hostname;
    boolean isElasticAgent = elasticAgentAutoregistrationInfoPresent(elasticAgentId, elasticPluginId);
    try {
        if (!encodeBase64String(hmac().doFinal(uuid.getBytes())).equals(token)) {
            String message = "Not a valid token.";
            LOG.error("Rejecting request for registration. Error: HttpCode=[{}] Message=[{}] UUID=[{}] Hostname=[{}]" + "ElasticAgentID=[{}] PluginID=[{}]", FORBIDDEN, message, uuid, hostname, elasticAgentId, elasticPluginId);
            return new ResponseEntity<>(message, FORBIDDEN);
        }
        boolean shouldAutoRegister = shouldAutoRegister(agentAutoRegisterKey, isElasticAgent);
        if (shouldAutoRegister) {
            preferredHostname = getPreferredHostname(agentAutoRegisterHostname, hostname);
        } else {
            if (elasticAgentAutoregistrationInfoPresent(elasticAgentId, elasticPluginId)) {
                String message = String.format("Elastic agent registration requires an auto-register agent key to be" + " setup on the server. The agentAutoRegisterKey: [%s] is either not provided or expired. Agent-id: [%s], Plugin-id: [%s]", agentAutoRegisterKey, elasticAgentId, elasticPluginId);
                LOG.error("Rejecting request for registration. Error: HttpCode=[{}] Message=[{}] UUID=[{}] Hostname=[{}]" + "ElasticAgentID=[{}] PluginID=[{}]", UNPROCESSABLE_ENTITY, message, uuid, hostname, elasticAgentId, elasticPluginId);
                return new ResponseEntity<>(message, UNPROCESSABLE_ENTITY);
            }
        }
        Agent agent = createAgentFromRequest(uuid, preferredHostname, ipAddress, elasticAgentId, elasticPluginId);
        agent.validate();
        if (agent.hasErrors()) {
            List<ConfigErrors> errors = agent.errorsAsList();
            throw new GoConfigInvalidException(null, new AllConfigErrors(errors));
        }
        if (partialElasticAgentAutoregistrationInfo(elasticAgentId, elasticPluginId)) {
            String message = "Elastic agents must submit both elasticAgentId and elasticPluginId.";
            LOG.error("Rejecting request for registration. Error: HttpCode=[{}] Message=[{}] UUID=[{}] Hostname=[{}]" + "ElasticAgentID=[{}] PluginID=[{}]", UNPROCESSABLE_ENTITY, message, uuid, hostname, elasticAgentId, elasticPluginId);
            return new ResponseEntity<>(message, UNPROCESSABLE_ENTITY);
        }
        if (elasticAgentIdAlreadyRegistered(elasticAgentId, elasticPluginId)) {
            String message = "Duplicate Elastic agent Id used to register elastic agent.";
            LOG.error("Rejecting request for registration. Error: HttpCode=[{}] Message=[{}] UUID=[{}] Hostname=[{}]" + "ElasticAgentID=[{}] PluginID=[{}]", UNPROCESSABLE_ENTITY, message, uuid, hostname, elasticAgentId, elasticPluginId);
            return new ResponseEntity<>(message, UNPROCESSABLE_ENTITY);
        }
        if (shouldAutoRegister && !agentService.isRegistered(uuid)) {
            LOG.info("[Agent Auto Registration] Auto registering agent with uuid {} ", uuid);
            agent.setEnvironments(agentAutoRegisterEnvs);
            agent.setResources(agentAutoRegisterResources);
            agentService.register(agent);
            if (agent.hasErrors()) {
                throw new GoConfigInvalidException(null, new AllConfigErrors(agent.errorsAsList()).asString());
            }
        }
        boolean registeredAlready = agentService.isRegistered(uuid);
        long usableSpace = Long.parseLong(usableSpaceStr);
        AgentRuntimeInfo agentRuntimeInfo = AgentRuntimeInfo.fromServer(agent, registeredAlready, location, usableSpace, os);
        if (elasticAgentAutoregistrationInfoPresent(elasticAgentId, elasticPluginId)) {
            agentRuntimeInfo = ElasticAgentRuntimeInfo.fromServer(agentRuntimeInfo, elasticAgentId, elasticPluginId);
        }
        keyEntry = agentService.requestRegistration(agentRuntimeInfo);
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        return new ResponseEntity<>("", httpHeaders, keyEntry ? OK : ACCEPTED);
    } catch (Exception e) {
        LOG.error("Error occurred during agent registration process. Error: HttpCode=[{}] Message=[{}] UUID=[{}] " + "Hostname=[{}] ElasticAgentID=[{}] PluginID=[{}]", UNPROCESSABLE_ENTITY, getErrorMessage(e), uuid, hostname, elasticAgentId, elasticPluginId, e);
        return new ResponseEntity<>(String.format("Error occurred during agent registration process: %s", getErrorMessage(e)), UNPROCESSABLE_ENTITY);
    }
}
Also used : Agent(com.thoughtworks.go.config.Agent) HttpHeaders(org.springframework.http.HttpHeaders) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) GoConfigInvalidException(com.thoughtworks.go.config.exceptions.GoConfigInvalidException) GoConfigInvalidException(com.thoughtworks.go.config.exceptions.GoConfigInvalidException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) ResponseEntity(org.springframework.http.ResponseEntity) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with Agent

use of com.thoughtworks.go.config.Agent in project gocd by gocd.

the class AgentRegistrationController method createAgentFromRequest.

private Agent createAgentFromRequest(String uuid, String hostname, String ip, String elasticAgentId, String elasticPluginId) {
    Agent agent = new Agent(uuid, hostname, ip);
    if (elasticAgentAutoregistrationInfoPresent(elasticAgentId, elasticPluginId)) {
        agent.setElasticAgentId(elasticAgentId);
        agent.setElasticPluginId(elasticPluginId);
    }
    return agent;
}
Also used : Agent(com.thoughtworks.go.config.Agent)

Example 15 with Agent

use of com.thoughtworks.go.config.Agent in project gocd by gocd.

the class JobController method presenter.

private JobDetailPresentationModel presenter(JobInstance current) {
    String pipelineName = current.getIdentifier().getPipelineName();
    String stageName = current.getIdentifier().getStageName();
    JobInstances recent25 = jobInstanceService.latestCompletedJobs(pipelineName, stageName, current.getName());
    Agent agent = agentService.getAgentByUUID(current.getAgentUuid());
    Pipeline pipelineWithOneBuild = pipelineService.wrapBuildDetails(current);
    Tabs customizedTabs = goConfigService.getCustomizedTabs(pipelineWithOneBuild.getName(), pipelineWithOneBuild.getFirstStage().getName(), current.getName());
    TrackingTool trackingTool = goConfigService.pipelineConfigNamed(new CaseInsensitiveString(pipelineWithOneBuild.getName())).trackingTool();
    Stage stage = stageService.getStageByBuild(current);
    return new JobDetailPresentationModel(current, recent25, agent, pipelineWithOneBuild, customizedTabs, trackingTool, artifactService, stage);
}
Also used : Agent(com.thoughtworks.go.config.Agent) Tabs(com.thoughtworks.go.config.Tabs) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) JobDetailPresentationModel(com.thoughtworks.go.server.presentation.models.JobDetailPresentationModel) TrackingTool(com.thoughtworks.go.config.TrackingTool) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString)

Aggregations

Agent (com.thoughtworks.go.config.Agent)100 Test (org.junit.jupiter.api.Test)52 AgentInstance.createFromLiveAgent (com.thoughtworks.go.domain.AgentInstance.createFromLiveAgent)36 AgentInstance (com.thoughtworks.go.domain.AgentInstance)20 AgentStatusChangeListener (com.thoughtworks.go.listener.AgentStatusChangeListener)19 SystemEnvironment (com.thoughtworks.go.util.SystemEnvironment)16 AgentRuntimeInfo (com.thoughtworks.go.server.service.AgentRuntimeInfo)13 NullAgentInstance (com.thoughtworks.go.domain.NullAgentInstance)11 JobInstance (com.thoughtworks.go.domain.JobInstance)8 ResponseEntity (org.springframework.http.ResponseEntity)8 ServerConfig (com.thoughtworks.go.config.ServerConfig)6 AgentIdentifier (com.thoughtworks.go.remote.AgentIdentifier)6 Username (com.thoughtworks.go.server.domain.Username)5 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)5 AgentInstance.createFromAgent (com.thoughtworks.go.domain.AgentInstance.createFromAgent)4 AgentBuildingInfo (com.thoughtworks.go.server.service.AgentBuildingInfo)4 Query (org.hibernate.Query)4 TransactionCallback (org.springframework.transaction.support.TransactionCallback)4 Gson (com.google.gson.Gson)3 ResourceConfig (com.thoughtworks.go.config.ResourceConfig)3