def test_getsharedarealoc_notfound( self ): with patch('%s.Operations.getValue' % MODULE_NAME, new=Mock(return_value=[])), \ patch('%s.os.path.exists' % MODULE_NAME, new=Mock(side_effect=[ False, True ])), \ patch('%s.DIRAC.gConfig.getValue' % MODULE_NAME, new=Mock(side_effect=['a', 'a', '', ''])), \ patch('%s.os.path.isdir' % MODULE_NAME, new=Mock(return_value=True)): result = getSharedAreaLocation() assertEqualsImproved( result, '', self )
def execute(self): """ Look in folders (Shared Area and Local Area) and try ot remove the applications specified. """ self.softs = self.step_commons.get('Apps', None) if not self.softs: return S_ERROR('Applications to remove were not defined') self.platform = self.workflow_commons.get('Platform', None) if not self.platform: return S_ERROR('Platform, formerly known as SystemConfig not defined') self.softs.rstrip(";") self.apps = self.softs.split(';') self.log.info("Will delete %s" % self.apps) failed = [] for app in self.apps: if not app: continue appname = app.split(".")[0] appversion = app.split(".")[1] appDir = self.ops.getValue('/AvailableTarBalls/%s/%s/%s/TarBall' % (self.platform, appname, appversion), '') appDir = appDir.replace(".tgz", "").replace(".tar.gz", "") mySoftwareRoot = '' localArea = getLocalAreaLocation() sharedArea = getSharedAreaLocation() if os.path.exists('%s%s%s' %(localArea, os.sep, appDir)): mySoftwareRoot = localArea elif os.path.exists('%s%s%s' %(sharedArea, os.sep, appDir)): mySoftwareRoot = sharedArea else: self.log.error('%s: Could not find neither local area not shared area install' % app) continue myappDir = os.path.join(mySoftwareRoot, appDir) #### Hacky hack needed when the DB was in parallel to the Mokka version if appname.lower() == 'mokka': dbloc = os.path.join(mySoftwareRoot, "CLICMokkaDB.sql") if os.path.exists(dbloc): try: os.remove(dbloc) except OSError as x: self.log.error("Could not delete SQL DB file : %s" % (str(x))) if os.path.isdir(myappDir): try: shutil.rmtree(myappDir) except OSError as x: self.log.error("Could not delete %s : %s" % (app, str(x))) failed.append(app) else: try: os.remove(myappDir) except OSError as x: self.log.error("Could not delete %s: %s" % (myappDir, str(x))) if len(failed): return S_ERROR("Failed deleting applications %s" % failed) self.log.info("Successfully deleted %s" % self.apps) return S_OK()
def test_getsharedarealoc_notadir( self ): with patch('%s.Operations.getValue' % MODULE_NAME, new=Mock(return_value=[ 'testLocation135', '$MANY_MORE_LOCATIONS' ])), \ patch.dict( os.environ, { 'MANY_MORE_LOCATIONS' : '/abc/def/ghi'}, True ), \ patch('%s.os.path.exists' % MODULE_NAME, new=Mock(side_effect=[ False, True ])), \ patch('%s.DIRAC.gConfig.getValue' % MODULE_NAME, new=Mock(side_effect=['a', 'a', '', ''])), \ patch('%s.os.path.isdir' % MODULE_NAME, new=Mock(return_value=False)): result = getSharedAreaLocation() assertEqualsImproved( result, '', self )
def test_getsharedarealoc_notfound(self): with patch('%s.Operations.getValue' % MODULE_NAME, new=Mock(return_value=[])), \ patch('%s.os.path.exists' % MODULE_NAME) as exists_mock, \ patch('%s.DIRAC.gConfig.getValue' % MODULE_NAME, new=Mock(side_effect=['a', 'a', '', ''])), \ patch('%s.os.path.isdir' % MODULE_NAME, new=Mock(return_value=True)): result = getSharedAreaLocation() assertEqualsImproved(result, '', self) self.assertFalse(exists_mock.called)
def test_getsharedarealoc(self): exists_dict = {'mylocation123test': True} with patch('%s.Operations.getValue' % MODULE_NAME, new=Mock(return_value='mylocation123test')), \ patch('%s.os.path.exists' % MODULE_NAME, new=Mock(side_effect=lambda path: exists_dict[path])), \ patch('%s.DIRAC.gConfig.getValue' % MODULE_NAME, new=Mock(side_effect=['a', 'a', '',''])), \ patch('%s.os.path.isdir' % MODULE_NAME, new=Mock(return_value=True)): result = getSharedAreaLocation() assertEqualsImproved(result, 'mylocation123test', self)
def test_getsharedarealoc( self ): exists_dict = { 'mylocation123test' : True } with patch('%s.Operations.getValue' % MODULE_NAME, new=Mock(return_value='mylocation123test')), \ patch('%s.os.path.exists' % MODULE_NAME, new=Mock(side_effect=lambda path: exists_dict[path])), \ patch('%s.DIRAC.gConfig.getValue' % MODULE_NAME, new=Mock(side_effect=['a', 'a', '',''])), \ patch('%s.os.path.isdir' % MODULE_NAME, new=Mock(return_value=True)): result = getSharedAreaLocation() assertEqualsImproved( result, 'mylocation123test', self )
def test_getsharedarealoc_notadir(self): exists_dict = {'testLocation135': False, '/abc/def/ghi/clic': True} with patch('%s.Operations.getValue' % MODULE_NAME, new=Mock(return_value=[ 'testLocation135', '$MANY_MORE_LOCATIONS' ])), \ patch.dict( os.environ, { 'MANY_MORE_LOCATIONS' : '/abc/def/ghi'}, True ), \ patch('%s.os.path.exists' % MODULE_NAME, new=Mock(side_effect=lambda path: exists_dict[path])), \ patch('%s.DIRAC.gConfig.getValue' % MODULE_NAME, new=Mock(side_effect=['a', 'a', '', ''])), \ patch('%s.os.path.isdir' % MODULE_NAME, new=Mock(return_value=False)): result = getSharedAreaLocation() assertEqualsImproved(result, '', self)
def test_getsharedarealoc_overwrite_via_config( self ): exists_dict = { 'testLocation135' : False, '/abc/def/ghi/clic' : True } with patch('%s.Operations.getValue' % MODULE_NAME, new=Mock(return_value=[ 'testLocation135', '$MANY_MORE_LOCATIONS' ])), \ patch.dict( os.environ, { 'MANY_MORE_LOCATIONS' : '/abc/def/ghi'}, True ), \ patch('%s.os.path.exists' % MODULE_NAME, new=Mock(side_effect=lambda path: exists_dict[path])), \ patch('%s.DIRAC.gConfig.getValue' % MODULE_NAME, new=Mock(side_effect=['a', 'a', '/myotherpath/hereissharedarea', '/myotherpath/hereissharedarea'])), \ patch('%s.os.path.isdir' % MODULE_NAME, new=Mock(return_value=True)): result = getSharedAreaLocation() assertEqualsImproved( result, '/myotherpath/hereissharedarea', self )
def execute(self): """ Run the module """ result = self.resolveInputVariables() if not result['OK']: LOG.error("Failed to get the input parameters:", result['Message']) return result if not self.applicationLog: LOG.warn("Log file name missing, reverting to default") self.applicationLog = "AnalyseWN.log" info = [] try: info.append("Host is %s" % socket.gethostname()) except EnvironmentError: info.append("Could not determine host") size = getDiskSpace() if size>0: info.append("Local disk is %s MB"% size) fileName = '/proc/cpuinfo' if os.path.exists( fileName ): with open( fileName, 'r' ) as cpuInfoFile: cpu = cpuInfoFile.readlines() nCPU = 0 for line in cpu: if line.find( 'cpu MHz' ) == 0: nCPU += 1 freq = line.split()[3] elif line.find( 'model name' ) == 0: cpuModel = line.split( ': ' )[1].strip() info.append('CPU (model) = %s' % cpuModel) info.append('CPU (MHz) = %s x %s' % ( nCPU, freq )) fileName = '/proc/meminfo' if os.path.exists( fileName ): with open( fileName, 'r' ) as memInfoFile: mem = memInfoFile.readlines() freeMem = 0 for line in mem: if line.find( 'MemTotal:' ) == 0: totalMem = int( line.split()[1] ) if line.find( 'MemFree:' ) == 0: freeMem += int( line.split()[1] ) if line.find( 'Cached:' ) == 0: freeMem += int( line.split()[1] ) info.append( 'Memory (kB) = %s' % totalMem ) info.append( 'FreeMem. (kB) = %s' % freeMem ) fs = os.statvfs( "." ) # bsize; /* file system block size */ # frsize; /* fragment size */ # blocks; /* size of fs in f_frsize units */ # bfree; /* # free blocks */ # bavail; /* # free blocks for non-root */ # files; /* # inodes */ # ffree; /* # free inodes */ # favail; /* # free inodes for non-root */ # flag; /* mount flags */ # namemax; /* maximum filename length */ diskSpace = fs[4] * fs[0] / 1024 / 1024 info.append( 'DiskSpace (MB) = %s' % diskSpace ) sha = getSharedAreaLocation() if not sha: info.append("No shared Area found here") else: info.append("Shared Area found: %s" % sha) info.append("Content:") sha_list = os.listdir(sha) for item in sha_list: info.append(" %s"% item) sha_size = getDirectorySize(sha) if sha_size: info.append("It uses %s MB of disk"% sha_size) if os.path.isdir("/cvmfs/ilc.cern.ch"): info.append("Has CVMFS") try: of = open(self.applicationLog, "w") of.write("\n".join(info)) of.close() except OSError: LOG.error("Could not create the log file") return S_ERROR("Failed saving the site info") return S_OK()
def execute(self): """ Run the module """ result = self.resolveInputVariables() if not result['OK']: self.log.error("Failed to get the input parameters:", result['Message']) return result if not self.applicationLog: self.log.warn("Log file name missing, reverting to default") self.applicationLog = "AnalyseWN.log" info = [] try: info.append("Host is %s" % socket.gethostname()) except EnvironmentError: info.append("Could not determine host") size = getDiskSpace() if size > 0: info.append("Local disk is %s MB" % size) fileName = '/proc/cpuinfo' if os.path.exists(fileName): with open(fileName, 'r') as cpuInfoFile: cpu = cpuInfoFile.readlines() nCPU = 0 for line in cpu: if line.find('cpu MHz') == 0: nCPU += 1 freq = line.split()[3] elif line.find('model name') == 0: cpuModel = line.split(': ')[1].strip() info.append('CPU (model) = %s' % cpuModel) info.append('CPU (MHz) = %s x %s' % (nCPU, freq)) fileName = '/proc/meminfo' if os.path.exists(fileName): with open(fileName, 'r') as memInfoFile: mem = memInfoFile.readlines() freeMem = 0 for line in mem: if line.find('MemTotal:') == 0: totalMem = int(line.split()[1]) if line.find('MemFree:') == 0: freeMem += int(line.split()[1]) if line.find('Cached:') == 0: freeMem += int(line.split()[1]) info.append('Memory (kB) = %s' % totalMem) info.append('FreeMem. (kB) = %s' % freeMem) fs = os.statvfs(".") # bsize; /* file system block size */ # frsize; /* fragment size */ # blocks; /* size of fs in f_frsize units */ # bfree; /* # free blocks */ # bavail; /* # free blocks for non-root */ # files; /* # inodes */ # ffree; /* # free inodes */ # favail; /* # free inodes for non-root */ # flag; /* mount flags */ # namemax; /* maximum filename length */ diskSpace = fs[4] * fs[0] / 1024 / 1024 info.append('DiskSpace (MB) = %s' % diskSpace) sha = getSharedAreaLocation() if not sha: info.append("No shared Area found here") else: info.append("Shared Area found: %s" % sha) info.append("Content:") sha_list = os.listdir(sha) for item in sha_list: info.append(" %s" % item) sha_size = getDirectorySize(sha) if sha_size: info.append("It uses %s MB of disk" % sha_size) if os.path.isdir("/cvmfs/ilc.cern.ch"): info.append("Has CVMFS") try: of = open(self.applicationLog, "w") of.write("\n".join(info)) of.close() except OSError: self.log.error("Could not create the log file") return S_ERROR("Failed saving the site info") return S_OK()
def execute(self): """ Look in folders (Shared Area and Local Area) and try ot remove the applications specified. """ self.softs = self.step_commons.get('Apps', None) if not self.softs: return S_ERROR('Applications to remove were not defined') self.platform = self.workflow_commons.get('Platform', None) if not self.platform: return S_ERROR( 'Platform, formerly known as SystemConfig not defined') self.softs.rstrip(";") self.apps = self.softs.split(';') self.log.info("Will delete %s" % self.apps) failed = [] for app in self.apps: if not app: continue appname = app.split(".")[0] appversion = app.split(".")[1] appDir = self.ops.getValue( '/AvailableTarBalls/%s/%s/%s/TarBall' % (self.platform, appname, appversion), '') appDir = appDir.replace(".tgz", "").replace(".tar.gz", "") mySoftwareRoot = '' localArea = getLocalAreaLocation() sharedArea = getSharedAreaLocation() if os.path.exists('%s%s%s' % (localArea, os.sep, appDir)): mySoftwareRoot = localArea elif os.path.exists('%s%s%s' % (sharedArea, os.sep, appDir)): mySoftwareRoot = sharedArea else: self.log.error( '%s: Could not find neither local area not shared area install' % app) continue myappDir = os.path.join(mySoftwareRoot, appDir) #### Hacky hack needed when the DB was in parallel to the Mokka version if appname.lower() == 'mokka': dbloc = os.path.join(mySoftwareRoot, "CLICMokkaDB.sql") if os.path.exists(dbloc): try: os.remove(dbloc) except OSError as x: self.log.error("Could not delete SQL DB file : %s" % (str(x))) if os.path.isdir(myappDir): try: shutil.rmtree(myappDir) except OSError as x: self.log.error("Could not delete %s : %s" % (app, str(x))) failed.append(app) else: try: os.remove(myappDir) except OSError as x: self.log.error("Could not delete %s: %s" % (myappDir, str(x))) if len(failed): return S_ERROR("Failed deleting applications %s" % failed) self.log.info("Successfully deleted %s" % self.apps) return S_OK()