7#include "ui_clusterWidget.h"
16#include <QRadioButton>
39# define pclose _pclose
47std::string
shellQuote(
const std::string& value)
65std::vector<std::string>
splitTabs(
const std::string& line)
67 std::vector<std::string>
fields;
68 std::stringstream stream(line);
70 while (std::getline(stream,
field,
'\t'))
81 if (!std::isalnum(
static_cast<unsigned char>(
c)))
96 if (!node.
docker.empty()) {
99 if (!node.
conda.empty()) {
102 if (!node.
pixi.empty()) {
110 if (!node.
docker.empty()) {
113 if (!node.
conda.empty()) {
116 if (!node.
pixi.empty()) {
124 return (!node.
docker.empty() ? 1 : 0) +
125 (!node.conda.
empty() ? 1 : 0) +
126 (!node.pixi.
empty() ? 1 : 0);
137 dockerGroupBox(nullptr),
138 dockerTreeWidget(nullptr),
139 dockerRefreshBtn(nullptr),
140 dockerStartBtn(nullptr),
141 dockerStopBtn(nullptr)
149 ui->nodestreeWidget->setColumnCount(8);
150 ui->nodestreeWidget->setHeaderLabels(
QStringList() <<
"Status" <<
"Name" <<
"Display" <<
"User" <<
"Address" <<
"Runtime" <<
"Target" <<
"Log");
151 ui->executeBtn->setDisabled(
true);
170 connect(ui->nodestreeWidget, &QTreeWidget::itemChanged,
this, [
this](QTreeWidgetItem*,
int column) {
172 updateYarprunDockerColors();
192 yError()<<
"ClusterWidget:Unable parse cluster-config.xml in context iCubCluster";
198 ui->lineEditUser->setText(cluster.
user.c_str());
199 ui->lineEditNs->setText(cluster.
nameSpace.c_str());
208 l.push_back(cluster.
nsNode.c_str());
210 for (
auto& node:cluster.
nodes)
217 l.push_back(node.
name.c_str());
221 ui->executeComboBox->addItems(
l);
222 ui->executeComboBox->setEditable(
true);
224 ui->nsNodeComboBox->addItems(
l);
225 ui->nsNodeComboBox->setEditable(
true);
227 onRefreshDockerContainers();
235 ui->nodestreeWidget->header()->resizeSection(0, 50);
237 onNodeSelectionChanged();
241void ClusterWidget::onCheckAll()
243 for (
int i = 0; i<ui->nodestreeWidget->topLevelItemCount(); i++)
245 QTreeWidgetItem *
it = ui->nodestreeWidget->topLevelItem(i);
246 int itr = getNodeIndex(
it);
248 if (checkNode(node.
name))
250 cluster.
nodes[itr].onOff=
true;
251 it->setIcon(0,
QIcon(
":/computer-available22.svg"));
255 cluster.
nodes[itr].onOff=
false;
256 it->setIcon(0,
QIcon(
":/computer-unavailable22.svg"));
259 updateYarprunDockerColors();
263void ClusterWidget::onCheckServer()
265 checkNs = checkNameserver();
273 ui->checkRos->setDisabled(checkNs);
274 ui->runServerBtn->setDisabled(checkNs);
275 ui->nsNodeComboBox->setDisabled(checkNs);
276 ui->stopServerBtn->setDisabled(!checkNs);
279void ClusterWidget::onRunServer()
281 updateServerEntries();
285 if (ui->checkRos->isChecked())
295 std::string err =
"ClusterWidget: failed to run the server on " + cluster.
nsNode;
306void ClusterWidget::onStopServer()
308 updateServerEntries();
310 auto count = std::count_if(cluster.
nodes.begin(), cluster.
nodes.end(),
315 auto reply = QMessageBox::warning(
this,
"Shutting down yarpserver",
316 "You have some yarprun on execution."
317 " After shutting down yarpserver you might not be able to recover them."
319 QMessageBox::Yes|QMessageBox::No);
320 if (reply== QMessageBox::No) {
333 std::string err =
"ClusterWidget: failed to stop the server on " + cluster.
nsNode;
350 std::string
info =
"ClusterWidget: yarpserver successfully stopped on "+ cluster.
nsNode;
355void ClusterWidget::onKillServer()
357 updateServerEntries();
366 std::string err =
"ClusterWidget: failed to kill the server on " + cluster.
nsNode;
372 std::string
info =
"ClusterWidget: yarpserver successfully killed on "+ cluster.
nsNode;
379void ClusterWidget::onRunSelected()
384 int itr = getNodeIndex(
it);
390 std::string
runtime =
it->text(5).trimmed().toStdString();
391 std::string target =
it->text(6).trimmed().toStdString();
395 }
else if (
runtime ==
"conda") {
397 }
else if (
runtime ==
"pixi") {
400 std::string portName = node.
name;
402 if (portName.find(
'/') == std::string::npos)
404 portName.insert(0, 1,
'/');
416 logger->
addError(
"ClusterWidget: only one runtime can be specified among docker, conda and pixi.");
421 if (!node.
docker.empty() && !isDockerRunning(node, node.
docker))
423 logger->
addError(
"ClusterWidget: cannot run yarprun inside docker " + node.
docker +
424 " because the container is not running. Please start the docker container first.");
429 if (!node.
pixi.empty() && !checkPixiRuntime(node))
444 std::string err =
"ClusterWidget: failed to run yarprun on " + node.
name;
451 if (!checkNode(node.
name))
460 std::string
info =
"ClusterWidget: yarprun successfully executed on " + node.
name +
". Remote log: " +
logFile;
470void ClusterWidget::onStopSelected()
475 int itr = getNodeIndex(
it);
481 std::string portName = node.
name;
482 if (portName.find(
'/') == std::string::npos)
484 portName.insert(0, 1,
'/');
489 cmdStopYarprun.append(
" yarprun --exit --on ").append(portName).append(
" &");
494 std::string err =
"ClusterWidget: failed to stop yarprun on " + node.
name;
500 std::string
info =
"ClusterWidget: yarprun successfully stopped on "+ node.
name;
509void ClusterWidget::onKillSelected()
514 int itr = getNodeIndex(
it);
528 std::string err =
"ClusterWidget: failed to kill yarprun on " + node.
name;
534 std::string
info =
"ClusterWidget: yarprun successfully killed on "+ node.
name;
542void ClusterWidget::onExecute()
544 if (ui->lineEditExecute->text().trimmed().size() == 0)
549 auto nodeName = ui->executeComboBox->currentText();
551 if (nodeName.trimmed().size() == 0)
557 [&nodeName](
const ClusterNode&
n){ return n.name == nodeName.toStdString(); });
566 auto command = ui->lineEditExecute->text().toStdString();
575 std::string err =
"ClusterWidget: failed to run "+ command +
" on " + node.
name;
581 std::string
info =
"ClusterWidget: command "+ command +
" successfully executed on " + node.
name;
585 ui->lineEditExecute->clear();
588void ClusterWidget::onNodeSelectionChanged()
590 if(ui->nodestreeWidget->selectedItems().isEmpty())
592 ui->runSelBtn->setDisabled(
true);
593 ui->stopSelBtn->setDisabled(
true);
594 ui->killSelBtn->setDisabled(
true);
598 ui->runSelBtn->setDisabled(!checkNs);
599 ui->stopSelBtn->setDisabled(!checkNs);
600 ui->killSelBtn->setDisabled(!checkNs);
605void ClusterWidget::onExecuteTextChanged()
607 if (ui->lineEditExecute->text().trimmed().size() > 0) {
608 ui->executeBtn->setDisabled(
false);
610 ui->executeBtn->setDisabled(
true);
616void ClusterWidget::addRow(
const std::string& name,
const std::string& display,
617 const std::string& user,
const std::string& address,
618 const std::string&
runtime,
const std::string& target,
bool onOff,
bool log,
int id)
623 it->setData(0, Qt::UserRole,
id);
624 it->setFlags(
it->flags() | Qt::ItemIsEditable);
625 ui->nodestreeWidget->addTopLevelItem(
it);
626 ui->nodestreeWidget->setItemWidget((QTreeWidgetItem *)
it, 7,
new QCheckBox(
this));
634 it->setIcon(0,
QIcon(
":/computer-available22.svg"));
638 it->setIcon(0,
QIcon(
":/computer-unavailable22.svg"));
643int ClusterWidget::getNodeIndex(QTreeWidgetItem*
item)
const
645 return item->data(0, Qt::UserRole).toInt();
669 if (!node.
conda.empty())
673 "\"$HOME/miniforge3/etc/profile.d/conda.sh\" "
674 "\"$HOME/miniconda3/etc/profile.d/conda.sh\" "
675 "\"$HOME/anaconda3/etc/profile.d/conda.sh\" "
676 "\"/opt/conda/etc/profile.d/conda.sh\"; do "
677 "[ -f \"$conda_sh\" ] && source \"$conda_sh\" && break; "
679 "if ! command -v conda >/dev/null 2>&1; then "
680 "echo \"conda command not found. Please install conda or add conda.sh to one of the standard locations.\" >&2; "
688 if (!node.
pixi.empty())
691 "export PATH=\"$HOME/.pixi/bin:$HOME/.cargo/bin:$PATH\" && "
708 std::array<char, 512>
buffer{};
712 output =
"unable to execute ssh command";
729 "export PATH=\"$HOME/.pixi/bin:$HOME/.cargo/bin:$PATH\"; "
731 "echo \"pixi project directory not found: " + node.
pixi +
"\"; exit 1; "
734 "echo \"pixi.toml not found in: " + node.
pixi +
"\"; exit 1; "
736 "if ! command -v pixi >/dev/null 2>&1; then "
737 "echo \"pixi command not found. Install pixi or add it to PATH on host " + node.
address +
".\"; exit 1; "
741 if (!runRemoteCommand(node, script, output))
759 cmd +=
shellQuote(
"docker ps --format '{{.Names}}'");
762 std::array<char, 512>
buffer{};
783 std::stringstream
lines(output);
785 while (std::getline(
lines, line))
796void ClusterWidget::updateYarprunDockerColors()
798 for (
int i = 0; i < ui->nodestreeWidget->topLevelItemCount(); i++)
800 auto*
item = ui->nodestreeWidget->topLevelItem(i);
807 std::string
runtime =
item->text(5).trimmed().toStdString();
808 std::string target =
item->text(6).trimmed().toStdString();
811 if (
runtime ==
"docker" && !target.empty())
821void ClusterWidget::addDockerControls()
823 dockerGroupBox =
new QGroupBox(
"Docker containers",
this);
827 dockerRefreshBtn =
new QPushButton(
"Refresh", dockerGroupBox);
828 dockerStartBtn =
new QPushButton(
"Start", dockerGroupBox);
829 dockerStopBtn =
new QPushButton(
"Stop", dockerGroupBox);
830 dockerStartBtn->setDisabled(
true);
831 dockerStopBtn->setDisabled(
true);
837 dockerTreeWidget =
new QTreeWidget(dockerGroupBox);
838 dockerTreeWidget->setColumnCount(7);
839 dockerTreeWidget->setHeaderLabels(
QStringList() <<
"Host" <<
"Container" <<
"Status" <<
"ID" <<
"Address" <<
"User" <<
"SSH options");
840 dockerTreeWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
841 dockerTreeWidget->setRootIsDecorated(
false);
842 dockerTreeWidget->setAlternatingRowColors(
true);
843 dockerTreeWidget->setColumnHidden(4,
true);
844 dockerTreeWidget->setColumnHidden(5,
true);
845 dockerTreeWidget->setColumnHidden(6,
true);
846 dockerTreeWidget->header()->setStretchLastSection(
false);
847 dockerTreeWidget->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
848 dockerTreeWidget->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
849 dockerTreeWidget->header()->setSectionResizeMode(2, QHeaderView::Stretch);
850 dockerTreeWidget->header()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
854 ui->verticalLayout_5->addWidget(dockerGroupBox);
856 connect(dockerRefreshBtn,
SIGNAL(
clicked(
bool)),
this,
SLOT(onRefreshDockerContainers()));
857 connect(dockerStartBtn,
SIGNAL(
clicked(
bool)),
this,
SLOT(onStartSelectedDockerContainers()));
858 connect(dockerStopBtn,
SIGNAL(
clicked(
bool)),
this,
SLOT(onStopSelectedDockerContainers()));
861void ClusterWidget::updateDockerButtons()
863 const bool hasSelection = dockerTreeWidget && !dockerTreeWidget->selectedItems().isEmpty();
870 std::vector<yarp::manager::DockerContainer>
containers;
881 std::array<char, 512>
buffer{};
902 std::stringstream
lines(output);
904 while (std::getline(
lines, line))
935 return system(cmd.c_str()) == 0;
938void ClusterWidget::onRefreshDockerContainers()
940 if (!dockerTreeWidget)
945 dockerTreeWidget->clear();
947 for (
const auto& node : cluster.nodes)
971 dockerTreeWidget->addTopLevelItem(
new QTreeWidgetItem(
row));
975 dockerTreeWidget->resizeColumnToContents(0);
976 dockerTreeWidget->resizeColumnToContents(1);
977 dockerTreeWidget->resizeColumnToContents(3);
978 updateDockerButtons();
979 updateYarprunDockerColors();
983void ClusterWidget::onStartSelectedDockerContainers()
988 container.
name =
item->text(1).toStdString();
989 container.
host =
item->text(4).toStdString();
990 container.
user =
item->text(5).toStdString();
993 if (!runDockerCommand(container,
"docker start"))
999 onRefreshDockerContainers();
1002void ClusterWidget::onStopSelectedDockerContainers()
1007 container.
name =
item->text(1).toStdString();
1008 container.
host =
item->text(4).toStdString();
1009 container.
user =
item->text(5).toStdString();
1012 if (!runDockerCommand(container,
"docker stop"))
1018 onRefreshDockerContainers();
1021std::string ClusterWidget::getSSHCmd(
const std::string &user,
const std::string &host,
const std::string &ssh_options,
bool background)
1024 cmd = background ?
"ssh -f" :
"ssh";
1025 if (!ssh_options.empty())
1027 cmd = cmd +
" " + ssh_options;
1031 cmd = cmd +
" " + host;
1035 cmd = cmd +
" " + user +
"@" +host;
1041bool ClusterWidget::checkNameserver()
1043 std::string name = ui->lineEditNs->text().toStdString();
1050 if (name.find(
'/') == std::string::npos)
1058 yError()<<
"ClusterWidget: yarpserver is not running";
1070 yError()<<
"ClusterWidget: Cannot contact the NameClient";
1073 if (reply.
size()==6)
1091bool ClusterWidget::checkNode(
const std::string &name)
1093 std::string portname = name;
1094 if (portname.find(
'/') == std::string::npos)
1096 portname =
"/" + portname;
1101 yError()<<
"ClusterWidget: yarpserver is not running";
1108 yError()<<
"ClusterWidget: port"<<portname<<
"is not responding";
1120 yError()<<
"ClusterWidget: Cannot contact the NameClient";
1123 if (reply.
size()==6)
1142void ClusterWidget::updateServerEntries()
1145 cluster.
user = ui->lineEditUser->text().simplified().trimmed().toStdString();
1146 cluster.
nsNode = ui->nsNodeComboBox->currentText().simplified().trimmed().toStdString();
1149void ClusterWidget::reportErrors()
1168 clusLoader =
nullptr;
int SIGNAL(int pid, int signum)
Singleton class ErrorLogger.
const char * getLastError()
void addError(const char *szError)
static ErrorLogger * Instance()
Singleton class ErrorLogger.
bool parseXmlFile(Cluster &_cluster)
A simple collection of objects that can be described and transmitted in a portable way.
size_type size() const
Gets the number of elements in the bottle.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
A mini-server for performing network communication in the background.
static bool checkNetwork()
Check if the YARP Network is up and running.
virtual bool asBool() const
Get boolean value.
static NameClient & getNameClient()
Get an instance of the name client.
static bool getPortDetails(const std::string &portName, PortDetails &info, double timeout=2.0)
Get the details (connections, owner process and machine) of a port.
void delay(double seconds)
Wait for a certain number of seconds.
An interface to the operating system, including Port based communication.
std::vector< ClusterNode > nodes