diff --git a/Patronum/src/PServiceBase.cpp b/Patronum/src/PServiceBase.cpp index cf36fb2..8c8dda7 100644 --- a/Patronum/src/PServiceBase.cpp +++ b/Patronum/src/PServiceBase.cpp @@ -117,7 +117,7 @@ void ServiceBase::printDefaultHelp() { serviceHelp.unite({{QObject::tr("Options that available befor install"),{ {"start / s", QObject::tr("Start a service in console")}, {"daemon / d", QObject::tr("Start a service as a daemon")}, - {"install / i", QObject::tr("Install a service")}, + {"install / i / -install username", QObject::tr("Install a service. Add user name if you want to run service from custom user. Example: -install username")}, {"unistall / u", QObject::tr("unistall a service")}, }}}); @@ -163,7 +163,7 @@ int ServiceBase::exec() { bool fDaemon = QuasarAppUtils::Params::isEndable("daemon") || QuasarAppUtils::Params::isEndable("d"); if (QuasarAppUtils::Params::isEndable("install") || QuasarAppUtils::Params::isEndable("i")) { - if (!d_ptr->install()) + if (!d_ptr->install(QuasarAppUtils::Params::getArg("install", "root"))) return Patronum::PatronumError::UnsupportedPlatform; return 0; } diff --git a/Patronum/src/Private/SystemD/service.service b/Patronum/src/Private/SystemD/service.service index 52862c6..5f6ec59 100644 --- a/Patronum/src/Private/SystemD/service.service +++ b/Patronum/src/Private/SystemD/service.service @@ -3,8 +3,8 @@ Description=%3 service. This service generated automaticly by Patronum libarary. [Service] Type=forking -User=root -Group=root +User=%4 +Group=%4 ExecStart=%0 d ExecStop=%0 stop PIDFile=%1 diff --git a/Patronum/src/Private/baseinstaller.cpp b/Patronum/src/Private/baseinstaller.cpp index bd8121a..15514a6 100644 --- a/Patronum/src/Private/baseinstaller.cpp +++ b/Patronum/src/Private/baseinstaller.cpp @@ -18,7 +18,10 @@ BaseInstaller::BaseInstaller(): } -bool BaseInstaller::install(const QString &executable) { +bool BaseInstaller::install(const QString &executable, const QString &user) { + + Q_UNUSED(user) + if (!QFile::exists(executable)) { QuasarAppUtils::Params::log(QObject::tr("The service executable file is not exists %0\n").arg(executable), QuasarAppUtils::Error); diff --git a/Patronum/src/Private/baseinstaller.h b/Patronum/src/Private/baseinstaller.h index 902c31a..f968094 100644 --- a/Patronum/src/Private/baseinstaller.h +++ b/Patronum/src/Private/baseinstaller.h @@ -25,9 +25,10 @@ public: /** * @brief install This implementation prepare executable for instalation. * @param executable This is path to executable file of service. + * @param user This is user that will be run installed service. by default it is root. * @return true if service is not installed else false */ - bool install(const QString &executable) override; + bool install(const QString &executable, const QString& user = DEFAULT_USER) override; /** * @brief uninstall This method check if service installed. diff --git a/Patronum/src/Private/installer.h b/Patronum/src/Private/installer.h index 24b778d..fbb2ae2 100644 --- a/Patronum/src/Private/installer.h +++ b/Patronum/src/Private/installer.h @@ -10,9 +10,11 @@ #include <QString> +#define DEFAULT_USER "root" namespace Patronum { + /** * @brief The Installer class - base interface for all plugins */ @@ -22,12 +24,36 @@ public: Installer(); virtual ~Installer() = default; - virtual bool install(const QString& executable) = 0; + /** + * @brief install This method install service on host. + * @param executable This is path to service executable. + * @param user This is user tah will run serveice. If this argument is empty the service will be use root user. + * @return true if service installed successfull + */ + virtual bool install(const QString& executable, const QString& user = DEFAULT_USER) = 0; + + /** + * @brief uninstall This method remove service from autostart. + * @return trur if service removed successful + */ virtual bool uninstall() = 0; + /** + * @brief enable This method enable installed service. This method invoked by default in the Installer::install method. + * @return true if the service enabled successfull. + */ virtual bool enable() = 0; + + /** + * @brief disable This method disable installed service. + * @return true if the service disabled successfull. + */ virtual bool disable() = 0; + /** + * @brief isInstalled This method return true if service alredy installed. + * @return true if service alredy installed else flase. + */ virtual bool isInstalled() const = 0; diff --git a/Patronum/src/Private/installersystemd.cpp b/Patronum/src/Private/installersystemd.cpp index 5699544..c33753e 100644 --- a/Patronum/src/Private/installersystemd.cpp +++ b/Patronum/src/Private/installersystemd.cpp @@ -25,7 +25,7 @@ Patronum::InstallerSystemD::~InstallerSystemD() { } -bool InstallerSystemD::install(const QString &executable) { +bool InstallerSystemD::install(const QString &executable, const QString& user) { if (!BaseInstaller::install(executable)) { return false; @@ -50,6 +50,7 @@ bool InstallerSystemD::install(const QString &executable) { service = service.arg(PCommon::instance()->getPidfile()); service = service.arg(PCommon::instance()->getPWD()); service = service.arg(PCommon::instance()->getServiceName()); + service = service.arg(user); templ.setFileName(absaluteServicePath()); diff --git a/Patronum/src/Private/installersystemd.h b/Patronum/src/Private/installersystemd.h index 88ad920..019ec5b 100644 --- a/Patronum/src/Private/installersystemd.h +++ b/Patronum/src/Private/installersystemd.h @@ -26,7 +26,7 @@ public: // Installer interface public: - bool install(const QString &executable) override; + bool install(const QString &executable, const QString &user = DEFAULT_USER) override; bool uninstall() override; bool enable() override; bool disable() override; diff --git a/Patronum/src/Private/serviceprivate.cpp b/Patronum/src/Private/serviceprivate.cpp index f9fb579..de746c8 100644 --- a/Patronum/src/Private/serviceprivate.cpp +++ b/Patronum/src/Private/serviceprivate.cpp @@ -78,7 +78,7 @@ bool ServicePrivate::sendCloseConnection() { return _socket->send(_parser->createPackage(Command::CloseConnection)); } -bool ServicePrivate::install() { +bool ServicePrivate::install(const QString& user) { if (!_installer) { QuasarAppUtils::Params::log(errorToString(UnsupportedPlatform), @@ -86,7 +86,7 @@ bool ServicePrivate::install() { return false; } - if (!_installer->install(getServiceLauncher())) { + if (!_installer->install(getServiceLauncher(), user)) { return false; } diff --git a/Patronum/src/Private/serviceprivate.h b/Patronum/src/Private/serviceprivate.h index afe9248..c7f5e87 100644 --- a/Patronum/src/Private/serviceprivate.h +++ b/Patronum/src/Private/serviceprivate.h @@ -48,9 +48,10 @@ public: /** * @brief install This method install your service in thec current system. + * @param user This is name of custom user that will be run your service after reboot system. * @return return true if the service installed succesful */ - bool install(); + bool install(const QString &user); /** * @brief uninstall This method unistall your service in thec current system. diff --git a/README.md b/README.md index 1bc1f67..27ae6eb 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,8 @@ Becouse This library offers easy interface to control your demons likewise the m * Auto create a Controller of your Service. ## Deffault sopprted commands -* install - deploys your daemon into your host system. -* unistall - removes old deployed daemon. +* install - deploys your daemon into your host system. This command deploy service for root user if you want to deploy service for specify user just use **-install <UserName>** command (root right required) +* unistall - removes old deployed daemon. (root right required) * start - starts your service * stop - stops ypur service * pause - sends pause command to your daemon