start on queuedctl realization

This commit is contained in:
2017-03-13 03:37:52 +03:00
parent a2872e7c23
commit 80c6f977d7
23 changed files with 436 additions and 24 deletions

View File

@ -0,0 +1,13 @@
# set project name
set (SUBPROJECT "queuedctl")
message (STATUS "Subproject ${SUBPROJECT}")
add_subdirectory ("src")
# build man
file (GLOB SUBPROJECT_MAN_IN "*.1")
file (RELATIVE_PATH SUBPROJECT_MAN "${CMAKE_SOURCE_DIR}" "${SUBPROJECT_MAN_IN}")
configure_file ("${SUBPROJECT_MAN_IN}" "${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT_MAN}")
install (FILES "${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT_MAN}" DESTINATION "${DATA_INSTALL_DIR}/man/man1")
install (FILES "bash-completions" DESTINATION "${DATA_INSTALL_DIR}/bash-completion/completions" RENAME "${SUBPROJECT}")
install (FILES "zsh-completions" DESTINATION "${DATA_INSTALL_DIR}/zsh/site-functions" RENAME "_${SUBPROJECT}")

View File

View File

View File

@ -0,0 +1,18 @@
# set files
file (GLOB_RECURSE SUBPROJECT_SOURCES "*.cpp")
file (GLOB_RECURSE SUBPROJECT_HEADERS "*.h")
# include_path
include_directories ("${PROJECT_LIBRARY_DIR}/include"
"${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_BINARY_DIR}"
"${PROJECT_TRDPARTY_DIR}"
"${Qt_INCLUDE}")
qt5_wrap_cpp (SUBPROJECT_MOC_SOURCES "${SUBPROJECT_HEADERS}")
add_executable ("${SUBPROJECT}" "${SUBPROJECT_HEADERS}" "${SUBPROJECT_SOURCES}"
"${SUBPROJECT_MOC_SOURCES}")
target_link_libraries ("${SUBPROJECT}" "${PROJECT_LIBRARY}" "${Qt_LIBRARIES}")
# install properties
install (TARGETS "${SUBPROJECT}" DESTINATION "${BIN_INSTALL_DIR}")

View File

@ -0,0 +1,104 @@
/*
* Copyright (c) 2016 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 "QueuedctlAuth.h"
#include <QSettings>
#include <queued/Queued.h>
extern "C" {
#include <termios.h>
#include <unistd.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);
}
QString QueuedctlAuth::getToken(const QString &_cache, const QString &_user)
{
qCDebug(LOG_APP) << "Get token using cache" << _cache << "and user"
<< _user;
QString tokenId = token(_cache);
if (tryAuth(tokenId)) {
return tokenId;
} else {
tokenId = auth(_user);
setToken(tokenId, _cache);
return getToken(_cache, _user);
}
}
void QueuedctlAuth::parser(QCommandLineParser &_parser)
{
_parser.clearPositionalArguments();
}
void QueuedctlAuth::setToken(const QString &_token, const QString &_cache)
{
qCDebug(LOG_APP) << "Save token to" << _cache;
QSettings settings(_cache, QSettings::IniFormat);
settings.beginGroup("queuedctl");
settings.setValue("Token", _token);
settings.endGroup();
settings.sync();
}
QString QueuedctlAuth::token(const QString &_cache)
{
qCDebug(LOG_APP) << "Load token from" << _cache;
QString token;
QSettings settings(_cache, QSettings::IniFormat);
settings.beginGroup("queuedctl");
token = settings.value("Token").toString();
settings.endGroup();
return token;
}
bool QueuedctlAuth::tryAuth(const QString &_token)
{
qCDebug(LOG_APP) << "Try auth with" << _token;
return QueuedCoreAdaptor::auth(_token);
}

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2016 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 QUEUEDCTLAUTH_H
#define QUEUEDCTLAUTH_H
#include <QCommandLineParser>
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);
bool tryAuth(const QString &_token);
};
#endif /* QUEUEDCTLAUTH_H */

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2016 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 "QueuedctlOption.h"
#include <queued/Queued.h>
#include "QueuedctlAuth.h"
bool QueuedctlOption::editOption(const QString &_option, const QVariant &_value,
const QString &_cache, const QString &_user)
{
qCDebug(LOG_APP) << "Edit option" << _option << "to" << _value;
QString token = QueuedctlAuth::getToken(_cache, _user);
return QueuedCoreAdaptor::sendOptionEdit(_option, _value, token);
}
void QueuedctlOption::parser(QCommandLineParser &_parser)
{
_parser.clearPositionalArguments();
_parser.addPositionalArgument("option", "Option name.");
_parser.addPositionalArgument("value", "Option value.");
}

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2016 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 QUEUEDCTLOPTION_H
#define QUEUEDCTLOPTION_H
#include <QCommandLineParser>
namespace QueuedctlOption
{
bool editOption(const QString &_option, const QVariant &_value,
const QString &_cache, const QString &_user);
void parser(QCommandLineParser &_parser);
};
#endif /* QUEUEDCTLOPTION_H */

View File

@ -0,0 +1,107 @@
/*
* Copyright (c) 2016 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 <QCommandLineParser>
#include <QCoreApplication>
#include <queued/Queued.h>
#include "QueuedctlAuth.h"
#include "QueuedctlOption.h"
#include "version.h"
extern "C" {
#include <unistd.h>
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
app.setApplicationName(NAME);
app.setApplicationVersion(VERSION);
// parser
QCommandLineParser parser;
parser.setApplicationDescription(
"Daemon for starting jobs to queue of calculations");
parser.addHelpOption();
parser.addVersionOption();
// info
QCommandLineOption infoOption(QStringList() << "i"
<< "info",
"Show additional info.");
parser.addOption(infoOption);
// debug mode
QCommandLineOption debugOption(QStringList() << "d"
<< "debug",
"Print debug information.");
parser.addOption(debugOption);
// configuration option
QCommandLineOption tokenOption(QStringList() << "t"
<< "token",
"Path to cached token.", "token",
QueuedSettings::defaultTokenPath());
parser.addOption(tokenOption);
QCommandLineOption userOption(QStringList() << "u"
<< "user",
"User to login instead of current one.",
"user", ::getlogin());
parser.addOption(userOption);
parser.addPositionalArgument("command", "Command to execute.");
// pre-parse
parser.parse(QCoreApplication::arguments());
QStringList args = parser.positionalArguments();
QString command = args.isEmpty() ? QString() : args.first();
if (command == "auth") {
QueuedctlAuth::parser(parser);
} else if (command == "option-edit") {
QueuedctlOption::parser(parser);
} else if (command == "task-add") {
parser.clearPositionalArguments();
} else if (command == "task-edit") {
parser.clearPositionalArguments();
} else if (command == "user-add") {
parser.clearPositionalArguments();
} else if (command == "user-edit") {
parser.clearPositionalArguments();
} else {
parser.process(app);
qWarning() << "Unknown command" << command;
parser.showHelp(1);
}
parser.process(app);
// show info and exit
if (parser.isSet(infoOption)) {
auto metadata = QueuedDebug::getBuildData();
for (auto &string : metadata)
QDebug(QtMsgType::QtInfoMsg).noquote() << string;
return 0;
}
// enable debug
if (parser.isSet(debugOption))
QueuedDebug::enableDebug();
return 0;
}

View File