|
17 | 17 |
|
18 | 18 | from util import slugify, make_db_random_password |
19 | 19 | from util.models import BaseModel |
20 | | -from physical.models import DatabaseInfra, Environment |
| 20 | +from physical.models import DatabaseInfra, Environment, Offering |
21 | 21 | from drivers import factory_for |
22 | 22 | from system.models import Configuration |
23 | 23 | from account.models import Team |
@@ -1373,6 +1373,67 @@ def update_team_labels(self): |
1373 | 1373 | LOG.error(msg) |
1374 | 1374 | status = False |
1375 | 1375 | return status, msg |
| 1376 | + |
| 1377 | + def get_future_offering(self, resize_target): |
| 1378 | + LOG.info('Buscando Future Offering de %s para database %s -> Offer atual: %s', resize_target, self.name, self.infra.offering.name) |
| 1379 | + current_offer = self.infra.offering |
| 1380 | + environment = self.environment |
| 1381 | + |
| 1382 | + future_offer = None |
| 1383 | + |
| 1384 | + # Busca ofertas disponíveis para o environment |
| 1385 | + possible_offerings_environment = self.get_possible_future_offerings_for_environment(environment) |
| 1386 | + |
| 1387 | + if resize_target == 'cpu': |
| 1388 | + # Busca por offer > de CPU e >= de RAM |
| 1389 | + future_offer = self.get_next_offer_for_cpu(environment, current_offer, possible_offerings_environment) |
| 1390 | + |
| 1391 | + elif resize_target == 'ram': |
| 1392 | + # Busca por offer >= de CPU e > de RAM |
| 1393 | + future_offer = self.get_next_offer_for_ram(environment, current_offer, possible_offerings_environment) |
| 1394 | + |
| 1395 | + elif resize_target == 'cpu_ram': |
| 1396 | + # Busca por offer > de CPU e > de RAM |
| 1397 | + future_offer = self.get_next_offer_for_cpu_ram(environment, current_offer, possible_offerings_environment) |
| 1398 | + |
| 1399 | + LOG.info('Future Offering selecionada: %s', future_offer.name) |
| 1400 | + |
| 1401 | + return future_offer |
| 1402 | + |
| 1403 | + def get_possible_future_offerings_for_environment(self, environment): |
| 1404 | + # Busca na Configuration os nomes resumidos (ex: c2m2) das offerings disponíveis no DBaaS para auto upgrade |
| 1405 | + possible_offerings_names = Configuration.get_by_name_as_list( |
| 1406 | + 'allowed_future_offerings_names_auto_upgrade_vm' |
| 1407 | + ) |
| 1408 | + LOG.info('Possiveis offerings no DBaaS: %s', possible_offerings_names) |
| 1409 | + |
| 1410 | + possible_offerings_environment = [] |
| 1411 | + |
| 1412 | + # traz o nome real das Offerings, ja filtrando pelo environment |
| 1413 | + for possible_offer in possible_offerings_names: |
| 1414 | + offer_with_name = environment.offerings.filter(name__icontains=possible_offer).first() |
| 1415 | + if offer_with_name: |
| 1416 | + possible_offerings_environment.append(offer_with_name.name) |
| 1417 | + |
| 1418 | + return possible_offerings_environment |
| 1419 | + |
| 1420 | + def get_next_offer_for_cpu(self, environment, current_offer, possible_offerings_environment): |
| 1421 | + return environment.offerings.filter( |
| 1422 | + cpus__gt=current_offer.cpus, |
| 1423 | + memory_size_mb__gte=current_offer.memory_size_mb, |
| 1424 | + name__in=possible_offerings_environment).order_by('cpus', 'memory_size_mb').first() |
| 1425 | + |
| 1426 | + def get_next_offer_for_ram(self, environment, current_offer, possible_offerings_environment): |
| 1427 | + return environment.offerings.filter( |
| 1428 | + cpus__gte=current_offer.cpus, |
| 1429 | + memory_size_mb__gt=current_offer.memory_size_mb, |
| 1430 | + name__in=possible_offerings_environment).order_by('cpus', 'memory_size_mb').first() |
| 1431 | + |
| 1432 | + def get_next_offer_for_cpu_ram(self, environment, current_offer, possible_offerings_environment): |
| 1433 | + return environment.offerings.filter( |
| 1434 | + cpus__gt=current_offer.cpus, |
| 1435 | + memory_size_mb__gt=current_offer.memory_size_mb, |
| 1436 | + name__in=possible_offerings_environment).order_by('cpus', 'memory_size_mb').first() |
1376 | 1437 |
|
1377 | 1438 |
|
1378 | 1439 | class DatabaseLock(BaseModel): |
|
0 commit comments