1 | # -*- coding: utf-8 -*- |
---|
2 | ################################################################################# |
---|
3 | # @file labs.py |
---|
4 | # @brief Module that encapsulates Opengnsys Rest API access |
---|
5 | # for Opengnsys Laboratories. |
---|
6 | # @warning None |
---|
7 | # @note Use: None |
---|
8 | # @license GNU GPLv3+ |
---|
9 | # @author David Fuertes, EUPT, University of Zaragoza. |
---|
10 | # Opengnsys Api Rest support provided by Juan Carlos García, EUPT, University of Zaragoza. |
---|
11 | # @version 1.1.0 - First version |
---|
12 | # @date 2019-15-11 |
---|
13 | ################################################################################# |
---|
14 | |
---|
15 | import concurrent.futures |
---|
16 | import threading |
---|
17 | import time |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | import ognsys_globals |
---|
22 | from http_requests import HttpRequest, UsingPoolManagerConnector, \ |
---|
23 | NotUsingPoolManagerConnector |
---|
24 | |
---|
25 | from ognsys_actions import GetDiskConfigClient, GetLabClients, GetStatusClient, GetImageInfo |
---|
26 | |
---|
27 | |
---|
28 | |
---|
29 | class Lab: |
---|
30 | |
---|
31 | |
---|
32 | def __init__(self, ou_id, lab_id): |
---|
33 | self.ou_id = ou_id |
---|
34 | self.lab_id = lab_id |
---|
35 | |
---|
36 | |
---|
37 | self.http_request = HttpRequest() |
---|
38 | |
---|
39 | |
---|
40 | def __get_clients(self): |
---|
41 | self.http_request.set_connector(UsingPoolManagerConnector(ognsys_globals.local.__POOL_MANAGER__)) |
---|
42 | |
---|
43 | clients = self.http_request.do_action(GetLabClients(self.ou_id, self.lab_id)) |
---|
44 | |
---|
45 | try: |
---|
46 | return sorted(clients, key = lambda k:k['name'], reverse=True) |
---|
47 | except: |
---|
48 | return {'error': 'Error al obtener lista de clientes.'} |
---|
49 | |
---|
50 | |
---|
51 | # Do not use Client object to do action GetStatus for save resources. |
---|
52 | # Then action GestatusClient have two entry point but code is only in one place. |
---|
53 | def __get_status_client(self, pc): |
---|
54 | self.http_request.set_connector(NotUsingPoolManagerConnector()) |
---|
55 | pc['status'] = self.http_request.do_action(GetStatusClient(pc['ip'])) |
---|
56 | |
---|
57 | return pc |
---|
58 | |
---|
59 | |
---|
60 | def __add_status_clients(self, PCs): |
---|
61 | |
---|
62 | start_time = time.time() |
---|
63 | with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor: |
---|
64 | PCs_with_status = list(executor.map(self.__get_status_client, PCs)) |
---|
65 | |
---|
66 | print(time.time() - start_time) |
---|
67 | return PCs_with_status |
---|
68 | |
---|
69 | |
---|
70 | def __get_diskconfig_client(self, pc_id): |
---|
71 | self.http_request.set_connector(UsingPoolManagerConnector(ognsys_globals.local.__POOL_MANAGER__)) |
---|
72 | return self.http_request.do_action(GetDiskConfigClient(self.ou_id, self.lab_id, pc_id)) |
---|
73 | |
---|
74 | def __check_disponible(self, pc_status): |
---|
75 | if 'error' in pc_status: |
---|
76 | return True |
---|
77 | else: |
---|
78 | if pc_status['status'] == "off": |
---|
79 | return True |
---|
80 | else: |
---|
81 | return False |
---|
82 | |
---|
83 | |
---|
84 | def __check_is_inremotepc(self, image_id): |
---|
85 | self.http_request.set_connector(UsingPoolManagerConnector(ognsys_globals.local.__POOL_MANAGER__)) |
---|
86 | image_info = self.http_request.do_action(GetImageInfo(self.ou_id, image_id)) |
---|
87 | if "inremotepc" in image_info: |
---|
88 | return image_info["inremotepc"] |
---|
89 | else: |
---|
90 | return False |
---|
91 | |
---|
92 | def get_available_images(self): |
---|
93 | pass |
---|
94 | |
---|
95 | def get_remote_clients(self): |
---|
96 | |
---|
97 | PCs = self.__get_clients() |
---|
98 | if 'error' in PCs: |
---|
99 | return {'error' : 'Error de optención lista de clientes desde opengnsys'} |
---|
100 | if PCs: |
---|
101 | if len(PCs) == 0 : |
---|
102 | return {'error' : 'Error No quedan equipos disponibles.'} |
---|
103 | else: |
---|
104 | return {'error' : 'Error No quedan equipos disponibles.'} |
---|
105 | |
---|
106 | PCs = self.__add_status_clients(PCs) |
---|
107 | |
---|
108 | pcs_total = len(PCs) |
---|
109 | pcs_disponibles = 0 |
---|
110 | PCs_info = [] |
---|
111 | images_info = [] |
---|
112 | #print('pcs') |
---|
113 | #print(PCs) |
---|
114 | for pc in PCs: |
---|
115 | # NO porque saturamos servidor opengnsys |
---|
116 | #pc_status = __getStatusClient(ou_id, lab_id, str(pc['id'])) |
---|
117 | |
---|
118 | #Por el momento da error si el equipo hace tiempo no levantado |
---|
119 | #pc_status = __getStatusClientFromList(PCs_status, pc['id']) |
---|
120 | |
---|
121 | pc_info = {} |
---|
122 | |
---|
123 | pc_info['pc'] = pc |
---|
124 | #pc_info['status'] = pc_status |
---|
125 | |
---|
126 | PCs_info.append(pc_info) |
---|
127 | |
---|
128 | if self.__check_disponible(pc['status']): |
---|
129 | |
---|
130 | pcs_disponibles = pcs_disponibles + 1 |
---|
131 | |
---|
132 | pc_diskcfg = self.__get_diskconfig_client(str(pc['id'])) |
---|
133 | #print('discfg') |
---|
134 | #print(pc_diskcfg) |
---|
135 | for partition in pc_diskcfg['diskcfg']: |
---|
136 | #print('partition') |
---|
137 | #print(partition) |
---|
138 | image__aready_added = False |
---|
139 | |
---|
140 | if ("parttype" and 'image') in partition: |
---|
141 | |
---|
142 | for image in images_info: |
---|
143 | if image['id'] == partition['image']['id']: |
---|
144 | image__aready_added = True |
---|
145 | |
---|
146 | if image__aready_added == False: |
---|
147 | if self.__check_is_inremotepc(partition['image']['id']): |
---|
148 | images_info.append({'id': partition['image']['id'], 'os': partition['os'], |
---|
149 | 'lab_id': self.lab_id, 'ou_id': self.ou_id}) |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | if pcs_disponibles == 0: |
---|
154 | return {'error' : 'Error No quedan equipos disponibles'} |
---|
155 | else: |
---|
156 | if len(images_info) == 0: |
---|
157 | return {'error' : 'Error Los equipos no tienen imagen asignada en OpenGnsys.'} |
---|
158 | else: |
---|
159 | return {'PCs_info' : sorted(PCs_info, key = lambda k:k['pc']['name'], reverse=False), |
---|
160 | 'images_info' : images_info, |
---|
161 | 'disponibles_info': {'lab_id': self.lab_id, |
---|
162 | 'total': pcs_total, |
---|
163 | 'disponibles': pcs_disponibles} |
---|
164 | } |
---|
165 | |
---|
166 | def get_total_clients(self): |
---|
167 | return len(self.__get_clients()) |
---|