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;
}
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();
}
}
}
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();
}
}
}
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);
}
}
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;
}
Aggregations