-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.cpp
259 lines (213 loc) · 6.17 KB
/
manager.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
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
//
// Created by steccas on 24/06/17.
//
#include <sstream>
#include <iostream>
#include <vector>
#include <random>
#include "manager.h"
using namespace std;
//private
void manager::addMan(string id, string tp) {
managed toAdd(id, tp);
//toAdd= new managed(id, tp);
this->mged.push_back(toAdd);
}
void manager::delMan(string id) {
for (list<managed>::iterator it=mged.begin(); it!=mged.end(); ++it){
string _id = it->getId();
if (_id == id) {mged.erase(it);}
}
}
//public
manager::manager() {
docker= new dockInt();
routerImg = "debian:networkR";
hostImg = "debian:networkH";
}
bool manager::isRouter(string id) {
if(docker->getContImg(id) == routerImg) return true;
else return false;
}
int manager::connectC(string id1, string id2) { //Ho realizzato che è meglio conservarsi le cose in una struttura dati propria
bool r1, r2 = false;
r1 = isRouter(id1);
r2 = isRouter(id2);
if (r1 || r2) {
//vedi chi è il router
string router, host;
if (r1 == 1 && r2 == 0) {
router = id1;
host = id2;
//usa readContainerNetworks e poi prendi l'ip del router in base alla rete interessata
//docker->routeAdd(id2, "default", )
}
if (r1 == 0 && r2 == 1) {
router = id2;
host = id1;
}
if (r1 == 1 && r2 == 1) {
}
//controlla se r1 e r2 sono sulla stessa rete
//aggiungi il router nel def gw dell'host
//aggiungili vicendevolmente (metti la route per la rete/i dietro ogni router)
}
else return 1;
}
dockInt *manager::getDocker() {
return docker;
}
vector <string> manager::readContainersId() {
string tmp;
string out;
out = (docker->ps("id", 0));
//cout << "call1 : "+ out << endl;
vector<string> ret;
ret = splitString(out, "\n");
return ret;
}
vector <string> manager::readContainersStatus() {
string out;
out = (docker->ps("status", 0));
//cout << "call1 : "+ out << endl;
vector<string> ret;
ret = splitString(out, "\n");
return ret;
}
vector <string> manager::readContainersNames() {
string out;
out = (docker->ps("names", 0));
//cout << "call1 : "+ out << endl;
vector<string> ret;
ret = splitString(out, "\n");
return ret;
}
vector <string> manager::readContainerNetworks(string id) {
string tmp;
string out;
out = docker->inspect(id, "'{{range $key, $value := .NetworkSettings.Networks}}{{printf \"%s\\n\" $key}}{{end}}'");
vector<string> ret;
ret = splitString(out, "\n");
return ret;
}
vector <string> manager::readNetworksId() {
string out;
out = (docker->lsNet("id"));
vector<string> ret;
ret = splitString(out, "\n");
return ret;
}
vector <string> manager::readNetworksNames() {
string out;
out = (docker->lsNet("name"));
vector<string> ret;
ret = splitString(out, "\n");
return ret;
}
list <string> manager::readNetworkConts(string id) {
string tmp;
stringstream out;
out.str(docker->inspect(id, "'{{range $key, $value := .Containers}}{{printf \"%s\n\" $key}}{{end}}'"));
list<string> ret;
while ( getline(out, tmp) ) {
if (tmp.empty()) // be careful: an empty line might be read
continue;
ret.push_back(tmp);
}
return ret;
}
vector<string> manager::splitString(const std::string& str,
const std::string& delim)
{
try {
std::vector<std::string> strings;
std::string::size_type pos = 0;
std::string::size_type prev = 0;
while ((pos = str.find(delim, prev)) != std::string::npos)
{
strings.push_back(str.substr(prev, pos - prev));
prev = pos + 1;
}
// To get the last substring (or only, if delimiter is not found)
strings.push_back(str.substr(prev));
return strings;
} catch(int e) {
vector<string> err;
err[0] = "unsplittable " + e;
return err;
}
}
bool manager::isNotWhiteSpace(string str) {
auto it = str.begin();
do {
if (it == str.end()) return false;
} while (*it >= 0 && *it <= 0x7f && std::isspace(*(it++)));
// one of these conditions will be optimized away by the compiler,
// which one depends on whether char is signed or not
return true;
}
string manager::getNetSubnet(string id) {
//string str = execCmd("docker inspect " + id + " -f '{{ .IPAM.Config}}'");
string str;
str = this->docker->inspect(id, "'{{ .IPAM.Config}}'");
unsigned first = str.find("[{");
unsigned last = str.find(" ");
string strNew = str.substr (first,last - first);
strNew.erase (0,2);
//cout << strNew << endl;
return strNew;
}
string manager::getNetName(string id) {
string ret;
ret = docker->inspect(id, "{{.Name}}");
return ret;
}
string manager::createGwForExam(string sub) {
vector<string> ip = splitString(sub, ".");
string gw;
if(ip.size() == 4){
ip[3] = "60";
gw = ip[0] + "." + ip[1] + "." + ip[2] + "." + ip[3];
}
else gw = "error";
return gw;
}
string manager::createSubNetForExam() {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(0, 254);
int i3 = dist(mt);
string s3 = to_string(i3);
string ip = "10.0." + s3 + ".0/24";
cout << ip;
return ip;
}
string manager::addNetExam(string name) {
bool ok = false;
string res;
while (!ok){
string sub = createSubNetForExam();
string gw = createGwForExam(sub);
res = docker->addNet(name, sub, gw);
if (res != "overlapping") ok = true;
}
return res;
}
vector<string> manager::readContIfs(string id) {
string out;
out = (docker->ifconfig(id, "| grep \"eth\" | awk -F'[: ]+' '{ print $1 }'"));
cout << out;
vector<string> ret;
ret = splitString(out, "\n");
return ret;
}
bool manager::dockerService() {
string status = docker->getDockerStatus();
if (status == "active\n") return 1;
else {
docker->startDocker();
status = docker->getDockerStatus();
if (status == "active\n") return 1;
else return 0;
}
}