From 78738e8175c99741fd33e779ae8d80279191358c Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 00:16:28 +0200 Subject: [PATCH 01/14] Change cores to SerializerMethodField and obtain it from cpu --- opennebula_api/serializers.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index c7418aa5..ec7bd5d5 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -46,7 +46,7 @@ class VirtualMachineSerializer(serializers.Serializer): """Serializer to map the virtual machine instance into JSON format.""" name = serializers.SerializerMethodField() - cores = serializers.IntegerField(source='template.vcpu') + cores = serializers.SerializerMethodField() disk = serializers.IntegerField(write_only=True) set_memory = serializers.IntegerField(write_only=True, label='Memory') memory = serializers.SerializerMethodField() @@ -99,6 +99,9 @@ class VirtualMachineSerializer(serializers.Serializer): return manager.get_vm(opennebula_id) + def get_cores(self, obj): + return (obj.template.cpu) * 10 + def get_memory(self, obj): return int(obj.template.memory) / 1024 From 316496b9694d589d34dd102708a9480afb5302b6 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 00:22:39 +0200 Subject: [PATCH 02/14] More fix for vcpu --- opennebula_api/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index ec7bd5d5..98162d1f 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -130,7 +130,7 @@ class VirtualMachineSerializer(serializers.Serializer): def get_price(self, obj): template = obj.template - price = float(template.vcpu) * 5.0 + price = float(template.cpu * 10) * 5.0 price += (int(template.memory) / 1024 * 2.0) for disk in template.disks: price += int(disk.size) / 1024 * 0.6 From 9f58fdd4561820cc76f9a610879ee1b53159eed1 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 00:26:59 +0200 Subject: [PATCH 03/14] Wrong multiplication to string instead of number --- opennebula_api/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 98162d1f..1fa1a802 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -130,7 +130,7 @@ class VirtualMachineSerializer(serializers.Serializer): def get_price(self, obj): template = obj.template - price = float(template.cpu * 10) * 5.0 + price = float(template.cpu) * 10 * 5.0 price += (int(template.memory) / 1024 * 2.0) for disk in template.disks: price += int(disk.size) / 1024 * 0.6 From 85b251d2e6a9c984360e56da6ce8386ef6303347 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 07:40:06 +0200 Subject: [PATCH 04/14] Check if the template VM serializer object has template attr --- opennebula_api/serializers.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 1fa1a802..3d451538 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -137,12 +137,15 @@ class VirtualMachineSerializer(serializers.Serializer): return price def get_configuration(self, obj): - template_id = obj.template.template_id - template = OpenNebulaManager().get_template(template_id) - if template.name.startswith('public-'): - return template.name.lstrip('public-') + if hasattr(obj, 'template'): + template_id = obj.template.template_id + template = OpenNebulaManager().get_template(template_id) + if template.name.startswith('public-'): + return template.name.lstrip('public-') + else: + return template.name else: - return template.name + return "" def get_ipv4(self, obj): """ From 6a5a5ed1609abe5de0c60111e8b6c86450f88ad6 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 07:43:06 +0200 Subject: [PATCH 05/14] Check template_id attr exists within template --- opennebula_api/serializers.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 3d451538..69c593fb 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -138,12 +138,13 @@ class VirtualMachineSerializer(serializers.Serializer): def get_configuration(self, obj): if hasattr(obj, 'template'): - template_id = obj.template.template_id - template = OpenNebulaManager().get_template(template_id) - if template.name.startswith('public-'): - return template.name.lstrip('public-') - else: - return template.name + if hasattr(obj, 'template.template_id'): + template_id = obj.template.template_id + template = OpenNebulaManager().get_template(template_id) + if template.name.startswith('public-'): + return template.name.lstrip('public-') + else: + return template.name else: return "" From fa018739c9df10c5bdea3407a7837cef04ea1ab2 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 07:47:36 +0200 Subject: [PATCH 06/14] Change cores to float before multiplication --- opennebula_api/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 69c593fb..e0f635f4 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -100,7 +100,7 @@ class VirtualMachineSerializer(serializers.Serializer): return manager.get_vm(opennebula_id) def get_cores(self, obj): - return (obj.template.cpu) * 10 + return float(obj.template.cpu) * 10 def get_memory(self, obj): return int(obj.template.memory) / 1024 From 5b7d1301cf3cb0628cb9aa6bfca04d0e954afa30 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 08:56:10 +0200 Subject: [PATCH 07/14] Return cores as int --- opennebula_api/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index e0f635f4..3a77c0d8 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -100,7 +100,7 @@ class VirtualMachineSerializer(serializers.Serializer): return manager.get_vm(opennebula_id) def get_cores(self, obj): - return float(obj.template.cpu) * 10 + return int(float(obj.template.cpu) * 10) def get_memory(self, obj): return int(obj.template.memory) / 1024 From a8caa035c090713fdbf5b4c5bf2c5232600c7a4e Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 09:12:09 +0200 Subject: [PATCH 08/14] Set configuration to empty string if not available --- utils/hosting_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/utils/hosting_utils.py b/utils/hosting_utils.py index ec97a320..ee65fccf 100644 --- a/utils/hosting_utils.py +++ b/utils/hosting_utils.py @@ -47,7 +47,8 @@ def get_or_create_vm_detail(user, manager, vm_id): vm_detail_obj = VMDetail.objects.create( user=user, vm_id=vm_id, disk_size=vm['disk_size'], cores=vm['cores'], memory=vm['memory'], - configuration=vm['configuration'], ipv4=vm['ipv4'], + configuration="" if vm['configuration'] is None else vm['configuration'], + ipv4=vm['ipv4'], ipv6=vm['ipv6'] ) return vm_detail_obj From 3f75fac1581dd28d17263322c67855813cada708 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 11 Apr 2019 01:05:34 +0200 Subject: [PATCH 09/14] Make use of the infoextended method where needed --- opennebula_api/models.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/opennebula_api/models.py b/opennebula_api/models.py index adc39bf0..a348cf86 100644 --- a/opennebula_api/models.py +++ b/opennebula_api/models.py @@ -168,10 +168,21 @@ class OpenNebulaManager(): raise return user_pool - def _get_vm_pool(self): + def _get_vm_pool(self, vm_id=None, infoextended=True): + """ + vm_id: int - the id of the VM that needs to looked up in the vm pool; + when set to None, looks for everything in the infoextended + """ try: vm_pool = oca.VirtualMachinePool(self.client) - vm_pool.info() + if infoextended: + vm_pool.infoextended( + filter_key_value_str='ID={vm_id}'.format(vm_id=vm_id) if + vm_id is not None else '', + vm_state=3 # look for VMs only in ACTIVE state + ) + else: + vm_pool.info() return vm_pool except AttributeError: logger.error('Could not connect via client, using oneadmin instead') @@ -202,7 +213,7 @@ class OpenNebulaManager(): def get_vm(self, vm_id): vm_id = int(vm_id) try: - vm_pool = self._get_vm_pool() + vm_pool = self._get_vm_pool(vm_id) return vm_pool.get_by_id(vm_id) except WrongIdError: raise WrongIdError From 5f8cc2199bea22f6b525a0b517396e64801c9163 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 11 Apr 2019 01:39:06 +0200 Subject: [PATCH 10/14] Change vm_pool infoextended to retrieve VMs in any state except DONE --- datacenterlight/tasks.py | 2 ++ opennebula_api/models.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/datacenterlight/tasks.py b/datacenterlight/tasks.py index 2779f79b..5f12b7df 100644 --- a/datacenterlight/tasks.py +++ b/datacenterlight/tasks.py @@ -245,6 +245,8 @@ def create_vm_task(self, vm_template_id, user, specs, template, order_id): } email = EmailMessage(**email_data) email.send() + else: + logger.debug("VM's ipv6 is None. Hence not created VMDetail") except Exception as e: logger.error(str(e)) try: diff --git a/opennebula_api/models.py b/opennebula_api/models.py index a348cf86..73dc4405 100644 --- a/opennebula_api/models.py +++ b/opennebula_api/models.py @@ -179,7 +179,7 @@ class OpenNebulaManager(): vm_pool.infoextended( filter_key_value_str='ID={vm_id}'.format(vm_id=vm_id) if vm_id is not None else '', - vm_state=3 # look for VMs only in ACTIVE state + vm_state=-1 # Look for VMs in any state, except DONE ) else: vm_pool.info() From 7f9ada60ee355e5dd91653908e74b2e059a1f490 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 11 Apr 2019 01:58:51 +0200 Subject: [PATCH 11/14] Revert back to old serializer code --- opennebula_api/serializers.py | 21 +++++++-------------- utils/hosting_utils.py | 3 +-- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 3a77c0d8..c7418aa5 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -46,7 +46,7 @@ class VirtualMachineSerializer(serializers.Serializer): """Serializer to map the virtual machine instance into JSON format.""" name = serializers.SerializerMethodField() - cores = serializers.SerializerMethodField() + cores = serializers.IntegerField(source='template.vcpu') disk = serializers.IntegerField(write_only=True) set_memory = serializers.IntegerField(write_only=True, label='Memory') memory = serializers.SerializerMethodField() @@ -99,9 +99,6 @@ class VirtualMachineSerializer(serializers.Serializer): return manager.get_vm(opennebula_id) - def get_cores(self, obj): - return int(float(obj.template.cpu) * 10) - def get_memory(self, obj): return int(obj.template.memory) / 1024 @@ -130,23 +127,19 @@ class VirtualMachineSerializer(serializers.Serializer): def get_price(self, obj): template = obj.template - price = float(template.cpu) * 10 * 5.0 + price = float(template.vcpu) * 5.0 price += (int(template.memory) / 1024 * 2.0) for disk in template.disks: price += int(disk.size) / 1024 * 0.6 return price def get_configuration(self, obj): - if hasattr(obj, 'template'): - if hasattr(obj, 'template.template_id'): - template_id = obj.template.template_id - template = OpenNebulaManager().get_template(template_id) - if template.name.startswith('public-'): - return template.name.lstrip('public-') - else: - return template.name + template_id = obj.template.template_id + template = OpenNebulaManager().get_template(template_id) + if template.name.startswith('public-'): + return template.name.lstrip('public-') else: - return "" + return template.name def get_ipv4(self, obj): """ diff --git a/utils/hosting_utils.py b/utils/hosting_utils.py index ee65fccf..ec97a320 100644 --- a/utils/hosting_utils.py +++ b/utils/hosting_utils.py @@ -47,8 +47,7 @@ def get_or_create_vm_detail(user, manager, vm_id): vm_detail_obj = VMDetail.objects.create( user=user, vm_id=vm_id, disk_size=vm['disk_size'], cores=vm['cores'], memory=vm['memory'], - configuration="" if vm['configuration'] is None else vm['configuration'], - ipv4=vm['ipv4'], + configuration=vm['configuration'], ipv4=vm['ipv4'], ipv6=vm['ipv6'] ) return vm_detail_obj From f8458f369db4d929dd28643762251ca41e0b10c0 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 11 Apr 2019 02:12:24 +0200 Subject: [PATCH 12/14] Update Changelog 2.5 --- Changelog | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Changelog b/Changelog index 96108b80..780168a7 100644 --- a/Changelog +++ b/Changelog @@ -1,3 +1,6 @@ +2.5: 2019-04-11 + * #6589: [vm creation, listing] Fix obtaining ip address/template related info from oca api call (MR!690, MR!691 and MR!692) + [python-oca] VirtualMachinePool infoextended (https://github.com/ungleich/python-oca/pull/3) 2.4.10: 2019-02-14 * #6451: [dg, gdpr banner] Update phone number (MR!688 by pcoder) 2.4.9: 2019-02-01 From 4cf7b93ee20b29feb450fcc89202c26dd5b56827 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 13 Apr 2019 12:02:48 +0200 Subject: [PATCH 13/14] Create logs directory --- logs/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 logs/.keep diff --git a/logs/.keep b/logs/.keep new file mode 100644 index 00000000..e69de29b From f50af4f2aaabe7efef12b332b55e15c9c5b5ff63 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 13 Apr 2019 12:04:18 +0200 Subject: [PATCH 14/14] Improve log formatting --- dynamicweb/settings/base.py | 14 +++++++++++--- logs/{.keep => .gitkeep} | 0 2 files changed, 11 insertions(+), 3 deletions(-) rename logs/{.keep => .gitkeep} (100%) diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index 49570143..324de04d 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -685,18 +685,26 @@ if ENABLE_LOGGING: custom_handler_item = { 'custom_file': { 'level': LOG_LEVEL, - 'class': 'logging.FileHandler', + 'class': 'logging.handlers.RotatingFileHandler', 'filename': - "{PROJECT_DIR}/{LEVEL}.log".format( + "{PROJECT_DIR}/logs/{LEVEL}.log".format( LEVEL=LOG_LEVEL.lower(), PROJECT_DIR=PROJECT_DIR - ) + ), + 'maxBytes': 1024 * 1024 * 5, + 'backupCount': 10, + 'formatter': 'standard', } } handlers_dict.update(custom_handler_item) LOGGING = { 'version': 1, 'disable_existing_loggers': False, + 'formatters': { + 'standard': { + 'format': '%(asctime)s %(levelname)s %(name)s: %(message)s' + } + }, 'handlers': handlers_dict, 'loggers': loggers_dict } diff --git a/logs/.keep b/logs/.gitkeep similarity index 100% rename from logs/.keep rename to logs/.gitkeep