Guest User

Untitled

a guest
Nov 4th, 2021
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.47 KB | None | 0 0
  1. package com.java.nemonix;
  2.  
  3. import net.schmizz.sshj.SSHClient;
  4. import net.schmizz.sshj.common.IOUtils;
  5. import net.schmizz.sshj.connection.ConnectionException;
  6. import net.schmizz.sshj.connection.channel.direct.Session;
  7. import net.schmizz.sshj.sftp.SFTPClient;
  8. import net.schmizz.sshj.transport.TransportException;
  9. import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
  10. import net.schmizz.sshj.xfer.FileSystemFile;
  11. import org.apache.commons.io.FileUtils;
  12. import org.apache.commons.net.ftp.FTP;
  13. import org.apache.commons.net.ftp.FTPClient;
  14.  
  15. import javax.swing.*;
  16. import java.io.*;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.HashMap;
  20. import java.util.Locale;
  21. import java.util.concurrent.BrokenBarrierException;
  22. import java.util.concurrent.CyclicBarrier;
  23. import java.util.concurrent.TimeUnit;
  24.  
  25. public class Linpack {
  26.  
  27.     private HashMap<String, Session> sessions = new HashMap<>();
  28.     private HashMap<String, String> ips;
  29.     public Setup setup;
  30.     private final ArrayList<String> installLinpack = new ArrayList<>(Arrays.asList("tar xfv l_mklb_p_2020.2.001.tgz", "tar xfv l_mpi_2019.8.254.tgz", "cd l_mpi_2019.8.254", "./install.sh", " ", "q", "accept", "1", "1", "1", "1"));
  31.     private final ArrayList<String> executeLinpack = new ArrayList<>(Arrays.asList("cd", "cd l_mklb_p_2020.2.001/benchmarks_2020/linux/mkl/benchmarks/mp_linpack; ln -s /opt/intel/compilers_and_libraries_2020.2.254/linux/mpi/intel64/bin/mpirun /bin/mpirun", "ln -s /opt/intel/compilers_and_libraries_2020.2.254/linux/mpi/intel64/bin/mpivars.sh /bin/mpivars.sh", "cd", "cd l_mklb_p_2020.2.001/benchmarks_2020/linux/mkl/benchmarks/linpack", "./runme_xeon64"));
  32.  
  33.     public Linpack(Setup setup) {
  34.         this.setup = setup;
  35.     }
  36.  
  37.     public void startLinpack() {
  38.         ips = setup.getIPsByMAC();
  39.  
  40.         File mklb = new File(new JFileChooser().getFileSystemView().getDefaultDirectory()+"\\NX_SystemSetup\\data\\linpack\\l_mklb_p_2020.2.001.tgz");
  41.         File mpi = new File(new JFileChooser().getFileSystemView().getDefaultDirectory()+"\\NX_SystemSetup\\data\\linpack\\l_mpi_2019.8.254.tgz");
  42.         File lininput = new File(new JFileChooser().getFileSystemView().getDefaultDirectory()+"\\NX_SystemSetup\\data\\linpack\\lininput_xeon64");
  43.         CyclicBarrier gate = new CyclicBarrier(ips.size()+1);
  44.         ArrayList<Thread> threads = new ArrayList<>();
  45.         for(String ip : ips.keySet()) {
  46.             Thread t = new Thread(() -> {
  47.                 //System.out.println("Uploading file mklb to " + ips.get(ip));
  48.                 uploadFile(ips.get(ip), mklb, "/root/");
  49.                 uploadFile(ips.get(ip), mpi, "/root/");
  50.                 try {
  51.                     gate.await();
  52.                 } catch (InterruptedException | BrokenBarrierException e) {
  53.                     setup.writeErrorLog(e);
  54.                 }
  55.             });
  56.             threads.add(t);
  57.         }
  58.  
  59.         for(Thread t : threads) t.start();
  60.         threads.clear();
  61.  
  62.         try {
  63.             gate.await();
  64.         } catch (InterruptedException | BrokenBarrierException e) {
  65.             setup.writeErrorLog(e);
  66.         }
  67.  
  68.         for(String command : installLinpack) {
  69.             executeCommand("192.168.4.55", command);
  70.         }
  71.  
  72.         gate.reset();
  73.         for(String ip : ips.keySet()) {
  74.             Thread t = new Thread(() -> {
  75.                 for(String command : installLinpack) {
  76.                     executeCommand(ips.get(ip), command);
  77.                 }
  78.                 try {
  79.                     gate.await();
  80.                 } catch (InterruptedException | BrokenBarrierException e) {
  81.                     setup.writeErrorLog(e);
  82.                 }
  83.             });
  84.             threads.add(t);
  85.         }
  86.  
  87.         for(Thread t : threads) t.start();
  88.         threads.clear();
  89.  
  90.         String configFileName = getLinpackConfigFileName(setup.getTemplate().getName(), setup.getVersion());
  91.         File config = new File(new JFileChooser().getFileSystemView().getDefaultDirectory()+"\\NX_SystemSetup\\data\\linpack\\" + configFileName);
  92.         try {
  93.             FileUtils.copyFile(config, lininput);
  94.         } catch (IOException e) {
  95.             setup.writeErrorLog(e);
  96.         }
  97.  
  98.         for(String ip : ips.keySet()) {
  99.             Thread t = new Thread(() -> {
  100.                 uploadFile(ips.get(ip), lininput, "l_mklb_p_2020.2.001/benchmarks_2020/linux/mkl/benchmarks/linpack/");
  101.                 try {
  102.                     gate.await();
  103.                 } catch (InterruptedException | BrokenBarrierException e) {
  104.                     setup.writeErrorLog(e);
  105.                 }
  106.             });
  107.             threads.add(t);
  108.         }
  109.  
  110.         try {
  111.             gate.await();
  112.         } catch (InterruptedException | BrokenBarrierException e) {
  113.             setup.writeErrorLog(e);
  114.         }
  115.  
  116.         gate.reset();
  117.         for(Thread t : threads) t.start();
  118.         threads.clear();
  119.  
  120.         try {
  121.             gate.await();
  122.         } catch (InterruptedException | BrokenBarrierException e) {
  123.             setup.writeErrorLog(e);
  124.         }
  125.  
  126.         for(String ip : ips.keySet()) {
  127.             Thread t = new Thread(() -> {
  128.                 for(String command : executeLinpack) {
  129.                     executeCommand(ips.get(ip), command);
  130.                 }
  131.             });
  132.             threads.add(t);
  133.         }
  134.  
  135.         for(Thread t : threads) t.start();
  136.         threads.clear();
  137.     }
  138.  
  139.     public void executeCommand(String ip, String command) {
  140.         StringBuilder test = new StringBuilder();
  141.         final SSHClient sshClient = new SSHClient();
  142.         sshClient.addHostKeyVerifier(new PromiscuousVerifier());
  143.         Session session = null;
  144.         try {
  145.             sshClient.connect(ip);
  146.             sshClient.authPassword("root", "admin");
  147.             session = sshClient.startSession();
  148.             final Session.Command cmd = session.exec(command);
  149.             test.append(IOUtils.readFully(cmd.getInputStream()));
  150.             cmd.join(5, TimeUnit.SECONDS);
  151.             test.append("\n** exit status: ").append(cmd.getExitStatus());
  152.             System.out.println(test);
  153.         } catch (TransportException | ConnectionException e) {
  154.             setup.writeErrorLog(e);
  155.             System.out.println("SSH Error");
  156.         } catch (IOException e) {
  157.             setup.writeErrorLog(e);
  158.         } finally {
  159.             try {
  160.                 if (session != null) {
  161.                     session.close();
  162.                 }
  163.             } catch (TransportException | ConnectionException e) {
  164.                 setup.writeErrorLog(e);
  165.             }
  166.             try {
  167.                 sshClient.disconnect();
  168.             } catch (IOException e) {
  169.                 setup.writeErrorLog(e);
  170.             }
  171.         }
  172.     }
  173.  
  174.     public void uploadFile(String ip, File file, String dest) {
  175.         SSHClient sshClient = new SSHClient();
  176.         sshClient.addHostKeyVerifier(new PromiscuousVerifier());
  177.         try {
  178.             sshClient.connect(ip);
  179.             sshClient.authPassword("root", "admin");
  180.             try {
  181.                 //sshClient.authPublickey(System.getProperty("user.name"));
  182.                 final String src = file.getAbsolutePath();
  183.                 try (SFTPClient sftp = sshClient.newSFTPClient()) {
  184.                     sftp.put(new FileSystemFile(src), dest);
  185.                 }
  186.             } finally {
  187.                 sshClient.disconnect();
  188.             }
  189.         } catch (IOException e) {
  190.             setup.writeErrorLog(e);
  191.         }
  192.     }
  193. }
  194.  
Advertisement
Add Comment
Please, Sign In to add comment