[lib] Use VscBuffer in _gen_vm_xml_string().
authorMatthias Bolte <matthias.bolte@googlemail.com>
Sat, 20 Jun 2009 17:56:52 +0000 (19:56 +0200)
committerMatthias Bolte <matthias.bolte@googlemail.com>
Sat, 20 Jun 2009 17:56:52 +0000 (19:56 +0200)
  refs #76

Signed-off-by: Matthias Bolte <matthias.bolte@googlemail.com>

lib/vm.c
tests/bindings_test.py

index 507a33f..c6d53bf 100644 (file)
--- a/lib/vm.c
+++ b/lib/vm.c
@@ -34,6 +34,7 @@
 #include <uuid/uuid.h>
 
 #include <libvscmisc/assert.h>
+#include <libvscmisc/buffer.h>
 #include <libvscmisc/error.h>
 #include <libvscmisc/memory.h>
 
@@ -201,79 +202,93 @@ free:
 
 
 static char *
-_gen_vm_xml_string (struct VscError *error,
-                    const struct VscMgmtVm *vm,
-                   enum VscMgmtHostType host_type)
+_gen_vm_xml_string (struct VscError *error, const struct VscMgmtVm *vm,
+                    enum VscMgmtHostType host_type)
 {
-       char *xml_config = NULL;
+       struct VscBuffer buffer;
 
        VSC__ASSERT (error != NULL);
        VSC__ASSERT (! error->occured);
        VSC__ASSERT (vm != NULL);
 
-       xml_config = vsc_alloc (error, 4096);
-       if (error->occured) {
-               VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
-               return NULL;
-       }
-
+       vsc_buffer_init (&buffer);
 
        if (host_type == VSC_MGMT__HOST_TYPE__VIRTUAL) {
-               snprintf (xml_config, 4090,
-                         "<domain type='%s'>\n"
-                         "  <name>%s</name>\n"
-                         "  <uuid>%s</uuid>\n"
-                         "  <memory>%lu</memory>\n"
-                         "  <vcpu>%u</vcpu>\n"
-                         "  <os>\n"
-                         "    <type arch='i686'>hvm</type>\n"
-                         "    <boot dev='hd'/>\n"
-                         "  </os>\n"
-                         "  <clock offset='utc'/>\n"
-                         "  <on_poweroff>destroy</on_poweroff>\n"
-                         "  <on_reboot>restart</on_reboot>\n"
-                         "  <on_crash>destroy</on_crash>\n"
-                         "</domain>\n",
-
-                         "test",
-                         vm->info.uuid_string,
-                         vm->info.uuid_string,
-                         vm->info.resource_config.memory_size,
-                         vm->info.resource_config.num_cores
-               );
-
+               vsc_buffer_append (&buffer,
+                                  "<domain type='test'>\n"
+                                  "  <os>\n"
+                                  "    <type arch='i686'>hvm</type>\n"
+                                  "    <boot dev='hd' />\n"
+                                  "  </os>\n");
        }
 
        else if (host_type == VSC_MGMT__HOST_TYPE__XEN) {
-               snprintf (xml_config, 4090,
-                         "<domain type='%s'>\n"
-                         "  <name>%s</name>\n"
-                         "  <uuid>%s</uuid>\n"
-                         "  <memory>%lu</memory>\n"
-                         "  <vcpu>%u</vcpu>\n"
-                         "  <os>\n"
-                         "    <type arch='i686'>xen</type>\n"
-                         "    <boot dev='hd'/>\n"
-                         "  </os>\n"
-                         "  <clock offset='utc'/>\n"
-                         "  <on_poweroff>destroy</on_poweroff>\n"
-                         "  <on_reboot>restart</on_reboot>\n"
-                         "  <on_crash>destroy</on_crash>\n"
-                         "</domain>\n",
-
-                         "xen",
-                         vm->info.uuid_string,
-                         vm->info.uuid_string,
-                         vm->info.resource_config.memory_size,
-                         vm->info.resource_config.num_cores
-               );
-
+               vsc_buffer_append (&buffer,
+                                  "<domain type='xen'>\n"
+                                  "  <os>\n"
+                                  "    <type arch='i686'>xen</type>\n"
+                                  "    <boot dev='hd' />\n"
+                                  "  </os>\n");
+
+               vsc_buffer_sprintf (&buffer,
+                                   "<disk type='file'>"
+                                   "  <source file='%s'/>"
+                                   "  <target dev='sda' bus='xen'/>"
+                                   "</disk>",
+                                   vm->info.image_filename);
+
+               vsc_buffer_sprintf (&buffer,
+                                   "<interface type='bridge'>"
+                                   "  <source bridge='br0'/>"
+                                   "  <mac address='%02x:%02x:%02x:%02x:%02x:%02x'/>"
+                                   "</interface>",
+                                   vm->info.mac.bytes[0], vm->info.mac.bytes[1],
+                                   vm->info.mac.bytes[2], vm->info.mac.bytes[3],
+                                   vm->info.mac.bytes[4], vm->info.mac.bytes[5]);
        }
 
        else if (host_type == VSC_MGMT__HOST_TYPE__ESX) {
+               vsc_buffer_append (&buffer,
+                                  "<domain type='vmware'>\n"
+                                  "  <os>\n"
+                                  "    <type arch='i686'>hvm</type>\n"
+                                  "    <boot dev='hd' />\n"
+                                  "  </os>\n");
+
+               vsc_buffer_sprintf (&buffer,
+                                   "<disk type='file'>"
+                                   "  <source file='%s'/>"
+                                   "  <target dev='sda' bus='scsi'/>"
+                                   "</disk>",
+                                   vm->info.image_filename);
+
+               // FIXME: network interface config
+       }
+
+
+       vsc_buffer_sprintf (&buffer,
+                           "  <name>%s</name>\n"
+                           "  <uuid>%s</uuid>\n"
+                           "  <memory>%lu</memory>\n"
+                           "  <vcpu>%u</vcpu>\n",
+                           vm->info.uuid_string,
+                           vm->info.uuid_string,
+                           vm->info.resource_config.memory_size,
+                           vm->info.resource_config.num_cores);
+
+       vsc_buffer_append (&buffer,
+                          "  <clock offset='utc' />\n"
+                          "  <on_poweroff>destroy</on_poweroff>\n"
+                          "  <on_reboot>restart</on_reboot>\n"
+                          "  <on_crash>destroy</on_crash>\n"
+                          "</domain>\n");
+
+       if (buffer.error) {
+               VSC__ERROR0 (error, VSC__ERROR_CODE__OUT_OF_MEMORY);
+               return NULL;
        }
 
-       return xml_config;
+       return vsc_buffer_take (&buffer);
 }
 
 
index 2990df0..6c378b6 100755 (executable)
@@ -100,9 +100,17 @@ if len(host_ipv4_list) > 0:
         print "\n\n>>> vm_get_info(%s)" % vm_uuid_list[0]
         print libvscmgmt.vm_get_info(vm_uuid_list[0])
 
+    print "\n\n>>> vm_deploy(\"/dev/null\", %s, %s, %s)" % \
+          (host_ipv4_list, vm_resource_config, network_uuid)
+    print libvscmgmt.vm_deploy("/dev/null", host_ipv4_list, vm_resource_config,
+                               network_uuid)
 
-# FIXME: can't do this due to an list handling problem in vm_deploy
-#    print "\n\n>>> vm_deploy(\"/dev/null\", %s, %s, %s)" % \
-#          (host_ipv4_list, vm_resource_config, network_uuid)
-#    print libvscmgmt.vm_deploy("/dev/null", host_ipv4_list, vm_resource_config,
-#                               network_uuid)
+    # print vm uuid list for first host ipv4 in list
+    print "\n\n>>> host_get_vm_uuid_list(%s)" % host_ipv4_list[0]
+    vm_uuid_list = libvscmgmt.host_get_vm_uuid_list(host_ipv4_list[0])
+    print vm_uuid_list
+
+    if len(vm_uuid_list) > 0:
+        # print vm info for first vm uuid in list
+        print "\n\n>>> vm_get_info(%s)" % vm_uuid_list[-1]
+        print libvscmgmt.vm_get_info(vm_uuid_list[-1])