-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainer.cpp
More file actions
112 lines (92 loc) · 2.57 KB
/
Container.cpp
File metadata and controls
112 lines (92 loc) · 2.57 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
#include "Container.h"
using namespace std;
Container::Container(const size_t ctId) :
ctId_(ctId),
isExist_(false),
isRunning_(false)
{
updateStatus();
}
size_t Container::getTimeout() const
{
// How long should we wait until container is loading
// in milliseconds
return ConfigFile::getInstance()->read<size_t> ("container_timeout", 5000);
}
size_t Container::getTimeStep() const
{
// Step, after which we checking if container is running or not
// should be smaller then getTimeout() constant
// in milliseconds
return ConfigFile::getInstance()->read<size_t> ("container_timeStep", 500);
}
bool Container::run()
{
Logger *Log = Logger::getInstance();
stringstream command;
command << "vzctl start ";
command << ctId_;
// Supress output and errors
command << " > /dev/null 2> /dev/null";
stringstream message;
message << "Starting container: " << command.str();
Log->info(message.str());
Utils::myExec((command.str()).c_str());
// So we should wait until it loads in cycle for maximum timeout() ms
size_t counter = 0;
const size_t step = getTimeStep();
while (counter < getTimeout() && !isRunning_) {
updateStatus();
usleep(step * 1000);
counter += step;
}
if(counter < getTimeout()) {
Log->info("Container successfully started");
}
else {
Log->error("Timeout reached while starting container");
}
return isRunning_;
}
bool Container::isExist() const
{
return isExist_;
}
bool Container::isRunning() const
{
return isRunning_;
}
void Container::updateStatus()
{
Logger *Log = Logger::getInstance();
stringstream command;
command << "vzctl status ";
command << ctId_;
stringstream message;
message << "Checking status of container: " << command.str();
Log->info(message.str());
string result = Utils::myExec(command.str().c_str());
message.str("");
message << "Returned result: " << endl << result;
Log->debug(message.str());
if(result.length() > 0) {
vector<string> params;
Utils::split(result, ' ', params);
if(params.size() == 5) {
isExist_ = (strncmp(params[2].c_str(), "exist",
strlen("exist")) == 0);
isRunning_ = (strncmp(params[4].c_str(), "running",
strlen("running")) == 0);
message.str("");
message << "Container is exist: " << isExist_ << endl;
message << "Container is running: " << isRunning_ << endl;
Log->info(message.str());
}
else {
Log->error("Vzctl returned string in unknown format.");
}
}
else {
Log->error("Vzctl returned empty string.");
}
}