def mount(self): """ Mount a GlusterFS volume for use. """ if self.fs and self._mounted: # Already mounted return self.fs = api.glfs_new(self.volname) if not self.fs: err = ctypes.get_errno() raise LibgfapiException("glfs_new(%s) failed: %s" % (self.volname, os.strerror(err))) ret = api.glfs_set_volfile_server(self.fs, self.protocol, self.host, self.port) if ret < 0: err = ctypes.get_errno() raise LibgfapiException("glfs_set_volfile_server(%s, %s, %s, " "%s) failed: %s" % (self.fs, self.protocol, self.host, self.port, os.strerror(err))) self.set_logging(self.log_file, self.log_level) if self.fs and not self._mounted: ret = api.glfs_init(self.fs) if ret < 0: err = ctypes.get_errno() raise LibgfapiException("glfs_init(%s) failed: %s" % (self.fs, os.strerror(err))) else: self._mounted = True
def mount(self): """ Mount a GlusterFS volume for use. """ if self.fs and self._mounted: # Already mounted return self.fs = api.glfs_new(self.volname) if not self.fs: raise LibgfapiException("glfs_new(%s) failed." % (self.volname)) ret = api.glfs_set_volfile_server(self.fs, self.protocol, self.host, self.port) if ret < 0: # FIXME: For some reason, get_errno() is not able to capture # proper errno. Until then.. # https://bugzilla.redhat.com/show_bug.cgi?id=1196161 raise LibgfapiException("glfs_set_volfile_server(%s, %s, %s, " "%s) failed." % (self.fs, self.protocol, self.host, self.port)) self.set_logging(self.log_file, self.log_level) if self.fs and not self._mounted: ret = api.glfs_init(self.fs) if ret < 0: raise LibgfapiException("glfs_init(%s) failed." % (self.fs)) else: self._mounted = True
def __init__(self, host, volname, proto="tcp", port=24007, log_file=None, log_level=7): """ Create a Volume object instance. :param host: Host with glusterd management daemon running. :param volname: Name of GlusterFS volume to be mounted and used. :param proto: Transport protocol to be used to connect to management daemon. Permitted values are "tcp" and "rdma". :param port: Port number where gluster management daemon is listening. :param log_file: Path to log file. When this is set to None, a new logfile will be created in default log directory i.e /var/log/glusterfs :param log_level: Integer specifying the degree of verbosity. Higher the value, more verbose the logging. """ # TODO: Provide an interface where user can specify volfile directly # instead of providing host and other details. This is helpful in cases # where user wants to load some non default xlator on client side. For # example, aux-gfid-mount or mount volume as read-only. # Add a reference so the module-level variable "api" doesn't # get yanked out from under us (see comment above File def'n). self._api = api self._mounted = False self.fs = None self.log_file = log_file self.log_level = log_level if None in (volname, host): # TODO: Validate host based on regex for IP/FQDN. raise LibgfapiException("Host and Volume name should not be None.") if proto not in ('tcp', 'rdma'): raise LibgfapiException("Invalid protocol specified.") if not isinstance(port, (int, long)): raise LibgfapiException("Invalid port specified.") self.host = host self.volname = volname self.protocol = proto self.port = port
def set_logging(self, log_file, log_level): """ Set logging parameters. Can be invoked either before or after invoking mount(). When invoked before mount(), the preferred log file and log level choices are recorded and then later enforced internally as part of mount() When invoked at any point after mount(), the change in log file and log level is instantaneous. :param log_file: Path of log file. If set to "/dev/null", nothing will be logged. If set to None, a new logfile will be created in default log directory (/var/log/glusterfs) :param log_level: Integer specifying the degree of verbosity. Higher the value, more verbose the logging. """ if self.fs: ret = api.glfs_set_logging(self.fs, self.log_file, self.log_level) if ret < 0: err = ctypes.get_errno() raise LibgfapiException( "glfs_set_logging(%s, %s) failed: %s" % (self.log_file, self.log_level, os.strerror(err))) self.log_file = log_file self.log_level = log_level
def dup(self): raise LibgfapiException("glfs_dup is currently broken (BZ 1311146)") dupfd = api.glfs_dup(self.fd) if not dupfd: err = ctypes.get_errno() raise OSError(err, os.strerror(err)) return File(dupfd, self.originalpath)
def umount(self): """ Unmount a mounted GlusterFS volume. Provides users a way to free resources instead of just waiting for python garbage collector to call __del__() at some point later. """ if self.fs: ret = self._api.glfs_fini(self.fs) if ret < 0: raise LibgfapiException("glfs_fini(%s) failed." % (self.fs)) else: # Succeeded. Protect against multiple umount() calls. self._mounted = False self.fs = None