Ignore:
Timestamp:
Aug 27, 2013 6:45:34 AM (8 months ago)
Author:
Ken Moore <ken@…>
Branches:
master, 9.2-release, releng/10.0, releng/10.0.1
Children:
7fa1c1d, 845aaf6
Parents:
539d8eb
Message:

Setup the life preserver backend to use QProcesses for all commands, and process window events while commands are running.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src-qt4/life-preserver/LPBackend.cpp

    r4e117af r2ac57ce  
    66QStringList LPBackend::listPossibleDatasets(){ 
    77  QString cmd = "zpool list -H -o name"; 
    8   //Need output, so run this in a QProcess 
    9   QProcess *proc = new QProcess; 
    10   proc->setProcessChannelMode(QProcess::MergedChannels); 
    11   proc->start(cmd); 
    12   proc->waitForFinished(); 
    13   QStringList out = QString(proc->readAllStandardOutput()).split("\n");  
    14   delete proc; 
     8  QStringList out = LPBackend::getCmdOutput(cmd); 
    159  //Now process the output (one dataset per line - no headers) 
    1610  QStringList list; 
     
    2620QStringList LPBackend::listDatasets(){ 
    2721  QString cmd = "lpreserver listcron"; 
    28   //Need output, so run this in a QProcess 
    29   QProcess *proc = new QProcess; 
    30   proc->setProcessChannelMode(QProcess::MergedChannels); 
    31   proc->start(cmd); 
    32   proc->waitForFinished(); 
    33   QStringList out = QString(proc->readAllStandardOutput()).split("\n");  
    34   delete proc; 
     22  QStringList out = LPBackend::getCmdOutput(cmd); 
    3523  //Now process the output 
    3624  QStringList list; 
     
    4533QStringList LPBackend::listDatasetSubsets(QString dataset){ 
    4634  QString cmd = "zfs list -H -t filesystem -o name,mountpoint,mounted"; 
    47   //Need output, so run this in a QProcess 
    48   QProcess *proc = new QProcess; 
    49   proc->setProcessChannelMode(QProcess::MergedChannels); 
    50   proc->start(cmd); 
    51   proc->waitForFinished(); 
    52   QStringList out = QString(proc->readAllStandardOutput()).split("\n");  
    53   delete proc; 
     35  QStringList out = LPBackend::getCmdOutput(cmd); 
    5436  //Now process the output (one dataset per line - no headers) 
    5537  QStringList list; 
     
    7961QStringList LPBackend::listLPSnapshots(QString dataset){ 
    8062  QString cmd = "lpreserver listsnap "+dataset; 
    81   //Need output, so run this in a QProcess 
    82   QProcess *proc = new QProcess; 
    83   proc->setProcessChannelMode(QProcess::MergedChannels); 
    84   proc->start(cmd); 
    85   proc->waitForFinished(); 
    86   QStringList out = QString(proc->readAllStandardOutput()).split("\n");  
    87   delete proc; 
     63  QStringList out = LPBackend::getCmdOutput(cmd); 
    8864  //Now process the output 
    8965  QStringList list; 
     
    10076QStringList LPBackend::listReplicationTargets(){ 
    10177  QString cmd = "lpreserver replicate list"; 
    102   //Need output, so run this in a QProcess 
    103   QProcess *proc = new QProcess; 
    104   proc->setProcessChannelMode(QProcess::MergedChannels); 
    105   proc->start(cmd); 
    106   proc->waitForFinished(); 
    107   QStringList out = QString(proc->readAllStandardOutput()).split("\n");  
    108   delete proc; 
     78  QStringList out = LPBackend::getCmdOutput(cmd); 
    10979  //Now process the output 
    11080  QStringList list; 
     
    12191QStringList LPBackend::listCurrentStatus(){ 
    12292  QString cmd = "lpreserver status"; 
    123   //Need output, so run this in a QProcess 
    124   QProcess *proc = new QProcess; 
    125   proc->setProcessChannelMode(QProcess::MergedChannels); 
    126   proc->start(cmd); 
    127   proc->waitForFinished(); 
    128   QStringList out = QString(proc->readAllStandardOutput()).split("\n");  
    129   delete proc; 
    130   QStringList list; 
     93  QStringList out = LPBackend::getCmdOutput(cmd); 
    13194  //Now process the output       
     95  QStringList list; 
    13296  for(int i=2; i<out.length(); i++){ //first 2 lines are headers 
    13397    //Format: <dataset>:::<lastsnapshot | NONE>:::<lastreplication | NONE> 
     
    158122  //Create the command 
    159123  QString cmd = "lpreserver cronsnap "+dataset+" start "+freq+" "+QString::number(numToKeep); 
    160   int ret = system(cmd.toUtf8()); 
     124  int ret = LPBackend::runCmd(cmd); 
    161125    
    162126  return (ret == 0); 
     
    165129bool LPBackend::removeDataset(QString dataset){ 
    166130  QString cmd = "lpreserver cronsnap "+dataset+" stop"; 
    167   int ret = system(cmd.toUtf8());        
     131  int ret = LPBackend::runCmd(cmd); 
    168132    
    169133  return (ret == 0); 
     
    172136bool LPBackend::datasetInfo(QString dataset, int& time, int& numToKeep){ 
    173137  QString cmd = "lpreserver listcron"; 
    174   //Need output, so run this in a QProcess 
    175   QProcess *proc = new QProcess; 
    176   proc->setProcessChannelMode(QProcess::MergedChannels); 
    177   proc->start(cmd); 
    178   proc->waitForFinished(); 
    179   QStringList out = QString(proc->readAllStandardOutput()).split("\n");  
    180   delete proc; 
     138  QStringList out = LPBackend::getCmdOutput(cmd); 
    181139  //Now process the output 
    182140  bool ok = false; 
     
    206164bool LPBackend::newSnapshot(QString dataset, QString snapshotname){ 
    207165  QString cmd = "lpreserver mksnap --replicate "+dataset+" "+snapshotname; 
    208   int ret = system(cmd.toUtf8()); 
     166  int ret = LPBackend::runCmd(cmd); 
    209167    
    210168  return (ret == 0); 
     
    213171bool LPBackend::removeSnapshot(QString dataset, QString snapshot){ 
    214172  QString cmd = "lpreserver rmsnap "+dataset +" "+snapshot; 
    215   int ret = system(cmd.toUtf8());        
     173  int ret = LPBackend::runCmd(cmd); 
    216174    
    217175  return (ret == 0); 
     
    220178bool LPBackend::revertSnapshot(QString dataset, QString snapshot){ 
    221179  QString cmd = "lpreserver revertsnap "+dataset +" "+snapshot; 
    222   int ret  = system(cmd.toUtf8()); 
     180  int ret  = LPBackend::runCmd(cmd); 
    223181    
    224182  return (ret == 0); 
     
    281239   
    282240  QString cmd = "lpreserver replicate add "+remotehost+" "+user+" "+ QString::number(port)+" "+dataset+" "+remotedataset+" "+stime; 
    283   int ret = system(cmd.toUtf8()); 
     241  int ret = LPBackend::runCmd(cmd); 
    284242   
    285243  return (ret == 0); 
     
    288246bool LPBackend::removeReplication(QString dataset){ 
    289247  QString cmd = "lpreserver replicate remove "+dataset; 
    290   int ret = system(cmd.toUtf8());        
     248  int ret = LPBackend::runCmd(cmd);      
    291249    
    292250  return (ret == 0); 
     
    295253bool LPBackend::replicationInfo(QString dataset, QString& remotehost, QString& user, int& port, QString& remotedataset, int& time){ 
    296254  QString cmd = "lpreserver replicate list"; 
    297   //Need output, so run this in a QProcess 
    298   QProcess *proc = new QProcess; 
    299   proc->setProcessChannelMode(QProcess::MergedChannels); 
    300   proc->start(cmd); 
    301   proc->waitForFinished(); 
    302   QStringList out = QString(proc->readAllStandardOutput()).split("\n");  
    303   delete proc; 
     255  QStringList out = LPBackend::getCmdOutput(cmd); 
    304256  //Now process the output 
    305257  bool ok = false; 
     
    328280  QString LPPATH = "/usr/local/share/lifePreserver"; 
    329281  QString cmd = "xterm -e \""+LPPATH+"/scripts/setup-ssh-keys.sh "+remoteUser+" "+remoteHost+" "+QString::number(remotePort)+"\""; 
    330   int ret = system(cmd.toUtf8()); 
     282  int ret = LPBackend::runCmd(cmd); 
    331283  return (ret == 0); 
    332284} 
     
    335287  //Return format: "<mountpoint> (<device node>") 
    336288  QString cmd = "mount"; 
    337   //Need output, so run this in a QProcess 
    338   QProcess *proc = new QProcess; 
    339   proc->setProcessChannelMode(QProcess::MergedChannels); 
    340   proc->start(cmd); 
    341   proc->waitForFinished(); 
    342   QStringList out = QString(proc->readAllStandardOutput()).split("\n");  
    343   delete proc; 
     289  QStringList out = LPBackend::getCmdOutput(cmd); 
    344290  //Now process the output 
    345291  QStringList list; 
     
    367313  return ok; 
    368314} 
     315 
     316 
     317// ========================= 
     318//             PRIVATE FUNCTIONS 
     319// ========================= 
     320QStringList LPBackend::getCmdOutput(QString cmd){ 
     321  QProcess *proc = new QProcess; 
     322  proc->setProcessChannelMode(QProcess::MergedChannels); 
     323  proc->start(cmd); 
     324  while(!proc->waitForFinished(300)){ 
     325    QCoreApplication::processEvents(); 
     326  } 
     327  QStringList out = QString(proc->readAllStandardOutput()).split("\n");  
     328  delete proc;   
     329  return out; 
     330} 
     331 
     332int LPBackend::runCmd(QString cmd){ 
     333  QProcess *proc = new QProcess; 
     334  proc->setProcessChannelMode(QProcess::MergedChannels); 
     335  proc->start(cmd); 
     336  while(!proc->waitForFinished(300)){ 
     337    QCoreApplication::processEvents(); 
     338  } 
     339  int ret = proc->exitCode(); 
     340  delete proc;   
     341  return ret; 
     342} 
Note: See TracChangeset for help on using the changeset viewer.