Polish code and divide functionality into libvirt-{domain|info|network} classes
[php-virt-control.git] / classes / libvirt-domain.php
1 <?php
2         class LibvirtDomain extends LoggerBase {
3                 private $data = false;
4                 private $lang = false;
5                 private $lv = false;
6
7                 function LibvirtDomain($libvirtInstance, $languageInstance, $action = false, $input = array()) {
8                         $this->lv = $libvirtInstance;
9                         $this->lang = $languageInstance;
10
11                         if ($action)
12                                 $this->_processAction($action, $input);
13                 }
14
15                 function getData() {
16                         return $this->data;
17                 }
18
19                 function _processAction($action, $input = array()) {
20                         if (!$action)
21                                 return $this->log(TYPE_ERROR, __CLASS__.'::'.__FUNCTION__, 'No action defined', 'Cannot process empty action');
22
23                         $msg = false;
24                         $frm = false;
25                         $xml = false;
26
27                         $lv = $this->lv;
28                         $lang = $this->lang;
29
30                         if ($action == 'domain-start') {
31                                 if (!array_key_exists('dom', $input))
32                                         return $this->log(TYPE_ERROR, __CLASS__.'::'.__FUNCTION__, 'Domain start failed', 'Domain name not present');
33
34                                 $name = $input['dom'];
35                                 $msg = $lv->domain_start($name) ? $lang->get('dom-start-ok') :
36                                         $lang->get('dom-start-err').': '.$lv->get_last_error();
37                         }
38
39                         if ($action == 'domain-stop') {
40                                 if (!array_key_exists('dom', $input))
41                                         return $this->log(TYPE_ERROR, __CLASS__.'::'.__FUNCTION__, 'Domain shutdown failed', 'Domain name not present');
42
43                                 $name = $input['dom'];
44                                 $msg = $lv->domain_shutdown($name) ? $lang->get('dom-shutdown-ok') :
45                                         $lang->get('dom-shutdown-err').': '.$lv->get_last_error();
46                         }
47
48                         if ($action == 'domain-destroy') {
49                                 if (!array_key_exists('dom', $input))
50                                         return $this->log(TYPE_ERROR, __CLASS__.'::'.__FUNCTION__, 'Domain destroy failed', 'Domain name not present');
51
52                                 $name = $input['dom'];
53                                 $msg = $lv->domain_destroy($name) ? $lang->get('dom-destroy-ok') :
54                                         $lang->get('dom-destroy-err').': '.$lv->get_last_error();
55                         }
56
57                         if (($action == 'domain-undefine') && (verify_user($db, USER_PERMISSION_VM_DELETE))) {
58                                 if (!array_key_exists('dom', $input))
59                                         return $this->log(TYPE_ERROR, __CLASS__.'::'.__FUNCTION__, 'Domain undefine failed', 'Domain name not present');
60
61                                 $name = $input['dom'];
62                                 if ((!array_key_exists('confirmed', $_GET)) || ($_GET['confirmed'] != 1)) {
63                                         $frm = '<div class="section">'.$lang->get('dom-undefine').'</div>
64                                                 <table id="form-table">
65                                                 <tr>
66                                                   <td colspan="3">'.$lang->get('dom-undefine-question').' '.$lang->get('name').': <u>'.$name.'</u></td>
67                                                 </tr>
68                                                 <tr align="center">
69                                                   <td><a href="'.$_SERVER['REQUEST_URI'].'&amp;confirmed=1">'.$lang->get('delete').'</a></td>
70                                                   <td><a href="'.$_SERVER['REQUEST_URI'].'&amp;confirmed=1&amp;deldisks=1">'.$lang->get('delete-with-disks').'</a></td>
71                                                   <td><a href="?page='.$page.'">'.$lang->get('No').'</a></td>
72                                                 </td>
73                                                 </table>';
74                                 }
75                                 else {
76                                         $err = '';
77                                         if (array_key_exists('deldisks', $_GET) && $_GET['deldisks'] == 1) {
78                                                 $disks = $lv->get_disk_stats($name);
79
80                                                 for ($i = 0; $i < sizeof($disks); $i++) {
81                                                         $img = $disks[$i]['file'];
82
83                                                         if (!$lv->remove_image($img, array(2) ))
84                                                                 $err .= $img.': '.$lv->get_last_error();
85                                                 }
86                                         }
87
88                                         $msg = $lv->domain_undefine($name) ? $lang->get('dom-undefine-ok') :
89                                                 $lang->get('dom-undefine-err').': '.$lv->get_last_error();
90
91                                         if ($err)
92                                                 $msg .= ' (err: '.$err.')';
93                                 }
94                         }
95
96                         if ($action == 'domain-dump') {
97                                 if (!array_key_exists('dom', $input))
98                                         return $this->log(TYPE_ERROR, __CLASS__.'::'.__FUNCTION__, 'Domain dump failed', 'Domain name not present');
99
100                                 $name = $input['dom'];
101                                 $inactive = (!$lv->domain_is_running($name)) ? true : false;
102
103                                 $xml = $lv->domain_get_xml($name, $inactive);
104                                 $frm = '<div class="section">'.$lang->get('dom-xmldesc').' - <i>'.$name.'</i></div><form method="POST">
105                                         <table id="form-table"><tr><td>'.$lang->get('dom-xmldesc').': </td>
106                                         <td><textarea readonly="readonly" name="xmldesc" rows="25" cols="90%">'.$xml.'</textarea></td></tr><tr align="center"><td colspan="2">
107                                         </tr></form></table>';
108                         }
109
110                         if ($action == 'domain-migrate') {
111                                 if (!array_key_exists('dom', $input))
112                                         return $this->log(TYPE_ERROR, __CLASS__.'::'.__FUNCTION__, 'Domain migration failed', 'Domain name not present');
113
114                                 $name = $input['dom'];
115                                 if (!array_key_exists('dest-uri', $_POST)) {
116                                         $uris = array();
117
118                                         if (isset($conns) && $conns) {
119                                                 foreach ($conns as $conn) {
120                                                         if ($conn['connection_uri'] != $uri)
121                                                                 $uris[] = array(
122                                                                         'id' => $conn['id'],
123                                                                         'name' => $conn['connection_name']
124                                                                         );
125                                                 }
126                                         }
127                                         else
128                                                 $uris = array();
129
130                                         if (sizeof($uris) == 0)
131                                                 echo $lang->get('no-destination-present');
132                                         else {
133                                                 echo "<form method='POST'>".$lang->get('choose-destination')." ($name): <br /><select name='dest-uri' style='width: 150px'>";
134
135                                                 foreach ($uris as $cn)
136                                                         echo "<option value=\"${cn['id']}\">{$cn['name']}</option>";
137
138                                                 echo "</select><br /><input type='submit' value='".$lang->get('dom-migrate')."'>";
139                                         }
140                                 }
141                                 else {
142                                         $arr = false;
143
144                                         for ($i = 0; $i < sizeof($conns); $i++) {
145                                                 if ($conns[$i]['id'] == $_POST['dest-uri']) {
146                                                         $arr = $conns[$i];
147                                                         break;
148                                                 }
149                                         }
150
151                                         if ($arr && (!$lv->migrate($name, $arr)))
152                                                 echo '<b>'.$lang->get('error-page-title').'</b>: '.$lv->get_last_error();
153                                 }
154                         }
155
156                         if ($action == 'domain-edit') {
157                                 if (!array_key_exists('dom', $input))
158                                         return $this->log(TYPE_ERROR, __CLASS__.'::'.__FUNCTION__, 'Domain editing failed', 'Domain name not present');
159
160                                 $name = $input['dom'];
161                                 $inactive = (!$lv->domain_is_running($name)) ? true : false;
162
163                                 if (array_key_exists('xmldesc', $_POST)) {
164                                         $msg = $lv->domain_change_xml($name, $_POST['xmldesc']) ? $lang->get('dom-define-changed') :
165                                                 $lang->get('dom-define-change-err').': '.$lv->get_last_error();
166                                 }
167                                 else {
168                                         $xml = $lv->domain_get_xml($name, $inactive);
169                                         $frm = '<div class="section">'.$lang->get('dom-editxml').' - <i>'.$name.'</i></div><form method="POST"><table id="form-table"><tr><td>'.$lang->get('dom-xmldesc').': </td>
170                                                 <td><textarea name="xmldesc" rows="25" cols="90%">'.$xml.'</textarea></td></tr><tr align="center"><td colspan="2">
171                                                 <input type="submit" value=" '.$lang->get('dom-editxml').' "></tr></form></table>';
172                                 }
173                         }
174
175                         $this->data = array(
176                                         'msg' => $msg,
177                                         'frm' => $frm,
178                                         'xml' => $xml
179                                         );
180                 }
181
182                 function createNewVM($input) {
183                         $lv = $this->lv;
184                         $lang = $this->lang;
185
186                         $skip = false;
187                         $msg = false;
188                         if (array_key_exists('sent', $input)) {
189                                 $features = array('apic', 'acpi', 'pae', 'hap');
190                                 $iso_path = ini_get('libvirt.iso_path');
191                                 $img = $iso_path.'/'.$input['install_img'];
192
193                                 $feature = array();
194                                 for ($i = 0; $i < sizeof($features); $i++)
195                                         if (array_key_exists('feature_'.$features[$i], $input))
196                                                 $feature[] = $features[$i];
197
198                                 $nic = array();
199                                 if ($input['setup_nic']) {
200                                         $nic['mac'] = $input['nic_mac'];
201                                         $nic['type'] = $input['nic_type'];
202                                         $nic['network'] = $input['nic_net'];
203                                 }
204                                 $disk = array();
205                                 if ($input['setup_disk']) {
206                                         if ($input['new_vm_disk']) {
207                                                 $disk['image'] = $input['name'].'.'.$input['disk_driver'];
208                                                 $disk['size'] = (int)$input['img_data'];
209                                                 $disk['bus'] = $input['disk_bus'];
210                                                 $disk['driver'] = $input['disk_driver'];
211                                         }
212                                         else {
213                                                 $disk['image'] = $input['img_data'];
214                                                 $disk['size'] = 0;
215                                                 $disk['bus'] = $input['disk_bus'];
216                                                 $disk['driver'] = $input['disk_driver'];
217                                         }
218                                 }
219
220                                 $tmp = $lv->domain_new($input['name'], $img, $input['cpu_count'], $feature, $input['memory'], $input['maxmem'], $input['clock_offset'], $nic, $disk, $input['setup_persistent']);
221                                 if (!$tmp)
222                                         $msg = $lv->get_last_error();
223                                 else {
224                                         $skip = true;
225                                         $msg = $lang->get('new-vm-created');
226                                 }
227                         }
228
229                         $isos = libvirt_get_iso_images();
230
231                         if (empty($isos))
232                                 $msg = $lang->get('no-iso');
233
234                         $ci  = $lv->get_connect_information();
235                         $maxvcpu = $ci['hypervisor_maxvcpus'];
236                         unset($ci);
237
238                         return array(
239                                                 'skip' => $skip,
240                                                 'msg' => $msg,
241                                                 'isos' => $isos,
242                                                 'maxvcpu' => $maxvcpu
243                                         );
244                 }
245         }
246 ?>