Changeset 2295


Ignore:
Timestamp:
Mar 17, 2014, 2:09:53 PM (9 years ago)
Author:
Nicklas Nordborg
Message:

References #533: Add secondary analysis section to Reggie

Added a page listing connected OGS clusters.

Added some more error handling when sending commands to servers via SSH.

Location:
extensions/net.sf.basedb.reggie/trunk
Files:
5 added
3 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.reggie/trunk/META-INF/servlets.xml

    r2267 r2295  
    101101    <servlet-class>net.sf.basedb.reggie.servlet.SessionServlet</servlet-class>
    102102  </servlet>
     103  <servlet>
     104    <servlet-name>Ogs</servlet-name>
     105    <servlet-class>net.sf.basedb.reggie.servlet.OgsServlet</servlet-class>
     106  </servlet>
    103107</servlets>
  • extensions/net.sf.basedb.reggie/trunk/resources/index.jsp

    r2267 r2295  
    6565  <script language="JavaScript">
    6666 
    67   var debug = false;
     67  var debug = 1;
    6868  var myPermissions = {};
    6969 
     
    769769            </ul>
    770770          </dd>
     771          <dt>
     772            <base:icon image="<%=home + "/images/ogs-cluster.png"%>" />
     773            <span class="require-permission" data-role="Administrator" data-link="admin/ogs-clusters.jsp?ID=<%=ID%>">Open Grid Scheduler cluster</span>
    771774          </dl>
    772775          <%
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/ssh/SshHost.java

    r2293 r2295  
    11package net.sf.basedb.reggie.ssh;
    22
     3import java.io.ByteArrayOutputStream;
    34import java.io.IOException;
    4 import java.io.InputStream;
    5 import java.io.InputStreamReader;
    6 import java.io.StringWriter;
    75import java.security.PublicKey;
    86import java.util.HashMap;
    97import java.util.Map;
     8import java.util.concurrent.TimeUnit;
    109
    1110import org.json.simple.JSONObject;
     
    1817import net.schmizz.sshj.connection.channel.direct.Session.Command;
    1918import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
    20 import net.sf.basedb.util.FileUtil;
     19import net.sf.basedb.util.FileCopyRunnable;
    2120
    2221/**
     
    4342 
    4443  private String hostInfoCommand = "uname -a";
     44//  private String hostInfoCommand = "./demux.sh";
    4545 
    4646  /**
     
    171171    response is cached.
    172172  */
    173   public String getHostInfo()
    174   {
    175     String systemInfo = (String)infoCache.get("host-info");
     173  public CmdResult getHostInfo()
     174  {
     175    CmdResult systemInfo = (CmdResult)infoCache.get("host-info");
    176176    if (systemInfo == null)
    177177    {
    178       try
    179       {
    180         systemInfo = executeCmd(getHostInfoCommand());
    181       }
    182       catch (Exception ex)
    183       {
    184         systemInfo = ex.getMessage();
    185       }
     178      systemInfo = executeCmd(getHostInfoCommand(), 2);
    186179      infoCache.put("host-info", systemInfo);
    187180    }
     
    193186    be used to send commands or transfer files to/from
    194187    the host.
    195   */
    196   public SSHClient connect()
     188    @param timeout Timeout in seconds for the connection to be established
     189  */
     190  public SSHClient connect(int timeout)
    197191  {
    198192    SSHClient ssh = new SSHClient();
     
    207201    try
    208202    {
     203      ssh.setConnectTimeout(timeout * 1000);
    209204      ssh.connect(getAddress(), getPort());
    210205      ssh.authPassword(getUser(), getPassword());
     
    221216    Execute a single command on the host and return the
    222217    output as a string.
    223   */
    224   public String executeCmd(String cmd)
    225   {
    226    
    227     SSHClient ssh = connect();
     218    @param cmd The command to execute
     219    @param timeout Timeout in seconds to wait for the command to
     220      finish
     221  */
     222  public CmdResult executeCmd(String cmd, int timeout)
     223  {
     224    SSHClient ssh = null;
    228225    Session s = null;
    229     StringWriter out = new StringWriter();
     226    CmdResult result = new CmdResult();
    230227    try
    231228    {
     229      ssh = connect(timeout);
     230      ByteArrayOutputStream stdout = new ByteArrayOutputStream();
     231      ByteArrayOutputStream stderr = new ByteArrayOutputStream();
     232     
    232233      s = ssh.startSession();
    233234      Command c = s.exec(cmd);
    234       InputStream result = c.getInputStream();
    235       FileUtil.copy(new InputStreamReader(result), out);
     235
     236      new Thread(new FileCopyRunnable(c.getInputStream(), stdout)).start();
     237      new Thread(new FileCopyRunnable(c.getErrorStream(), stderr)).start();
     238      s.join(timeout, TimeUnit.SECONDS);
     239
     240      result.setStdout(stdout.toString("UTF-8"));
     241      result.setStderr(stderr.toString("UTF-8"));
     242      result.setExitStatus(c.getExitStatus());
    236243    }
    237244    catch (Exception ex)
    238245    {
    239       throw new RuntimeException(ex);
     246      result.setException(ex);
    240247    }
    241248    finally
     
    244251      SshUtil.close(ssh);
    245252    }
    246     return out.toString();
     253    return result;
    247254  }
    248255
     
    265272      JSONObject jsonHostInfo = new JSONObject();
    266273      jsonHostInfo.put("cmd", getHostInfoCommand());
    267       jsonHostInfo.put("info", getHostInfo());
     274      jsonHostInfo.put("info", getHostInfo().asJSONObject());
    268275      json.put("hostInfo", jsonHostInfo);
    269276     
Note: See TracChangeset for help on using the changeset viewer.