Allow non-persistent setup as well as domain deletion
authorMichal Novotny <minovotn@redhat.com>
Fri, 22 Jul 2011 09:47:21 +0000 (11:47 +0200)
committerMichal Novotny <minovotn@redhat.com>
Fri, 22 Jul 2011 09:47:21 +0000 (11:47 +0200)
Signed-off-by: Michal Novotny <minovotn@redhat.com>

classes/language.php
classes/libvirt.php
lang/cs.php
lang/en.php
pages/details/screenshot.php
pages/domain-list.php
pages/new-vm.php

index 44342ea..cdb4e18 100644 (file)
                                        'dom_dumpxml' => 'Dump domain XML',
                                        'dom_editxml' => 'Edit domain XML',
                                        'dom_xmldesc' => 'Domain XML description',
+                                       'dom_undefine' => 'Undefine domain',
+                                       'dom_start_ok' => 'Domain has been started successfully',
+                                       'dom_start_err' => 'Error while starting domain',
+                                       'dom_shutdown_ok' => 'Command to shutdown domain sent successfully',
+                                       'dom_shutdown_err' => 'Error while sending shutdown command',
+                                       'dom_destroy_ok' => 'Domain has been destroyed successfully',
+                                       'dom_destroy_err' => 'Error while destroying domain',
+                                       'dom_undefine_ok' => 'Domain has been undefined successfully',
+                                       'dom_undefine_err' => 'Error while undefining domain',
+                                       'dom_define_changed' => 'Domain definition has been changed',
+                                       'dom_define_change_err' => 'Cannot change domain definition',
+                                       'dom_undefine_question' => 'You can delete the domain with or without disks assigned to the domain. If you select option to delete disks only the domain disks will get deleted and the CD-ROM images will be intact. Are you sure you want to delete (undefine) this domain?',
+                                       'delete' => 'Delete',
+                                       'delete_with_disks' => 'Delete with disks',
                                        'changed_uri' => 'Changed connection URI to',
                                        'click_reload' => 'Click here to reload and connect using new URI',
                                        'conn_saved' => 'Connection has been saved to the list.',
                                        'setup' => 'Setup',
                                        'nic' => 'network',
                                        'disk' => 'disk',
+                                       'persistent' => 'Set as persistent',
                                        'new-vm-disk' => 'VM Disk',
                                        'new-vm-existing' => 'Use existing disk image',
                                        'new-vm-create' => 'Create new disk image',
index 02b48e3..3bb9168 100644 (file)
                        return ($tmp) ? $tmp : $this->_set_last_error();
                }
 
-               function domain_new($name, $img, $vcpus, $features, $mem, $maxmem, $clock, $nic, $disk) {
+               function domain_new($name, $img, $vcpus, $features, $mem, $maxmem, $clock, $nic, $disk, $persistent=true) {
                        $uuid = $this->domain_generate_uuid_unique();
                        $emulator = $this->get_default_emulator();
-                       $name = str_replace(' ', '', $name);
 
                        $mem *= 1024;
                        $maxmem *= 1024;
 
                        $diskstr = '';
                        if (!empty($disk)) {
-                               if ($disk['size'])
+                               if ($disk['size']) {
+                                       $disk['image'] = str_replace(' ', '_', $disk['image']);
                                        if (!$this->create_image($disk['image'], $disk['size'], $disk['driver']))
                                                return false;
+                               }
 
                                if ($disk['image'][0] != '/')
                                        $path = ini_get('libvirt.image_path').'/'.$disk['image'];
                        if (!$tmp)
                                return $this->_set_last_error();
 
-                       $xml = "<domain type='kvm'>
-                               <name>$name</name>
-                               <currentMemory>$mem</currentMemory>
-                               <memory>$maxmem</memory>
-                               <uuid>$uuid</uuid>
-                               <os>
-                                       <type arch='i686'>hvm</type>
-                                       <boot dev='hd'/>
-                               </os>
-                               <features>
-                               $fs
-                               </features>
-                               <clock offset=\"$clock\"/>
-                               <on_poweroff>destroy</on_poweroff>
-                               <on_reboot>destroy</on_reboot>
-                               <on_crash>destroy</on_crash>
-                               <vcpu>$vcpus</vcpu>
-                               <devices>
-                                       <emulator>$emulator</emulator>
-                                       $diskstr
-                                       $netstr
-                                       <input type='mouse' bus='ps2'/>
-                                       <graphics type='vnc' port='-1'/>
-                                       <console type='pty'/>
-                                       <sound model='ac97'/>
-                                       <video>
-                                               <model type='cirrus'/>
-                                       </video>
-                               </devices>
-                               </domain>";
+                       if ($persistent) {
+                               $xml = "<domain type='kvm'>
+                                       <name>$name</name>
+                                       <currentMemory>$mem</currentMemory>
+                                       <memory>$maxmem</memory>
+                                       <uuid>$uuid</uuid>
+                                       <os>
+                                               <type arch='i686'>hvm</type>
+                                               <boot dev='hd'/>
+                                       </os>
+                                       <features>
+                                       $fs
+                                       </features>
+                                       <clock offset=\"$clock\"/>
+                                       <on_poweroff>destroy</on_poweroff>
+                                       <on_reboot>destroy</on_reboot>
+                                       <on_crash>destroy</on_crash>
+                                       <vcpu>$vcpus</vcpu>
+                                       <devices>
+                                               <emulator>$emulator</emulator>
+                                               $diskstr
+                                               $netstr
+                                               <input type='mouse' bus='ps2'/>
+                                               <graphics type='vnc' port='-1'/>
+                                               <console type='pty'/>
+                                               <sound model='ac97'/>
+                                               <video>
+                                                       <model type='cirrus'/>
+                                               </video>
+                                       </devices>
+                                       </domain>";
                                
-                       $tmp = libvirt_domain_define_xml($this->conn, $xml);
-                       return ($tmp) ? $tmp : $this->_set_last_error();
+                               $tmp = libvirt_domain_define_xml($this->conn, $xml);
+                               return ($tmp) ? $tmp : $this->_set_last_error();
+                       }
+                       else
+                               return $tmp;
                }
 
                function create_image($image, $size, $driver) {
                         return ($tmp) ? $tmp : $this->_set_last_error();
                }
 
+               function remove_image($image, $ignore_error_codes=false ) {
+                       $tmp = libvirt_image_remove($this->conn, $image);
+                       if ((!$tmp) && ($ignore_error_codes)) {
+                               $err = libvirt_get_last_error();
+                               $comps = explode(':', $err);
+                               $err = explode('(', $comps[sizeof($comps)-1]);
+                               $code = (int)Trim($err[0]);
+
+                               if (in_array($code, $ignore_error_codes))
+                                       return true;
+                       }
+
+                       return ($tmp) ? $tmp : $this->_set_last_error();
+               }
+
                function generate_connection_uri($hv, $remote, $remote_method, $remote_username, $remote_hostname, $session=false) {
                        if ($hv == 'qemu') {
                                if ($session)
                        $buses =  $this->get_xpath($dom, '//domain/devices/disk[@device="disk"]/target/@bus', false);
                        $disks =  $this->get_xpath($dom, '//domain/devices/disk[@device="disk"]/target/@dev', false);
                        $files =  $this->get_xpath($dom, '//domain/devices/disk[@device="disk"]/source/@file', false);
-                       // create image as: qemu-img create -f qcow2 -o backing_file=RAW_IMG OUT_QCOW_IMG SIZE[K,M,G suffixed]
 
                        $ret = array();
                        for ($i = 0; $i < $disks['num']; $i++) {
index 3d1d645..6363cd2 100644 (file)
                                        'dom_dumpxml' => 'Zobrazit XML konfiguraci domény',
                                        'dom_editxml' => 'Upravit XML konfiguraci domény',
                                        'dom_xmldesc' => 'XML konfigurace domény',
+                                       'dom_start_ok' => 'Doména byla nastartována',
+                                       'dom_start_err' => 'Nastala chyba při startování domény',
+                                       'dom_shutdown_ok' => 'Příkaz k ukončení domény byl zaslán',
+                                       'dom_shutdown_err' => 'Nastala chyba při zasílání ukončovacího příkazu',
+                                       'dom_destroy_ok' => 'Doména byla ukončena',
+                                       'dom_destroy_err' => 'Nastala chyba při ukončování domény',
+                                       'dom_undefine_ok' => 'Doména byla úspěšně smazána',
+                                       'dom_undefine_err' => 'Nastala chyba při mazání domény',
+                                       'dom_define_changed' => 'Definice domény byla změněna',
+                                       'dom_define_change_err' => 'Nezdařilo se změnit definici domény',
+                                       'dom_undefine' => 'Smazat doménu',
+                                       'dom_undefine_question' => 'Doménu je možné smazat bez disků i včetně disků. Pokud zvolíte možnost smazání disků, pouze disky přiřazené danému virtuálnímu stroji budou smazané. Obrazy CD-ROM budou netknuté. Skutečně si přejete doménu smazat?',
+                                       'delete' => 'Smazat',
+                                       'delete_with_disks' => 'Smazat i s disky',
                                        'changed_uri' => 'Změněno nastavení připojovacího řetězce URI na ',
                                        'click_reload' => 'Klikněte zde pro obnovení a připojení pomocí nového připojovacího řetězce',
                                        'conn_saved' => 'Připojení bylo uloženo',
                                        'setup' => 'Nastavit',
                                        'nic' => 'síť',
                                        'disk' => 'disk',
+                                       'persistent' => 'Nastavit jako trvalé spojení',
                                        'new-vm-disk' => 'VM Disk',
                                        'new-vm-existing' => 'Použít existující disk',
                                        'new-vm-create' => 'Vytvořit nový disk',
index 73734ee..fad8ac3 100644 (file)
                                        'dom_dumpxml' => 'Dump domain XML',
                                        'dom_editxml' => 'Edit domain XML',
                                        'dom_xmldesc' => 'Domain XML description',
+                                       'dom_undefine' => 'Undefine domain',
+                                       'dom_start_ok' => 'Domain has been started successfully',
+                                       'dom_start_err' => 'Error while starting domain',
+                                       'dom_shutdown_ok' => 'Command to shutdown domain sent successfully',
+                                       'dom_shutdown_err' => 'Error while sending shutdown command',
+                                       'dom_destroy_ok' => 'Domain has been destroyed successfully',
+                                       'dom_destroy_err' => 'Error while destroying domain',
+                                       'dom_undefine_ok' => 'Domain has been undefined successfully',
+                                       'dom_undefine_err' => 'Error while undefining domain',
+                                       'dom_define_changed' => 'Domain definition has been changed',
+                                       'dom_define_change_err' => 'Cannot change domain definition',
+                                       'dom_undefine_question' => 'You can delete the domain with or without disks assigned to the domain. If you select option to delete disks only the domain disks will get deleted and the CD-ROM images will be intact. Are you sure you want to delete (undefine) this domain?',
+                                       'delete' => 'Delete',
+                                       'delete_with_disks' => 'Delete with disks',
                                        'changed_uri' => 'Changed connection URI to',
                                        'click_reload' => 'Click here to reload and connect using new URI',
                                        'conn_saved' => 'Connection has been saved to the list.',
                                        'setup' => 'Setup',
                                        'nic' => 'network',
                                        'disk' => 'disk',
+                                       'persistent' => 'Set as persistent',
                                        'new-vm-disk' => 'VM Disk',
                                        'new-vm-existing' => 'Use existing disk image',
                                        'new-vm-create' => 'Create new disk image',
index c334e96..e1526e2 100644 (file)
         function update_screenshot() {
                 src = "<?= $_SERVER['REQUEST_URI'].'&data=png' ?>";
                 var date = new Date();
-                src = src + '&date=' + date.getTime()
+                src = src + '&date=' + date.getTime()+'-'+Math.random()
                 document.getElementById('screenshot').src = src;
 
                 clearTimeout(timerId);
index 5fac928..010af28 100644 (file)
@@ -5,20 +5,55 @@
   $frm = '';
   if ($action == 'domain-start') {
     $name = $_GET['dom'];
-    $msg = $lv->domain_start($name) ? 'Domain has been started successfully' :
-               'Error while starting domain: '.$lv->get_last_error();
+    $msg = $lv->domain_start($name) ? $lang->get('dom_start_ok') :
+           $lang->get('dom_start_err').': '.$lv->get_last_error();
   }
 
   if ($action == 'domain-stop') {
     $name = $_GET['dom'];
-    $msg = $lv->domain_shutdown($name) ? 'Domain command to stop has been completed successfully' :
-               'Error while stopping domain: '.$lv->get_last_error();
+    $msg = $lv->domain_shutdown($name) ? $lang->get('dom_shutdown_ok') :
+           $lang->get('dom_shutdown_err').': '.$lv->get_last_error();
   }
 
   if ($action == 'domain-destroy') {
     $name = $_GET['dom'];
-    $msg = $lv->domain_destroy($name) ? 'Domain has been destroyed successfully' :
-                'Error while destroying domain: '.$lv->get_last_error();
+    $msg = $lv->domain_destroy($name) ? $lang->get('dom_destroy_ok') :
+           $lang->get('dom_destroy_err').': '.$lv->get_last_error();
+  }
+
+  if ($action == 'domain-undefine') {
+    $name = $_GET['dom'];
+    if ((!array_key_exists('confirmed', $_GET)) || ($_GET['confirmed'] != 1)) {
+        $frm = '<div class="section">'.$lang->get('dom_undefine').'</div>
+                <table id="form-table">
+                <tr>
+                  <td colspan="3">'.$lang->get('dom_undefine_question').' '.$lang->get('name').': <u>'.$name.'</u></td>
+                </tr>
+                <tr align="center">
+                  <td><a href="'.$_SERVER['REQUEST_URI'].'&amp;confirmed=1">'.$lang->get('delete').'</a></td>
+                  <td><a href="'.$_SERVER['REQUEST_URI'].'&amp;confirmed=1&amp;deldisks=1">'.$lang->get('delete_with_disks').'</a></td>
+                  <td><a href="?page='.$page.'">'.$lang->get('No').'</a></td>
+                </td>
+                </table>';
+    }
+    else {
+       $err = '';
+       if (array_key_exists('deldisks', $_GET) && $_GET['deldisks'] == 1) {
+               $disks = $lv->get_disk_stats($name);
+
+               for ($i = 0; $i < sizeof($disks); $i++) {
+                       $img = $disks[$i]['file'];
+
+                       if (!$lv->remove_image($img, array(2) ))
+                               $err .= $img.': '.$lv->get_last_error();
+               }
+       }
+        $msg = $lv->domain_undefine($name) ? $lang->get('dom_undefine_ok') :
+               $lang->get('dom_undefine_err').': '.$lv->get_last_error();
+
+       if ($err)
+               $msg .= ' (err: '.$err.')';
+    }
   }
 
   if ($action == 'domain-dump') {
@@ -39,8 +74,8 @@
     $inactive = (!$lv->domain_is_running($name)) ? true : false;
 
     if (array_key_exists('xmldesc', $_POST)) {
-        $msg = $lv->domain_change_xml($name, $_POST['xmldesc']) ? 'Domain definition has been changed' :
-                                      'Error changing domain definition: '.$lv->get_last_error();
+        $msg = $lv->domain_change_xml($name, $_POST['xmldesc']) ? $lang->get('dom_define_changed') :
+               $lang->get('dom_define_change_err').': '.$lv->get_last_error();
 
     }
     else {
                                        $actions  = '<a href="?page='.$page.'&amp;action=domain-start&amp;dom='.$name.'">'.$lang->get('dom_start').'</a> | ';
                                        $actions .= '<a href="?page='.$page.'&amp;action=domain-dump&amp;dom='.$name.'">'.$lang->get('dom_dumpxml').'</a> | ';
                                        $actions .= '<a href="?page='.$page.'&amp;action=domain-edit&amp;dom='.$name.'">'.$lang->get('dom_editxml').'</a> | ';
+                                       $actions .= '<a href="?page='.$page.'&amp;action=domain-undefine&amp;dom='.$name.'">'.$lang->get('dom_undefine').'</a> | ';
 
                                        $actions[ strlen($actions) - 2 ] = ' ';
                                        $actions = Trim($actions);
index 6eb8859..7225dcf 100644 (file)
@@ -35,7 +35,7 @@
                }
        }
 
-       $tmp = $lv->domain_new($_POST['name'], $img, $_POST['cpu_count'], $feature, $_POST['memory'], $_POST['maxmem'], $_POST['clock_offset'], $nic, $disk);
+       $tmp = $lv->domain_new($_POST['name'], $img, $_POST['cpu_count'], $feature, $_POST['memory'], $_POST['maxmem'], $_POST['clock_offset'], $nic, $disk, $_POST['setup_persistent']);
        if (!$tmp)
                $msg = $lv->get_last_error();
        else {
        <td>hda</td>
 </tr>
 
+<tr>
+    <td align="right"><?= $lang->get('persistent') ?>:</td>
+    <td>
+      <select name="setup_persistent">
+        <option value="0"><?= $lang->get('No') ?></option>
+        <option value="1" selected="selected"><?= $lang->get('Yes') ?></option>
+      </select>
+    </td>
+</tr>
+
 </div>
 
 <tr align="center">