Search in sources :

Example 1 with FabricException

use of io.fabric8.api.FabricException in project fabric8 by jboss-fuse.

the class EncryptedPropertyResolver method initialize.

/**
 * When {@link FabricService} becomes available, we can initialize this {@link PlaceholderResolver}
 * @param fabricService
 */
public void initialize(FabricService fabricService) {
    this.fabricService = fabricService;
    encryptor = getEncryptor(fabricService);
    if (bundleContext != null) {
        seRegistration = bundleContext.registerService(PBEStringEncryptor.class, encryptor, null);
        BundleContext context = FrameworkUtil.getBundle(PersistenceManager.class).getBundleContext();
        encryptingPersistenceManager = new EncryptingPersistenceManager(context, context.getProperty(ConfigurationManager.CM_CONFIG_DIR), encryptor);
        originalPersistenceManager = inject(configAdmin, encryptingPersistenceManager);
        passwordNodeCache = new NodeCacheExtended(fabricService.adapt(CuratorFramework.class), AUTHENTICATION_CRYPT_PASSWORD.getPath());
        passwordNodeCache.getListenable().addListener(this);
        alogrithmNodeCache = new NodeCacheExtended(fabricService.adapt(CuratorFramework.class), AUTHENTICATION_CRYPT_ALGORITHM.getPath());
        alogrithmNodeCache.getListenable().addListener(this);
        try {
            passwordNodeCache.start();
            alogrithmNodeCache.start();
        } catch (Exception e) {
            throw new FabricException(e);
        }
    }
}
Also used : NodeCacheExtended(org.apache.curator.framework.recipes.cache.NodeCacheExtended) EncryptingPersistenceManager(org.apache.felix.cm.file.EncryptingPersistenceManager) PersistenceManager(org.apache.felix.cm.PersistenceManager) FabricException(io.fabric8.api.FabricException) PBEStringEncryptor(org.jasypt.encryption.pbe.PBEStringEncryptor) StandardPBEStringEncryptor(org.jasypt.encryption.pbe.StandardPBEStringEncryptor) EncryptingPersistenceManager(org.apache.felix.cm.file.EncryptingPersistenceManager) FabricException(io.fabric8.api.FabricException) IOException(java.io.IOException) EncryptionOperationNotPossibleException(org.jasypt.exceptions.EncryptionOperationNotPossibleException) BundleContext(org.osgi.framework.BundleContext)

Example 2 with FabricException

use of io.fabric8.api.FabricException in project fabric8 by jboss-fuse.

the class ZookeeperPortService method registerPort.

private void registerPort(Container container, String pid, String key, int port, Lease existingLease) {
    assertValid();
    String portAsString = String.valueOf(port);
    String containerPortsPath = ZkPath.PORTS_CONTAINER_PID_KEY.getPath(container.getId(), pid, key);
    String reservedPortsPath = ZkPath.PORTS_CONTAINER_RESERVED_PORTS.getPath(container.getId());
    String ip = container.getIp();
    assertValidIp(container, ip);
    String ipPortsPath = ZkPath.PORTS_IP.getPath(ip);
    Lease lease = null;
    try {
        if (existingLease != null) {
            lease = existingLease;
        } else {
            lease = interProcessLock.acquire(60, TimeUnit.SECONDS);
        }
        if (lease != null) {
            createDefault(curator.get(), containerPortsPath, portAsString);
            createDefault(curator.get(), ipPortsPath, portAsString);
            setData(curator.get(), containerPortsPath, portAsString);
            String existingPorts = getStringData(curator.get(), ipPortsPath);
            if (!existingPorts.contains(portAsString)) {
                setData(curator.get(), ipPortsPath, existingPorts + " " + portAsString);
                createDefault(curator.get(), reservedPortsPath, portAsString);
                String reservedPortsPerContainer = getStringData(curator.get(), reservedPortsPath);
                if (!reservedPortsPerContainer.contains(portAsString)) {
                    setData(curator.get(), reservedPortsPath, reservedPortsPerContainer + " " + portAsString);
                }
            }
        } else {
            throw new FabricException("Could not acquire port lock for pid " + pid);
        }
    } catch (InterruptedException ex) {
        cleanUpDirtyZKNodes(interProcessLock);
        throw FabricException.launderThrowable(ex);
    } catch (Exception ex) {
        throw FabricException.launderThrowable(ex);
    } finally {
        if (existingLease == null) {
            releaseLock(lease);
        }
    }
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException)

Example 3 with FabricException

use of io.fabric8.api.FabricException in project fabric8 by jboss-fuse.

the class ZookeeperPortService method registerPort.

@Override
public int registerPort(Container container, String pid, String key, int fromPort, int toPort, Set<Integer> excludes) {
    assertValid();
    Lease lease = null;
    try {
        lease = interProcessLock.acquire(60, TimeUnit.SECONDS);
        if (lease != null) {
            int port = lookupPort(container, pid, key);
            if (port > 0 && (port >= fromPort && port <= toPort)) {
                // get one from the port range
                return port;
            }
            Set<Integer> boundPorts = findUsedPortByHost(container, lease);
            boundPorts.addAll(excludes);
            for (port = fromPort; port <= toPort; port++) {
                if (!boundPorts.contains(port)) {
                    if (Ports.isPortFree(port)) {
                        registerPort(container, pid, key, port, lease);
                        return port;
                    }
                }
            }
        } else {
            throw new FabricException("Could not acquire port lock for pid " + pid);
        }
        throw new FabricException("Could not find port within range [" + fromPort + "," + toPort + "] for pid " + pid);
    } catch (InterruptedException ex) {
        cleanUpDirtyZKNodes(interProcessLock);
        throw FabricException.launderThrowable(ex);
    } catch (Exception ex) {
        throw FabricException.launderThrowable(ex);
    } finally {
        releaseLock(lease);
    }
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException)

Example 4 with FabricException

use of io.fabric8.api.FabricException in project fabric8 by jboss-fuse.

the class ZookeeperPortService method findUsedPortByHost.

private Set<Integer> findUsedPortByHost(Container container, Lease existingLease) {
    assertValid();
    String ip = container.getIp();
    assertValidIp(container, ip);
    Set<Integer> ports = new HashSet<Integer>();
    String path = ZkPath.PORTS_IP.getPath(ip);
    Lease lease = null;
    try {
        if (existingLease != null) {
            lease = existingLease;
        } else {
            lease = interProcessLock.acquire(60, TimeUnit.SECONDS);
        }
        if (lease != null) {
            createDefault(curator.get(), path, "");
            String boundPorts = getStringData(curator.get(), path);
            if (boundPorts != null && !boundPorts.isEmpty()) {
                for (String port : boundPorts.split(" ")) {
                    try {
                        ports.add(Integer.parseInt(port.trim()));
                    } catch (NumberFormatException ex) {
                    // ignore
                    }
                }
            }
        } else {
            throw new FabricException("Could not acquire port lock");
        }
    } catch (InterruptedException ex) {
        cleanUpDirtyZKNodes(interProcessLock);
        throw FabricException.launderThrowable(ex);
    } catch (Exception ex) {
        throw FabricException.launderThrowable(ex);
    } finally {
        if (existingLease == null) {
            releaseLock(lease);
        }
    }
    return ports;
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException) HashSet(java.util.HashSet)

Example 5 with FabricException

use of io.fabric8.api.FabricException in project fabric8 by jboss-fuse.

the class ZookeeperPortService method findUsedPortByContainer.

@Override
public Set<Integer> findUsedPortByContainer(Container container) {
    assertValid();
    Set<Integer> ports = new HashSet<Integer>();
    String path = ZkPath.PORTS_CONTAINER.getPath(container.getId());
    Lease lease = null;
    try {
        lease = interProcessLock.acquire(60, TimeUnit.SECONDS);
        if (lease != null) {
            if (exists(curator.get(), path) != null) {
                for (String pid : getChildren(curator.get(), path)) {
                    for (String key : getChildren(curator.get(), ZkPath.PORTS_CONTAINER_PID.getPath(container.getId(), pid))) {
                        String port = getStringData(curator.get(), ZkPath.PORTS_CONTAINER_PID_KEY.getPath(container.getId(), pid, key));
                        try {
                            ports.add(Integer.parseInt(port));
                        } catch (Exception ex) {
                        // ignore
                        }
                    }
                }
            }
        } else {
            throw new FabricException("Could not acquire port lock");
        }
    } catch (InterruptedException ex) {
        cleanUpDirtyZKNodes(interProcessLock);
        throw FabricException.launderThrowable(ex);
    } catch (Exception ex) {
        throw FabricException.launderThrowable(ex);
    } finally {
        releaseLock(lease);
    }
    return ports;
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException) HashSet(java.util.HashSet)

Aggregations

FabricException (io.fabric8.api.FabricException)29 IOException (java.io.IOException)10 Container (io.fabric8.api.Container)7 CreateContainerMetadata (io.fabric8.api.CreateContainerMetadata)6 Lease (org.apache.curator.framework.recipes.locks.Lease)6 HashSet (java.util.HashSet)5 EncryptionOperationNotPossibleException (org.jasypt.exceptions.EncryptionOperationNotPossibleException)5 Session (com.jcraft.jsch.Session)4 FabricService (io.fabric8.api.FabricService)3 Profile (io.fabric8.api.Profile)3 ProfileDependencyException (io.fabric8.api.ProfileDependencyException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 SortedMap (java.util.SortedMap)3 TreeMap (java.util.TreeMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ContainerProvider (io.fabric8.api.ContainerProvider)2 ProfileBuilder (io.fabric8.api.ProfileBuilder)2