use of org.bf2.srs.fleetmanager.rest.publicapi.beans.Registry in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class TestInfraManager method runTenantManager.
// TODO replace tenant manager with mock?
private void runTenantManager(boolean authEnabled) throws IOException {
Map<String, String> appEnv = new HashMap<>();
if (authEnabled) {
appEnv.put("AUTH_ENABLED", "true");
appEnv.put("KEYCLOAK_URL", getMandatoryEnvVar(MAS_SSO_URL));
appEnv.put("KEYCLOAK_REALM", getMandatoryEnvVar(MAS_SSO_REALM));
appEnv.put("KEYCLOAK_API_CLIENT_ID", getMandatoryEnvVar(MAS_SSO_CLIENT_ID));
}
String datasourceUrl = deployPostgresql("tenant-manager");
appEnv.put("DATASOURCE_URL", datasourceUrl);
appEnv.put("DATASOURCE_USERNAME", "postgres");
appEnv.put("DATASOURCE_PASSWORD", "postgres");
// registry is not deployed in purpose, it may still work
appEnv.put("REGISTRY_ROUTE_URL", "http://localhost:3888");
appEnv.put("LOG_LEVEL", "DEBUG");
String path = getTenantManagerJarPath();
LOGGER.info("Starting Tenant Manager app from: {}", path);
Exec executor = new Exec();
CompletableFuture.supplyAsync(() -> {
try {
List<String> cmd = new ArrayList<>();
cmd.add("java");
cmd.addAll(Arrays.asList("-jar", path));
int timeout = executor.execute(cmd, appEnv);
return timeout == 0;
} catch (Exception e) {
LOGGER.error("Failed to start tenant manager (could not find runner JAR).", e);
System.exit(1);
return false;
}
}, runnable -> new Thread(runnable).start());
processes.add(new TestInfraProcess() {
@Override
public String getName() {
return "tenant-manager";
}
@Override
public void close() throws Exception {
executor.stop();
}
@Override
public String getStdOut() {
return executor.stdOut();
}
@Override
public String getStdErr() {
return executor.stdErr();
}
@Override
public boolean isContainer() {
return false;
}
});
Awaitility.await("Tenant Manager is reachable").atMost(45, TimeUnit.SECONDS).pollInterval(1, TimeUnit.SECONDS).until(() -> HttpUtils.isReachable("localhost", 8585, "Tenant Manager"));
Awaitility.await("Tenant Manager is ready").atMost(45, TimeUnit.SECONDS).pollInterval(1, TimeUnit.SECONDS).until(() -> HttpUtils.isReady(this.tenantManagerUrl, "/q/health/ready", false, "Tenant Manager"));
}
use of org.bf2.srs.fleetmanager.rest.publicapi.beans.Registry in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class StartDeprovisionRegistryWorker method execute.
@Transactional
@Override
public void execute(Task aTask, WorkerContext ctl) throws RegistryStorageConflictException {
StartDeprovisionRegistryTask task = (StartDeprovisionRegistryTask) aTask;
Optional<RegistryData> registryOptional = storage.getRegistryById(task.getRegistryId());
if (registryOptional.isPresent()) {
// FAILURE POINT 1
var registry = registryOptional.get();
var force = Instant.now().isAfter(registry.getCreatedAt().plus(props.getDeprovisionStuckInstanceTimeout()));
if (force) {
log.warn("Registry instance '{}' is forced to be deprovisioned.", registry);
}
var deprovision = force;
var status = RegistryStatusValueDto.of(registry.getStatus());
switch(status) {
case ACCEPTED:
case PROVISIONING:
if (!force) {
log.debug("Provisioning in progress. Retrying.");
ctl.retry();
}
break;
case READY:
case FAILED:
deprovision = true;
break;
case REQUESTED_DEPROVISIONING:
case DEPROVISIONING_DELETING:
if (!force) {
log.debug("Deprovisioning is already in progress. Stopping. Registry = {}", registry);
ctl.stop();
}
break;
default:
throw new IllegalStateException("Unexpected status value: " + status);
}
if (deprovision) {
registry.setStatus(RegistryStatusValueDto.DEPROVISIONING_DELETING.value());
// FAILURE POINT 2
storage.createOrUpdateRegistry(registry);
ctl.delay(() -> tasks.submit(DeprovisionRegistryTask.builder().registryId(registry.getId()).build()));
}
} else {
log.warn("Registry id='{}' not found. Stopping.", task.getRegistryId());
ctl.stop();
}
}
use of org.bf2.srs.fleetmanager.rest.publicapi.beans.Registry in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class RegistryServiceImpl method deleteRegistry.
@Override
@Audited(extractParameters = { "0", KEY_REGISTRY_ID })
@CheckDeletePermissions
public void deleteRegistry(String registryId) throws RegistryNotFoundException, RegistryStorageConflictException {
try {
// Verify preconditions - Registry exists
storage.getRegistryById(registryId).orElseThrow(() -> new RegistryNotFoundException(registryId));
tasks.submit(StartDeprovisionRegistryTask.builder().registryId(registryId).build());
} catch (NumberFormatException ex) {
throw new RegistryNotFoundException(registryId);
}
}
use of org.bf2.srs.fleetmanager.rest.publicapi.beans.Registry in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class RegistryServiceImpl method createRegistry.
@Audited
@Override
public RegistryDto createRegistry(RegistryCreateDto registryCreate) throws RegistryStorageConflictException, TermsRequiredException, ResourceLimitReachedException, EvalInstancesNotAllowedException, TooManyEvalInstancesForUserException, TooManyInstancesException, AccountManagementServiceException {
final AccountInfo accountInfo = authService.extractAccountInfo();
// Make sure we have more instances available (max capacity not yet reached).
long instanceCount = storage.getRegistryCountTotal();
if (instanceCount >= maxInstances) {
throw new TooManyInstancesException();
}
// Figure out if we are going to create a standard or eval instance.
ResourceType resourceType = evalInstancesOnlyEnabled ? ResourceType.REGISTRY_INSTANCE_EVAL : accountManagementService.determineAllowedResourceType(accountInfo);
if (resourceType == ResourceType.REGISTRY_INSTANCE_EVAL) {
// Are eval instances allowed?
if (!evalInstancesEnabled) {
throw new EvalInstancesNotAllowedException();
}
// Limit the # of eval instances per user. Need to check storage for list of eval registry instances.
List<RegistryData> registriesByOwner = storage.getRegistriesByOwner(accountInfo.getAccountUsername());
int evalInstanceCount = 0;
for (RegistryData registryData : registriesByOwner) {
// TODO Perform a dedicated query
if (RegistryInstanceTypeValueDto.EVAL.value().equals(registryData.getInstanceType())) {
evalInstanceCount++;
}
}
if (evalInstanceCount >= maxEvalInstancesPerUser) {
throw new TooManyEvalInstancesForUserException();
}
}
// Try to consume some quota from AMS for the appropriate resource type (standard or eval). If successful
// we'll get back a subscriptionId - if not we'll throw an exception.
String subscriptionId = accountManagementService.createResource(accountInfo, resourceType);
// Convert to registry data and persist it in the DB.
RegistryInstanceTypeValueDto instanceType = resourceTypeToInstanceType(resourceType);
RegistryData registryData = convertRegistry.convert(registryCreate, subscriptionId, accountInfo.getAccountUsername(), accountInfo.getOrganizationId(), accountInfo.getAccountId(), instanceType);
// Generate the ID
registryData.setId(UUID.randomUUID().toString());
storage.createOrUpdateRegistry(registryData);
tasks.submit(ScheduleRegistryTask.builder().registryId(registryData.getId()).build());
return convertRegistry.convert(registryData);
}
use of org.bf2.srs.fleetmanager.rest.publicapi.beans.Registry in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class ScheduleRegistryWorker method finallyExecute.
@Override
public void finallyExecute(Task aTask, WorkerContext ctl, Optional<Exception> error) throws RegistryNotFoundException, RegistryStorageConflictException {
ScheduleRegistryTask task = (ScheduleRegistryTask) aTask;
// SUCCESS STATE
Optional<RegistryData> registryOpt = storage.getRegistryById(task.getRegistryId());
if (registryOpt.isPresent() && registryOpt.get().getRegistryDeployment() != null)
return;
// The only thing to handle is if we were able to schedule but storage does not work
// In that case, the only thing to do is to just try deleting the registry.
storage.deleteRegistry(task.getRegistryId());
}
Aggregations