improved tool so that it now retreives the list of machines hosted at dsi automatically from the inventory

work related to https://bugzilla.ipr.univ-rennes1.fr/show_bug.cgi?id=3541
This commit is contained in:
Guillaume Raffy 2023-10-06 14:06:10 +02:00
parent 5241bbe3f8
commit 60291a9b76
1 changed files with 48 additions and 28 deletions

View File

@ -1,6 +1,9 @@
from typing import List
from cocluto.inventory import Inventory
from cocluto.SimpaDbUtil import SqlDatabaseReader, SqlFile
from urllib.parse import urlparse
import re
def get_md_table_separator_line(columns_width):
@ -55,12 +58,7 @@ def render_table_wiki(columns_data, labels):
print(line_format % tuple(row))
def print_machine_table(machines_fqdn):
itinv_db_url = "file:///home/graffy/work/simpaweb/itinv.git/itinv.sql"
url_parts = urlparse(itinv_db_url)
sql_source = SqlFile(url_parts.path)
sql_reader = SqlDatabaseReader(sql_source)
inventory = Inventory(sql_reader)
def print_machine_table(machines_fqdn: List[str], inventory: Inventory):
machines_name = []
machines_spec_id = []
@ -81,26 +79,48 @@ def print_machine_table(machines_fqdn):
render_table_wiki([machines_name, machines_spec_id, machines_serial_number], [name_label, model_label, serial_label])
if __name__ == '__main__':
machines_fqdn = [
'physix49.ipr.univ-rennes1.fr',
'physix50.ipr.univ-rennes1.fr',
'physix51.ipr.univ-rennes1.fr',
'physix52.ipr.univ-rennes1.fr',
'physix53.ipr.univ-rennes1.fr',
'physix54.ipr.univ-rennes1.fr',
'physix55.ipr.univ-rennes1.fr',
'physix56.ipr.univ-rennes1.fr',
'physix57.ipr.univ-rennes1.fr',
'physix58.ipr.univ-rennes1.fr',
'simpatix59.ipr.univ-rennes1.fr',
'physix72.ipr.univ-rennes1.fr',
'physix73.ipr.univ-rennes1.fr',
'physix74.ipr.univ-rennes1.fr',
'physix75.ipr.univ-rennes1.fr',
'physix90.ipr.univ-rennes1.fr',
'physix91.ipr.univ-rennes1.fr',
'alambix103.ipr.univ-rennes1.fr',
'alambix104.ipr.univ-rennes1.fr'
# get the list of machines hosted at the dsi
def get_hosted_machines_fqdn(inventory: Inventory) -> List[str]:
machines_fqdn = []
domain_name = 'ipr.univ-rennes1.fr'
dsi_rack_ids = [
'rack_dsi_h7',
'rack_dsi_i7'
]
print_machine_table(machines_fqdn)
conditions = [('rack_id == "%s"' % rack_id) for rack_id in dsi_rack_ids]
sql_query = 'select machine_id from rackable_machine_to_location where %s;' % ' or '.join(conditions)
rows = inventory.query(sql_query)
for (machine_id, ) in rows:
match = re.match(r'(?P<cluster_name>[a-z]+)(?P<from>[0-9]+)_(?P<to>[0-9]+)', machine_id)
if match:
# handle containers such as physix72_75
from_index = int(match['from'])
to_index = int(match['to'])
cluster_name = match['cluster_name']
# print(from_index, to_index)
for machine_index in range(from_index, to_index + 1):
machine_id = '%s%d' % (cluster_name, machine_index)
if machine_id == 'physix75':
machine_id = 'alambix75' # hack because the inventory expects all machines of a container such as physix72_75 to belong to the same cluster, and it's not the case here
machines_fqdn.append('%s.%s' % (machine_id, domain_name))
else:
if re.match('^switch', machine_id) is None: # ignore switches, as they belong to the dsi
machines_fqdn.append('%s.%s' % (machine_id, domain_name))
return sorted(machines_fqdn)
def main():
itinv_db_url = "file:///home/graffy/work/simpaweb/itinv.git/itinv.sql"
url_parts = urlparse(itinv_db_url)
sql_source = SqlFile(url_parts.path)
sql_reader = SqlDatabaseReader(sql_source)
inventory = Inventory(sql_reader)
machines_fqdn = get_hosted_machines_fqdn(inventory)
print('ipr machines hosted at the dsi (%d):' % len(machines_fqdn))
print_machine_table(machines_fqdn, inventory)
if __name__ == '__main__':
main()