2020-01-04 16:39:25 +03:00
|
|
|
#include "Distributions/idistribution.h"
|
2020-01-16 15:25:36 +03:00
|
|
|
#include "deployconfig.h"
|
2020-08-15 14:38:03 +03:00
|
|
|
#include "filemanager.h"
|
2020-01-04 16:39:25 +03:00
|
|
|
#include "packing.h"
|
2020-08-14 17:47:34 +03:00
|
|
|
#include "pathutils.h"
|
2020-01-05 13:58:38 +03:00
|
|
|
#include "quasarapp.h"
|
|
|
|
#include <QDebug>
|
2020-01-12 19:48:22 +03:00
|
|
|
#include <QProcess>
|
2020-01-05 13:58:38 +03:00
|
|
|
#include <QThread>
|
2020-09-07 10:14:15 +03:00
|
|
|
#include <cassert>
|
2020-01-04 16:39:25 +03:00
|
|
|
|
2020-08-15 19:29:17 +03:00
|
|
|
#define TMP_PACKAGE_DIR "tmp_data"
|
|
|
|
|
2020-08-15 14:38:03 +03:00
|
|
|
Packing::Packing(FileManager *fileManager) {
|
|
|
|
assert(fileManager);
|
|
|
|
|
|
|
|
_fileManager = fileManager;
|
2020-01-20 11:23:29 +03:00
|
|
|
_proc = new QProcess(this);
|
2020-01-04 16:39:25 +03:00
|
|
|
|
2020-01-20 15:43:00 +03:00
|
|
|
connect(_proc, SIGNAL(readyReadStandardError()),
|
|
|
|
this, SLOT(handleOutputUpdate()));
|
2020-01-05 13:58:38 +03:00
|
|
|
|
2020-01-20 15:43:00 +03:00
|
|
|
connect(_proc, SIGNAL(readyReadStandardOutput()),
|
|
|
|
this, SLOT(handleOutputUpdate()));
|
2020-01-05 13:58:38 +03:00
|
|
|
|
2020-01-04 16:39:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Packing::~Packing() {
|
2020-01-05 13:58:38 +03:00
|
|
|
_proc->kill();
|
2020-01-04 16:39:25 +03:00
|
|
|
}
|
|
|
|
|
2020-08-14 17:47:34 +03:00
|
|
|
void Packing::setDistribution(const QList<iDistribution*> &pakages) {
|
2020-08-15 19:29:17 +03:00
|
|
|
_pakages = pakages;
|
2020-01-04 16:39:25 +03:00
|
|
|
}
|
|
|
|
|
2020-01-20 16:40:18 +03:00
|
|
|
bool Packing::create() {
|
2020-01-04 16:39:25 +03:00
|
|
|
|
2020-08-15 14:38:03 +03:00
|
|
|
if (!collectPackages()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-15 19:29:17 +03:00
|
|
|
for (auto package : _pakages) {
|
2020-01-04 16:39:25 +03:00
|
|
|
|
2020-08-14 17:47:34 +03:00
|
|
|
if (!package)
|
|
|
|
return false;
|
2020-01-14 15:48:35 +03:00
|
|
|
|
2020-08-15 14:38:03 +03:00
|
|
|
if (!package->deployTemplate(*this))
|
2020-08-14 17:47:34 +03:00
|
|
|
return false;
|
2020-01-05 13:58:38 +03:00
|
|
|
|
2020-11-10 11:50:54 +03:00
|
|
|
auto commands = package->runCmd();
|
|
|
|
|
|
|
|
for (const auto& cmd: commands) {
|
2020-08-14 17:47:34 +03:00
|
|
|
const DeployConfig *cfg = DeployCore::_config;
|
2020-01-16 15:25:36 +03:00
|
|
|
|
2020-11-10 11:50:54 +03:00
|
|
|
|
|
|
|
QFileInfo cmdInfo(cmd.command);
|
2020-05-04 20:24:21 +03:00
|
|
|
|
2020-08-14 17:47:34 +03:00
|
|
|
auto allExecRight = QFile::ExeUser | QFile::ExeGroup | QFile::ExeOwner;
|
|
|
|
if (!cmdInfo.permission(allExecRight)) {
|
|
|
|
QFile::setPermissions(cmdInfo.absoluteFilePath(), cmdInfo.permissions() | allExecRight);
|
|
|
|
}
|
|
|
|
|
2020-11-10 11:50:54 +03:00
|
|
|
_proc->setProgram(cmd.command);
|
2020-08-14 17:47:34 +03:00
|
|
|
_proc->setProcessEnvironment(_proc->processEnvironment());
|
2020-11-10 11:50:54 +03:00
|
|
|
_proc->setArguments(cmd.arguments);
|
2020-08-14 17:47:34 +03:00
|
|
|
_proc->setWorkingDirectory(cfg->getTargetDir());
|
|
|
|
|
|
|
|
_proc->start();
|
2020-05-04 20:24:21 +03:00
|
|
|
|
2020-08-14 17:47:34 +03:00
|
|
|
if (!_proc->waitForStarted(1000)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-05 13:58:38 +03:00
|
|
|
|
2020-08-14 17:47:34 +03:00
|
|
|
if (!_proc->waitForFinished(-1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto exit = QString("exit code = %0").arg(_proc->exitCode());
|
|
|
|
QString stdoutLog = _proc->readAllStandardOutput();
|
|
|
|
QString erroutLog = _proc->readAllStandardError();
|
|
|
|
auto message = QString("message = %0").arg(stdoutLog + " " + erroutLog);
|
|
|
|
|
|
|
|
if (_proc->exitCode() != 0) {
|
|
|
|
QuasarAppUtils::Params::log(message, QuasarAppUtils::Error);
|
|
|
|
|
|
|
|
if (QuasarAppUtils::Params::isDebug())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 16:33:50 +03:00
|
|
|
if (!package->cb()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-15 14:38:03 +03:00
|
|
|
if (!restorePackagesLocations()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-05 13:58:38 +03:00
|
|
|
|
2020-08-15 14:38:03 +03:00
|
|
|
package->removeTemplate();
|
2020-08-15 19:29:17 +03:00
|
|
|
delete package;
|
2020-01-05 13:58:38 +03:00
|
|
|
}
|
2020-08-15 14:38:03 +03:00
|
|
|
|
2020-08-15 19:29:17 +03:00
|
|
|
const DeployConfig *cfg = DeployCore::_config;
|
|
|
|
return QDir(cfg->getTargetDir() + "/" + TMP_PACKAGE_DIR).removeRecursively();
|
2020-08-15 14:38:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Packing::movePackage(const QString &package,
|
|
|
|
const QString &newLocation) {
|
|
|
|
|
|
|
|
if (moveData(_packagesLocations.value(package),
|
|
|
|
newLocation)) {
|
|
|
|
|
|
|
|
_packagesLocations[package] = newLocation;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-08-14 17:47:34 +03:00
|
|
|
}
|
2020-01-05 13:58:38 +03:00
|
|
|
|
2020-08-15 19:29:17 +03:00
|
|
|
bool Packing::copyPackage(const QString &package, const QString &newLocation) {
|
|
|
|
return _fileManager->copyFolder(_packagesLocations[package], newLocation, {}, nullptr, nullptr, true);
|
2020-08-14 17:47:34 +03:00
|
|
|
}
|
2020-01-16 15:25:36 +03:00
|
|
|
|
2020-08-15 14:38:03 +03:00
|
|
|
bool Packing::collectPackages() {
|
2020-08-14 17:47:34 +03:00
|
|
|
const DeployConfig *cfg = DeployCore::_config;
|
2020-04-14 17:59:12 +03:00
|
|
|
|
2020-08-15 19:29:17 +03:00
|
|
|
for (auto it = cfg->packages().begin(); it != cfg->packages().end(); ++it) {
|
|
|
|
if (!moveData(cfg->getTargetDir() + "/" + it.key(), cfg->getTargetDir() + "/" + TMP_PACKAGE_DIR + "/" + it.key()))
|
2020-08-14 17:47:34 +03:00
|
|
|
return false;
|
2020-08-15 19:29:17 +03:00
|
|
|
|
|
|
|
_packagesLocations.insert(it.key(), cfg->getTargetDir() + "/" + TMP_PACKAGE_DIR + "/" + it.key());
|
2020-08-14 17:47:34 +03:00
|
|
|
}
|
2020-08-15 14:38:03 +03:00
|
|
|
|
|
|
|
_defaultPackagesLocations = _packagesLocations;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Packing::moveData(const QString &from, const QString &to, const QString &ignore) const {
|
2020-08-15 19:29:17 +03:00
|
|
|
|
|
|
|
if (from == to )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!_fileManager->moveFolder(from, to, ignore)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return QDir(from).removeRecursively();
|
2020-08-15 14:38:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Packing::restorePackagesLocations() {
|
|
|
|
for (auto it = _packagesLocations.begin(); it != _packagesLocations.end(); ++it) {
|
|
|
|
if (!moveData(it.value(), _defaultPackagesLocations.value(it.key()))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 19:29:17 +03:00
|
|
|
_packagesLocations = _defaultPackagesLocations;
|
2020-08-15 14:38:03 +03:00
|
|
|
|
|
|
|
return true;
|
2020-01-04 16:39:25 +03:00
|
|
|
}
|
2020-01-05 13:58:38 +03:00
|
|
|
|
|
|
|
void Packing::handleOutputUpdate() {
|
2020-01-20 15:43:00 +03:00
|
|
|
|
2020-01-12 20:13:40 +03:00
|
|
|
QByteArray stdoutLog = _proc->readAllStandardOutput();
|
|
|
|
QByteArray erroutLog = _proc->readAllStandardError();
|
2020-01-05 13:58:38 +03:00
|
|
|
|
2020-01-12 20:13:40 +03:00
|
|
|
if (stdoutLog.size())
|
2020-04-04 15:43:46 +03:00
|
|
|
QuasarAppUtils::Params::log(stdoutLog,
|
2020-08-14 17:47:34 +03:00
|
|
|
QuasarAppUtils::Info);
|
2020-01-05 13:58:38 +03:00
|
|
|
|
2020-01-12 20:13:40 +03:00
|
|
|
if (erroutLog.size())
|
2020-04-04 15:43:46 +03:00
|
|
|
QuasarAppUtils::Params::log(erroutLog,
|
2020-08-14 17:47:34 +03:00
|
|
|
QuasarAppUtils::Info);
|
2020-01-05 13:58:38 +03:00
|
|
|
}
|
|
|
|
|
2020-01-20 13:24:01 +03:00
|
|
|
|