use of com.jcraft.jsch.Channel in project coprhd-controller by CoprHD.
the class CinderCommunicationInterface method scan.
/**
* Process the cinder.conf file and deduce storage systems from it
*/
@Override
public void scan(AccessProfile accessProfile) throws BaseCollectionException {
_logger.info("Scanning started for provider: {}", accessProfile.getSystemId());
StorageProvider storageProvider = _dbClient.queryObject(StorageProvider.class, accessProfile.getSystemId());
String username = storageProvider.getUserName();
String password = storageProvider.getPassword();
String hostName = storageProvider.getIPAddress();
// map to hold cinder end point info
StringMap providerKeys = storageProvider.getKeys();
if (providerKeys == null) {
providerKeys = new StringMap();
}
updateKeyInProvider(providerKeys, CinderConstants.KEY_CINDER_HOST_NAME, hostName);
Integer portNumber = storageProvider.getPortNumber();
ArrayList<Section> sections = new ArrayList<Section>();
String volume_driver = "";
String auth_strategy = "unknown";
ChannelSftp sftp = null;
Session session = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(username, hostName, portNumber);
session.setPassword(password);
Hashtable<String, String> config = new Hashtable<String, String>();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(timeout);
_logger.debug("Session Connected...");
Channel channel = session.openChannel("sftp");
sftp = (ChannelSftp) channel;
InputStream ins;
sftp.connect(connectTimeout);
if (sftp.isConnected()) {
_logger.debug("SFTP Connected");
ins = sftp.get(CONFFILE);
BufferedReader b = new BufferedReader(new InputStreamReader(ins));
int next_section_index = 0;
String section_title = "";
Boolean auth_section = false;
while (!sftp.isEOF()) {
String line = b.readLine();
if (line == null) {
_logger.debug("End of buffer -- break");
break;
}
if (isComment(line)) {
// skip comments
continue;
}
if (isSection(line)) {
// start new section
section_title = line.substring(line.indexOf('[') + 1, line.indexOf(']'));
Section section = new Section();
section.index = next_section_index;
section.title = section_title;
sections.add(section);
next_section_index++;
_logger.debug("Section {}: Title: {}", section.index, section.title);
auth_section = section_title.startsWith(auth_strategy);
continue;
}
if (!line.contains("=")) {
// not a value-parameter pair
continue;
}
// Now process each line
String[] splits = line.split("=");
if (splits.length == 2) {
String parameter = splits[0].trim();
String value = splits[1].trim();
if (auth_section) {
if (parameter.equalsIgnoreCase("admin_user") || parameter.equalsIgnoreCase("username")) {
updateKeyInProvider(providerKeys, CinderConstants.KEY_CINDER_REST_USER, value);
_logger.debug("REST user name = {}", value);
} else if (parameter.equalsIgnoreCase("admin_password") || parameter.equalsIgnoreCase("password")) {
updateKeyInProvider(providerKeys, CinderConstants.KEY_CINDER_REST_PASSWORD, value);
_logger.debug("REST password = {}", value);
} else if (parameter.equalsIgnoreCase("admin_tenant_name") || parameter.equalsIgnoreCase("project_name")) {
updateKeyInProvider(providerKeys, CinderConstants.KEY_CINDER_TENANT_NAME, value);
_logger.debug("Tenant name = {}", value);
} else if (parameter.equalsIgnoreCase("auth_uri")) {
updateKeyInProvider(providerKeys, CinderConstants.KEY_CINDER_REST_URI_BASE, value);
_logger.info("REST uri = {}", value);
}
} else {
// this is a storage section
_logger.debug("Storage section: parameter = {}, value = {}", parameter, value);
if (parameter.equalsIgnoreCase("auth_strategy")) {
auth_strategy = value.trim();
_logger.info("Auth strategy = {}", auth_strategy);
} else if (parameter.equalsIgnoreCase("volume_driver")) {
volume_driver = value.trim();
sections.get(next_section_index - 1).volume_driver = volume_driver;
_logger.debug("Volume driver = {}", volume_driver);
} else if (parameter.equalsIgnoreCase("volume_backend_name")) {
String volume_backend_name = value.trim();
_logger.debug("Volume backend_name = {}", volume_backend_name);
sections.get(next_section_index - 1).volume_backend_name = volume_backend_name;
}
}
}
/* if splits.length */
}
/* while not EOF */
b.close();
}
/* if sftp is connected */
storageProvider.setConnectionStatus(ConnectionStatus.CONNECTED.name());
}/* try */
catch (Exception e) {
// exceptionOccured = true;
storageProvider.setConnectionStatus(ConnectionStatus.NOTCONNECTED.name());
_logger.error("Exception occurred while scanning provider {}", accessProfile.getSystemId(), e);
} finally {
fillStorageSystemCache(accessProfile, sections);
if (storageProvider.getKeys() == null) {
/* first time scan */
storageProvider.setKeys(providerKeys);
}
_dbClient.persistObject(storageProvider);
if (sftp != null) {
sftp.disconnect();
}
if (session != null) {
session.disconnect();
}
}
_logger.info("Scanning ended for provider: {}", accessProfile.getSystemId());
}
use of com.jcraft.jsch.Channel in project coprhd-controller by CoprHD.
the class VNXFileSshApi method executeSsh.
/**
* Executes a command on the VNX File CLI.
*
* @param command command to execute on the VNX File CLI.
* @param request payload for the command
* @return result of executing the command.
*/
public XMLApiResult executeSsh(String command, String request) {
XMLApiResult result = new XMLApiResult();
if ((_host == null) || (_userName == null) || (_password == null)) {
_log.error("Invalid connection parameter");
result.setCommandFailed();
return result;
}
String cmd = "export NAS_DB=/nas;" + command + " " + request;
_log.info("executeSsh: cmd: " + cmd);
InputStream in = null;
Session session = null;
Channel channel = null;
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
session = jsch.getSession(_userName, _host, DEFAULT_PORT);
session.setPassword(_password);
session.setConfig(config);
session.connect();
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(cmd);
channel.setInputStream(null);
in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[BUFFER_SIZE];
StringBuilder cmdResults = new StringBuilder();
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, BUFFER_SIZE);
if (i < 0) {
break;
}
cmdResults.append(new String(tmp, 0, i));
}
if (channel.isClosed()) {
_log.info("Ssh exit status: " + channel.getExitStatus());
result.setMessage(cmdResults.toString());
// Set the command result status.
if (channel.getExitStatus() == 0) {
StringTokenizer st = new StringTokenizer(cmdResults.toString());
if (st.hasMoreTokens()) {
// data mover name
st.nextToken();
}
if (!command.equalsIgnoreCase(SERVER_USER_CMD)) {
if (st.hasMoreTokens()) {
st.nextToken();
}
}
String res = "";
if (st.hasMoreTokens()) {
// contains status or result.
res = st.nextToken();
}
if (res.equalsIgnoreCase("done")) {
result.setCommandSuccess();
} else if (res.equalsIgnoreCase("error")) {
result.setCommandFailed();
} else {
result.setCommandSuccess();
}
} else {
result.setCommandFailed();
}
break;
}
try {
Thread.sleep(_respDelay);
} catch (InterruptedException e) {
_log.error("VNX File executeSsh Communication thread interrupted for command: " + cmd, e);
}
}
_log.info("executeSsh: Done");
} catch (Exception e) {
_log.error("VNX File executeSsh connection failed while attempting to execute: " + cmd, e);
result.setCommandFailed();
result.setMessage(e.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {
_log.error("Exception occured while closing input stream due to ", ignored);
}
}
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
return result;
}
use of com.jcraft.jsch.Channel in project airavata by apache.
the class ClusterStatusMonitorJob method execute.
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
try {
String superTenantGatewayId = ServerSettings.getSuperTenantGatewayId();
RegistryService.Client registryClient = getRegistryClient();
List<ComputeResourceProfile> computeResourceProfiles = new ArrayList<>();
List<ComputeResourcePreference> computeResourcePreferences = null;
try {
computeResourcePreferences = registryClient.getAllGatewayComputeResourcePreferences(superTenantGatewayId);
} catch (Exception ex) {
logger.warn("Could not find super tenant compute resources preferences for cluster status monitoring...");
}
if (computeResourcePreferences != null && computeResourcePreferences.size() > 0) {
computeResourcePreferences.stream().forEach(p -> {
try {
String computeResourceId = p.getComputeResourceId();
String credentialStoreToken = p.getResourceSpecificCredentialStoreToken();
String loginUserName = p.getLoginUserName();
String hostName = null;
if (credentialStoreToken == null || credentialStoreToken.equals("")) {
credentialStoreToken = registryClient.getGatewayResourceProfile(superTenantGatewayId).getCredentialStoreToken();
}
int port = -1;
ArrayList queueNames = new ArrayList<>();
ComputeResourceDescription computeResourceDescription = registryClient.getComputeResource(computeResourceId);
hostName = computeResourceDescription.getHostName();
// FIXME This should come from compute resource description
port = 22;
computeResourceDescription.getBatchQueues().stream().forEach(q -> {
queueNames.add(q.getQueueName());
});
List<JobSubmissionInterface> jobSubmissionInterfaces = computeResourceDescription.getJobSubmissionInterfaces();
if (jobSubmissionInterfaces != null && jobSubmissionInterfaces.size() > 0) {
if (jobSubmissionInterfaces.get(0).getJobSubmissionProtocol().equals(JobSubmissionProtocol.SSH)) {
String resourceManagerType = registryClient.getSSHJobSubmission(jobSubmissionInterfaces.get(0).getJobSubmissionInterfaceId()).getResourceJobManager().getResourceJobManagerType().name();
ComputeResourceProfile computeResourceProfile = new ComputeResourceProfile(hostName, loginUserName, port, credentialStoreToken, queueNames, resourceManagerType);
computeResourceProfiles.add(computeResourceProfile);
}
}
} catch (TException e) {
logger.error(e.getMessage());
}
});
}
ArrayList<QueueStatusModel> queueStatuses = new ArrayList<>();
for (ComputeResourceProfile computeResourceProfile : computeResourceProfiles) {
String userName = computeResourceProfile.getUserName();
String hostName = computeResourceProfile.getHostName();
int port = computeResourceProfile.getPort();
try {
JSch jsch = new JSch();
CredentialStoreService.Client credentialClient = getCredentialStoreClient();
SSHCredential sshCredential = credentialClient.getSSHCredential(computeResourceProfile.getCredentialStoreToken(), superTenantGatewayId);
jsch.addIdentity(hostName, sshCredential.getPrivateKey().getBytes(), sshCredential.getPublicKey().getBytes(), sshCredential.getPassphrase().getBytes());
Session session = jsch.getSession(userName, hostName, port);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
logger.debug("Connected to " + hostName);
session.connect();
for (String queue : computeResourceProfile.getQueueNames()) {
String command = "";
if (computeResourceProfile.getResourceManagerType().equals("SLURM"))
command = "sinfo -s -p " + queue + " -o \"%a %F\" | tail -1";
else if (computeResourceProfile.getResourceManagerType().equals("PBS"))
command = "qstat -Q " + queue + "| tail -1";
if (command.equals("")) {
logger.warn("No matching resource manager type found for " + computeResourceProfile.getResourceManagerType());
continue;
}
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
String result = "";
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
result += new String(tmp, 0, i);
}
if (channel.isClosed()) {
if (in.available() > 0)
continue;
logger.debug(hostName + " " + queue + " " + "exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
channel.disconnect();
if (result != null && result.length() > 0) {
QueueStatusModel queueStatus = null;
if (computeResourceProfile.getResourceManagerType().equals("SLURM")) {
String[] sparts = result.split(" ");
boolean isUp = sparts[0].equalsIgnoreCase("up");
String knts = sparts[1];
sparts = knts.split("/");
int running = Integer.parseInt(sparts[0].trim());
int queued = Integer.parseInt(sparts[1].trim());
queueStatus = new QueueStatusModel(hostName, queue, isUp, running, queued, System.currentTimeMillis());
} else if (computeResourceProfile.getResourceManagerType().equals("PBS")) {
result = result.replaceAll("\\s+", " ");
String[] sparts = result.split(" ");
boolean isUp = sparts[3].equalsIgnoreCase("yes");
int running = Integer.parseInt(sparts[6].trim());
int queued = Integer.parseInt(sparts[5].trim());
queueStatus = new QueueStatusModel(hostName, queue, isUp, running, queued, System.currentTimeMillis());
}
if (queueStatus != null)
queueStatuses.add(queueStatus);
}
}
session.disconnect();
} catch (Exception ex) {
logger.error("Failed to get cluster status from " + computeResourceProfile.getHostName());
logger.error(ex.getMessage(), ex);
}
}
if (queueStatuses != null && queueStatuses.size() > 0) {
registryClient.registerQueueStatuses(queueStatuses);
}
} catch (Exception e) {
throw new JobExecutionException(e);
}
}
use of com.jcraft.jsch.Channel in project ASAP by salmant.
the class TTS1 method How_many_existing_servers.
// ///////////////////////////////////////////
public static int How_many_existing_servers() {
int i = 0;
String haproy_ip = "194.249.1.110";
String haproy_host_user = "root";
String haproy_host_password = "****************";
try {
String command = "cat /etc/haproxy/haproxy.cfg";
JSch jsch = new JSch();
com.jcraft.jsch.Session session = jsch.getSession(haproy_host_user, haproy_ip, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
;
session.setPassword(haproy_host_password);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream input = channel.getInputStream();
channel.connect();
try {
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(inputReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("-www"))
i++;
// System.out.println(line);
}
bufferedReader.close();
inputReader.close();
} catch (IOException ex) {
ex.printStackTrace();
return i;
}
channel.disconnect();
session.disconnect();
return i;
} catch (Exception ex) {
ex.printStackTrace();
return i;
}
}
use of com.jcraft.jsch.Channel in project ASAP by salmant.
the class TTS2 method How_many_existing_servers.
// ///////////////////////////////////////////
public static int How_many_existing_servers() {
int i = 0;
String haproy_ip = "194.249.1.110";
String haproy_host_user = "root";
String haproy_host_password = "****************";
try {
String command = "cat /etc/haproxy/haproxy.cfg";
JSch jsch = new JSch();
com.jcraft.jsch.Session session = jsch.getSession(haproy_host_user, haproy_ip, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
;
session.setPassword(haproy_host_password);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream input = channel.getInputStream();
channel.connect();
try {
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(inputReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("-www"))
i++;
// System.out.println(line);
}
bufferedReader.close();
inputReader.close();
} catch (IOException ex) {
ex.printStackTrace();
return i;
}
channel.disconnect();
session.disconnect();
return i;
} catch (Exception ex) {
ex.printStackTrace();
return i;
}
}
Aggregations