Search in sources :

Example 1 with SSHCredential

use of org.apache.airavata.model.credential.store.SSHCredential in project airavata by apache.

the class SSHUtil method main.

public static void main(String[] args) throws JSchException {
    // Test the validate method
    String username = System.getProperty("user.name");
    String privateKeyFilepath = System.getProperty("user.home") + "/.ssh/id_rsa";
    String publicKeyFilepath = privateKeyFilepath + ".pub";
    String passphrase = "changeme";
    String hostname = "changeme";
    Path privateKeyPath = Paths.get(privateKeyFilepath);
    Path publicKeyPath = Paths.get(publicKeyFilepath);
    SSHCredential sshCredential = new SSHCredential();
    sshCredential.setPassphrase(passphrase);
    try {
        sshCredential.setPublicKey(new String(Files.readAllBytes(publicKeyPath), "UTF-8"));
        sshCredential.setPrivateKey(new String(Files.readAllBytes(privateKeyPath), "UTF-8"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    boolean result = validate(hostname, 22, username, sshCredential);
    System.out.println(result);
}
Also used : Path(java.nio.file.Path) SSHCredential(org.apache.airavata.model.credential.store.SSHCredential) IOException(java.io.IOException)

Example 2 with SSHCredential

use of org.apache.airavata.model.credential.store.SSHCredential in project airavata by apache.

the class TestSSLClient method testSSHCredential.

public static void testSSHCredential(CredentialStoreService.Client client) {
    try {
        SSHCredential sshCredential = new SSHCredential();
        sshCredential.setUsername("test");
        sshCredential.setGatewayId("testGateway");
        sshCredential.setPassphrase("mypassphrase");
        String token = client.addSSHCredential(sshCredential);
        System.out.println("SSH Token :" + token);
        SSHCredential credential = client.getSSHCredential(token, "testGateway");
        Map<String, String> allSSHKeysForGateway = client.getAllSSHKeysForGateway(sshCredential.getGatewayId());
        System.out.println("private key : " + credential.getPrivateKey());
        System.out.println("public key : " + credential.getPublicKey());
    } catch (TTransportException e) {
        e.printStackTrace();
    } catch (TException e) {
        e.printStackTrace();
    }
}
Also used : TException(org.apache.thrift.TException) SSHCredential(org.apache.airavata.model.credential.store.SSHCredential) TTransportException(org.apache.thrift.transport.TTransportException)

Example 3 with SSHCredential

use of org.apache.airavata.model.credential.store.SSHCredential 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);
    }
}
Also used : TException(org.apache.thrift.TException) ComputeResourcePreference(org.apache.airavata.model.appcatalog.gatewayprofile.ComputeResourcePreference) JobSubmissionInterface(org.apache.airavata.model.appcatalog.computeresource.JobSubmissionInterface) ArrayList(java.util.ArrayList) JSch(com.jcraft.jsch.JSch) JobExecutionException(org.quartz.JobExecutionException) QueueStatusModel(org.apache.airavata.model.status.QueueStatusModel) RegistryService(org.apache.airavata.registry.api.RegistryService) SSHCredential(org.apache.airavata.model.credential.store.SSHCredential) ComputeResourceDescription(org.apache.airavata.model.appcatalog.computeresource.ComputeResourceDescription) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) TTransportException(org.apache.thrift.transport.TTransportException) TException(org.apache.thrift.TException) JobExecutionException(org.quartz.JobExecutionException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) ChannelExec(com.jcraft.jsch.ChannelExec) CredentialStoreService(org.apache.airavata.credential.store.cpi.CredentialStoreService) Session(com.jcraft.jsch.Session)

Aggregations

SSHCredential (org.apache.airavata.model.credential.store.SSHCredential)3 TException (org.apache.thrift.TException)2 TTransportException (org.apache.thrift.transport.TTransportException)2 Channel (com.jcraft.jsch.Channel)1 ChannelExec (com.jcraft.jsch.ChannelExec)1 JSch (com.jcraft.jsch.JSch)1 Session (com.jcraft.jsch.Session)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 ApplicationSettingsException (org.apache.airavata.common.exception.ApplicationSettingsException)1 CredentialStoreService (org.apache.airavata.credential.store.cpi.CredentialStoreService)1 ComputeResourceDescription (org.apache.airavata.model.appcatalog.computeresource.ComputeResourceDescription)1 JobSubmissionInterface (org.apache.airavata.model.appcatalog.computeresource.JobSubmissionInterface)1 ComputeResourcePreference (org.apache.airavata.model.appcatalog.gatewayprofile.ComputeResourcePreference)1 QueueStatusModel (org.apache.airavata.model.status.QueueStatusModel)1 RegistryService (org.apache.airavata.registry.api.RegistryService)1 JobExecutionException (org.quartz.JobExecutionException)1