-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponent.cpp
385 lines (337 loc) · 11.9 KB
/
Component.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <chrono>
#include <thread>
#include <dlfcn.h> // dlopen, RTLD_LAZY, dlsym
#include <iostream>
#include <fstream>
#include <unistd.h>
#include "sys/types.h"
#include "sys/sysinfo.h"
#include "inc/Util.hpp"
#include "inc/Control.hpp"
#include "inc/Core.hpp"
#include "inc/Component.hpp"
using namespace std;
int Component::componentNum = 0;
Component::Component(){
lex = nullptr;
parent = nullptr;
objectsNum = 0;
init();
}
Component::Component(Component* parent){
this->parent = parent;
if (parent!=nullptr) lex = parent->lex;
objectsNum = 0;
init();
}
void Component::init(){
attributes[ATT_CLASSNAME]="Component";
attributes[ATT_NAME]="Component-"+componentNum++;
synLastChar='\0';
synLastType=SYN_UNDEF;
token="";
}
string Component::execute( string actionName){
LOG( "Excution not contemplated " + actionName );
return (string)"{ \"result\" : \"KO\" , \"message\" : \"" + "Excution not contemplated " + actionName + "\" }";
}
void Component::addComponent(Component* component){
childs.push_back(component);
}
//--------- PARSER
bool Component::readToken(){
if (lex->fin.eof()) return false; // EOF
lex->lexGetToken();
if (lex->lexLastType == LEX_BLANC ||
lex->lexLastToken.empty() ||
lex->lexLastToken == " " ||
lex->lexLastToken == "\n" ||
lex->lexLastToken == "\r"
) lex->lexGetToken();
token = lex->lexLastToken;
lex->lexLastToken = "";
lex->isTokenDefined = false;
return true;
}
bool Component::isToken(string compareTo){
//return ( token.compare( compareTo ) == 0 );
return ( token == compareTo );
}
bool Component::isBlanc(){
//return (lex->lexLastType == LEX_BLANC);
return ( token.compare( "" ) == 0 );
}
bool Component::isEndLine(){
return ( lex->lexLastChar == '\n' || lex->lexLastChar == '\r' );
}
void Component::parse(){
LOG( "Parse by " + attributes[ATT_NAME] );
while ( readToken() ){
//DEBUG LOG( "\nTOKEN: [" << token << "]\n";
if (isBlanc()) {
//DEBUG LOG( "¬" );
SEG_SHOW_BLANC();
}
else if (isToken( TK_POINT )){ return; }
else if (lex->lexLastType == LEX_COMMENT){ parseComment(); }
else if (isToken( TK_Control )){ parseControl(); }
else if (isToken( TK_Debug )){ parseDebug(); }
else if ( lex->lexLastChar == ':' ){ loadPlugin(token);
return;
}else {
LOG( "Systax ERROR: " + token );
return;
}
}
LOG( "End!" );
}
void Component::parseComment(){ //TODO REMOVE THIS. Now parsed in lex.
LOG( "Comment: " + token );
}
void Component::parseControl(){
while ( readToken() ){
LOG( "Control: [" + token );
if (isToken( TK_start )){
//TODO control->start()
LOG( "Control: start activated" );
return;
}else{
//SYNTAC ERROR
return;
}
}
}
void Component::parseDebug(){
while ( readToken() ){
LOG( "Debug: " + token );
if (isToken( TK_on )){
//TODO debug->on() */;
LOG("Debug: ON!");
return;
}else{
//SYNTAC ERROR
return;
}
}
}
void Component::parsePortForward(){
//DEBUG LOG( "Executor");
bool parsedName = false;
bool parsedPort = false;
bool parsedNamespace = false;
string key="", value="";
while ( readToken() ){
if (isBlanc()) { SEG_SHOW_BLANC(); }
else if (isToken( TK_POINT )){ break; }
else if ( !parsedName && token.compare( "" ) != 0 ){ // Gets the name of the pod to search
attributes[ATT_APP] = token;
parsedName = true;
}else if ( !parsedPort && token.compare( "" ) != 0 ){ // Gets the nport number to bind
attributes[ATT_PORT] = token;
parsedPort = true;
}else if ( !parsedNamespace && token.compare( "" ) != 0 ){ // Gets the namespace
attributes[ATT_NAMESPACE] = token;
parsedNamespace = true;
} else PARSE_ATT_KEY_TOKEN(attributes, key, token);
}
// VERIFY that the namespace was defined in this component and not in parents
// for tha looks attributes[ATT_NAMESPACE] instead getAtt( ATT_NAMESPACE)
// --------------------------------\/----------------------------------------
if (parsedName && parsedPort && (attributes[ATT_NAMESPACE]!="") ){
portForward = true;
string actionPF = (string) getAtt(ATT_CLASSNAME)+"-"+TK_PORT_FORWARD;
Core* core = (Core*) getRootComponent( this );
core->createPortForward(actionPF, getAtt( ATT_APP ), getAtt( ATT_PORT ), getAtt( ATT_NAMESPACE));
LOG( "PortForward app: "+ getAtt(ATT_APP)+" port: "+ getAtt(ATT_PORT)+ " namespace: "+ getAtt(ATT_NAMESPACE));
}else{
LOG( "ERROR: PortForward bad defined. Attributes necesaries: app, port, namespace.");
LOG_ATTRIBUTES();
}
}
Component* Component::loadPlugin(string pluginName){
string folder ="plugins/";
string extension = ".so";
void *libPlugin = loadLibrary(folder + pluginName + extension);
plugins.push_back( libPlugin );
//DEBUG LOG("Getting interface... " << pluginName);
void* (*loadPlugin)(Component*) = (void* (*) (Component*)) dlsym(libPlugin, "loadPlugin");
void (*parse)(void*) = (void (*) (void*)) dlsym(libPlugin, "parse");
// if couldn't find the function
if (loadPlugin == NULL) {
perror("ERROR\n[ERROR] dlsym");
printf( "---\n" );
printf( dlerror() );
printf( "\n---\n" );
exit(EXIT_FAILURE);
}
//DEBUG else{ LOG("Interface Ok!"); }
void* instance = loadPlugin(this);
Component* newComponent = (Component*) instance;
objectsNum++;
addComponent( newComponent );
parse(instance);
return newComponent;
}
void* Component::loadLibrary(string librarypath){
LOG("Plugin: " + librarypath );
void* libhandle = dlopen(&librarypath[0], RTLD_LAZY); //functions will be loaded when I try to use them (lazy loading)
// if couldn't find the library
if (libhandle == NULL) {
LOG("ERROR!!!" );
perror("[ERROR] dlopen");
printf( "---\n" );
printf( dlerror() );
printf( "\n---\n" );
exit(EXIT_FAILURE);
}else{
LOG("<<< Plugin Ok! >>>" );
}
return libhandle;
}
string Component::getAtt(string name){
string value = this->attributes[name];
if ((value == "") && (this->parent!=nullptr))
value = this->parent->getAtt(name);
return value;
}
string Component::systemCommand(string command, string filenaMeOut, string filenaMeErr){
LOG_FILE("System > " + command, filenaMeOut);
LOG_FILE("System > " + command, filenaMeErr);
command += " >> "+filenaMeOut+" 2>> "+filenaMeErr;
try{
system(command.c_str());
}catch(...){
LOG("systemCommand. Exception! ");
}
return "OK"; //resultCommand;
}
string Component::getAtt(string name, string defValue){
string value = this->attributes[name];
if (value == ""){
if (this->parent!=nullptr)
value = this->parent->getAtt(name, defValue);
else
value = defValue;
}
return value;
}
// TODO: Maintenance with K8S::getJsonComponent(), Kafca::getJsonComponent()
string Component::getJsonComponent(){
string components="{";
components+=JSON_PROPERTY(ATT_CLASSNAME, attributes[ATT_CLASSNAME])+",\n";
components+=JSON_PROPERTY(ATT_NAME, attributes[ATT_NAME])+",\n";
components+=JSON_ARRAY( "actions", getJsonActions() )+",\n";
components+=JSON_ARRAY("attributes", getJsonAttributes() )+",\n";
components+=JSON_ARRAY( "childs", getJsonChilds() )+"\n";
components+="}";
return components;
}
string Component::getJsonActions(){
string jsonActions = "";
// Print Strings stored in Vector
// for (long unsigned int i = 0; i < actions.size(); i++){
// jsonActions += "\"" + (string)(actions[i]) + "\"\n";
// jsonActions += (i+1 < actions.size() ? ",\n" : "\n" );
// }
for (std::map<string,string>::iterator item = actions.begin(); item != actions.end(); ++item){
/*DEBUG LOG( (string)"[ " + (item->first) + " = "+ (item->second) + "]");*/ \
jsonActions += "\"" + (string)(item->first) + "\"\n";
jsonActions += (std::next(item) != actions.end() ? ",\n" : "\n" );
}
return jsonActions;
}
string Component::getJsonAttributes(){
string json_attributes = "";
MAP_TO_STRING(attributes, json_attributes);
return json_attributes;
}
string Component::getJsonChilds(){
string jsonChilds = "";
for (std::list<Component*>::iterator child = childs.begin(); child != childs.end(); ++child){
jsonChilds += (*child)->getJsonComponent()+"\n";
jsonChilds += (std::next(child) != childs.end() ? ",\n" : "\n");
}
return jsonChilds;
}
string Component::getJsonLabels(){
//DEBUG
LOG( "K8S Labels..." ); //TODO why not core directly.
string json_labels = "";
MAP_TO_STRING(labels, json_labels);
return json_labels;
}
//TODO
string Component::getYamlLabels(){
return (string)
" labels:\n";
}
string Component::doPlay(){
string result = "";
for (std::list<Component*>::iterator child = childs.begin(); child != childs.end(); ++child){
result += (*child)->doPlay();
}
return result;
}
string Component::doDestroy(){
string result = "";
for (std::list<Component*>::iterator child = childs.begin(); child != childs.end(); ++child){
result += (*child)->doDestroy();
}
string nameSpace=attributes[ATT_NAMESPACE];
if (nameSpace!=""){
string command = "kubectl delete namespace "+nameSpace;
result += systemCommand( command );
LOG( "Destroy namespace "+attributes[ATT_NAME] + ": " + result );
}
return result;
}
string Component::doQuit(){
LOG( attributes[ATT_NAME] + ".doQuit! Childs: " + to_string(childs.size()) + " Plugins: "+ to_string(plugins.size()) );
string result = (string) "doQuit " + attributes[ATT_NAME]+"\n";
for (std::list<Component*>::iterator child = childs.begin(); child != childs.end(); ++child){
result += (*child)->doQuit();
DELETE_NEBEL( (*child)->attributes[ATT_NAME] , (*child) );
}
childs.clear();
for (std::list<void*>::iterator plugin = plugins.begin(); plugin != plugins.end(); ++plugin){
result += "Remove library!\n";
dlclose( *plugin );
}
plugins.clear();
return result;
}
Component* Component::getRootComponent(Component* component){
if (component == nullptr) return nullptr;
if (component->parent == nullptr){
return (Core*) component;
}
return getRootComponent(component->parent);
}
int Component::getObjectNum(){
int n = objectsNum;
for (std::list<Component*>::iterator child = childs.begin(); child != childs.end(); ++child){
n += (*child)->getObjectNum();
}
return n;
}
// Use attributes: app, namespace, port
void Component::startPortForward(PortForward* pf){
if (portForwardInited){
LOG("PortForward previosly started! Restarting...");
stopPortForward( pf );
}
string nameSpace=getAtt(ATT_NAMESPACE, "default");
string command = "kubectl port-forward `kubectl get pod -l app="+ pf->appName
+" -n "+ pf->nameSpace
+" -o jsonpath='{.items[0].metadata.name}'` "+ pf->port
+" -n "+ pf->nameSpace
+ " &";
systemCommand( command , "portforward.out", "portforward_error.out");
LOG( "PortForward running for "<< pf->appName <<" port "<< pf->port );
portForwardInited = true;
}
void Component::stopPortForward(PortForward* pf){
string command = "kill -9 `ps -ef |grep \"port-forward "+ pf->appName +"\"|awk '!/grep/ {print $2}'`";
string resultCommand = systemCommand( command , "portforward-stop.out", "portforward-stop_error.out" );
LOG( "PortForward stoped! for " << pf->appName );
}