add environment value parsing
This commit is contained in:
parent
a9fb05c5a9
commit
d96bc26b97
1 changed files with 274 additions and 137 deletions
203
main.cpp
203
main.cpp
|
@ -79,12 +79,91 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QCoreApplication a(argc, argv);
|
QCoreApplication a(argc, argv);
|
||||||
a.setApplicationName("Server Manager Subprocess");
|
a.setApplicationName("Server Manager Subprocess");
|
||||||
a.setApplicationVersion("0.3.1");
|
a.setApplicationVersion("0.4");
|
||||||
|
|
||||||
#ifdef Q_OS_UNIX
|
#ifdef Q_OS_UNIX
|
||||||
catchUnixSignals({SIGINT, SIGHUP, SIGQUIT, SIGTERM});
|
catchUnixSignals({SIGINT, SIGHUP, SIGQUIT, SIGTERM});
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
bool rportSet = false;
|
||||||
|
bool timeoutSet = false;
|
||||||
|
int termTimeout = 60000;
|
||||||
|
quint16 rport;
|
||||||
|
QString socket;
|
||||||
|
QString rsocket;
|
||||||
|
QString executable;
|
||||||
|
QString workingDirectory;
|
||||||
|
QStringList argumentList;
|
||||||
|
|
||||||
|
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) << "Termination timeout is not a number in environment, aborting!" << smsub_endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
rportSet = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
QCommandLineParser commandLineParser;
|
QCommandLineParser commandLineParser;
|
||||||
commandLineParser.addHelpOption();
|
commandLineParser.addHelpOption();
|
||||||
commandLineParser.addVersionOption();
|
commandLineParser.addVersionOption();
|
||||||
|
@ -120,12 +199,26 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
commandLineParser.process(a);
|
commandLineParser.process(a);
|
||||||
|
|
||||||
if (unlikely(commandLineParser.isSet(processManifest) && commandLineParser.isSet(processExecutable))) {
|
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;
|
QTextStream(stderr) << "You can't define a Process executable and a JSON process manifest at the same time!" << smsub_endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unlikely(commandLineParser.isSet(subprocessRemotePort) && commandLineParser.isSet(subprocessRemoteSocket))) {
|
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
|
#ifdef Q_OS_WIN
|
||||||
QTextStream(stderr) << "You can't define a WebSockets port and a IPC socket at same time!" << smsub_endl;
|
QTextStream(stderr) << "You can't define a WebSockets port and a IPC socket at same time!" << smsub_endl;
|
||||||
#else
|
#else
|
||||||
|
@ -134,8 +227,6 @@ int main(int argc, char *argv[])
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool timeoutSet = false;
|
|
||||||
int termTimeout = 60000;
|
|
||||||
if (unlikely(commandLineParser.isSet(processTimeout))) {
|
if (unlikely(commandLineParser.isSet(processTimeout))) {
|
||||||
bool ok;
|
bool ok;
|
||||||
const int _termTimeout = commandLineParser.value(processTimeout).toInt(&ok);
|
const int _termTimeout = commandLineParser.value(processTimeout).toInt(&ok);
|
||||||
|
@ -143,13 +234,48 @@ int main(int argc, char *argv[])
|
||||||
termTimeout = _termTimeout;
|
termTimeout = _termTimeout;
|
||||||
timeoutSet = true;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QString executable;
|
if (unlikely(commandLineParser.isSet(processExecutable))) {
|
||||||
QString workingDirectory;
|
executable = commandLineParser.value(processExecutable);
|
||||||
QStringList argumentList;
|
}
|
||||||
|
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))) {
|
if (likely(commandLineParser.isSet(processManifest))) {
|
||||||
QFile manifestFile(commandLineParser.value(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))) {
|
if (likely(manifestFile.open(QIODevice::ReadOnly))) {
|
||||||
const QByteArray jsonData = manifestFile.readAll();
|
const QByteArray jsonData = manifestFile.readAll();
|
||||||
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData);
|
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData);
|
||||||
|
@ -179,9 +305,6 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
workingDirectory = jsonWorkingDirectory.toString();
|
workingDirectory = jsonWorkingDirectory.toString();
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
workingDirectory = QFileInfo(executable).absolutePath();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (likely(jsonObject.contains("Arguments"))) {
|
if (likely(jsonObject.contains("Arguments"))) {
|
||||||
const QJsonValue jsonArguments = jsonObject.value("Arguments");
|
const QJsonValue jsonArguments = jsonObject.value("Arguments");
|
||||||
|
@ -207,7 +330,7 @@ int main(int argc, char *argv[])
|
||||||
termTimeout = qRound(jsonTimeout.toDouble());
|
termTimeout = qRound(jsonTimeout.toDouble());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
QTextStream(stderr) << "Termination Timeout is not a number in manifest, aborting!" << smsub_endl;
|
QTextStream(stderr) << "Termination timeout is not a number in manifest, aborting!" << smsub_endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -220,10 +343,12 @@ int main(int argc, char *argv[])
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString socket;
|
|
||||||
if (likely(commandLineParser.isSet(subprocessSocket))) {
|
if (likely(commandLineParser.isSet(subprocessSocket))) {
|
||||||
socket = commandLineParser.value(subprocessSocket);
|
socket = commandLineParser.value(subprocessSocket);
|
||||||
}
|
}
|
||||||
|
else if (!envSocket.isEmpty()) {
|
||||||
|
socket = QString::fromUtf8(envSocket);
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
#ifdef Q_OS_WIN
|
#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;
|
||||||
|
@ -233,6 +358,37 @@ int main(int argc, char *argv[])
|
||||||
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;
|
SMSubServerSettings localSettings;
|
||||||
localSettings.isLocal = true;
|
localSettings.isLocal = true;
|
||||||
|
|
||||||
|
@ -246,25 +402,6 @@ int main(int argc, char *argv[])
|
||||||
return 1;
|
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);
|
SMSubProcess subProcess(executable, argumentList, workingDirectory, termTimeout);
|
||||||
|
|
||||||
SMSubServerSettings remoteSettings;
|
SMSubServerSettings remoteSettings;
|
||||||
|
|
Loading…
Reference in a new issue