blob: 4d6903f95254750dee8be6bea397bfd04d887c52 (
plain)
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
|
#include "servicecontrol.h"
#include <QFile>
#include <QDir>
#include <QDebug>
#include <QCoreApplication>
#include <QProcess>
ServiceControl::ServiceControl(QObject *parent) : QObject(parent)
{
}
QString ServiceControl::serviceName() const
{
return m_serviceName;
}
void ServiceControl::setServiceName(const QString &serviceName)
{
if (m_serviceName != serviceName) {
m_serviceName = serviceName;
emit serviceNameChanged();
}
}
bool ServiceControl::serviceFileInstalled() const
{
if (m_serviceName.isEmpty()) {
qDebug() << "Service name not set.";
return false;
}
QFile f(QDir::homePath() + "/.config/upstart/" + m_serviceName + ".conf");
return f.exists();
}
bool ServiceControl::installServiceFile()
{
if (m_serviceName.isEmpty()) {
qDebug() << "Service name not set. Cannot generate service file.";
return false;
}
QFile f(QDir::homePath() + "/.config/upstart/" + m_serviceName + ".conf");
if (f.exists()) {
qDebug() << "Service file already existing...";
return false;
}
if (!f.open(QFile::WriteOnly | QFile::Truncate)) {
qDebug() << "Cannot create service file";
return false;
}
QString appDir = qApp->applicationDirPath();
// Try to replace version with "current" to be more robust against updates
appDir.replace(QRegExp("rockwork.mzanetti\/[0-9.]*\/"), "rockwork.mzanetti/current/");
f.write("start on started unity8\n");
f.write("pre-start script\n");
f.write(" initctl set-env LD_LIBRARY_PATH=" + appDir.toUtf8() + "/../:$LD_LIBRARY_PATH\n");
f.write("end script\n");
f.write("exec " + appDir.toUtf8() + "/" + m_serviceName.toUtf8() + "\n");
f.close();
return true;
}
bool ServiceControl::removeServiceFile()
{
if (m_serviceName.isEmpty()) {
qDebug() << "Service name not set.";
return false;
}
QFile f(QDir::homePath() + "/.config/upstart/" + m_serviceName + ".conf");
return f.remove();
}
bool ServiceControl::serviceRunning() const
{
QProcess p;
p.start("initctl", {"status", m_serviceName});
p.waitForFinished();
QByteArray output = p.readAll();
qDebug() << output;
return output.contains("running");
}
bool ServiceControl::setServiceRunning(bool running)
{
if (running && !serviceRunning()) {
return startService();
} else if (!running && serviceRunning()) {
return stopService();
}
return true; // Requested state is already the current state.
}
bool ServiceControl::startService()
{
qDebug() << "should start service";
int ret = QProcess::execute("start", {m_serviceName});
return ret == 0;
}
bool ServiceControl::stopService()
{
qDebug() << "should stop service";
int ret = QProcess::execute("stop", {m_serviceName});
return ret == 0;
}
bool ServiceControl::restartService()
{
qDebug() << "should stop service";
int ret = QProcess::execute("restart", {m_serviceName});
return ret == 0;
}
|