Salome HOME
c78e1f609650f580d0e3b00d8ba3015a49ca85c0
[modules/jobmanager.git] / src / genericgui / JM_EditSalomeResource.cxx
1 // Copyright (C) 2009-2013  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.
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 JM::EditSalomeResource::EditSalomeResource(QWidget *parent, BL::SALOMEServices * salome_services,
24                                            const std::string & resource_name) : QDialog(parent)
25 {
26   DEBTRACE("Creating JM::EditSalomeResource");
27   BL_ASSERT(parent);
28   BL_ASSERT(salome_services);
29   _parent = parent;
30   _salome_services = salome_services;
31   _resource_name = resource_name;
32
33   //setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
34
35   // Widget code
36
37   // Part 1
38   QGroupBox * main_groupBox = new QGroupBox("Main values");
39   QLabel * name_label = new QLabel("Name:");
40   _name_line = new QLineEdit(this);
41   QLabel * hostname_label = new QLabel("Hostname:");
42   _hostname_line = new QLineEdit(this);
43   QLabel * username_label = new QLabel("Username:");
44   _username_line = new QLineEdit(this);
45   QLabel * applipath_label = new QLabel("Applipath:");
46   _applipath_line = new QLineEdit(this);
47
48   QLabel * protocol_label = new QLabel("Protocol:");
49   _protocol_line = new QComboBox(this);
50   _protocol_line->addItem("ssh");
51   _protocol_line->addItem("rsh");
52   _protocol_line->addItem("sh");
53   _protocol_line->addItem("srun");
54   _protocol_line->addItem("pbsdsh");
55   _protocol_line->addItem("blaunch");
56   _protocol_line->setCurrentIndex(0);
57
58   QLabel * componentList_label = new QLabel("Component List:");
59   _add_button = new QPushButton("Add");
60   _remove_button = new QPushButton("Remove");
61   _remove_button->setEnabled(false);
62   QWidget * component_widget = new QWidget(this);
63   _componentList = new QListWidget(this);
64   _componentList->setSelectionMode(QAbstractItemView::MultiSelection);
65   QGridLayout * input_box = new QGridLayout(this);
66   input_box->addWidget(_add_button, 0, 0);
67   input_box->addWidget(_remove_button, 0, 1);
68   input_box->addWidget(_componentList, 1, 0, 1, -1);
69   component_widget->setLayout(input_box);
70   connect(_add_button, SIGNAL(clicked()), this, SLOT(add_component()));
71   connect(_remove_button, SIGNAL(clicked()), this, SLOT(remove_components()));
72   connect(_componentList, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelectionChanged()));
73
74   QLabel * working_directory_label = new QLabel("Working Directory:");
75   _working_directory = new QLineEdit(this);
76   _can_launch_batch_jobs = new QCheckBox("This resource can be used to launch batch jobs", this);
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:");
134   _batch_line = new QComboBox(this);
135   _batch_line->addItem("pbs");
136   _batch_line->addItem("lsf");
137   _batch_line->addItem("sge");
138   _batch_line->addItem("ssh");
139   _batch_line->addItem("ccc");
140   _batch_line->addItem("slurm");
141   _batch_line->addItem("ll");
142   _batch_line->addItem("vishnu");
143   _batch_line->addItem("oar");
144   _batch_line->addItem("coorm");
145   _batch_line->setCurrentIndex(-1);
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 if(protocol == "srun")
230     _protocol_line->setCurrentIndex(3);
231   else if(protocol == "pbsdsh")
232     _protocol_line->setCurrentIndex(4);
233   else if(protocol == "blaunch")
234     _protocol_line->setCurrentIndex(5);
235   else
236     _protocol_line->setCurrentIndex(-1);
237
238   std::string iprotocol = resource_descr.iprotocol.c_str();
239   if (iprotocol == "ssh")
240     _iprotocol_line->setCurrentIndex(0);
241   else if (iprotocol == "rsh")
242     _iprotocol_line->setCurrentIndex(1);
243   else if (iprotocol == "srun")
244     _iprotocol_line->setCurrentIndex(2);
245   else if (iprotocol == "pbsdsh")
246     _iprotocol_line->setCurrentIndex(3);
247   else if (iprotocol == "blaunch")
248     _iprotocol_line->setCurrentIndex(4);
249   else
250     _iprotocol_line->setCurrentIndex(-1);
251
252   std::string batch = resource_descr.batch.c_str();
253   if (batch == "pbs")
254     _batch_line->setCurrentIndex(0);
255   else if (batch == "lsf")  
256     _batch_line->setCurrentIndex(1);
257   else if (batch == "sge")  
258     _batch_line->setCurrentIndex(2);
259   else if (batch == "ssh_batch")
260     _batch_line->setCurrentIndex(3);
261   else if (batch == "ccc")
262     _batch_line->setCurrentIndex(4);
263   else if (batch == "slurm")
264     _batch_line->setCurrentIndex(5);
265   else if (batch == "ll")
266     _batch_line->setCurrentIndex(6);
267   else if (batch == "vishnu")
268     _batch_line->setCurrentIndex(7);
269   else if (batch == "oar")
270     _batch_line->setCurrentIndex(8);
271   else if (batch == "coorm")
272     _batch_line->setCurrentIndex(9);
273   else
274     _batch_line->setCurrentIndex(-1);
275
276   std::string mpiImpl = resource_descr.mpiImpl.c_str();
277   if (mpiImpl == "lam")
278     _mpiImpl_line->setCurrentIndex(0);
279   else if (mpiImpl == "mpich1")  
280     _mpiImpl_line->setCurrentIndex(1);
281   else if (mpiImpl == "mpich2")  
282     _mpiImpl_line->setCurrentIndex(2);
283   else if (mpiImpl == "openmpi")
284     _mpiImpl_line->setCurrentIndex(3);
285   else if (mpiImpl == "slurmmpi")
286     _mpiImpl_line->setCurrentIndex(4);
287   else if (mpiImpl == "prun")
288     _mpiImpl_line->setCurrentIndex(5);
289   else  
290     _mpiImpl_line->setCurrentIndex(-1);
291
292   int value = resource_descr.mem_mb;
293   if (value > 0)
294     _mem_mb_line->setValue(value);
295   value = resource_descr.cpu_clock;
296   if (value > 0)
297     _cpu_clock_line->setValue(value);
298   value = resource_descr.nb_node;
299   if (value > 1)
300     _nb_node_line->setValue(value);
301   value = resource_descr.nb_proc_per_node;
302   if (value > 1)
303     _nb_proc_per_node_line->setValue(value);
304
305    std::list<std::string>::iterator it = resource_descr.componentList.begin();
306    for(; it != resource_descr.componentList.end(); it++)
307      _componentList->addItem(QString((*it).c_str()));
308    
309   // Better if string's length is bigger than the widget
310   _name_line->setCursorPosition(0);
311   _hostname_line->setCursorPosition(0);
312   _username_line->setCursorPosition(0);
313   _applipath_line->setCursorPosition(0);
314   _os_line->setCursorPosition(0);
315   _working_directory->setCursorPosition(0);
316 }
317
318
319 void 
320 JM::EditSalomeResource::itemSelectionChanged()
321 {
322   if (_componentList->selectedItems().size() > 0)
323     _remove_button->setEnabled(true);
324   else
325     _remove_button->setEnabled(false);
326 }
327
328 void 
329 JM::EditSalomeResource::add_component()
330 {
331   bool ok;
332   QString text = QInputDialog::getText(this, "Add a component",
333                                        "Component name:", QLineEdit::Normal,
334                                        "", &ok);
335   if (ok && !text.isEmpty())
336     _componentList->addItem(text);
337 }
338
339 void 
340 JM::EditSalomeResource::remove_components()
341 {
342   QList<QListWidgetItem *> list = _componentList->selectedItems();
343   for (int i = 0; i < list.size(); ++i)
344   {
345     int row = _componentList->row( list.at(i) );
346     delete _componentList->takeItem(row);
347   }
348 }
349
350 void
351 JM::EditSalomeResource::accept()
352 {
353   BL::ResourceDescr resource;
354
355   // Text
356   resource.name = _name_line->text().toStdString();
357   resource.hostname = _hostname_line->text().toStdString();
358   resource.username = _username_line->text().toStdString();
359   resource.applipath = _applipath_line->text().toStdString();
360   resource.OS = _os_line->text().toStdString();
361   resource.working_directory = _working_directory->text().toStdString();
362   resource.can_launch_batch_jobs = (_can_launch_batch_jobs->checkState() == Qt::Checked);
363   resource.can_run_containers = (_can_run_containers->checkState() == Qt::Checked);
364
365   // Components
366   int count = _componentList->count();
367   for (int i = 0; i < count; i++)
368     resource.componentList.push_back(_componentList->item(i)->text().toStdString());
369
370   // ComboBox
371   resource.protocol = _protocol_line->currentText().toStdString();
372   resource.iprotocol = _iprotocol_line->currentText().toStdString();
373   resource.batch = _batch_line->currentText().toStdString();
374   if (resource.batch == "ssh")
375     resource.batch = "ssh_batch";
376   resource.mpiImpl = _mpiImpl_line->currentText().toStdString();
377
378   // QSpinBox
379   resource.mem_mb = _mem_mb_line->value();
380   resource.cpu_clock = _cpu_clock_line->value();
381   resource.nb_node = _nb_node_line->value();
382   resource.nb_proc_per_node = _nb_proc_per_node_line->value();
383
384   if (resource.name != "" &&
385       resource.hostname != "" &&
386       resource.protocol != "")
387   {
388     try
389     {
390       _salome_services->addResource(resource);
391       QDialog::accept();
392     }
393     catch (const BL::Exception & ex)
394     {
395       QMessageBox::critical(this, "Error", QString("Cannot add resource: ") + ex.what());
396     }
397   }
398   else
399   {
400     QMessageBox::warning(NULL, "Values missing", "name, hostname and protocol are mandatory! Cancel or add values!");
401   }
402 }