def umount(self, mounteddir): """ Default behaviour is to unmount with fuse """ if os.path.ismount(mounteddir): self.logger.debug("Unmounting `%s`" % mounteddir) # Create output and error log file outptr, outFile = tempfile.mkstemp(prefix="fuseUmount_output_") errptr, errFile = tempfile.mkstemp(prefix="fuseUmount_error_") # Call the subprocess using convenience method using lazy umount retval = subprocess.call(["fusermount", "-u", "-z", mounteddir], 0, None, None, outptr, errptr) # Close log handles os.close(errptr) os.close(outptr) outStr, errStr = local_file_utils.readfile( outFile), local_file_utils.readfile(errFile) local_file_utils.delete(outFile) local_file_utils.delete(errFile) self.logger.debug("fusermount output:\n%s\n%s" % (outStr, errStr)) if retval != 0: raise exceptions.SBException("Unable to unmount `%s`: %s" % (mounteddir, errStr)) else: self.logger.info("Successfully unmounted: `%s`" % mounteddir) else: self.logger.warning("Unable to unmount `%s`: not mounted" % mounteddir)
def __force_unsetlock(self): """Remove lockfile. """ if local_file_utils.path_exists(self.__lockfile): try: local_file_utils.delete(self.__lockfile) self.__logger.debug("Lock file '%s' removed." % self.__lockfile) except (OSError, IOError), _exc: self.__logger.error( _("Unable to remove lock file: %s") % str(_exc))
def unlock(self): """Remove lockfile. """ if local_file_utils.path_exists(self.__lockfile): if self.__is_lock_owned(): try: local_file_utils.delete(self.__lockfile) self.__logger.debug("Lock file '%s' removed." % self.__lockfile) except (OSError, IOError), _exc: self.__logger.error( _("Unable to remove lock file: %s") % str(_exc)) else: self.__logger.debug( "Unable to remove lock: not owned by this process.")
def launch(cmd, opts, env=None): """ launch a command and gets stdout and stderr outStr = a String containing the output from stdout" errStr = a String containing the error from stderr retVal = the return code (= 0 means that everything were fine ) @param cmd: The command to launch @return: (outStr, errStr, retVal) """ _logger = log.LogFactory.getLogger() # Create output log file outptr, outFile = tempfile.mkstemp(prefix="output_") # Create error log file errptr, errFile = tempfile.mkstemp(prefix="error_") # Call the subprocess using convenience method opts.insert(0, cmd) _logger.debug("Lauching : " + str(opts)) retval = subprocess.call(opts, stdin=None, stdout=outptr, stderr=errptr, env=env) # Close log handles os.close(errptr) os.close(outptr) outStr = local_file_utils.readfile(outFile) errStr = local_file_utils.readfile(errFile) local_file_utils.delete(outFile) local_file_utils.delete(errFile) return (outStr, errStr, retval)
def delete(cls, uri): local_file_utils.delete(uri)
def mount(self, source, mountbase): """ Mount the source intor the mountbase dir . This method should create a mount point to mount the source. The name of the mount point should be very expressive so that we avoid collision with other mount points @param source: The remote path @param mountbase: The mount points base dir @return: The mount point complete path """ exp = re.compile(ssh_url_re) match = exp.search(source) if not match: raise FuseFAMException( _("Error matching the schema 'ssh://*****:*****@example.com/home/' with '%s' (The '/' after server is mandatory)" ) % source) else: remoteSource = "ssh://" + match.group(1) if match.group(3): remoteSource += ":" + match.group(3) remoteSource += "@" + match.group(4) if match.group(6): remoteSource += ":" + match.group(6) remoteSource += "/" user = match.group(1) mountpoint = local_file_utils.joinpath( mountbase, self._defineMountDirName(source)) if match.group(7): pathinside = match.group(7) else: pathinside = "" #If the path is already mounted No need to retry if self.checkifmounted(source, mountbase): return (remoteSource, mountpoint, pathinside) cmd = "sshfs " + user + "@" + match.group(4) + ":/" cmd = cmd + " " + mountpoint port = match.group(6) if port: cmd += " -p " + port if not local_file_utils.path_exists(mountpoint): local_file_utils.makedir(mountpoint) if system.is_superuser(): cmd += " -o allow_root" self.logger.debug("Spawning: " + cmd) password = match.group(3) sshfsp = pexpect.spawn(cmd) i = sshfsp.expect(['(yes/no)', 'password:'******'Password:'******'yes') i = sshfsp.expect( ['(yes/no)', 'password:'******'Password:'******'(yes/no)', 'password:'******'Password:'******'%(command)s' didn't perform normally. Output => %(erroroutput)s " ) % { "command": cmd, "erroroutput": result }) return (remoteSource, mountpoint, pathinside)
def mount(self, source, mountbase): """Mount the source into the mountbase dir . This method should create a mount point to mount the source. The name of the mount point should be very expressive so that we avoid collision with other mount points @param source: The remote path @param mountbase: The mount points base dir @return: The mount point complete path """ #make the mount point spliturl = SplittedURL(source) mountpoint = self.__get_mount_dir(mountbase, spliturl) if not os.path.exists(mountpoint): os.mkdir(mountpoint) #If the path is already mounted No need to retry if not self.checkifmounted(source, mountbase): # Create output log file outptr, outFile = mkstemp(prefix="sftpFuseFAMmount_output_") # Create error log file errptr, errFile = mkstemp(prefix="sftpFuseFAMmount_error_") # the option 'allow_root' is necessary to grant access # if the script is invoked as superuser curl_cmd = ["curlftpfs", "-o", "direct_io"] if spliturl.user and spliturl.password: curl_cmd.append("-o") opts = "user=%s:%s" % (spliturl.user, spliturl.password) curl_cmd.append(opts) if os.getuid() == 0: curl_cmd.append("-o") curl_cmd.append("allow_root") server = spliturl.server if spliturl.port: server += ":" + spliturl.port curl_cmd.append(server) curl_cmd.append(mountpoint) # Call the subprocess using convenience method try: retval = subprocess.call(curl_cmd, 0, None, None, outptr, errptr) except OSError, _exc: os.rmdir(mountpoint) raise FuseFAMException( _("Couldn't found external application 'curlftpfs' needed for handling of sftp sites: %s" ) % _exc) # Close log handles os.close(errptr) os.close(outptr) outStr, errStr = local_file_utils.readfile( outFile), local_file_utils.readfile(errFile) local_file_utils.delete(outFile) local_file_utils.delete(errFile) if retval != 0: os.rmdir(mountpoint) raise FuseFAMException( _("Couldn't mount '%(server)s' into '%(mountpoint)s' : %(error)s" ) % { 'server': spliturl.server, 'mountpoint': mountpoint, 'error': errStr })