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 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 __is_lock_owned(self): owned = False if local_file_utils.path_exists(self.__lockfile): # the lockfile exists, is it valid? try: last_sb_pid = local_file_utils.readfile(self.__lockfile) last_sb_pid = int(last_sb_pid) except (OSError, IOError, ValueError), error: self.__logger.error("Error while reading lockfile: %s" % str(error)) last_sb_pid = None if last_sb_pid is not None: if last_sb_pid == self.__pid: owned = True
def __is_lock_valid(self): valid = False if local_file_utils.path_exists(self.__lockfile): # the lockfile exists, is it valid? try: last_pid = local_file_utils.readfile(self.__lockfile) last_pid = int(last_pid) except (OSError, IOError, ValueError), error: self.__logger.error("Error while reading lockfile: %s" % str(error)) last_pid = None if last_pid is not None: if system.pid_exists(pid=str(last_pid), processname=self.__processname): valid = True
def readfile(cls, path): return local_file_utils.readfile(path)
def __sendEmail(self): """Checks if the sent of emails is set in the config file then send an email with the report :todo: Transfer this functionality to a specialized class! """ self.logger.debug("Send email report") if self.__bprofilehdl.config.has_option("report", "from"): _from = self.__bprofilehdl.config.get("report", "from") else: hostname = socket.gethostname() if "." in hostname: mailsuffix = hostname else: mailsuffix = hostname + ".ext" _from = _("SBackup Daemon <%(login)s@%(hostname)s>")\ % {'login' : str(system.get_user_from_env()), 'hostname': mailsuffix} _to = self.__bprofilehdl.config.get("report", "to") _title = _("[SBackup] [%(profile)s] Report of %(date)s")\ % { 'profile':self.__profilename, 'date': datetime.datetime.now() } logf = self.__bprofilehdl.config.get_current_logfile() if logf is None: _content = _("No log file specified.") else: if local_file_utils.path_exists(logf): _content = local_file_utils.readfile(logf) else: _content = _("Unable to find log file.") server = smtplib.SMTP() if self.logger.isEnabledFor(10): server.set_debuglevel(True) msg = MIMEMultipart() msg['Subject'] = _title msg['From'] = _from msg['To'] = _to msg.preamble = _title msg_content = MIMEText(_content) # Set the filename parameter msg_content.add_header('Content-Disposition', 'attachment', filename="sbackup.log") msg.attach(msg_content) # getting the connection if self.__bprofilehdl.config.has_option("report", "smtpserver"): if self.__bprofilehdl.config.has_option("report", "smtpport"): server.connect( self.__bprofilehdl.config.get("report", "smtpserver"), self.__bprofilehdl.config.get("report", "smtpport")) else: server.connect( self.__bprofilehdl.config.get("report", "smtpserver")) if self.__bprofilehdl.config.has_option("report", "smtptls") and\ self.__bprofilehdl.config.get("report", "smtptls") == "1": if self.__bprofilehdl.config.has_option("report", "smtpcert") and\ self.__bprofilehdl.config.has_option("report", "smtpkey"): server.starttls( self.__bprofilehdl.config.get("report", "smtpkey"), self.__bprofilehdl.config.get("report", "smtpcert")) else: server.starttls() if self.__bprofilehdl.config.has_option("report", "smtpuser") and\ self.__bprofilehdl.config.has_option("report", "smtppassword"): server.login( self.__bprofilehdl.config.get("report", "smtpuser"), self.__bprofilehdl.config.get("report", "smtppassword")) # send and close connection server.sendmail(_from, _to, msg.as_string()) server.close()
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 })