CQtDeployer/Deploy/packing.cpp

261 lines
7.3 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2018-2021 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
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"
#include "quasarapp.h"
#include <QDebug>
2020-01-12 19:48:22 +03:00
#include <QProcess>
#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-20 15:43:00 +03:00
connect(_proc, SIGNAL(readyReadStandardOutput()),
this, SLOT(handleOutputUpdate()));
2020-01-04 16:39:25 +03:00
}
Packing::~Packing() {
_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()) {
2021-03-01 09:44:59 +03:00
QuasarAppUtils::Params::log("Fail to collect packages data.", QuasarAppUtils::Error);
2020-08-15 14:38:03 +03:00
return false;
}
for (auto package : qAsConst(_pakages)) {
2020-01-04 16:39:25 +03:00
2021-03-01 09:44:59 +03:00
if (!package) {
internalError();
2020-08-14 17:47:34 +03:00
return false;
2021-03-01 09:44:59 +03:00
}
2020-01-14 15:48:35 +03:00
2021-02-03 13:08:38 +03:00
if (!package->deployTemplate(*this)) {
QuasarAppUtils::Params::log(QString("Deploy package template error ocured. Package: %0.").
arg(package->getClassName()),
QuasarAppUtils::Error);
2020-08-14 17:47:34 +03:00
return false;
2021-02-03 13:08:38 +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
QFileInfo cmdInfo(cmd.command);
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);
}
_proc->setProgram(cmd.command);
2020-08-14 17:47:34 +03:00
_proc->setProcessEnvironment(_proc->processEnvironment());
_proc->setArguments(cmd.arguments);
2020-08-14 17:47:34 +03:00
_proc->setWorkingDirectory(cfg->getTargetDir());
_proc->start();
2020-11-13 15:43:05 +03:00
if (!_proc->waitForStarted()) {
2020-12-02 13:50:33 +03:00
QuasarAppUtils::Params::log(_proc->errorString(), QuasarAppUtils::Error);
QuasarAppUtils::Params::log(QString("Process error code: %0").arg(_proc->error()),
QuasarAppUtils::Error);
2020-08-14 17:47:34 +03:00
return false;
}
2020-08-14 17:47:34 +03:00
if (!_proc->waitForFinished(-1)) {
2020-11-12 17:57:04 +03:00
QuasarAppUtils::Params::log(_proc->errorString(), QuasarAppUtils::Error);
2020-11-13 14:50:12 +03:00
QuasarAppUtils::Params::log(QString("Process error code: %0").arg(_proc->error()),
QuasarAppUtils::Error);
2020-11-12 17:57:04 +03:00
2020-08-14 17:47:34 +03:00
return false;
}
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()) {
2021-03-01 09:44:59 +03:00
internalError();
2020-11-09 16:33:50 +03:00
return false;
}
2020-08-15 14:38:03 +03:00
if (!restorePackagesLocations()) {
2021-03-01 09:44:59 +03:00
internalError();
2020-08-15 14:38:03 +03:00
return false;
}
2020-08-15 14:38:03 +03:00
package->removeTemplate();
2020-08-15 19:29:17 +03:00
delete package;
}
2020-08-15 14:38:03 +03:00
2020-08-15 19:29:17 +03:00
const DeployConfig *cfg = DeployCore::_config;
2021-03-01 09:44:59 +03:00
if (!QDir(cfg->getTargetDir() + "/" + TMP_PACKAGE_DIR).removeRecursively()) {
QuasarAppUtils::Params::log("Fail to remove " + cfg->getTargetDir() + "/" + TMP_PACKAGE_DIR);
return false;
}
return true;
2020-08-15 14:38:03 +03:00
}
bool Packing::movePackage(const QString &package,
const QString &newLocation) {
2021-01-12 12:24:06 +03:00
// Disable moving data for extracting defaults templates.
if (QuasarAppUtils::Params::isEndable("getDefaultTemplate")) {
return true;
}
2020-08-15 14:38:03 +03:00
if (moveData(_packagesLocations.value(package),
newLocation)) {
_packagesLocations[package] = newLocation;
return true;
}
return false;
2020-08-14 17:47:34 +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
bool Packing::extractTemplates() {
const DeployConfig *cfg = DeployCore::_config;
QuasarAppUtils::Params::log("You use the getDefaultTemplate. All using templates will be extracted into " + cfg->getTargetDir(),
QuasarAppUtils::Info);
if (!prepareTemplatesForExtract()) {
return false;
}
for (auto package : qAsConst(_pakages)) {
if (!package)
return false;
if (!package->deployTemplate(*this))
return false;
delete package;
}
return true;
}
2020-12-14 10:52:22 +03:00
QStringList Packing::availablePackages() const {
return _packagesLocations.keys();
2020-12-10 11:12:04 +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) {
2020-12-03 20:00:14 +03:00
auto from = cfg->getTargetDir() + "/" + it.key();
2020-12-10 11:12:04 +03:00
if (PackageControl::isEmpty(from)) {
continue;
2020-12-03 20:00:14 +03:00
}
2021-03-01 09:44:59 +03:00
if (!moveData(from, cfg->getTargetDir() + "/" + TMP_PACKAGE_DIR + "/" + it.key())) {
QuasarAppUtils::Params::log("Fail to move " + from, QuasarAppUtils::Error);
2020-08-14 17:47:34 +03:00
return false;
2021-03-01 09:44:59 +03:00
}
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::prepareTemplatesForExtract() {
const DeployConfig *cfg = DeployCore::_config;
for (auto it = cfg->packages().begin(); it != cfg->packages().end(); ++it) {
_packagesLocations.insert(it.key(),
cfg->getTargetDir() + "/" + TMP_PACKAGE_DIR + "/" + it.key());
}
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
}
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-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-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);
}