use of io.fabric8.service.ssh.CreateSshContainerOptions in project fabric8 by jboss-fuse.
the class SshContainerProvider method start.
@Override
public void start(Container container) {
CreateContainerMetadata metadata = container.getMetadata();
if (!(metadata instanceof CreateSshContainerMetadata)) {
throw new IllegalStateException("Container doesn't have valid create container metadata type");
} else {
CreateSshContainerMetadata sshContainerMetadata = (CreateSshContainerMetadata) metadata;
CreateSshContainerOptions options = sshContainerMetadata.getCreateOptions();
Session session = null;
try {
String script = buildStartScript(container.getId(), options);
session = createSession(options);
runScriptOnHost(session, script);
} catch (Throwable t) {
LOGGER.error("Failed to start container: " + container.getId(), t);
throw new FabricException(t);
} finally {
if (session != null) {
session.disconnect();
}
}
}
}
use of io.fabric8.service.ssh.CreateSshContainerOptions in project fabric8 by jboss-fuse.
the class SshContainerProvider method createSession.
protected Session createSession(CreateSshContainerOptions options) throws Exception {
Session session = null;
Exception connectException = null;
for (int i = 0; i <= options.getSshRetries(); i++) {
if (i > 0) {
long delayMs = (long) (200L * Math.pow(i, 2));
Thread.sleep(delayMs);
}
try {
JSch jsch = new JSch();
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
byte[] privateKey = readFile(options.getPrivateKeyFile());
byte[] passPhrase = options.getPassPhrase() != null ? options.getPassPhrase().getBytes() : null;
if (privateKey != null && options.getPassword() == null) {
jsch.addIdentity(options.getUsername(), privateKey, null, passPhrase);
session = jsch.getSession(options.getUsername(), options.getHost(), options.getPort());
config.put("PreferredAuthentications", "publickey");
} else {
session = jsch.getSession(options.getUsername(), options.getHost(), options.getPort());
session.setPassword(options.getPassword());
config.put("PreferredAuthentications", "password,keyboard-interactive");
}
session.setTimeout(60000);
session.setConfig(config);
session.connect();
connectException = null;
break;
} catch (Exception from) {
connectException = from;
if (session != null && session.isConnected()) {
session.disconnect();
}
session = null;
}
}
if (connectException != null) {
throw connectException;
}
return session;
}
use of io.fabric8.service.ssh.CreateSshContainerOptions in project fabric8 by jboss-fuse.
the class SshContainerProvider method destroy.
@Override
public void destroy(Container container) {
CreateContainerMetadata metadata = container.getMetadata();
if (!(metadata instanceof CreateSshContainerMetadata)) {
throw new IllegalStateException("Container doesn't have valid create container metadata type");
} else {
CreateSshContainerMetadata sshContainerMetadata = (CreateSshContainerMetadata) metadata;
CreateSshContainerOptions options = sshContainerMetadata.getCreateOptions();
Session session = null;
String prevProvisionResult = container.getProvisionResult();
try {
String script = buildUninstallScript(container.getId(), options);
session = createSession(options);
container.setProvisionResult(Container.PROVISION_DELETING);
runScriptOnHost(session, script);
} catch (Throwable t) {
LOGGER.error("Failed to stop container: " + container.getId(), t);
throw new FabricException(t);
} finally {
if (session != null) {
session.disconnect();
}
}
}
}
use of io.fabric8.service.ssh.CreateSshContainerOptions 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.service.ssh.CreateSshContainerOptions in project fabric8 by jboss-fuse.
the class SshContainerProvider method stop.
@Override
public void stop(Container container) {
CreateContainerMetadata metadata = container.getMetadata();
if (!(metadata instanceof CreateSshContainerMetadata)) {
throw new IllegalStateException("Container doesn't have valid create container metadata type");
} else {
CreateSshContainerMetadata sshContainerMetadata = (CreateSshContainerMetadata) metadata;
CreateSshContainerOptions options = sshContainerMetadata.getCreateOptions();
Session session = null;
try {
String script = buildStopScript(container.getId(), options);
session = createSession(options);
container.setProvisionResult(Container.PROVISION_STOPPING);
runScriptOnHost(session, script);
container.setProvisionResult(Container.PROVISION_STOPPED);
} catch (Throwable t) {
LOGGER.error("Failed to stop container: " + container.getId(), t);
throw new FabricException(t);
} finally {
if (session != null) {
session.disconnect();
}
}
}
}
Aggregations