diff --git a/IM/tosca/Tosca.py b/IM/tosca/Tosca.py index 21d79f0c6..9f7b426fe 100644 --- a/IM/tosca/Tosca.py +++ b/IM/tosca/Tosca.py @@ -36,6 +36,7 @@ class Tosca: ARTIFACTS_PATH = os.path.dirname(os.path.realpath(__file__)) + "/tosca-types/artifacts" ARTIFACTS_REMOTE_REPO = "https://raw.githubusercontent.com/indigo-dc/tosca-types/master/artifacts/" + GET_TIMEOUT = 20 logger = logging.getLogger('InfrastructureManager') @@ -796,7 +797,7 @@ def _gen_configure_from_interfaces(self, node, compute, interfaces): if implementation_url[0] in ['http', 'https', 'ftp']: script_path = implementation_url[2] try: - resp = requests.get(implementation) + resp = requests.get(implementation, timeout=self.GET_TIMEOUT) script_content = resp.text if resp.status_code != 200: raise Exception(resp.reason + "\n" + resp.text) @@ -814,7 +815,7 @@ def _gen_configure_from_interfaces(self, node, compute, interfaces): f.close() else: try: - resp = requests.get(Tosca.ARTIFACTS_REMOTE_REPO + implementation) + resp = requests.get(Tosca.ARTIFACTS_REMOTE_REPO + implementation, timeout=self.GET_TIMEOUT) script_content = resp.text if resp.status_code != 200: raise Exception(resp.reason + "\n" + resp.text) diff --git a/scripts/db_1_14_X_to_1_15_X.py b/scripts/db_1_14_X_to_1_15_X.py index fa00230da..a12e082d8 100644 --- a/scripts/db_1_14_X_to_1_15_X.py +++ b/scripts/db_1_14_X_to_1_15_X.py @@ -15,16 +15,53 @@ # along with this program. If not, see . import sys +import json sys.path.append("..") sys.path.append(".") from IM.InfrastructureList import InfrastructureList +from IM.InfrastructureInfo import InfrastructureInfo from IM.db import DataBase +from IM.auth import Authentication + + +def get_data_from_db(db, inf_id): + """ + Get data from DB. + """ + + if db.db_type == DataBase.MONGO: + res = db.find("inf_list", {"id": inf_id}, {"data": True}) + else: + res = db.select("select data from inf_list where id = %s", (inf_id,)) + + if len(res) > 0: + elem = res[0] + if db.db_type == DataBase.MONGO: + data = elem[data] + else: + data = elem[0] + try: + newinf = InfrastructureInfo() + dic = json.loads(data) + newinf.deleted = dic['deleted'] + newinf.id = dic['id'] + if dic['auth']: + newinf.auth = Authentication.deserialize(dic['auth']) + return newinf + except Exception: + print("ERROR reading infrastructure from database, ignoring it!.") + else: + return None if __name__ == "__main__": - DATA_DB= "sqlite:///etc/im/inf.dat" + if len(sys.argv) != 2: + sys.stderr.write("Usage: %s \n" % sys.argv[0]) + sys.exit(-1) + + DATA_DB = sys.argv[1] db = DataBase(DATA_DB) if db.connect(): @@ -34,15 +71,19 @@ db.execute("ALTER TABLE `inf_list` ADD COLUMN `auth` BLOB;") infs = [] - for inf_id in InfrastructureList._get_inf_ids_from_db(): - res = InfrastructureList._get_data_from_db(DATA_DB, inf_id, True) - if res: - auth = res[inf_id].auth.serialize() - res = db.execute("UPDATE `inf_list` SET `auth` = %s WHERE id = %s", (auth, inf_id)) - - db.close() + for inf_id in InfrastructureList._get_inf_ids_from_db(get_all=True): + try: + inf = get_data_from_db(db, inf_id) + print(inf_id) + if inf: + auth = inf.auth.serialize() + res = db.execute("UPDATE `inf_list` SET `auth` = %s WHERE id = %s", (auth, inf_id)) + except Exception as e: + sys.stderr.write("Error updating auth field in Inf ID: %s. Ignoring.\n" % inf_id) else: sys.stdout.write("There are no inf_list table. Do not need to update.") + + db.close() else: sys.stderr.write("Error connecting with DB: %s\n" % DATA_DB) sys.exit(-1) diff --git a/test/files/tosca_simple.yml b/test/files/tosca_simple.yml new file mode 100644 index 000000000..1310a412e --- /dev/null +++ b/test/files/tosca_simple.yml @@ -0,0 +1,25 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +description: TOSCA test for the IM + +topology_template: + + node_templates: + + server: + type: tosca.nodes.Compute + capabilities: + # Host container properties + host: + properties: + num_cpus: 1 + mem_size: 4 GB + os: + properties: + type: linux + distribution: ubuntu + version: 18.04 + + outputs: + server: + value: { get_attribute: [ server, public_address ] } \ No newline at end of file diff --git a/test/unit/REST.py b/test/unit/REST.py index 5694abdfd..dcd1184c7 100755 --- a/test/unit/REST.py +++ b/test/unit/REST.py @@ -30,6 +30,7 @@ sys.path.append("..") sys.path.append(".") +from IM.tosca.Tosca import Tosca from IM.config import Config from IM import __version__ as version from IM.InfrastructureManager import (DeletedInfrastructureException, @@ -304,19 +305,20 @@ def test_CreateInfrastructure(self, bottle_request, get_infrastructure, CreateIn "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; " "username = user; password = pass"), "Content-Type": "text/yaml"} - bottle_request.body = read_file_as_bytes("../files/tosca_create.yml") + bottle_request.body = read_file_as_bytes("../files/tosca_simple.yml") CreateInfrastructure.return_value = "1" res = RESTCreateInfrastructure() self.assertEqual(res, "http://imserver.com/infrastructures/1") - bottle_request.body = read_file_as_bytes("../files/tosca_create.yml") + bottle_request.headers["Content-Type"] = "application/json" + bottle_request.body = read_file_as_bytes("../files/test_simple.json") CreateInfrastructure.side_effect = InvaliddUserException() res = RESTCreateInfrastructure() self.assertEqual(res, "Error Getting Inf. info: Invalid InfrastructureManager credentials") - bottle_request.body = read_file_as_bytes("../files/tosca_create.yml") + bottle_request.body = read_file_as_bytes("../files/test_simple.json") CreateInfrastructure.side_effect = UnauthorizedUserException() res = RESTCreateInfrastructure() self.assertEqual(res, "Error Creating Inf.: Access to this infrastructure not granted.") @@ -448,22 +450,23 @@ def test_AddResource(self, bottle_request, get_infrastructure, AddResource): "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; " "username = user; password = pass"), "Content-Type": "text/yaml"} - bottle_request.body = read_file_as_bytes("../files/tosca_create.yml") + bottle_request.body = read_file_as_bytes("../files/tosca_simple.yml") res = RESTAddResource("1") self.assertEqual(res, "http://imserver.com/infrastructures/1/vms/1") - bottle_request.body = read_file_as_bytes("../files/tosca_create.yml") + bottle_request.headers["Content-Type"] = "application/json" + bottle_request.body = read_file_as_bytes("../files/test_simple.json") AddResource.side_effect = DeletedInfrastructureException() res = RESTAddResource("1") self.assertEqual(res, "Error Adding resources: Deleted infrastructure.") - bottle_request.body = read_file_as_bytes("../files/tosca_create.yml") + bottle_request.body = read_file_as_bytes("../files/test_simple.json") AddResource.side_effect = IncorrectInfrastructureException() res = RESTAddResource("1") self.assertEqual(res, "Error Adding resources: Invalid infrastructure ID or access not granted.") - bottle_request.body = read_file_as_bytes("../files/tosca_create.yml") + bottle_request.body = read_file_as_bytes("../files/test_simple.json") AddResource.side_effect = UnauthorizedUserException() res = RESTAddResource("1") self.assertEqual(res, "Error Adding resources: Access to this infrastructure not granted.") @@ -523,7 +526,7 @@ def test_AlterVM(self, bottle_request, AlterVM): "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; " "username = user; password = pass"), "Content-Type": "text/yaml"} - bottle_request.body = read_file_as_bytes("../files/tosca_create.yml") + bottle_request.body = read_file_as_bytes("../files/tosca_simple.yml") res = RESTAlterVM("1", "1") self.assertEqual(res, "vm_info")