diff --git a/ovh_management.py b/ovh_management.py index 47aa083..782b980 100644 --- a/ovh_management.py +++ b/ovh_management.py @@ -12,14 +12,18 @@ regions = ['GRA7', 'SBG5', 'DE1', 'UK1', 'BHS5'] def get_flavor_id(region, name='s1-2'): flavors = client.get(service_url + '/flavor') - flavorId = [f['id'] for f in flavors if f['name'] == name and f['region']==region][0] + flavorId = [f['id'] for f in flavors if f['name'] + == name and f['region'] == region][0] return flavorId + def get_image_id(region, name='Ubuntu 20.04'): images = client.get(service_url + '/image') - imageId = [i['id'] for i in images if i['name']==name and i['region']==region][0] + imageId = [i['id'] for i in images if i['name'] + == name and i['region'] == region][0] return imageId + def get_eleves(): ovh_ssh_keys = client.get(service_url + '/sshkey') liste_eleves = [(k['id'], k['name'].split('_')[1], k['publicKey']) @@ -27,14 +31,17 @@ def get_eleves(): if k['name'].startswith('ssh_')] return liste_eleves + def delete_eleve(eleve_id): client.delete(service_url + '/sshkey/' + eleve_id) + def create_eleve(prenom, publicKey): client.post(service_url + '/sshkey', - name = 'ssh_' + prenom, - publicKey = publicKey - ) + name='ssh_' + prenom, + publicKey=publicKey + ) + def get_instances(): ovh_instances = client.get(service_url + '/instance') @@ -53,21 +60,24 @@ def get_instances(): instances.append((id_inst, name, ip, region, status,)) return instances + def create_instance(eleve_id, name, region): image_id = get_image_id(region) flavor_id = get_flavor_id(region) client.post(service_url + '/instance', - name = 'eica-' + name, - sshKeyId = eleve_id, - flavorId = flavor_id, - imageId = image_id, - region = region - ) + name='eica-' + name, + sshKeyId=eleve_id, + flavorId=flavor_id, + imageId=image_id, + region=region + ) + def delete_instance(instance_id): print(service_url + '/instance/' + instance_id) client.delete(service_url + '/instance/' + instance_id) + def make_main_window(): liste_eleves = get_eleves() liste_instances = get_instances() @@ -76,23 +86,24 @@ def make_main_window(): [sg.Button('Modifier', size=(12, 1))], [sg.Button('Supprimer', size=(12, 1))], [sg.Combo(regions, size=(12, 1), default_value=regions[0], k='region')], - [sg.Button('Instancier', size=(12, 1), button_color=('white', 'green'))], + [sg.Button('Instancier', size=(12, 1), + button_color=('white', 'green'))], [sg.Button('Quitter', size=(12, 1))], ] col1 = [ [sg.Table(values=liste_instances, - headings=['id', 'Nom', 'Adresse IP', 'Région', 'Statut'], - max_col_width=25, - col_widths=[0, 15, 12, 10, 10], - hide_vertical_scroll=True, - background_color='light blue', - text_color='black', - auto_size_columns=False, - justification='right', - num_rows=len(liste_instances), - alternating_row_color='lightyellow', - key='table_instances', - tooltip='Liste des instances actives')], + headings=['id', 'Nom', 'Adresse IP', 'Région', 'Statut'], + max_col_width=25, + col_widths=[0, 15, 12, 10, 10], + hide_vertical_scroll=True, + background_color='light blue', + text_color='black', + auto_size_columns=False, + justification='right', + num_rows=len(liste_instances), + alternating_row_color='lightyellow', + key='table_instances', + tooltip='Liste des instances actives')], ] col2 = [ [sg.Button('Détruire', size=(12, 1), button_color=('white', 'red'))], @@ -116,7 +127,7 @@ def make_main_window(): sg.Column(col), sg.Column(col1), sg.Column(col2)], - + ] window = sg.Window('OVH - Gestion des instances', layout=layout, @@ -133,7 +144,7 @@ def make_add_window(caption, role='create', initial_values=None): else: eleve_id = prenom = pk = '' layout = [ - [sg.InputText(eleve_id, k='eleve_id', visible=True)], + [sg.InputText(eleve_id, k='eleve_id', visible=False)], [sg.Text(text='Prénom', size=(10, 2)), sg.InputText( default_text=prenom, size=(20, 2), k='prenom')], [sg.Text(text='Clé publique', size=(10, 2)), sg.InputText( @@ -178,7 +189,7 @@ while True: if confirmation == "Non": continue sg.PopupAnimated("sablier.png") - + delete_eleve(id_eleve) liste_eleves = get_eleves() @@ -200,7 +211,7 @@ while True: initial_values=[id_eleve, prenom, pk], role='modify') window2.make_modal() - + if event == 'Instancier': selected_lines = values['table_eleves'] if len(selected_lines) == 0: @@ -220,18 +231,19 @@ while True: continue sg.PopupAnimated("sablier.png") - + for el in eleves: - active_instances_names = [i[1].split('-')[1] for i in window1.liste_instances] + active_instances_names = [i[1].split( + '-')[1] for i in window1.liste_instances] if not el[1] in active_instances_names: create_instance(el[0], el[1], values['region']) - + liste_instances = get_instances() sg.PopupAnimated(None) window1['table_instances'].update(values=liste_instances, - num_rows=len(liste_instances)) + num_rows=len(liste_instances)) window1.liste_instances = liste_instances if event == 'Détruire': @@ -249,31 +261,28 @@ while True: custom_text=("Oui", "Non")) if confirmation == "Non": continue - + sg.PopupAnimated("sablier.png") for id_inst in instances: delete_instance(id_inst) - liste_instances = get_instances() sg.PopupAnimated(None) window1['table_instances'].update(values=liste_instances, - num_rows=len(liste_instances)) + num_rows=len(liste_instances)) window1.liste_instances = liste_instances - if event == 'Actualiser': sg.PopupAnimated("sablier.png") liste_instances = get_instances() sg.PopupAnimated(None) window1['table_instances'].update(values=liste_instances, - num_rows=len(liste_instances)) + num_rows=len(liste_instances)) window1.liste_instances = liste_instances - if window == window2: if event in (sg.WIN_CLOSED, 'Annuler'): window2.close() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5631f23 Binary files /dev/null and b/requirements.txt differ diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..5ccba77 --- /dev/null +++ b/setup.py @@ -0,0 +1,27 @@ +import sys +from cx_Freeze import setup, Executable + +options = { + "build_exe": { + 'include_files' : ['ovh_.conf', 'sablier.png'], + "include_msvcr": True, + 'optimize': 2, + } +} + +build_exe_options = { + +} +base = None +if sys.platform == "win32": + base = "Win32GUI" + +executables = [Executable("ovh_management.py", base=base)] + +setup( + name="OVH instances management", + version="0.1", + description="To manage several OVH instances at once", + executables=executables, + options=options +) \ No newline at end of file