def getCopyObject(element, doc):
    """ Factory function to create Copyset Objects"""
    if isinstance(element, Node):
        __type=element.getAttribute("type")
        #print "getCopyObject(%s)" %(element.tagName)
        if __type == "disk":
            from ComPartitionCopyObject import PartitionCopyObject
            cls=PartitionCopyObject
        elif __type == "filesystem":
            from ComFilesystemCopyObject import FilesystemCopyObject
            cls=FilesystemCopyObject
        elif __type == "lvm":
            from ComLVMCopyObject import LVMCopyObject
            cls=LVMCopyObject
        elif __type == "backup":
            from ComArchiveCopyObject import ArchiveCopyObject
            cls=ArchiveCopyObject
        elif _copyobject_registry.has_key(__type):
            cls=_copyobject_registry[__type]
            #from ComPathCopyObject import PathCopyObject
            #cls=PathCopyObject
        else:
            raise UnsupportedCopyObjectException("Unsupported CopyObject type %s in element %s" % (__type, element.tagName))
        ComLog.getLogger(CopyObject.__logStrLevel__).debug("Returning new object %s" %(cls))
        return cls(element, doc)
    else:
        raise UnsupportedCopyObjectException("Wrong parameters passed to factory method getCopyObject. Expected element, doc.")
    def _testCdsls(self, repository, clusterinfo, results):
        self.cwd.pushd(os.path.join(repository.root, repository.getMountpoint()))
#        setupCDSLRepository._createCDSLFiles(".")
        _dirs=results.keys()
        _dirs.sort()
        repository.buildInfrastructure(clusterinfo)
        for _path in _dirs:
            _cdsl=None
            try:
                _cdsl=repository.getCdsl(_path)
            except CdslNotFoundException:
                if results[_path][1] == True:
                    _cdsl=getCdsl(_path, CDSL_HOSTDEPENDENT_TYPE, repository, clusterinfo)
                elif results[_path][1] == False:
                    _cdsl=getCdsl(_path, CDSL_SHARED_TYPE, repository, clusterinfo)
            
            if _cdsl:
                self.assert_(repository.commit(_cdsl))
        ComLog.setLevel(logging.DEBUG, "comoonics.cdsl")
        repository.refresh()
        ComLog.setLevel(logging.INFO, "comoonics.cdsl")
        for cdsl in repository.getCdsls():
            self.assert_(repository.delete(cdsl))
        repository.removeInfrastructure(clusterinfo)       
#        setupCDSLRepository._removeCDSLFiles(".")
        self.cwd.popd()
 def getIP(self):
     """
     @return: Returns ip-address of interface
     @rtype: string
     """
     #optional attribute, return empty string if not set
     try:
         self.log.debug("get ip attribute: " + self.getAttribute(ComoonicsClusterRepository.attribute_netdev_ip))
         # special case for dhcp we'll return the given ipaddress
         if self.isDHCP():
             from comoonics import ComSystem
             import re
             try:
                 output=ComSystem.execLocalOutput("PATH=/sbin:/usr/sbin ip addr show %s" %self.getName(), True)
                 _mac=re.search("link/ether (?P<mac>\S+)", output).group("mac")
                 _ip=re.search(".*inet (?P<ip>[0-9.]+)", output).group("ip")
                 if _mac.upper() == self.getMac().upper():
                     return _ip
                 else:
                     return self.getAttribute(ComoonicsClusterRepository.attribute_netdev_ip)
             except:
                 ComLog.debugTraceLog(self.log)
                 return self.getAttribute(ComoonicsClusterRepository.attribute_netdev_ip)
         else:
             return self.getAttribute(ComoonicsClusterRepository.attribute_netdev_ip)
     except NameError:
         return ""
    def doPre(self):
        """
        Unpacks the given file to dest
        """
        srcfile=self.getAttribute("name")
        destfile=self.getAttribute("dest")
        __mkdir=self.getAttributeBoolean("mkdir", default=True)

        if not ComSystem.execMethod(os.path.exists, destfile) and __mkdir:
            ComLog.getLogger(ArchiveRequirement.__logStrLevel__).debug("Path %s does not exists. I'll create it." % destfile)
            os.makedirs(destfile)

        if self.check() and not ComSystem.isSimulate():
            if not os.access(srcfile, os.R_OK) or not os.access(destfile, os.F_OK) or not os.access(destfile, os.W_OK):
                raise ArchiveRequirementException("Either srcfile %s is not readable or dest %s is not writeable" % (srcfile, destfile))

        __cmd="rm -rf %s/*" % destfile
        (rc, rv) = ComSystem.execLocalGetResult(__cmd)
        if rc >> 8 != 0:
            raise RuntimeError("running \"%s\" failed: %u, %s" % (__cmd, rc,rv))

        self.olddir=os.curdir
        ComSystem.execMethod(os.chdir, destfile)
        __cmd="gzip -cd %s | cpio -i" % srcfile
        (rc, rv, stderr) = ComSystem.execLocalGetResult(__cmd, True)
        if rc >> 8 != 0:
            raise RuntimeError("running \"%s\" failed: %u, %s, %s" % (__cmd, rc,rv, stderr))
    def getCommands(self, what):
            names = self.get_names()
            cmds = {}
            prevname = ''
            for name in names:
                if name[:len(what)] == what:
                    if name == prevname:
                        continue
                    prevname = name
                    under1=len(what)+name[len(what):].find("_")+1
#                    under2=-1
#                    if under1>0:
#                        under2=under1+name[under1:].find("_")+1
                    ComLog.getLogger().debug("do_help(name %s, what %s, under1 %u/%s" %(name, what, under1, name[:under1]))

                    if under1==len(what):
                        cmds[name[len(what):]]=1
                    elif under1>0 and not hasattr(self, name[:under1-1]):
                        cmds[name[len(what):]]=1
                    #elif not hasattr(self, "%s_%s" %(name[:name(+len
                    #    cmds[name[len(what):]]=1

#            self.stdout.write("%s\n"%str(self.doc_leader))
            self.stdout.write("%s" %(self.doc_header))
#            ComLog.getLogger().debug("do_help(cmds: %s" %(cmds))
            cmds_keys=cmds.keys()
            cmds_keys.sort()
            return cmds_keys
    def do_help(self, arg):
        ComLog.getLogger().debug("do_help(%s)" %(arg))
        arg=arg.strip()
        arg=arg.replace(" ", "_")
        if arg == '?' or arg == "help":
            cmds_keys=self.getCommands("do_")
            self.print_topics("",  cmds_keys, 15, 6)
            #self.print_topics(self.undoc_header, cmds_undoc, 15,80)
        elif arg:
            # XXX check arg syntax
            try:
                func = getattr(self, 'help_' + arg)
            except AttributeError:
                cmds_keys=self.getCommands("do_%s_" %(arg))
                if cmds_keys:
                    ComLog.getLogger().debug("do_help(%s, %s)" %(arg, cmds_keys))
                    self.print_topics("", cmds_keys, 15, 6)
                else:
                    try:
                        doc=getattr(self, 'do_' + arg).__doc__
                        if doc:
                            self.stdout.write("%s"%str(doc))
                            return
                    except AttributeError:
                        pass
#                    self.stdout.write("%s\n"%str(self.nohelp % (arg)))
                return
            func()
        else:
            self.stdout.write(HP_EVA_SSSU_Sim.DEFAULT_HELP)
 def updateMetaData(self, element):
    ComLog.getLogger(self.__logStrLevel__).debug("%u logical volumes cloning all from source" %(len(self.getVolumeGroup().getLogicalVolumes())))
    #ComLog.getLogger(self.__logStrLevel__).debug("Element to copy %s" %(element))
    if (len(self.getVolumeGroup().getLogicalVolumes()) == 0):
       #ComLog.getLogger(self.__logStrLevel__).debug("0 logical volumes cloning all from source")
       XmlTools.merge_trees_with_pk(element, self.getVolumeGroup().getElement(), self.document, "name", XmlTools.ElementFilter("logicalvolume"))
       self.vg=VolumeGroup(self.getVolumeGroup().getElement(), self.getDocument())
 def __new__(cls, *args, **kwds):
     if len (args) > 0 and isinstance(args[0], Node):
         __type=args[0].getAttribute("type")
         #print "getCopyObject(%s)" %(element.tagName)
         if __type == "disk":
             from ComPartitionCopyObject import PartitionCopyObject
             cls=PartitionCopyObject
         elif __type == "filesystem":
             from ComFilesystemCopyObject import FilesystemCopyObject
             cls=FilesystemCopyObject
         elif __type == "lvm":
             from ComLVMCopyObject import LVMCopyObject
             cls=LVMCopyObject
         elif __type == "backup":
             from ComArchiveCopyObject import ArchiveCopyObject
             cls=ArchiveCopyObject
         elif _copyobject_registry.has_key(__type):
             cls=_copyobject_registry[__type]
             #from ComPathCopyObject import PathCopyObject
             #cls=PathCopyObject
         else:
             raise UnsupportedCopyObjectException("Unsupported CopyObject type %s in element %s" % (__type, args[0].tagName))
         ComLog.getLogger(CopyObject.__logStrLevel__).debug("Returning new object %s" %(cls))
         return object.__new__(cls, args, kwds)
     else:
         return object.__new__(cls, args, kwds)
    def create(self):
        """
        Newly creates the logical volume
        """
        LinuxVolumeManager.has_lvm()
        size=""

        if self.ondisk and self.getAttribute("overwrite", "false") == "true":
            self.remove()

        try:
            self.init_from_disk()
        except:
            pass

        if self.ondisk:
            raise LinuxVolumeManager.LVMAlreadyExistsException(self.__class__.__name__+"("+str(self.getAttribute("name"))+")")
        try:
            size=self.getAttribute("size")
            if int(self.getAttribute("size")) > int(self.parentvg.getAttribute("free")):
                ComLog.getLogger(self.__logStrLevel__).warn("Requested LV size %s is too big taking free %s" % (self.getAttribute("size"), self.parentvg.getAttribute("free")))
                self.setAttribute("size", self.parentvg.getAttribute("free"))
                size=self.getAttribute("size")
        except NameError:
            if ComSystem.isSimulate():
                size="1000"
            else:
                size=self.parentvg.getAttribute("free")
        LinuxVolumeManager.lvm('lvcreate', '-L %sM' %size, '-n %s' %str(self.getAttribute("name")), '%s' %str(self.parentvg.getAttribute("name")))
        self.init_from_disk()
        if ComSystem.isSimulate():
            self.ondisk=True
def parseClusterConfFP(_clusterconffp, _clusterconf, _validate=False):
    from comoonics import ComLog
    from comoonics import XmlTools
    try:
        doc = XmlTools.parseXMLFP(_clusterconffp)
    except Exception, arg:
        ComLog.getLogger().critical("Problem while reading clusterconfiguration (%s): %s" %(_clusterconf, str(arg)))
        raise
 def selection_changed(self, selection, dest, filter):
     (model, iter) = selection.get_selected()
     ComLog.getLogger(__logStrLevel__).debug("Selection changed %s, %s, %s %s" % (model, iter, dest, filter))
     dest.clear()
     if iter:
         dest.createStoreModelFromNode(model.get_value(iter, DOMModel.COLUMN_NODE).get_data(DOMModel.NODE_KEY))
     if filter:
         filter.refilter()
 def setUp(self):
     import inspect
     import os.path
     self.filename=os.path.join(os.path.dirname(inspect.getfile(self.__class__)), "loggingconfig.xml")
     self.cp=ConfigParser()
     self.cp.read(self.filename)
     self.loggers=["atix", "atix.atix1", "atix.atix2"]
     ComLog.fileConfig(self.filename)
 def delete_attribute(self, item, model, iter, name):
     self.status("Menu Delete attribute... " + name + " pressed menuitem %s, model %s, iter %s" % (item, model, iter))
     value = model.get_value(iter, DOMModel.COLUMN_NODE)
     ref_node=value.get_data(DOMModel.NODE_KEY)
     ref_node.removeAttribute(name)
     ComLog.getLogger(__logStrLevel__).debug("ref_node: %s" % ref_node)
     self.__basemodelr.clear()
     self.__basemodelr.createStoreModelFromNode(ref_node)
    def serialize(self, element):
        from comoonics.storage.ComArchive import Archive
        earchive=self.getElement().getElementsByTagName("archive")[0]
        archive=Archive(earchive, self.getDocument())
#        print "Created archive: %s" %(archive)
        ComLog.getLogger(self.__logStrLevel__).debug("Saving element %s to archive" %(element))
        archive.addNextDOMElement(element)
        ComLog.getLogger(self.__logStrLevel__).debug("Saved element %s to archive element" %(element.tagName))
 def getRootPartition(self):
     _dev = self._scanRootDevice()
     ComLog.getLogger(__logStrLevel__).debug("detected rootdevice %s" %_dev)                
     if not _dev: 
         return
     _pv = self._getLVM_physicalVolume(_dev)
     if _pv:
         return [ self._normalizeDisk(_pv)[1] ]
     return [ self._normalizeDisk(_dev) [1] ]
 def saveFile(self, _uri=None):
     if not _uri:
         _uri=self.filename
     ComLog.getLogger(__logStrLevel__).debug("file: %s" % _uri)
     stream=open(_uri,"w+")
     PrettyPrint(self.__basemodell.document, stream)
     self.filename=_uri
     self.status("save to file %s succeeded" % _uri)
     self.updateTitle(sefl.filename)
 def dtd_changed(self, filedialog, chooser):
     ComLog.getLogger(__logStrLevel__).debug("dtd changed pressed %s, %s." %(filedialog, chooser))
     try:
         self.newFromDTDFile(filedialog.get_filename())
         chooser.get_model().clear()
         for element_txt in self.dtd.get_elements():
             chooser.append_text(element_txt)
     except:
         pass
def setVerbose(flag):
    if flag:
        try:
            import logging
            from comoonics import ComLog
            logging.basicConfig()
            ComLog.setLevel(logging.DEBUG)
        except ImportError:
            pass
 def __init__(self, element, doc):
     CopysetJournaled.__init__(self, element, doc)
     try:
         __source = element.getElementsByTagName("source")[0]
         self.source = CopyObject(__source, doc)
     # except Exception, e:
     except Exception, e:
         ComLog.getLogger(__logStrLevel__).warning(e)
         raise ComException("source for copyset not defined")
    def resolve(self):
#        print "ArchiveMetadata.resolve"
        from comoonics.storage.ComArchive import Archive
        earchive=self.getElement().getElementsByTagName("archive")[0]
        archive=Archive(earchive, self.getDocument())
#        print "Created archive: %s" %(archive)
        element=archive.getNextDOMElement()
        ComLog.getLogger(self.__logStrLevel__).debug("Found element %s" %(element.tagName))
        return element
def createAssistantHelper(classname, query):
    from ComClusterAssistantHelper import RedHatClusterAssistantHelper
    from ComStorageAssistantHelper import StorageAssistantHelper

    if classname:
        try: 
            return eval(classname)(query)
        except Exception:
            ComLog.getLogger(__logStrLevel__).warning("Assitanthelper %s coud not be initialized" %classname)
 def undoModifications(self):
     """undos all modifications """
     ComLog.getLogger(Modificationset.__logStrLevel__).debug("Modifications: %s" % self.modifications)
     self.modifications.reverse()
     for mod in self.modifications:
         try:
             mod.undoRequirements()
             mod.undoModification()
         except NotImplementedError, e:
             log.warning(e)
 def _fromDB(self):
     _rs=self._getRecordSet()
     if _rs.num_rows()>0:
         _obj=_rs.fetch_row(1, 1)
         for _key, _value in _obj[0].items():
             setattr(self, self.__dict__["schemarev"][_key], _value)
     else:
         ComLog.debugTraceLog(self.logger)
         raise NoDataFoundForObjectException("No data found for object. ID: %s/%s" %(str(self.tablename), str(self.id)))
     self._fromdb=True
 def setPathElement(self, element):
     __parent=self.path.getElement().parentNode
     __newnode=element.cloneNode(True)
     __oldnode=self.path.getElement()
     self.path.setElement(__newnode)
     # only replace attributes
     try:
         __parent.replaceChild(__newnode, __oldnode)
     except Exception, e:
         ComLog.getLogger(PathCopyObject.__logStrLevel__).warning(e)
def getMetadataSerializer(element, doc):
    if isinstance(element, Node):
        archives = element.getElementsByTagName("archive")
        if len(archives) > 0:
            ComLog.getLogger(MetadataSerializer.__logStrLevel__).debug("Returning new object ArchiveMetadataSerializer")
            return ArchiveMetadataSerializer(element, doc)
        else:
            raise UnsupportedMetadataException("Unsupported Metadata type in element " % (element.tagName))
    else:
        raise UnsupportedMetadataException("Unsupported Metadata type because no domelement given (%s)" % (element))
Esempio n. 26
0
def getBlockDeviceForWWWNLun(wwwn, lun, hosts=None):
   """ returns the scsidevicename of wwwn, lun combination searching on all hosts or the given ones. """
   blockdev_file=None
   if not hosts:
      try:
         hosts=getFCHosts()
      except SCSIException:
         ComLog.debugTraceLog(log)
         hosts=getQlaHosts()

   for host in hosts:
      device_dir=FCPATH_HOST_DEVICE %(host)
      try:
         for device_file in os.listdir(device_dir):
            match=re.match(SCSITARGET_PATTERN, device_file)
            if match:
               (scsi_hostid, scsi_busid, scsi_id)=match.groups()
               _wwwn=getWWWN(scsi_hostid, scsi_busid, scsi_id)
               log.debug("Found WWWN: %s==%s" %(_wwwn, wwwn))
               if _wwwn==wwwn:
                  blockdev_file=FCPATH_HOST_LUN %(host, int(scsi_hostid), int(scsi_busid), int(scsi_id), int(scsi_hostid), int(scsi_busid), int(scsi_id), int(scsi_hostid), int(scsi_busid), int(scsi_id), int(lun))
                  for filename in os.listdir(blockdev_file):
                     if filename.startswith("block:"):
                        blockdev_file=os.path.join(blockdev_file, filename)
                        break
      except IOError:
         hostid=getHostId(host)
         busid=0
         qlafile=QLA_SCSIPATH_PROC+"/%u" %(int(hostid))
         if os.path.exists(qlafile):
            qla=open(qlafile, "r")
            regexp_lun=re.compile(QLALUN_PATTERN)
            regexp_port=re.compile(QLAPORT_PATTERN)
            scsi_ids=list()
            for line in qla:
               match=regexp_port.match(line)
               if match:
                  (scsi_hostid, scsi_id, wwpn, wwnn, _x, _y)=match.groups()
                  log.debug("match: %s, %s, %s" %(wwpn, wwnn, scsi_id))
                  if wwnn==wwwn or wwpn==wwwn:
                     scsi_ids.append(int(scsi_id))
               if not match:
                  match=regexp_lun.match(line)
                  if match:
                     (scsi_id, scsi_lun)=match.groups()
                     if int(scsi_id) in scsi_ids and int(scsi_lun) == int(lun):
                        blockdev_file=SCSIPATH_TARGET_BLOCK %(int(hostid), int(busid), int(scsi_id), int(scsi_lun))
            qla.close()

   if blockdev_file and os.path.exists(blockdev_file):
      log.debug("blockdevfile: %s" %(blockdev_file))
      if os.path.exists(blockdev_file):
         return "/dev/"+os.path.basename(os.readlink(blockdev_file))
   else:
      raise SCSIException("Could not find blockdevice for wwwn: %s, lun: %u" %(wwwn, int(lun)))
    def testlogger(self):
        import logging
        import inspect
        import os.path
        from comoonics import ComLog
        _mylogger=logging.getLogger("comoonics.ComLog")
        logging.basicConfig()
        _mylogger.setLevel(logging.DEBUG)
        #from comoonics.db.ComDBLogger import DBLogger
        #registerHandler("DBLogger", DBLogger)
        _filenames=("loggingconfig.ini")
        ComLog.getLogger().info("Testing ComLog:")
        loggers={"test1": logging.DEBUG,
                 "test2": logging.INFO,
                 "test3": logging.WARNING}
        for loggername in loggers.keys():
            print "%s level: %s" %(loggername, logging.getLevelName(loggers[loggername]))
            ComLog.setLevel(loggers[loggername], loggername)
            self.__testLogger(loggername, ComLog.getLogger(loggername))

        print("mylogger without level")
        self.__testLogger("mylogger", ComLog.getLogger("mylogger"))
        cp=None

        print("ComLog._classregistry: %s" %ComLog._classregistry)
        for _filename in _filenames:
            logging.shutdown()
            print("Testing configfile %s/%s cwd: %s" %(os.path.dirname(inspect.getfile(self.__class__)), _filename, os.path.curdir))
            ComLog.fileConfig(os.path.join(os.path.dirname(inspect.getfile(self.__class__)), _filename), None, )
            rootlogger=ComLog.getLogger()
            self.__testLogger("root", rootlogger)
            print("handlernames: %s" %rootlogger.manager.loggerDict.keys())
            for _lname in [ "atix", "atix", "atix.atix1" ]:
                self.__testLogger(_lname, logging.getLogger(_lname))
                self.__testLogger(_lname+".test", logging.getLogger(_lname+".test"))
 def replayJournal(self):
    """
    replays the journal from top to buttom. The last inserted method first.
    """
    self.__journal__.reverse()
    for je in self.__journal__:
       ComLog.getLogger(JournaledObject.__logStrLevel__).debug("Journalentry: %s, %s" %(je.method, je.ref.__class__))
       undomethod=""
       try:
          if inspect.ismodule(je.ref):
             undomethod=self.__undomap__[str(je.ref.__name__+"."+je.method)]
          else:
             undomethod=self.__undomap__[str(je.ref.__class__.__name__+"."+je.method)]
          ComLog.getLogger(JournaledObject.__logStrLevel__).debug("Undomethod: %s(%s)" % (undomethod, je.params))
          if not je.params or (type(je.params)==list and len(je.params) == 0):
             # changed call of replay
             # __class__.__dict__[undomethod] does not work with inherited methods
             #je.ref.__class__.__dict__[undomethod](je.ref)
             getattr(je.ref,undomethod)()
          elif type(je.params)!=list and type(je.params)!=tuple:
             getattr(je.ref, undomethod)(je.params)
          elif len(je.params) == 1:
             #je.ref.__class__.__dict__[undomethod](je.ref, je.params[0])
             getattr(je.ref,undomethod)(je.params[0])
          else:
             #je.ref.__class__.__dict__[undomethod](je.ref, je.params)
             getattr(je.ref,undomethod)(*je.params)
       except Exception, e:
          ComLog.getLogger(JournaledObject.__logStrLevel__).warn("Caught exception e during journal replay of method %s => %s on %s: %s" % (je.method, undomethod, je.ref.__class__.__name__, e))
          ComLog.debugTraceLog(ComLog.getLogger(JournaledObject.__logStrLevel__))
 def __new__(cls, *args, **kwds):
     if len (args) > 0 and isinstance(args[0], Node):
         archives=args[0].getElementsByTagName("archive")
         if len(archives) > 0:
             cls=ArchiveMetadataSerializer
             ComLog.getLogger(MetadataSerializer.__logStrLevel__).debug("Returning new object %s" %(cls))
             return object.__new__(cls, *args, **kwds)
         else:
             raise UnsupportedMetadataException("Unsupported Metadata type in element " % (args[0].tagName))
     else:
         raise UnsupportedMetadataException("Unsupported Metadata type because no domelement given (%u)" %(len(args)))
 def getValidElementNamesAppend(self, element):
     ComLog.getLogger(__logStrLevel__).debug("in getValidElementNamesAppend")
     __elem=self.dtd.get_elem(element.tagName)
     __state=__elem.get_start_state()
     __nodes=element.childNodes
     for i in range(len(__nodes)):
         if __nodes[i].nodeType != __nodes[i].ELEMENT_NODE:
             continue
         ComLog.getLogger(__logStrLevel__).debug("found element %s" %__nodes[i].tagName)
         __state=__elem.next_state(__state, __nodes[i].tagName)
     return __elem.get_valid_elements(__state)
Esempio n. 31
0
class Plugin(object):
    """
    Plugin Interface
    """
    logger = ComLog.getLogger("comoonics.fenceacksv.plugins.ComPlugin.Plugin")

    def __init__(self,
                 _name=None,
                 stdout=sys.stdout,
                 stdin=sys.stdin,
                 stderr=sys.stderr):
        self._commands = dict()
        self._commands_help = dict()
        self.stdout = stdout
        self.stdin = stdin
        self.stderr = stderr
        self.name = _name

    def getName(self):
        return self.name

    def addCommand(self, _name, methodref, *params, **kwds):
        self._commands[_name] = Method(methodref, *params, **kwds)

    def getCommands(self):
        return self._commands.keys()

    def hasCommand(self, _name):
        return self._commands.has_key(_name)

    def getCommand(self, _name):
        """
        returns a Methodtype
        """
        if isinstance(self._commands[_name], Method):
            return self._commands[_name]
        else:
            return Method(self._commands[_name])

    def doCommand(self, _name, *params, **kwds):
        self.doPre(_name, *params, **kwds)
        _ret = self.getCommand(_name).do(*params, **kwds)
        self.doPost(_name, *params, **kwds)
        return _ret

    def doPre(self, _name, *params, **kwds):
        pass

    def doPost(self, _name, *params, **kwds):
        pass

    def help_short(self):
        buf = inspect.getdoc(self)
        if not buf:
            buf = ""
        return buf

    def help(self, _command=None):
        """
        Help about this plugin
        """
        Plugin.logger.debug("help(%s)" % _command)
        if not _command or _command == "" or _command == "help":
            buf = inspect.getdoc(self)
            if not buf:
                buf = ""
            buf += "\nCommands:\n" + ", ".join(self.getCommands())
            return buf
        else:
            if self._commands_help.has_key(_command):
                return self._commands_help[_command]
            else:
                return self.getCommand(_command).help()
Esempio n. 32
0
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

__version__= "$Revision $"

from comoonics import ComLog

logger=ComLog.getLogger("comoonics.DictTools")

def searchDict(hash,searchedElement):
    """
    Searches for a given key in a nested dictionary
    @param hash: Dictionary to seach in
    @type hash: L{dict}
    @param searchedElement: key to search for
    @type searchedElement: L{string}
    @return: Returns if the given string is found as key in given hash
    @rtype: L{Boolean}
    """
    for (key, value) in hash.items():
        if key == searchedElement:
            return True
        else:
Esempio n. 33
0
class ISOFSModificationset(ModificationsetJournaled):
    """ implementation class for this modificationset """
    __logStrLevel__ = "comoonics.enterprisecopy.ComISOFSModificationset.ISOFSModificationset"
    logger = ComLog.getLogger(__logStrLevel__)

    def __init__(self, element, doc):
        """
        default constructor:
        __init__(element, doc)
        """
        super(ISOFSModificationset, self).__init__(element, doc)
        try:
            __path = element.getElementsByTagName('path')
        except Exception:
            raise ComException("Path for modificationset \"%s\" not defined" %
                               self.getAttribute("name", "unknown"))
        self.pathlist = __path
        self.createModificationsList(
            self.getElement().getElementsByTagName("modification"), doc)
        self.isoname = self.getAttribute("name")

    def doPre(self):
        ISOFSModificationset.logger.debug("doPre() CWD: " + os.getcwd())
        super(ISOFSModificationset, self).doPre()

    def doPost(self):
        super(ISOFSModificationset, self).doPost()
        self.replayJournal()
        self.commitJournal()
        ISOFSModificationset.logger.debug("doPost() CWD: " + os.getcwd())
        __cmd = self._get_mkisofs_command()
        __rc, __ret = ComSystem.execLocalStatusOutput(__cmd)
        ISOFSModificationset.logger.debug("mkisofs: " + __cmd + ": " + __ret)
        if __rc != 0:
            raise ComException(__cmd + __ret)

    def _get_mkisofs_command(self):
        _knownopts = ["bootcd", "cdlabel"]
        _opts = ["-o " + self.isoname, "-J", "-R", "-D"]
        if self.getProperties() and self.getProperties().has_key("bootcd"):
            if self.getProperties().getProperty(
                    "bootcd").getValue() == "livecd":
                _opts.append("-no-emul-boot")
                _opts.append("-boot-info-table")
                _opts.append("-boot-load-size 4")
                _opts.append("-b boot/isolinux.bin")
                _opts.append("-c boot/isolinux.boot")
        if self.getProperties() and self.getProperties().has_key("cdlabel"):
            key = self.getProperties().getProperty("cdlabel")
            _opts.append("-A '%s'" % key.getValue())
            _opts.append("-V '%s'" % key.getValue())
        for _property in self.getProperties().keys():
            if _property in _knownopts:
                continue
            _value = self.getProperties()[_property].getValue()
            if _value == "":
                _opts.append("-%s" % _property)
            else:
                _opts.append("-%s %s" % (_property, _value))
        for _element in self.pathlist:
            _opts.append(Path(_element, self.getDocument()).getPath())

        return CMD_MKISOFS + " " + " ".join(_opts)
Esempio n. 34
0
# $Id: testlog.py,v 1.3 2007-06-13 09:17:22 marc Exp $
import sys
import traceback

sys.path.append("../lib")
#print sys.path

try:
    from comoonics import ComLog
    from comoonics import ComSystem
except ImportError:
    print "Exception in import "
    print traceback.print_exc()

log = ComLog.getLogger()
print log
log.critical("This is really critical")

try:
    raise TypeError, "bogus typerror for testing"
except Exception, e:
    log.exception("Huston, we have a problem")

#ComSystem.execLocal("echo \"hallo du\"")

#################
# $Log: testlog.py,v $
# Revision 1.3  2007-06-13 09:17:22  marc
# - better testing, but obsolete??
#
def parse_cmdline(args=sys.argv):
    parser = OptionParser(description=__doc__,
                          usage="usage: %prog [options] action [arguments]",
                          version=__version__)
    # Flags
    parser.add_option("-v",
                      "--verbose",
                      default=False,
                      action="store_true",
                      help="toggle debugmode and be more helpful")
    #   parser.add_option("-a", "--ask",      dest="ask",      default=False, action="store_true", help="ask before any being command executed")
    #   parser.add_option("-S", "--simulate",   dest="simulate",   default=False, action="store_true", help="don't execute anything just simulate. Only valid for developer. It might not do what you expect!")

    # Options
    backup_options = OptionGroup(parser, "Backup Options")
    backup_options.add_option(
        "-t",
        "--taskname",
        default="",
        help="Specify the name of the job to be executed. Default %default")
    backup_options.add_option(
        "-T",
        "--type",
        default=comoonics.storage.ComArchive.ArchiveHandler.NONE,
        help="Set the backup format type. Default: %default")
    backup_options.add_option(
        "-c",
        "--compression",
        default=comoonics.storage.ComArchive.ArchiveHandler.NONE,
        help=
        "Set the compression of this backup handler (if any). Default %default."
    )
    backup_options.add_option(
        "-f",
        "--format",
        default=comoonics.storage.ComArchive.ArchiveHandler.NONE,
        help=
        "Set the backup format of this backup handler (if any). Default %default."
    )
    backup_options.add_option(
        "-p",
        "--property",
        dest="properties",
        default=list(),
        action="append",
        help=
        "List of properties to be added to initialization (dependent on implementation). Each property must be name=value pair. Default %default"
    )
    parser.add_option_group(backup_options)
    try:
        parser.setGlobalDefaultsFilename(get_defaultsfiles()[0])
        parser.setLocalDefaultsFilename(get_defaultsfiles()[1],
                                        get_defaultsenvkey())
    except (TypeError, NameError):
        pass

    (options, args) = parser.parse_args(args)

    if options.verbose:
        ComLog.setLevel(logging.DEBUG)
    else:
        ComLog.setLevel(logging.INFO)
    if not args or len(args) < 3:
        parser.error("To few arguments given to command.")
    properties = dict()
    for property in options.properties:
        try:
            name, value = property.split("=", 1)
            properties[name] = value
        except ValueError:
            warnings.warn(
                "Skipping property %s because of wrong format. Expecting name=value."
                % property)
    return (options, args[1], args[2:], properties)
Esempio n. 36
0
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

__version__ = "$Revision: 1.19 $"
# $Source: /atix/ATIX/CVSROOT/nashead2004/management/comoonics-clustersuite/python/lib/comoonics/cluster/ComClusterRepository.py,v $

from comoonics import ComLog
from comoonics.ComDataObject import DataObject
from comoonics.ComExceptions import ComException
from comoonics.DictTools import searchDict, createDomFromHash
import comoonics.XmlTools

log = ComLog.getLogger("comoonics.cdsl.ComClusterRepository")


class ClusterMacNotFoundException(ComException):
    pass


class ClusterIdNotFoundException(ComException):
    pass


class ClusterRepositoryConverterNotFoundException(ComException):
    pass


class ClusterObject(DataObject):
                 "python" + sys.version[:3], "site-packages"))

try:
    from comoonics.tools.poptparse import PersistentOptionParser as OptionParser
except ImportError:
    from optparse import OptionParser
from optparse import OptionGroup

import comoonics.storage
from comoonics import ComLog

__logStrLevel__ = "com-backup"

import logging
logging.basicConfig()
ComLog.setLevel(logging.INFO)


def parse_cmdline(args=sys.argv):
    parser = OptionParser(description=__doc__,
                          usage="usage: %prog [options] action [arguments]",
                          version=__version__)
    # Flags
    parser.add_option("-v",
                      "--verbose",
                      default=False,
                      action="store_true",
                      help="toggle debugmode and be more helpful")
    #   parser.add_option("-a", "--ask",      dest="ask",      default=False, action="store_true", help="ask before any being command executed")
    #   parser.add_option("-S", "--simulate",   dest="simulate",   default=False, action="store_true", help="don't execute anything just simulate. Only valid for developer. It might not do what you expect!")
Esempio n. 38
0
from ConfigParser import ConfigParser as oldConfigParser
from comoonics.ComExceptions import ComException
from comoonics.ComProperties import Properties
from comoonics import ComLog
from comoonics import XmlTools

from xml.dom import Node

class NoLoggingConfigFound(ComException):
    pass

class IncompatibleConfigFileType(ComException):
    def __str__(self):
        return "The file %s seems to be not compatible to XMLConfigParser." %self.value

mylogger=ComLog.getLogger("comoonics.XMLConfigParser")
#mylogger.setLevel(logging.DEBUG)

class ConfigParser(oldConfigParser):
    LOGGING_TAGNAME="logging"
    LOGGERS_TAGNAME="loggers"
    LOGGER_TAGNAME="logger"
    FORMATTERS_TAGNAME="formatters"
    FORMATTER_TAGNAME="formatter"
    HANDLERS_TAGNAME="handlers"
    HANDLER_TAGNAME="handler"
    ARGS_TAGNAME="args"
    ARG_TAGNAME="arg"
    #PROPERTIES_TAGNAME="properties"
    #PROPERTY_TAGNAME="property"
def getBlockDeviceForWWWNLun(wwwn, lun, hosts=None):
    """ returns the scsidevicename of wwwn, lun combination searching on all hosts or the given ones. """
    blockdev_file = None
    if not hosts:
        try:
            hosts = getFCHosts()
        except SCSIException:
            ComLog.debugTraceLog(log)
            hosts = getQlaHosts()

    for host in hosts:
        device_dir = FCPATH_HOST_DEVICE % (host)
        if os.path.isdir(device_dir):
            for device_file in os.listdir(device_dir):
                match = re.match(SCSITARGET_PATTERN, device_file)
                if match:
                    (scsi_hostid, scsi_busid, scsi_id) = match.groups()
                    _wwwn = getWWWN(scsi_hostid, scsi_busid, scsi_id)
                    log.debug("Found WWWN: %s==%s" % (_wwwn, wwwn))
                    if _wwwn == wwwn:
                        blockdev_file = FCPATH_HOST_LUN % (
                            host, int(scsi_hostid), int(scsi_busid),
                            int(scsi_id), int(scsi_hostid), int(scsi_busid),
                            int(scsi_id), int(lun))
                        blockdev_file += "/block"

        hostid = getHostId(host)
        busid = 0
        qlafile = QLA_SCSIPATH_PROC + "/%u" % (int(hostid))
        if os.path.exists(qlafile):
            qla = open(qlafile, "r")
            regexp_lun = re.compile(QLALUN_PATTERN)
            regexp_port = re.compile(QLAPORT_PATTERN)
            nodename = None
            scsi_ids = list()
            for line in qla:
                match = regexp_port.match(line)
                if match:
                    (scsi_hostid, scsi_id, wwpn, wwnn, _x, _y) = match.groups()
                    log.debug("match: %s, %s, %s" % (wwpn, wwnn, scsi_id))
                    if wwnn == wwwn or wwpn == wwwn:
                        scsi_ids.append(int(scsi_id))
                if not match:
                    match = regexp_lun.match(line)
                    if match:
                        (scsi_id, scsi_lun) = match.groups()
                        if int(scsi_id) in scsi_ids and int(scsi_lun) == int(
                                lun):
                            blockdev_file = SCSIPATH_TARGET_BLOCK % (
                                int(hostid), int(busid), int(scsi_id),
                                int(scsi_lun))
            qla.close()

    if blockdev_file and os.path.exists(blockdev_file):
        log.debug("blockdevfile: %s" % (blockdev_file))
        if os.path.exists(blockdev_file):
            return "/dev/" + os.path.basename(os.readlink(blockdev_file))
    else:
        raise SCSIException(
            "Could not find blockdevice for wwwn: %s, lun: %u" %
            (wwwn, int(lun)))
Esempio n. 40
0

def setConfigPath(option, opt, value, parser):
    print "setConfigPath %s" % value
    print option
    print opt
    print parser
    CONFIGURATION_PATH = value


def exit(value, text=""):
    print text
    sys.exit(value)


ComLog.setLevel(logging.INFO)
log = ComLog.getLogger("comoonics-assistant")

parser = OptionParser(description=__doc__, version=__version__)

# Default Options
parser.add_option("-D",
                  "--debug",
                  dest="debug",
                  default=False,
                  action="callback",
                  callback=setDebug,
                  help="Debug")
parser.add_option("-s",
                  "--simulate",
                  dest="debug",
Esempio n. 41
0
def setDebug(option, opt, value, parser):
    ComLog.setLevel(logging.DEBUG)
    setattr(parser.values, option.dest, True)
"""

Init this package

"""

from comoonics import ComLog
from ComDBLogger import DBLogger
from ComDBObject import DBObject, NoDataFoundForObjectException, loadDBObjectsRegistry, setDBObjectsRegistry, registerDBObjectProperties, getDBObjectProperties, hasDBObjectProperties
from ComDBJobs import DBJobs, DBJob, DBAction, DBJob2Action, registerActionObject, JobException
ComLog.registerHandler("DBLogger", DBLogger)

########
# $Log: __init__.py,v $
# Revision 1.4  2008-02-28 14:19:09  marc
# - added autoimports.
# - added the dbobject and dbjobs
#
# Revision 1.3  2007/06/19 15:10:07  marc
# fixed importing
#
# Revision 1.2  2007/06/13 09:03:52  marc
# - using new ComLog api
# - default importing of ComDBLogger and registering at ComLog
#
# Revision 1.1  2007/04/02 11:21:23  marc
# For Hilti RPM Control:
# - initial revision
#
# Revision 1.1  2007/03/05 16:10:30  marc
# first rpm version
class SysrqModification(Modification):
    logger = ComLog.getLogger(
        "comoonics.enterprisecopy.ComSysrq.SysrqModification")

    type = None
    child = None
    _log = None

    def _createElement(**kwds):
        import xml.dom
        _impl = xml.dom.getDOMImplementation()
        _doc = _impl.createDocument(None, Modification.TAGNAME, None)
        _element = _doc.documentElement
        _element.setAttribute("type", "sysrq")
        if kwds.has_key("command"):
            _element.appendChild(
                Sysrq._createElement(kwds.get("command"),
                                     kwds.get("settrigger")))
        elif kwds.has_key("commands"):
            _element.appendChild(
                Sysrq._createElement(kwds.get("commands"),
                                     kwds.get("settrigger")))
        return (_element, _doc)

    _createElement = staticmethod(_createElement)

    def __init__(self, *params, **kwds):
        """
        Constructors:
        __init__(element, doc)
        __init__(self, command=cmd, [settrigger=*True|False])
        __init__(self, commands=list<cmd>, [settrigger=*True|False])
        __init__(element=element, doc=doc)

        @element: the element of the modification
        @doc: the root document
        @command: sysrq command to be executed
        @commands: list of sysrq commands to be executed
        @settrigger: set the trigger or not
        """
        if len(params) == 2:
            _element = params[0]
            _doc = params[0]
        elif kwds:
            if kwds.has_key("element") and kwds.has_key("doc"):
                _element = kwds["element"]
                _doc = kwds["doc"]
            else:
                (_element, _doc) = self._createElement(**kwds)
        else:
            raise TypeError(
                "__init__() takes exactly 2 arguments or different keys. (%u given)"
                % len(params))
        Modification.__init__(self, _element, _doc)
        self.sysrqs = list()
        for _sysrq in self.getElement().getElementsByTagName(Sysrq.TAGNAME):
            self.sysrqs.append(Sysrq(_sysrq, _doc))

    def doRealModifications(self):
        """
        calls doCatifAction for every action
        """
        for _sysrq in self.sysrqs:
            _sysrq.doCommands()
# here is some internal information
# $Id: ComSCSI.py,v 1.4 2010-03-08 12:30:48 marc Exp $
#

__version__ = "$Revision: 1.4 $"
# $Source: /atix/ATIX/CVSROOT/nashead2004/management/comoonics-clustersuite/python/lib/comoonics/scsi/ComSCSI.py,v $

from comoonics import ComLog
from comoonics.ComExceptions import ComException

import os.path
import re
from comoonics import ComSystem

log = ComLog.getLogger("comoonics.scsi.ComSCSI")


class SCSIException(ComException):
    def __str__(self):
        return "SCSI-Error: %s" % (self.value)


FCPATH_2_HOST = "/sys/class/fc_host"
FCPATH_TRANSPORT = "/sys/class/fc_transport"
FCPATH_TRANSPORT_TARGET = FCPATH_TRANSPORT + "/target:%u:%u:%u"
FCPATH_TRANSPORT_TARGET_NODENAME = FCPATH_TRANSPORT_TARGET + "/node_name"
FCPATH_HOST_DEVICE = FCPATH_2_HOST + "/%s/device"
FCPATH_HOST_TARGET = FCPATH_2_HOST + "/target:%u:%u:%u"
FCPATH_HOST_LUN = FCPATH_HOST_TARGET + "/%u:%u:%u:%u"
SCSIPATH_2_PROC = "/proc/scsi"
class HostDisk(Disk):
    log=ComLog.getLogger("comoonics.ComDisk.HostDisk")
    LABEL_PREFIX="LABEL="
    TAGNAME="disk"
    class DeviceNameResolver(object):
        key=None
        def getKey(self):
            return self.key
        def resolve(self, value):
            pass

    class DeviceNameResolverError(ComException):
        pass
    resolver=dict()

    def __init__(self, *params):
        """ Disk represents a raw disk
            Possible constructors:
            __init__(self, element, doc=None)  type(element)=Node
            __init__(self, name, doc=None) type(name)==str
        """
        if len(params)==1:
            doc=xml.dom.getDOMImplementation().createDocument(None, self.TAGNAME, None)
        elif len(params)==2:
            doc=params[1]
        else:
            raise exceptions.TypeError("""Wrong number of arguments to constructor of HostDisk.
   Either __init__(self, element, doc=None) or
          __init__(self, name, doc=None) are supported.""")

        if isinstance(params[0], basestring):
            element=doc.createElement(self.TAGNAME)
            element.setAttribute("name", params[0])
        else:
            element=params[0]
        super(HostDisk, self).__init__(element, doc)
        self.log.debug("__init__")
        self.devicename=None
        self.stabilizedfile="/proc/partitions"
        self.stabilizedtype="hash"
        self.stabilizedgood=2

    def is_lvm(self):
        return hasattr(self, "volumegroup") and hasattr(self, "logicalvolume")

    def is_lvm_activated(self):
        return self.is_lvm() and self.logicalvolume.isActivated()

    def lvm_vg_activate(self):
        self.volumegroup.activate()

    def lvm_vg_deactivate(self):
        self.volumegroup.deactivate()

    def getLog(self):
        return self.log

    def exists(self):
        return ComSystem.execMethod(os.path.exists, self.getDeviceName())

    def sameSize(self):
        """ 
        """
        return self.getAttributeBoolean("samesize", False)

    def getDeviceName(self):
        """ returns the Disks device name (e.g. /dev/sda) """
        if hasattr(self, "devicename") and self.devicename != None:
            return self.devicename
        else:
            return self.getAttribute("name")

    def getDevicePath(self):
        return self.getDeviceName()

    def getSize(self):
        """ returns the size of the disk in sectors"""
        pass

    def refByLabel(self):
        """
        is disk referenced by Label? returns True or False
        Format <disk name="LABEL=..."/>
        """
        return self.getDeviceName()[:len(self.LABEL_PREFIX)]==self.LABEL_PREFIX

    def resolveDeviceNameByKeyValue(self, key, value):
        """
        resolves a device name by searching for a method to resolve it by key and the resolve via value as
        Parameter
        """
        if self.resolver.has_key(key):
            name=self.resolver[key].resolve(value)
            if name and name != "":
                self.devicename=name
            else:
                raise HostDisk.DeviceNameResolverError("Could not resolve device \"%s\" for type \"%s\"" %(value, key))
        else:
            raise HostDisk.DeviceNameResolverError("Could not find resolver for device referrenced by %s=%s" %(key, value))

    def resolveDeviceName(self):
        """
        resolves the given devicenames and returns a list of executed commands.
        """
        journal_cmds=list()
        if len(self.getDeviceName().split("="))==2:
            (key, value)=self.getDeviceName().split("=")[:2]
            self.resolveDeviceNameByKeyValue(key, value)
        if LogicalVolume.isValidLVPath(self.getDeviceName()):
            self.initLVM()
            self.lvm_activated=False
            if self.getAttribute("options", "") != "skipactivate" and not self.is_lvm_activated():
                self.lvm_vg_activate()
                self.lvm_activated=True
                journal_cmds.append("lvm_vg_activate")
        return journal_cmds

    def initLVM(self):
        (vgname, lvname)=LogicalVolume.splitLVPath(self.getDeviceName())
        self.volumegroup=VolumeGroup(vgname, self.getDocument())
        self.logicalvolume=LogicalVolume(lvname, self.volumegroup, self.getDocument())
        self.volumegroup.init_from_disk()
        self.logicalvolume.init_from_disk()

    def initFromDisk(self):
        """ reads partition information from the disk and fills up DOM
        with new information
        """
        HostDisk.log.debug("initFromDisk()")

        #FIXME: create LabelResolver
        if self.refByLabel():
            pass
        if not self.exists():
            raise ComException("Device %s not found or no valid device!" % self.getDeviceName())
        try:
            self.initFromDiskParted()
        except ImportError:
            self.initFromDiskPartedCmd()
            
    def initFromDiskPartedCmd(self):
        raise ImportError("No pyparted found. You might want to install pyparted.")       

    def initFromDiskParted(self):
        import parted
        import ComParted

        phelper=ComParted.PartedHelper()
        dev=parted.PedDevice.get(self.getDeviceName())
        try:
            disk=parted.PedDisk.new(dev)
            partlist=phelper.get_primary_partitions(disk)
            for part in partlist:
                self.appendChild(Partition(part, self.getDocument()))
        except parted.error:
            HostDisk.log.debug("no partitions found")

    def restore(self):
        if hasattr(self, "lvm_activated") and self.lvm_activated:
            self.lvm_vg_deactivate()

    def createPartitions(self):
        """ creates new partition table """
        try:
            self.createPartitionsParted()
        except ImportError:
            self.createPartitionsPartedCmd()

    def createPartitionsPartedCmd(self):
        pass

    def createPartitionsParted(self):
        import parted
        import ComParted
        if not self.exists():
            raise ComException("Device %s not found" % self.getDeviceName())

        phelper=ComParted.PartedHelper()
        #IDEA compare the partition configurations for update
        #1. delete all aprtitions
        dev=parted.PedDevice.get(self.getDeviceName())

        try:
            disk=parted.PedDisk.new(dev)
            disk.delete_all()
        except parted.error:
            #FIXME use generic disk types
            disk=dev.disk_new_fresh(parted.disk_type_get("msdos"))

        # create new partitions
        for com_part in self.getAllPartitions():
            type=com_part.getPartedType()
            if self.sameSize():
                size=com_part.getPartedSize(dev)
            else:
                size=com_part.getPartedSizeOptimum(dev)
            flags=com_part.getPartedFlags()
            self.log.debug("creating partition: size: %i" % size )
            phelper.add_partition(disk, type, size, flags)

        disk.commit()
        self.commit()

        #dev.sync()
        #dev.close()

        # run partx if the device is a multipath device
        self.log.debug("ComHostDisk: checking for multipath devices")
        if self.isDMMultipath():
            self.log.debug("Device %s is a dm_multipath device, adding partitions" %self.getDeviceName())
            __cmd=CMD_KPARTX + " -d " + self.getDeviceName()
            try:
                __ret = ComSystem.execLocalOutput(__cmd, True, "")
                self.log.debug(__ret)
                __cmd=CMD_KPARTX + " -a " + self.getDeviceName()
                __ret = ComSystem.execLocalOutput(__cmd, True, "")
                self.log.debug(__ret)
                #FIXME: crappy fix to give os some time to create devicefiles.
                time.sleep(10)
            except ComSystem.ExecLocalException, ele:
                ComLog.debugTraceLog(self.log)
                self.log.debug("Could not execute %s. Error %s" %(ele.cmd, ele))
class Source(BaseDB):
    """
    Source Class
    """
    log=ComLog.getLogger("Source")

    def __init__(self, **kwds):
        """
        Creates a connection to the database
        __init__(hostname=.., user=.., password=.., database=.., tablename=..)
        __init__(dbhandle=.., tablename=..)
        """
        if not kwds.has_key("tablename"):
            kwds["tablename"]="sources"
        super(Source, self).__init__(**kwds)

    def getSourcesAsSysteminformations(self, name=None):
        sysinfos=list()
        # to be compatible with mysql-python rhel4 and mysql4.1 dateformat it has to be formated
        selectquery="SELECT source_type AS type, name, category, architecture, operating_system AS operatingsystem, "+\
                    "kernel_version AS kernelversion, uptime, DATE_FORMAT(lastimport, '%Y%m%d%H%i%s') AS lastimport FROM sources"
        if name and name != "":
            selectquery+=" WHERE name LIKE \"%"+name+"%\";"
        Source.log.debug("getSourcesAsSysteminformations: %s" %(selectquery))
        rs=self.selectQuery(selectquery)
        row=rs.fetch_row(1,1)
        while row:
            from comoonics.ComSystemInformation import SystemInformation
            sysinfos.append(SystemInformation(**row[0]))
            row=rs.fetch_row(1,1)
        return sysinfos

    def getSourceAsSysteminformation(self, name):
        selectquery="SELECT source_type AS type, name, category, architecture, operating_system AS operatingsystem, kernel_version AS kernelversion, uptime, lastimport FROM sources WHERE name=\""+name+"\";"
        rs=self.selectQuery(selectquery)
        if rs.num_rows > 0:
            row=rs.fetch_row(1,1)
            from comoonics.ComSystemInformation import SystemInformation
            systeminformation=SystemInformation(**row[0])
        else:
            return None

    def getCategories(self):
        """
        Returns the first found channel channelversion combination from the dsl of the given rpm
        rpm: is a rpm_header as defined by python-rpm
        """
        selectquery="SELECT DISTINCT category FROM %s;" %(self.tablename)
        self.log.debug("select %s" % selectquery)
        rs=self.selectQuery(selectquery)
        categories=list()
        row=rs.fetch_row()
        while row:
            categories.append(row[0][0])
            row=rs.fetch_row()
        return categories

    def getSourcesForCategory(self, category):
        """
        Returns the category for the given sourcename
        """
        selectquery="SELECT DISTINCT name FROM %s WHERE category=\"%s\";" %(self.tablename, category)
        rs=self.selectQuery(selectquery)
        sources=list()
        row=rs.fetch_row()
        while row:
            sources.append(row[0][0])
            row=rs.fetch_row()
        self.log.debug("select %s %u, %u" %(selectquery, rs.num_rows(), len(sources)))
        return sources

    def updateSystemInformation(self, sysinfo, category):
        values=dict()
        if isinstance(sysinfo, RedhatClusterSystemInformation):
            source_type="cluster"
        else:
            source_type="single"
#        values["uptime"]=sysinfo.getUptime()
#        values["source_type"]=source_type
#        values["category"]=category
#        values["architecture"]=sysinfo.getArchitecture()
#        values["operating_system"]=sysinfo.getOperatingsystem()
#        values["kernel_version"]=sysinfo.getKernelversion()
        ret=super(Source, self).updateRPM("INSERT INTO %s VALUES(\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", NOW());" % (self.tablename, source_type, sysinfo.getName(), category, sysinfo.getArchitecture(), sysinfo.getOperatingsystem(), sysinfo.getKernelversion(), sysinfo.getUptime()),
                  "UPDATE %s SET source_type=\"%s\", category=\"%s\", architecture=\"%s\", operating_system=\"%s\", kernel_version=\"%s\", uptime=\"%s\", lastimport=Now() WHERE name=\"%s\";" % (self.tablename, source_type, category, sysinfo.getArchitecture(), sysinfo.getOperatingsystem(), sysinfo.getKernelversion(), sysinfo.getUptime(), sysinfo.getName()),
                  "SELECT name, source_type, category, architecture, operating_system, kernel_version, uptime FROM %s WHERE name=\"%s\";" %(self.tablename, sysinfo.getName()), None)
        if ret==1:
            self.dblog.log(DBLogger.DB_LOG_LEVEL, "Added new system %s, %s (table: %s)" %(sysinfo.getName(), category, self.tablename))
        elif ret>1:
            self.dblog.log(DBLogger.DB_LOG_LEVEL, "Updated existing system %s, %s (table: %s)" %(sysinfo.getName(), category, self.tablename))
Esempio n. 47
0
All files created by lockfile will be read-only, and therefore will have to be removed with rm -f.

If you specify a locktimeout then a lockfile will be removed by force after locktimeout seconds have passed since the lockfile was last modified/created (most
likely  by some other program that unexpectedly died a long time ago, and hence could not clean up any leftover lockfiles). Lockfile is clock skew immune. After
a lockfile has been removed by force, a suspension of suspend seconds (defaults to 16) is taken into account, in order to prevent the inadvertent immediate
removal of any newly created lockfile by another program (compare SUSPEND in procmail(1)).

Mailbox locks
   If the permissions on the system mail spool directory allow it, or if lockfile is suitably setgid, it will be able to lock and unlock your system mailbox by using
   the options -ml and -mu respectively
"""

from comoonics import ComLog, ComSystem, ComExceptions

logger = ComLog.getLogger("comoonics.lockfile")


class lockfileTimeout(ComExceptions.ComException):
    def __str__(self):
        return "Sorry, giving up on \"%s\"\nAnother process seems to have locked this file. You may try again later." % self.value


class lockfile(object):
    COMMAND = "lockfile"
    """
    The lockfile class
    """
    def __init__(self, _filename):
        self.filename = _filename
Esempio n. 48
0
class TimeExpression(object):
    logger=ComLog.getLogger("TimeExpression")
    months_map={"Jan.*":1, "Feb.*":2, "Mar.*":3, "Apr.*":4, "May":5, "Jun.*":6, "Jul.*":7, "Aug.*":8, "Sep.*":9, "Oct.*":10, "Nov.*":11, "Dec.*":12}
    months=("Jan\w*", "Feb\w*", "Mar\w*", "Apr\w*", "Mai", "Jun\w*", "Jul\w*", "Aug\w*", "Sep\w*", "Oct\w*", "Nov\w*", "Dec\w*")
    def __init__(self, time_expression):
        super(TimeExpression, self).__init__()
        #self.timezone=".*"
        #time_expression=TimeExpression.toRegExp(time_expression)
        if time_expression=="now":
            import time
            time_expression=time.strftime("%Y/%m/%d %H:%M:*")
        (date, time)=split(time_expression, " ", 2)
        (self.year, self.month, self.day)=split(date, "/", 3)
        (self.hour, self.minute, self.second)=split(time, ":", 3)
    def __str__(self):
        return "%s/%s/%s %s:%s:%s" %(self.year, self.month, self.day, self.hour, self.minute, self.second)
    def format(self, _format):
        time_format=TimeFormat(_format)
        TimeExpression.logger.debug("time_format: %s" %(time_format))
        return time_format.format(self)
    def now():
        return TimeExpression("now")
    def toRegExp(string, format="[A-Za-z0-9]"):
        regexps=( ("(\d{1,4})-(\d{1,4})", "[\g<1>-\g<2>]"),
                  ("(\B|[^\.])\*", "\g<1>%s" %(format)),
                  ("(\B|[^\.])\+","\g<1>%s" %(format) ))
        import re
        for (regexp,subs) in regexps:
            string=re.sub(regexp, subs, string)
        return string

    def toHourRegExp(string):
        return "(?P<hour>"+TimeExpression.toRegExp(string,"\d{1,2}")+")"
    def toMinuteRegExp(string):
        return "(?P<minute>"+TimeExpression.toRegExp(string,"\d{1,2}")+")"
    def toSecondRegExp(string):
        return "(?P<second>"+TimeExpression.toRegExp(string,"\d{1,2}")+")"
    def toYearRegExp(string):
        return "(?P<year>"+TimeExpression.toRegExp(string,"\d{2,4}")+")"
    def toMonthnumberRegExp(string):
        string=TimeExpression.toRegExp(string, "1-12")
        import re
        regexp=re.compile("%s" %("|".join(TimeExpression.months)))
        return "(?P<month>["+regexp.sub(TimeExpression.toMonthnumber, string)+"])"
    def toMonthnameRegExp(string):
        
        #ComLog.getLogger().debug("toMonthnameRegExp(%s)" %(str))
        import re
        # resolve the [\d+-\d+] expressions to name1|name2|name3..
        regexp=re.compile("(\d{1,2})-(\d{1,2})")
        _match=regexp.match(string)
        if _match:
            low=int(_match.group(1))
            if low > 0:
                low=low-1
            high=int(_match.group(2))
#            print "%s, %s" %(_match.group(1), _match.group(2))
            string="|".join(TimeExpression.months[low:high])
        elif re.match("^\.\*", string) or re.match("^\*", string):
            string="|".join(TimeExpression.months)
        regexp=re.compile("\d{1,2}")
        return "(?P<month>"+regexp.sub(TimeExpression.toMonthname, string)+")"
    def toMonthDayRegExp(string):
        return "(?P<day>"+TimeExpression.toRegExp(string,"\d{1,2}")+")"

    def toMonthname(number, isregexp=True):
        if isregexp:
            _number=int(number.group(0))
        else:
            _number=int(number)
        #print "%u=>%s" %(_number, TimeExpression.months[_number-1])
        return TimeExpression.months[_number-1]

    def toMonthnumber(name, isregexp=True):
        import re
        if isregexp:
            _name=name.group(0)
        else:
            _name=name
        for key in TimeExpression.months_map.keys():
            if re.match(key, _name):
                #print "%s=>%u" %(_name, int(TimeExpression.months_map[key]))
                return str(TimeExpression.months_map[key])
        return None

    now=staticmethod(now)
    toRegExp=staticmethod(toRegExp)
    toMonthname=staticmethod(toMonthname)
    toMonthnumber=staticmethod(toMonthnumber)
    toYearRegExp=staticmethod(toYearRegExp)
    toMonthnameRegExp=staticmethod(toMonthnameRegExp)
    toMonthnumberRegExp=staticmethod(toMonthnumberRegExp)
    toMonthDayRegExp=staticmethod(toMonthDayRegExp)
    toHourRegExp=staticmethod(toHourRegExp)
    toMinuteRegExp=staticmethod(toMinuteRegExp)
    toSecondRegExp=staticmethod(toSecondRegExp)
Esempio n. 49
0
class DateFormat(TimeFormat):
    logger=ComLog.getLogger("comoonics.search.datatime.DateFormat")
    format_string=""
    _formated=False
    def __init__(self, _searchfor=None, _formatstring="%d/%b/%Y:%h:%M:%S"):
        super(DateFormat, self).__init__(_searchfor, _formatstring)

    #def clear(self):
    #    super(DateFormat, self).clear()
    #    self._formated=False

    def format(self, time_expression=None):
        #DateFormat.logger.debug("format: formated: %s" %self._formated)
        if not self._formated:
            if not time_expression:
                time_expression=TimeExpression(self.searchfor)
            if isinstance(time_expression, basestring):
                time_expression=TimeExpression(time_expression)
            DateFormat.logger.debug("format: time_expression: %s" %time_expression)
            regexps=list()
            if self.format_string.find("%Y")>=0:
                regexps.append(("%Y", "%s" %(TimeExpression.toYearRegExp(time_expression.year))))
            if self.format_string.find("%m")>=0:
                regexps.append(("%m", "%s" %(TimeExpression.toMonthnumberRegExp(time_expression.month))))
            if self.format_string.find("%d")>=0:
                regexps.append(("%d", "%s" %(TimeExpression.toMonthDayRegExp(time_expression.day))))
            if self.format_string.find("%h")>=0:
                regexps.append(("%h", "%s" %(TimeExpression.toHourRegExp(time_expression.hour))))
            if self.format_string.find("%M")>=0:
                regexps.append(("%M", "%s" %(TimeExpression.toMinuteRegExp(time_expression.minute))))
            if self.format_string.find("%S")>=0:
                regexps.append(("%S", "%s" %(TimeExpression.toSecondRegExp(time_expression.second))))
            if self.format_string.find("%b")>=0:
                regexps.append(("%b", "%s" %(TimeExpression.toMonthnameRegExp(time_expression.month))))
            import re
            _format=self.format_string
            for (regexp,subs) in regexps:
                _format=re.sub(regexp, subs, _format)
            self._format=_format
            self._formated=True
        return self._format

    def getYear(self, default=""):
        return self._getMatchValue("year", default)

    def getMonth(self, default=""):
        return self._getMatchValue("month", default)

    def getDay(self, default=""):
        return self._getMatchValue("day", default)

    def getHour(self, default=""):
        return self._getMatchValue("hour", default)

    def getMinute(self, default=""):
        return self._getMatchValue("minute", default)

    def getSecond(self, default=""):
        return self._getMatchValue("second", default)

    def timeHash(self):
        #if self.getMatch():
        #    DateFormat.logger.debug("%s/%s/%s:%s:%s:%s, match: %s" %(int(self.getYear("0")),\
        #                                                             TimeExpression.toMonthnumber(self.getMonth("Jan"), False), int(self.getDay("0")),\
        #                                                             int(self.getHour("0")),int(self.getMinute("0")), int(self.getSecond("0")), self.getMatch()))
        return "%04u%02u%02u%02u%02u%02u" %(int(self.getYear("0")),\
            int(TimeExpression.toMonthnumber(self.getMonth("Jan"), False)), int(self.getDay("0")),\
            int(self.getHour("0")),int(self.getMinute("0")), int(self.getSecond("0")))

    def _getMatchValue(self, _key, _default=""):
        _value=_default
        if self.getMatch():
            try:
                _value=self.getMatch().group(_key)
            except IndexError:
                pass
        return _value
# here is some internal information
# $Id: ComUtils.py,v 1.1 2011-02-15 14:51:50 marc Exp $
#

__version__ = "$Revision: 1.1 $"
# $Source: /atix/ATIX/CVSROOT/nashead2004/management/comoonics-clustersuite/python/lib/comoonics/ecbase/ComUtils.py,v $

from exceptions import *

import re
from comoonics import ComLog
from comoonics import ComSystem
from comoonics.ComExceptions import ComException

log = ComLog.getLogger("ComUtils")

CMD_SFDISK = "/sbin/sfdisk"


def isInt(str):
    """ Is the given string an integer?     """
    ok = 1
    try:
        num = int(str)
        #log.debug(str + " is of int")
    except ValueError:
        #log.debug(str + " is not of int")
        ok = 0
    return ok