1 | # -*- coding: utf-8 -*- |
---|
2 | ################################################################################# |
---|
3 | # @file cnection_stage.py |
---|
4 | # @brief Module that manage connection stages with remotePC |
---|
5 | # @warning None |
---|
6 | # @note Use: None |
---|
7 | # @license GNU GPLv3+ |
---|
8 | # @author David Fuertes, EUPT, University of Zaragoza. |
---|
9 | # @version 1.1.0 - First version |
---|
10 | # @date 2019-15-11 |
---|
11 | ################################################################################# |
---|
12 | from gluon import current |
---|
13 | |
---|
14 | from gluon.storage import Storage |
---|
15 | |
---|
16 | import json |
---|
17 | from datetime import datetime, timedelta |
---|
18 | import time |
---|
19 | |
---|
20 | from client_lab import Client |
---|
21 | from ados import adoDB_reserves, adoDB_openRlabs_setup |
---|
22 | import errors |
---|
23 | |
---|
24 | |
---|
25 | class Connection: |
---|
26 | |
---|
27 | def __init__(self, my_context): |
---|
28 | |
---|
29 | self.my_context = Storage() |
---|
30 | |
---|
31 | # Rellenamos context |
---|
32 | for k,v in my_context.items(): |
---|
33 | self.my_context[k] = v |
---|
34 | |
---|
35 | |
---|
36 | self.client = Client(self.my_context) |
---|
37 | |
---|
38 | self.WAIT_CHECK_LOOP = 10 |
---|
39 | self.MAX_RETRIES = int((adoDB_openRlabs_setup.getSetup_OpenRLabs(my_context['db'])['seconds_to_wait'] / self.WAIT_CHECK_LOOP) - 1) |
---|
40 | |
---|
41 | |
---|
42 | def do_assign_reserve(self): |
---|
43 | reserve = adoDB_reserves.get_available_hosts(self.my_context.db,self.my_context['lab_id'], |
---|
44 | self.my_context['image_id']).first() |
---|
45 | if reserve: |
---|
46 | reserve.update_record(user_id=self.my_context['user_id'], |
---|
47 | is_assigned = True, |
---|
48 | assigned_init_time = datetime.now()) |
---|
49 | |
---|
50 | self.my_context.db.commit() |
---|
51 | |
---|
52 | equipo_reservado = {} |
---|
53 | equipo_reservado['id'] = reserve['pc_id'] |
---|
54 | equipo_reservado['name'] = reserve['pc_name'] |
---|
55 | equipo_reservado['ip'] = reserve['ip'] |
---|
56 | equipo_reservado['mac'] = reserve['mac'] |
---|
57 | equipo_reservado['protocol'] = reserve['protocol'] |
---|
58 | equipo_reservado['port'] = reserve['port'] |
---|
59 | equipo_reservado['ou_id'] = reserve['ou_id'] |
---|
60 | equipo_reservado['lab_id'] = reserve['lab_id'] |
---|
61 | |
---|
62 | #Por compatibilidad con el resto de la app |
---|
63 | #Que usa pc_id por claridad. |
---|
64 | equipo_reservado['pc_id'] = equipo_reservado['id'] |
---|
65 | |
---|
66 | |
---|
67 | return equipo_reservado |
---|
68 | else: |
---|
69 | return None |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | def do_reserve(self): |
---|
74 | print('reserve remote PC') |
---|
75 | equipo_reservado= None |
---|
76 | # Al hacer reserva se manda arrancar el equipo y se crea entrada en cola acciones para levantar el equipo |
---|
77 | # deseado. |
---|
78 | equipo_reservado = self.client.reserve_remote_pc() |
---|
79 | print("equipo_reservado en opengnsys") |
---|
80 | #### |
---|
81 | ## COMPROBAR FORMATO RESPUESTA VALIDA Y CHEQUEAR QUE CONTIENE TODOS LOS CAMPOS NECESARIOS |
---|
82 | ### |
---|
83 | if 'id' not in equipo_reservado: |
---|
84 | return {'error': '\n ' + errors.ERROR_RESERVA} |
---|
85 | else: |
---|
86 | if equipo_reservado: |
---|
87 | for k,v in equipo_reservado.items(): |
---|
88 | self.my_context[k] = v |
---|
89 | |
---|
90 | expiration_time = datetime.now() + timedelta(hours=int(self.my_context.maxtime)) |
---|
91 | #expiration_time = expiration_time.strftime('%d/%m/%Y %H:%M:%S') |
---|
92 | |
---|
93 | self.my_context.expiration_time = expiration_time |
---|
94 | |
---|
95 | self.my_context.reserved_init_time = datetime.now() |
---|
96 | self.my_context.assigned_init_time = datetime.now() |
---|
97 | self.my_context.is_assigned = True |
---|
98 | |
---|
99 | print('insert reserva') |
---|
100 | try: |
---|
101 | adoDB_reserves.insert(self.my_context) |
---|
102 | except: |
---|
103 | return {'error': '\n Error de reserva. No es posible registrar reserva en openrlabs'} |
---|
104 | |
---|
105 | self.client.update_context(self.my_context) |
---|
106 | |
---|
107 | print('eventos redirigidos') |
---|
108 | self.client.redirect_events() |
---|
109 | |
---|
110 | print('register timeout') |
---|
111 | self.client.register_session_timeout() |
---|
112 | |
---|
113 | |
---|
114 | ''' |
---|
115 | print('setup_task') |
---|
116 | gestor_de_reservas.setup_task(opengnsys.__OPENGNSYS_SERVER__, opengnsys.__APIKEY__, |
---|
117 | ou_id, lab_id, pc_id, ip, int(maxtime)) |
---|
118 | ''' |
---|
119 | # Devolvemos todos los datos necesarios para poder hacer conexion a |
---|
120 | # escritorio remoto. |
---|
121 | |
---|
122 | |
---|
123 | equipo_reservado['client_type'] = self.my_context.client_type |
---|
124 | equipo_reservado['protocol'] = self.my_context.protocol |
---|
125 | equipo_reservado['port'] = self.my_context.port |
---|
126 | equipo_reservado.update({'ou_id' : self.my_context.ou_id, 'ou' : None, |
---|
127 | 'lab_id': self.my_context.lab_id, 'lab' : None, |
---|
128 | 'num_retries' : self.my_context.num_retries}) |
---|
129 | |
---|
130 | #Por compatibilidad con el resto de la app |
---|
131 | #Que usa pc_id por claridad. |
---|
132 | equipo_reservado['pc_id'] = equipo_reservado['id'] |
---|
133 | |
---|
134 | return {'status': '\n Reserva realizada. Equipo concedido: ' + equipo_reservado['name'] + |
---|
135 | '\n Arrancando equipo: ../', |
---|
136 | 'equipo_reservado': equipo_reservado} |
---|
137 | else: |
---|
138 | return {'error': '\n Error de reserva.'} |
---|
139 | |
---|
140 | def check_pc_status(self): |
---|
141 | print('cheking..') |
---|
142 | ou_id = str(self.my_context.ou_id) |
---|
143 | lab_id = str(self.my_context.lab_id) |
---|
144 | pc_id = str(self.my_context.id) |
---|
145 | pc_name = str(self.my_context.name) |
---|
146 | ip = str(self.my_context.ip) |
---|
147 | protocol = str(self.my_context.protocol) |
---|
148 | port = str(self.my_context.port) |
---|
149 | client_type = str(self.my_context.client_type) |
---|
150 | |
---|
151 | num_retries = int(self.my_context.num_retries) |
---|
152 | |
---|
153 | if num_retries < self.MAX_RETRIES: |
---|
154 | |
---|
155 | #time.sleep( self.WAIT_CHECK_LOOP ) |
---|
156 | |
---|
157 | self.my_context.num_retries = str(num_retries + 1) |
---|
158 | |
---|
159 | pc_status = self.client.get_status_client() |
---|
160 | |
---|
161 | if 'status' not in pc_status: |
---|
162 | pc_status['status'] = 'error' |
---|
163 | |
---|
164 | if pc_status['status'] == 'off' or pc_status['status'] == 'oglive' \ |
---|
165 | or pc_status['status'] == 'busy' or pc_status['status'] == 'error': |
---|
166 | |
---|
167 | return {'status' : '.../', |
---|
168 | 'equipo_reservado' : self.my_context, |
---|
169 | 'wait_to_check' : self.WAIT_CHECK_LOOP} |
---|
170 | else: |
---|
171 | |
---|
172 | adoDB_reserves.set_is_running_true(self.my_context.db, pc_id) |
---|
173 | |
---|
174 | return {'ip': ip, 'protocol': protocol, 'port': port, |
---|
175 | 'pc_id': pc_id,'pc_name' : pc_name, |
---|
176 | 'ou_id': ou_id, 'lab_id': lab_id, |
---|
177 | 'finalizado': True, |
---|
178 | 'client_type': client_type, |
---|
179 | 'status' : '\n Conexión realizada con éxito.'} |
---|
180 | else: |
---|
181 | |
---|
182 | self.client.unreserve_remote_pc() |
---|
183 | |
---|
184 | return {'error': 'Fallo al encender el equipo de forma remota.\n Intentelo con otro equipo.'} |
---|
185 | |
---|
186 | |
---|
187 | |
---|