Search in sources :

Example 31 with Path

use of io.fabric8.annotations.Path 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)

Example 32 with Path

use of io.fabric8.annotations.Path in project fabric8 by jboss-fuse.

the class CloudContainerInstallationTask method uploadToNode.

private void uploadToNode(ComputeServiceContext context, NodeMetadata node, LoginCredentials credentials, URL url, String path) {
    Utils utils = context.utils();
    SshClient ssh = credentials != null ? utils.sshForNode().apply(NodeMetadataBuilder.fromNodeMetadata(nodeMetadata).credentials(credentials).build()) : utils.sshForNode().apply(node);
    try (InputStream is = url.openStream()) {
        ssh.connect();
        File distro = Files.createTempFile("/tmp");
        Files.copy(is, new FileOutputStream(distro));
        ssh.put(path, Payloads.newFilePayload(distro));
        distro.delete();
    } catch (IOException e) {
        LOGGER.warn("Failed to upload. Will attempt downloading distribution via maven.");
    } finally {
        if (ssh != null) {
            ssh.disconnect();
        }
    }
}
Also used : SshClient(org.jclouds.ssh.SshClient) Utils(org.jclouds.compute.Utils) ContainerProviderUtils(io.fabric8.internal.ContainerProviderUtils) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 33 with Path

use of io.fabric8.annotations.Path in project fabric8 by jboss-fuse.

the class SshContainerProvider method uploadTo.

protected void uploadTo(Session session, URL url, String path) {
    Channel channel = null;
    try (InputStream is = url.openStream()) {
        channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;
        final CountDownLatch uploadLatch = new CountDownLatch(1);
        sftpChannel.put(is, path, new SftpProgressMonitor() {

            @Override
            public void init(int op, String src, String dest, long max) {
            }

            @Override
            public boolean count(long count) {
                try {
                    return is.available() > 0;
                } catch (IOException e) {
                    return false;
                }
            }

            @Override
            public void end() {
                uploadLatch.countDown();
            }
        }, ChannelSftp.OVERWRITE);
        uploadLatch.await(10, TimeUnit.MINUTES);
    } catch (Exception e) {
        LOGGER.warn("Failed to upload. Will attempt downloading distribution via maven.");
    } finally {
        if (channel != null) {
            channel.disconnect();
        }
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) SftpProgressMonitor(com.jcraft.jsch.SftpProgressMonitor) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) FabricException(io.fabric8.api.FabricException) IOException(java.io.IOException)

Example 34 with Path

use of io.fabric8.annotations.Path in project fabric8 by jboss-fuse.

the class SshContainerProvider method create.

/**
 * Creates an {@link io.fabric8.api.Container} with the given name pointing to the specified zooKeeperUrl.
 */
public CreateSshContainerMetadata create(CreateSshContainerOptions options, CreationStateListener listener) {
    try {
        String path = options.getPath();
        String host = options.getHost();
        String ip = InetAddress.getByName(host).getHostAddress();
        if (host == null) {
            throw new IllegalArgumentException("Host name not specified.");
        }
        int port = options.getPort();
        if (port == -1) {
            port = 22;
        }
        String containerName = options.getName();
        CreateSshContainerMetadata metadata = new CreateSshContainerMetadata();
        metadata.setCreateOptions(options);
        metadata.setContainerName(containerName);
        String script = buildInstallAndStartScript(containerName, options);
        LOGGER.debug("Running script on host {}:\n{}", host, script);
        Session session = null;
        try {
            session = createSession(options);
            if (options.doUploadDistribution()) {
                uploadTo(session, options.getProxyUri().resolve("io/fabric8/fabric8-karaf/" + FabricConstants.FABRIC_VERSION + "/fabric8-karaf-" + FabricConstants.FABRIC_VERSION + ".zip").toURL(), "/tmp/fabric8-karaf-" + FabricConstants.FABRIC_VERSION + ".zip");
            }
            runScriptOnHost(session, script);
        } catch (Throwable ex) {
            metadata.setFailure(ex);
            throw new FabricException(ex);
        } finally {
            if (session != null) {
                session.disconnect();
            }
        }
        return metadata;
    } catch (Exception e) {
        throw FabricException.launderThrowable(e);
    }
}
Also used : FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException) IOException(java.io.IOException) Session(com.jcraft.jsch.Session)

Example 35 with Path

use of io.fabric8.annotations.Path in project fabric8 by jboss-fuse.

the class SshContainerProvider method readFile.

private byte[] readFile(String path) {
    byte[] bytes = null;
    FileInputStream fin = null;
    File file = new File(path);
    if (path != null && file.exists()) {
        try {
            fin = new FileInputStream(file);
            bytes = new byte[(int) file.length()];
            fin.read(bytes);
        } catch (IOException e) {
            LOGGER.warn("Error reading file {}.", path);
        } finally {
            if (fin != null) {
                try {
                    fin.close();
                } catch (Exception ex) {
                }
            }
        }
    }
    return bytes;
}
Also used : IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) FabricException(io.fabric8.api.FabricException) IOException(java.io.IOException)

Aggregations

Test (org.junit.Test)45 File (java.io.File)41 IOException (java.io.IOException)34 ArrayList (java.util.ArrayList)18 PathTestUtil.createTmpFile (io.fabric8.maven.docker.util.PathTestUtil.createTmpFile)17 HashMap (java.util.HashMap)12 ResourceConfig (io.fabric8.maven.core.config.ResourceConfig)11 VolumeConfig (io.fabric8.maven.core.config.VolumeConfig)11 Path (java.nio.file.Path)11 FabricService (io.fabric8.api.FabricService)10 RuntimeProperties (io.fabric8.api.RuntimeProperties)9 Properties (java.util.Properties)9 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)8 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)8 InputStream (java.io.InputStream)8 Path (javax.ws.rs.Path)8 HttpProxyRule (io.fabric8.gateway.model.HttpProxyRule)7 NodeState (io.fabric8.groups.NodeState)7 URISyntaxException (java.net.URISyntaxException)7 List (java.util.List)6