Search in sources :

Example 26 with Registry

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"));
}
Also used : Exec(org.bf2.srs.fleetmanager.it.executor.Exec) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ConditionTimeoutException(org.awaitility.core.ConditionTimeoutException) UncheckedIOException(java.io.UncheckedIOException)

Example 27 with Registry

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();
    }
}
Also used : StartDeprovisionRegistryTask(org.bf2.srs.fleetmanager.execution.impl.tasks.deprovision.StartDeprovisionRegistryTask) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) Transactional(javax.transaction.Transactional)

Example 28 with Registry

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);
    }
}
Also used : RegistryNotFoundException(org.bf2.srs.fleetmanager.storage.RegistryNotFoundException) Audited(org.bf2.srs.fleetmanager.common.operation.auditing.Audited) CheckDeletePermissions(org.bf2.srs.fleetmanager.auth.interceptor.CheckDeletePermissions)

Example 29 with Registry

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);
}
Also used : TooManyInstancesException(org.bf2.srs.fleetmanager.spi.common.TooManyInstancesException) EvalInstancesNotAllowedException(org.bf2.srs.fleetmanager.spi.common.EvalInstancesNotAllowedException) ResourceType(org.bf2.srs.fleetmanager.spi.common.model.ResourceType) TooManyEvalInstancesForUserException(org.bf2.srs.fleetmanager.spi.common.TooManyEvalInstancesForUserException) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) RegistryInstanceTypeValueDto(org.bf2.srs.fleetmanager.rest.service.model.RegistryInstanceTypeValueDto) AccountInfo(org.bf2.srs.fleetmanager.spi.common.model.AccountInfo) Audited(org.bf2.srs.fleetmanager.common.operation.auditing.Audited)

Example 30 with Registry

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());
}
Also used : ScheduleRegistryTask(org.bf2.srs.fleetmanager.execution.impl.tasks.ScheduleRegistryTask) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData)

Aggregations

RegistryData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData)13 Transactional (javax.transaction.Transactional)8 Test (org.junit.jupiter.api.Test)7 AccountInfo (org.bf2.srs.fleetmanager.spi.common.model.AccountInfo)6 TenantNotFoundServiceException (org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException)5 RegistryDeploymentData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData)5 Secret (io.fabric8.kubernetes.api.model.Secret)4 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)4 ManagedConnector (org.bf2.cos.fleetshard.api.ManagedConnector)4 TenantManagerClient (io.apicurio.multitenant.client.TenantManagerClient)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Optional (java.util.Optional)3 ApplicationScoped (javax.enterprise.context.ApplicationScoped)3 Inject (javax.inject.Inject)3 ConnectorDeployment (org.bf2.cos.fleet.manager.model.ConnectorDeployment)3 FleetShardSyncConfig (org.bf2.cos.fleetshard.sync.FleetShardSyncConfig)3 FleetManagerClient (org.bf2.cos.fleetshard.sync.client.FleetManagerClient)3 FleetShardClient (org.bf2.cos.fleetshard.sync.client.FleetShardClient)3