-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemManager.cpp
More file actions
274 lines (223 loc) · 5.91 KB
/
Copy pathSystemManager.cpp
File metadata and controls
274 lines (223 loc) · 5.91 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "SystemManager.h"
#include <nlohmann/json.hpp>
#include <libutils/FileUtils.h>
#include <libutils/Process.h>
#include <libutils/Logger.h>
#include <libopi/ServiceHelper.h>
#include <libopi/SysConfig.h>
#define SLOG ScopedLog l(__func__)
using namespace Utils;
using json = nlohmann::json;
SystemManager::SystemManager(): providers({"OpenProducts"})
{
logg << Logger::Debug << "SystemManager created" << lend;
this->shellaccess = make_unique<ShellAccess>();
}
SystemManager &SystemManager::Instance()
{
static SystemManager mgr;
return mgr;
}
tuple<bool, string> SystemManager::UpgradeAvailable()
{
SLOG;
if( ! File::FileExists("/usr/share/opi-updates/kgp-checkupgrade") )
{
logg << Logger::Debug << "No upgradescript available" << lend;
return make_tuple(false,"");
}
bool retval = false;
string retoutput;
tie(retval, retoutput) = Process::Exec("/usr/share/opi-updates/kgp-checkupgrade");
if( ! retval )
{
logg << Logger::Debug << "Failed to execute checker script" << lend;
return make_tuple(false,"");
}
json val;
try
{
val = json::parse(retoutput);
}
catch (json::parse_error& err)
{
logg << Logger::Debug << "Unable to parse output of checker script" << lend;
return make_tuple(false,"");
}
if( ! val.contains("status") || !val["status"].is_boolean() )
{
logg << Logger::Debug << "Missing status from checker script" << lend;
return make_tuple(false,"");
}
if( ! val.contains("description") || !val["description"].is_string() )
{
logg << Logger::Debug << "Missing description from checker script" << lend;
return make_tuple(false,"");
}
return make_tuple(val["status"],val["description"]);
}
bool SystemManager::IsConfigured()
{
return OPI::SysConfig().HasKey("hostinfo","hostname");
}
void SystemManager::StartUpgrade()
{
SLOG;
try
{
if( !File::FileExists("/usr/share/opi-updates/kgp-distupgrade") )
{
logg << Logger::Debug << "Upgrade script not available" << lend;
return;
}
Process::Spawn("/usr/share/opi-updates/kgp-distupgrade");
}
catch (ErrnoException& err)
{
logg << Logger::Notice << "Launch of upgrade script failed: " << err.what() << lend;
this->global_error = "Launch of upgrade script failed: "s + err.what();
}
}
void SystemManager::StartUpdate()
{
SLOG;
try
{
if( File::FileExists("/usr/share/opi-updates/kgp-update") )
{
logg << Logger::Debug << "Try executing /usr/share/opi-updates/kgp-update"<<lend;
Process::Spawn("/usr/share/opi-updates/kgp-update", {"-f"});
}
else
{
logg << Logger::Debug << "Try executing /usr/share/opi-updates/opi-dist-upgrade.sh"<<lend;
Process::Spawn("/usr/share/opi-updates/opi-dist-upgrade.sh");
}
}
catch (ErrnoException& err)
{
logg << Logger::Notice << "Failed to start update ("<< err.what()<<")" << lend;
this->global_error = "Failed to start update ("s + err.what();
}
}
bool SystemManager::HasProviders()
{
SLOG;
return this->providers.size() > 0;
}
const list<string> &SystemManager::Providers()
{
return this->providers;
}
/*
*
* Shell access implementation
*
*/
class ShellAccess
{
public:
ShellAccess()
{
this->accessavailable = ! OPI::ServiceHelper::IsAvailable("ssh");
if( this->accessavailable )
{
this->dropbearinstalled = OPI::ServiceHelper::IsAvailable("dropbear");
}
if( this->dropbearinstalled )
{
this->shellenabled = OPI::ServiceHelper::IsRunning("dropbear");
}
logg << Logger::Debug
<< "ShellAccess:"
<< " Available: " << this->accessavailable
<< " Installed " << this->dropbearinstalled
<< " Enabled " << this->shellenabled << lend;
}
ShellAccess(const ShellAccess& shell) = default;
ShellAccess(ShellAccess&& shell ) = default;
ShellAccess& operator=(const ShellAccess& shell) = default;
ShellAccess& operator=(ShellAccess&& shell) = default;
bool Available()
{
return this->accessavailable;
}
bool Enabled()
{
return this->shellenabled;
}
bool Enable()
{
logg << Logger::Debug << "ShellAccess: Enable" << lend;
bool ret = false;
if( this->accessavailable && ! this->shellenabled )
{
tie(ret, std::ignore) = Process::Exec("/usr/share/kgp-assets/kgp-shell/enable_shell.sh");
if( ! ret )
{
logg << Logger::Error << "Failed to install dropbear ssh server" << lend;
}
this->shellenabled = ret;
this->dropbearinstalled = ret;
}
return ret;
}
bool Disable()
{
logg << Logger::Debug << "ShellAccess: Disable" << lend;
bool ret = false;
if( ! this->dropbearinstalled )
{
logg << Logger::Warning << "Trying to disable ssh but dropbear not installed!" << lend;
return false;
}
tie(ret, std::ignore) = Process::Exec( "/usr/share/kgp-assets/kgp-shell/disable_shell.sh" );
if( ! ret )
{
logg << Logger::Error << "Failed to remove dropbear ssh server" << lend;
}
else
{
// Succesfully removed
this->shellenabled = false;
this->dropbearinstalled = false;
}
return ret;
}
~ShellAccess() = default;
private:
bool accessavailable{false}; /// Is shell access supported (I.e. no openssh installed)
// TODO: Below most likely redundant. Should be enough with one of them,
// There currently are no scenarion with dropbear installed but not running(?)
bool shellenabled{false}; /// Is ssh server running?
bool dropbearinstalled{false}; /// Drobbear currently installed?
};
// We currently support shell access on all arch as long as openssh is not installed
// We should revise this and give finer control over this
bool SystemManager::ShellAccessAvailable()
{
return this->shellaccess->Available();
}
bool SystemManager::ShellAccessEnabled()
{
return this->shellaccess->Enabled();
}
bool SystemManager::ShellAccessEnable()
{
if( ! this->shellaccess->Enable() )
{
this->global_error = "Failed to enable shell access";
return false;
}
return true;
}
bool SystemManager::ShellAccessDisable()
{
if( !this->shellaccess->Disable())
{
this->global_error = "Failed to disable shell access";
return false;
}
return true;
}
SystemManager::~SystemManager() = default;