use of com.thoughtworks.go.domain.AllConfigErrors in project gocd by gocd.
the class AgentRegistrationController method agentRequest.
@RequestMapping(value = "/admin/agent", method = RequestMethod.POST)
public ModelAndView agentRequest(@RequestParam("hostname") String hostname, @RequestParam("uuid") String uuid, @RequestParam("location") String location, @RequestParam("usablespace") String usablespaceAsString, @RequestParam("operatingSystem") String operatingSystem, @RequestParam("agentAutoRegisterKey") String agentAutoRegisterKey, @RequestParam("agentAutoRegisterResources") String agentAutoRegisterResources, @RequestParam("agentAutoRegisterEnvironments") String agentAutoRegisterEnvironments, @RequestParam("agentAutoRegisterHostname") String agentAutoRegisterHostname, @RequestParam("elasticAgentId") String elasticAgentId, @RequestParam("elasticPluginId") String elasticPluginId, @RequestParam(value = "supportsBuildCommandProtocol", required = false, defaultValue = "false") boolean supportsBuildCommandProtocol, HttpServletRequest request) throws IOException {
final String ipAddress = request.getRemoteAddr();
if (LOG.isDebugEnabled()) {
LOG.debug("Processing registration request from agent [{}/{}]", hostname, ipAddress);
}
Registration keyEntry;
String preferredHostname = hostname;
try {
if (goConfigService.serverConfig().shouldAutoRegisterAgentWith(agentAutoRegisterKey)) {
preferredHostname = getPreferredHostname(agentAutoRegisterHostname, hostname);
LOG.info("[Agent Auto Registration] Auto registering agent with uuid {} ", uuid);
} else {
if (elasticAgentAutoregistrationInfoPresent(elasticAgentId, elasticPluginId)) {
throw new RuntimeException(String.format("Elastic agent registration requires an auto-register agent key to be setup on the server. Agent-id: [%s], Plugin-id: [%s]", elasticAgentId, elasticPluginId));
}
}
AgentConfig agentConfig = new AgentConfig(uuid, preferredHostname, ipAddress);
if (partialElasticAgentAutoregistrationInfo(elasticAgentId, elasticPluginId)) {
throw new RuntimeException("Elastic agents must submit both elasticAgentId and elasticPluginId");
}
if (elasticAgentAutoregistrationInfoPresent(elasticAgentId, elasticPluginId)) {
agentConfig.setElasticAgentId(elasticAgentId);
agentConfig.setElasticPluginId(elasticPluginId);
}
if (goConfigService.serverConfig().shouldAutoRegisterAgentWith(agentAutoRegisterKey)) {
LOG.info(String.format("[Agent Auto Registration] Auto registering agent with uuid %s ", uuid));
GoConfigDao.CompositeConfigCommand compositeConfigCommand = new GoConfigDao.CompositeConfigCommand(new AgentConfigService.AddAgentCommand(agentConfig), new UpdateResourceCommand(uuid, agentAutoRegisterResources), new UpdateEnvironmentsCommand(uuid, agentAutoRegisterEnvironments));
HttpOperationResult result = new HttpOperationResult();
agentConfig = agentConfigService.updateAgent(compositeConfigCommand, uuid, result, agentService.agentUsername(uuid, ipAddress, preferredHostname));
if (!result.isSuccess()) {
List<ConfigErrors> errors = com.thoughtworks.go.config.ErrorCollector.getAllErrors(agentConfig);
throw new GoConfigInvalidException(null, new AllConfigErrors(errors).asString());
}
}
boolean registeredAlready = goConfigService.hasAgent(uuid);
long usablespace = Long.parseLong(usablespaceAsString);
AgentRuntimeInfo agentRuntimeInfo = AgentRuntimeInfo.fromServer(agentConfig, registeredAlready, location, usablespace, operatingSystem, supportsBuildCommandProtocol);
if (elasticAgentAutoregistrationInfoPresent(elasticAgentId, elasticPluginId)) {
agentRuntimeInfo = ElasticAgentRuntimeInfo.fromServer(agentRuntimeInfo, elasticAgentId, elasticPluginId);
}
keyEntry = agentService.requestRegistration(agentService.agentUsername(uuid, ipAddress, preferredHostname), agentRuntimeInfo);
} catch (Exception e) {
keyEntry = Registration.createNullPrivateKeyEntry();
LOG.error("Error occured during agent registration process: ", e);
}
return render(keyEntry);
}
use of com.thoughtworks.go.domain.AllConfigErrors in project gocd by gocd.
the class UpdateEnvironmentCommandTest method shouldValidateDuplicateEnvironmentVariables.
@Test
public void shouldValidateDuplicateEnvironmentVariables() throws Exception {
newEnvironmentConfig.addEnvironmentVariable("foo", "bar");
newEnvironmentConfig.addEnvironmentVariable("foo", "baz");
UpdateEnvironmentCommand command = new UpdateEnvironmentCommand(goConfigService, oldEnvironmentConfig.name().toString(), newEnvironmentConfig, currentUser, actionFailed, md5, entityHashingService, result);
command.update(cruiseConfig);
assertThat(command.isValid(cruiseConfig), is(false));
HttpLocalizedOperationResult expectResult = new HttpLocalizedOperationResult();
String allErrors = new AllConfigErrors(cruiseConfig.getAllErrors()).asString();
expectResult.unprocessableEntity(actionFailed.addParam(allErrors));
assertThat(result, is(expectResult));
}
use of com.thoughtworks.go.domain.AllConfigErrors in project gocd by gocd.
the class EnvironmentCommand method isValid.
public boolean isValid(CruiseConfig preprocessedConfig) {
EnvironmentConfig config = preprocessedConfig.getEnvironments().find(this.config.name());
boolean isValid = config.validateTree(ConfigSaveValidationContext.forChain(preprocessedConfig), preprocessedConfig);
if (!isValid) {
String allErrors = new AllConfigErrors(preprocessedConfig.getAllErrors()).asString();
result.unprocessableEntity(actionFailed.addParam(allErrors));
}
return isValid;
}
use of com.thoughtworks.go.domain.AllConfigErrors in project gocd by gocd.
the class AddEnvironmentCommandTest method shouldValidateDuplicateEnvironmentVariables.
@Test
public void shouldValidateDuplicateEnvironmentVariables() throws Exception {
environmentConfig.addEnvironmentVariable("foo", "bar");
environmentConfig.addEnvironmentVariable("foo", "baz");
AddEnvironmentCommand command = new AddEnvironmentCommand(goConfigService, environmentConfig, currentUser, actionFailed, result);
command.update(cruiseConfig);
HttpLocalizedOperationResult expectedResult = new HttpLocalizedOperationResult();
assertThat(command.isValid(cruiseConfig), is(false));
String allErrors = new AllConfigErrors(cruiseConfig.getAllErrors()).asString();
expectedResult.unprocessableEntity(actionFailed.addParam(allErrors));
assertThat(result, is(expectedResult));
}
Aggregations