reformatted code to pep8 convention

This commit is contained in:
Guillaume Raffy 2018-08-27 13:32:41 +00:00
parent c750804914
commit 197e94d320
1 changed files with 219 additions and 212 deletions

View File

@ -1,38 +1,38 @@
import MySQLdb import MySQLdb
import time import time
import subprocess
import StringIO import StringIO
import re import re
from wol import * from wol import *
import os import os
import signal
from Util import * from Util import *
import abc import abc
import sqlite3 import sqlite3
from mysql2sqlite import mysql_to_sqlite from mysql2sqlite import mysql_to_sqlite
def isMachineResponding(machineName): def isMachineResponding(machineName):
(returnCode, stdout, stderr) = executeProgram( [ 'ping', '-o', '-t', '1', machineName ] ) (returnCode, stdout, stderr) = executeProgram(['ping', '-o', '-t', '1', machineName])
#log( 'isMachineResponding : result of command %s : %d' % (command, returnCode) ) # log( 'isMachineResponding : result of command %s : %d' % (command, returnCode) )
if returnCode == 0: if returnCode == 0:
return True return True
else: else:
bMachineNameIsNotKnown = (returnCode == 68) bMachineNameIsNotKnown = (returnCode == 68)
bMachineIsNotResponding = (returnCode == 2) bMachineIsNotResponding = (returnCode == 2)
if bMachineIsNotResponding == False: if bMachineIsNotResponding is False:
bBUG_00000004_IS_STILL_ALIVE = True bBUG_00000004_IS_STILL_ALIVE = True
if bBUG_00000004_IS_STILL_ALIVE == True and returnCode == 142: if bBUG_00000004_IS_STILL_ALIVE is True and returnCode == 142:
log('isMachineResponding : bug00000004 Unexpected return code : returnCode=%d, stdout="%s", stderr="%s" , machineName = %s' % (returnCode, stdout, stderr, machineName) ) log('isMachineResponding : bug00000004 Unexpected return code : returnCode=%d, stdout="%s", stderr="%s" , machineName = %s' % (returnCode, stdout, stderr, machineName))
# don't stop the program until we understand bug00000004 # don't stop the program until we understand bug00000004
elif bBUG_00000004_IS_STILL_ALIVE == True and returnCode == -14: # I had this error code on 07/09/2009 20:38 but I don't know yet what that means elif bBUG_00000004_IS_STILL_ALIVE is True and returnCode == -14: # I had this error code on 07/09/2009 20:38 but I don't know yet what that means
log('isMachineResponding : bug00000004 Unexpected return code : returnCode=%d, stdout="%s", stderr="%s" , machineName = %s' % (returnCode, stdout, stderr, machineName) ) log('isMachineResponding : bug00000004 Unexpected return code : returnCode=%d, stdout="%s", stderr="%s" , machineName = %s' % (returnCode, stdout, stderr, machineName))
# don't stop the program until we understand bug00000004 # don't stop the program until we understand bug00000004
else: else:
log('isMachineResponding : Unexpected return code : returnCode=%d, stdout="%s", stderr="%s" , machineName = %s' % (returnCode, stdout, stderr, machineName) ) log('isMachineResponding : Unexpected return code : returnCode=%d, stdout="%s", stderr="%s" , machineName = %s' % (returnCode, stdout, stderr, machineName))
assert(False) assert(False)
return False return False
class ISqlDatabaseBackend(object): class ISqlDatabaseBackend(object):
def __init__(self): def __init__(self):
pass pass
@ -44,6 +44,7 @@ class ISqlDatabaseBackend(object):
""" """
pass pass
class RemoteMysqlDb(ISqlDatabaseBackend): class RemoteMysqlDb(ISqlDatabaseBackend):
def __init__(self, db_server_fqdn, db_user, db_name): def __init__(self, db_server_fqdn, db_user, db_name):
""" """
@ -64,20 +65,20 @@ class RemoteMysqlDb(ISqlDatabaseBackend):
""" """
:param str sql_query: the sql query to perform :param str sql_query: the sql query to perform
""" """
self._conn.query( sql_query ) self._conn.query(sql_query)
rows = conn.store_result() rows = conn.store_result()
return rows return rows
class SqlFile(ISqlDatabaseBackend): class SqlFile(ISqlDatabaseBackend):
def __init__(self, sql_file_path, truncate_hex_strings = False): def __init__(self, sql_file_path, truncate_hex_strings=False):
""" """
:param str sql_file_path: the path of the sql file containing the inventory database :param str sql_file_path: the path of the sql file containing the inventory database
""" """
self._sql_file_path = sql_file_path self._sql_file_path = sql_file_path
self._cur = None # sqlite cursor self._cur = None # sqlite cursor
sqlite_db_path=':memory:' # sqlite-specific special name for a file stored in memory. We could use something like '/tmp/simpadb.sqlite' here but this would make parsing really slow (1 minute instead of 1s), unless either : sqlite_db_path = ':memory:' # sqlite-specific special name for a file stored in memory. We could use something like '/tmp/simpadb.sqlite' here but this would make parsing really slow (1 minute instead of 1s), unless either :
# - proper fix : group of INSERT statements are surrounded by BEGIN and COMMIT (see http://stackoverflow.com/questions/4719836/python-and-sqlite3-adding-thousands-of-rows) # - proper fix : group of INSERT statements are surrounded by BEGIN and COMMIT (see http://stackoverflow.com/questions/4719836/python-and-sqlite3-adding-thousands-of-rows)
# - the file is stored on a solid state disk # - the file is stored on a solid state disk
try: try:
@ -92,13 +93,13 @@ class SqlFile(ISqlDatabaseBackend):
self._con = sqlite3.connect(sqlite_db_path, check_same_thread=check_same_thread) self._con = sqlite3.connect(sqlite_db_path, check_same_thread=check_same_thread)
f = open(self._sql_file_path, 'r') f = open(self._sql_file_path, 'r')
sql = f.read() # watch out for built-in `str` sql = f.read() # watch out for built-in `str`
#print(sql) # print(sql)
self._cur = self._con.cursor() self._cur = self._con.cursor()
#print(mysql_to_sqlite(sql)) # print(mysql_to_sqlite(sql))
sqlite_sql = mysql_to_sqlite(sql, truncate_hex_strings) sqlite_sql = mysql_to_sqlite(sql, truncate_hex_strings)
#with open('/tmp/toto.sqlite.sql', 'w') as f: # with open('/tmp/toto.sqlite.sql', 'w') as f:
# f.write(sqlite_sql) # f.write(sqlite_sql)
#with open('/tmp/toto.sqlite.sql', 'r') as f: # with open('/tmp/toto.sqlite.sql', 'r') as f:
# sqlite_sql = f.read() # sqlite_sql = f.read()
self._cur.executescript(sqlite_sql) self._cur.executescript(sqlite_sql)
@ -107,7 +108,7 @@ class SqlFile(ISqlDatabaseBackend):
:param str sql_query: the sql query to perform :param str sql_query: the sql query to perform
""" """
pass pass
self._cur.execute( sql_query ) self._cur.execute(sql_query)
rows = self._cur.fetchall() rows = self._cur.fetchall()
return rows return rows
@ -128,7 +129,7 @@ class SqlDatabaseReader(object):
""" """
return self._inv_provider.query(sql_query) return self._inv_provider.query(sql_query)
def get_table_attr(self, table, key_name, key_value, attr_name ): def get_table_attr(self, table, key_name, key_value, attr_name):
""" """
reads the value of the fiven attribute of the given item in the given table reads the value of the fiven attribute of the given item in the given table
@ -138,41 +139,43 @@ class SqlDatabaseReader(object):
:param str attr_name: the name of the attribute to read from the item :param str attr_name: the name of the attribute to read from the item
""" """
attr_value = None attr_value = None
rows = self.query("SELECT "+attr_name+" FROM "+table+" WHERE "+key_name+"='"+key_value+"'") rows = self.query("SELECT " + attr_name + " FROM " + table + " WHERE " + key_name + "='" + key_value + "'")
if len(rows) > 0: if len(rows) > 0:
attr_value = rows[0][0] attr_value = rows[0][0]
return attr_value return attr_value
def machineNameToMacAddress( machineName ):
def machineNameToMacAddress(machineName):
conn = MySQLdb.connect('simpatix10', 'simpadb_reader', '', 'simpadb') conn = MySQLdb.connect('simpatix10', 'simpadb_reader', '', 'simpadb')
assert(conn) assert(conn)
sqlQuery = """SELECT mac_address FROM ethernet_cards WHERE machine_name='"""+machineName+"""' AND type='normal'""" sqlQuery = """SELECT mac_address FROM ethernet_cards WHERE machine_name='""" + machineName + """' AND type='normal'"""
#print sqlQuery # print sqlQuery
conn.query( sqlQuery ) conn.query(sqlQuery)
r=conn.store_result() r = conn.store_result()
row = r.fetch_row(0) row = r.fetch_row(0)
assert( len(row) == 1 ) assert( len(row) == 1)
#print 'row =', row # print 'row =', row
macAddress = row[0][0] macAddress = row[0][0]
#print macAddress # print macAddress
conn.close() conn.close()
return macAddress return macAddress
def getLightOutManagementIpAddress( machineName ):
def getLightOutManagementIpAddress(machineName):
""" """
the light out management ip of servers allows to talk to the server even when it's asleep the light out management ip of servers allows to talk to the server even when it's asleep
""" """
conn = MySQLdb.connect('simpatix10', 'simpadb_reader', '', 'simpadb') conn = MySQLdb.connect('simpatix10', 'simpadb_reader', '', 'simpadb')
assert(conn) assert(conn)
sqlQuery = """SELECT ip_address_1,ip_address_2,ip_address_3,ip_address_4 FROM ethernet_cards WHERE machine_name='"""+machineName+"""' AND type='light_out_management'""" sqlQuery = """SELECT ip_address_1,ip_address_2,ip_address_3,ip_address_4 FROM ethernet_cards WHERE machine_name='""" + machineName + """' AND type='light_out_management'"""
#print sqlQuery # print sqlQuery
conn.query( sqlQuery ) conn.query(sqlQuery)
r=conn.store_result() r = conn.store_result()
row = r.fetch_row(0) row = r.fetch_row(0)
assert( len(row) == 1 ) assert(len(row) == 1)
#print 'row =', row # print 'row =', row
ipAddress = ('%s.%s.%s.%s') % (row[0][0], row[0][1], row[0][2], row[0][3]) ipAddress = ('%s.%s.%s.%s') % (row[0][0], row[0][1], row[0][2], row[0][3])
#print macAddress # print macAddress
conn.close() conn.close()
return ipAddress return ipAddress
@ -182,17 +185,18 @@ def getClusterMachinesNames():
conn = MySQLdb.connect('simpatix10', 'simpadb_reader', '', 'simpadb') conn = MySQLdb.connect('simpatix10', 'simpadb_reader', '', 'simpadb')
assert(conn) assert(conn)
sqlQuery = """SELECT name FROM machines WHERE affectation='cluster'""" sqlQuery = """SELECT name FROM machines WHERE affectation='cluster'"""
#print sqlQuery # print sqlQuery
conn.query( sqlQuery ) conn.query(sqlQuery)
r=conn.store_result() r = conn.store_result()
rows = r.fetch_row(0) rows = r.fetch_row(0)
for row in rows: for row in rows:
#print row # print row
clusterMachinesNames.append( row[0] ) clusterMachinesNames.append(row[0])
conn.close() conn.close()
return clusterMachinesNames return clusterMachinesNames
def machineSupportsIpmi( machineName ):
def machineSupportsIpmi(machineName):
if (machineName == 'simpatix') or (machineName == 'simpatix01' or (machineName == 'simpa-mac2')): if (machineName == 'simpatix') or (machineName == 'simpatix01' or (machineName == 'simpa-mac2')):
# the command ipmitool sensor on simpatix doesn't work : # the command ipmitool sensor on simpatix doesn't work :
# Unabled to establish a session with the BMC. # Unabled to establish a session with the BMC.
@ -200,9 +204,10 @@ def machineSupportsIpmi( machineName ):
return False return False
return True return True
def putToSleep( machineName ):
def putToSleep(machineName):
# note : pmset must be executed as root # note : pmset must be executed as root
(returnCode, stdout, stderr) = executeCommand([ 'ssh', machineName, 'pmset sleepnow' ]) (returnCode, stdout, stderr) = executeCommand(['ssh', machineName, 'pmset sleepnow'])
""" """
print returnCode print returnCode
print 'stdout :' print 'stdout :'
@ -210,7 +215,7 @@ def putToSleep( machineName ):
print 'stderr :' print 'stderr :'
print stderr print stderr
""" """
assert( returnCode == 0 ) assert(returnCode == 0)
# check if the command succeeded by looking at the output (that's the only way I found) # check if the command succeeded by looking at the output (that's the only way I found)
f = StringIO.StringIO(stdout) f = StringIO.StringIO(stdout)
line = f.readline() line = f.readline()
@ -221,8 +226,9 @@ def putToSleep( machineName ):
else: else:
return False return False
def wakeUp(machineName): def wakeUp(machineName):
macAddress = machineNameToMacAddress( machineName ) macAddress = machineNameToMacAddress(machineName)
wake_on_lan(macAddress) wake_on_lan(macAddress)
return True return True
@ -233,13 +239,14 @@ def isNonRespondingMachineSleeping(machineName):
""" """
wakeUp(machineName) wakeUp(machineName)
time.sleep(120) time.sleep(120)
if isMachineResponding( machineName ): if isMachineResponding(machineName):
putToSleep( machineName ) putToSleep(machineName)
time.sleep(30) # allow a little time to make sure the machine is ready to receive other wake on lan messages time.sleep(30) # allow a little time to make sure the machine is ready to receive other wake on lan messages
return True return True
else: else:
return False return False
if __name__ == '__main__': if __name__ == '__main__':
""" """
for i in range(30): for i in range(30):
@ -247,5 +254,5 @@ if __name__ == '__main__':
print 'lom ip of %s is %s' % (machineName, getLightOutManagementIpAddress(machineName)) print 'lom ip of %s is %s' % (machineName, getLightOutManagementIpAddress(machineName))
""" """
wakeUp('simpatix21') wakeUp('simpatix21')
#print putToSleep('simpatix13') # print putToSleep('simpatix13')
#print isNonRespondingMachineSleeping('simpatix13') # print isNonRespondingMachineSleeping('simpatix13')