add environment value parsing

This commit is contained in:
Syping 2020-10-16 05:38:18 +02:00
parent a9fb05c5a9
commit d96bc26b97

411
main.cpp
View file

@ -79,158 +79,314 @@ int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
a.setApplicationName("Server Manager Subprocess");
a.setApplicationVersion("0.3.1");
a.setApplicationVersion("0.4");
#ifdef Q_OS_UNIX
catchUnixSignals({SIGINT, SIGHUP, SIGQUIT, SIGTERM});
#endif
QCommandLineParser commandLineParser;
commandLineParser.addHelpOption();
commandLineParser.addVersionOption();
QCommandLineOption processManifest("json", "JSON process manifest.", "json");
commandLineParser.addOption(processManifest);
QCommandLineOption processExecutable(QStringList() << "exec" << "executable", "Process executable to run.", "exec");
commandLineParser.addOption(processExecutable);
QCommandLineOption processArguments(QStringList() << "args" << "arguments", "Arguments given to process.", "args");
commandLineParser.addOption(processArguments);
#ifdef Q_OS_WIN
QCommandLineOption subprocessSocket(QStringList() << "sock" << "socket", "IPC socket used for local communication.", "sock");
#else
QCommandLineOption subprocessSocket(QStringList() << "sock" << "socket", "Unix socket used for local communication.", "sock");
#endif
commandLineParser.addOption(subprocessSocket);
QCommandLineOption subprocessRemotePort("rport", "WebSockets port used for remote communication.", "rport");
commandLineParser.addOption(subprocessRemotePort);
#ifdef Q_OS_WIN
QCommandLineOption subprocessRemoteSocket(QStringList() << "rsock" << "rsocket", "IPC socket used for remote communication.", "rsock");
#else
QCommandLineOption subprocessRemoteSocket(QStringList() << "rsock" << "rsocket", "Unix socket used for remote communication.", "rsock");
#endif
commandLineParser.addOption(subprocessRemoteSocket);
QCommandLineOption processTimeout("timeout", "SMSub termination timeout.", "timeout");
commandLineParser.addOption(processTimeout);
commandLineParser.process(a);
if (unlikely(commandLineParser.isSet(processManifest) && commandLineParser.isSet(processExecutable))) {
QTextStream(stderr) << "You can't define a Process executable and a JSON process manifest at the same time!" << smsub_endl;
return 1;
}
if (unlikely(commandLineParser.isSet(subprocessRemotePort) && commandLineParser.isSet(subprocessRemoteSocket))) {
#ifdef Q_OS_WIN
QTextStream(stderr) << "You can't define a WebSockets port and a IPC socket at same time!" << smsub_endl;
#else
QTextStream(stderr) << "You can't define a WebSockets port and a Unix socket at same time!" << smsub_endl;
#endif
return 1;
}
bool rportSet = false;
bool timeoutSet = false;
int termTimeout = 60000;
if (unlikely(commandLineParser.isSet(processTimeout))) {
bool ok;
const int _termTimeout = commandLineParser.value(processTimeout).toInt(&ok);
if (ok) {
termTimeout = _termTimeout;
timeoutSet = true;
}
}
quint16 rport;
QString socket;
QString rsocket;
QString executable;
QString workingDirectory;
QStringList argumentList;
if (likely(commandLineParser.isSet(processManifest))) {
QFile manifestFile(commandLineParser.value(processManifest));
if (likely(manifestFile.open(QIODevice::ReadOnly))) {
const QByteArray jsonData = manifestFile.readAll();
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData);
QJsonObject jsonObject = jsonDocument.object();
if (likely(jsonObject.contains("Executable"))) {
const QJsonValue jsonExecutable = jsonObject.value("Executable");
if (unlikely(!jsonExecutable.isString())) {
QTextStream(stderr) << "Executable is not a string in manifest, aborting!" << smsub_endl;
manifestFile.close();
return 1;
}
executable = jsonExecutable.toString();
QByteArray envOnly = qgetenv("SMSUB_ENV_ONLY");
QByteArray envManifest = qgetenv("SMSUB_JSON");
QByteArray envExecutable = qgetenv("SMSUB_EXEC");
QByteArray envArguments = qgetenv("SMSUB_ARGS");
QByteArray envSocket = qgetenv("SMSUB_SOCK");
QByteArray envRemotePort = qgetenv("SMSUB_RPORT");
QByteArray envRemoteSocket = qgetenv("SMSUB_RSOCK");
QByteArray envTimeout = qgetenv("SMSUB_TIMEOUT");
QByteArray envWorkDir = qgetenv("SMSUB_WORKDIR");
if (unlikely(envOnly == "1" || envOnly.toLower() == "true")) {
if (likely(envExecutable.isEmpty() && envArguments.isEmpty())) {
QStringList arguments = a.arguments();
executable = arguments.takeFirst();
argumentList = arguments;
}
else {
// Not yet
}
if (unlikely(!envTimeout.isEmpty())) {
bool ok;
const int _termTimeout = envTimeout.toInt(&ok);
if (ok) {
termTimeout = _termTimeout;
timeoutSet = true;
}
else {
QTextStream(stderr) << "Executable is not defined in manifest, aborting!" << smsub_endl;
manifestFile.close();
QTextStream(stderr) << "Termination timeout is not a number in environment, aborting!" << smsub_endl;
return 1;
}
}
if (likely(jsonObject.contains("WorkingDirectory"))) {
const QJsonValue jsonWorkingDirectory = jsonObject.value("WorkingDirectory");
if (unlikely(!jsonWorkingDirectory.isString())) {
QTextStream(stderr) << "Working Directory is not a string in manifest, aborting!" << smsub_endl;
manifestFile.close();
return 1;
}
workingDirectory = jsonWorkingDirectory.toString();
if (unlikely(!envWorkDir.isEmpty())) {
workingDirectory = QString::fromUtf8(envWorkDir);
}
else {
workingDirectory = QFileInfo(executable).absolutePath();
}
if (likely(!envSocket.isEmpty())) {
socket = QString::fromUtf8(envSocket);
}
else {
#ifdef Q_OS_WIN
QTextStream(stderr) << "You must define at least a local IPC socket!" << smsub_endl;
#else
QTextStream(stderr) << "You must define at least a local Unix socket!" << smsub_endl;
#endif
return 1;
}
if (unlikely(!envRemoteSocket.isEmpty())) {
rsocket = QString::fromUtf8(envRemoteSocket);
}
if (unlikely(!envRemotePort.isEmpty())) {
bool ok;
rport = envRemotePort.toUShort(&ok);
if (!ok) {
QTextStream(stderr) << "WebSockets port is not valid in environment!" << smsub_endl;
return 1;
}
else {
workingDirectory = QFileInfo(executable).absolutePath();
rportSet = true;
}
}
}
else {
QCommandLineParser commandLineParser;
commandLineParser.addHelpOption();
commandLineParser.addVersionOption();
if (likely(jsonObject.contains("Arguments"))) {
const QJsonValue jsonArguments = jsonObject.value("Arguments");
if (likely(jsonArguments.isArray())) {
const QJsonArray jsonArray = jsonArguments.toArray();
QJsonArray::const_iterator it = jsonArray.constBegin();
QJsonArray::const_iterator end = jsonArray.constEnd();
while (it != end) {
argumentList << it->toString();
it++;
QCommandLineOption processManifest("json", "JSON process manifest.", "json");
commandLineParser.addOption(processManifest);
QCommandLineOption processExecutable(QStringList() << "exec" << "executable", "Process executable to run.", "exec");
commandLineParser.addOption(processExecutable);
QCommandLineOption processArguments(QStringList() << "args" << "arguments", "Arguments given to process.", "args");
commandLineParser.addOption(processArguments);
#ifdef Q_OS_WIN
QCommandLineOption subprocessSocket(QStringList() << "sock" << "socket", "IPC socket used for local communication.", "sock");
#else
QCommandLineOption subprocessSocket(QStringList() << "sock" << "socket", "Unix socket used for local communication.", "sock");
#endif
commandLineParser.addOption(subprocessSocket);
QCommandLineOption subprocessRemotePort("rport", "WebSockets port used for remote communication.", "rport");
commandLineParser.addOption(subprocessRemotePort);
#ifdef Q_OS_WIN
QCommandLineOption subprocessRemoteSocket(QStringList() << "rsock" << "rsocket", "IPC socket used for remote communication.", "rsock");
#else
QCommandLineOption subprocessRemoteSocket(QStringList() << "rsock" << "rsocket", "Unix socket used for remote communication.", "rsock");
#endif
commandLineParser.addOption(subprocessRemoteSocket);
QCommandLineOption processTimeout("timeout", "SMSub termination timeout.", "timeout");
commandLineParser.addOption(processTimeout);
commandLineParser.process(a);
if (unlikely(commandLineParser.isSet(processManifest) && commandLineParser.isSet(processExecutable) ||
!envManifest.isEmpty() && !envExecutable.isEmpty() ||
commandLineParser.isSet(processManifest) && !envExecutable.isEmpty() ||
!envManifest.isEmpty() && commandLineParser.isSet(processExecutable))) {
QTextStream(stderr) << "You can't define a Process executable and a JSON process manifest at the same time!" << smsub_endl;
return 1;
}
if (unlikely(commandLineParser.isSet(processManifest) && commandLineParser.isSet(processArguments) ||
!envManifest.isEmpty() && !envArguments.isEmpty() ||
commandLineParser.isSet(processManifest) && !envArguments.isEmpty() ||
!envManifest.isEmpty() && commandLineParser.isSet(processArguments))) {
QTextStream(stderr) << "You can't define a Process arguments and a JSON process manifest at the same time!" << smsub_endl;
return 1;
}
if (unlikely(commandLineParser.isSet(subprocessRemotePort) && commandLineParser.isSet(subprocessRemoteSocket) ||
!envRemotePort.isEmpty() && !envRemoteSocket.isEmpty() ||
commandLineParser.isSet(subprocessRemotePort) && !envRemoteSocket.isEmpty() ||
!envRemotePort.isEmpty() && commandLineParser.isSet(subprocessRemoteSocket))) {
#ifdef Q_OS_WIN
QTextStream(stderr) << "You can't define a WebSockets port and a IPC socket at same time!" << smsub_endl;
#else
QTextStream(stderr) << "You can't define a WebSockets port and a Unix socket at same time!" << smsub_endl;
#endif
return 1;
}
if (unlikely(commandLineParser.isSet(processTimeout))) {
bool ok;
const int _termTimeout = commandLineParser.value(processTimeout).toInt(&ok);
if (ok) {
termTimeout = _termTimeout;
timeoutSet = true;
}
else {
QTextStream(stderr) << "Termination timeout is not a number in argument, aborting!" << smsub_endl;
return 1;
}
}
else if (unlikely(!envTimeout.isEmpty())) {
bool ok;
const int _termTimeout = envTimeout.toInt(&ok);
if (ok) {
termTimeout = _termTimeout;
timeoutSet = true;
}
else {
QTextStream(stderr) << "Termination timeout is not a number in environment, aborting!" << smsub_endl;
return 1;
}
}
if (unlikely(commandLineParser.isSet(processExecutable))) {
executable = commandLineParser.value(processExecutable);
}
else if (unlikely(!envExecutable.isEmpty())) {
executable = QString::fromUtf8(envExecutable);
}
if (unlikely(!envWorkDir.isEmpty())) {
workingDirectory = QString::fromUtf8(envWorkDir);
}
else {
workingDirectory = QFileInfo(executable).absolutePath();
}
QString manifestPath;
if (likely(commandLineParser.isSet(processManifest))) {
manifestPath = commandLineParser.value(processManifest);
}
else if (!envManifest.isEmpty()) {
manifestPath = QString::fromUtf8(envManifest);
}
if (likely(!manifestPath.isEmpty())) {
QFile manifestFile(manifestPath);
if (likely(manifestFile.open(QIODevice::ReadOnly))) {
const QByteArray jsonData = manifestFile.readAll();
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData);
QJsonObject jsonObject = jsonDocument.object();
if (likely(jsonObject.contains("Executable"))) {
const QJsonValue jsonExecutable = jsonObject.value("Executable");
if (unlikely(!jsonExecutable.isString())) {
QTextStream(stderr) << "Executable is not a string in manifest, aborting!" << smsub_endl;
manifestFile.close();
return 1;
}
executable = jsonExecutable.toString();
}
else {
QTextStream(stderr) << "Arguments is not a array in manifest, aborting!" << smsub_endl;
QTextStream(stderr) << "Executable is not defined in manifest, aborting!" << smsub_endl;
manifestFile.close();
return 1;
}
}
if (unlikely(!timeoutSet && jsonObject.contains("TerminationTimeout"))) {
const QJsonValue jsonTimeout = jsonObject.value("TerminationTimeout");
if (unlikely(!jsonTimeout.isDouble())) {
termTimeout = qRound(jsonTimeout.toDouble());
if (likely(jsonObject.contains("WorkingDirectory"))) {
const QJsonValue jsonWorkingDirectory = jsonObject.value("WorkingDirectory");
if (unlikely(!jsonWorkingDirectory.isString())) {
QTextStream(stderr) << "Working Directory is not a string in manifest, aborting!" << smsub_endl;
manifestFile.close();
return 1;
}
workingDirectory = jsonWorkingDirectory.toString();
}
else {
QTextStream(stderr) << "Termination Timeout is not a number in manifest, aborting!" << smsub_endl;
return 1;
}
}
manifestFile.close();
if (likely(jsonObject.contains("Arguments"))) {
const QJsonValue jsonArguments = jsonObject.value("Arguments");
if (likely(jsonArguments.isArray())) {
const QJsonArray jsonArray = jsonArguments.toArray();
QJsonArray::const_iterator it = jsonArray.constBegin();
QJsonArray::const_iterator end = jsonArray.constEnd();
while (it != end) {
argumentList << it->toString();
it++;
}
}
else {
QTextStream(stderr) << "Arguments is not a array in manifest, aborting!" << smsub_endl;
manifestFile.close();
return 1;
}
}
if (unlikely(!timeoutSet && jsonObject.contains("TerminationTimeout"))) {
const QJsonValue jsonTimeout = jsonObject.value("TerminationTimeout");
if (unlikely(!jsonTimeout.isDouble())) {
termTimeout = qRound(jsonTimeout.toDouble());
}
else {
QTextStream(stderr) << "Termination timeout is not a number in manifest, aborting!" << smsub_endl;
return 1;
}
}
manifestFile.close();
}
}
else if (unlikely(commandLineParser.isSet(processArguments))) {
QTextStream(stderr) << "Arguments over command line are not supported yet!" << smsub_endl;
return 1;
}
}
else if (unlikely(commandLineParser.isSet(processArguments))) {
QTextStream(stderr) << "Arguments over command line are not supported yet!" << smsub_endl;
return 1;
}
QString socket;
if (likely(commandLineParser.isSet(subprocessSocket))) {
socket = commandLineParser.value(subprocessSocket);
}
else {
if (likely(commandLineParser.isSet(subprocessSocket))) {
socket = commandLineParser.value(subprocessSocket);
}
else if (!envSocket.isEmpty()) {
socket = QString::fromUtf8(envSocket);
}
else {
#ifdef Q_OS_WIN
QTextStream(stderr) << "You must define at least a local IPC socket!" << smsub_endl;
QTextStream(stderr) << "You must define at least a local IPC socket!" << smsub_endl;
#else
QTextStream(stderr) << "You must define at least a local Unix socket!" << smsub_endl;
QTextStream(stderr) << "You must define at least a local Unix socket!" << smsub_endl;
#endif
return 1;
return 1;
}
if (unlikely(commandLineParser.isSet(subprocessRemoteSocket))) {
rsocket = commandLineParser.value(subprocessRemoteSocket);
}
else if (unlikely(!envRemoteSocket.isEmpty())) {
rsocket = QString::fromUtf8(envRemoteSocket);
}
if (unlikely(commandLineParser.isSet(subprocessRemotePort))) {
bool ok;
rport = commandLineParser.value(subprocessRemotePort).toUShort(&ok);
if (!ok) {
QTextStream(stderr) << "WebSockets port is not valid in arguments!" << smsub_endl;
return 1;
}
else {
rportSet = true;
}
}
else if (!envRemotePort.isEmpty()) {
bool ok;
rport = envRemotePort.toUShort(&ok);
if (!ok) {
QTextStream(stderr) << "WebSockets port is not valid in environment!" << smsub_endl;
return 1;
}
else {
rportSet = true;
}
}
}
SMSubServerSettings localSettings;
@ -246,25 +402,6 @@ int main(int argc, char *argv[])
return 1;
}
QString rsocket;
if (unlikely(commandLineParser.isSet(subprocessRemoteSocket))) {
rsocket = commandLineParser.value(subprocessRemoteSocket);
}
bool rportSet = false;
quint16 rport;
if (unlikely(commandLineParser.isSet(subprocessRemotePort))) {
bool ok;
rport = commandLineParser.value(subprocessRemotePort).toUShort(&ok);
if (!ok) {
QTextStream(stderr) << "WebSockets port is not valid!" << smsub_endl;
return 1;
}
else {
rportSet = true;
}
}
SMSubProcess subProcess(executable, argumentList, workingDirectory, termTimeout);
SMSubServerSettings remoteSettings;