Example #1
0
    def __exec(self, cmd, rrdFile=None):
        """
        Executes a system command.

        :type cmd: string
        :param cmd: The cmd command to be executed.
        :type rrdFile: string
        :param rrdFile: name of the rrd file.
        :return: The value dictionary / S_ERROR with a message.
        """
        self.log.debug("RRD command: %s" % cmd)
        retVal = Subprocess.shellCall(0, cmd)
        if self.__logRRDCommands and rrdFile:
            try:
                logFile = "%s.log" % rrdFile
                with open(logFile, "a") as fd:
                    if not retVal["OK"] or retVal["Value"][0]:
                        fd.write("ERROR %s\n" % cmd)
                    else:
                        fd.write("OK    %s\n" % cmd)
            except Exception as e:
                self.log.warn("Cannot write log %s: %s" % (logFile, str(e)))
        if not retVal["OK"]:
            return retVal
        retTuple = retVal["Value"]
        if retTuple[0]:
            return S_ERROR("Failed to execute rrdtool: %s" % (retTuple[2]))
        return retVal
Example #2
0
def executeRabbitmqctl(arg, *argv):
  """Executes RabbitMQ administration command.
    It uses rabbitmqctl command line interface.
    For every command the -q argument ("quit mode")
    is used, since in some cases the output must be processed,
    so we don't want any additional informations printed.
  Args:
    arg(str): command recognized by the rabbitmqctl.
    argv(list): optional list of string parameters.
  Returns:
    S_OK:
    S_ERROR:

  """
  command =['sudo','/usr/sbin/rabbitmqctl','-q', arg] + list(argv)
  timeOut = 30
  result = Subprocess.systemCall(timeout = timeOut, cmdSeq = command)
  if result['OK']:
    errorcode, cmd_out, cmd_err = result['Value']
  else:
    return S_ERROR(errno.EPERM, "%r failed, status code: %s stdout: %r stderr: %r" %
                                (command, errorcode, cmd_out, cmd_err) )
  if errorcode:
    # No idea what errno code should be used here.
    # Maybe we should define some specific for rabbitmqctl
    return S_ERROR(errno.EPERM, "%r failed, status code: %s stdout: %r stderr: %r" %
                                (command, errorcode, cmd_out, cmd_err) )
  return S_OK(cmd_out)
Example #3
0
 def getLFCRegisteredDNs(self):
     #Request a proxy
     if gConfig.useServerCertificate():
         if not self.__generateProxy():
             return False
     #Execute the call
     cmdEnv = dict(os.environ)
     cmdEnv['LFC_HOST'] = 'lfc-egee.in2p3.fr'
     if os.path.isfile(self.proxyLocation):
         cmdEnv['X509_USER_PROXY'] = self.proxyLocation
     lfcDNs = []
     try:
         retlfc = Subprocess.systemCall(30, ('lfc-listusrmap', ),
                                        env=cmdEnv)
         if not retlfc['OK']:
             self.log.fatal('Can not get LFC User List', retlfc['Message'])
             return retlfc
         if retlfc['Value'][0]:
             self.log.fatal('Can not get LFC User List', retlfc['Value'][2])
             return S_ERROR("lfc-listusrmap failed")
         else:
             for item in List.fromChar(retlfc['Value'][1], '\n'):
                 dn = item.split(' ', 1)[1]
                 lfcDNs.append(dn)
         return S_OK(lfcDNs)
     finally:
         if os.path.isfile(self.proxyLocation):
             self.log.info("Destroying proxy...")
             os.unlink(self.proxyLocation)
Example #4
0
def executeRabbitmqctl(arg, *argv):
    """Executes RabbitMQ administration command.
      It uses rabbitmqctl command line interface.
      For every command the -q argument ("quit mode")
      is used, since in some cases the output must be processed,
      so we don't want any additional informations printed.

    Args:
      arg(str): command recognized by the rabbitmqctl.
      argv: optional list of string parameters.

    :rtype: S_OK or S_ERROR
    :type argv: python:list

    """
    command = ["sudo", "/usr/sbin/rabbitmqctl", "-q", arg] + list(argv)
    timeOut = 30
    result = Subprocess.systemCall(timeout=timeOut, cmdSeq=command)
    if not result["OK"]:
        return S_ERROR(errno.EPERM, "%r failed to launch" % command)
    errorcode, cmd_out, cmd_err = result["Value"]
    if errorcode:
        # No idea what errno code should be used here.
        # Maybe we should define some specific for rabbitmqctl
        return S_ERROR(
            errno.EPERM, "%r failed, status code: %s stdout: %r stderr: %r" % (command, errorcode, cmd_out, cmd_err)
        )
    return S_OK(cmd_out)
Example #5
0
  def __checkoutFromSVN(self, moduleName=None, sourceURL=None, tagVersion=None):
    """
    This method checkout a given tag from a SVN repository.
    Note: we can checkout any project form a SVN repository

    :param str moduleName: The name of the Module
    :param str sourceURL: The code repository
    :param str tagVersion: the tag for example: v4r3p6

    """

    if not moduleName:
      moduleName = self.params.name

    if not sourceURL:
      sourceURL = self.params.sourceURL

    if not tagVersion:
      tagVersion = self.params.version

    cmd = "svn export --trust-server-cert --non-interactive '%s/%s' '%s'" % (sourceURL, tagVersion,
                                                                             os.path.join(self.params.destination,
                                                                                          moduleName))
    gLogger.verbose("Executing: %s" % cmd)
    result = Subprocess.systemCall(900, shlex.split(cmd))
    if not result['OK']:
      return S_ERROR("Error while retrieving sources from SVN: %s" % result['Message'])
    exitStatus, stdData, errData = result['Value']
    if exitStatus:
      return S_ERROR("Error while retrieving sources from SVN: %s" % "\n".join([stdData, errData]))
    return S_OK()
Example #6
0
def executeRabbitmqctl(arg, *argv):
    """Executes RabbitMQ administration command.
    It uses rabbitmqctl command line interface.
    For every command the -q argument ("quit mode")
    is used, since in some cases the output must be processed,
    so we don't want any additional informations printed.
  Args:
    arg(str): command recognized by the rabbitmqctl.
    argv(list): optional list of string parameters.
  Returns:
    S_OK:
    S_ERROR:

  """
    command = ['sudo', '/usr/sbin/rabbitmqctl', '-q', arg] + list(argv)
    timeOut = 30
    result = Subprocess.systemCall(timeout=timeOut, cmdSeq=command)
    if result['OK']:
        errorcode, cmd_out, cmd_err = result['Value']
    else:
        return S_ERROR(
            errno.EPERM, "%r failed, status code: %s stdout: %r stderr: %r" %
            (command, errorcode, cmd_out, cmd_err))
    if errorcode:
        # No idea what errno code should be used here.
        # Maybe we should define some specific for rabbitmqctl
        return S_ERROR(
            errno.EPERM, "%r failed, status code: %s stdout: %r stderr: %r" %
            (command, errorcode, cmd_out, cmd_err))
    return S_OK(cmd_out)
 def __checkoutFromSVN( self, moduleName = None, sourceURL = None, tagVersion = None ):
   """
   This method checkout a given tag from a SVN repository. 
   Note: we can checkout any project form a SVN repository 
   
   :param str moduleName: The name of the Module
   :param str sourceURL: The code repository
   :param str tagVersion: the tag for example: v4r3p6
   
   """
   
   if not moduleName:
     moduleName = self.params.name
   
   if not sourceURL:
     sourceURL = self.params.sourceURL   
     
   if not tagVersion:
     tagVersion = self.params.version
     
   cmd = "svn export --trust-server-cert --non-interactive '%s/%s' '%s'" % ( sourceURL, tagVersion,
                                                                             os.path.join( self.params.destination, moduleName ) )
   gLogger.verbose( "Executing: %s" % cmd )
   result = Subprocess.systemCall( 900, shlex.split(cmd) )
   if not result[ 'OK' ]:
     return S_ERROR( "Error while retrieving sources from SVN: %s" % result[ 'Message' ] )
   exitStatus, stdData, errData = result[ 'Value' ]
   if exitStatus:
     return S_ERROR( "Error while retrieving sources from SVN: %s" % "\n".join( [ stdData, errData ] ) )
   return S_OK()
Example #8
0
 def getLFCRegisteredDNs( self ):
   #Request a proxy
   if gConfig.useServerCertificate():
     if not self.__generateProxy():
       return False
   #Execute the call
   cmdEnv = dict( os.environ )
   cmdEnv['LFC_HOST'] = 'lfc-egee.in2p3.fr'
   if os.path.isfile( self.proxyLocation ):
     cmdEnv[ 'X509_USER_PROXY' ] = self.proxyLocation
   lfcDNs = []
   try:
     retlfc = Subprocess.systemCall( 30, ( 'lfc-listusrmap', ), env = cmdEnv )
     if not retlfc['OK']:
       self.log.fatal( 'Can not get LFC User List', retlfc['Message'] )
       return retlfc
     if retlfc['Value'][0]:
       self.log.fatal( 'Can not get LFC User List', retlfc['Value'][2] )
       return S_ERROR( "lfc-listusrmap failed" )
     else:
       for item in List.fromChar( retlfc['Value'][1], '\n' ):
         dn = item.split( ' ', 1 )[1]
         lfcDNs.append( dn )
     return S_OK( lfcDNs )
   finally:
     if os.path.isfile( self.proxyLocation ):
       self.log.info( "Destroying proxy..." )
       os.unlink( self.proxyLocation )
Example #9
0
 def __checkoutFromCVS( self ):
   cmd = "cvs export -d '%s' '%s'" % ( self.params.sourceURL, os.path.join( self.params.destination, self.params.name ) )
   gLogger.verbose( "Executing: %s" % cmd )
   result = Subprocess.shellCall( 900, cmd )
   if not result[ 'OK' ]:
     return S_ERROR( "Error while retrieving sources from CVS: %s" % result[ 'Message' ] )
   exitStatus, stdData, errData = result[ 'Value' ]
   if exitStatus:
     return S_ERROR( "Error while retrieving sources from CVS: %s" % "\n".join( [ stdData, errData ] ) )
   return S_OK()
Example #10
0
 def __checkoutFromSVN( self ):
   cmd = "svn export --trust-server-cert --non-interactive '%s/%s' '%s'" % ( self.params.sourceURL, self.params.version,
                                                                             os.path.join( self.params.destination, self.params.name ) )
   gLogger.verbose( "Executing: %s" % cmd )
   result = Subprocess.shellCall( 900, cmd )
   if not result[ 'OK' ]:
     return S_ERROR( "Error while retrieving sources from SVN: %s" % result[ 'Message' ] )
   exitStatus, stdData, errData = result[ 'Value' ]
   if exitStatus:
     return S_ERROR( "Error while retrieving sources from SVN: %s" % "\n".join( [ stdData, errData ] ) )
   return S_OK()
Example #11
0
 def __getLastUpdateTime( self, rrdFile ):
   """
   Get last update time from an rrd
   """
   cmd = "%s last %s" % ( self.rrdExec, rrdFile )
   retVal = Subprocess.shellCall( 0, cmd )
   if not retVal[ 'OK' ]:
     return retVal
   retTuple = retVal[ 'Value' ]
   if retTuple[0]:
     return S_ERROR( "Failed to fetch last update %s : %s" % ( rrdFile, retTuple[2] ) )
   return S_OK( int( retTuple[1].strip() ) )
Example #12
0
 def __getLastUpdateTime(self, rrdFile):
     """
 Get last update time from an rrd
 """
     cmd = "%s last %s" % (self.rrdExec, rrdFile)
     retVal = Subprocess.shellCall(0, cmd)
     if not retVal['OK']:
         return retVal
     retTuple = retVal['Value']
     if retTuple[0]:
         return S_ERROR("Failed to fetch last update %s : %s" %
                        (rrdFile, retTuple[2]))
     return S_OK(int(retTuple[1].strip()))
Example #13
0
 def __exec(self, cmd, rrdFile=None):
     """
 Execute a system command
 """
     self.log.debug("RRD command: %s" % cmd)
     retVal = Subprocess.shellCall(0, cmd)
     if self.__logRRDCommands and rrdFile:
         try:
             logFile = "%s.log" % rrdFile
             fd = file(logFile, "a")
             if not retVal['OK'] or retVal['Value'][0]:
                 fd.write("ERROR %s\n" % cmd)
             else:
                 fd.write("OK    %s\n" % cmd)
             fd.close()
         except Exception, e:
             self.log.warn("Cannot write log %s: %s" % (logFile, str(e)))
Example #14
0
    def __getLastUpdateTime(self, rrdFile):
        """
        Gets last update time from an rrd.

        :type rrdFile: string
        :param rrdFile: name of the rrd file.
        :return: S_OK / S_ERROR with a message.
        """
        cmd = "%s last %s" % (self.rrdExec, rrdFile)
        retVal = Subprocess.shellCall(0, cmd)
        if not retVal["OK"]:
            return retVal
        retTuple = retVal["Value"]
        if retTuple[0]:
            return S_ERROR("Failed to fetch last update %s : %s" %
                           (rrdFile, retTuple[2]))
        return S_OK(int(retTuple[1].strip()))
Example #15
0
 def __exec( self, cmd, rrdFile = None ):
   """
   Execute a system command
   """
   self.log.debug( "RRD command: %s" % cmd )
   retVal = Subprocess.shellCall( 0, cmd )
   if self.__logRRDCommands and rrdFile:
     try:
       logFile = "%s.log" % rrdFile
       fd = file( logFile, "a" )
       if not retVal[ 'OK' ] or retVal[ 'Value' ][0]:
         fd.write( "ERROR %s\n" % cmd )
       else:
         fd.write( "OK    %s\n" % cmd )
       fd.close()
     except Exception, e:
       self.log.warn( "Cannot write log %s: %s" % ( logFile, str( e ) ) )
Example #16
0
 keyList = list()
 from DIRAC.Core.Utilities import Subprocess
 for i in descriptionList:
     key = dict()
     name = i["name"]
     p12 = i["p12"]
     key["pem"] = i["pem"]
     for j in "pub", "private":
         tmp = "".join(random.choice(string.letters) for x in range(10))
         key[j] = os.path.join(storePath, tmp)
     cmdCert = "openssl pkcs12 -clcerts -nokeys -in %s -out %s -password file:%s" % (
         name, key["pub"], p12)
     cmdKey = "openssl pkcs12 -nocerts -in %s -out %s -passout file:%s -password file:%s" % (
         name, key["private"], key["pem"], p12)
     for cmd in cmdCert, cmdKey:
         result = Subprocess.shellCall(900, cmd)
         gLogger.debug("Command is: %s" % cmd)
         gLogger.debug("Result is: %s" % result)
         if not result["OK"]:
             shutil.rmtree(storePath)
             gLogger.error(result["Message"])
             error = "Error while executing SSL command: %s" % result[
                 "Message"]
             error = error + disclaimer
             gLogger.debug("Service response: %s" % error)
             return {"success": "false", "error": error}
     keyList.append(key)
 if not len(keyList) > 0:
     shutil.rmtree(storePath)
     error = "List of public and private keys is empty"
     gLogger.error(error)
Example #17
0
   return {"success":"false","error":error}
 gLogger.info("Split certificate(s) to public and private keys")
 keyList = list()
 from DIRAC.Core.Utilities import Subprocess
 for i in descriptionList:
   key = dict()
   name = i["name"]
   p12 = i["p12"]
   key["pem"] = i["pem"]
   for j in "pub","private":
     tmp = "".join(random.choice(string.letters) for x in range(10))
     key[j] = os.path.join(storePath,tmp)
   cmdCert = "openssl pkcs12 -clcerts -nokeys -in %s -out %s -password file:%s" % (name,key["pub"],p12)
   cmdKey = "openssl pkcs12 -nocerts -in %s -out %s -passout file:%s -password file:%s" % (name,key["private"],key["pem"],p12)
   for cmd in cmdCert,cmdKey:
     result = Subprocess.shellCall(900,cmd)
     gLogger.debug("Command is: %s" % cmd)
     gLogger.debug("Result is: %s" % result)
     if not result["OK"]:
       shutil.rmtree(storePath)
       gLogger.error(result["Message"])
       error =  "Error while executing SSL command: %s" % result["Message"]
       error = error + disclaimer
       gLogger.debug("Service response: %s" % error)
       return {"success":"false","error":error}
   keyList.append(key)
 if not len(keyList) > 0:
   shutil.rmtree(storePath)
   error = "List of public and private keys is empty"
   gLogger.error(error)
   error = error + disclaimer