10 #include "RunRemoteGingaVM.h"
13 #include <QDomDocument>
15 #include <QStringList>
20 #include <QMessageBox>
21 #include <QApplication>
24 #include <core/util/ComposerSettings.h>
25 using namespace composer::core::util;
27 void RunRemoteGingaVMAction::setCurrentProject(Project *project)
29 this->project = project;
32 QStringList RunRemoteGingaVMAction::filesToSendToGingaVM(Project *project,
35 QStringList filesToSend;
36 QFileInfo fileInfo(project->getLocation());
37 QString absoluteFilePath = fileInfo.path();
40 QDirIterator it(absoluteFilePath, QDirIterator::Subdirectories);
41 while (it.hasNext()) {
42 QString file = it.next();
43 if(!file.endsWith(
"."))
105 bool RunRemoteGingaVMAction::sendFilesToGingaVM(
SimpleSSHClient &sshclient,
106 QString baseLocalPath,
107 QString baseRemotePath,
108 QStringList filesToSend)
115 if(!baseRemotePath.endsWith(
"/"))
116 baseRemotePath.append(
"/");
120 qDebug() <<
"Must send these files:" << filesToSend;
122 emit taskDescription(tr(
"Sending files to remote machine..."));
123 emit taskMaximumValue(filesToSend.size());
126 QString mkdir =
"mkdir -p ";
127 mkdir += baseRemotePath;
129 qDebug() <<
"Running command = " << mkdir;
130 ret = !sshclient.
exec_cmd(mkdir.toStdString().c_str());
132 for(
int i = 0; i < filesToSend.size(); i++)
139 QString fullpath = filesToSend.at(i);
140 if(fullpath.contains(baseLocalPath))
142 QString relativePath = fullpath.mid(baseLocalPath.size()+1);
143 QString relativePathDir = relativePath.mid(0, relativePath.lastIndexOf(
"/")+1);
144 QFileInfo fileInfo(fullpath);
146 if(fileInfo.isFile())
148 qWarning() <<
"Sending file = " << fullpath <<
149 " to " << baseRemotePath + relativePathDir;
152 (baseRemotePath+relativePathDir).toStdString().c_str());
156 QString mkdir =
"mkdir -p ";
157 mkdir += baseRemotePath + relativePath;
159 qWarning() <<
"Running command = " << mkdir;
160 resp = sshclient.
exec_cmd(mkdir.toStdString().c_str());
170 bool RunRemoteGingaVMAction::fixSrcsFromNCLFile(
const QString &nclLocalPath)
173 QFile tmpXmlFile(nclLocalPath);
174 QDomDocument document(
"doc");
175 document.setContent(&tmpXmlFile);
176 QStack <QDomElement> elements;
177 elements.push(document.documentElement());
178 while(elements.size())
180 QDomElement current = elements.top();
183 if(current.hasAttribute(
"uniqueEntityId"))
184 current.removeAttribute(
"uniqueEntityId");
186 if(current.hasAttribute(
"src"))
188 QString src = current.attribute(
"src");
189 int start = src.lastIndexOf(QDir::separator());
190 src = src.mid(start+1);
191 current.setAttribute(
"src", src);
194 if(current.hasAttribute(
"documentURI"))
196 QString src = current.attribute(
"documentURI");
197 int start = src.lastIndexOf(QDir::separator());
198 src = src.mid(start+1);
199 current.setAttribute(
"documentURI", src);
201 if(current.hasAttribute(
"focusSrc"))
203 QString src = current.attribute(
"focusSrc");
204 int start = src.lastIndexOf(QDir::separator());
205 src = src.mid(start+1);
206 current.setAttribute(
"focusSrc", src);
208 if(current.hasAttribute(
"focusSelSrc"))
210 QString src = current.attribute(
"focusSelSrc");
211 int start = src.lastIndexOf(QDir::separator());
212 src = src.mid(start+1);
213 current.setAttribute(
"focusSelSrc", src);
216 QDomElement child = current.firstChildElement();
217 while(!child.isNull())
219 elements.push_back(child);
220 child = child.nextSiblingElement();
226 if (tmpXmlFile.open( QIODevice::WriteOnly ))
228 QTextStream textStream(&tmpXmlFile);
229 textStream << document.toString() ;
233 qWarning() <<
"I couldn't fix the path in XML file, probably it will "
239 void RunRemoteGingaVMAction::runCurrentProject()
247 QString location = project->getLocation();
249 emit taskDescription(tr(
"Getting remote connection configuration"));
252 settings.beginGroup(
"runginga");
253 QString remoteIp = settings.value(
"remote_ip").toString();
254 QString remoteUser = settings.value(
"remote_user").toString();
255 QString remotePasswd = settings.value(
"remote_password").toString();
256 QString remotePath = settings.value(
"remote_path").toString();
257 QString remoteCmd = settings.value(
"remote_start_cmd").toString();
262 remotePasswd.toStdString().c_str(),
263 remoteIp.toStdString().c_str(),
264 remotePath.toStdString().c_str());
267 emit taskDescription(tr(
"Trying to connect to machine from IP: %1").
271 qWarning() <<
"Trying to connect to remote machine...";
275 if( connRet != 0 && !mustStop)
277 qWarning() <<
"Could not connect to remote machine...";
282 QFileInfo fileNCLInfo (location);
283 QString tmpNCLDir = fileNCLInfo.absoluteDir().absolutePath();
284 QString nclLocalPath = tmpNCLDir;
286 nclLocalPath +=
"tmp.ncl";
288 emit taskDescription(tr(
"Computing files to transmit..."));
291 QStringList filesToSend = filesToSendToGingaVM(project, nclLocalPath);
295 sendFilesToGingaVM(sshclient, tmpNCLDir, remotePath, filesToSend))
299 QFile file(nclLocalPath);
300 if(file.open(QFile::WriteOnly | QIODevice::Truncate))
303 if(project->getChildren().size())
304 file.write(project->getChildren().at(0)->toString(0,
false).toAscii());
312 remotePath.toStdString().c_str());
313 QString cmd = remoteCmd;
315 cmd += remotePath +
"/tmp.ncl";
317 sshclient.
exec_cmd(cmd.toStdString().c_str());
322 qWarning() <<
"Error trying to run NCL. Could not create : "
323 << nclLocalPath <<
" !";
328 qWarning() <<
"Error copying the dependency files";
336 void RunRemoteGingaVMAction::stopExecution()
338 qDebug() <<
"RunRemoteGingaVMAction::stopExecution";
340 emit taskDescription(tr(
"Cancelling..."));
345 void StopRemoteGingaVMAction::stopRunningApplication()
349 settings.beginGroup(
"runginga");
350 QString remoteIp = settings.value(
"remote_ip").toString();
351 QString remoteUser = settings.value(
"remote_user").toString();
352 QString remotePasswd = settings.value(
"remote_password").toString();
353 QString remotePath = settings.value(
"remote_path").toString();
354 QString remoteStopCmd = settings.value(
"remote_stop_cmd").toString();
359 remotePasswd.toStdString().c_str(),
360 remoteIp.toStdString().c_str(),
361 remotePath.toStdString().c_str());
366 qWarning() <<
"Could not connect to remote machine...";
370 sshclient.
exec_cmd(remoteStopCmd.toStdString().c_str());