Search in sources :

Example 71 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class Activator method start.

@Override
@SuppressWarnings("unchecked")
public void start(final BundleContext context) throws Exception {
    bundle = context.getBundle();
    systemContext = context.getBundle(0).getBundleContext();
    logServiceTracker = new ServiceTracker(context, "org.osgi.service.log.LogService", null);
    logServiceTracker.open();
    patchManagementService = new GitPatchManagementServiceImpl(context);
    final int targetStartLevel = Integer.parseInt(System.getProperty("org.osgi.framework.startlevel.beginning"));
    sl = context.getBundle(0).adapt(FrameworkStartLevel.class);
    activatedAt = sl.getStartLevel();
    if (!patchManagementService.isEnabled()) {
        log(LogService.LOG_INFO, "\nPatch management is disabled");
        return;
    }
    switch(activatedAt) {
        case PATCH_MANAGEMENT_START_LEVEL:
            // this bundle is configured in etc/startup.properties, we can handle registered tasks
            // like updating critical bundles
            startupVersion = context.getBundle().getVersion();
            // we're started before fileinstall, so we can remove this bundle if it is available in deploy/
            patchManagementService.cleanupDeployDir();
            break;
        default:
            // this bundle was activated from deploy/ directory or osgi:install. But this doesn't mean there's no
            // patch-management bundle configured in etc/startup.properties
            // the point is that when etc/startup.properties already has this bundle, there should be no such bundle in deploy/
            // TODO (there may be newer version however)
            deployVersion = context.getBundle().getVersion();
            break;
    }
    patchManagementService.start();
    patchManagementRegistration = systemContext.registerService(PatchManagement.class, PatchManagement.class.cast(patchManagementService), null);
    backupServiceRegistration = systemContext.registerService(BackupService.class, new FileBackupService(systemContext), null);
    if (startupVersion != null) {
        // we should be at start level 2. let's check if there are any rollup paatches being installed or
        // rolled back - we've got some work to do at this early stage of Karaf
        patchManagementService.checkPendingPatches();
    }
    if (sl.getStartLevel() == targetStartLevel) {
        // dropped to deploy/ when framework was already at target start-level
        patchManagementService.ensurePatchManagementInitialized();
    } else {
        // let's wait for last start level
        startLevelNotificationFrameworkListener = new StartLevelNotificationFrameworkListener(targetStartLevel);
        systemContext.addFrameworkListener(startLevelNotificationFrameworkListener);
    }
}
Also used : BackupService(io.fabric8.patch.management.BackupService) ServiceTracker(org.osgi.util.tracker.ServiceTracker) PatchManagement(io.fabric8.patch.management.PatchManagement) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel)

Example 72 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class FabricServiceImpl method createContainers.

@Override
public CreateContainerMetadata[] createContainers(CreateContainerOptions options, CreationStateListener listener) {
    assertValid();
    try {
        final ContainerProvider provider = getProvider(options.getProviderType());
        if (provider == null) {
            throw new FabricException("Unable to find a container provider supporting '" + options.getProviderType() + "'");
        }
        if (!provider.isValidProvider()) {
            throw new FabricException("The provider '" + options.getProviderType() + "' is not valid in current environment");
        }
        String originalName = options.getName();
        if (originalName == null || originalName.length() == 0) {
            throw new FabricException("A name must be specified when creating containers");
        }
        // Only allow containers with valid names to get created.
        FabricValidations.validateContainerName(originalName);
        if (listener == null) {
            listener = new NullCreationStateListener();
        }
        validateProfileDependencies(options);
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        Map optionsMap = mapper.readValue(mapper.writeValueAsString(options), Map.class);
        String versionId = options.getVersion() != null ? options.getVersion() : dataStore.get().getDefaultVersion();
        Set<String> profileIds = options.getProfiles();
        if (profileIds == null || profileIds.isEmpty()) {
            profileIds = new LinkedHashSet<String>();
            profileIds.add("default");
        }
        optionsMap.put("version", versionId);
        optionsMap.put("profiles", profileIds);
        optionsMap.put("number", 0);
        // assign parent resolver if it's a child container, else assign global resolver policy
        if (bootstrapConfig != null) {
            String configuredGlobalResolver = bootstrapConfig.getGlobalResolver();
            if (!Strings.isNullOrEmpty(configuredGlobalResolver)) {
                optionsMap.put("globalResolver", configuredGlobalResolver);
                if (optionsMap.get("resolver") == null) {
                    if (optionsMap.get("parent") == null) {
                        optionsMap.put("resolver", configuredGlobalResolver);
                    }
                }
            }
        }
        final List<CreateContainerMetadata> metadatas = new CopyOnWriteArrayList<CreateContainerMetadata>();
        int orgNumber = options.getNumber();
        int number = Math.max(orgNumber, 1);
        if (orgNumber > 1) {
            originalName = originalName + "1";
        }
        final CountDownLatch latch = new CountDownLatch(number);
        Set<String> ignoreContainerNames = new HashSet<>();
        Container[] containers = getContainers();
        // check that there is no container with the given name
        for (Container container : containers) {
            if (container.getId().equals(options.getName())) {
                throw new IllegalArgumentException("A container with name " + options.getName() + " already exists.");
            }
        }
        for (int i = 1; i <= number; i++) {
            NameValidator validator = Containers.createNameValidator(containers, ignoreContainerNames);
            final String containerName = Containers.createUniqueContainerName(containers, originalName, validator);
            ignoreContainerNames.add(containerName);
            optionsMap.put("name", containerName);
            // Check if datastore configuration has been specified and fallback to current container settings.
            if (!hasValidDataStoreProperties(optionsMap)) {
                optionsMap.put("dataStoreProperties", profileRegistry.get().getDataStoreProperties());
            }
            Class cl = options.getClass().getClassLoader().loadClass(options.getClass().getName() + "$Builder");
            CreateContainerBasicOptions.Builder builder = (CreateContainerBasicOptions.Builder) mapper.readValue(mapper.writeValueAsString(optionsMap), cl);
            // We always want to pass the obfuscated version of the password to the container provider.
            builder = (CreateContainerBasicOptions.Builder) builder.zookeeperPassword(PasswordEncoder.encode(getZookeeperPassword()));
            final CreateContainerOptions containerOptions = builder.build();
            final CreationStateListener containerListener = listener;
            final FabricService fabricService = this;
            new Thread("Creating container " + containerName) {

                public void run() {
                    try {
                        if (dataStore.get().hasContainer(containerName)) {
                            CreateContainerBasicMetadata metadata = new CreateContainerBasicMetadata();
                            metadata.setContainerName(containerName);
                            metadata.setCreateOptions(containerOptions);
                            metadata.setFailure(new IllegalArgumentException("A container with name " + containerName + " already exists."));
                            metadatas.add(metadata);
                            return;
                        }
                        dataStore.get().createContainerConfig(containerOptions);
                        CreateContainerMetadata metadata = provider.create(containerOptions, containerListener);
                        if (metadata.isSuccess()) {
                            Container parent = containerOptions.getParent() != null ? getContainer(containerOptions.getParent()) : null;
                            // TODO: We need to make sure that this entries are somehow added even to ensemble servers.
                            if (!containerOptions.isEnsembleServer()) {
                                dataStore.get().createContainerConfig(metadata);
                            }
                            ContainerImpl container = new ContainerImpl(parent, metadata.getContainerName(), FabricServiceImpl.this);
                            metadata.setContainer(container);
                            LOGGER.info("The container " + metadata.getContainerName() + " has been successfully created");
                        } else {
                            LOGGER.warn("The creation of the container " + metadata.getContainerName() + " has failed", metadata.getFailure());
                            dataStore.get().deleteContainer(fabricService, containerOptions.getName());
                        }
                        metadatas.add(metadata);
                    } catch (Throwable t) {
                        CreateContainerBasicMetadata metadata = new CreateContainerBasicMetadata();
                        metadata.setContainerName(containerName);
                        metadata.setCreateOptions(containerOptions);
                        metadata.setFailure(t);
                        metadatas.add(metadata);
                        dataStore.get().deleteContainer(fabricService, containerOptions.getName());
                    } finally {
                        latch.countDown();
                    }
                }
            }.start();
        }
        if (!latch.await(30, TimeUnit.MINUTES)) {
            throw new FabricException("Timeout waiting for container creation");
        }
        return metadatas.toArray(new CreateContainerMetadata[metadatas.size()]);
    } catch (Exception e) {
        LOGGER.error("Failed to create containers " + e, e);
        throw FabricException.launderThrowable(e);
    }
}
Also used : ContainerProvider(io.fabric8.api.ContainerProvider) NullCreationStateListener(io.fabric8.api.NullCreationStateListener) NameValidator(io.fabric8.api.NameValidator) ProfileBuilder(io.fabric8.api.ProfileBuilder) FabricException(io.fabric8.api.FabricException) CreateContainerOptions(io.fabric8.api.CreateContainerOptions) Container(io.fabric8.api.Container) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) CountDownLatch(java.util.concurrent.CountDownLatch) CreateContainerBasicMetadata(io.fabric8.api.CreateContainerBasicMetadata) ProfileDependencyException(io.fabric8.api.ProfileDependencyException) EncryptionOperationNotPossibleException(org.jasypt.exceptions.EncryptionOperationNotPossibleException) FabricException(io.fabric8.api.FabricException) IOException(java.io.IOException) NullCreationStateListener(io.fabric8.api.NullCreationStateListener) CreationStateListener(io.fabric8.api.CreationStateListener) FabricService(io.fabric8.api.FabricService) ContainerImpl(io.fabric8.internal.ContainerImpl) CreateContainerBasicOptions(io.fabric8.api.CreateContainerBasicOptions) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 73 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class ZooKeeperClusterBootstrapImpl method create.

@Override
public void create(CreateEnsembleOptions options) {
    assertValid();
    try {
        // Wait for bootstrap to be complete
        ServiceLocator.awaitService(BootstrapComplete.class);
        LOGGER.info("Create fabric with: {}", options);
        stopBundles();
        BundleContext syscontext = bundleContext.getBundle(0).getBundleContext();
        long bootstrapTimeout = options.getBootstrapTimeout();
        RuntimeProperties runtimeProps = runtimeProperties.get();
        BootstrapConfiguration bootConfig = bootstrapConfiguration.get();
        if (options.isClean()) {
            bootConfig = cleanInternal(syscontext, bootConfig, runtimeProps);
        }
        // before we start fabric, register CM listener that'll mark end of work of FabricConfigAdminBridge
        final CountDownLatch fcabLatch = new CountDownLatch(1);
        final ServiceRegistration<ConfigurationListener> registration = bundleContext.registerService(ConfigurationListener.class, new ConfigurationListener() {

            @Override
            public void configurationEvent(ConfigurationEvent event) {
                if (event.getType() == ConfigurationEvent.CM_UPDATED && event.getPid() != null && event.getPid().equals(Constants.CONFIGADMIN_BRIDGE_PID)) {
                    fcabLatch.countDown();
                }
            }
        }, null);
        BootstrapCreateHandler createHandler = new BootstrapCreateHandler(syscontext, bootConfig, runtimeProps);
        createHandler.bootstrapFabric(name, homeDir, options);
        startBundles(options);
        long startTime = System.currentTimeMillis();
        ServiceLocator.awaitService(FabricComplete.class, bootstrapTimeout, TimeUnit.MILLISECONDS);
        // FabricComplete is registered somewhere in the middle of registering CuratorFramework (SCR activates
        // it when CuratorFramework is registered), but CuratorFramework leads to activation of >100 SCR
        // components, so let's wait for new CuratorComplete service - it is registered after registration
        // of CuratorFramework finishes
        ServiceLocator.awaitService(CuratorComplete.class, bootstrapTimeout, TimeUnit.MILLISECONDS);
        CuratorFramework curatorFramework = ServiceLocator.getService(CuratorFramework.class);
        Map<String, String> dataStoreProperties = options.getDataStoreProperties();
        if (ZooKeeperUtils.exists(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL.getPath()) == null)
            ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL.getPath());
        if (dataStoreProperties != null) {
            String remoteUrl = dataStoreProperties.get(Constants.GIT_REMOTE_URL);
            if (remoteUrl != null) {
                ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_URL.getPath());
                ZooKeeperUtils.add(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_URL.getPath(), remoteUrl);
            }
            String remoteUser = dataStoreProperties.get("gitRemoteUser");
            if (remoteUser != null) {
                ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_USER.getPath());
                ZooKeeperUtils.add(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_USER.getPath(), remoteUser);
            }
            String remotePassword = dataStoreProperties.get("gitRemotePassword");
            if (remotePassword != null) {
                ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_PASSWORD.getPath());
                ZooKeeperUtils.add(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_PASSWORD.getPath(), remotePassword);
            }
        }
        // HttpService is registered differently. not as SCR component activation, but after
        // FabricConfigAdminBridge updates (or doesn't touch) org.ops4j.pax.web CM configuration
        // however in fabric, this PID contains URL to jetty configuration in the form "profile:jetty.xml"
        // so we have to have FabricService and ProfileUrlHandler active
        // some ARQ (single container instance) tests failed because tests ended without http service running
        // of course we have to think if all fabric instances need http service
        ServiceLocator.awaitService("org.osgi.service.http.HttpService", bootstrapTimeout, TimeUnit.MILLISECONDS);
        // and last wait - too much synchronization never hurts
        fcabLatch.await(bootstrapTimeout, TimeUnit.MILLISECONDS);
        registration.unregister();
        long timeDiff = System.currentTimeMillis() - startTime;
        createHandler.waitForContainerAlive(name, syscontext, bootstrapTimeout);
        if (options.isWaitForProvision() && options.isAgentEnabled()) {
            long currentTime = System.currentTimeMillis();
            createHandler.waitForSuccessfulDeploymentOf(name, syscontext, bootstrapTimeout - (currentTime - startTime));
        }
    } catch (RuntimeException rte) {
        throw rte;
    } catch (Exception ex) {
        throw new FabricException("Unable to create zookeeper server configuration", ex);
    }
}
Also used : ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) BootstrapConfiguration(io.fabric8.zookeeper.bootstrap.BootstrapConfiguration) FabricException(io.fabric8.api.FabricException) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) FabricException(io.fabric8.api.FabricException) IOException(java.io.IOException) ConfigurationListener(org.osgi.service.cm.ConfigurationListener) CuratorFramework(org.apache.curator.framework.CuratorFramework) RuntimeProperties(io.fabric8.api.RuntimeProperties) BundleContext(org.osgi.framework.BundleContext)

Example 74 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class AutoScaleController method autoScaleProfile.

private void autoScaleProfile(FabricService service, final ContainerAutoScaler autoScaler, FabricRequirements requirements, ProfileRequirements profileRequirement, AutoScaleStatus status) {
    final String profile = profileRequirement.getProfile();
    Integer minimumInstances = profileRequirement.getMinimumInstances();
    Integer maximumInstances = profileRequirement.getMaximumInstances();
    String requirementsVersion = requirements.getVersion();
    final String version = Strings.isNotBlank(requirementsVersion) ? requirementsVersion : service.getDefaultVersionId();
    if (maximumInstances != null || minimumInstances != null) {
        if (maximumInstances != null) {
            List<Container> containers = Containers.aliveAndSuccessfulContainersForProfile(version, profile, service);
            int count = containers.size();
            int delta = count - maximumInstances;
            if (delta > 0) {
                stopContainers(containers, autoScaler, requirements, profileRequirement, status, delta);
            }
        }
        if (minimumInstances != null) {
            // lets check if we need to provision more
            List<Container> containers = Containers.aliveOrPendingContainersForProfile(version, profile, service);
            int count = containers.size();
            int delta = minimumInstances - count;
            try {
                AutoScaleProfileStatus profileStatus = status.profileStatus(profile);
                if (delta < 0) {
                    FabricService fs = this.fabricService.get();
                    if (fs != null) {
                        profileStatus.destroyingContainer();
                        for (int i = delta; i < 0; i++) {
                            while (!containers.isEmpty()) {
                                Container container = containers.remove(0);
                                if (container.getId().startsWith("auto_")) {
                                    fs.destroyContainer(container);
                                    break;
                                }
                            }
                        }
                    }
                } else if (delta > 0) {
                    if (AutoScalers.requirementsSatisfied(service, version, requirements, profileRequirement, status)) {
                        profileStatus.creatingContainer();
                        final AutoScaleRequest command = new AutoScaleRequest(service, version, profile, delta, requirements, profileRequirement, status);
                        new Thread("Creating container for " + command.getProfile()) {

                            @Override
                            public void run() {
                                try {
                                    autoScaler.createContainers(command);
                                } catch (Exception e) {
                                    LOGGER.error("Failed to create container of profile: " + profile + ". Caught: " + e, e);
                                }
                            }
                        }.start();
                    }
                } else {
                    profileStatus.provisioned();
                }
            } catch (Exception e) {
                LOGGER.error("Failed to auto-scale " + profile + ". Caught: " + e, e);
            }
        }
    }
}
Also used : Container(io.fabric8.api.Container) FabricService(io.fabric8.api.FabricService) AutoScaleProfileStatus(io.fabric8.api.AutoScaleProfileStatus) AutoScaleRequest(io.fabric8.api.AutoScaleRequest)

Example 75 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class ContainerResource method start.

/**
 * Start the container
 */
@POST
@Path("start")
public void start() {
    FabricService fabricService = getFabricService();
    Objects.notNull(fabricService, "fabricService");
    fabricService.startContainer(container);
}
Also used : FabricService(io.fabric8.api.FabricService) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

IOException (java.io.IOException)27 Test (org.junit.Test)25 File (java.io.File)18 HashMap (java.util.HashMap)16 Map (java.util.Map)10 Git (org.eclipse.jgit.api.Git)10 ArrayList (java.util.ArrayList)9 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)8 Bundle (org.osgi.framework.Bundle)8 PatchException (io.fabric8.patch.management.PatchException)7 URISyntaxException (java.net.URISyntaxException)7 BundleException (org.osgi.framework.BundleException)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 CuratorFramework (org.apache.curator.framework.CuratorFramework)6 Container (io.fabric8.api.Container)5 CreateContainerMetadata (io.fabric8.api.CreateContainerMetadata)5 FabricService (io.fabric8.api.FabricService)5 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)5 HashSet (java.util.HashSet)5 ObjectId (org.eclipse.jgit.lib.ObjectId)5