Salome HOME
Synchronize adm files
[modules/jobmanager.git] / src / genericgui / JM_EditSalomeResource.cxx
1 // Copyright (C) 2009-2014  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "JM_EditSalomeResource.hxx"
21 #include "BL_Traces.hxx"
22
23 using namespace std;
24
25 JM::EditSalomeResource::EditSalomeResource(QWidget *parent, BL::SALOMEServices * salome_services,
26                                            const std::string & resource_name) : QDialog(parent)
27 {
28   DEBTRACE("Creating JM::EditSalomeResource");
29   BL_ASSERT(parent);
30   BL_ASSERT(salome_services);
31   _parent = parent;
32   _salome_services = salome_services;
33   _resource_name = resource_name;
34
35   //setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
36
37   // Widget code
38
39   // Part 1
40   QGroupBox * main_groupBox = new QGroupBox("Main values");
41   QLabel * name_label = new QLabel("Name:");
42   _name_line = new QLineEdit(this);
43   QLabel * hostname_label = new QLabel("Hostname:");
44   _hostname_line = new QLineEdit(this);
45   QLabel * username_label = new QLabel("Username:");
46   _username_line = new QLineEdit(this);
47   QLabel * applipath_label = new QLabel("Applipath:");
48   _applipath_line = new QLineEdit(this);
49
50   QLabel * protocol_label = new QLabel("Protocol:");
51   _protocol_line = new QComboBox(this);
52   _protocol_line->addItem("ssh");
53   _protocol_line->addItem("rsh");
54   _protocol_line->addItem("sh");
55   _protocol_line->setCurrentIndex(0);
56
57   QLabel * componentList_label = new QLabel("Component List:");
58   _add_button = new QPushButton("Add");
59   _remove_button = new QPushButton("Remove");
60   _remove_button->setEnabled(false);
61   QWidget * component_widget = new QWidget(this);
62   _componentList = new QListWidget(this);
63   _componentList->setSelectionMode(QAbstractItemView::MultiSelection);
64   QGridLayout * input_box = new QGridLayout(this);
65   input_box->addWidget(_add_button, 0, 0);
66   input_box->addWidget(_remove_button, 0, 1);
67   input_box->addWidget(_componentList, 1, 0, 1, -1);
68   component_widget->setLayout(input_box);
69   connect(_add_button, SIGNAL(clicked()), this, SLOT(add_component()));
70   connect(_remove_button, SIGNAL(clicked()), this, SLOT(remove_components()));
71   connect(_componentList, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelectionChanged()));
72
73   QLabel * working_directory_label = new QLabel("Working Directory:");
74   _working_directory = new QLineEdit(this);
75   _can_launch_batch_jobs = new QCheckBox("This resource can be used to launch batch jobs", this);
76   _can_launch_batch_jobs->setCheckState(Qt::Checked);
77   _can_run_containers = new QCheckBox("This resource can be used to run interactive containers", this);
78
79   QGridLayout * m_layout = new QGridLayout;
80   m_layout->addWidget(name_label, 0, 0);
81   m_layout->addWidget(_name_line, 0, 1);
82   m_layout->addWidget(hostname_label, 1, 0);
83   m_layout->addWidget(_hostname_line, 1, 1);
84   m_layout->addWidget(protocol_label, 2, 0);
85   m_layout->addWidget(_protocol_line, 2, 1);
86   m_layout->addWidget(username_label, 3, 0);
87   m_layout->addWidget(_username_line, 3, 1);
88   m_layout->addWidget(applipath_label, 4, 0);
89   m_layout->addWidget(_applipath_line, 4, 1);
90   m_layout->addWidget(componentList_label, 5, 0);
91   m_layout->addWidget(component_widget, 5, 1);
92   m_layout->addWidget(working_directory_label, 6, 0);
93   m_layout->addWidget(_working_directory, 6, 1);
94   m_layout->addWidget(_can_launch_batch_jobs, 7, 1);
95   m_layout->addWidget(_can_run_containers, 8, 1);
96   main_groupBox->setLayout(m_layout);
97
98   // Part 2
99   QGroupBox * config_groupBox = new QGroupBox("Configuration values");
100   QLabel * os_label = new QLabel("OS:");
101   _os_line = new QLineEdit(this);
102
103   QLabel * mem_mb_label = new QLabel("Memory (mb):");
104   _mem_mb_line = new QSpinBox(this);
105   _mem_mb_line->setMinimum(0);
106   _mem_mb_line->setMaximum(1000000);
107   _mem_mb_line->setValue(0);
108   QLabel * cpu_clock_label = new QLabel("CPU Clock:");
109   _cpu_clock_line = new QSpinBox(this);
110   _cpu_clock_line->setMinimum(0);
111   _cpu_clock_line->setMaximum(1000000);
112   _cpu_clock_line->setValue(0);
113   QLabel * nb_node_label = new QLabel("Nb node:");
114   _nb_node_line = new QSpinBox(this);
115   _nb_node_line->setMinimum(1);
116   _nb_node_line->setMaximum(1000000);
117   _nb_node_line->setValue(1);
118   QLabel * nb_proc_per_node_label = new QLabel("Nb proc/node:");
119   _nb_proc_per_node_line = new QSpinBox(this);
120   _nb_proc_per_node_line->setMinimum(1);
121   _nb_proc_per_node_line->setMaximum(1000000);
122   _nb_proc_per_node_line->setValue(1);
123
124   QLabel * iprotocol_label = new QLabel("Internal protocol:");
125   _iprotocol_line = new QComboBox(this);
126   _iprotocol_line->addItem("ssh");
127   _iprotocol_line->addItem("rsh");
128   _iprotocol_line->addItem("srun");
129   _iprotocol_line->addItem("pbsdsh");
130   _iprotocol_line->addItem("blaunch");
131   _iprotocol_line->setCurrentIndex(0);
132
133   QLabel * batch_label = new QLabel("Batch Manager:");
134   _batch_line = new QComboBox(this);
135   _batch_line->addItem("None", "none");
136   _batch_line->addItem("CCC", "ccc");
137   _batch_line->addItem("LSF", "lsf");
138   _batch_line->addItem("SGE", "sge");
139   _batch_line->addItem("SLURM", "slurm");
140   _batch_line->addItem("CooRM (limited support)", "coorm");
141   _batch_line->addItem("LoadLeveler (limited support)", "ll");
142   _batch_line->addItem("OAR (limited support)", "oar");
143   _batch_line->addItem("PBS (limited support)", "pbs");
144   _batch_line->addItem("VISHNU (limited support)", "vishnu");
145   _batch_line->setCurrentIndex(0);
146
147   QLabel * mpiImpl_label = new QLabel("MPI impl:");
148   _mpiImpl_line = new QComboBox(this);
149   _mpiImpl_line->addItem("lam");
150   _mpiImpl_line->addItem("mpich1");
151   _mpiImpl_line->addItem("mpich2");
152   _mpiImpl_line->addItem("openmpi");
153   _mpiImpl_line->addItem("slurmmpi");
154   _mpiImpl_line->addItem("prun");
155   _mpiImpl_line->setCurrentIndex(-1);
156
157   QGridLayout * c_layout = new QGridLayout;
158   c_layout->addWidget(os_label, 0, 0);
159   c_layout->addWidget(_os_line, 0, 1);
160   c_layout->addWidget(mem_mb_label, 1, 0);
161   c_layout->addWidget(_mem_mb_line, 1, 1);
162   c_layout->addWidget(cpu_clock_label, 2, 0);
163   c_layout->addWidget(_cpu_clock_line, 2, 1);
164   c_layout->addWidget(nb_node_label, 3, 0);
165   c_layout->addWidget(_nb_node_line, 3, 1);
166   c_layout->addWidget(nb_proc_per_node_label, 4, 0);
167   c_layout->addWidget(_nb_proc_per_node_line, 4, 1);
168   c_layout->addWidget(batch_label, 5, 0);
169   c_layout->addWidget(_batch_line, 5, 1);
170   c_layout->addWidget(mpiImpl_label, 6, 0);
171   c_layout->addWidget(_mpiImpl_line, 6, 1);
172   c_layout->addWidget(iprotocol_label, 7, 0);
173   c_layout->addWidget(_iprotocol_line, 7, 1);
174   config_groupBox->setLayout(c_layout);
175
176   // Part 3
177   QDialogButtonBox * buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
178                                                       | QDialogButtonBox::Cancel);
179
180   connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
181   connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
182
183   // Main Layout
184   QVBoxLayout * mainLayout = new QVBoxLayout(this);
185   mainLayout->addWidget(main_groupBox);
186   mainLayout->addWidget(config_groupBox);
187   mainLayout->addWidget(buttonBox);
188   setLayout(mainLayout);
189
190   setWindowTitle("Edit/Add a resource");
191   if (_resource_name != "")
192     get_infos();
193 }
194
195 JM::EditSalomeResource::~EditSalomeResource()
196 {
197   DEBTRACE("Destroying JM::EditSalomeResource");
198 }
199
200 void
201 JM::EditSalomeResource::get_infos()
202 {
203   BL::ResourceDescr resource_descr = _salome_services->getResourceDescr(_resource_name);
204
205   _name_line->setText(QString(resource_descr.name.c_str()));
206   _hostname_line->setText(QString(resource_descr.hostname.c_str()));
207   _username_line->setText(QString(resource_descr.username.c_str()));
208   _applipath_line->setText(QString(resource_descr.applipath.c_str()));
209   _os_line->setText(QString(resource_descr.OS.c_str()));
210   _working_directory->setText(QString(resource_descr.working_directory.c_str()));
211
212   if (resource_descr.can_launch_batch_jobs)
213     _can_launch_batch_jobs->setCheckState(Qt::Checked);
214   else
215     _can_launch_batch_jobs->setCheckState(Qt::Unchecked);
216
217   if (resource_descr.can_run_containers)
218     _can_run_containers->setCheckState(Qt::Checked);
219   else
220     _can_run_containers->setCheckState(Qt::Unchecked);
221
222   std::string protocol = resource_descr.protocol.c_str();
223   if (protocol == "ssh")
224     _protocol_line->setCurrentIndex(0);
225   else if(protocol == "rsh")
226     _protocol_line->setCurrentIndex(1);
227   else if(protocol == "sh")
228     _protocol_line->setCurrentIndex(2);
229   else
230     _protocol_line->setCurrentIndex(-1);
231
232   std::string iprotocol = resource_descr.iprotocol.c_str();
233   if (iprotocol == "ssh")
234     _iprotocol_line->setCurrentIndex(0);
235   else if (iprotocol == "rsh")
236     _iprotocol_line->setCurrentIndex(1);
237   else if (iprotocol == "srun")
238     _iprotocol_line->setCurrentIndex(2);
239   else if (iprotocol == "pbsdsh")
240     _iprotocol_line->setCurrentIndex(3);
241   else if (iprotocol == "blaunch")
242     _iprotocol_line->setCurrentIndex(4);
243   else
244     _iprotocol_line->setCurrentIndex(-1);
245
246   for (int i=0 ; i<_batch_line->count() ; i++)
247   {
248     if (_batch_line->itemData(i).toString().toStdString() == resource_descr.batch)
249     {
250       _batch_line->setCurrentIndex(i);
251     }
252   }
253
254   std::string mpiImpl = resource_descr.mpiImpl.c_str();
255   if (mpiImpl == "lam")
256     _mpiImpl_line->setCurrentIndex(0);
257   else if (mpiImpl == "mpich1")  
258     _mpiImpl_line->setCurrentIndex(1);
259   else if (mpiImpl == "mpich2")  
260     _mpiImpl_line->setCurrentIndex(2);
261   else if (mpiImpl == "openmpi")
262     _mpiImpl_line->setCurrentIndex(3);
263   else if (mpiImpl == "slurmmpi")
264     _mpiImpl_line->setCurrentIndex(4);
265   else if (mpiImpl == "prun")
266     _mpiImpl_line->setCurrentIndex(5);
267   else  
268     _mpiImpl_line->setCurrentIndex(-1);
269
270   int value = resource_descr.mem_mb;
271   if (value > 0)
272     _mem_mb_line->setValue(value);
273   value = resource_descr.cpu_clock;
274   if (value > 0)
275     _cpu_clock_line->setValue(value);
276   value = resource_descr.nb_node;
277   if (value > 1)
278     _nb_node_line->setValue(value);
279   value = resource_descr.nb_proc_per_node;
280   if (value > 1)
281     _nb_proc_per_node_line->setValue(value);
282
283    std::list<std::string>::iterator it = resource_descr.componentList.begin();
284    for(; it != resource_descr.componentList.end(); it++)
285      _componentList->addItem(QString((*it).c_str()));
286    
287   // Better if string's length is bigger than the widget
288   _name_line->setCursorPosition(0);
289   _hostname_line->setCursorPosition(0);
290   _username_line->setCursorPosition(0);
291   _applipath_line->setCursorPosition(0);
292   _os_line->setCursorPosition(0);
293   _working_directory->setCursorPosition(0);
294 }
295
296
297 void 
298 JM::EditSalomeResource::itemSelectionChanged()
299 {
300   if (_componentList->selectedItems().size() > 0)
301     _remove_button->setEnabled(true);
302   else
303     _remove_button->setEnabled(false);
304 }
305
306 void 
307 JM::EditSalomeResource::add_component()
308 {
309   bool ok;
310   QString text = QInputDialog::getText(this, "Add a component",
311                                        "Component name:", QLineEdit::Normal,
312                                        "", &ok);
313   if (ok && !text.isEmpty())
314     _componentList->addItem(text);
315 }
316
317 void 
318 JM::EditSalomeResource::remove_components()
319 {
320   QList<QListWidgetItem *> list = _componentList->selectedItems();
321   for (int i = 0; i < list.size(); ++i)
322   {
323     int row = _componentList->row( list.at(i) );
324     delete _componentList->takeItem(row);
325   }
326 }
327
328 void
329 JM::EditSalomeResource::accept()
330 {
331   BL::ResourceDescr resource;
332
333   // Text
334   resource.name = _name_line->text().trimmed().toUtf8().constData();
335   resource.hostname = _hostname_line->text().trimmed().toStdString();
336   resource.username = _username_line->text().trimmed().toStdString();
337   resource.applipath = _applipath_line->text().trimmed().toUtf8().constData();
338   resource.OS = _os_line->text().trimmed().toStdString();
339   resource.working_directory = _working_directory->text().trimmed().toUtf8().constData();
340   resource.can_launch_batch_jobs = (_can_launch_batch_jobs->checkState() == Qt::Checked);
341   resource.can_run_containers = (_can_run_containers->checkState() == Qt::Checked);
342
343   // Components
344   int count = _componentList->count();
345   for (int i = 0; i < count; i++)
346     resource.componentList.push_back(_componentList->item(i)->text().trimmed().toStdString());
347
348   // ComboBox
349   resource.protocol = _protocol_line->currentText().toStdString();
350   resource.iprotocol = _iprotocol_line->currentText().toStdString();
351   resource.batch = _batch_line->itemData(_batch_line->currentIndex()).toString().toStdString();
352   resource.mpiImpl = _mpiImpl_line->currentText().toStdString();
353
354   // QSpinBox
355   resource.mem_mb = _mem_mb_line->value();
356   resource.cpu_clock = _cpu_clock_line->value();
357   resource.nb_node = _nb_node_line->value();
358   resource.nb_proc_per_node = _nb_proc_per_node_line->value();
359
360   if (resource.name != "" &&
361       resource.hostname != "" &&
362       resource.protocol != "")
363   {
364     try
365     {
366       _salome_services->addResource(resource);
367       QDialog::accept();
368     }
369     catch (const BL::Exception & ex)
370     {
371       QMessageBox::critical(this, "Error", QString("Cannot add resource: ") + ex.what());
372     }
373   }
374   else
375   {
376     QMessageBox::warning(NULL, "Values missing", "name, hostname and protocol are mandatory! Cancel or add values!");
377   }
378 }