more improvements of queuedctl

This commit is contained in:
2017-03-16 01:58:50 +03:00
parent f59095dbb0
commit 69fbab45b2
27 changed files with 925 additions and 132 deletions

View File

@ -20,29 +20,14 @@
#include <queued/Queued.h>
extern "C" {
#include <termios.h>
#include <unistd.h>
}
#include "QueuedctlUser.h"
QString QueuedctlAuth::auth(const QString &_user)
{
qCDebug(LOG_APP) << "Auth as user" << _user;
// read password
// do not show input characters
struct termios tty;
::tcgetattr(STDIN_FILENO, &tty);
tty.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &tty);
qInfo() << "Password for" << _user;
QTextStream stream(stdin);
QString password;
stream >> password;
return QueuedCoreAdaptor::auth(_user, password);
return QueuedCoreAdaptor::auth(_user, QueuedctlUser::getPassword());
}
@ -51,12 +36,12 @@ QString QueuedctlAuth::getToken(const QString &_cache, const QString &_user)
qCDebug(LOG_APP) << "Get token using cache" << _cache << "and user"
<< _user;
QString tokenId = token(_cache);
QString tokenId = token(_user, _cache);
if (tryAuth(tokenId)) {
return tokenId;
} else {
tokenId = auth(_user);
setToken(tokenId, _cache);
setToken(tokenId, _user, _cache);
return getToken(_cache, _user);
}
}
@ -67,12 +52,13 @@ void QueuedctlAuth::parser(QCommandLineParser &_parser)
}
void QueuedctlAuth::setToken(const QString &_token, const QString &_cache)
void QueuedctlAuth::setToken(const QString &_token, const QString &_user,
const QString &_cache)
{
qCDebug(LOG_APP) << "Save token to" << _cache;
qCDebug(LOG_APP) << "Save token to" << _cache << "from user" << _user;
QSettings settings(_cache, QSettings::IniFormat);
settings.beginGroup("queuedctl");
settings.beginGroup(QString("queuedctl/%1").arg(_user));
settings.setValue("Token", _token);
settings.endGroup();
@ -80,14 +66,14 @@ void QueuedctlAuth::setToken(const QString &_token, const QString &_cache)
}
QString QueuedctlAuth::token(const QString &_cache)
QString QueuedctlAuth::token(const QString &_user, const QString &_cache)
{
qCDebug(LOG_APP) << "Load token from" << _cache;
qCDebug(LOG_APP) << "Load token from" << _cache << "for user" << _user;
QString token;
QSettings settings(_cache, QSettings::IniFormat);
settings.beginGroup("queuedctl");
settings.beginGroup(QString("queuedctl/%1").arg(_user));
token = settings.value("Token").toString();
settings.endGroup();

View File

@ -25,8 +25,9 @@ namespace QueuedctlAuth
QString auth(const QString &_user);
QString getToken(const QString &_cache, const QString &_user);
void parser(QCommandLineParser &_parser);
void setToken(const QString &_token, const QString &_cache);
QString token(const QString &_cache);
void setToken(const QString &_token, const QString &_user,
const QString &_cache);
QString token(const QString &_user, const QString &_cache);
bool tryAuth(const QString &_token);
};

View File

@ -20,7 +20,10 @@
#include "QueuedctlAuth.h"
#include "QueuedctlOption.h"
#include "QueuedctlPermissions.h"
#include "QueuedctlPlugins.h"
#include "QueuedctlTask.h"
#include "QueuedctlUser.h"
void QueuedctlCommon::checkArgs(const QStringList &_args, const int _count,
@ -50,6 +53,21 @@ QString QueuedctlCommon::commandsHelp()
}
QString QueuedctlCommon::hashToString(const QVariantHash &_hash)
{
qCDebug(LOG_APP) << "Convert hash to string" << _hash;
QStringList output;
QStringList keys = _hash.keys();
keys.sort();
for (auto &key : keys)
output += QString("%1: %2").arg(key).arg(
_hash[key].toString().replace('\n', ' '));
return output.join('\n');
}
void QueuedctlCommon::preprocess(const QStringList &_args,
QCommandLineParser &_parser)
{
@ -77,6 +95,16 @@ void QueuedctlCommon::preprocess(const QStringList &_args,
case QueuedctlArgument::OptionSet:
QueuedctlOption::parserSet(_parser);
break;
case QueuedctlArgument::PermissionAdd:
case QueuedctlArgument::PermissionRemove:
QueuedctlPermissions::parser(_parser);
break;
case QueuedctlArgument::PluginAdd:
case QueuedctlArgument::PluginRemove:
QueuedctlPlugins::parser(_parser);
break;
case QueuedctlArgument::PluginList:
break;
case QueuedctlArgument::TaskAdd:
QueuedctlTask::parserAdd(_parser);
break;
@ -86,11 +114,18 @@ void QueuedctlCommon::preprocess(const QStringList &_args,
case QueuedctlArgument::TaskSet:
QueuedctlTask::parserSet(_parser);
break;
case QueuedctlArgument::TaskStart:
case QueuedctlArgument::TaskStop:
QueuedctlTask::parserStart(_parser);
break;
case QueuedctlArgument::UserAdd:
QueuedctlUser::parserAdd(_parser);
break;
case QueuedctlArgument::UserGet:
QueuedctlUser::parserGet(_parser);
break;
case QueuedctlArgument::UserSet:
QueuedctlUser::parserSet(_parser);
break;
case QueuedctlArgument::Invalid:
checkArgs(_args, -1, _parser);
@ -129,7 +164,7 @@ QueuedctlCommon::process(QCommandLineParser &_parser, const QString &_cache,
QString token = QueuedctlAuth::auth(_user);
result.status = !token.isEmpty();
if (result.status)
QueuedctlAuth::setToken(token, _cache);
QueuedctlAuth::setToken(token, _user, _cache);
break;
}
case QueuedctlArgument::OptionGet: {
@ -150,6 +185,61 @@ QueuedctlCommon::process(QCommandLineParser &_parser, const QString &_cache,
.arg(args.at(1), args.at(2));
break;
}
case QueuedctlArgument::PermissionAdd: {
auto userId = QueuedctlUser::getUserId(args.at(1));
QString token = QueuedctlAuth::getToken(_cache, _user);
result.status
= QueuedctlPermissions::addPermission(userId, args.at(2), token);
if (result.status)
result.output = QString("Add permission %2 to user %1")
.arg(args.at(1))
.arg(args.at(2));
else
result.output = QString("Could not add permission %2 to user %1")
.arg(args.at(1))
.arg(args.at(2));
break;
}
case QueuedctlArgument::PermissionRemove: {
auto userId = QueuedctlUser::getUserId(args.at(1));
QString token = QueuedctlAuth::getToken(_cache, _user);
result.status
= QueuedctlPermissions::removePermission(userId, args.at(2), token);
if (result.status)
result.output = QString("Remove permission %2 from user %1")
.arg(args.at(1))
.arg(args.at(2));
else
result.output
= QString("Could not remove permission %2 from user %1")
.arg(args.at(1))
.arg(args.at(2));
break;
}
case QueuedctlArgument::PluginAdd: {
QString token = QueuedctlAuth::getToken(_cache, _user);
result.status = QueuedctlPlugins::addPlugin(args.at(1), token);
if (result.status)
result.output = QString("Add plugin %1").arg(args.at(1));
else
result.output = QString("Could not add plugin %1").arg(args.at(1));
break;
}
case QueuedctlArgument::PluginList: {
result.status = true;
result.output = QueuedctlPlugins::listPlugins().join('\n');
break;
}
case QueuedctlArgument::PluginRemove: {
QString token = QueuedctlAuth::getToken(_cache, _user);
result.status = QueuedctlPlugins::removePlugin(args.at(1), token);
if (result.status)
result.output = QString("Remove plugin %1").arg(args.at(1));
else
result.output
= QString("Could not remove plugin %1").arg(args.at(1));
break;
}
case QueuedctlArgument::TaskAdd: {
QString token = QueuedctlAuth::getToken(_cache, _user);
auto definitions = QueuedctlTask::getDefinitions(_parser, false);
@ -165,7 +255,8 @@ QueuedctlCommon::process(QCommandLineParser &_parser, const QString &_cache,
QVariant value
= QueuedctlTask::getTask(args.at(1).toLongLong(), args.at(2));
result.status = value.isValid();
result.output = value.toString();
result.output = args.at(2).isEmpty() ? hashToString(value.toHash())
: value.toString();
break;
}
case QueuedctlArgument::TaskSet: {
@ -175,13 +266,41 @@ QueuedctlCommon::process(QCommandLineParser &_parser, const QString &_cache,
definitions, token);
break;
}
case QueuedctlArgument::TaskStart: {
QString token = QueuedctlAuth::getToken(_cache, _user);
result.status
= QueuedctlTask::startTask(args.at(1).toLongLong(), token);
break;
}
case QueuedctlArgument::TaskStop: {
QString token = QueuedctlAuth::getToken(_cache, _user);
result.status = QueuedctlTask::stopTask(args.at(1).toLongLong(), token);
break;
}
case QueuedctlArgument::UserAdd: {
QString token = QueuedctlAuth::getToken(_cache, _user);
auto definitions = QueuedctlUser::getDefinitions(_parser, false);
long long id = QueuedctlUser::addUser(definitions, token);
result.status = (id > 0);
if (result.status)
result.output = QString("User %1 added").arg(id);
else
result.output = QString("Could not add user");
break;
}
case QueuedctlArgument::UserGet: {
auto userId = QueuedctlUser::getUserId(args.at(1));
QVariant value = QueuedctlUser::getUser(userId, args.at(2));
result.status = value.isValid();
result.output = args.at(2).isEmpty() ? hashToString(value.toHash())
: value.toString();
break;
}
case QueuedctlArgument::UserSet: {
auto userId = QueuedctlUser::getUserId(args.at(1));
QString token = QueuedctlAuth::getToken(_cache, _user);
auto definitions = QueuedctlUser::getDefinitions(_parser, true);
result.status = QueuedctlUser::setUser(userId, definitions, token);
break;
}
case QueuedctlArgument::Invalid: {

View File

@ -28,9 +28,16 @@ enum class QueuedctlArgument {
Auth,
OptionGet,
OptionSet,
PermissionAdd,
PermissionRemove,
PluginAdd,
PluginList,
PluginRemove,
TaskAdd,
TaskGet,
TaskSet,
TaskStart,
TaskStop,
UserAdd,
UserGet,
UserSet
@ -44,20 +51,32 @@ typedef struct {
QString description;
int positionalArgsCount;
} QueuedctlArgumentInfo;
const QHash<QString, QueuedctlArgumentInfo> QueuedctlArguments
= {{"auth", {QueuedctlArgument::Auth, "Gets new auth token.", 1}},
{"option-get", {QueuedctlArgument::OptionGet, "Gets option value.", 2}},
{"option-set", {QueuedctlArgument::OptionSet, "Sets option value.", 3}},
{"task-add", {QueuedctlArgument::TaskAdd, "Adds new task.", 2}},
{"task-get", {QueuedctlArgument::TaskGet, "Gets task properties.", 3}},
{"task-set", {QueuedctlArgument::TaskSet, "Sets task properties.", 2}},
{"user-add", {QueuedctlArgument::UserAdd, "Adds new user."}},
{"user-get", {QueuedctlArgument::UserGet, "Gets user properties."}},
{"user-set", {QueuedctlArgument::UserSet, "Sets user properties."}}};
const QHash<QString, QueuedctlArgumentInfo> QueuedctlArguments = {
{"auth", {QueuedctlArgument::Auth, "Gets new auth token.", 1}},
{"option-get", {QueuedctlArgument::OptionGet, "Gets option value.", 2}},
{"option-set", {QueuedctlArgument::OptionSet, "Sets option value.", 3}},
{"perm-add",
{QueuedctlArgument::PermissionAdd, "Sets user permission.", 3}},
{"perm-remove",
{QueuedctlArgument::PermissionRemove, "Removes user permission.", 3}},
{"plugin-add", {QueuedctlArgument::PluginAdd, "Adds plugin to load.", 2}},
{"plugin-list",
{QueuedctlArgument::PluginList, "Shows enabled plugins.", 1}},
{"plugin-remove",
{QueuedctlArgument::PluginRemove, "Removes plugin to load.", 2}},
{"task-add", {QueuedctlArgument::TaskAdd, "Adds new task.", 2}},
{"task-get", {QueuedctlArgument::TaskGet, "Gets task properties.", 3}},
{"task-set", {QueuedctlArgument::TaskSet, "Sets task properties.", 2}},
{"task-start", {QueuedctlArgument::TaskStart, "Starts task.", 2}},
{"task-stop", {QueuedctlArgument::TaskStop, "Stops task.", 2}},
{"user-add", {QueuedctlArgument::UserAdd, "Adds new user.", 2}},
{"user-get", {QueuedctlArgument::UserGet, "Gets user properties.", 3}},
{"user-set", {QueuedctlArgument::UserSet, "Sets user properties.", 2}}};
// methods
void checkArgs(const QStringList &_args, const int _count,
QCommandLineParser &_parser);
QString commandsHelp();
QString hashToString(const QVariantHash &_hash);
void preprocess(const QStringList &_args, QCommandLineParser &_parser);
void print(const QueuedctlResult &_result);
QueuedctlResult process(QCommandLineParser &_parser, const QString &_cache,

View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2017 Evgeniy Alekseev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#include "QueuedctlPermissions.h"
#include <queued/Queued.h>
bool QueuedctlPermissions::addPermission(const long long _id,
const QString &_permission,
const QString &_token)
{
qCDebug(LOG_APP) << "Add permission" << _permission << "to" << _id;
auto permission = QueuedEnums::stringToPermission(_permission);
if (permission != QueuedEnums::Permission::Invalid)
return QueuedCoreAdaptor::sendUserPermissionAdd(_id, permission,
_token);
else
return false;
}
bool QueuedctlPermissions::removePermission(const long long _id,
const QString &_permission,
const QString &_token)
{
qCDebug(LOG_APP) << "Remove permission" << _permission << "to" << _id;
auto permission = QueuedEnums::stringToPermission(_permission);
if (permission != QueuedEnums::Permission::Invalid)
return QueuedCoreAdaptor::sendUserPermissionRemove(_id, permission,
_token);
else
return false;
}
void QueuedctlPermissions::parser(QCommandLineParser &_parser)
{
_parser.addPositionalArgument("user", "User ID.", "<user>");
_parser.addPositionalArgument("permission", "Permission name.",
"<permission>");
}

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2017 Evgeniy Alekseev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#ifndef QUEUEDCTLPERMISSIONS_H
#define QUEUEDCTLPERMISSIONS_H
#include <QCommandLineParser>
namespace QueuedctlPermissions
{
bool addPermission(const long long _id, const QString &_permission,
const QString &_token);
bool removePermission(const long long _id, const QString &_permission,
const QString &_token);
void parser(QCommandLineParser &_parser);
};
#endif /* QUEUEDCTLPERMISSIONS_H */

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2017 Evgeniy Alekseev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#include "QueuedctlPlugins.h"
#include <queued/Queued.h>
bool QueuedctlPlugins::addPlugin(const QString &_plugin, const QString &_token)
{
qCDebug(LOG_APP) << "Add plugin" << _plugin;
return QueuedCoreAdaptor::sendPluginAdd(_plugin, _token);
}
QStringList QueuedctlPlugins::listPlugins()
{
return QueuedCoreAdaptor::getOption(
QueuedAdvancedSettings::internalId(
QueuedConfig::QueuedSettings::Plugins))
.toString()
.split('\n');
}
bool QueuedctlPlugins::removePlugin(const QString &_plugin,
const QString &_token)
{
qCDebug(LOG_APP) << "Remove plugin" << _plugin;
return QueuedCoreAdaptor::sendPluginRemove(_plugin, _token);
}
void QueuedctlPlugins::parser(QCommandLineParser &_parser)
{
_parser.addPositionalArgument("plugin", "Plugin name.", "<plugin>");
}

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2017 Evgeniy Alekseev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#ifndef QUEUEDCTLPLUGINS_H
#define QUEUEDCTLPLUGINS_H
#include <QCommandLineParser>
namespace QueuedctlPlugins
{
bool addPlugin(const QString &_plugin, const QString &_token);
QStringList listPlugins();
bool removePlugin(const QString &_plugin, const QString &_token);
void parser(QCommandLineParser &_parser);
};
#endif /* QUEUEDCTLPLUGINS_H */

View File

@ -20,6 +20,12 @@
#include <queued/Queued.h>
#include "QueuedctlUser.h"
extern "C" {
#include <unistd.h>
}
long long QueuedctlTask::addTask(
const QueuedProcess::QueuedProcessDefinitions &_definitions,
@ -48,7 +54,10 @@ QueuedctlTask::getDefinitions(const QCommandLineParser &_parser,
});
definitions.nice = _parser.value("nice").toUInt();
definitions.user = _parser.value("task-user").toLongLong();
definitions.user
= _parser.value("task-user").isEmpty()
? 0
: QueuedctlUser::getUserId(_parser.value("task-user"));
definitions.workingDirectory = _parser.value("directory");
// limits now
QueuedLimits::Limits limits(
@ -68,6 +77,9 @@ QueuedctlTask::getDefinitions(const QCommandLineParser &_parser,
definitions.startTime
= QDateTime::fromString(_parser.value("start"), Qt::ISODate);
definitions.uid = _parser.value("uid").toUInt();
} else {
// queuedctl -- task-add /path/to/application
definitions.command = _parser.positionalArguments().at(1);
}
return definitions;
@ -78,7 +90,11 @@ QVariant QueuedctlTask::getTask(const long long _id, const QString &_property)
{
qCDebug(LOG_APP) << "Get property" << _property << "from task" << _id;
return QueuedCoreAdaptor::getTask(_id, _property);
auto value = QueuedCoreAdaptor::getTask(_id, _property);
if (_property.isEmpty())
return qdbus_cast<QVariantHash>(value.value<QDBusArgument>());
else
return value;
}
@ -97,8 +113,8 @@ void QueuedctlTask::parserAdd(QCommandLineParser &_parser)
"directory", QDir::currentPath());
_parser.addOption(directoryOption);
// user
// TODO grab used user ID
QCommandLineOption userOption("task-user", "Task user.", "task-user", "0");
QCommandLineOption userOption("task-user", "Task user.", "task-user",
::getlogin());
_parser.addOption(userOption);
// nice
QCommandLineOption niceOption("nice", "Task nice level.", "nice", "0");
@ -153,7 +169,7 @@ void QueuedctlTask::parserSet(QCommandLineParser &_parser)
"directory", "Command working directory.", "directory", "");
_parser.addOption(directoryOption);
// user
QCommandLineOption userOption("task-user", "Task user.", "task-user", "0");
QCommandLineOption userOption("task-user", "Task user.", "task-user", "");
_parser.addOption(userOption);
// nice
QCommandLineOption niceOption("nice", "Task nice level.", "nice", "0");
@ -193,6 +209,12 @@ void QueuedctlTask::parserSet(QCommandLineParser &_parser)
}
void QueuedctlTask::parserStart(QCommandLineParser &_parser)
{
_parser.addPositionalArgument("id", "Task ID.", "<id>");
}
bool QueuedctlTask::setTask(
const long long _id,
const QueuedProcess::QueuedProcessDefinitions &_definitions,
@ -202,3 +224,19 @@ bool QueuedctlTask::setTask(
return QueuedCoreAdaptor::sendTaskEdit(_id, _definitions, _token);
}
bool QueuedctlTask::startTask(const long long _id, const QString &_token)
{
qCDebug(LOG_APP) << "Start task" << _id;
return QueuedCoreAdaptor::sendTaskStart(_id, _token);
}
bool QueuedctlTask::stopTask(const long long _id, const QString &_token)
{
qCDebug(LOG_APP) << "Stop task" << _id;
return QueuedCoreAdaptor::sendTaskStop(_id, _token);
}

View File

@ -32,9 +32,12 @@ QVariant getTask(const long long _id, const QString &_property);
void parserAdd(QCommandLineParser &_parser);
void parserGet(QCommandLineParser &_parser);
void parserSet(QCommandLineParser &_parser);
void parserStart(QCommandLineParser &_parser);
bool setTask(const long long _id,
const QueuedProcess::QueuedProcessDefinitions &_definitions,
const QString &_token);
bool startTask(const long long _id, const QString &_token);
bool stopTask(const long long _id, const QString &_token);
};

View File

@ -0,0 +1,219 @@
/*
* Copyright (c) 2017 Evgeniy Alekseev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#include "QueuedctlUser.h"
#include <queued/Queued.h>
extern "C" {
#include <termios.h>
#include <unistd.h>
}
long long
QueuedctlUser::addUser(const QueuedUser::QueuedUserDefinitions &_definitions,
const QString &_token)
{
qCDebug(LOG_APP) << "Add user" << _definitions.name;
return QueuedCoreAdaptor::sendUserAdd(_definitions, _token);
}
QueuedUser::QueuedUserDefinitions
QueuedctlUser::getDefinitions(const QCommandLineParser &_parser,
const bool _expandAll)
{
qCDebug(LOG_APP) << "Parse user definitions from parser, expand all"
<< _expandAll;
QueuedUser::QueuedUserDefinitions definitions;
definitions.email = _parser.value("email");
definitions.password = _parser.isSet("stdin-password")
? getPassword()
: _parser.value("password");
// limits now
QueuedLimits::Limits limits(
_parser.value("limit-cpu").toLongLong(),
_parser.value("limit-gpu").toLongLong(),
QueuedLimits::convertMemory(_parser.value("limit-memory")),
QueuedLimits::convertMemory(_parser.value("limit-gpumemory")),
QueuedLimits::convertMemory(_parser.value("limit-storage")));
definitions.limits = limits.toString();
// all options
if (_expandAll) {
definitions.name = _parser.value("name");
} else {
definitions.permissions = _parser.value("access").toUInt();
// queuedctl -- user-add username
definitions.name = _parser.positionalArguments().at(1);
}
return definitions;
}
QString QueuedctlUser::getPassword()
{
// do not show input characters
struct termios tty;
::tcgetattr(STDIN_FILENO, &tty);
tty.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &tty);
qInfo() << "Password";
QTextStream stream(stdin);
QString password;
stream >> password;
return password;
}
QVariant QueuedctlUser::getUser(const long long _id, const QString &_property)
{
qCDebug(LOG_APP) << "Get property" << _property << "from user" << _id;
auto value = QueuedCoreAdaptor::getUser(_id, _property);
if (_property.isEmpty())
return qdbus_cast<QVariantHash>(value.value<QDBusArgument>());
else
return value;
}
long long QueuedctlUser::getUserId(const QString &_name)
{
qCDebug(LOG_APP) << "Get user ID for" << _name;
bool status = false;
long long stringToLong = _name.toLongLong(&status);
if (status)
return stringToLong;
else
return QueuedCoreAdaptor::getUserId(_name);
}
void QueuedctlUser::parserAdd(QCommandLineParser &_parser)
{
_parser.addPositionalArgument("name", "User name.", "<name>");
// permissions
QCommandLineOption accessOption(QStringList() << "a"
<< "access",
"User permission.", "access", "0");
_parser.addOption(accessOption);
// email
QCommandLineOption emailOption(QStringList() << "e"
<< "email",
"User email.", "email", "");
_parser.addOption(emailOption);
// password
QCommandLineOption passwordOption("password", "User password.", "password",
"");
_parser.addOption(passwordOption);
// password
QCommandLineOption stdinPasswordOption("stdin-password",
"User password from stdin.");
_parser.addOption(stdinPasswordOption);
// cpu limit
QCommandLineOption cpuOption("limit-cpu", "User CPU limit.", "limit-cpu",
"0");
_parser.addOption(cpuOption);
// gpu limit
QCommandLineOption gpuOption("limit-gpu", "User GPU limit.", "limit-gpu",
"0");
_parser.addOption(gpuOption);
// memory limit
QCommandLineOption memoryOption("limit-memory", "User memory limit.",
"limit-memory", "0");
_parser.addOption(memoryOption);
// gpu memory limit
QCommandLineOption gpumemoryOption(
"limit-gpumemory", "User GPU memory limit.", "limit-gpumemory", "0");
_parser.addOption(gpumemoryOption);
// storage limit
QCommandLineOption storageOption("limit-storage", "User storage limit.",
"limit-storage", "0");
_parser.addOption(storageOption);
}
void QueuedctlUser::parserGet(QCommandLineParser &_parser)
{
_parser.addPositionalArgument("id", "User ID.", "<id>");
_parser.addPositionalArgument("property", "User property name.",
"<property>");
}
void QueuedctlUser::parserSet(QCommandLineParser &_parser)
{
_parser.addPositionalArgument("id", "User ID.", "<id>");
// email
QCommandLineOption emailOption(QStringList() << "e"
<< "email",
"User email.", "email", "");
_parser.addOption(emailOption);
// name
QCommandLineOption nameOption(QStringList() << "n"
<< "name",
"User name.", "name", "");
_parser.addOption(nameOption);
// password
QCommandLineOption passwordOption("password", "User password.", "password",
"");
_parser.addOption(passwordOption);
// password
QCommandLineOption stdinPasswordOption("stdin-password",
"User password from stdin.");
_parser.addOption(stdinPasswordOption);
// cpu limit
QCommandLineOption cpuOption("limit-cpu", "User CPU limit.", "limit-cpu",
"0");
_parser.addOption(cpuOption);
// gpu limit
QCommandLineOption gpuOption("limit-gpu", "User GPU limit.", "limit-gpu",
"0");
_parser.addOption(gpuOption);
// memory limit
QCommandLineOption memoryOption("limit-memory", "User memory limit.",
"limit-memory", "0");
_parser.addOption(memoryOption);
// gpu memory limit
QCommandLineOption gpumemoryOption(
"limit-gpumemory", "User GPU memory limit.", "limit-gpumemory", "0");
_parser.addOption(gpumemoryOption);
// storage limit
QCommandLineOption storageOption("limit-storage", "User storage limit.",
"limit-storage", "0");
_parser.addOption(storageOption);
}
bool QueuedctlUser::setUser(
const long long _id, const QueuedUser::QueuedUserDefinitions &_definitions,
const QString &_token)
{
qCDebug(LOG_APP) << "Edit user" << _id;
return QueuedCoreAdaptor::sendUserEdit(_id, _definitions, _token);
}

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2017 Evgeniy Alekseev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#ifndef QUEUEDCTLUSER_H
#define QUEUEDCTLUSER_H
#include <QCommandLineParser>
#include <queued/QueuedUser.h>
namespace QueuedctlUser
{
long long addUser(const QueuedUser::QueuedUserDefinitions &_definitions,
const QString &_token);
QueuedUser::QueuedUserDefinitions
getDefinitions(const QCommandLineParser &_parser, const bool _expandAll);
QString getPassword();
QVariant getUser(const long long _id, const QString &_property);
long long getUserId(const QString &_name);
void parserAdd(QCommandLineParser &_parser);
void parserGet(QCommandLineParser &_parser);
void parserSet(QCommandLineParser &_parser);
bool setUser(const long long _id,
const QueuedUser::QueuedUserDefinitions &_definitions,
const QString &_token);
};
#endif /* QUEUEDCTLUSER_H */