Pad optional rows in the creation scripts and add option to generate new MAC address
authorMichal Novotny <minovotn@redhat.com>
Fri, 12 Aug 2011 10:47:06 +0000 (12:47 +0200)
committerMichal Novotny <minovotn@redhat.com>
Fri, 12 Aug 2011 10:47:06 +0000 (12:47 +0200)
Signed-off-by: Michal Novotny <minovotn@redhat.com>

classes/language.php
data/mysql_conn.php [deleted file]
index.php
lang/cs.php
lang/en.php
pages/network-list.php
pages/new-net.php
pages/new-vm.php

index 073d7b9..ae0850f 100644 (file)
                                        'net_undefine_err' => 'An error occurred while undefining the network',
                                        'net_ip_range_def' => 'IP Range definition',
                                        'net_ip_cidr' => 'CIDR (192.168.111.0/24)',
-                                       'net_ip_direct' => 'Direct (2 values)',
+                                       'net_ip_direct' => 'IP and mask definition',
                                        'net_ipdef_cidr' => 'CIDR Definition',
                                        'dhcp' => 'DHCP',
                                        'net_dhcp_start' => 'DHCP Range start',
                                        'net_forward_none' => 'none',
                                        'net_forward_nat' => 'NAT',
                                        'net_forward_route' => 'Route',
+                                       'net_forward_dev_empty_msg' => 'Leave empty for forwarding to any device/interface',
+                                       'net_forward_dev_any' => 'any interface / device',
                                        'net_created' => 'Network has been created successfully',
                                        'network-add-ok' => 'Network card has been successfully added to the guest. Changes will take effect on next domain startup.',
                                        'network-add-error' => 'Cannot add NIC to the guest',
+                                       'network-generate-mac' => 'Generate new MAC address',
                                        'network-remove-ok' => 'Network card has been removed successfully. Changes will take effect on next domain startup.',
                                        'network-remove-error' => 'Cannot remove disk',
                                        );
diff --git a/data/mysql_conn.php b/data/mysql_conn.php
deleted file mode 100644 (file)
index 1a957d9..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-<?php
-       $server = 'localhost';
-       $user = 'root';
-       $password = 'password';
-       $dbname = 'php-virt-control';
-       $prefix = '';
-?>
index f42132c..36f5ba2 100644 (file)
--- a/index.php
+++ b/index.php
 
        $errmsg = false;
        $lv = new Libvirt($uri, $lg, $lang_str);
+
+       /* Get new MAC address in plain text - called by Ajax from pages/new-vm.php */
+       if (array_key_exists('get_mac', $_GET)) {
+               die( $lv->generate_random_mac_addr() );
+       }
+
        if ($lv->get_last_error()) {
                $page = 'overview';
                $name = false;
index 7f99f09..0f996db 100644 (file)
                                        'net_undefine_err' => 'Nastala chyba při mazání sítě',
                                        'net_ip_range_def' => 'Definice rozsahu',
                                        'net_ip_cidr' => 'CIDR (192.168.111.0/24)',
-                                       'net_ip_direct' => 'Přímá definice (2 hodnoty)',
+                                       'net_ip_direct' => 'Definice IP adresy a masky',
                                        'net_ipdef_cidr' => 'CIDR Definice',
                                        'dhcp' => 'DHCP',
                                        'net_dhcp_start' => 'Počátek DHCP rozsahu',
                                        'net_forward_none' => 'žádné',
                                        'net_forward_nat' => 'NAT',
                                        'net_forward_route' => 'Route',
+                                       'net_forward_dev_empty_msg' => 'Nevyplňujte pro možnost směrování na jakékoli zařízení',
+                                       'net_forward_dev_any' => 'jakékoli zařízení',
                                        'net_created' => 'Síť byla vytvořena úspěšně',
                                        'network-add-ok' => 'Síťová karta byla úspěšně přidána. Změny se projeví po dalším restartu domény.',
                                        'network-add-error' => 'Nelze přidat síťovou kartu do domény',
+                                       'network-generate-mac' => 'Vygenerovat novou MAC adresu',
                                        'network-remove-ok' => 'Síťové karta byla úspěšně odebrána. Změny se projeví po dalším restartu domény.',
                                        'network-remove-error' => 'Nelze odebrat síťovou kartu',
                                        );
index bb7e0da..f3473df 100644 (file)
                                        'net_undefine_err' => 'An error occurred while undefining the network',
                                        'net_ip_range_def' => 'Range definition',
                                        'net_ip_cidr' => 'CIDR (192.168.111.0/24)',
-                                       'net_ip_direct' => 'Direct (2 values)',
+                                       'net_ip_direct' => 'IP and mask definition',
                                        'net_ipdef_cidr' => 'CIDR Definition',
                                        'dhcp' => 'DHCP',
                                        'net_dhcp_start' => 'DHCP Range start',
                                        'net_forward_none' => 'none',
                                        'net_forward_nat' => 'NAT',
                                        'net_forward_route' => 'Route',
+                                       'net_forward_dev_empty_msg' => 'Leave empty for forwarding to any device/interface',
+                                       'net_forward_dev_any' => 'any interface / device',
                                        'net_created' => 'Network has been created successfully',
                                        'network-add-ok' => 'Network card has been successfully added to the guest. Changes will take effect on next domain startup.',
                                        'network-add-error' => 'Cannot add NIC to the guest',
+                                       'network-generate-mac' => 'Generate new MAC address',
                                        'network-remove-ok' => 'Network card has been removed successfully. Changes will take effect on next domain startup.',
                                        'network-remove-error' => 'Cannot remove disk',
                                        );
index fd072ba..f2266d4 100644 (file)
                                $active = $netinfo['active'];
                                $active_str = $lang->get( $active ? 'Yes' : 'No' );
 
+                               if ($dev == 'any interface')
+                                       $dev = $lang->get('net_forward_dev_any');
+
                                $actions = '';
                                if (!$active) {
                                        $actions .= '<a href="?page='.$page.'&amp;action=net-start&amp;net='.$name.'">'.$lang->get('net_start').'</a> | ';
index 4502c18..2b993f7 100644 (file)
                else
                        style = 'none';
 
-               for (i = 1; i < 10; i++) {
-                       name = 'setup_'+what+i;
-                       d = document.getElementById(name);
-                       if (d == null)
-                               break;
+               name = 'setup_'+what;
+               d = document.getElementById(name);
+               if (d != null)
                        d.style.display = style;
-               }
        }
 
        function net_ip_change(val) {
                if (val == 1) {
                        document.getElementById('net_ip_cidr').style.display = 'table-row';
-                       document.getElementById('net_ip_direct1').style.display = 'none';
-                       document.getElementById('net_ip_direct2').style.display = 'none';
+                       document.getElementById('net_ip_direct').style.display = 'none';
                } else {
                        document.getElementById('net_ip_cidr').style.display = 'none';
-                       document.getElementById('net_ip_direct1').style.display = 'table-row';
-                       document.getElementById('net_ip_direct2').style.display = 'table-row';
+                       document.getElementById('net_ip_direct').style.display = 'table-row';
+               }
+       }
+
+       function check_values() {
+               if (document.getElementById('net_name').value == '') {
+                       alert('Network name is not set!');
+                       return false;
+               }
+
+               cidr  = document.getElementById('net_cidr').value;
+               bIP   = (document.getElementById('net_ip').value != '');
+               bMask = (document.getElementById('net_mask').value != '');
+
+               sCidr = (document.getElementById('ipdef_val').value == 1);
+               if (sCidr) {
+                       if (cidr == '') {
+                               alert('CIDR definition missing');
+                               return false;
+                       }
+                       if (cidr.indexOf("/") == -1) {
+                               alert('Invalid CIDR definition');
+                               return false;
+                       }
+               }
+
+               if (!sCidr && !(bIP && bMask)) {
+                       if (!bIP)
+                               alert('No IP address defined!');
+                       if (!bMask)
+                               alert('No network mask defined!');
+                       return false;
                }
+
+
+               return true;
        }
 -->
 </script>
 
 <div class="section"><?= $lang->get('create-new-network') ?></div>
 
-<form method="POST">
+<form method="POST" onsubmit="return check_values()">
 
 <table id="form-table">
 <tr>
     <td align="right"><?= $lang->get('name') ?>: </td>
-    <td><input type="text" name="name" /></td>
+    <td><input type="text" name="name" id="net_name" /></td>
 </tr>
 
 <tr>
     <td align="right"><?= $lang->get('net_ip_range_def') ?>:</td>
     <td>
-      <select name="ip_range_cidr" onchange="net_ip_change(this.value)">
+      <select name="ip_range_cidr" onchange="net_ip_change(this.value)" id="ipdef_val">
        <option value="1"><?= $lang->get('net_ip_cidr') ?></option>
        <option value="0"><?= $lang->get('net_ip_direct') ?></option>
       </select>
 
 <tr id="net_ip_cidr">
     <td align="right"><?= $lang->get('net_ipdef_cidr') ?>:</td>
-    <td><input type="text" name="net_cidr" /></td>
+    <td><input type="text" name="net_cidr" id="net_cidr" /></td>
 </tr>
 
-<tr id="net_ip_direct1" style="display: none">
-    <td align="right"><?= $lang->get('net_ip') ?>:</td>
-    <td><input type="text" name="net_ip" /></td>
-</tr>
-
-<tr id="net_ip_direct2" style="display: none">
-    <td align="right"><?= $lang->get('net_mask') ?>:</td>
-    <td><input type="text" name="net_mask" /></td>
+<tr id="net_ip_direct" style="display: none">
+    <td>&nbsp;</td>
+    <td>
+    <table>
+       <tr>
+           <td align="right"><?= $lang->get('net_ip') ?>:</td>
+           <td><input type="text" name="net_ip" id="net_ip" /></td>
+       </tr>
+       <tr>
+           <td align="right"><?= $lang->get('net_mask') ?>:</td>
+           <td><input type="text" name="net_mask" id="net_mask" /></td>
+        </tr>
+    </table>
+    <td>
 </tr>
 
 <tr>
     </td>
 </tr>
 
-<tr id="setup_dhcp1" style="display: none">
-    <td align="right"><?= $lang->get('net_dhcp_start') ?>:</td>
+<tr id="setup_dhcp" style="display: none">
+    <td>&nbsp;</td>
     <td>
-      <input type="text" name="net_dhcp_start" />
-    </td>
-</tr>
-
-<tr id="setup_dhcp2" style="display: none">
-    <td align="right"><?= $lang->get('net_dhcp_end') ?>:</td>
-    <td>
-      <input type="text" name="net_dhcp_end" />
-    </td>
+       <table>
+       <tr>
+           <td align="right"><?= $lang->get('net_dhcp_start') ?>:</td>
+           <td>
+             <input type="text" name="net_dhcp_start" />
+           </td>
+       </tr>
+       <tr>
+           <td align="right"><?= $lang->get('net_dhcp_end') ?>:</td>
+           <td>
+             <input type="text" name="net_dhcp_end" />
+           </td>
+       </tr>
+       </table>
 </tr>
 
 <tr>
 <tr>
     <td align="right"><?= $lang->get('net_dev') ?>:</td>
     <td>
-      <input type="text" name="net_forward_dev" />
+      <input type="text" name="net_forward_dev" /> (<?= $lang->get('net_forward_dev_empty_msg') ?>)
     </td>
 </tr>
 
index 67a8b89..b4fb506 100644 (file)
                else
                        style = 'none';
 
-               for (i = 1; i < 10; i++) {
-                       name = 'setup_'+what+i;
-                       d = document.getElementById(name);
-                       if (d == null)
-                               break;
+               name = 'setup_'+what;
+               d = document.getElementById(name);
+               if (d != null)
                        d.style.display = style;
-               }
        }
 
        function vm_disk_change(val) {
                        document.getElementById('vm_disk_create').style.display = 'inline';
                }
        }
+
+       function generate_mac_addr() {
+               var xmlhttp;
+               if (window.XMLHttpRequest)
+                       xmlhttp = new XMLHttpRequest();
+               else
+                       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
+
+               xmlhttp.onreadystatechange=function() {
+                       if (xmlhttp.readyState==4 && xmlhttp.status==200) {
+                               document.getElementById('nic_mac_addr').value = xmlhttp.responseText;
+                       }
+               }
+
+               xmlhttp.open("GET", '<?= $_SERVER['REQUEST_URI'] ?>&get_mac=1',true);
+               xmlhttp.send();
+       }
 -->
 </script>
 
     </td>
 </tr>
 
-<tr id="setup_network1" style="display: none">
-    <td align="right"><?= $lang->get('vm_network_mac') ?>:</td>
-    <td><input type="text" name="nic_mac" value="<?= $lv->generate_random_mac_addr() ?>"/></td>
-</tr>
-
-<tr id="setup_network2" style="display: none">
-    <td align="right"><?= $lang->get('vm_network_type') ?>:</td>
+<tr id="setup_network" style="display: none">
+    <td>&nbsp;</td>
     <td>
-                      <select name="nic_type">';
+        <table>
+            <tr>
+                <td align="right"><?= $lang->get('vm_network_mac') ?>:</td>
+                <td>
+                       <input type="text" name="nic_mac" value="<?= $lv->generate_random_mac_addr() ?>" id="nic_mac_addr" />
+                       <input type="button" onclick="generate_mac_addr()" value="<?= $lang->get('network-generate-mac') ?>">
+               </td>
+            </tr>
+            <tr>
+                 <td align="right"><?= $lang->get('vm_network_type') ?>:</td>
+                 <td>
+                     <select name="nic_type">';
 
 <?php
        $models = $lv->get_nic_models();
         for ($i = 0; $i < sizeof($models); $i++)
                 echo '<option value="'.$models[$i].'">'.$models[$i].'</option>';
 ?>
-                      </select>
-    </td>
-</tr>
-
-<tr id="setup_network3" style="display: none">
-    <td align="right"><?= $lang->get('vm_network_net') ?>:</td>
-    <td>
-                      <select name="nic_net">';
+                     </select>
+                 </td>
+            </tr>
+            <tr>
+                 <td align="right"><?= $lang->get('vm_network_net') ?>:</td>
+                 <td>
+                     <select name="nic_net">';
 
 <?php
         $nets = $lv->get_networks();
         for ($i = 0; $i < sizeof($nets); $i++)
                 echo '<option value="'.$nets[$i].'">'.$nets[$i].'</option>';
 ?>
-                      </select>
+                     </select>
+                 </td>
+            </tr>
+        </table>
     </td>
 </tr>
 
     </td>
 </tr>
 
-<tr id="setup_disk1" style="display: none">
-        <td align="right"><?= $lang->get('new-vm-disk')?>: </td>
-        <td>
-               <select name="new_vm_disk" onchange="vm_disk_change(this.value)">
+<tr id="setup_disk" style="display: none">
+    <td>&nbsp;</td>
+    <td>
+        <table>
+            <tr>
+                <td align="right"><?= $lang->get('new-vm-disk')?>: </td>
+                <td>
+                   <select name="new_vm_disk" onchange="vm_disk_change(this.value)">
                        <option value="0"><?= $lang->get('new-vm-existing') ?></option>
                        <option value="1"><?= $lang->get('new-vm-create') ?></option>
-               </select>
-       </td>
-</tr>
-
-<tr id="setup_disk2" style="display: none">
-       <td align="right">
-               <span id="vm_disk_existing">
-               <?= $lang->get('vm_disk_image')?>:
-               </span>
-               <span id="vm_disk_create" style="display: none">
-               <?= $lang->get('vm-disk-size') ?> (MiB): 
-               </span>
-       </td>
-       <td><input type="text" name="img_data" /></td>
-</tr>
-
-<tr id="setup_disk3" style="display: none">
-       <td align="right"><?= $lang->get('vm_disk_location') ?>: </td>
-       <td>
-               <select name="disk_bus">
+                   </select>
+               </td>
+           </tr>
+            <tr>
+               <td align="right">
+                       <span id="vm_disk_existing">
+                       <?= $lang->get('vm_disk_image')?>:
+                       </span>
+                       <span id="vm_disk_create" style="display: none">
+                       <?= $lang->get('vm-disk-size') ?> (MiB): 
+                       </span>
+               </td>
+               <td><input type="text" name="img_data" /></td>
+           </tr>
+           <tr>
+               <td align="right"><?= $lang->get('vm_disk_location') ?>: </td>
+               <td>
+                   <select name="disk_bus">
                        <option value="ide">IDE Bus</option>
                        <option value="scsi">SCSI Bus</option>
-               </select>
-       </td>
-</tr>
-<tr id="setup_disk4" style="display: none">
-       <td align="right"><?= $lang->get('vm_disk_type') ?>: </td>
-       <td>
-               <select name="disk_driver">
+                   </select>
+               </td>
+           </tr>
+           <tr>
+               <td align="right"><?= $lang->get('vm_disk_type') ?>: </td>
+               <td>
+                   <select name="disk_driver">
                        <option value="raw">raw</option>
                        <option value="qcow">qcow</option>
                        <option value="qcow2">qcow2</option>
-               </select>
-       </td>
-</tr>
-<tr id="setup_disk5" style="display: none">
-       <td align="right"><?= $lang->get('vm_disk_dev') ?>: </td>
-       <td>hda</td>
+                   </select>
+               </td>
+           </tr>
+           <tr>
+               <td align="right"><?= $lang->get('vm_disk_dev') ?>: </td>
+               <td>hda</td>
+           </tr>
+       </table>
+    </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>
+       <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>