-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatasource.cpp
213 lines (173 loc) · 5.9 KB
/
datasource.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "datasource.h"
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonArray>
#include <QProcess>
#include <QDir>
#define UPDATE_INTERVAL 5000
/*
The VM info will be provided by command line util vmd-cli.
Usage: vmd-client [OPTIONS] --hostname <HOSTNAME> --port <PORT> --cacert <CACERT> --key <KEY> --cert <CERT> --output <OUTPUT> <COMMAND>
Commands:
list
info
action
help Print this message or the help of the given subcommand(s)
Options:
--hostname <HOSTNAME>
-p, --port <PORT>
--cacert <CACERT>
--key <KEY>
--cert <CERT>
--verbose
--output <OUTPUT> [possible values: json, yaml, text]
-h, --help Print help
-V, --version Print version
For testing purposes the test.txt file is used (#DEFINES += TEST in .pro file must uncommented).
This file must be in the same directory as executable file. The template is shown below:
<VM_name> <status>
*/
#define RUN_CLI "./result/bin/vmd-client"
/*/
#define RUN_CLI "./vmd-client \
--hostname localhost \
--port 8080 \
--cacert ./test/auth/certs/sample-ca-crt.pem \
--cert ./test/auth/certs/sample-vmd-client-chain.pem \
--key ./test/auth/certs/sample-vmd-client-key.pem \
--output text \
"
//*/
DataSource::DataSource(QObject *parent)
: QObject{parent}
{
QObject::connect(&updateModelTimer, &QTimer::timeout, this, &DataSource::updateModel);
updateModelTimer.start(UPDATE_INTERVAL);
QObject::connect(this, &DataSource::listReady, this, &DataSource::fillInTheModelSlot);
}
//TODO: login mechanism
void DataSource::loginRequest(const QString &passwd)
{
Q_UNUSED(passwd)
}
void DataSource::pinRequest(const QString &number)
{
Q_UNUSED(number)
}
void DataSource::pinSubmit(const QString &code)
{
Q_UNUSED(code)
}
///////////////////////////////////////////////////////
void DataSource::updateModel()
{
#ifdef TEST
QString vmInfo = execCommand("cat test.txt");
vmInfo = vmInfo.trimmed();
//parse the execution result and add to the model
QTextStream stream (&vmInfo, QIODevice::ReadOnly);
while(!stream.atEnd()) {
QString temp1, temp2;
stream >> temp1 >> temp2;
mVMDataModel.addData(Parameter(temp1, temp1, temp2));
}
#else
qDebug() << "updateModel()";
//! get the ID's list
runCLI(Commands::List);
#endif
}
void DataSource::fillInTheModelSlot(QStringList list)
{
for (const QString &id: list)
{
//runCLI(Commands::Info, id);
mVMDataModel.addData(Parameter(id, "vm" + id, "running"));//for testing purposes all of the VMs marked as running
}
}
void DataSource::fillInTheModelItemInfoSlot(QByteArray info)
{
//parse & add
//mVMDataModel.addData(Parameter(arg, "vm" + arg, "running"));//for testing purposes all of the VMs marked as running
}
void DataSource::setVmdDir(const QString &newVmdDir)
{
qDebug() << "setVmdDir() " << newVmdDir;
vmdDir = newVmdDir;
updateModel();
}
void DataSource::switchPower(bool on, QString name)
{
runCLI(Commands::Action, QStringList() << name << (on? "start" : "stop"));
}
void DataSource::saveSettings()
{
}
//put it into separate class as statuc method VMDCLI::run(Commands cmd, QStringList args) ?
void DataSource::runCLI(Commands cmd, QStringList args)
{
QProcess * process = new QProcess(this);
process->setReadChannel(QProcess::StandardOutput);
process->setProcessChannelMode(QProcess::MergedChannels);
QObject::connect(process, &QProcess::stateChanged, [process] {qDebug() << process->processId() << process->processEnvironment().toStringList() << process->state() << process->errorString();});
if (!vmdDir.isEmpty())
process->setWorkingDirectory(vmdDir);
//or set the directory for the whole app
// QDir::setCurrent(vmdDir);
qDebug() << "runCLI in " << process->workingDirectory();
QStringList allArgs;
allArgs << "--hostname" << "localhost" << "--port" << "8080" << "--cacert" << "test/auth/certs/sample-ca-crt.pem" << "--cert"
<< "test/auth/certs/sample-vmd-client-chain.pem" << "--key" << "test/auth/certs/sample-vmd-client-key.pem" << "--verbose" << "--output" << "text";
switch(cmd)
{
case Commands::List:
{
qDebug() << "List cmd";
allArgs << "list";
QObject::connect(process, &QProcess::readyRead, [process, this] () {
QByteArray a = process->readAllStandardOutput();
QStringList list = QString(a).split(QLatin1Char(','), Qt::SkipEmptyParts);
qDebug() << "list ready" << a;
emit listReady(list);
});
}
break;
case Commands::Info:
{
allArgs << "info" << args;
QObject::connect(process, &QProcess::readyRead, [process, this] () {
QByteArray a = process->readAllStandardOutput();
emit infoReady(a);
});
}
break;
case Commands::Action:
{
allArgs << "action" << args;
}
break;
}
QObject::connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[=](int exitCode, QProcess::ExitStatus /*exitStatus*/){
qDebug()<< "process exited with code " << exitCode;
process->deleteLater();
});
process->start(QString(RUN_CLI), allArgs, QIODevice::ReadOnly);
qDebug() << "ARGS: " << process->arguments() << process->workingDirectory() << process->program();
}
#ifdef TEST
QString DataSource::execCommand(const QString &cmd)
{
std::array<char, 128> buffer;
std::string result;
auto pipe = popen(cmd.toLocal8Bit().data(), "r");
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe)) {
if (fgets(buffer.data(), 128, pipe) != nullptr)
result += buffer.data();
}
auto rc = pclose(pipe);
qDebug() << QString::fromStdString(result) << ", code is " << rc;
return QString::fromStdString(result);
}
#endif