Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Feb 15, 2024
1 parent 4a84264 commit a3872d8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 35 deletions.
4 changes: 2 additions & 2 deletions IM/ConfManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import time
import tempfile
import shutil
from distutils.version import LooseVersion
from packaging.version import Version

try:
from StringIO import StringIO
Expand Down Expand Up @@ -758,7 +758,7 @@ def get_vault_editor(vault_password):
"""
Get the correct VaultEditor object in different Ansible versions
"""
if LooseVersion(ansible_version) >= LooseVersion("2.4.0"):
if Version(ansible_version) >= Version("2.4.0"):
# for Ansible version 2.4.0 or higher
vault_secrets = [('default', VaultSecret(_bytes=to_bytes(vault_password)))]
return VaultEditor(VaultLib(vault_secrets))
Expand Down
6 changes: 3 additions & 3 deletions IM/ansible_utils/ansible_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import psutil
import signal
import logging
from distutils.version import LooseVersion
from packaging.version import Version
from collections import namedtuple
from ansible import errors
from ansible import __version__ as ansible_version
Expand Down Expand Up @@ -137,7 +137,7 @@ def run(self):
self._kill_childs()

def get_play_prereqs(self, options):
if LooseVersion(ansible_version) >= LooseVersion("2.4.0"):
if Version(ansible_version) >= Version("2.4.0"):
# for Ansible version 2.4.0 or higher
return self.get_play_prereqs_2_4(options)
else:
Expand Down Expand Up @@ -208,7 +208,7 @@ def version_info(ansible_version_string):
'revision': ansible_versions[2]}

def _gen_options(self):
if LooseVersion(ansible_version) >= LooseVersion("2.8.0"):
if Version(ansible_version) >= Version("2.8.0"):
from ansible.module_utils.common.collections import ImmutableDict
from ansible import context
context.CLIARGS = ImmutableDict(connection='ssh',
Expand Down
12 changes: 6 additions & 6 deletions IM/connectors/OpenNebula.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import os.path
import time
from distutils.version import LooseVersion
from packaging.version import Version
from IM.xmlobject import XMLObject
try:
from urlparse import urlparse
Expand Down Expand Up @@ -430,9 +430,9 @@ def create_security_groups(self, inf, radl, auth_data):
session_id = self.getSessionID(auth_data)

sgs = {}
one_ver = LooseVersion(self.getONEVersion(auth_data))
one_ver = Version(self.getONEVersion(auth_data))
# Security Groups appears in version 4.12.0
if one_ver >= LooseVersion("4.12.0"):
if one_ver >= Version("4.12.0"):
sgs = {}
i = 0
system = radl.systems[0]
Expand Down Expand Up @@ -571,7 +571,7 @@ def finalize(self, vm, last, auth_data):
if vm.id:
one_ver = self.getONEVersion(auth_data)
op = 'terminate'
if one_ver <= LooseVersion("4.14.0"):
if one_ver <= Version("4.14.0"):
op = 'delete'
success, err = server.one.vm.action(session_id, op, int(vm.id))[0:2]
else:
Expand All @@ -587,7 +587,7 @@ def finalize(self, vm, last, auth_data):
if last and success:
one_ver = self.getONEVersion(auth_data)
# Security Groups appears in version 4.12.0
if one_ver >= LooseVersion("4.12.0"):
if one_ver >= Version("4.12.0"):
self.delete_security_groups(vm.inf, auth_data)

return (success, err)
Expand Down Expand Up @@ -1181,7 +1181,7 @@ def create_snapshot(self, vm, disk_num, image_name, auto_delete, auth_data):

image_type = "" # Use the default one
one_ver = self.getONEVersion(auth_data)
if one_ver >= LooseVersion("5.0"):
if one_ver >= Version("5.0"):
success, res_info = server.one.vm.disksaveas(session_id, int(vm.id), disk_num,
image_name, image_type, -1)[0:2]
else:
Expand Down
46 changes: 23 additions & 23 deletions IM/tosca/Tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _get_placement_property(self, sys_name, prop):
sys_name))
return policy.properties[prop]
else:
Tosca.logger.warn("Policy %s not supported. Ignoring it." % policy.type_definition.type)
Tosca.logger.warning("Policy %s not supported. Ignoring it." % policy.type_definition.type)

return None

Expand Down Expand Up @@ -135,7 +135,7 @@ def to_radl(self, inf_info=None):
elif token_type == "private_key":
ansible_host.setValue("credentials.private_key", token)
else:
Tosca.logger.warn("Unknown tyoe of token %s. Ignoring." % token_type)
Tosca.logger.warning("Unknown tyoe of token %s. Ignoring." % token_type)
radl.ansible_hosts = [ansible_host]
elif root_type == "tosca.nodes.aisprint.FaaS.Function":
min_instances, _, default_instances, count, removal_list = self._get_scalable_properties(node)
Expand Down Expand Up @@ -260,7 +260,7 @@ def to_radl(self, inf_info=None):
# Select the host to host this element
compute = self._find_host_node(node, self.tosca.nodetemplates)
if not compute:
Tosca.logger.warn(
Tosca.logger.warning(
"Node %s has not compute node to host in." % node.name)

interfaces = Tosca._get_interfaces(node)
Expand Down Expand Up @@ -655,7 +655,7 @@ def _add_node_nets(self, node, radl, system, nodetemplates):
else:
# There are no a private IP, net the provider_id to the priv net
if not public_net:
Tosca.logger.warn("Node %s does not require any IP!!" % node.name)
Tosca.logger.warning("Node %s does not require any IP!!" % node.name)

if public_net:
if pool_name:
Expand Down Expand Up @@ -928,7 +928,7 @@ def _remove_recipe_header(script_content):
try:
yamlo = yaml.safe_load(script_content)
if not isinstance(yamlo, list):
Tosca.logger.warn("Error parsing YAML: " + script_content + "\n.Do not remove header.")
Tosca.logger.warning("Error parsing YAML: " + script_content + "\n.Do not remove header.")
return script_content
except Exception:
Tosca.logger.exception("Error parsing YAML: " + script_content + "\n.Do not remove header.")
Expand Down Expand Up @@ -1033,11 +1033,11 @@ def _get_intrinsic_value(self, func, node, inf_info):
"Incorrect substring_index in function token.")
return None
else:
Tosca.logger.warn(
Tosca.logger.warning(
"Intrinsic function token must receive 3 parameters.")
return None
else:
Tosca.logger.warn(
Tosca.logger.warning(
"Intrinsic function %s not supported." % func_name)
return None

Expand Down Expand Up @@ -1214,7 +1214,7 @@ def _get_attribute_result(self, func, node, inf_info):
vm_list = inf_info.get_vm_list_by_system_name()

if host_node.name not in vm_list:
Tosca.logger.warn("There are no VM associated with the name %s." % host_node.name)
Tosca.logger.warning("There are no VM associated with the name %s." % host_node.name)
return None
else:
# As default assume that there will be only one VM per group
Expand All @@ -1230,14 +1230,14 @@ def _get_attribute_result(self, func, node, inf_info):
if node.type == "tosca.nodes.indigo.Compute":
return vm.cont_out
else:
Tosca.logger.warn("Attribute ctxt_log only supported"
Tosca.logger.warning("Attribute ctxt_log only supported"
" in tosca.nodes.indigo.Compute nodes.")
return None
elif attribute_name == "ansible_output":
if node.type == "tosca.nodes.indigo.Compute":
return self._get_ansible_output(vm.cont_out, attribute_params)
else:
Tosca.logger.warn("Attribute ansible_output only supported"
Tosca.logger.warning("Attribute ansible_output only supported"
" in tosca.nodes.indigo.Compute nodes.")
return None
elif attribute_name == "credential" and capability_name == "endpoint":
Expand All @@ -1257,7 +1257,7 @@ def _get_attribute_result(self, func, node, inf_info):
res = res[index]
return res
else:
Tosca.logger.warn("Attribute credential of capability endpoint only"
Tosca.logger.warning("Attribute credential of capability endpoint only"
" supported in tosca.nodes.indigo.Compute nodes.")
return None
elif attribute_name == "private_address":
Expand Down Expand Up @@ -1308,7 +1308,7 @@ def _get_attribute_result(self, func, node, inf_info):
# OSCAR function deployed in a pre-deployed cluster or not dns_host set
return vm.getCloudConnector().cloud.get_url()

Tosca.logger.warn("Attribute endpoint only supported in tosca.nodes.aisprint.FaaS.Function")
Tosca.logger.warning("Attribute endpoint only supported in tosca.nodes.aisprint.FaaS.Function")
return None
elif attribute_name == "credential":
if root_type == "tosca.nodes.aisprint.FaaS.Function":
Expand All @@ -1327,7 +1327,7 @@ def _get_attribute_result(self, func, node, inf_info):
"token": oscar_pass},
attribute_params)

Tosca.logger.warn("No password defined in tosca.nodes.indigo.OSCAR host node")
Tosca.logger.warning("No password defined in tosca.nodes.indigo.OSCAR host node")
return None
else:
# OSCAR function deployed in a pre-deployed cluster or not dns_host set
Expand All @@ -1344,16 +1344,16 @@ def _get_attribute_result(self, func, node, inf_info):
"token": auth["token"]},
attribute_params)
else:
Tosca.logger.warn("No valid auth data in OSCAR connector")
Tosca.logger.warning("No valid auth data in OSCAR connector")
return None

Tosca.logger.warn("No auth data in OSCAR connector")
Tosca.logger.warning("No auth data in OSCAR connector")
return None

Tosca.logger.warn("Attribute credential only supported in tosca.nodes.aisprint.FaaS.Function")
Tosca.logger.warning("Attribute credential only supported in tosca.nodes.aisprint.FaaS.Function")
return None
else:
Tosca.logger.warn("Attribute %s not supported." % attribute_name)
Tosca.logger.warning("Attribute %s not supported." % attribute_name)
return None
else:
if attribute_name == "tosca_id":
Expand Down Expand Up @@ -1411,10 +1411,10 @@ def _get_attribute_result(self, func, node, inf_info):
if dns_host.strip("'\""):
return "https://%s" % dns_host

Tosca.logger.warn("Attribute endpoint only supported in tosca.nodes.aisprint.FaaS.Function")
Tosca.logger.warning("Attribute endpoint only supported in tosca.nodes.aisprint.FaaS.Function")
return None
else:
Tosca.logger.warn("Attribute %s not supported." % attribute_name)
Tosca.logger.warning("Attribute %s not supported." % attribute_name)
return None

def _final_function_result(self, func, node, inf_info=None):
Expand Down Expand Up @@ -1542,7 +1542,7 @@ def _node_fulfill_filter(self, node, node_filter):
elif op == "valid_values":
comparation = node_value in filter_value
else:
Tosca.logger.warn("Logical operator %s not supported." % op)
Tosca.logger.warning("Logical operator %s not supported." % op)

if not comparation:
return False
Expand Down Expand Up @@ -1788,7 +1788,7 @@ def _gen_system(self, node, nodetemplates):
feature = Feature("disk.0.os.credentials.public_key", "=", token)
res.addFeature(feature)
else:
Tosca.logger.warn("Unknown tyoe of token %s. Ignoring." % token_type)
Tosca.logger.warning("Unknown tyoe of token %s. Ignoring." % token_type)
if 'user' not in value or not value['user']:
raise Exception("User must be specified in the image credentials.")
name = "disk.0.os.credentials.username"
Expand Down Expand Up @@ -2069,7 +2069,7 @@ def _gen_oscar_system(self, node):
res.setValue("expose.%s" % elem, value)
else:
# this should never happen
Tosca.logger.warn("Property %s not expected. Ignoring." % prop.name)
Tosca.logger.warning("Property %s not expected. Ignoring." % prop.name)

if node.requirements:
deps = []
Expand Down Expand Up @@ -2114,7 +2114,7 @@ def _get_oscar_service_json(self, node):
res['image_pull_secrets'] = value
else:
# this should never happen
Tosca.logger.warn("Property %s not expected. Ignoring." % prop.name)
Tosca.logger.warning("Property %s not expected. Ignoring." % prop.name)

return res

Expand Down
3 changes: 2 additions & 1 deletion test/unit/REST.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
def read_file_as_bytes(file_name):
tests_path = os.path.dirname(os.path.abspath(__file__))
abs_file_path = os.path.join(tests_path, file_name)
return BytesIO(open(abs_file_path, 'r').read().encode())
with open(abs_file_path, 'r') as f:
return BytesIO(f.read().encode())


class TestREST(unittest.TestCase):
Expand Down

0 comments on commit a3872d8

Please sign in to comment.