Skip to content

Commit

Permalink
Update the example scripts
Browse files Browse the repository at this point in the history
Thanks to Yukihiro Kawada <warp.kawada@gmail.com> I noticed that the
new example scripts are not being updated to the repo so pushing now.

Signed-off-by: Michal Novotny <minovotn@redhat.com>
  • Loading branch information
MigNov committed Mar 31, 2011
1 parent dcd2378 commit 6f55650
Show file tree
Hide file tree
Showing 3 changed files with 476 additions and 71 deletions.
1 change: 1 addition & 0 deletions examples/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
| <a href="?action=host-information">Host information</a>
| <a href="?action=virtual-networks">Virtual networks</a>
| <a href="?action=node-devices">Node devices</a>
| <a href="?action=storage-pools">Storage pools</a>
<br /><br />
214 changes: 200 additions & 14 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

$lv = new Libvirt('qemu:///system');
$action = array_key_exists('action', $_GET) ? $_GET['action'] : false;
$subaction = array_key_exists('subaction', $_GET) ? $_GET['subaction'] : false;

$hn = $lv->get_hostname();
if ($hn == false)
Expand All @@ -17,6 +18,126 @@
$uri = $lv->get_uri();
$tmp = $lv->get_domain_count();

if ($action == 'storage-pools') {
$msg = '';
if ($subaction == 'volume-delete') {
if ((array_key_exists('confirm', $_GET)) && ($_GET['confirm'] == 'yes'))
$msg = $lv->storagevolume_delete( base64_decode($_GET['path']) ) ? 'Volume has been deleted successfully' : 'Cannot delete volume';
else
$msg = '<table>
<tr>
<td colspan="2">
<b>Do you really want to delete volume <i>'.base64_decode($_GET['path']).'</i> ?</b><br />
</td>
</tr>
<tr align="center">
<td>
<a href="'.$_SERVER['REQUEST_URI'].'&amp;confirm=yes">Yes, delete it</a>
</td>
<td>
<a href="?action='.$action.'">No, go back</a>
</td>
</tr>';
}
else if ($subaction == 'volume-create') {
if (array_key_exists('sent', $_POST)) {
$msg = $lv->storagevolume_create($_GET['pool'], $_POST['name'], $_POST['capacity'], $_POST['allocation']) ?
'Volume has been created successfully' : 'Cannot create volume';
}
else
$msg = '<h3>Create a new volume</h3><form method="POST">
<table>
<tr>
<td>Volume name: </td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Capacity (e.g. 10M or 1G): </td>
<td><input type="text" name="capacity"></td>
</tr>
<tr>
<td>Allocation (e.g. 10M or 1G): </td>
<td><input type="text" name="allocation"></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value=" Add storage volume "></td>
</tr>
<input type="hidden" name="sent" value="1" />
</table>
</form>';
}

echo "<h2>Storage pools</h2>";
echo "Here's the list of the storage pools available on the connection.<br /><br />";

echo "<table>";
echo "<tr>
<th>Name<th>
<th>Activity</th>
<th>Volume count</th>
<th>State</th>
<th>Capacity</th>
<th>Allocation</th>
<th>Available</th>
<th>Path</th>
<th>Permissions</th>
<th>Ownership</th>
<th>Actions</th>
</tr>";

$pools = $lv->get_storagepools();
for ($i = 0; $i < sizeof($pools); $i++) {
$info = $lv->get_storagepool_info($pools[$i]);
$act = $info['active'] ? 'Active' : 'Inactive';

echo "<tr align=\"center\">
<td>$spaces{$pools[$i]}$spaces<td>
<td>$spaces$act$spaces</td>
<td>$spaces{$info['volume_count']}$spaces</td>
<td>$spaces{$lv->translate_storagepool_state($info['state'])}$spaces</td>
<td>$spaces{$lv->format_size($info['capacity'], 2)}$spaces</td>
<td>$spaces{$lv->format_size($info['allocation'], 2)}$spaces</td>
<td>$spaces{$lv->format_size($info['available'], 2)}$spaces</td>
<td>$spaces{$info['path']}$spaces</td>
<td>$spaces{$lv->translate_perms($info['permissions'])}$spaces</td>
<td>$spaces{$info['id_user']} / {$info['id_group']}$spaces</td>
<td>$spaces<a href=\"?action=storage-pools&amp;pool={$pools[$i]}&amp;subaction=volume-create\">Create volume</a>$spaces</td>
</tr>";

if ($info['volume_count'] > 0) {
echo "<tr>
<td colspan=\"10\" style='padding-left: 40px'><table>
<tr>
<th>Name</th>
<th>Type</th>
<th>Capacity</th>
<th>Allocation</th>
<th>Path</th>
<th>Actions</th>
</tr>";
$tmp = $lv->storagepool_get_volume_information($pools[$i]);
$tmp_keys = array_keys($tmp);
for ($ii = 0; $ii < sizeof($tmp); $ii++) {
$path = base64_encode($tmp[$tmp_keys[$ii]]['path']);
echo "<tr>
<td>$spaces{$tmp_keys[$ii]}$spaces</td>
<td>$spaces{$lv->translate_volume_type($tmp[$tmp_keys[$ii]]['type'])}$spaces</td>
<td>$spaces{$lv->format_size($tmp[$tmp_keys[$ii]]['capacity'], 2)}$spaces</td>
<td>$spaces{$lv->format_size($tmp[$tmp_keys[$ii]]['allocation'], 2)}$spaces</td>
<td>$spaces{$tmp[$tmp_keys[$ii]]['path']}$spaces</td>
<td>$spaces<a href=\"?action=storage-pools&amp;path=$path&amp;subaction=volume-delete\">Delete volume</a>$spaces</td>
</tr>";
}

echo "</table></td>
</tr>";
}
}
echo "</table>";

echo (strpos($msg, '<form')) ? $msg : '<pre>'.$msg.'</pre>';
}
else
if ($action == 'node-devices') {
echo "<h2>Node devices</h2>";
echo "Here's the list of node devices available on the host machine. You can dump the information about the node devices there so you have simple way ".
Expand Down Expand Up @@ -99,16 +220,30 @@
echo "This is the administration of virtual networks. You can see all the virtual network being available with their settings. Please make sure you're using the right network for the purpose you need to since using the isolated network between two or multiple guests is providing the sharing option but internet connectivity will be disabled. Please enable internet services only on the guests that are really requiring internet access for operation like e.g. HTTP server or FTP server but you don't need to put the internet access to the guest with e.g. MySQL instance or anything that might be managed from the web-site. For the scenario described you could setup 2 network, internet and isolated, where isolated network should be setup on both machine with Apache and MySQL but internet access should be set up just on the machine with Apache webserver with scripts to remotely connect to MySQL instance and manage it (using e.g. phpMyAdmin). Isolated network is the one that's having forwarding column set to None.";

$ret = false;
if (array_key_exists('subaction', $_GET)) {
if ($subaction) {
$name = $_GET['name'];
if ($_GET['subaction'] == 'start')
if ($subaction == 'start')
$ret = $lv->set_network_active($name, true) ? "Network has been started successfully" : 'Error while starting network: '.$lv->get_last_error();
else
if ($_GET['subaction'] == 'stop')
if ($subaction == 'stop')
$ret = $lv->set_network_active($name, false) ? "Network has been stopped successfully" : 'Error while stopping network: '.$lv->get_last_error();
else
if ($_GET['subaction'] == 'dumpxml')
$ret = 'XML dump of network <i>'.$name.'</i>:<br /><br />'.htmlentities($lv->get_network_xml($name, false));
if (($subaction == 'dumpxml') || ($subaction == 'edit')) {
$xml = $lv->network_get_xml($name, false);

if ($subaction == 'edit') {
if (@$_POST['xmldesc']) {
$ret = $lv->network_change_xml($name, $_POST['xmldesc']) ? "Network definition has been changed" :
'Error changing network definition: '.$lv->get_last_error();
}
else
$ret = 'Editing network XML description: <br /><br /><form method="POST"><table><tr><td>Network XML description: </td>'.
'<td><textarea name="xmldesc" rows="25" cols="90%">'.$xml.'</textarea></td></tr><tr align="center"><td colspan="2">'.
'<input type="submit" value=" Edit domain XML description "></tr></form>';
}
else
$ret = 'XML dump of network <i>'.$name.'</i>:<br /><br />'.htmlentities($lv->get_network_xml($name, false));
}
}

echo "<h3>List of networks</h3>";
Expand Down Expand Up @@ -139,7 +274,11 @@

$act = !$tmp2['active'] ? "<a href=\"?action={$_GET['action']}&amp;subaction=start&amp;name={$tmp2['name']}\">Start network</a>" :
"<a href=\"?action={$_GET['action']}&amp;subaction=stop&amp;name={$tmp2['name']}\">Stop network</a>";
$act .= " | <a href=\"?action={$_GET['action']}&amp;subaction=dumpxml&amp;name={$tmp2['name']}\">Dump network</a>";
$act .= " | <a href=\"?action={$_GET['action']}&amp;subaction=dumpxml&amp;name={$tmp2['name']}\">Dump network XML</a>";
if (!$tmp2['active']) {
$act .= ' | <a href="?action='.$_GET['action'].'&amp;subaction=edit&amp;name='.$tmp2['name'].'">Edit network</a>';
}

echo "<tr>
<td>$spaces{$tmp2['name']}$spaces</td>
<td align=\"center\">$spaces$activity$spaces</td>
Expand All @@ -158,9 +297,31 @@
else
if ($action == 'host-information') {
$tmp = $lv->host_get_node_info();
$ci = $lv->get_connect_information();

$info = '';
if ($ci['uri'])
$info .= 'connected to <i>'.$ci['uri'].'</i> on <i>'.$ci['hostname'].'</i>, ';
if ($ci['encrypted'] == 'Yes')
$info .= 'encrypted, ';
if ($ci['secure'] == 'Yes')
$info .= 'secure, ';
if ($ci['hypervisor_maxvcpus'])
$info .= 'maximum '.$ci['hypervisor_maxvcpus'].' vcpus per guest, ';

if (strlen($info) > 2)
$info[ strlen($info) - 2 ] = ' ';

echo "<h2>Host information</h2>
<table>
<tr>
<td>Hypervisor: </td>
<td>{$ci['hypervisor_string']}</td>
</tr>
<tr>
<td>Connection information: </td>
<td>$info</td>
</tr>
<tr>
<td>Architecture: </td>
<td>{$tmp['model']}</td>
Expand Down Expand Up @@ -242,12 +403,13 @@
else
echo "Domain doesn't have any disk devices";

/* Network interface information */
/* Network interface information */
echo "<h3>Network devices</h3>";
$tmp = $lv->get_nic_info($domName);
if (!empty($tmp)) {
$anets = $lv->get_networks(VIR_NETWORKS_ACTIVE);
echo "<table>

echo "<table>
<tr>
<th>MAC Address</th>
<th>$spaces NIC Type$spaces</th>
Expand All @@ -256,7 +418,11 @@
</tr>";

for ($i = 0; $i < sizeof($tmp); $i++) {
$netUp = (in_array($tmp[$i]['network'], $anets)) ? 'Yes' : 'No';
if (in_array($tmp[$i]['network'], $anets))
$netUp = 'Yes';
else
$netUp = 'No <a href="?action=virtual-networks&amp;subaction=start&amp;name='.$tmp[$i]['network'].'">[Start]</a>';

echo "<tr>
<td>$spaces{$tmp[$i]['mac']}$spaces</td>
<td align=\"center\">$spaces{$tmp[$i]['nic_type']}$spaces</td>
Expand Down Expand Up @@ -286,9 +452,22 @@
else if ($action == 'domain-destroy') {
$ret = $lv->domain_destroy($domName) ? "Domain has been destroyed successfully" : 'Error while destroying domain: '.$lv->get_last_error();
}
else if ($action == 'domain-get-xml') {
else if (($action == 'domain-get-xml') || ($action == 'domain-edit')) {
$inactive = (!$lv->domain_is_running($domName)) ? true : false;
$ret = "Domain XML for domain <i>$domName</i>:<br /><br />".htmlentities($lv->domain_get_xml($domName, $inactive));
$xml = $lv->domain_get_xml($domName, $inactive);

if ($action == 'domain-edit') {
if (@$_POST['xmldesc']) {
$ret = $lv->domain_change_xml($domName, $_POST['xmldesc']) ? "Domain definition has been changed" :
'Error changing domain definition: '.$lv->get_last_error();
}
else
$ret = 'Editing domain XML description: <br /><br /><form method="POST"><table><tr><td>Domain XML description: </td>'.
'<td><textarea name="xmldesc" rows="25" cols="90%">'.$xml.'</textarea></td></tr><tr align="center"><td colspan="2">'.
'<input type="submit" value=" Edit domain XML description "></tr></form>';
}
else
$ret = "Domain XML for domain <i>$domName</i>:<br /><br />".htmlentities($xml);
}
}

Expand All @@ -307,11 +486,12 @@
<th>Action</th>
</tr>";
for ($i = 0; $i < sizeof($doms); $i++) {
$uuid = $domkeys[$i];
$name = $doms[$uuid];
$res = $lv->get_domain_by_name($doms[$i]);
$uuid = libvirt_domain_get_uuid_string($res);
$name = $doms[$i];
$domr= $lv->get_domain_object($name);
$dom = $lv->get_domain_info($name);
$mem = ($dom[$name]['memory'] / 1024).' MB';
$mem = number_format($dom[$name]['memory'] / 1024, 2, '.', ' ').' MB';
$cpu = $dom[$name]['nrVirtCpu'];
$state = $lv->domain_state_translate($dom[$name]['state']);
$id = $lv->domain_get_id($domr);
Expand Down Expand Up @@ -358,6 +538,12 @@

echo "
<a href=\"?action=domain-get-xml&amp;uuid=$uuid\">Dump domain</a>
";

if (!$lv->domain_is_running($name))
echo "| <a href=\"?action=domain-edit&amp;uuid=$uuid\">Edit domain XML</a>";

echo "
$spaces
</td>
</tr>";
Expand Down
Loading

0 comments on commit 6f55650

Please sign in to comment.