Skip to content

Commit

Permalink
Merge pull request #571 from subutai-io/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
crioto authored Jan 30, 2018
2 parents 0f58ef4 + 4a82dd2 commit b7d572b
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 115 deletions.
14 changes: 13 additions & 1 deletion hub/include/P2PController.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class SwarmConnector : public StatusChecker
private slots:
void run_checker() {
QFuture<system_call_wrapper_error_t> res =
QtConcurrent::run(CSystemCallWrapper::join_to_p2p_swarm, env.hash(), env.key(), QString("dhcp"), env.base_interface_id());
QtConcurrent::run(CSystemCallWrapper::join_to_p2p_swarm, env.hash(), env.key(),
QString("dhcp"), env.base_interface_id());
res.waitForFinished();
emit connection_finished(res.result());
}
Expand Down Expand Up @@ -111,6 +112,17 @@ public slots:
private:
std::set< std::pair<QString, QString> > connected_conts; // Connected container. Pair of environment id and container id.
std::set< QString > connected_envs; // Joined to swarm environment. Id of env is stored
std::map<int, QString> interface_ids; // Pair of interface id and swarm hash

int get_unselected_interface_id() {
for(int id = 0 ; id < 30 ; ++id) { // differents ids for 30 environments
if (interface_ids.find(id) == interface_ids.end()) {
return id;
}
}
return -1;
}

void join_swarm(const CEnvironment& env);
void leave_swarm(const QString &hash);
void check_status(const CEnvironment& env);
Expand Down
15 changes: 7 additions & 8 deletions hub/include/RestContainers.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class CEnvironment {
int m_hub_id;
QString m_status;
QString m_status_descr;
QString m_base_interface_id;
int m_base_interface_id;
std::vector<CHubContainer> m_lst_containers;
public:
CEnvironment() : m_name(""){}
Expand All @@ -120,7 +120,7 @@ class CEnvironment {
m_hub_id = obj["environment_hub_id"].toInt();
m_status = obj["environment_status"].toString();
m_status_descr = obj["environment_status_desc"].toString();
m_base_interface_id = get_base_interface_id();
m_base_interface_id = -1;
QJsonArray arr = obj["environment_containers"].toArray();
for (auto i = arr.begin(); i != arr.end(); ++i) {
if (i->isNull() || !i->isObject()) continue;
Expand All @@ -130,11 +130,6 @@ class CEnvironment {
}
}

static QString get_base_interface_id() {
static int current_id = 0;
return QString(base_interface_name() + QString::number(current_id ++));
}

~CEnvironment(){}

bool operator==(const CEnvironment& arg) const {
Expand Down Expand Up @@ -163,8 +158,12 @@ class CEnvironment {
const std::vector<CHubContainer>& containers() const {return m_lst_containers;}
const QString& status() const {return m_status;}
const QString& status_description() const {return m_status_descr;}
const QString& base_interface_id() const {return m_base_interface_id;}
const int& base_interface_id() const {return m_base_interface_id;}
bool healthy() const {return m_status == QString("HEALTHY");}

void set_base_interface_id(int base_interface_id) {
m_base_interface_id = base_interface_id;
}
};
////////////////////////////////////////////////////////////////////////////

Expand Down
3 changes: 2 additions & 1 deletion hub/include/SystemCallWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ class CSystemCallWrapper {

static bool is_in_swarm(const QString &hash);
static std::vector<QString> p2p_show();
static std::vector<std::pair<QString, QString>> p2p_show_interfaces();

static system_call_wrapper_error_t join_to_p2p_swarm(const QString &hash,
const QString &key,
const QString &ip,
const QString &swarm_base_interface_name);
int swarm_base_interface_id);

static system_call_wrapper_error_t leave_p2p_swarm(const QString &hash);
static system_call_wrapper_error_t restart_p2p_service(int *res_code);
Expand Down
62 changes: 59 additions & 3 deletions hub/src/P2PController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,82 @@ void P2PConnector::update_status() {
qInfo() << "Starting to update connection status";


QFuture<std::vector<QString> > res = QtConcurrent::run(CSystemCallWrapper::p2p_show);
res.waitForFinished();
QFuture<std::vector<QString>> res_swarm = QtConcurrent::run(CSystemCallWrapper::p2p_show);
res_swarm.waitForFinished();

QFuture<std::vector<std::pair<QString, QString>>> res_interfaces = QtConcurrent::run(CSystemCallWrapper::p2p_show_interfaces);
res_interfaces.waitForFinished();

std::vector<QString> joined_swarms = res_swarm.result(); // swarms + interfaces
std::vector<std::pair<QString, QString>> swarm_interfaces = res_interfaces.result(); // swarms + interfaces



std::vector<QString> joined_swarms = res.result();
std::vector<CEnvironment> hub_environments = CHubController::Instance().lst_environments();
QStringList already_joined;
QStringList hub_swarms;
QStringList lst_interfaces;

for (auto tt : swarm_interfaces) {
lst_interfaces << tt.first << " -- " << tt.second << "\n";
}
for (auto swarm : joined_swarms){
already_joined << swarm;
}
for (auto env : hub_environments){
hub_swarms << env.hash();
}

qDebug()
<< "Joined swarm: " << already_joined;

qDebug()
<< "Swarms from hub: " << hub_swarms;

qDebug()
<< "Swarm Interfaces: " << lst_interfaces;

// need to clear it everytime
interface_ids.clear();

// Setting the interfaces to swarm
// if an interface found with command `p2p show --interfaces --bind`, then we use that interface
for (CEnvironment &env : hub_environments) {
if (!env.healthy())
continue;
std::vector<std::pair<QString, QString>>::iterator found_swarm_interface =
std::find_if(swarm_interfaces.begin(), swarm_interfaces.end(), [&env](const std::pair<QString, QString>& swarm_interface) {
return env.hash() == swarm_interface.first;
});

if (found_swarm_interface != swarm_interfaces.end()
&& env.base_interface_id() == -1) {
QString interface = found_swarm_interface->second;
QRegExp regExp("(-?\\d+(?:[\\.,]\\d+(?:e\\d+)?)?)");
regExp.indexIn(interface);
int id = regExp.capturedTexts().first().toInt();
env.set_base_interface_id(id);
interface_ids[id] = env.hash();
}
}

// if an interface was not found with command, then we will give the unselected interfaces
for (CEnvironment &env : hub_environments) {
if (!env.healthy())
continue;
std::vector<std::pair<QString, QString>>::iterator found_swarm_interface =
std::find_if(swarm_interfaces.begin(), swarm_interfaces.end(), [&env](const std::pair<QString, QString>& swarm_interface) {
return env.hash() == swarm_interface.first;
});
if (found_swarm_interface == swarm_interfaces.end()
&& env.base_interface_id() == -1) {
int id = get_unselected_interface_id();
env.set_base_interface_id(id);
interface_ids[id] = env.hash();
}
}

//
for (CEnvironment env : hub_environments) {
if (std::find(joined_swarms.begin(), joined_swarms.end(), env.hash()) == joined_swarms.end()) { // environment not found in joined swarm hashes, we need to try to join it
connected_envs.erase(env.hash());
Expand Down
1 change: 0 additions & 1 deletion hub/src/SettingsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ static void qvar_to_map_string_qvariant(const QVariant& var, void* field) {
static const int DEFAULT_REFRESH_TIMEOUT_SEC = 20;
static QString settings_file_path() {
static const QString settings_file = "subutai_tray.ini";
return "/home/lezh1k/Desktop/" + settings_file;

QStringList lst_config=
QStandardPaths::standardLocations(QStandardPaths::ConfigLocation);
Expand Down
40 changes: 38 additions & 2 deletions hub/src/SystemCallWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,40 @@ bool CSystemCallWrapper::is_in_swarm(const QString &hash) {
}
////////////////////////////////////////////////////////////////////////////

std::vector<std::pair<QString, QString>> CSystemCallWrapper::p2p_show_interfaces() {
std::vector<std::pair<QString, QString>> swarm_lsts;

if (!p2p_daemon_check()) {
return swarm_lsts;
}

QString cmd = CSettingsManager::Instance().p2p_path();
QStringList args;


args << "show" << "--interfaces" << "--bind";
system_call_res_t res = ssystem_th(cmd, args, true, true);

if (res.res != SCWE_SUCCESS && res.exit_code != 1) {
qCritical("%s", error_strings[res.res].toStdString().c_str());
return swarm_lsts;
}

for (QString swarm : res.out) {
if (swarm.indexOf("swarm") != -1) {
QStringList lst = swarm.split("|", QString::SkipEmptyParts);
if (lst.empty() || lst.size() != 2) {
continue;
}
swarm_lsts.push_back(std::make_pair(lst[0], lst[1]));
}
}

return swarm_lsts;
}

////////////////////////////////////////////////////////////////////////////

std::vector<QString> CSystemCallWrapper::p2p_show() {
std::vector<QString> swarm_lsts;

Expand Down Expand Up @@ -138,8 +172,7 @@ std::vector<QString> CSystemCallWrapper::p2p_show() {
////////////////////////////////////////////////////////////////////////////

system_call_wrapper_error_t CSystemCallWrapper::join_to_p2p_swarm(
const QString &hash, const QString &key, const QString &ip, const QString &swarm_base_interface_name) {
UNUSED_ARG(swarm_base_interface_name);
const QString &hash, const QString &key, const QString &ip, int swarm_base_interface_id) {

if (is_in_swarm(hash)) return SCWE_SUCCESS;

Expand All @@ -155,6 +188,9 @@ system_call_wrapper_error_t CSystemCallWrapper::join_to_p2p_swarm(
<< "-key" << key
<< "-hash" << hash
<< "-dht" << p2p_dht_arg();
if (swarm_base_interface_id != -1)
args << "-dev" << base_interface_name() + QString::number(swarm_base_interface_id);

qDebug() << "ARGS=" << args;

system_call_res_t res;
Expand Down
2 changes: 1 addition & 1 deletion hub/src/updater/HubComponentsUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ CHubComponentsUpdater::set_update_freq(const QString &component_id,
return;

m_dct_components[component_id].set_timer_interval(
CSettingsManager::update_freq_to_sec(freq)*1000);
CSettingsManager::update_freq_to_sec(freq) * 1000);
m_dct_components[component_id].timer_start();
}
////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 2 additions & 0 deletions hub/src/updater/UpdaterComponentP2P.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ CUpdaterComponentP2P::update_available_internal() {

chue_t
CUpdaterComponentP2P::update_internal() {
qDebug() << "Starting to update P2P";

QString str_p2p_path = p2p_path();
if (str_p2p_path.isNull() ||
str_p2p_path.isEmpty() ||
Expand Down
1 change: 1 addition & 0 deletions hub/src/updater/UpdaterComponentRH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ CUpdaterComponentRH::update_available_internal() {

chue_t
CUpdaterComponentRH::update_internal() {
qDebug() << "Starting to update RH";
int exit_code = 0;
system_call_wrapper_error_t res = CSystemCallWrapper::run_rh_updater(
CSettingsManager::Instance().rh_host().toStdString().c_str(),
Expand Down
1 change: 1 addition & 0 deletions hub/src/updater/UpdaterComponentRHManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ CUpdaterComponentRHM::update_available_internal() {

chue_t
update_system::CUpdaterComponentRHM::update_internal() {
qDebug() << "Starting to update Management";
int exit_code = 0;
system_call_wrapper_error_t res = CSystemCallWrapper::run_rh_management_updater(
CSettingsManager::Instance().rh_host().toStdString().c_str(),
Expand Down
Loading

0 comments on commit b7d572b

Please sign in to comment.