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 time
import subprocess
import StringIO
import re
from wol import *
import os
import signal
from Util import *
import abc
import sqlite3
from mysql2sqlite import mysql_to_sqlite
def isMachineResponding(machineName):
(returnCode, stdout, stderr) = executeProgram( [ 'ping', '-o', '-t', '1', machineName ] )
#log( 'isMachineResponding : result of command %s : %d' % (command, returnCode) )
(returnCode, stdout, stderr) = executeProgram(['ping', '-o', '-t', '1', machineName])
# log( 'isMachineResponding : result of command %s : %d' % (command, returnCode) )
if returnCode == 0:
return True
else:
bMachineNameIsNotKnown = (returnCode == 68)
bMachineIsNotResponding = (returnCode == 2)
if bMachineIsNotResponding == False:
if bMachineIsNotResponding is False:
bBUG_00000004_IS_STILL_ALIVE = True
if bBUG_00000004_IS_STILL_ALIVE == True and returnCode == 142:
log('isMachineResponding : bug00000004 Unexpected return code : returnCode=%d, stdout="%s", stderr="%s" , machineName = %s' % (returnCode, stdout, stderr, machineName) )
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))
# 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
log('isMachineResponding : bug00000004 Unexpected return code : returnCode=%d, stdout="%s", stderr="%s" , machineName = %s' % (returnCode, stdout, stderr, machineName) )
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))
# don't stop the program until we understand bug00000004
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)
return False
class ISqlDatabaseBackend(object):
def __init__(self):
pass
@ -44,6 +44,7 @@ class ISqlDatabaseBackend(object):
"""
pass
class RemoteMysqlDb(ISqlDatabaseBackend):
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
"""
self._conn.query( sql_query )
self._conn.query(sql_query)
rows = conn.store_result()
return rows
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
"""
self._sql_file_path = sql_file_path
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)
# - the file is stored on a solid state disk
try:
@ -92,13 +93,13 @@ class SqlFile(ISqlDatabaseBackend):
self._con = sqlite3.connect(sqlite_db_path, check_same_thread=check_same_thread)
f = open(self._sql_file_path, 'r')
sql = f.read() # watch out for built-in `str`
#print(sql)
# print(sql)
self._cur = self._con.cursor()
#print(mysql_to_sqlite(sql))
# print(mysql_to_sqlite(sql))
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)
#with open('/tmp/toto.sqlite.sql', 'r') as f:
# with open('/tmp/toto.sqlite.sql', 'r') as f:
# sqlite_sql = f.read()
self._cur.executescript(sqlite_sql)
@ -107,7 +108,7 @@ class SqlFile(ISqlDatabaseBackend):
:param str sql_query: the sql query to perform
"""
pass
self._cur.execute( sql_query )
self._cur.execute(sql_query)
rows = self._cur.fetchall()
return rows
@ -128,7 +129,7 @@ class SqlDatabaseReader(object):
"""
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
@ -138,41 +139,43 @@ class SqlDatabaseReader(object):
:param str attr_name: the name of the attribute to read from the item
"""
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:
attr_value = rows[0][0]
return attr_value
def machineNameToMacAddress( machineName ):
def machineNameToMacAddress(machineName):
conn = MySQLdb.connect('simpatix10', 'simpadb_reader', '', 'simpadb')
assert(conn)
sqlQuery = """SELECT mac_address FROM ethernet_cards WHERE machine_name='"""+machineName+"""' AND type='normal'"""
#print sqlQuery
conn.query( sqlQuery )
r=conn.store_result()
sqlQuery = """SELECT mac_address FROM ethernet_cards WHERE machine_name='""" + machineName + """' AND type='normal'"""
# print sqlQuery
conn.query(sqlQuery)
r = conn.store_result()
row = r.fetch_row(0)
assert( len(row) == 1 )
#print 'row =', row
assert( len(row) == 1)
# print 'row =', row
macAddress = row[0][0]
#print macAddress
# print macAddress
conn.close()
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
"""
conn = MySQLdb.connect('simpatix10', 'simpadb_reader', '', 'simpadb')
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'"""
#print sqlQuery
conn.query( sqlQuery )
r=conn.store_result()
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
conn.query(sqlQuery)
r = conn.store_result()
row = r.fetch_row(0)
assert( len(row) == 1 )
#print 'row =', row
assert(len(row) == 1)
# print 'row =', row
ipAddress = ('%s.%s.%s.%s') % (row[0][0], row[0][1], row[0][2], row[0][3])
#print macAddress
# print macAddress
conn.close()
return ipAddress
@ -182,17 +185,18 @@ def getClusterMachinesNames():
conn = MySQLdb.connect('simpatix10', 'simpadb_reader', '', 'simpadb')
assert(conn)
sqlQuery = """SELECT name FROM machines WHERE affectation='cluster'"""
#print sqlQuery
conn.query( sqlQuery )
r=conn.store_result()
# print sqlQuery
conn.query(sqlQuery)
r = conn.store_result()
rows = r.fetch_row(0)
for row in rows:
#print row
clusterMachinesNames.append( row[0] )
# print row
clusterMachinesNames.append(row[0])
conn.close()
return clusterMachinesNames
def machineSupportsIpmi( machineName ):
def machineSupportsIpmi(machineName):
if (machineName == 'simpatix') or (machineName == 'simpatix01' or (machineName == 'simpa-mac2')):
# the command ipmitool sensor on simpatix doesn't work :
# Unabled to establish a session with the BMC.
@ -200,9 +204,10 @@ def machineSupportsIpmi( machineName ):
return False
return True
def putToSleep( machineName ):
def putToSleep(machineName):
# 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 'stdout :'
@ -210,7 +215,7 @@ def putToSleep( machineName ):
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)
f = StringIO.StringIO(stdout)
line = f.readline()
@ -221,8 +226,9 @@ def putToSleep( machineName ):
else:
return False
def wakeUp(machineName):
macAddress = machineNameToMacAddress( machineName )
macAddress = machineNameToMacAddress(machineName)
wake_on_lan(macAddress)
return True
@ -233,13 +239,14 @@ def isNonRespondingMachineSleeping(machineName):
"""
wakeUp(machineName)
time.sleep(120)
if isMachineResponding( machineName ):
putToSleep( machineName )
if isMachineResponding(machineName):
putToSleep(machineName)
time.sleep(30) # allow a little time to make sure the machine is ready to receive other wake on lan messages
return True
else:
return False
if __name__ == '__main__':
"""
for i in range(30):
@ -247,5 +254,5 @@ if __name__ == '__main__':
print 'lom ip of %s is %s' % (machineName, getLightOutManagementIpAddress(machineName))
"""
wakeUp('simpatix21')
#print putToSleep('simpatix13')
#print isNonRespondingMachineSleeping('simpatix13')
# print putToSleep('simpatix13')
# print isNonRespondingMachineSleeping('simpatix13')