Merge pull request #16 from QuasarApp/customUser

Added custom user support
This commit is contained in:
Andrei Yankovich 2021-10-13 16:44:00 +03:00 committed by GitHub
commit 943cab9fee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 46 additions and 14 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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);

View File

@ -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.

View File

@ -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;

View File

@ -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());

View File

@ -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;

View File

@ -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;
}

View File

@ -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.

View File

@ -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