コード例 #1
0
ファイル: sge.cpp プロジェクト: dlonie/XtalOpt
  bool SgeQueueInterface::startJob(Structure *s)
  {
    SSHConnection *ssh = m_opt->ssh()->getFreeConnection();

    if (ssh == NULL) {
      m_opt->warning(tr("Cannot connect to ssh server"));
      return false;
    }

    QWriteLocker wlocker (s->lock());

    QString command = "cd \"" + s->getRempath() + "\" && " +
      m_qsub + " job.sh";

    QString stdout_str;
    QString stderr_str;
    int ec;
    if (!ssh->execute(command, stdout_str, stderr_str, ec) || ec != 0) {
      m_opt->warning(tr("Error executing %1: %2")
                     .arg(command).arg(stderr_str));
      m_opt->ssh()->unlockConnection(ssh);
      return false;
    }
    m_opt->ssh()->unlockConnection(ssh);

    // Assuming stdout_str value is
    // your job <jobID> ('job.sh') has been submitted
    unsigned int jobID = stdout_str.split(QRegExp("\\s+"))[2].toUInt();

    s->setJobID(jobID);
    s->startOptTimer();
    return true;
  }
コード例 #2
0
bool SyncTransferThread::connectToRemoteHost()
{
    QMap<QString, QString> host;
    BaseStorage *storage = BaseStorage::instance();

    this->remoteHost = host = storage->getHost(sess_name);
    q_debug()<<host;
    
    // RemoteHostConnectThread *conn 
    //     = new RemoteHostConnectThread(host["user_name"], host["password"], host["host_name"],
    //                                   host["port"].toShort(), host["pubkey"]);
    // conn->run();
    // ssh2_sess = (LIBSSH2_SESSION*)conn->get_ssh2_sess();
    // delete conn; conn = NULL;
    SSHConnection *conn = new SSHConnection();
    conn->setHostInfo(host);
    conn->connect();
    ssh2_sess = conn->sess;
    Q_ASSERT(ssh2_sess != NULL);

    ssh2_sftp = libssh2_sftp_init(ssh2_sess);
    
    if (ssh2_sftp != NULL) {
        Q_ASSERT(ssh2_sftp != NULL);
        q_debug()<<"connect ok";
        return true;
    }
    
    return false;
}
コード例 #3
0
 void run()
 {
   SSHConnection* conn = manager->getFreeConnection();
   conn->copyDirectoryToServer(from, to);
   conn->removeRemoteDirectory(to);
   manager->unlockConnection(conn);
 }
コード例 #4
0
void SSHManagerCLITest::lockAllAndExecute()
{
  QList<SSHConnection*> list;

  for (int i = 0; i < NUM_CONN; i++) {
    list.append(manager->getFreeConnection());
  }

  QString command = "expr 2 + 4";
  QString stdout_str, stderr_str;
  SSHConnection* conn;
  int ec;

  for (int i = 0; i < NUM_CONN; i++) {
    conn = list.at(i);
    QVERIFY(conn->execute(command, stdout_str, stderr_str, ec));
    QCOMPARE(ec, 0);
    QCOMPARE(stdout_str, QString("6\n"));
    QVERIFY(stderr_str.isEmpty());
  }

  for (int i = 0; i < NUM_CONN; i++) {
    manager->unlockConnection(list.at(i));
  }
}
コード例 #5
0
ファイル: sge.cpp プロジェクト: dlonie/XtalOpt
  bool SgeQueueInterface::stopJob(Structure *s)
  {
    SSHConnection *ssh = m_opt->ssh()->getFreeConnection();

    if (ssh == NULL) {
      m_opt->warning(tr("Cannot connect to ssh server"));
      return false;
    }

    // lock structure
    QWriteLocker locker (s->lock());

    // jobid has not been set, cannot delete!
    if (s->getJobID() == 0) {
      if (m_cleanRemoteOnStop) {
        this->cleanRemoteDirectory(s, ssh);
      }
      m_opt->ssh()->unlockConnection(ssh);
      return true;
    }

    const QString command = m_qdel + " " + QString::number(s->getJobID());

    // Execute
    QString stdout_str;
    QString stderr_str;
    int ec;
    bool ret = true;
    if (!ssh->execute(command, stdout_str, stderr_str, ec) || ec != 0) {
      // Most likely job is already gone from queue.
      ret = false;
    }

    s->setJobID(0);
    s->stopOptTimer();
    m_opt->ssh()->unlockConnection(ssh);
    return ret;
  }
コード例 #6
0
ファイル: sge.cpp プロジェクト: dlonie/XtalOpt
  QStringList SgeQueueInterface::getQueueList() const
  {
    // recast queue mutex as mutable for safe access:
    QReadWriteLock &queueMutex = const_cast<QReadWriteLock&> (m_queueMutex);

    queueMutex.lockForRead();

    // Limit queries to once per second
    if (m_queueTimeStamp.isValid() &&
        // QDateTime::msecsTo is not implemented until Qt 4.7
#if QT_VERSION >= 0x040700
        m_queueTimeStamp.msecsTo(QDateTime::currentDateTime())
        <= 1000*m_interval
#else
        // Check if day is the same. If not, refresh. Otherwise check
        // msecsTo current time
        (m_queueTimeStamp.date() == QDate::currentDate() &&
         m_queueTimeStamp.time().msecsTo(QTime::currentTime())
         <= 1000*m_interval)
#endif // QT_VERSION >= 4.7
        ) {
      // If the cache is valid, return it
      QStringList ret (m_queueData);
      queueMutex.unlock();
      return ret;
    }

    // Otherwise, store a copy of the current timestamp and switch
    // queuemutex to writelock
    QDateTime oldTimeStamp (m_queueTimeStamp);
    queueMutex.unlock();

    // Relock mutex
    QWriteLocker queueLocker (&queueMutex);

    // Non-fatal assert: Check current timestamp against the
    // oldTimeStamp from earlier. If they don't match, another thread
    // has already updated the queueData, so tail-recurse this
    // function and try again.
    if (m_queueTimeStamp.time().msecsTo(oldTimeStamp.time()) != 0) {
      queueLocker.unlock();
      return this->getQueueList();
    }

    // Queue will be updated -- cast queue cache and timestamp as
    // mutable
    QStringList &queueData     = const_cast<QStringList&> (m_queueData);
    QDateTime &queueTimeStamp  = const_cast<QDateTime&> (m_queueTimeStamp);

    // Get SSH connection
    SSHConnection *ssh = m_opt->ssh()->getFreeConnection();

    if (ssh == NULL) {
      m_opt->warning(tr("Cannot connect to ssh server"));
      queueTimeStamp = QDateTime::currentDateTime();
      queueData.clear();
      queueData << "CommError";
      QStringList ret (m_queueData);
      return ret;
    }

    QString command = m_qstat + " | grep " + m_opt->username;

    // Execute
    QString stdout_str;
    QString stderr_str;
    int ec;
    // Valid exit codes for grep: (0) matches found, execution successful
    //                            (1) no matches found, execution successful
    //                            (2) execution unsuccessful
    if (!ssh->execute(command, stdout_str, stderr_str, ec)
        || (ec != 0 && ec != 1 )
        ) {
      m_opt->ssh()->unlockConnection(ssh);
      m_opt->warning(tr("Error executing %1: (%2) %3\n\t"
                        "Using cached queue data.")
                     .arg(command)
                     .arg(QString::number(ec))
                     .arg(stderr_str));
      queueTimeStamp = QDateTime::currentDateTime();
      QStringList ret (m_queueData);
      return ret;
    }
    m_opt->ssh()->unlockConnection(ssh);

    queueData = stdout_str.split("\n", QString::SkipEmptyParts);

    QStringList ret (m_queueData);
    queueTimeStamp = QDateTime::currentDateTime();
    return ret;
  }