[lib] Library configuration transisition: Add config options to vsc_mgmt_init()
authorMaximilian Wilhelm <max@rfc2324.org>
Sat, 22 Aug 2009 19:26:12 +0000 (21:26 +0200)
committerMaximilian Wilhelm <max@rfc2324.org>
Sat, 22 Aug 2009 19:26:12 +0000 (21:26 +0200)
  This commit adds four configuration parameters for vsc_mgmt_init()
   * image_dir_path

     Path to the directory where the VM images are stored in the cluster.

   * image_suffix

     The suffix which should be appended the VM image file names.
     In general VM image files will be named like <VM UUID>.<image_suffix>

   * data_backend_config_file

     The optional path to a configuration file for the data backend in use.
     An empty string or a NULL pointer is OK if your data backend does not have
     a configuration.

   * network_backend_config_file

     The optional path to a configuration file for the network backend in use.
     The configuration file be read by the network backend in use and most
     probably by the common network backend which will be used by all backends
     in general.
     Therefore a configuraton file will be needed in most cases.

  refs #97

Signed-off-by: Maximilian Wilhelm <max@rfc2324.org>

13 files changed:
backend/data/data.h
backend/data/glibtree/data_glibtree.c
backend/network/common/common.c
backend/network/common/common.h
backend/network/dhcponly/network.c
backend/network/network.h
include/libvscmgmt/core.h
lib/core.c
lib/vm.c
python/module.c
tests/host_test.c
tests/manual/global_test.c
tests/manual/network_read_test.c

index 1c6dce1..b2f64d4 100644 (file)
@@ -31,7 +31,8 @@
 #include "../../lib/types.h"
 
 void
-_vsc_mgmt_data_init (struct VscError *error);
+_vsc_mgmt_data_init (struct VscError *error,
+                     const char *config_file_path);
 
 void
 _vsc_mgmt_data_cleanup (void);
index 351f7f0..04eacb6 100644 (file)
@@ -135,13 +135,19 @@ _vm_ip_lookup_tree_traverse_free (gpointer key, gpointer value, gpointer data)
 
 
 void
-_vsc_mgmt_data_init (struct VscError *error)
+_vsc_mgmt_data_init (struct VscError *error, const char *config_file_path)
 {
+       (void) config_file_path;
+
        VSC__ASSERT (error != NULL);
        VSC__ASSERT (! error->occured);
        VSC__ASSERT (! _data_initialized);
 
-       _data_initialized = 1;
+       if (_data_initialized) {
+               VSC__ERROR1 (error, VSC__ERROR_CODE__INVALID_CALL,
+                            "Already initialized");
+               return;
+       }
 
        _host_by_ip_tree = g_tree_new (_ip_compare);
        if (! _host_by_ip_tree) {
@@ -199,6 +205,8 @@ _vsc_mgmt_data_cleanup (void)
                g_tree_foreach (_host_by_ip_tree, _host_tree_traverse_free, NULL);
                g_tree_destroy (_host_by_ip_tree);
        }
+
+       _data_initialized = 0;
 }
 
 
index bf8ae2c..d740948 100644 (file)
@@ -250,8 +250,10 @@ unlock:
 
 
 void
-_vsc_mgmt_network_common_init (struct VscError *error)
+_vsc_mgmt_network_common_init (struct VscError *error, const char *config_file_path)
 {
+       (void) config_file_path;
+
        const char *mac_base_str = MAC_BASE;
        unsigned char octet = 0;
        char i = 1;
@@ -260,6 +262,12 @@ _vsc_mgmt_network_common_init (struct VscError *error)
        VSC__ASSERT (! error->occured);
        VSC__ASSERT (! _network_initialized);
 
+       if (_network_initialized == TRUE) {
+               VSC__ERROR1 (error, VSC__ERROR_CODE__INVALID_CALL,
+                            "Already initialized");
+               return;
+       }
+
        _mac_base = 0;
 
        /* Cut MAC_BASE into octets and compute numerical value */
index dd96a48..34acce1 100644 (file)
@@ -29,7 +29,7 @@
 #include "../../../lib/network.h"
 
 void
-_vsc_mgmt_network_common_init (struct VscError *error);
+_vsc_mgmt_network_common_init (struct VscError *error, const char *config_file_path);
 
 void
 _vsc_mgmt_network_common_cleanup (void);
index 90e2cd1..ab2266e 100644 (file)
@@ -246,18 +246,21 @@ _ip_index (const struct DhcpNetwork *network,
 
 
 void
-_vsc_mgmt_network_init (struct VscError *error)
+_vsc_mgmt_network_init (struct VscError *error, const char *config_file_path)
 {
+       (void) config_file_path;
+
        VSC__ASSERT (error != NULL);
        VSC__ASSERT (! error->occured);
        VSC__ASSERT (! _network_initialized);
 
        if (_network_initialized) {
-               VSC__ERROR0 (error, VSC__ERROR_CODE__INVALID_CALL);
+               VSC__ERROR1 (error, VSC__ERROR_CODE__INVALID_CALL,
+                            "Already initialized");
                return;
        }
 
-       _vsc_mgmt_network_common_init (error);
+       _vsc_mgmt_network_common_init (error, config_file_path);
        if (error->occured) {
                VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
                return;
index 010d262..445e8dd 100644 (file)
@@ -30,7 +30,7 @@
 #include "../../include/libvscmgmt/network.h"
 
 void
-_vsc_mgmt_network_init (struct VscError *error);
+_vsc_mgmt_network_init (struct VscError *error, const char *config_file_path);
 
 void
 _vsc_mgmt_network_cleanup (void);
index d02be82..25d05c4 100644 (file)
@@ -36,7 +36,10 @@ extern "C" {
 #endif
 
 void
-vsc_mgmt_init (struct VscError *error);
+vsc_mgmt_init (struct VscError *error,
+               const char *image_dir_path, const char *image_suffix,
+               const char *data_backend_config_file,
+               const char *network_backend_config_file);
 
 void
 vsc_mgmt_cleanup (void);
index a50b3ec..13aa4bf 100644 (file)
 /*!
  * @file
  */
+#include <errno.h>
+#include <string.h>
 
 #include <libvirt/libvirt.h>
 #include <libvirt/virterror.h>
 
 #include <libvscmisc/assert.h>
 #include <libvscmisc/error.h>
+#include <libvscmisc/memory.h>
 
 #include "../include/libvscmgmt/core.h"
 #include "../backend/data/data.h"
 static int _initialized = FALSE;
 static struct VscMgmtMutex *_mutex = NULL;
 
+/* Path information for vsc_mgmt_image_{deploy,retract} in vm.c */
+char *_image_basedir = NULL;
+int _image_basedir_length = 0;
+char *_image_file_suffix = NULL;
+int _image_file_suffix_length = 0;
+
+
 void
 _vsc_mgmt_lock (void)
 {
@@ -85,10 +95,19 @@ _vsc_mgmt_error_from_libvirt (struct VscError *error, const virConnectPtr connec
  * @note Function is not threadsafe.
  */
 void
-vsc_mgmt_init (struct VscError *error)
+vsc_mgmt_init (struct VscError *error,
+               const char *image_dir_path, const char *image_suffix,
+               const char *data_backend_config_file,
+              const char *network_backend_config_file)
 {
        VSC__ASSERT (error != NULL);
        VSC__ASSERT (! error->occured);
+       VSC__ASSERT (image_dir_path != NULL);
+       VSC__ASSERT (image_suffix != NULL);
+       /*
+        * Don't check if the backend config files are given as they might be
+        * optional for the used backend. Just pass them to the backend.
+        */
 
        if (_initialized) {
                VSC__ERROR1 (error, VSC__ERROR_CODE__INVALID_CALL,
@@ -96,26 +115,59 @@ vsc_mgmt_init (struct VscError *error)
                return;
        }
 
-       _mutex = _vsc_mgmt_mutex_new_recursive (error);
+       /* Check if one of the paths is NULL which would break strdup() */
+       if (! image_dir_path || ! image_suffix) {
+               VSC__ERROR1 (error, VSC__ERROR_CODE__INVALID_ARGUMENT,
+                            "image_dir_path or image_suffix is NULL");
+               return;
+       }
+
+       _image_basedir = strdup (image_dir_path);
+       if (! _image_basedir) {
+               VSC__ERROR2 (error, VSC__ERROR_CODE__ERRNO,
+                            "Could not copy image_dir_path string: %s",
+                            strerror (errno));
+               return;
+       }
 
+       _image_file_suffix = strdup (image_suffix);
+       if (! _image_file_suffix) {
+               VSC__ERROR2 (error, VSC__ERROR_CODE__ERRNO,
+                            "Could not copy image_suffix string: %s",
+                            strerror (errno));
+               vsc_free (&_image_basedir);
+               return;
+       }
+
+       _image_basedir_length = strlen (_image_basedir);
+       _image_file_suffix_length = strlen (_image_file_suffix);
+
+
+       /* Initialize mutex */
+       _mutex = _vsc_mgmt_mutex_new_recursive (error);
        if (error->occured) {
                VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
                return;
        }
 
-       _vsc_mgmt_network_init (error);
 
+       /* Initialize network backend */
+       _vsc_mgmt_network_init (error,network_backend_config_file);
        if (error->occured) {
                VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
                return;
        }
 
-       _vsc_mgmt_data_init (error);
+
+       /* Initialize data backend */
+       _vsc_mgmt_data_init (error, data_backend_config_file);
        if (error->occured) {
                VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
                return;
        }
 
+
+       /* Initialize libVirt */
        if (virInitialize() < 0) {
                VSC__ERROR1 (error, VSC__ERROR_CODE__LIBVIRT_ERROR,
                             "Could not initialize libvirt");
index cca4a9e..7081dbc 100644 (file)
--- a/lib/vm.c
+++ b/lib/vm.c
 #define BUFFER_SIZE 4096
 
 
-static const char *image_basedir = "/srv/vsc/images";
-static unsigned int image_basedir_length = 0;
+/* Import global config options from core.c */
+extern char *_image_basedir;
+extern int _image_basedir_length;
 
-static const char *image_file_suffix = "image";
-static unsigned int image_file_suffix_length;
+extern char *_image_file_suffix;
+extern int _image_file_suffix_length;
 
 /*******************************************************************************
  *                              internal functions                             *
@@ -352,22 +353,16 @@ _vm_copy_image (struct VscError *error, const char *image_filename,
                return;
        }
 
-       /* Compute the string lengths of the static strings */
-       if (! image_basedir_length) {
-               image_basedir_length = strlen (image_basedir);
-               image_file_suffix_length = strlen (image_file_suffix);
-       }
-
        /* Allocate memory for the VM image dir and file */
-       dest_filename = vsc_alloc (error, image_basedir_length + 1 + /* <basedir>/ */
+       dest_filename = vsc_alloc (error, _image_basedir_length + 1 + /* <basedir>/ */
                                   VSC_MGMT__UUID__STRING_SIZE + 1 + /* <uuid>/ */
-                                  VSC_MGMT__UUID__STRING_SIZE + 1 + image_file_suffix_length + 1); /* <uuid>.<suffix>\0 */
+                                  VSC_MGMT__UUID__STRING_SIZE + 1 + _image_file_suffix_length + 1); /* <uuid>.<suffix>\0 */
        if (error->occured) {
                VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
                goto cleanup;
        }
 
-       dest_file_dirname = vsc_alloc (error, image_basedir_length + 1 + /* <basedir>/ */
+       dest_file_dirname = vsc_alloc (error, _image_basedir_length + 1 + /* <basedir>/ */
                                       VSC_MGMT__UUID__STRING_SIZE + 1); /* <uuid>\0 */
        if (error->occured) {
                VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
@@ -379,7 +374,7 @@ _vm_copy_image (struct VscError *error, const char *image_filename,
         * At first only print the path to the VM image directory so we can
         * create it.
         */
-       sprintf (dest_file_dirname, "%s/%s", image_basedir, vm->info.uuid_string);
+       sprintf (dest_file_dirname, "%s/%s", _image_basedir, vm->info.uuid_string);
 
        if (mkdir (dest_file_dirname, S_IRUSR|S_IWUSR|S_IXUSR | S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH) != 0) {
                VSC__ERROR2 (error, VSC__ERROR_CODE__ERRNO,
@@ -390,8 +385,8 @@ _vm_copy_image (struct VscError *error, const char *image_filename,
        cleanup_flag = 1;
 
        /* Concatenate all elements of the whole path */
-       sprintf (dest_filename, "%s/%s/%s.%s", image_basedir, vm->info.uuid_string,
-                vm->info.uuid_string, image_file_suffix);
+       sprintf (dest_filename, "%s/%s/%s.%s", _image_basedir, vm->info.uuid_string,
+                vm->info.uuid_string, _image_file_suffix);
        cleanup_flag = 2;
 
 
@@ -532,10 +527,10 @@ vsc_mgmt_vm_deploy (struct VscError *error, const char *image_filename,
         * Check if there is enough space left on the FS to store the images
         * for num_hosts * imagesize
         */
-       if (statvfs (image_basedir, &image_fs_stat) != 0) {
+       if (statvfs (_image_basedir, &image_fs_stat) != 0) {
                VSC__ERROR2 (error, VSC__ERROR_CODE__ERRNO,
                             "Failed to get information about image dir filesystem '%s': %s",
-                            image_basedir, strerror (errno));
+                            _image_basedir, strerror (errno));
                return NULL;
        }
 
@@ -575,7 +570,7 @@ vsc_mgmt_vm_deploy (struct VscError *error, const char *image_filename,
                VSC__ERROR2 (error, VSC__ERROR_CODE__INVALID_ARGUMENT,
                             "Not enought space on image filesystem '%s' "
                             "to save %u copies of image '%s'",
-                            image_basedir, num_hosts, image_filename);
+                            _image_basedir, num_hosts, image_filename);
                return NULL;
        }
 
index e82149e..d58cb79 100644 (file)
@@ -240,6 +240,54 @@ failure:
 
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+ * Core: init
+ */
+
+PyDoc_STRVAR (_init__doc__,
+"init(image_dir_path, image_suffix, data_backend_config_file, network_backend_config_file)\n\n"
+"Initialize the library");
+
+static PyObject *
+_init (PyObject *self VSC__ATTR__UNUSED, PyObject *args)
+{
+       struct VscError error;
+       PyObject *result = Py_None;
+       const char *image_dir_path;
+       const char *image_suffix;
+       const char *data_backend_config_file;
+       const char *network_backend_config_file;
+
+       if (! PyArg_ParseTuple (args, "ssss:init", &image_dir_path,
+                               &image_suffix, &data_backend_config_file,
+                               &network_backend_config_file)) {
+               return NULL;
+       }
+
+       vsc_error_init (&error);
+
+       vsc_mgmt_init (&error, image_dir_path, image_suffix,
+                      data_backend_config_file,
+                      network_backend_config_file);
+
+       if (error.occured) {
+               _format_error (&error);
+               goto failure;
+       }
+
+cleanup:
+       vsc_error_cleanup (&error);
+
+       return result;
+
+failure:
+       result = NULL;
+
+       goto cleanup;
+}
+
+
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  * Core: backup
  */
 
@@ -1970,6 +2018,7 @@ failure:
 
 static PyMethodDef _methods[] = {
        /* Core */
+       _METHOD_DEFINITION (init),
        _METHOD_DEFINITION (backup),
        _METHOD_DEFINITION (recover),
        _METHOD_DEFINITION (get_version),
@@ -2020,7 +2069,6 @@ PyMODINIT_FUNC
 initlibvscmgmtmodule (void)
 {
        PyObject *module;
-       struct VscError error;
        static int initialized = FALSE;
 
        if (initialized) {
@@ -2040,21 +2088,5 @@ initlibvscmgmtmodule (void)
        Py_INCREF (_error_type);
        PyModule_AddObject (module, "Error", _error_type);
 
-       /* Initialize libvscmgmt */
-       vsc_error_init (&error);
-
-       vsc_mgmt_init (&error);
-
-       if (error.occured) {
-               goto failure;
-       }
-
        initialized = TRUE;
-
-       return;
-
-failure:
-       _format_error (&error);
-
-       vsc_error_cleanup (&error);
 }
index a404a6d..306900b 100644 (file)
@@ -33,7 +33,7 @@ setup (void)
 
        vsc_error_init (&error);
 
-       vsc_mgmt_init (&error);
+       vsc_mgmt_init (&error, "/srv/vsc/images", "image", NULL, NULL);
 
        if (error.occured) {
                vsc_error_fprint (&error, stderr);
index 8ee4c90..b480999 100644 (file)
@@ -59,7 +59,7 @@ main (int argc, char **argv)
 
         vsc_error_init (&error);
 
-       vsc_mgmt_init (&error);
+       vsc_mgmt_init (&error, "/srv/vsc/images", "image", NULL, NULL);
        if (error.occured) {
                vsc_error_fprint (&error, stderr);
                return 1;
index 3a671aa..3fbb4c6 100644 (file)
@@ -46,7 +46,7 @@ main (int argc, char **argv)
 
         vsc_error_init (&error);
 
-       vsc_mgmt_init (&error);
+       vsc_mgmt_init (&error, "/srv/vsc/images", "image", NULL, NULL);
        if (error.occured) {
                vsc_error_fprint (&error, stderr);
                return 1;